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



##########
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:
       I think these case are already covered in the clause.




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