-
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 13 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 |
---|---|---|
|
@@ -27,6 +27,7 @@ import org.apache.spark.sql.catalyst.expressions._ | |
import org.apache.spark.sql.catalyst.expressions.aggregate._ | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types._ | ||
|
||
|
||
|
@@ -45,13 +46,14 @@ import org.apache.spark.sql.types._ | |
*/ | ||
object TypeCoercion { | ||
|
||
val typeCoercionRules = | ||
def typeCoercionRules(conf: SQLConf): List[Rule[LogicalPlan]] = | ||
InConversion :: | ||
WidenSetOperationTypes :: | ||
PromoteStrings :: | ||
DecimalPrecision :: | ||
BooleanEquality :: | ||
FunctionArgumentConversion :: | ||
ConcatCoercion(conf) :: | ||
CaseWhenCoercion :: | ||
IfCoercion :: | ||
StackCoercion :: | ||
|
@@ -658,6 +660,29 @@ object TypeCoercion { | |
} | ||
} | ||
|
||
/** | ||
* Coerces the types of [[Concat]] children to expected ones. | ||
* | ||
* If `spark.sql.function.concatBinaryAsString` is false and all children types are binary, | ||
* the expected types are binary. Otherwise, the expected ones are strings. | ||
*/ | ||
case class ConcatCoercion(conf: SQLConf) extends TypeCoercionRule { | ||
|
||
override protected def coerceTypes(plan: LogicalPlan): LogicalPlan = plan transform { case p => | ||
p transformExpressionsUp { | ||
// Skip nodes if unresolved or empty children | ||
case c @ Concat(children) if !c.childrenResolved || children.isEmpty => c | ||
|
||
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. Remove this line? 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. Probably, we cant cuz we hit unresolved calls. 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. The empty line. 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(children) if conf.concatBinaryAsString || | ||
!children.map(_.dataType).forall(_ == BinaryType) => | ||
val newChildren = c.children.map { e => | ||
ImplicitTypeCasts.implicitCast(e, StringType).getOrElse(e) | ||
} | ||
c.copy(children = newChildren) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Turns Add/Subtract of DateType/TimestampType/StringType and CalendarIntervalType | ||
* to TimeAdd/TimeSub | ||
|
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 |
---|---|---|
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,17 @@ select left("abcd", 2), left("abcd", 5), left("abcd", '2'), left("abcd", null); | |
select left(null, -2), left("abcd", -2), left("abcd", 0), left("abcd", 'a'); | ||
select right("abcd", 2), right("abcd", 5), right("abcd", '2'), right("abcd", null); | ||
select right(null, -2), right("abcd", -2), right("abcd", 0), right("abcd", 'a'); | ||
|
||
-- turn on concatBinaryAsString | ||
set spark.sql.function.concatBinaryAsString=false; | ||
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. turn on? 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. Since most of other dbms-like systems concat binary inputs as binary, IMO turning off by default is okay to me. 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. I meant you said turn on in the comment (L28). 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. oh.... |
||
|
||
-- Check if catalyst combine nested `Concat`s if concatBinaryAsString=false | ||
EXPLAIN SELECT ((col1 || col2) || (col3 || col4)) col | ||
FROM ( | ||
SELECT | ||
string(id) col1, | ||
string(id + 1) col2, | ||
encode(string(id + 2), 'utf-8') col3, | ||
encode(string(id + 3), 'utf-8') col4 | ||
FROM range(10) | ||
); |
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