stefankandic commented on code in PR #45383:
URL: https://github.com/apache/spark/pull/45383#discussion_r1519323364
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -764,6 +773,94 @@ abstract class TypeCoercionBase {
}
}
+ object CollationTypeCasts extends TypeCoercionRule {
+ override def transform: PartialFunction[Expression, Expression] = {
+ case e if !e.childrenResolved => e
+
+ case b @ BinaryComparison(left, right) if shouldCast(Seq(left.dataType,
right.dataType)) =>
+ val newChildren = collateToSingleType(Seq(left, right))
+ b.withNewChildren(newChildren)
+ }
+
+ def shouldCast(types: Seq[DataType]): Boolean = {
+ types.forall(_.isInstanceOf[StringType]) && types.distinct.length > 1
+ }
+
+ /**
+ * Collates the input expressions to a single collation.
+ */
+ def collateToSingleType(exprs: Seq[Expression]): Seq[Expression] = {
+ val collationId = getOutputCollation(exprs)
+
+ exprs.map { expression =>
+ expression.dataType match {
+ case st: StringType if st.collationId == collationId =>
+ expression
+ case _: StringType =>
+ Cast(expression, StringType(collationId))
+ }
+ }
+ }
+
+ /**
+ * Based on the data types of the input expressions this method determines
+ * a collation type which the output will have.
+ */
+ def getOutputCollation(exprs: Seq[Expression], failOnIndeterminate:
Boolean = true): Int = {
+ val explicitTypes =
exprs.filter(hasExplicitCollation).map(_.dataType).distinct
+
+ explicitTypes.size match {
Review Comment:
i thought about this for a bit and we should probably improve this logic so
that the explicit collation has the utmost precedence, ie if there is a one
input with explicit and another with indeterminate the output should be
explicit as well
the precedence hierarchy should probably go like this:
1. no collation
2. implicit collation
3. indeterminate collation
4. explicit collation
what do you think about this approach? It seems to me that this is the pgsql
behaviour as well
--
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]