Skip to content
Snippets Groups Projects
Commit fc27dfbf authored by Dilip Biswal's avatar Dilip Biswal Committed by Yin Huai
Browse files

[SPARK-11024][SQL] Optimize NULL in <inlist-expressions> by folding it to Literal(null)

Add a rule in optimizer to convert NULL [NOT] IN (expr1,...,expr2) to
Literal(null).

This is a follow up defect to SPARK-8654

cloud-fan Can you please take a look ?

Author: Dilip Biswal <dbiswal@us.ibm.com>

Closes #9348 from dilipbiswal/spark_11024.
parent ac4118db
No related branches found
No related tags found
No related merge requests found
...@@ -417,6 +417,11 @@ object NullPropagation extends Rule[LogicalPlan] { ...@@ -417,6 +417,11 @@ object NullPropagation extends Rule[LogicalPlan] {
case left :: Literal(null, _) :: Nil => Literal.create(null, e.dataType) case left :: Literal(null, _) :: Nil => Literal.create(null, e.dataType)
case _ => e case _ => e
} }
// If the value expression is NULL then transform the In expression to
// Literal(null)
case In(Literal(null, _), list) => Literal.create(null, BooleanType)
} }
} }
} }
......
...@@ -35,7 +35,8 @@ class OptimizeInSuite extends PlanTest { ...@@ -35,7 +35,8 @@ class OptimizeInSuite extends PlanTest {
val batches = val batches =
Batch("AnalysisNodes", Once, Batch("AnalysisNodes", Once,
EliminateSubQueries) :: EliminateSubQueries) ::
Batch("ConstantFolding", Once, Batch("ConstantFolding", FixedPoint(10),
NullPropagation,
ConstantFolding, ConstantFolding,
BooleanSimplification, BooleanSimplification,
OptimizeIn) :: Nil OptimizeIn) :: Nil
...@@ -82,4 +83,52 @@ class OptimizeInSuite extends PlanTest { ...@@ -82,4 +83,52 @@ class OptimizeInSuite extends PlanTest {
comparePlans(optimized, correctAnswer) comparePlans(optimized, correctAnswer)
} }
test("OptimizedIn test: NULL IN (expr1, ..., exprN) gets transformed to Filter(null)") {
val originalQuery =
testRelation
.where(In(Literal.create(null, NullType), Seq(Literal(1), Literal(2))))
.analyze
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer =
testRelation
.where(Literal.create(null, BooleanType))
.analyze
comparePlans(optimized, correctAnswer)
}
test("OptimizedIn test: Inset optimization disabled as " +
"list expression contains attribute)") {
val originalQuery =
testRelation
.where(In(Literal.create(null, StringType), Seq(Literal(1), UnresolvedAttribute("b"))))
.analyze
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer =
testRelation
.where(Literal.create(null, BooleanType))
.analyze
comparePlans(optimized, correctAnswer)
}
test("OptimizedIn test: Inset optimization disabled as " +
"list expression contains attribute - select)") {
val originalQuery =
testRelation
.select(In(Literal.create(null, StringType),
Seq(Literal(1), UnresolvedAttribute("b"))).as("a")).analyze
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer =
testRelation
.select(Literal.create(null, BooleanType).as("a"))
.analyze
comparePlans(optimized, correctAnswer)
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment