-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-22771][SQL] Concatenate binary inputs into a binary output #19977
Changes from 15 commits
628165a
b87e61e
4f8a762
583bc5d
1b57428
5a771f0
8faebdb
de2f808
9ddb231
766e0e6
fbe266c
179c6fd
1c94418
1e13b70
57a9d1e
b9febbd
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 |
---|---|---|
|
@@ -24,11 +24,10 @@ import java.util.regex.Pattern | |
|
||
import scala.collection.mutable.ArrayBuffer | ||
|
||
import org.apache.spark.sql.AnalysisException | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
import org.apache.spark.sql.catalyst.expressions.codegen._ | ||
import org.apache.spark.sql.catalyst.util.{ArrayData, GenericArrayData} | ||
import org.apache.spark.sql.catalyst.util.{ArrayData, GenericArrayData, TypeUtils} | ||
import org.apache.spark.sql.types._ | ||
import org.apache.spark.unsafe.types.{ByteArray, UTF8String} | ||
|
||
|
@@ -38,7 +37,8 @@ import org.apache.spark.unsafe.types.{ByteArray, UTF8String} | |
|
||
|
||
/** | ||
* An expression that concatenates multiple input strings into a single string. | ||
* An expression that concatenates multiple inputs into a single output. | ||
* If all inputs are binary, concat returns an output as binary. Otherwise, it returns as string. | ||
* If any input is null, concat returns null. | ||
*/ | ||
@ExpressionDescription( | ||
|
@@ -48,17 +48,37 @@ import org.apache.spark.unsafe.types.{ByteArray, UTF8String} | |
> SELECT _FUNC_('Spark', 'SQL'); | ||
SparkSQL | ||
""") | ||
case class Concat(children: Seq[Expression]) extends Expression with ImplicitCastInputTypes { | ||
case class Concat(children: Seq[Expression]) extends Expression { | ||
|
||
override def inputTypes: Seq[AbstractDataType] = Seq.fill(children.size)(StringType) | ||
override def dataType: DataType = StringType | ||
private lazy val isBinaryMode: Boolean = dataType == BinaryType | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = { | ||
if (children.isEmpty) { | ||
TypeCheckResult.TypeCheckSuccess | ||
} else { | ||
val childTypes = children.map(_.dataType) | ||
if (childTypes.exists(tpe => !Seq(StringType, BinaryType).contains(tpe))) { | ||
TypeCheckResult.TypeCheckFailure( | ||
s"input to function $prettyName should have StringType or BinaryType, but it's " + | ||
childTypes.map(_.simpleString).mkString("[", ", ", "]")) | ||
} | ||
TypeUtils.checkForSameTypeInputExpr(childTypes, s"function $prettyName") | ||
} | ||
} | ||
|
||
override def dataType: DataType = children.map(_.dataType).headOption.getOrElse(StringType) | ||
|
||
override def nullable: Boolean = children.exists(_.nullable) | ||
override def foldable: Boolean = children.forall(_.foldable) | ||
|
||
override def eval(input: InternalRow): Any = { | ||
val inputs = children.map(_.eval(input).asInstanceOf[UTF8String]) | ||
UTF8String.concat(inputs : _*) | ||
if (isBinaryMode) { | ||
val inputs = children.map(_.eval(input).asInstanceOf[Array[Byte]]) | ||
ByteArray.concat(inputs: _*) | ||
} else { | ||
val inputs = children.map(_.eval(input).asInstanceOf[UTF8String]) | ||
UTF8String.concat(inputs : _*) | ||
} | ||
} | ||
|
||
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
|
@@ -73,17 +93,27 @@ case class Concat(children: Seq[Expression]) extends Expression with ImplicitCas | |
} | ||
""" | ||
} | ||
|
||
val (concatenator, initCode) = if (isBinaryMode) { | ||
(classOf[ByteArray].getName, s"byte[][] $args = new byte[${evals.length}][];") | ||
} else { | ||
("UTF8String", s"UTF8String[] $args = new UTF8String[${evals.length}];") | ||
} | ||
val codes = ctx.splitExpressionsWithCurrentInputs( | ||
expressions = inputs, | ||
funcName = "valueConcat", | ||
extraArguments = ("UTF8String[]", args) :: Nil) | ||
extraArguments = (s"${ctx.javaType(dataType)}[]", args) :: Nil) | ||
ev.copy(s""" | ||
UTF8String[] $args = new UTF8String[${evals.length}]; | ||
$initCode | ||
$codes | ||
UTF8String ${ev.value} = UTF8String.concat($args); | ||
${ctx.javaType(dataType)} ${ev.value} = $concatenator.concat($args); | ||
boolean ${ev.isNull} = ${ev.value} == null; | ||
""") | ||
} | ||
|
||
override def toString: String = s"concat(${children.mkString(", ")})" | ||
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. Also need to override |
||
|
||
override def sql: String = s"concat(${children.map(_.sql).mkString(", ")})" | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import scala.collection.immutable.HashSet | |
import scala.collection.mutable.{ArrayBuffer, Stack} | ||
|
||
import org.apache.spark.sql.catalyst.analysis._ | ||
import org.apache.spark.sql.catalyst.analysis.TypeCoercion.ImplicitTypeCasts | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral} | ||
import org.apache.spark.sql.catalyst.expressions.aggregate._ | ||
|
@@ -645,6 +646,12 @@ object CombineConcats extends Rule[LogicalPlan] { | |
stack.pop() match { | ||
case Concat(children) => | ||
stack.pushAll(children.reverse) | ||
// If `spark.sql.function.concatBinaryAsString` is false, nested `Concat` exprs possibly | ||
// have `Concat`s with binary output. Since `TypeCoercion` casts them into strings, | ||
// we need to handle the case to combine all nested `Concat`s. | ||
case c @ Cast(Concat(children), StringType, _) => | ||
val newChildren = children.map { e => c.copy(child = e) } | ||
stack.pushAll(newChildren.reverse) | ||
case child => | ||
flattened += child | ||
} | ||
|
@@ -653,7 +660,11 @@ object CombineConcats extends Rule[LogicalPlan] { | |
} | ||
|
||
def apply(plan: LogicalPlan): LogicalPlan = plan.transformExpressionsDown { | ||
case concat: Concat if concat.children.exists(_.isInstanceOf[Concat]) => | ||
case concat: Concat if concat.children.exists { | ||
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. Create a dedicated helper function for the if condition? 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 |
||
case c: Concat => true | ||
case c @ Cast(Concat(children), StringType, _) => true | ||
case _ => false | ||
} => | ||
flattenConcats(concat) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2171,7 +2171,8 @@ object functions { | |
def base64(e: Column): Column = withExpr { Base64(e.expr) } | ||
|
||
/** | ||
* Concatenates multiple input string columns together into a single string column. | ||
* Concatenates multiple input columns together into a single column. | ||
* If all inputs are binary, concat returns an output as binary. Otherwise, it returns as string. | ||
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. shall we update document for python and R? 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 |
||
* | ||
* @group string_funcs | ||
* @since 1.5.0 | ||
|
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.
null check here too?
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.
aha, I see.UTF8String
seems to need the same null check?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.
nvm. I think we already have checked here https://github.com/apache/spark/pull/19977/files#diff-6df3223f9826f2b5d1d0c8e29a240ae3R82