Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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"""
if (generatedCode.size > 64 * 1000) {
Copy link
Contributor

Choose a reason for hiding this comment

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

64k is too large, JIT will stop to work after 8K bytes code. 1K?

Copy link
Author

Choose a reason for hiding this comment

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

I guess you mean 10K? That makes sense. Should we modify the corresponding check in CodegenContext.splitExpressions?

super.genCode(ctx, ev)
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

} else {

Copy link
Author

Choose a reason for hiding this comment

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

Thanks

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