Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -602,23 +602,36 @@ case class Least(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("leastTmpIsNull")
Copy link
Member

Choose a reason for hiding this comment

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

val leastTmpIsNull = ctx.freshName("leastTmpIsNull")

Copy link
Member

Choose a reason for hiding this comment

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

isNull, ev.isNull and eval.isNull looks too close.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, but I think we can stay with ev.isNull and there is no need for this new variable.

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, ev.value, eval.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 = "least",
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;""")
}
}

Expand Down Expand Up @@ -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("isNull")
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 = "least",
Copy link
Member

Choose a reason for hiding this comment

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

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;""")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can we switch to the new string style (with stripMargin...)?

Copy link
Member Author

Choose a reason for hiding this comment

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

yea, good catch

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext
import org.apache.spark.sql.types._

class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
Expand Down Expand Up @@ -343,4 +344,14 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(Least(inputsExpr), "s" * 1, EmptyRow)
checkEvaluation(Greatest(inputsExpr), "s" * N, EmptyRow)
}

test("SPARK-22704: Least and greatest use less global variables") {
val ctx1 = new CodegenContext()
Least(Seq(Literal(1), Literal(1))).genCode(ctx1)
assert(ctx1.mutableStates.size == 1)

val ctx2 = new CodegenContext()
Greatest(Seq(Literal(1), Literal(1))).genCode(ctx2)
assert(ctx2.mutableStates.size == 1)
}
}