peter-toth commented on code in PR #59918:
URL: https://github.com/apache/spark/pull/59918#discussion_r4415070216


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/UpdateTableSuiteBase.scala:
##########
@@ -915,6 +915,30 @@ abstract class UpdateTableSuiteBase extends 
RowLevelOperationSuiteBase {
     checkUpdateMetrics(numUpdatedRows = 2, numCopiedRows = 0)
   }
 
+  test("SPARK-60708: update char/varchar table with metadata columns") {
+    createTable("pk INT NOT NULL, id INT, dep STRING, name VARCHAR(10)")
+
+    append("pk INT NOT NULL, id INT, dep STRING, name STRING",
+      """{ "pk": 1, "id": 1, "dep": "hr", "name": "a" }
+        |{ "pk": 2, "id": 2, "dep": "software", "name": "b" }
+        |{ "pk": 3, "id": 3, "dep": "hr", "name": "c" }
+        |""".stripMargin)
+
+    sql(s"UPDATE $tableNameAsString SET id = -1 WHERE id IN (1, 2) AND 
_partition = 'hr'")
+
+    checkAnswer(
+      sql(s"SELECT * FROM $tableNameAsString"),
+      Row(1, -1, "hr", "a") :: Row(2, 2, "software", "b") :: Row(3, 3, "hr", 
"c") :: Nil)
+    checkUpdateMetrics(numUpdatedRows = 1, numCopiedRows = 1)
+
+    sql(s"UPDATE $tableNameAsString t SET t.id = t.index + 10 WHERE t.pk = 2")
+
+    checkAnswer(
+      sql(s"SELECT * FROM $tableNameAsString"),
+      Row(1, -1, "hr", "a") :: Row(2, 10, "software", "b") :: Row(3, 3, "hr", 
"c") :: Nil)
+    checkUpdateMetrics(numUpdatedRows = 1, numCopiedRows = 0)
+  }

Review Comment:
   **Finding 1.** The description names the outer references in subqueries as 
the reason for `rewriteAttrs`, and you checked them by hand, but no test pins 
them. I replaced `rewriteAttrs(metadataAttrMap)` in the `UpdateTable` case with 
a `transformExpressions` that maps only `AttributeReference`s. The test above 
still passes with that change, and the one below fails. With `rewriteAttrs`, 
the one below passes in all five UPDATE suites:
   
   ```scala
     test("SPARK-60708: update char/varchar table with an outer metadata 
reference") {
       createTable("pk INT NOT NULL, id INT, dep STRING, name VARCHAR(10)")
   
       append("pk INT NOT NULL, id INT, dep STRING, name STRING",
         """{ "pk": 1, "id": 1, "dep": "hr", "name": "a" }
           |{ "pk": 2, "id": 2, "dep": "software", "name": "b" }
           |{ "pk": 3, "id": 3, "dep": "hr", "name": "c" }
           |""".stripMargin)
   
       withTempView("deps") {
         Seq("hr").toDF("d").createOrReplaceTempView("deps")
         sql(s"UPDATE $tableNameAsString t SET id = -1 " +
           "WHERE id IN (1, 2) AND EXISTS (SELECT 1 FROM deps WHERE deps.d = 
t._partition)")
       }
   
       checkAnswer(
         sql(s"SELECT * FROM $tableNameAsString"),
         Row(1, -1, "hr", "a") :: Row(2, 2, "software", "b") :: Row(3, 3, "hr", 
"c") :: Nil)
     }
   ```
   



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