Skip to content
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

[SPARK-13242] [SQL] Fall back to interpreting complex when expressions #11243

Closed
wants to merge 6 commits into from
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 @@ -86,7 +86,7 @@ case class If(predicate: Expression, trueValue: Expression, falseValue: Expressi
* @param elseValue optional value for the else branch
*/
case class CaseWhen(branches: Seq[(Expression, Expression)], elseValue: Option[Expression] = None)
extends Expression {
extends Expression with CodegenFallback {

override def children: Seq[Expression] = branches.flatMap(b => b._1 :: b._2 :: Nil) ++ elseValue

Expand Down Expand Up @@ -155,6 +155,7 @@ case class CaseWhen(branches: Seq[(Expression, Expression)], elseValue: Option[E
// }
// }
// }

val cases = branches.map { case (condExpr, valueExpr) =>
val cond = condExpr.gen(ctx)
val res = valueExpr.gen(ctx)
Expand Down Expand Up @@ -182,11 +183,16 @@ case class CaseWhen(branches: Seq[(Expression, Expression)], elseValue: Option[E

generatedCode += "}\n" * cases.size

s"""
// Methods that are too long cannot be compiled, so fall back to interpreting
if (generatedCode.length > 1000) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do the switch based on number of branches? for example, 10 or 20 switches.

Because whole stage codegen can't work with CodegenFallback, If we do so, we can know when we will do the fallback, we can still support CaseWhen in whole stage codegen if only have a few branches.

super.genCode(ctx, ev)
} else {
s"""
boolean ${ev.isNull} = true;
${ctx.javaType(dataType)} ${ev.value} = ${ctx.defaultValue(dataType)};
$generatedCode
"""
"""
}
}

override def toString: String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ class CodeGenerationSuite extends SparkFunSuite with ExpressionEvalHelper {
}
}


test("SPARK-13242: complicated when expressions") {
val cases = 50
val clauses = 20

// Generate an individual case
def generateCase(n: Int): (Expression, Expression) = {
val condition = (1 to clauses)
.map(c => EqualTo(BoundReference(0, StringType, false), Literal(s"$c:$n")))
.reduceLeft[Expression]((l, r) => Or(l, r))
(condition, Literal(n))
}

val expression = CaseWhen((1 to cases).map(generateCase(_)))

val plan = GenerateMutableProjection.generate(Seq(expression))()
val input = new GenericMutableRow(Array[Any](UTF8String.fromString(s"${clauses}:${cases}")))
val actual = plan(input).toSeq(Seq(expression.dataType))

assert(actual(0) == cases)
}

test("test generated safe and unsafe projection") {
val schema = new StructType(Array(
StructField("a", StringType, true),
Expand Down