Skip to content

Commit f35974e

Browse files
committed
Remove useless isLiteral and isEvaluted. Add one more test.
1 parent c4f15f7 commit f35974e

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,7 @@ case class ExprCode(
6363
var isNull: String,
6464
var value: String,
6565
var inputRow: String = null,
66-
var inputVars: Seq[ExprInputVar] = Seq.empty) {
67-
68-
// Returns true if this value is a literal.
69-
def isLiteral(): Boolean = {
70-
assert(value.nonEmpty, "ExprCode.value can't be empty string.")
71-
72-
if (value == "true" || value == "false" || value == "null") {
73-
true
74-
} else {
75-
// The valid characters for the first character of a Java variable is [a-zA-Z_$].
76-
value.head match {
77-
case v if v >= 'a' && v <= 'z' => false
78-
case v if v >= 'A' && v <= 'Z' => false
79-
case '_' | '$' => false
80-
case _ => true
81-
}
82-
}
83-
}
84-
85-
// The code is emptied after evaluation.
86-
def isEvaluated(): Boolean = code == ""
87-
}
66+
var inputVars: Seq[ExprInputVar] = Seq.empty)
8867

8968
/**
9069
* Represents an input variable [[ExprCode]] to an evaluation of an [[Expression]].

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/codegen/ExpressionCodegenSuite.scala

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,58 @@ class ExpressionCodegenSuite extends SparkFunSuite {
134134
assert(inputRows(0) == "inputadaptor_row1")
135135
}
136136

137+
test("Returns both input rows and variables for expression") {
138+
val ctx = new CodegenContext()
139+
// 5 input variables in currentVars:
140+
// 1 evaluated variable (value1).
141+
// 3 not evaluated variables.
142+
// value2 depends on an evaluated column from other operator.
143+
// value3 depends on an input row from other operator.
144+
// value4 depends on a not evaluated yet column from other operator.
145+
// 1 null indicating to use input row "i".
146+
val currentVars = Seq(
147+
ExprCode("", isNull = "false", value = "value1"),
148+
ExprCode("fake code;", isNull = "isNull2", value = "value2"),
149+
ExprCode("fake code;", isNull = "isNull3", value = "value3"),
150+
ExprCode("fake code;", isNull = "isNull4", value = "value4"),
151+
null)
152+
// value2 depends on this evaluated column.
153+
currentVars(1).inputVars = Seq(ExprInputVar(ExprCode("", isNull = "isNull5", value = "value5"),
154+
dataType = IntegerType, nullable = true))
155+
// value3 depends on an input row "inputadaptor_row1".
156+
currentVars(2).inputRow = "inputadaptor_row1"
157+
// value4 depends on another not evaluated yet column.
158+
currentVars(3).inputVars = Seq(ExprInputVar(ExprCode("fake code;",
159+
isNull = "isNull6", value = "value6"), dataType = IntegerType, nullable = true))
160+
ctx.currentVars = currentVars
161+
ctx.INPUT_ROW = "i"
162+
163+
// expr: if (false) { value1 + value2 } else { (value3 + value4) + i[5] }
164+
val expr = If(Literal(false),
165+
Add(BoundReference(0, IntegerType, nullable = false),
166+
BoundReference(1, IntegerType, nullable = true)),
167+
Add(Add(BoundReference(2, IntegerType, nullable = true),
168+
BoundReference(3, IntegerType, nullable = true)),
169+
BoundReference(4, IntegerType, nullable = true))) // this is based on input row "i".
170+
171+
// input rows: "i", "inputadaptor_row1".
172+
val inputRows = ExpressionCodegen.getInputRowsForChildren(ctx, expr)
173+
assert(inputRows.length == 2)
174+
assert(inputRows(0) == "inputadaptor_row1")
175+
assert(inputRows(1) == "i")
176+
177+
// input variables: value1 and value5
178+
val inputVars = ExpressionCodegen.getInputVarsForChildren(ctx, expr)
179+
assert(inputVars.length == 2)
180+
181+
// value1 has inlined isNull "false", so don't need to include it in the params.
182+
val inputVarParams = ExpressionCodegen.prepareFunctionParams(ctx, inputVars)
183+
assert(inputVarParams.length == 3)
184+
assert(inputVarParams(0) == Tuple2("value1", "int value1"))
185+
assert(inputVarParams(1) == Tuple2("value5", "int value5"))
186+
assert(inputVarParams(2) == Tuple2("isNull5", "boolean isNull5"))
187+
}
188+
137189
test("isLiteral: literals") {
138190
val literals = Seq(
139191
ExprCode("", "", "true"),

0 commit comments

Comments
 (0)