-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-22704][SQL] Least and Greatest use less global variables #19899
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
Changes from 3 commits
bd3b4c7
d322931
facaf1c
d467192
e9a2202
47fcdc2
e5aa90e
e1ed6c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") | ||
| 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;""") | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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", | ||
|
||
| 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;""") | ||
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
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")There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isNull,ev.isNullandeval.isNulllooks too close.There was a problem hiding this comment.
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.isNulland there is no need for this new variable.