Github user davies commented on a diff in the pull request:
https://github.com/apache/spark/pull/7504#discussion_r34955580
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
---
@@ -56,15 +51,76 @@ case class Concat(children: Seq[Expression]) extends
Expression with ImplicitCas
override protected def genCode(ctx: CodeGenContext, ev:
GeneratedExpressionCode): String = {
val evals = children.map(_.gen(ctx))
- val inputs = evals.map { eval => s"${eval.isNull} ? null :
${eval.primitive}" }.mkString(", ")
+ val inputs = evals.map { eval =>
+ s"${eval.isNull} ? (UTF8String)null : ${eval.primitive}"
+ }.mkString(", ")
evals.map(_.code).mkString("\n") + s"""
boolean ${ev.isNull} = false;
UTF8String ${ev.primitive} = UTF8String.concat($inputs);
+ if (${ev.primitive} == null) {
+ ${ev.isNull} = true;
+ }
"""
}
}
+/**
+ * An expression that concatenates multiple input strings or array of
strings into a single string,
+ * using a given separator (the first child).
+ *
+ * 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 {
+
+ require(children.nonEmpty, s"$prettyName requires at least one
argument.")
+
+ override def prettyName: String = "concat_ws"
+
+ /** The 1st child (separator) is str, and rest are either str or array
of str. */
+ override def inputTypes: Seq[AbstractDataType] = {
+ val arrayOrStr = TypeCollection(ArrayType(StringType), StringType)
+ StringType +: Seq.fill(children.size - 1)(arrayOrStr)
+ }
+
+ override def dataType: DataType = StringType
+
+ override def nullable: Boolean = children.head.nullable
+ override def foldable: Boolean = children.forall(_.foldable)
+
+ override def eval(input: InternalRow): Any = {
+ val flatInputs = children.flatMap { child =>
+ child.eval(input) match {
+ case s: UTF8String => Iterator(s)
+ case arr: Seq[_] => arr.asInstanceOf[Seq[UTF8String]]
+ case null => Iterator(null.asInstanceOf[UTF8String])
--- End diff --
minor: Can we just ignore the `null`?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]