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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -233,7 +233,15 @@ class DataSourceV2Strategy(session: SparkSession) extends 
Strategy with Predicat
       // dynamicFilters need no such check: a DynamicPruningSubquery over a 
non-deterministic
       // filtering plan is itself non-deterministic, so 
CleanupDynamicPruningFilters has already
       // rewritten it to TrueLiteral by the time we get here.
-      val runtimeFilters = dynamicFilters ++ scalarSubqueryFilters
+      // A group-based read keeps only the group filter, for the reason above. 
PartitionPruning
+      // plans a DynamicPruningSubquery on one read only, such as the read of 
the updated rows of
+      // an UPDATE with a subquery, so it would drop groups from the shared 
scan whose rows the
+      // read of the copied rows still writes.
+      val runtimeFilters = if (groupBasedRead) {

Review Comment:
   **Finding 1.** This also fixes a DELETE and an UPDATE on the default 
configs, so the title, the ticket and the description need to cover them. With 
a `NOT EXISTS` subquery, DPP lands on the read that filters by the negated 
condition:
   
   - `NOT (cond <=> TRUE)` simplifies to `NOT (NOT EXISTS (...))`, since 
`EXISTS` is never null.
   - `BooleanSimplification` turns that into `EXISTS (...)`, and 
`RewritePredicateSubquery` makes it a left semi join.
   - `PartitionPruning` adds DPP to that read. It keeps the groups that have a 
match in the subquery, which are the groups the operation leaves alone.
   
   Here the group filter keeps only `software` and DPP keeps only `hr`. So the 
scan returns no group, and nothing is replaced:
   
   ```scala
   // the table of the new test, Seq("hr").toDF("d") as `src`, default configs, 
AQE on and off
   sql(s"DELETE FROM $tableNameAsString t WHERE NOT EXISTS (SELECT 1 FROM src 
WHERE d = t.dep)")
   // expected: (1, 300, hr), (3, 120, hr)
   // on master: all 3 rows stay
   
   sql(s"UPDATE $tableNameAsString t SET salary = -1 " +
     "WHERE NOT EXISTS (SELECT 1 FROM src WHERE d = t.dep)")
   // expected: (1, 300, hr), (2, -1, software), (3, 120, hr)
   // on master: (2, 150, software) stays too
   ```
   
   - With the group filter off, both are wrong on master too.
   - With this PR, all four cases give the right rows.
   
   These parts need an update:
   - the title and the ticket, which say UPDATE with the group filter disabled,
   - "When `spark.sql.optimizer.runtime.rowLevelOperationGroupFilter.enabled` 
is `false`" and "DELETE and MERGE are not affected" in the description,
   - "it did not prune more groups than the group filter" in the user-facing 
section.
   
   The DELETE shape needs only group-based DELETE and DPP on v2 scans. Both are 
already in v3.4.0, so it may predate SPARK-43975. I did not run it there.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/GroupBasedUpdateTableSuite.scala:
##########
@@ -222,6 +222,30 @@ class GroupBasedUpdateTableSuite extends 
UpdateTableSuiteBase {
     }
   }
 
+  test("SPARK-60878: update with a subquery on the group key and the group 
filter disabled") {

Review Comment:
   **Finding 2.** This test covers the UPDATE with the group filter off. The 
default-config shapes of finding 1 have no test. These two fail on master and 
pass on this PR:
   
   ```scala
   // GroupBasedDeleteFromTableSuite
   test("SPARK-60878: delete with a NOT EXISTS subquery on the group key") {
     withTempView("kept_dep") {
       createAndInitTable("id INT, salary INT, dep STRING",
         """{ "id": 1, "salary": 300, "dep": 'hr' }
           |{ "id": 2, "salary": 150, "dep": 'software' }
           |{ "id": 3, "salary": 120, "dep": 'hr' }
           |""".stripMargin)
   
       Seq("hr").toDF("d").createOrReplaceTempView("kept_dep")
   
       sql(s"DELETE FROM $tableNameAsString t " +
         "WHERE NOT EXISTS (SELECT 1 FROM kept_dep WHERE d = t.dep)")
   
       checkAnswer(
         sql(s"SELECT * FROM $tableNameAsString"),
         Row(1, 300, "hr") :: Row(3, 120, "hr") :: Nil)
   
       checkReplacedPartitions(Seq("software"))
     }
   }
   ```
   
   ```scala
   // GroupBasedUpdateTableSuite, the same setup with
   sql(s"UPDATE $tableNameAsString t SET salary = -1 " +
     "WHERE NOT EXISTS (SELECT 1 FROM kept_dep WHERE d = t.dep)")
   
   checkAnswer(
     sql(s"SELECT * FROM $tableNameAsString"),
     Row(1, 300, "hr") :: Row(2, -1, "software") :: Row(3, 120, "hr") :: Nil)
   
   checkReplacedPartitions(Seq("software"))
   ```
   
   On master, the DELETE leaves all 3 rows, and the UPDATE keeps `(2, 150, 
software)` next to the updated row.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -233,7 +233,15 @@ class DataSourceV2Strategy(session: SparkSession) extends 
Strategy with Predicat
       // dynamicFilters need no such check: a DynamicPruningSubquery over a 
non-deterministic
       // filtering plan is itself non-deterministic, so 
CleanupDynamicPruningFilters has already
       // rewritten it to TrueLiteral by the time we get here.
-      val runtimeFilters = dynamicFilters ++ scalarSubqueryFilters
+      // A group-based read keeps only the group filter, for the reason above. 
PartitionPruning
+      // plans a DynamicPruningSubquery on one read only, such as the read of 
the updated rows of
+      // an UPDATE with a subquery, so it would drop groups from the shared 
scan whose rows the
+      // read of the copied rows still writes.

Review Comment:
   **Finding 3.** DPP can also land on the read of the rows to copy over 
(finding 1), so this covers only one of the two reads:
   
   ```suggestion
         // A group-based read keeps only the group filter, for the reason 
above. PartitionPruning
         // plans a DynamicPruningSubquery from the join of one read, so it is 
a filter of one read
         // too. With an IN subquery it lands on the read of the updated rows 
of an UPDATE. With a
         // NOT EXISTS subquery the negated condition becomes a semi join, so 
it lands on the read
         // of the rows to copy over.
   ```
   



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