-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-49915][SQL] Handle zeros and ones in ReorderAssociativeOperator #48395
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
Changes from all commits
3420912
1800fcc
9dec9ee
1ce2dbb
c85d125
768259a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.optimizer | |
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.Count | ||
| import org.apache.spark.sql.catalyst.plans.{Inner, PlanTest} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
|
|
@@ -74,4 +75,35 @@ class ReorderAssociativeOperatorSuite extends PlanTest { | |
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("SPARK-49915: Handle zero and one in associative operators") { | ||
| val originalQuery = | ||
| testRelation.select( | ||
| $"a" + 0, | ||
| Literal(-3) + $"a" + 3, | ||
| $"b" * 0 * 1 * 2 * 3, | ||
|
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 test non-nullable
Member
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. addressed |
||
| Count($"b") * 0, | ||
| $"b" * 1 * 1, | ||
| ($"b" + 0) * 1 * 2 * 3 * 4, | ||
| $"a" + 0 + $"b" + 0 + $"c" + 0, | ||
| $"a" + 0 + $"b" * 1 + $"c" + 0 | ||
| ) | ||
|
|
||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
|
|
||
| val correctAnswer = | ||
| testRelation | ||
| .select( | ||
| $"a".as("(a + 0)"), | ||
| $"a".as("((-3 + a) + 3)"), | ||
| ($"b" * 0).as("((((b * 0) * 1) * 2) * 3)"), | ||
| Literal(0L).as("(count(b) * 0)"), | ||
| $"b".as("((b * 1) * 1)"), | ||
| ($"b" * 24).as("(((((b + 0) * 1) * 2) * 3) * 4)"), | ||
| ($"a" + $"b" + $"c").as("""(((((a + 0) + b) + 0) + c) + 0)"""), | ||
| ($"a" + $"b" + $"c").as("((((a + 0) + (b * 1)) + c) + 0)") | ||
| ).analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,24 @@ Project [a#x, (b#x + c#x) AS (b + c)#x] | |
| +- Relation spark_catalog.default.t1[a#x,b#x,c#x] parquet | ||
|
|
||
|
|
||
| -- !query | ||
| select b + 0 from t1 where a = 5 | ||
|
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 think we should test
Member
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. There are already such test cases in ReorderAssociativeOperatorSuite.scala
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. hmm, then do we need to add additional tests in this golden file?
Member
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.
If we want the test to be more intuitive and specific for NULL semantics, I can add some more null-relevant cases here
Member
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. NVM, I guess we already have such test cases https://github.com/apache/spark/pull/48395/files#diff-c3c4e84c1d15e1c494ce6c062e78a9d53128a489d4831af6940bda50a1148d38R16-L15 |
||
| -- !query analysis | ||
| Project [(b#x + 0) AS (b + 0)#x] | ||
| +- Filter (a#x = 5) | ||
| +- SubqueryAlias spark_catalog.default.t1 | ||
| +- Relation spark_catalog.default.t1[a#x,b#x,c#x] parquet | ||
|
|
||
|
|
||
| -- !query | ||
| select -100 + b + 100 from t1 where a = 5 | ||
| -- !query analysis | ||
| Project [((-100 + b#x) + 100) AS ((-100 + b) + 100)#x] | ||
| +- Filter (a#x = 5) | ||
| +- SubqueryAlias spark_catalog.default.t1 | ||
| +- Relation spark_catalog.default.t1[a#x,b#x,c#x] parquet | ||
|
|
||
|
|
||
| -- !query | ||
| select a+10, b*0 from t1 | ||
| -- !query analysis | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2688,7 +2688,7 @@ class JDBCV2Suite extends QueryTest with SharedSparkSession with ExplainSuiteHel | |
| val df = sql("SELECT SUM(2147483647 + DEPT) FROM h2.test.employee") | ||
| checkAggregateRemoved(df, ansiMode) | ||
| val expectedPlanFragment = if (ansiMode) { | ||
| "PushedAggregates: [SUM(2147483647 + DEPT)], " + | ||
| "PushedAggregates: [SUM(DEPT + 2147483647)], " + | ||
|
Member
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. This test changes as the reorder rule is applied when containing only one foldable expression |
||
| "PushedFilters: [], " + | ||
| "PushedGroupByExpressions: []" | ||
| } else { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.