-
Notifications
You must be signed in to change notification settings - Fork 29k
[WIP][SPARK-24051][SQL] Replace Aliases with the same exprId #21184
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
Closed
Closed
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ import org.apache.spark.sql.catalyst.plans.logical.{Filter, OneRowRelation, Unio | |
| import org.apache.spark.sql.execution.{FilterExec, QueryExecution, WholeStageCodegenExec} | ||
| import org.apache.spark.sql.execution.aggregate.HashAggregateExec | ||
| import org.apache.spark.sql.execution.exchange.{BroadcastExchangeExec, ReusedExchangeExec, ShuffleExchangeExec} | ||
| import org.apache.spark.sql.expressions.Window | ||
| import org.apache.spark.sql.functions._ | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.test.{ExamplePoint, ExamplePointUDT, SharedSQLContext} | ||
|
|
@@ -2265,4 +2266,15 @@ class DataFrameSuite extends QueryTest with SharedSQLContext { | |
| val df = spark.range(1).select($"id", new Column(Uuid())) | ||
| checkAnswer(df, df.collect()) | ||
| } | ||
|
|
||
| test("SPARK-24051: using the same alias can produce incorrect result") { | ||
|
Member
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. This test case failed without your changes?
Contributor
Author
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. yes, without the change the result is: |
||
| val ds1 = Seq((1, 42), (2, 99)).toDF("a", "b") | ||
| val ds2 = Seq(3).toDF("a").withColumn("b", lit(0)) | ||
|
|
||
| val cols = Seq(col("a"), col("b").alias("b"), | ||
| count(lit(1)).over(Window.partitionBy()).alias("n")) | ||
|
|
||
| val df = ds1.select(cols: _*).union(ds2.select(cols: _*)) | ||
| checkAnswer(df, Row(1, 42, 2) :: Row(2, 99, 2) :: Row(3, 0, 1) :: Nil) | ||
| } | ||
| } | ||
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.
what problem does it try to resolve?
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.
The main problem which causes the added UT to fail is that
FoldablePropagationreplaces all foldable aliases which are considered to be the same. If the same alias with same exprId is located in different part of the plan (referencing actually different things, despite they have the same id...) this can cause wrong replacement to happen. So in the added UT, the plan is:Please note that in both the branches of the union we have the same
b#17attribute, but they are referencing different things. As the lower one is a foldable value which evaluates to 0, all theb#17are replace with a literal 0, causing a wrong result.Despite we might fix this specific problem in the related Optimizer rule, I think that in general we assume that items with the same id are the same. So I proposed this solution in order to fix all the possible issues which may arise due to this situation which is not expected.
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.
I feel the root cause is in
FoldablePropagation. We should only replace attribute with literal from children, not siblings.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.
Yes, that is also true. But in many places in the codebase we just compare attributes using
semanticEqualsor in some other cases, evenequals. Well, if we admit that different attributes can have the sameexprId, all these places should be checked in order to be sure that the same problem cannot happen there too. Moreover (this is more a nit), thesemanticEqualsorsameRefmethod itself would be wrong according to its semantic, as it may returntrueeven when two attributes don't have the same reference. This is the reason why I opted for this solution, which seems to me cleaner as it solves the root cause of the problem. What do you think?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.
kindly ping @cloud-fan