Skip to content

Commit 2a29a60

Browse files
committed
Revert "[SPARK-22600][SQL] Fix 64kb limit for deeply nested expressions under wholestage codegen"
This reverts commit c7d0148.
1 parent bc7e4a9 commit 2a29a60

File tree

6 files changed

+13
-585
lines changed

6 files changed

+13
-585
lines changed

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

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ abstract class Expression extends TreeNode[Expression] {
105105
val isNull = ctx.freshName("isNull")
106106
val value = ctx.freshName("value")
107107
val eval = doGenCode(ctx, ExprCode("", isNull, value))
108-
eval.isNull = if (this.nullable) eval.isNull else "false"
109-
110-
// Records current input row and variables of this expression.
111-
eval.inputRow = ctx.INPUT_ROW
112-
eval.inputVars = findInputVars(ctx, eval)
113-
114108
reduceCodeSize(ctx, eval)
115109
if (eval.code.nonEmpty) {
116110
// Add `this` in the comment.
@@ -121,29 +115,9 @@ abstract class Expression extends TreeNode[Expression] {
121115
}
122116
}
123117

124-
/**
125-
* Returns the input variables to this expression.
126-
*/
127-
private def findInputVars(ctx: CodegenContext, eval: ExprCode): Seq[ExprInputVar] = {
128-
if (ctx.currentVars != null) {
129-
this.collect {
130-
case b @ BoundReference(ordinal, _, _) if ctx.currentVars(ordinal) != null =>
131-
ExprInputVar(exprCode = ctx.currentVars(ordinal),
132-
dataType = b.dataType, nullable = b.nullable)
133-
}
134-
} else {
135-
Seq.empty
136-
}
137-
}
138-
139-
/**
140-
* In order to prevent 64kb compile error, reducing the size of generated codes by
141-
* separating it into a function if the size exceeds a threshold.
142-
*/
143118
private def reduceCodeSize(ctx: CodegenContext, eval: ExprCode): Unit = {
144-
lazy val funcParams = ExpressionCodegen.getExpressionInputParams(ctx, this)
145-
146-
if (eval.code.trim.length > 1024 && funcParams.isDefined) {
119+
// TODO: support whole stage codegen too
120+
if (eval.code.trim.length > 1024 && ctx.INPUT_ROW != null && ctx.currentVars == null) {
147121
val setIsNull = if (eval.isNull != "false" && eval.isNull != "true") {
148122
val globalIsNull = ctx.freshName("globalIsNull")
149123
ctx.addMutableState(ctx.JAVA_BOOLEAN, globalIsNull)
@@ -158,20 +132,17 @@ abstract class Expression extends TreeNode[Expression] {
158132
val newValue = ctx.freshName("value")
159133

160134
val funcName = ctx.freshName(nodeName)
161-
val callParams = funcParams.map(_._1.mkString(", ")).get
162-
val declParams = funcParams.map(_._2.mkString(", ")).get
163-
164135
val funcFullName = ctx.addNewFunction(funcName,
165136
s"""
166-
|private $javaType $funcName($declParams) {
137+
|private $javaType $funcName(InternalRow ${ctx.INPUT_ROW}) {
167138
| ${eval.code.trim}
168139
| $setIsNull
169140
| return ${eval.value};
170141
|}
171142
""".stripMargin)
172143

173144
eval.value = newValue
174-
eval.code = s"$javaType $newValue = $funcFullName($callParams);"
145+
eval.code = s"$javaType $newValue = $funcFullName(${ctx.INPUT_ROW});"
175146
}
176147
}
177148

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeGenerator.scala

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,8 @@ import org.apache.spark.util.{ParentClassLoader, Utils}
5555
* to null.
5656
* @param value A term for a (possibly primitive) value of the result of the evaluation. Not
5757
* valid if `isNull` is set to `true`.
58-
* @param inputRow A term that holds the input row name when generating this code.
59-
* @param inputVars A list of [[ExprInputVar]] that holds input variables when generating this code.
6058
*/
61-
case class ExprCode(
62-
var code: String,
63-
var isNull: String,
64-
var value: String,
65-
var inputRow: String = null,
66-
var inputVars: Seq[ExprInputVar] = Seq.empty)
67-
68-
/**
69-
* Represents an input variable [[ExprCode]] to an evaluation of an [[Expression]].
70-
*
71-
* @param exprCode The [[ExprCode]] that represents the evaluation result for the input variable.
72-
* @param dataType The data type of the input variable.
73-
* @param nullable Whether the input variable can be null or not.
74-
*/
75-
case class ExprInputVar(exprCode: ExprCode, dataType: DataType, nullable: Boolean)
59+
case class ExprCode(var code: String, var isNull: String, var value: String)
7660

7761
/**
7862
* State used for subexpression elimination.
@@ -1028,25 +1012,16 @@ class CodegenContext {
10281012
commonExprs.foreach { e =>
10291013
val expr = e.head
10301014
val fnName = freshName("evalExpr")
1031-
val isNull = if (expr.nullable) {
1032-
s"${fnName}IsNull"
1033-
} else {
1034-
""
1035-
}
1015+
val isNull = s"${fnName}IsNull"
10361016
val value = s"${fnName}Value"
10371017

10381018
// Generate the code for this expression tree and wrap it in a function.
10391019
val eval = expr.genCode(this)
1040-
val assignIsNull = if (expr.nullable) {
1041-
s"$isNull = ${eval.isNull};"
1042-
} else {
1043-
""
1044-
}
10451020
val fn =
10461021
s"""
10471022
|private void $fnName(InternalRow $INPUT_ROW) {
10481023
| ${eval.code.trim}
1049-
| $assignIsNull
1024+
| $isNull = ${eval.isNull};
10501025
| $value = ${eval.value};
10511026
|}
10521027
""".stripMargin
@@ -1064,17 +1039,12 @@ class CodegenContext {
10641039
// 2. Less code.
10651040
// Currently, we will do this for all non-leaf only expression trees (i.e. expr trees with
10661041
// at least two nodes) as the cost of doing it is expected to be low.
1067-
if (expr.nullable) {
1068-
addMutableState(JAVA_BOOLEAN, isNull)
1069-
}
1070-
addMutableState(javaType(expr.dataType), value)
1042+
addMutableState(JAVA_BOOLEAN, isNull, s"$isNull = false;")
1043+
addMutableState(javaType(expr.dataType), value,
1044+
s"$value = ${defaultValue(expr.dataType)};")
10711045

10721046
subexprFunctions += s"${addNewFunction(fnName, fn)}($INPUT_ROW);"
1073-
val state = if (expr.nullable) {
1074-
SubExprEliminationState(isNull, value)
1075-
} else {
1076-
SubExprEliminationState("false", value)
1077-
}
1047+
val state = SubExprEliminationState(isNull, value)
10781048
e.foreach(subExprEliminationExprs.put(_, state))
10791049
}
10801050
}

0 commit comments

Comments
 (0)