Skip to content

Commit c875329

Browse files
committed
address comment
1 parent d126977 commit c875329

File tree

1 file changed

+35
-33
lines changed
  • sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions

1 file changed

+35
-33
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Expression.scala

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -104,43 +104,45 @@ abstract class Expression extends TreeNode[Expression] {
104104
}.getOrElse {
105105
val isNull = ctx.freshName("isNull")
106106
val value = ctx.freshName("value")
107-
val ve = doGenCode(ctx, ExprCode("", isNull, value))
108-
109-
// TODO: support whole stage codegen too
110-
if (ve.code.trim.length > 1024 && ctx.INPUT_ROW != null && ctx.currentVars == null) {
111-
val setIsNull = if (ve.isNull != "false" && ve.isNull != "true") {
112-
val globalIsNull = ctx.freshName("globalIsNull")
113-
ctx.addMutableState("boolean", globalIsNull, s"$globalIsNull = false;")
114-
val localIsNull = ve.isNull
115-
ve.isNull = globalIsNull
116-
s"$globalIsNull = $localIsNull;"
117-
} else {
118-
""
119-
}
120-
121-
val javaType = ctx.javaType(dataType)
122-
val newValue = ctx.freshName("value")
123-
124-
val funcName = ctx.freshName(nodeName)
125-
val funcFullName = ctx.addNewFunction(funcName,
126-
s"""
127-
|private $javaType $funcName(InternalRow ${ctx.INPUT_ROW}) {
128-
| ${ve.code.trim}
129-
| $setIsNull
130-
| return ${ve.value};
131-
|}
132-
""".stripMargin)
133-
134-
ve.value = newValue
135-
ve.code = s"$javaType $newValue = $funcFullName(${ctx.INPUT_ROW});"
107+
val eval = doGenCode(ctx, ExprCode("", isNull, value))
108+
reduceCodeSize(ctx, eval)
109+
if (eval.code.nonEmpty) {
110+
// Add `this` in the comment.
111+
eval.copy(code = s"${ctx.registerComment(this.toString)}\n" + eval.code.trim)
112+
} else {
113+
eval
136114
}
115+
}
116+
}
137117

138-
if (ve.code.nonEmpty) {
139-
// Add `this` in the comment.
140-
ve.copy(code = s"${ctx.registerComment(this.toString)}\n" + ve.code.trim)
118+
private def reduceCodeSize(ctx: CodegenContext, eval: ExprCode): Unit = {
119+
// TODO: support whole stage codegen too
120+
if (eval.code.trim.length > 100 && ctx.INPUT_ROW != null && ctx.currentVars == null) {
121+
val setIsNull = if (eval.isNull != "false" && eval.isNull != "true") {
122+
val globalIsNull = ctx.freshName("globalIsNull")
123+
ctx.addMutableState(ctx.JAVA_BOOLEAN, globalIsNull)
124+
val localIsNull = eval.isNull
125+
eval.isNull = globalIsNull
126+
s"$globalIsNull = $localIsNull;"
141127
} else {
142-
ve
128+
""
143129
}
130+
131+
val javaType = ctx.javaType(dataType)
132+
val newValue = ctx.freshName("value")
133+
134+
val funcName = ctx.freshName(nodeName)
135+
val funcFullName = ctx.addNewFunction(funcName,
136+
s"""
137+
|private $javaType $funcName(InternalRow ${ctx.INPUT_ROW}) {
138+
| ${eval.code.trim}
139+
| $setIsNull
140+
| return ${eval.value};
141+
|}
142+
""".stripMargin)
143+
144+
eval.value = newValue
145+
eval.code = s"$javaType $newValue = $funcFullName(${ctx.INPUT_ROW});"
144146
}
145147
}
146148

0 commit comments

Comments
 (0)