Skip to content

Commit b6030d9

Browse files
committed
address review comment
1 parent 9080500 commit b6030d9

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -279,33 +279,38 @@ case class CaseWhenCodegen(
279279
"""
280280
}
281281

282-
var isGlobalVariable = false
283-
var generatedCode = ""
284282
var numIfthen = 0
285-
cases.foreach { ifthen =>
286-
generatedCode += ifthen + "\nelse {\n"
287-
numIfthen += 1
288-
289-
if (generatedCode.length > 1024 &&
290-
// Split these expressions only if they are created from a row object
291-
(ctx.INPUT_ROW != null && ctx.currentVars == null)) {
292-
val flag = "flag"
293-
generatedCode += s" $flag = false;\n" + "}\n" * numIfthen
294-
val funcName = ctx.freshName("caseWhenNestedIf")
295-
val funcBody =
296-
s"""
297-
|private boolean $funcName(InternalRow ${ctx.INPUT_ROW}) {
298-
| boolean $flag = true;
299-
| $generatedCode
300-
| return $flag;
301-
|}
302-
""".stripMargin
303-
val fullFuncName = ctx.addNewFunction(funcName, funcBody)
304-
isGlobalVariable = true
305-
306-
generatedCode = s"if ($fullFuncName(${ctx.INPUT_ROW})) {\n// do nothing\n} else {\n"
307-
numIfthen = 1
283+
var isGlobalVariable = false
284+
var generatedCode = if (cases.map(s => s.length).sum <= 1024) {
285+
cases.mkString("\nelse {\n")
286+
} else {
287+
var code = ""
288+
cases.foreach { ifthen =>
289+
code += ifthen + "\nelse {\n"
290+
numIfthen += 1
291+
292+
if (code.length > 1024 &&
293+
// Split these expressions only if they are created from a row object
294+
(ctx.INPUT_ROW != null && ctx.currentVars == null)) {
295+
val flag = "flag"
296+
code += s" $flag = false;\n" + "}\n" * numIfthen
297+
val funcName = ctx.freshName("caseWhenNestedIf")
298+
val funcBody =
299+
s"""
300+
|private boolean $funcName(InternalRow ${ctx.INPUT_ROW}) {
301+
| boolean $flag = true;
302+
| $code
303+
| return $flag;
304+
|}
305+
""".stripMargin
306+
val fullFuncName = ctx.addNewFunction(funcName, funcBody)
307+
isGlobalVariable = true
308+
309+
code = s"if ($fullFuncName(${ctx.INPUT_ROW})) {\n// do nothing\n} else {\n"
310+
numIfthen = 1
311+
}
308312
}
313+
code
309314
}
310315

311316
elseValue.foreach { elseExpr =>

sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,20 +2159,13 @@ class DataFrameSuite extends QueryTest with SharedSQLContext {
21592159
assert(mean.collect().toSet === Set(Row("0.0345678900000000000000000000000000000")))
21602160
}
21612161

2162-
testQuietly("SPARK-21413: Multiple projections with CASE WHEN fails") {
2162+
// ignore end-to-end test since sbt test does not go to fallback path in whole-stage codegen
2163+
ignore("SPARK-21413: Multiple projections with CASE WHEN fails") {
21632164
val schema = StructType(StructField("a", IntegerType) :: Nil)
2164-
val df = spark.createDataFrame(sparkContext.parallelize(Seq(Row(1))), schema)
2165-
val df1 =
2166-
df.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2167-
.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2168-
.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2169-
.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2170-
.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2171-
.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2172-
.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2173-
.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2174-
.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2175-
.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2176-
checkAnswer(df1, Row(1))
2165+
var df = spark.createDataFrame(sparkContext.parallelize(Seq(Row(1))), schema)
2166+
for (i <- 1 to 10) {
2167+
df = df.withColumn("a", when($"a" === 0, null).otherwise($"a"))
2168+
}
2169+
checkAnswer(df, Row(1))
21772170
}
21782171
}

0 commit comments

Comments
 (0)