Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,13 @@ class EquivalentExpressions {
// a subexpression among values doesn't need to be in conditions because no matter which
// condition is true, it will be evaluated.
val conditions = c.branches.tail.map(_._1)
val values = c.branches.map(_._2) ++ c.elseValue
// For an expression to be in all branch values of a CaseWhen statement, it must also be in
// the elseValue.
val values = if (c.elseValue.nonEmpty) {
c.branches.map(_._2) ++ c.elseValue
} else {
Nil
}
Seq(conditions, values)
case c: Coalesce => Seq(c.children.tail)
case _ => Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class SubexpressionEliminationSuite extends SparkFunSuite with ExpressionEvalHel
(GreaterThan(add2, Literal(4)), add1) ::
(GreaterThan(add2, Literal(5)), add1) :: Nil

val caseWhenExpr2 = CaseWhen(conditions2, None)
val caseWhenExpr2 = CaseWhen(conditions2, add1)
val equivalence2 = new EquivalentExpressions
equivalence2.addExprTree(caseWhenExpr2)

Expand Down Expand Up @@ -309,6 +309,22 @@ class SubexpressionEliminationSuite extends SparkFunSuite with ExpressionEvalHel
CodeGenerator.compile(code)
}
}

test("SPARK-35499: Subexpressions should only be extracted from CaseWhen values with an "
+ "elseValue") {
val add1 = Add(Literal(1), Literal(2))
val add2 = Add(Literal(2), Literal(3))
val conditions = (GreaterThan(add1, Literal(3)), add1) ::
(GreaterThan(add2, Literal(4)), add1) ::
(GreaterThan(add2, Literal(5)), add1) :: Nil

val caseWhenExpr = CaseWhen(conditions, None)
val equivalence = new EquivalentExpressions
equivalence.addExprTree(caseWhenExpr)

// `add1` is not in the elseValue, so we can't extract it from the branches
assert(equivalence.getAllEquivalentExprs.count(_.size == 2) == 0)
}
}

case class CodegenFallbackExpression(child: Expression)
Expand Down