mihailom-db commented on code in PR #45383:
URL: https://github.com/apache/spark/pull/45383#discussion_r1513956640


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala:
##########
@@ -764,6 +782,91 @@ 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 expression 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 be.
+     */
+    def getOutputCollation(exprs: Seq[Expression], failOnIndeterminate: 
Boolean = true): Int = {
+      val explicitTypes = 
exprs.filter(hasExplicitCollation).map(_.dataType).distinct
+
+      explicitTypes.size match {
+        case 1 => explicitTypes.head.asInstanceOf[StringType].collationId
+        case size if size > 1 => throw 
QueryCompilationErrors.explicitCollationMismatchError(
+          explicitTypes.head.simpleString, 
explicitTypes.tail.head.simpleString)
+        case _ =>
+          val dataTypes = exprs.map(_.dataType.asInstanceOf[StringType])
+
+          if (isIndeterminate(dataTypes)) {
+            if (failOnIndeterminate) {
+              throw QueryCompilationErrors.indeterminateCollationError()
+            } else {
+              CollationFactory.INDETERMINATE_COLLATION_ID
+            }
+          }
+          else if (hasMultipleImplicits(dataTypes)) {
+            if (failOnIndeterminate) {
+              throw QueryCompilationErrors.implicitCollationMismatchError()
+            } else {
+              CollationFactory.INDETERMINATE_COLLATION_ID
+            }
+          }
+          else {
+            dataTypes.find(!_.isDefaultCollation)
+              .getOrElse(StringType)
+              .collationId
+          }
+      }
+    }
+
+    private def isIndeterminate(dataTypes: Seq[StringType]): Boolean =
+      dataTypes.exists(_.isIndeterminateCollation)
+
+
+    private def hasMultipleImplicits(dataTypes: Seq[StringType]): Boolean =
+      dataTypes.filter(!_.isDefaultCollation).distinct.size > 1
+
+    private def hasExplicitCollation(expression: Expression): Boolean = {
+      if (!expression.dataType.isInstanceOf[StringType]) {
+        false
+      }
+      else {
+        expression match {
+          case _: Collate => true

Review Comment:
   This is the only possible change for here. As `Collate` constructor expects 
2 parameters and scala does not want to match only on `Collate`



-- 
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]

Reply via email to