-
Notifications
You must be signed in to change notification settings - Fork 28.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-22498][SQL] Fix 64KB JVM bytecode limit problem with concat #19728
Changes from 5 commits
bc97d41
0ed8d16
366f721
6fdb451
e6b73ab
cd20410
e668b98
555575e
22019b1
b9ca4ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,15 +63,32 @@ case class Concat(children: Seq[Expression]) extends Expression with ImplicitCas | |
|
||
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
val evals = children.map(_.genCode(ctx)) | ||
val inputs = evals.map { eval => | ||
s"${eval.isNull} ? null : ${eval.value}" | ||
}.mkString(", ") | ||
ev.copy(evals.map(_.code).mkString("\n") + s""" | ||
boolean ${ev.isNull} = false; | ||
UTF8String ${ev.value} = UTF8String.concat($inputs); | ||
if (${ev.value} == null) { | ||
${ev.isNull} = true; | ||
val argNums = evals.length | ||
val args = ctx.freshName("args") | ||
|
||
val inputs = evals.zipWithIndex.map { case (eval, index) => | ||
if (eval.isNull != "true") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please do not mix in optimizations with bug fix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. I will remove this. |
||
s""" | ||
${eval.code} | ||
if (!${eval.isNull}) { | ||
$args[$index] = ${eval.value}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we are better to assign null too if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, while There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is guaranteed before the first row processed. After we process rows, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the next row is processed, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. I didn't notice that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for taking your time for my code. |
||
""" | ||
} else { | ||
"" | ||
} | ||
} | ||
val codes = if (ctx.INPUT_ROW != null && ctx.currentVars == null) { | ||
ctx.splitExpressions(inputs, "valueConcat", | ||
("InternalRow", ctx.INPUT_ROW) :: ("UTF8String[]", args) :: Nil) | ||
} else { | ||
inputs.mkString("\n") | ||
} | ||
ev.copy(s""" | ||
UTF8String[] $args = new UTF8String[$argNums]; | ||
$codes | ||
UTF8String ${ev.value} = UTF8String.concat($args); | ||
boolean ${ev.isNull} = ${ev.value} == null; | ||
""") | ||
} | ||
} | ||
|
@@ -126,18 +143,41 @@ case class ConcatWs(children: Seq[Expression]) | |
// All children are strings. In that case we can construct a fixed size array. | ||
val evals = children.map(_.genCode(ctx)) | ||
|
||
val inputs = evals.map { eval => | ||
s"${eval.isNull} ? (UTF8String) null : ${eval.value}" | ||
}.mkString(", ") | ||
|
||
ev.copy(evals.map(_.code).mkString("\n") + s""" | ||
UTF8String ${ev.value} = UTF8String.concatWs($inputs); | ||
val argNums = evals.length | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sorry that I cannot understand your suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, I am saying the opposite. Let's consider an example, maybe I better can explain what I mean in this way. Let's assume that we are running
I think that in this way the code would be much clearer (other than fixing this little bug). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your kindly explanation. I totally agree with you. |
||
val args = ctx.freshName("args") | ||
|
||
val inputs = evals.tail.zipWithIndex.map { case (eval, index) => | ||
if (eval.isNull != "true") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue as https://github.com/apache/spark/pull/19728/files#r150441287. |
||
s""" | ||
${eval.code} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: in this and the next 4 lines an indentation space is missing if I am not wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch |
||
if (!${eval.isNull}) { | ||
$args[$index] = ${eval.value}; | ||
} | ||
""" | ||
} else { | ||
"" | ||
} | ||
} | ||
val codes = s"${evals.head.code}\n" + | ||
(if (ctx.INPUT_ROW != null && ctx.currentVars == null) { | ||
ctx.splitExpressions(inputs, "valueConcatWs", | ||
("InternalRow", ctx.INPUT_ROW) :: ("UTF8String[]", args) :: Nil) | ||
} else { | ||
inputs.mkString("\n") | ||
}) | ||
ev.copy(s""" | ||
UTF8String[] $args = new UTF8String[$argNums]; | ||
$codes | ||
UTF8String ${ev.value} = UTF8String.concatWs(${evals.head.value}, $args); | ||
boolean ${ev.isNull} = ${ev.value} == null; | ||
""") | ||
} else { | ||
val array = ctx.freshName("array") | ||
ctx.addMutableState("UTF8String[]", array, "") | ||
val varargNum = ctx.freshName("varargNum") | ||
ctx.addMutableState("int", varargNum, "") | ||
val idxInVararg = ctx.freshName("idxInVararg") | ||
ctx.addMutableState("int", idxInVararg, "") | ||
|
||
val evals = children.map(_.genCode(ctx)) | ||
val (varargCount, varargBuild) = children.tail.zip(evals.tail).map { case (child, eval) => | ||
|
@@ -163,13 +203,17 @@ case class ConcatWs(children: Seq[Expression]) | |
} | ||
}.unzip | ||
|
||
ev.copy(evals.map(_.code).mkString("\n") + | ||
val codes = ctx.splitExpressions(ctx.INPUT_ROW, evals.map(_.code)) | ||
val varargCounts = ctx.splitExpressions(ctx.INPUT_ROW, varargCount) | ||
val varargBuilds = ctx.splitExpressions(ctx.INPUT_ROW, varargBuild) | ||
ev.copy( | ||
s""" | ||
int $varargNum = ${children.count(_.dataType == StringType) - 1}; | ||
int $idxInVararg = 0; | ||
${varargCount.mkString("\n")} | ||
UTF8String[] $array = new UTF8String[$varargNum]; | ||
${varargBuild.mkString("\n")} | ||
$codes | ||
$varargNum = ${children.count(_.dataType == StringType) - 1}; | ||
$idxInVararg = 0; | ||
$varargCounts | ||
$array = new UTF8String[$varargNum]; | ||
$varargBuilds | ||
UTF8String ${ev.value} = UTF8String.concatWs(${evals.head.value}, $array); | ||
boolean ${ev.isNull} = ${ev.value} == null; | ||
""") | ||
|
@@ -224,22 +268,55 @@ case class Elt(children: Seq[Expression]) | |
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
val index = indexExpr.genCode(ctx) | ||
val strings = stringExprs.map(_.genCode(ctx)) | ||
val indexVal = ctx.freshName("index") | ||
val stringVal = ctx.freshName("stringVal") | ||
val assignStringValue = strings.zipWithIndex.map { case (eval, index) => | ||
s""" | ||
case ${index + 1}: | ||
${ev.value} = ${eval.isNull} ? null : ${eval.value}; | ||
${eval.code} | ||
$stringVal = ${eval.isNull} ? null : ${eval.value}; | ||
break; | ||
""" | ||
}.mkString("\n") | ||
val indexVal = ctx.freshName("index") | ||
val stringArray = ctx.freshName("strings"); | ||
} | ||
|
||
ev.copy(index.code + "\n" + strings.map(_.code).mkString("\n") + s""" | ||
final int $indexVal = ${index.value}; | ||
UTF8String ${ev.value} = null; | ||
switch ($indexVal) { | ||
$assignStringValue | ||
val cases = ctx.splitCodes(assignStringValue) | ||
val codes = if (cases.length == 1) { | ||
s""" | ||
UTF8String $stringVal = null; | ||
switch ($indexVal) { | ||
${cases.head} | ||
} | ||
""" | ||
} else { | ||
var fullFuncName = "" | ||
cases.reverse.zipWithIndex.map { case (s, index) => | ||
val prevFunc = if (index == 0) { | ||
"null" | ||
} else { | ||
s"$fullFuncName(${ctx.INPUT_ROW}, $indexVal)" | ||
} | ||
val funcName = ctx.freshName("eltFunc") | ||
val funcBody = s""" | ||
private UTF8String $funcName(InternalRow ${ctx.INPUT_ROW}, int $indexVal) { | ||
UTF8String $stringVal = null; | ||
switch ($indexVal) { | ||
$s | ||
default: | ||
return $prevFunc; | ||
} | ||
return $stringVal; | ||
} | ||
""" | ||
fullFuncName = ctx.addNewFunction(funcName, funcBody) | ||
} | ||
s"UTF8String $stringVal = $fullFuncName(${ctx.INPUT_ROW}, ${indexVal});" | ||
} | ||
|
||
ev.copy(index.code + "\n" + | ||
s""" | ||
final int $indexVal = ${index.value}; | ||
$codes | ||
UTF8String ${ev.value} = $stringVal; | ||
final boolean ${ev.isNull} = ${ev.value} == null; | ||
""") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
numArgs
.