-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-24966][SQL] Implement precedence rules for set operations. #21941
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
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 |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedAttribute | |
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans._ | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types.IntegerType | ||
|
|
||
| /** | ||
|
|
@@ -676,4 +677,48 @@ class PlanParserSuite extends AnalysisTest { | |
| OneRowRelation().select('rtrim.function("c&^,.", "bc...,,,&&&ccc")) | ||
| ) | ||
| } | ||
|
|
||
| test("precedence of set operations") { | ||
| val a = table("a").select(star()) | ||
| val b = table("b").select(star()) | ||
| val c = table("c").select(star()) | ||
| val d = table("d").select(star()) | ||
|
|
||
| val query1 = | ||
| """ | ||
| |SELECT * FROM a | ||
| |UNION | ||
| |SELECT * FROM b | ||
| |EXCEPT | ||
| |SELECT * FROM c | ||
| |INTERSECT | ||
| |SELECT * FROM d | ||
| """.stripMargin | ||
|
|
||
| val query2 = | ||
| """ | ||
| |SELECT * FROM a | ||
| |UNION | ||
| |SELECT * FROM b | ||
| |EXCEPT ALL | ||
| |SELECT * FROM c | ||
| |INTERSECT ALL | ||
| |SELECT * FROM d | ||
| """.stripMargin | ||
|
|
||
| assertEqual(query1, Distinct(a.union(b)).except(c.intersect(d))) | ||
|
||
| assertEqual(query2, Distinct(a.union(b)).except(c.intersect(d, isAll = true), isAll = true)) | ||
|
|
||
| // Now disable precedence enforcement to verify the old behaviour. | ||
| withSQLConf(SQLConf.LEGACY_SETOPS_PRECEDENCE_ENABLED.key -> "true") { | ||
| assertEqual(query1, Distinct(a.union(b)).except(c).intersect(d)) | ||
| assertEqual(query2, Distinct(a.union(b)).except(c, isAll = true).intersect(d, isAll = true)) | ||
| } | ||
|
|
||
| // Explicitly enable the precedence enforcement | ||
| withSQLConf(SQLConf.LEGACY_SETOPS_PRECEDENCE_ENABLED.key -> "false") { | ||
| assertEqual(query1, Distinct(a.union(b)).except(c.intersect(d))) | ||
| assertEqual(query2, Distinct(a.union(b)).except(c.intersect(d, isAll = true), isAll = true)) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -535,14 +535,14 @@ abstract class SparkStrategies extends QueryPlanner[SparkPlan] { | |
| case logical.Intersect(left, right, true) => | ||
| throw new IllegalStateException( | ||
| "logical intersect operator should have been replaced by union, aggregate" + | ||
| "and generate operators in the optimizer") | ||
| " and generate operators in the optimizer") | ||
|
||
| case logical.Except(left, right, false) => | ||
| throw new IllegalStateException( | ||
| "logical except operator should have been replaced by anti-join in the optimizer") | ||
| case logical.Except(left, right, true) => | ||
| throw new IllegalStateException( | ||
| "logical except (all) operator should have been replaced by union, aggregate" + | ||
| "and generate operators in the optimizer") | ||
| " and generate operators in the optimizer") | ||
|
||
|
|
||
| case logical.DeserializeToObject(deserializer, objAttr, child) => | ||
| execution.DeserializeToObjectExec(deserializer, objAttr, planLater(child)) :: Nil | ||
|
|
||
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.
This is not related to the current PR. This addresses a comment from @HyukjinKwon in 21886