@@ -137,43 +137,19 @@ case class ConcatWs(children: Seq[Expression])
137137 if (children.forall(_.dataType == StringType )) {
138138 // All children are strings. In that case we can construct a fixed size array.
139139 val evals = children.map(_.genCode(ctx))
140- val separator = evals.head
141- val strings = evals.tail
142- val numArgs = strings.length
143- val args = ctx.freshName(" args" )
144-
145- val inputs = strings.zipWithIndex.map { case (eval, index) =>
146- if (eval.isNull != " true" ) {
147- s """
148- ${eval.code}
149- if (! ${eval.isNull}) {
150- $args[ $index] = ${eval.value};
151- }
152- """
153- } else {
154- " "
155- }
156- }
157- val codes = if (ctx.INPUT_ROW != null && ctx.currentVars == null ) {
158- ctx.splitExpressions(inputs, " valueConcatWs" ,
159- (" InternalRow" , ctx.INPUT_ROW ) :: (" UTF8String[]" , args) :: Nil )
160- } else {
161- inputs.mkString(" \n " )
162- }
163- ev.copy(s """
164- UTF8String[] $args = new UTF8String[ $numArgs];
165- ${separator.code}
166- $codes
167- UTF8String ${ev.value} = UTF8String.concatWs( ${separator.value}, $args);
140+
141+ val inputs = evals.map { eval =>
142+ s " ${eval.isNull} ? (UTF8String) null : ${eval.value}"
143+ }.mkString(" , " )
144+
145+ ev.copy(evals.map(_.code).mkString(" \n " ) + s """
146+ UTF8String ${ev.value} = UTF8String.concatWs( $inputs);
168147 boolean ${ev.isNull} = ${ev.value} == null;
169148 """ )
170149 } else {
171150 val array = ctx.freshName(" array" )
172- ctx.addMutableState(" UTF8String[]" , array, " " )
173151 val varargNum = ctx.freshName(" varargNum" )
174- ctx.addMutableState(" int" , varargNum, " " )
175152 val idxInVararg = ctx.freshName(" idxInVararg" )
176- ctx.addMutableState(" int" , idxInVararg, " " )
177153
178154 val evals = children.map(_.genCode(ctx))
179155 val (varargCount, varargBuild) = children.tail.zip(evals.tail).map { case (child, eval) =>
@@ -199,17 +175,13 @@ case class ConcatWs(children: Seq[Expression])
199175 }
200176 }.unzip
201177
202- val codes = ctx.splitExpressions(ctx.INPUT_ROW , evals.map(_.code))
203- val varargCounts = ctx.splitExpressions(ctx.INPUT_ROW , varargCount)
204- val varargBuilds = ctx.splitExpressions(ctx.INPUT_ROW , varargBuild)
205- ev.copy(
178+ ev.copy(evals.map(_.code).mkString(" \n " ) +
206179 s """
207- $codes
208- $varargNum = ${children.count(_.dataType == StringType ) - 1 };
209- $idxInVararg = 0;
210- $varargCounts
211- $array = new UTF8String[ $varargNum];
212- $varargBuilds
180+ int $varargNum = ${children.count(_.dataType == StringType ) - 1 };
181+ int $idxInVararg = 0;
182+ ${varargCount.mkString(" \n " )}
183+ UTF8String[] $array = new UTF8String[ $varargNum];
184+ ${varargBuild.mkString(" \n " )}
213185 UTF8String ${ev.value} = UTF8String.concatWs( ${evals.head.value}, $array);
214186 boolean ${ev.isNull} = ${ev.value} == null;
215187 """ )
@@ -264,55 +236,22 @@ case class Elt(children: Seq[Expression])
264236 override protected def doGenCode (ctx : CodegenContext , ev : ExprCode ): ExprCode = {
265237 val index = indexExpr.genCode(ctx)
266238 val strings = stringExprs.map(_.genCode(ctx))
267- val indexVal = ctx.freshName(" index" )
268- val stringVal = ctx.freshName(" stringVal" )
269239 val assignStringValue = strings.zipWithIndex.map { case (eval, index) =>
270240 s """
271241 case ${index + 1 }:
272- ${eval.code}
273- $stringVal = ${eval.isNull} ? null : ${eval.value};
242+ ${ev.value} = ${eval.isNull} ? null : ${eval.value};
274243 break;
275244 """
276- }
277-
278- val cases = ctx.splitCodes(assignStringValue)
279- val codes = if (cases.length == 1 ) {
280- s """
281- UTF8String $stringVal = null;
282- switch ( $indexVal) {
283- ${cases.head}
284- }
285- """
286- } else {
287- var fullFuncName = " "
288- cases.reverse.zipWithIndex.map { case (s, index) =>
289- val prevFunc = if (index == 0 ) {
290- " null"
291- } else {
292- s " $fullFuncName( ${ctx.INPUT_ROW }, $indexVal) "
293- }
294- val funcName = ctx.freshName(" eltFunc" )
295- val funcBody = s """
296- private UTF8String $funcName(InternalRow ${ctx.INPUT_ROW }, int $indexVal) {
297- UTF8String $stringVal = null;
298- switch ( $indexVal) {
299- $s
300- default:
301- return $prevFunc;
302- }
303- return $stringVal;
304- }
305- """
306- fullFuncName = ctx.addNewFunction(funcName, funcBody)
307- }
308- s " UTF8String $stringVal = $fullFuncName( ${ctx.INPUT_ROW }, ${indexVal}); "
309- }
245+ }.mkString(" \n " )
246+ val indexVal = ctx.freshName(" index" )
247+ val stringArray = ctx.freshName(" strings" );
310248
311- ev.copy(index.code + " \n " +
312- s """
249+ ev.copy(index.code + " \n " + strings.map(_.code).mkString(" \n " ) + s """
313250 final int $indexVal = ${index.value};
314- $codes
315- UTF8String ${ev.value} = $stringVal;
251+ UTF8String ${ev.value} = null;
252+ switch ( $indexVal) {
253+ $assignStringValue
254+ }
316255 final boolean ${ev.isNull} = ${ev.value} == null;
317256 """ )
318257 }
0 commit comments