Skip to content

Commit fa244e5

Browse files
kiszkcloud-fan
authored andcommitted
[SPARK-15285][SQL] Generated SpecificSafeProjection.apply method grows beyond 64 KB
## What changes were proposed in this pull request? This PR splits the generated code for ```SafeProjection.apply``` by using ```ctx.splitExpressions()```. This is because the large code body for ```NewInstance``` may grow beyond 64KB bytecode size for ```apply()``` method. ## How was this patch tested? Added new tests Author: Kazuaki Ishizaki <ishizaki@jp.ibm.com> Closes #13243 from kiszk/SPARK-15285.
1 parent d207716 commit fa244e5

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,27 +232,47 @@ case class NewInstance(
232232

233233
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
234234
val javaType = ctx.javaType(dataType)
235-
val argGen = arguments.map(_.genCode(ctx))
236-
val argString = argGen.map(_.value).mkString(", ")
235+
val argIsNulls = ctx.freshName("argIsNulls")
236+
ctx.addMutableState("boolean[]", argIsNulls,
237+
s"$argIsNulls = new boolean[${arguments.size}];")
238+
val argValues = arguments.zipWithIndex.map { case (e, i) =>
239+
val argValue = ctx.freshName("argValue")
240+
ctx.addMutableState(ctx.javaType(e.dataType), argValue, "")
241+
argValue
242+
}
243+
244+
val argCodes = arguments.zipWithIndex.map { case (e, i) =>
245+
val expr = e.genCode(ctx)
246+
expr.code + s"""
247+
$argIsNulls[$i] = ${expr.isNull};
248+
${argValues(i)} = ${expr.value};
249+
"""
250+
}
251+
val argCode = ctx.splitExpressions(ctx.INPUT_ROW, argCodes)
237252

238253
val outer = outerPointer.map(func => Literal.fromObject(func()).genCode(ctx))
239254

240255
var isNull = ev.isNull
241256
val setIsNull = if (propagateNull && arguments.nonEmpty) {
242-
s"final boolean $isNull = ${argGen.map(_.isNull).mkString(" || ")};"
257+
s"""
258+
boolean $isNull = false;
259+
for (int idx = 0; idx < ${arguments.length}; idx++) {
260+
if ($argIsNulls[idx]) { $isNull = true; break; }
261+
}
262+
"""
243263
} else {
244264
isNull = "false"
245265
""
246266
}
247267

248268
val constructorCall = outer.map { gen =>
249-
s"""${gen.value}.new ${cls.getSimpleName}($argString)"""
269+
s"""${gen.value}.new ${cls.getSimpleName}(${argValues.mkString(", ")})"""
250270
}.getOrElse {
251-
s"new $className($argString)"
271+
s"new $className(${argValues.mkString(", ")})"
252272
}
253273

254274
val code = s"""
255-
${argGen.map(_.code).mkString("\n")}
275+
$argCode
256276
${outer.map(_.code).getOrElse("")}
257277
$setIsNull
258278
final $javaType ${ev.value} = $isNull ? ${ctx.defaultValue(javaType)} : $constructorCall;

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,39 @@ class DataFrameComplexTypeSuite extends QueryTest with SharedSQLContext {
5858
val nullIntRow = df.selectExpr("i[1]").collect()(0)
5959
assert(nullIntRow == org.apache.spark.sql.Row(null))
6060
}
61+
62+
test("SPARK-15285 Generated SpecificSafeProjection.apply method grows beyond 64KB") {
63+
val ds100_5 = Seq(S100_5()).toDS()
64+
ds100_5.rdd.count
65+
}
6166
}
67+
68+
case class S100(
69+
s1: String = "1", s2: String = "2", s3: String = "3", s4: String = "4",
70+
s5: String = "5", s6: String = "6", s7: String = "7", s8: String = "8",
71+
s9: String = "9", s10: String = "10", s11: String = "11", s12: String = "12",
72+
s13: String = "13", s14: String = "14", s15: String = "15", s16: String = "16",
73+
s17: String = "17", s18: String = "18", s19: String = "19", s20: String = "20",
74+
s21: String = "21", s22: String = "22", s23: String = "23", s24: String = "24",
75+
s25: String = "25", s26: String = "26", s27: String = "27", s28: String = "28",
76+
s29: String = "29", s30: String = "30", s31: String = "31", s32: String = "32",
77+
s33: String = "33", s34: String = "34", s35: String = "35", s36: String = "36",
78+
s37: String = "37", s38: String = "38", s39: String = "39", s40: String = "40",
79+
s41: String = "41", s42: String = "42", s43: String = "43", s44: String = "44",
80+
s45: String = "45", s46: String = "46", s47: String = "47", s48: String = "48",
81+
s49: String = "49", s50: String = "50", s51: String = "51", s52: String = "52",
82+
s53: String = "53", s54: String = "54", s55: String = "55", s56: String = "56",
83+
s57: String = "57", s58: String = "58", s59: String = "59", s60: String = "60",
84+
s61: String = "61", s62: String = "62", s63: String = "63", s64: String = "64",
85+
s65: String = "65", s66: String = "66", s67: String = "67", s68: String = "68",
86+
s69: String = "69", s70: String = "70", s71: String = "71", s72: String = "72",
87+
s73: String = "73", s74: String = "74", s75: String = "75", s76: String = "76",
88+
s77: String = "77", s78: String = "78", s79: String = "79", s80: String = "80",
89+
s81: String = "81", s82: String = "82", s83: String = "83", s84: String = "84",
90+
s85: String = "85", s86: String = "86", s87: String = "87", s88: String = "88",
91+
s89: String = "89", s90: String = "90", s91: String = "91", s92: String = "92",
92+
s93: String = "93", s94: String = "94", s95: String = "95", s96: String = "96",
93+
s97: String = "97", s98: String = "98", s99: String = "99", s100: String = "100")
94+
95+
case class S100_5(
96+
s1: S100 = S100(), s2: S100 = S100(), s3: S100 = S100(), s4: S100 = S100(), s5: S100 = S100())

0 commit comments

Comments
 (0)