EamesTrinh commented on code in PR #58831:
URL: https://github.com/apache/spark/pull/58831#discussion_r4562078350


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveInsertionBase.scala:
##########
@@ -51,39 +53,79 @@ abstract class ResolveInsertionBase extends 
Rule[LogicalPlan] {
         (queryOutputCol.dataType, resolvedCol.dataType) match {
           case (input: StructType, expected: StructType) =>
             // Rename inner fields of the input column to pass the by-name 
INSERT analysis.
-            Alias(Cast(queryOutputCol, renameFieldsInStruct(input, expected)), 
resolvedCol.name)()
+            val renamedType = renameFieldsInStruct(tblName, input, expected, 
Seq(resolvedCol.name))
+            Alias(Cast(queryOutputCol, renamedType), resolvedCol.name)()
           case (input: ArrayType, expected: ArrayType) =>
-            Alias(Cast(queryOutputCol, renameFieldsInType(input, expected)), 
resolvedCol.name)()
+            val renamedType = renameFieldsInType(tblName, input, expected, 
Seq(resolvedCol.name))
+            Alias(Cast(queryOutputCol, renamedType), resolvedCol.name)()
           case (input: MapType, expected: MapType) =>
-            Alias(Cast(queryOutputCol, renameFieldsInType(input, expected)), 
resolvedCol.name)()
+            val renamedType = renameFieldsInType(tblName, input, expected, 
Seq(resolvedCol.name))
+            Alias(Cast(queryOutputCol, renamedType), resolvedCol.name)()
           case _ =>
             Alias(queryOutputCol, resolvedCol.name)()
         }
       }
     Project(projectByName, i.query)
   }
 
-  private def renameFieldsInStruct(input: StructType, expected: StructType): 
StructType = {
-    if (input.length == expected.length) {
-      val newFields = input.zip(expected).map { case (f1, f2) =>
-        f1.copy(name = f2.name, dataType = renameFieldsInType(f1.dataType, 
f2.dataType))
+  private def renameFieldsInStruct(
+      tableName: String,
+      input: StructType,
+      expected: StructType,
+      colPath: Seq[String]): StructType = {
+    if (input.length == expected.length || resolveDifferentArityByPosition) {
+      val renamedFields = input.zip(expected).map { case (f1, f2) =>
+        f1.copy(
+          name = f2.name,

Review Comment:
   Agreed this is a valid optimization opportunity. The equal-arity path 
already produces the same field-renaming cast shape, and the additional impact 
here is limited to case-only different-arity inputs. I’m keeping the current 
behavior in this correctness fix and deferring cast simplification to a 
separate optimizer follow-up.



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