-
Notifications
You must be signed in to change notification settings - Fork 282
fix: add explicit sort for window aggregates to fix correctness issues #3397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andygrove
wants to merge
2
commits into
apache:main
Choose a base branch
from
andygrove:fix-window-agg-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1583,9 +1583,22 @@ impl PhysicalPlanner { | |
| }) | ||
| .collect(); | ||
|
|
||
| // Ensure input is properly sorted when ORDER BY is present | ||
| // BoundedWindowAggExec requires InputOrderMode::Sorted | ||
| let needs_explicit_sort = !sort_exprs.is_empty(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
can we just make a simple |
||
| let sorted_child: Arc<dyn ExecutionPlan> = if needs_explicit_sort { | ||
| // Insert explicit sort to ensure data ordering | ||
| Arc::new(SortExec::new( | ||
| LexOrdering::new(sort_exprs.to_vec()).unwrap(), | ||
| Arc::clone(&child.native_plan), | ||
| )) | ||
| } else { | ||
| Arc::clone(&child.native_plan) | ||
| }; | ||
|
|
||
| let window_agg = Arc::new(BoundedWindowAggExec::try_new( | ||
| window_expr?, | ||
| Arc::clone(&child.native_plan), | ||
| sorted_child, | ||
| InputOrderMode::Sorted, | ||
| !partition_exprs.is_empty(), | ||
| )?); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ package org.apache.spark.sql.comet | |
|
|
||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, AttributeSet, CurrentRow, Expression, NamedExpression, RangeFrame, RowFrame, SortOrder, SpecifiedWindowFrame, UnboundedFollowing, UnboundedPreceding, WindowExpression} | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeSet, CurrentRow, Expression, NamedExpression, RangeFrame, RowFrame, SortOrder, SpecifiedWindowFrame, UnboundedFollowing, UnboundedPreceding, WindowExpression} | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, Complete, Count, Max, Min, Sum} | ||
| import org.apache.spark.sql.catalyst.plans.physical.Partitioning | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
|
|
@@ -34,7 +34,7 @@ import com.google.common.base.Objects | |
|
|
||
| import org.apache.comet.{CometConf, ConfigEntry} | ||
| import org.apache.comet.CometSparkSessionExtensions.withInfo | ||
| import org.apache.comet.serde.{AggSerde, CometOperatorSerde, Incompatible, OperatorOuterClass, SupportLevel} | ||
| import org.apache.comet.serde.{AggSerde, CometOperatorSerde, Compatible, OperatorOuterClass, SupportLevel, Unsupported} | ||
| import org.apache.comet.serde.OperatorOuterClass.Operator | ||
| import org.apache.comet.serde.QueryPlanSerde.{aggExprToProto, exprToProto} | ||
|
|
||
|
|
@@ -44,7 +44,17 @@ object CometWindowExec extends CometOperatorSerde[WindowExec] { | |
| CometConf.COMET_EXEC_WINDOW_ENABLED) | ||
|
|
||
| override def getSupportLevel(op: WindowExec): SupportLevel = { | ||
| Incompatible(Some("Native WindowExec has known correctness issues")) | ||
| // DataFusion requires that partition expressions must be part of the sort ordering. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if its true, need to check |
||
| // If partition spec and order spec use different columns, we need to fall back to Spark. | ||
| if (op.partitionSpec.nonEmpty && op.orderSpec.nonEmpty) { | ||
| val orderExprs = op.orderSpec.map(_.child).toSet | ||
| val partitionExprsInOrder = op.partitionSpec.forall(orderExprs.contains) | ||
| if (!partitionExprsInOrder) { | ||
| return Unsupported( | ||
| Some("Partition expressions must be a subset of order expressions for native window")) | ||
| } | ||
| } | ||
| Compatible() | ||
| } | ||
|
|
||
| override def convert( | ||
|
|
@@ -72,11 +82,6 @@ object CometWindowExec extends CometOperatorSerde[WindowExec] { | |
| return None | ||
| } | ||
|
|
||
| if (op.partitionSpec.nonEmpty && op.orderSpec.nonEmpty && | ||
| !validatePartitionAndSortSpecsForWindowFunc(op.partitionSpec, op.orderSpec, op)) { | ||
| return None | ||
| } | ||
|
|
||
| val windowExprProto = winExprs.map(windowExprToProto(_, output, op.conf)) | ||
| val partitionExprs = op.partitionSpec.map(exprToProto(_, op.child.output)) | ||
|
|
||
|
|
@@ -279,40 +284,6 @@ object CometWindowExec extends CometOperatorSerde[WindowExec] { | |
| SerializedPlan(None)) | ||
| } | ||
|
|
||
| private def validatePartitionAndSortSpecsForWindowFunc( | ||
| partitionSpec: Seq[Expression], | ||
| orderSpec: Seq[SortOrder], | ||
| op: SparkPlan): Boolean = { | ||
| if (partitionSpec.length != orderSpec.length) { | ||
| return false | ||
| } | ||
|
|
||
| val partitionColumnNames = partitionSpec.collect { | ||
| case a: AttributeReference => a.name | ||
| case other => | ||
| withInfo(op, s"Unsupported partition expression: ${other.getClass.getSimpleName}") | ||
| return false | ||
| } | ||
|
|
||
| val orderColumnNames = orderSpec.collect { case s: SortOrder => | ||
| s.child match { | ||
| case a: AttributeReference => a.name | ||
| case other => | ||
| withInfo(op, s"Unsupported sort expression: ${other.getClass.getSimpleName}") | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| if (partitionColumnNames.zip(orderColumnNames).exists { case (partCol, orderCol) => | ||
| partCol != orderCol | ||
| }) { | ||
| withInfo(op, "Partitioning and sorting specifications must be the same.") | ||
| return false | ||
| } | ||
|
|
||
| true | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
|
|
||
2 changes: 1 addition & 1 deletion
2
...rces/tpcds-plan-stability/approved-plans-v1_4-spark3_5/q12.native_datafusion/extended.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../tpcds-plan-stability/approved-plans-v1_4-spark3_5/q12.native_iceberg_compat/extended.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
spark/src/test/resources/tpcds-plan-stability/approved-plans-v1_4-spark3_5/q12/extended.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rces/tpcds-plan-stability/approved-plans-v1_4-spark3_5/q20.native_datafusion/extended.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../tpcds-plan-stability/approved-plans-v1_4-spark3_5/q20.native_iceberg_compat/extended.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
spark/src/test/resources/tpcds-plan-stability/approved-plans-v1_4-spark3_5/q20/extended.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rces/tpcds-plan-stability/approved-plans-v1_4-spark3_5/q36.native_datafusion/extended.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../tpcds-plan-stability/approved-plans-v1_4-spark3_5/q36.native_iceberg_compat/extended.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
spark/src/test/resources/tpcds-plan-stability/approved-plans-v1_4-spark3_5/q36/extended.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ouch I thought window was disabled