Skip to content
Closed
Changes from 2 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 @@ -72,7 +72,7 @@ case class Concat(children: Seq[Expression]) extends Expression with ImplicitCas
* Returns null if the separator is null. Otherwise, concat_ws skips all null values.
*/
case class ConcatWs(children: Seq[Expression])
extends Expression with ImplicitCastInputTypes with CodegenFallback {
extends Expression with ImplicitCastInputTypes {

require(children.nonEmpty, s"$prettyName requires at least one argument.")

Expand Down Expand Up @@ -114,8 +114,44 @@ case class ConcatWs(children: Seq[Expression])
boolean ${ev.isNull} = ${ev.primitive} == null;
"""
} else {
// Contains a mix of strings and array<string>s. Fall back to interpreted mode for now.
super.genCode(ctx, ev)
val array = ctx.freshName("array")
val varargNum = ctx.freshName("varargNum")
val idxInVararg = ctx.freshName("idxInVararg")

val evals = children.map(_.gen(ctx))
val (varargCount, varargBuild) = children.tail.zip(evals.tail).map { case (child, eval) =>
child.dataType match {
case StringType =>
(s"$varargNum ++;",
s"$array[$idxInVararg ++] = ${eval.isNull} ? (UTF8String) null : ${eval.primitive};")
case _: ArrayType =>
val size = ctx.freshName("n")
(s"""
if (!${eval.isNull}) {
$varargNum += ${eval.primitive}.numElements();
}
""",
s"""
if (!${eval.isNull}) {
final int $size = ${eval.primitive}.numElements();
for (int j = 0; j < $size; j ++) {
$array[$idxInVararg ++] = ${ctx.getValue(eval.primitive, StringType, "j")};
}
}
""")
}
}.unzip

evals.map(_.code).mkString("\n") +
s"""
int $varargNum = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be the number of vargs that is UTF8String (${children.count(_ == StringType) - 1} ).

Copy link
Member Author

Choose a reason for hiding this comment

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

I use this as the total num of strings, stringType + arrayType's element num.

Copy link
Contributor

Choose a reason for hiding this comment

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

I means we can get rid of some many num ++

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, yes, I get this.

Copy link
Member Author

Choose a reason for hiding this comment

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

@davies , more comments on this PR?

int $idxInVararg = 0;
${varargCount.mkString("\n")}
UTF8String[] $array = new UTF8String[$varargNum];
${varargBuild.mkString("\n")}
UTF8String ${ev.primitive} = UTF8String.concatWs(${evals.head.primitive}, $array);
boolean ${ev.isNull} = ${ev.primitive} == null;
"""
}
}
}
Expand Down