-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-22772][SQL] Use splitExpressionsWithCurrentInputs to split codes in elt #19964
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
Changes from 2 commits
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 |
|---|---|---|
|
|
@@ -289,53 +289,61 @@ case class Elt(children: Seq[Expression]) | |
| val index = indexExpr.genCode(ctx) | ||
| val strings = stringExprs.map(_.genCode(ctx)) | ||
| val indexVal = ctx.freshName("index") | ||
|
|
||
| // -1 means the given index doesn't match indices of strings in split function. | ||
| val NOT_MATCHED = -1 | ||
| // 0 means the given index matches one of indices of strings in split function. | ||
| val MATCHED = 0 | ||
| val resultState = ctx.freshName("eltResultState") | ||
|
Contributor
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 can be a
Member
Author
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, right. :-)
Contributor
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. maybe this can have a better name now that it is a boolean.... I am not very good at naming, but something like
Member
Author
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. Ok. |
||
|
|
||
| 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}; | ||
| | $resultState = (byte)$MATCHED; | ||
| | continue; | ||
| |} | ||
| """.stripMargin | ||
| } | ||
|
|
||
| val cases = ctx.buildCodeBlocks(assignStringValue) | ||
|
Contributor
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. now
Member
Author
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.
Contributor
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. 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_BYTE, | ||
| makeSplitFunction = body => | ||
| s""" | ||
| |${ctx.JAVA_BYTE} $resultState = $NOT_MATCHED; | ||
| |do { | ||
| | $body | ||
| |} while (false); | ||
| |return $resultState; | ||
| """.stripMargin, | ||
| foldFunctions = _.map { funcCall => | ||
| s""" | ||
| |$resultState = $funcCall; | ||
| |if ($resultState != $NOT_MATCHED) { | ||
| | 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_BYTE} $resultState = $NOT_MATCHED; | ||
| |$stringVal = ${ctx.defaultValue(dataType)}; | ||
|
Contributor
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: I would prefer
Member
Author
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 have required tests in
Contributor
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. +1, since at the end we do |
||
| |do { | ||
| | $codes | ||
| |} while (false); | ||
| |final UTF8String ${ev.value} = $stringVal; | ||
| |final boolean ${ev.isNull} = ${ev.value} == null; | ||
| """.stripMargin) | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
only 2 possible values, we can use boolean
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.
yea, missing it.