Github user kiszk commented on a diff in the pull request:
https://github.com/apache/spark/pull/19899#discussion_r155190509
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
---
@@ -668,22 +681,35 @@ case class Greatest(children: Seq[Expression])
extends Expression {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val evalChildren = children.map(_.genCode(ctx))
- ctx.addMutableState(ctx.JAVA_BOOLEAN, ev.isNull)
- ctx.addMutableState(ctx.javaType(dataType), ev.value)
- def updateEval(eval: ExprCode): String = {
+ val isNull = ctx.freshName("greatestTmpIsNull")
+ ctx.addMutableState(ctx.JAVA_BOOLEAN, isNull)
+ val evals = evalChildren.map(eval =>
s"""
${eval.code}
- if (!${eval.isNull} && (${ev.isNull} ||
+ if (!${eval.isNull} && (${isNull} ||
${ctx.genGreater(dataType, eval.value, ev.value)})) {
- ${ev.isNull} = false;
+ $isNull = false;
${ev.value} = ${eval.value};
}
"""
- }
- val codes =
ctx.splitExpressionsWithCurrentInputs(evalChildren.map(updateEval))
+ )
+
+ val resultType = ctx.javaType(dataType)
+ val codes = ctx.splitExpressionsWithCurrentInputs(
+ expressions = evals,
+ funcName = "greatest",
+ extraArguments = Seq(resultType -> ev.value),
+ returnType = resultType,
+ makeSplitFunction = body =>
+ s"""
+ |$body
+ |return ${ev.value};
+ """.stripMargin,
+ foldFunctions = _.map(funcCall => s"${ev.value} =
$funcCall;").mkString("\n"))
ev.copy(code = s"""
- ${ev.isNull} = true;
- ${ev.value} = ${ctx.defaultValue(dataType)};
- $codes""")
+ $isNull = true;
+ ${ctx.javaType(dataType)} ${ev.value} =
${ctx.defaultValue(dataType)};
+ $codes
+ final boolean ${ev.isNull} = $isNull;""")
--- End diff --
yea, good catch
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]