vladimirg-db commented on code in PR #50192:
URL: https://github.com/apache/spark/pull/50192#discussion_r1986999174


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -4082,3 +4084,31 @@ object RemoveTempResolvedColumn extends 
Rule[LogicalPlan] {
     }
   }
 }
+
+object ReassignAliasNamesWithCollations extends Rule[LogicalPlan] {
+  override def apply(plan: LogicalPlan): LogicalPlan = {
+    plan.resolveExpressionsWithPruning(_.containsPattern(ALIAS)) {
+      case a: Alias if
+          a.resolved &&
+          a.metadata.contains(AUTO_GENERATED_ALIAS) &&
+          hasNonDefaultCollationInTheSubtree(a.child) =>
+        val newName = toPrettySQL(a.child)
+        if (newName != a.name) {
+          a.withName(newName)
+        } else {
+          a
+        }
+    }
+  }
+
+  private def hasNonDefaultCollationInTheSubtree(rootExpression: Expression) = 
{
+    rootExpression.exists {
+      case expression =>
+        val dataType = try { Some(expression.dataType) } catch { case ex: 
Throwable => None }

Review Comment:
   Some expressions don't support calling `dataType` on them. Any idea how to 
implement that cleanly?



##########
sql/core/src/test/resources/sql-tests/analyzer-results/collations.sql.out:
##########
@@ -821,15 +821,15 @@ InsertIntoHadoopFsRelationCommand file:[not included in 
comparison]/{warehouse_d
 -- !query
 select concat_ws(' ', utf8_lcase, utf8_lcase) from t5
 -- !query analysis
-Project [concat_ws( , utf8_lcase#x, utf8_lcase#x) AS concat_ws( , utf8_lcase, 
utf8_lcase)#x]
+Project [concat_ws( , utf8_lcase#x, utf8_lcase#x) AS concat_ws(' ' collate 
UTF8_LCASE, utf8_lcase, utf8_lcase)#x]
 +- SubqueryAlias spark_catalog.default.t5
    +- Relation spark_catalog.default.t5[s#x,utf8_binary#x,utf8_lcase#x] parquet
 
 
 -- !query
 select concat_ws(' ', utf8_binary, utf8_lcase) from t5
 -- !query analysis
-Project [concat_ws( , cast(utf8_binary#x as string collate null), 
cast(utf8_lcase#x as string collate null)) AS concat_ws( , utf8_binary, 
utf8_lcase)#x]
+Project [concat_ws( , cast(utf8_binary#x as string collate null), 
cast(utf8_lcase#x as string collate null)) AS concat_ws(' ' collate null, 
utf8_binary, utf8_lcase)#x]

Review Comment:
   I wonder why we get `collate null` here...



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -451,6 +451,8 @@ class Analyzer(override val catalogManager: CatalogManager) 
extends RuleExecutor
       RewriteMergeIntoTable),
     Batch("Subquery", Once,
       UpdateOuterReferences),
+    Batch("ReassignAliasNamesWithCollations", Once,

Review Comment:
   @cloud-fan @stefankandic @mihailom-db please comment what are your opinions 
on that approach?



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to