You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/08/12 16:02:33 UTC

[GitHub] [spark] dilipbiswal commented on a change in pull request #33728: [SPARK-36498][SQL] Reorder inner fields of the input query in byName V2 write

dilipbiswal commented on a change in pull request #33728:
URL: https://github.com/apache/spark/pull/33728#discussion_r687873492



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TableOutputResolver.scala
##########
@@ -74,6 +66,42 @@ object TableOutputResolver {
     }
   }
 
+  private def reorderColumnsByName(
+      inputCols: Seq[NamedExpression],
+      expectedCols: Seq[Attribute],
+      conf: SQLConf,
+      addError: String => Unit,
+      colPath: Seq[String] = Nil): Seq[NamedExpression] = {
+    expectedCols.flatMap { expectedCol =>
+      val matched = inputCols.filter(col => conf.resolver(col.name, expectedCol.name))
+      val newColPath = colPath :+ expectedCol.name
+      if (matched.isEmpty) {
+        addError(s"Cannot find data for output column '${newColPath.quoted}'")
+        None
+      } else if (matched.length > 1) {
+        addError(s"Ambiguous column name in the input data: '${newColPath.quoted}'")
+        None
+      } else {
+        val matchedCol = matched.head match {
+          case a: Attribute => a.withName(expectedCol.name)
+          case a: Alias => a.withName(expectedCol.name)
+          case other => other
+        }
+        (matchedCol.dataType, expectedCol.dataType) match {
+          case (input: StructType, expected: StructType) =>
+            val fields = input.zipWithIndex.map { case (f, i) =>
+              Alias(GetStructField(matchedCol, i, Some(f.name)), f.name)()
+            }
+            val reordered = reorderColumnsByName(
+              fields, expected.toAttributes, conf, addError, newColPath)
+            Some(Alias(CreateStruct(reordered), expectedCol.name)())
+          case _ =>

Review comment:
       @cloud-fan Thanks for the fix !! I am thinking, do we need cases for Array and Map types ? Like for cases.
   Struct<a: Array<Struct>> . Don't we need to create the arrays and maps from the remapped inner structs ?




-- 
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-unsubscribe@spark.apache.org

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



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