dbatomic commented on code in PR #45383:
URL: https://github.com/apache/spark/pull/45383#discussion_r1530286778
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -764,6 +773,184 @@ abstract class TypeCoercionBase {
}
}
+ object CollationTypeCasts extends TypeCoercionRule {
+ override val transform: PartialFunction[Expression, Expression] = {
+ case e if !e.childrenResolved => e
+
+ case checkCastWithIndeterminate: Concat
+ if shouldCast(checkCastWithIndeterminate.children) =>
+ val newChildren =
+ collateToSingleType(checkCastWithIndeterminate.children,
failOnIndeterminate = false)
+ checkCastWithIndeterminate.withNewChildren(newChildren)
+
+ case checkCastWithoutIndeterminate@(_: BinaryExpression | _: In | _:
SortOrder)
+ if shouldCast(checkCastWithoutIndeterminate.children) =>
+ val newChildren =
collateToSingleType(checkCastWithoutIndeterminate.children)
+ checkCastWithoutIndeterminate.withNewChildren(newChildren)
+
+ case checkIndeterminate@(_: BinaryExpression | _: In | _: SortOrder)
+ if hasIndeterminate(checkIndeterminate.children
+ .filter(e => hasStringType(e.dataType))
+ .map(e => extractStringType(e.dataType))) =>
+ throw QueryCompilationErrors.indeterminateCollationError()
+
+ case checkImplicitCastInputTypes: ImplicitCastInputTypes
+ if checkImplicitCastInputTypes.children.exists(e =>
hasStringType(e.dataType))
+ && checkImplicitCastInputTypes.inputTypes.nonEmpty =>
+ val collationId: Int =
+ getOutputCollation(checkImplicitCastInputTypes
+ .children.filter { e => hasStringType(e.dataType) })
+ val children: Seq[Expression] = checkImplicitCastInputTypes
+ .children.zip(checkImplicitCastInputTypes.inputTypes).map {
+ case (e, st) if hasStringType(st) =>
+ castStringType(e, collationId, Some(st)).getOrElse(e)
+ case (e, TypeCollection(types)) if types.exists(hasStringType) =>
+ types.flatMap{ dt =>
+ if (hasStringType(dt)) {
+ castStringType(e, collationId, Some(dt))
+ } else {
+ implicitCast(e, dt)
+ }
+ }.headOption.getOrElse(e)
+ case (in, expected) => implicitCast(in, expected).getOrElse(in)
+ }
+ checkImplicitCastInputTypes.withNewChildren(children)
+
+ case checkExpectsInputType: ExpectsInputTypes
+ if checkExpectsInputType.children.exists(e =>
hasStringType(e.dataType))
+ && checkExpectsInputType.inputTypes.nonEmpty =>
+ val collationId: Int = getOutputCollation(
+ checkExpectsInputType.children.filter {e =>
hasStringType(e.dataType)})
+ val children: Seq[Expression] = checkExpectsInputType
+ .children.zip(checkExpectsInputType.inputTypes).map {
+ case (st, _) if hasStringType(st.dataType) =>
+ castStringType(st, collationId).getOrElse(st)
+ case (nt, e)
+ if hasStringType(e) && nt.dataType == NullType =>
+ castStringType(nt, collationId, Some(e)).getOrElse(nt)
+ case (nt, e: TypeCollection)
+ if hasStringType(e.defaultConcreteType) && nt.dataType ==
NullType =>
+ castStringType(nt, collationId,
Some(e.defaultConcreteType)).getOrElse(nt)
+ case (in, _) => in
+ }
+ checkExpectsInputType.withNewChildren(children)
+ }
+
+ def shouldCast(types: Seq[Expression]): Boolean = {
+ types.filter(e => hasStringType(e.dataType))
+ .map(e => extractStringType(e.dataType).collationId).distinct.size > 1
+ }
+
+ /**
+ * Whether the data type contains StringType.
+ */
+ @tailrec
Review Comment:
You have `existsRecursively` which we use for similar purpose in QP side.
E.g.
```scala
def isBinaryStable(dataType: DataType): Boolean =
dataType.existsRecursively {
case st: StringType =>
CollationFactory.fetchCollation(st.collationId).isBinaryCollation
case _ => true
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]