Skip to content

Commit 46a6c20

Browse files
committed
[SPARK-9401][SQL] codeGen concatWs
1 parent 708794e commit 46a6c20

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,32 @@ case class ConcatWs(children: Seq[Expression])
114114
boolean ${ev.isNull} = ${ev.primitive} == null;
115115
"""
116116
} else {
117-
// Contains a mix of strings and array<string>s. Fall back to interpreted mode for now.
118-
super.genCode(ctx, ev)
117+
val list = ctx.freshName("list")
118+
val array = ctx.freshName("array")
119+
val sep = children.head.gen(ctx)
120+
val argsCode = children.tail.map(x => {
121+
val gen = x.gen(ctx)
122+
if (x.dataType == StringType) {
123+
s"""
124+
${gen.code}
125+
$list.add(${gen.primitive});
126+
"""
127+
} else {
128+
s"""
129+
${gen.code}
130+
$list.addAll(scala.collection.JavaConversions.asJavaCollection(${gen.primitive}));
131+
"""
132+
}
133+
}).foldLeft("")((a, b) => a + "\n" + b)
134+
s"""
135+
${sep.code}
136+
java.util.ArrayList<UTF8String> $list = new java.util.ArrayList<UTF8String>();
137+
$argsCode
138+
UTF8String[] $array = new UTF8String[$list.size()];
139+
$list.toArray($array);
140+
UTF8String ${ev.primitive} = UTF8String.concatWs(${sep.primitive}, $array);
141+
boolean ${ev.isNull} = ${ev.primitive} == null;
142+
"""
119143
}
120144
}
121145
}

0 commit comments

Comments
 (0)