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


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/InsertIntoTests.scala:
##########
@@ -736,6 +736,221 @@ trait InsertIntoSchemaEvolutionTests { this: 
InsertIntoTests =>
     }
   }
 
+  Seq(
+    true -> Row(1, 2, 3),
+    false -> Row(2, 1, 3)
+  ).foreach { case (resolveDifferentArityByPosition, expected) =>
+    test("Insert schema evolution: column list resolves nested fields by 
position - " +
+      s"differentArityByPosition=$resolveDifferentArityByPosition") {
+      withSQLConf(
+          
SQLConf.INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION.key ->
+            resolveDifferentArityByPosition.toString) {
+        val t1 = s"${catalogAndNamespace}tbl"
+        withTable(t1) {
+          sql(s"""CREATE TABLE $t1 (
+               |  s struct<x: int, y: int>,
+               |  arr array<struct<x: int, y: int>>,
+               |  m map<string, struct<x: int, y: int>>)
+               |USING $v2Format""".stripMargin)
+          sql(
+            s"""INSERT WITH SCHEMA EVOLUTION INTO $t1 (s, arr, m)
+               |SELECT
+               |  named_struct('y', 1, 'x', 2, 'z', 3),
+               |  array(named_struct('y', 1, 'x', 2, 'z', 3)),
+               |  map('k', named_struct('y', 1, 'x', 2, 'z', 3))
+               |""".stripMargin)
+
+          checkAnswer(
+            sql(s"""SELECT
+                 |  s.x, s.y, s.z,
+                 |  arr[0].x, arr[0].y, arr[0].z,
+                 |  m['k'].x, m['k'].y, m['k'].z
+                 |FROM $t1""".stripMargin),
+            Row.fromSeq(expected.toSeq ++ expected.toSeq ++ expected.toSeq))
+        }
+      }
+    }
+  }
+
+  Seq(
+    true -> Row(1, null),
+    false -> Row(null, 1)
+  ).foreach { case (resolveDifferentArityByPosition, expected) =>
+    test(
+      "Insert schema evolution: column list resolves narrower nested struct by 
position - " +
+        s"differentArityByPosition=$resolveDifferentArityByPosition") {
+      withSQLConf(
+          
SQLConf.INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION.key ->
+            resolveDifferentArityByPosition.toString) {
+        withInsertNestedTypeCoercion {
+          val t1 = s"${catalogAndNamespace}tbl"
+          withTable(t1) {
+            sql(s"CREATE TABLE $t1 (s struct<x: int, y: int>) USING $v2Format")
+            sql(
+              s"""INSERT WITH SCHEMA EVOLUTION INTO $t1 (s)
+                 |SELECT named_struct('y', 1)
+                 |""".stripMargin)
+
+            checkAnswer(sql(s"SELECT s.x, s.y FROM $t1"), expected)
+          }
+        }
+      }
+    }
+  }
+
+  Seq(false, true).foreach { schemaEvolutionEnabled =>
+    test(
+      "Insert schema evolution: column list rejects duplicate fields after 
positional rename - " +
+        s"schemaEvolutionEnabled=$schemaEvolutionEnabled") {
+      withSQLConf(
+          
SQLConf.INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION.key -> 
"true") {
+        val t1 = s"${catalogAndNamespace}tbl"
+        withTable(t1) {
+          sql(s"CREATE TABLE $t1 (s struct<x: int>) USING $v2Format")
+          val schemaEvolution = if (schemaEvolutionEnabled) "WITH SCHEMA 
EVOLUTION " else ""
+          checkError(
+            exception = intercept[AnalysisException] {
+              sql(
+                s"""INSERT $schemaEvolution INTO $t1 (s)
+                   |SELECT named_struct('y', 2, 'x', 3, 'z', 4)
+                   |""".stripMargin)
+            },
+            condition = "INCOMPATIBLE_DATA_FOR_TABLE.AMBIGUOUS_COLUMN_NAME",
+            parameters = Map(
+              "tableName" -> 
t1.split("\\.").map(quoteIdentifier).mkString("."),
+              "colName" -> "`s`.`x`"))
+          assert(spark.table(t1).schema("s").dataType ===
+            StructType(Seq(StructField("x", IntegerType))))
+        }
+      }
+    }
+  }
+
+  test("Insert column list rejects a rename-created duplicate for 
ACCEPT_ANY_SCHEMA") {
+    withSQLConf(
+        
SQLConf.INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION.key -> 
"true") {
+      val t1 = s"${catalogAndNamespace}tbl"
+      withTable(t1) {
+        sql(s"""CREATE TABLE $t1 (s struct<x: int>) USING $v2Format
+             |TBLPROPERTIES ('accept-any-schema' = 'true')""".stripMargin)
+        checkError(
+          exception = intercept[AnalysisException] {
+            sql(
+              s"""INSERT INTO $t1 (s)
+                 |SELECT named_struct('y', 2, 'x', 3, 'z', 4)
+                 |""".stripMargin)
+          },
+          condition = "INCOMPATIBLE_DATA_FOR_TABLE.AMBIGUOUS_COLUMN_NAME",
+          parameters = Map(
+            "tableName" -> t1.split("\\.").map(quoteIdentifier).mkString("."),
+            "colName" -> "`s`.`x`"))
+      }
+    }
+  }
+
+  test("Insert column list does not reject user-written duplicates for 
ACCEPT_ANY_SCHEMA") {
+    withSQLConf(
+        
SQLConf.INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION.key -> 
"true") {
+      val t1 = s"${catalogAndNamespace}tbl"
+      withTable(t1) {
+        sql(s"""CREATE TABLE $t1 (s struct<x: int>) USING $v2Format
+             |TBLPROPERTIES ('accept-any-schema' = 'true')""".stripMargin)
+        sql(s"INSERT INTO $t1 (s) SELECT named_struct('x', 2, 'x', 3)")
+
+        checkAnswer(spark.table(t1), Row(Row(2)))
+        assert(spark.table(t1).schema("s").dataType ===
+          StructType(Seq(StructField("x", IntegerType))))
+      }
+    }
+  }
+
+  test("Insert column list reports a rename-created duplicate in an array 
element") {
+    withSQLConf(
+        
SQLConf.INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION.key -> 
"true") {
+      val t1 = s"${catalogAndNamespace}tbl"
+      withTable(t1) {
+        sql(s"CREATE TABLE $t1 (arr array<struct<x: int>>) USING $v2Format")
+        checkError(
+          exception = intercept[AnalysisException] {
+            sql(
+              s"""INSERT INTO $t1 (arr)
+                 |SELECT array(named_struct('y', 2, 'x', 3))
+                 |""".stripMargin)
+          },
+          condition = "INCOMPATIBLE_DATA_FOR_TABLE.AMBIGUOUS_COLUMN_NAME",
+          parameters = Map(
+            "tableName" -> t1.split("\\.").map(quoteIdentifier).mkString("."),
+            "colName" -> "`arr`.`element`.`x`"))
+      }
+    }
+  }
+
+  test("Insert column list reports a rename-created duplicate in a map value") 
{
+    withSQLConf(
+        
SQLConf.INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION.key -> 
"true") {
+      val t1 = s"${catalogAndNamespace}tbl"
+      withTable(t1) {
+        sql(s"CREATE TABLE $t1 (m map<string, struct<x: int>>) USING 
$v2Format")
+        checkError(
+          exception = intercept[AnalysisException] {
+            sql(
+              s"""INSERT INTO $t1 (m)
+                 |SELECT map('k', named_struct('y', 2, 'x', 3))
+                 |""".stripMargin)
+          },
+          condition = "INCOMPATIBLE_DATA_FOR_TABLE.AMBIGUOUS_COLUMN_NAME",
+          parameters = Map(
+            "tableName" -> t1.split("\\.").map(quoteIdentifier).mkString("."),
+            "colName" -> "`m`.`value`.`x`"))
+      }
+    }
+  }
+
+  test("Insert column list reports a rename-created duplicate in a map key by 
default") {
+    val t1 = s"${catalogAndNamespace}tbl"
+    withTable(t1) {
+      sql(s"CREATE TABLE $t1 (m map<struct<x: int>, string>) USING $v2Format")
+      checkError(
+        exception = intercept[AnalysisException] {
+          sql(
+            s"""INSERT INTO $t1 (m)
+               |SELECT map(named_struct('y', 2, 'x', 3), 'v')
+               |""".stripMargin)
+        },
+        condition = "INCOMPATIBLE_DATA_FOR_TABLE.AMBIGUOUS_COLUMN_NAME",
+        parameters = Map(
+          "tableName" -> t1.split("\\.").map(quoteIdentifier).mkString("."),
+          "colName" -> "`m`.`key`.`x`"))
+    }
+  }
+
+  test("Insert column list detects rename-created duplicates using the session 
resolver") {
+    withSQLConf(
+        
SQLConf.INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION.key -> 
"true") {
+      val t1 = s"${catalogAndNamespace}tbl"
+      withTable(t1) {
+        sql(s"CREATE TABLE $t1 (s struct<x: int>) USING $v2Format")
+        withSQLConf(SQLConf.CASE_SENSITIVE.key -> "false") {
+          checkError(
+            exception = intercept[AnalysisException] {
+              sql(s"INSERT INTO $t1 (s) SELECT named_struct('y', 2, 'X', 3)")

Review Comment:
   Updated the case-insensitive branch to use an `ACCEPT_ANY_SCHEMA` table, so 
the test now isolates `findRenameCreatedCollision`'s use of the session 
resolver. The case-sensitive branch still verifies that case-distinct fields 
are not treated as a collision.



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/InsertIntoTests.scala:
##########
@@ -736,6 +736,221 @@ trait InsertIntoSchemaEvolutionTests { this: 
InsertIntoTests =>
     }
   }
 
+  Seq(
+    true -> Row(1, 2, 3),
+    false -> Row(2, 1, 3)
+  ).foreach { case (resolveDifferentArityByPosition, expected) =>
+    test("Insert schema evolution: column list resolves nested fields by 
position - " +

Review Comment:
   Renamed both parameterized tests to use neutral different-arity wording, so 
the flag-off test names no longer claim positional resolution.



##########
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,
+          dataType = renameFieldsInType(
+            tableName, f1.dataType, f2.dataType, colPath :+ f2.name))

Review Comment:
   Added a nested-struct regression using source field `other` and target field 
`inner`, asserting the collision path is `` `s`.`inner`.`x` ``.



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