Skip to content

Commit e30ba2b

Browse files
committed
[SPARK-22141][SQL] Propagate empty relation before checking Cartesian products
When inferring constraints from children, Join's condition can be simplified as None. For example, ``` val testRelation = LocalRelation('a.int) val x = testRelation.as("x") val y = testRelation.where($"a" === 2 && !($"a" === 2)).as("y") x.join.where($"x.a" === $"y.a") ``` The plan will become ``` Join Inner :- LocalRelation <empty>, [a#23] +- LocalRelation <empty>, [a#224] ``` And the Cartesian products check will throw exception for above plan. Propagate empty relation before checking Cartesian products, and the issue is resolved. Unit test Author: Wang Gengliang <[email protected]> Closes #19362 from gengliangwang/MoveCheckCartesianProducts.
1 parent b0f30b5 commit e30ba2b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ abstract class Optimizer(sessionCatalog: SessionCatalog, conf: SQLConf)
113113
SimplifyCreateArrayOps,
114114
SimplifyCreateMapOps) ++
115115
extendedOperatorOptimizationRules: _*) ::
116-
Batch("Check Cartesian Products", Once,
117-
CheckCartesianProducts(conf)) ::
118116
Batch("Join Reorder", Once,
119117
CostBasedJoinReorder(conf)) ::
120118
Batch("Decimal Optimizations", fixedPoint,
@@ -125,6 +123,8 @@ abstract class Optimizer(sessionCatalog: SessionCatalog, conf: SQLConf)
125123
Batch("LocalRelation", fixedPoint,
126124
ConvertToLocalRelation,
127125
PropagateEmptyRelation) ::
126+
Batch("Check Cartesian Products", Once,
127+
CheckCartesianProducts(conf)) ::
128128
Batch("OptimizeCodegen", Once,
129129
OptimizeCodegen(conf)) ::
130130
Batch("RewriteSubquery", Once,

sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ class JoinSuite extends QueryTest with SharedSQLContext {
200200
Nil)
201201
}
202202

203+
test("SPARK-22141: Propagate empty relation before checking Cartesian products") {
204+
Seq("inner", "left", "right", "left_outer", "right_outer", "full_outer").foreach { joinType =>
205+
val x = testData2.where($"a" === 2 && !($"a" === 2)).as("x")
206+
val y = testData2.where($"a" === 1 && !($"a" === 1)).as("y")
207+
checkAnswer(x.join(y, Seq.empty, joinType), Nil)
208+
}
209+
}
210+
203211
test("big inner join, 4 matches per row") {
204212
val bigData = testData.union(testData).union(testData).union(testData)
205213
val bigDataX = bigData.as("x")

0 commit comments

Comments
 (0)