Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ class CodegenContext {
*
* @param expressions the codes to evaluate expressions.
*/
def buildCodeBlocks(expressions: Seq[String]): Seq[String] = {
private def buildCodeBlocks(expressions: Seq[String]): Seq[String] = {
val blocks = new ArrayBuffer[String]()
val blockBuilder = new StringBuilder()
var length = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,53 +289,56 @@ case class Elt(children: Seq[Expression])
val index = indexExpr.genCode(ctx)
val strings = stringExprs.map(_.genCode(ctx))
val indexVal = ctx.freshName("index")
val indexMatched = ctx.freshName("eltIndexMatched")

val stringVal = ctx.freshName("stringVal")
ctx.addMutableState(ctx.javaType(dataType), stringVal)

val assignStringValue = strings.zipWithIndex.map { case (eval, index) =>
s"""
case ${index + 1}:
${eval.code}
$stringVal = ${eval.isNull} ? null : ${eval.value};
break;
"""
|if ($indexVal == ${index + 1}) {
| ${eval.code}
| $stringVal = ${eval.isNull} ? null : ${eval.value};
| $indexMatched = true;
| continue;
|}
""".stripMargin
}

val cases = ctx.buildCodeBlocks(assignStringValue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now ctx.buildCodeBlock doesn't need to be a separate method, can we revert that change and inline buildCodeBlock to splitExpressions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

splitExpressions is quite complicated. I think it is still good to have buildCodeBlock as a separate method. Maybe makes it as private?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

val codes = if (cases.length == 1) {
s"""
UTF8String $stringVal = null;
switch ($indexVal) {
${cases.head}
}
"""
} else {
var prevFunc = "null"
for (c <- cases.reverse) {
val funcName = ctx.freshName("eltFunc")
val funcBody = s"""
private UTF8String $funcName(InternalRow ${ctx.INPUT_ROW}, int $indexVal) {
UTF8String $stringVal = null;
switch ($indexVal) {
$c
default:
return $prevFunc;
}
return $stringVal;
}
"""
val fullFuncName = ctx.addNewFunction(funcName, funcBody)
prevFunc = s"$fullFuncName(${ctx.INPUT_ROW}, $indexVal)"
}
s"UTF8String $stringVal = $prevFunc;"
}
val codes = ctx.splitExpressionsWithCurrentInputs(
expressions = assignStringValue,
funcName = "eltFunc",
extraArguments = ("int", indexVal) :: Nil,
returnType = ctx.JAVA_BOOLEAN,
makeSplitFunction = body =>
s"""
|${ctx.JAVA_BOOLEAN} $indexMatched = false;
|do {
| $body
|} while (false);
|return $indexMatched;
""".stripMargin,
foldFunctions = _.map { funcCall =>
s"""
|$indexMatched = $funcCall;
|if ($indexMatched) {
| continue;
|}
""".stripMargin
}.mkString)

ev.copy(
s"""
${index.code}
final int $indexVal = ${index.value};
$codes
UTF8String ${ev.value} = $stringVal;
final boolean ${ev.isNull} = ${ev.value} == null;
""")
|${index.code}
|final int $indexVal = ${index.value};
|${ctx.JAVA_BOOLEAN} $indexMatched = false;
|$stringVal = null;
|do {
| $codes
|} while (false);
|final UTF8String ${ev.value} = $stringVal;
|final boolean ${ev.isNull} = ${ev.value} == null;
""".stripMargin)
}
}

Expand Down