This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.x by this push:
new 3c70653da1ce [SPARK-61480][SQL] Fix wrong results by runtime group
filtering in group-based DELETE when the condition uses a CHAR column
3c70653da1ce is described below
commit 3c70653da1cea606326b7d7b9878a9b609d999af
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Wed Nov 11 20:16:57 2026 -0800
[SPARK-61480][SQL] Fix wrong results by runtime group filtering in
group-based DELETE when the condition uses a CHAR column
### What changes were proposed in this pull request?
This PR aims to fix a correctness issue of group-based `DELETE` whose
condition uses a `CHAR` column. When `RowLevelOperationRuntimeGroupFiltering`
builds the runtime group filter of a `DELETE`, it now replaces the aliases in
the condition with their definitions in the rewrite plan. So, the group filter
compares the read-side padded values like the `DELETE` itself does.
`ApplyCharTypePadding` reads a `CHAR` column through a `Project` that pads
it, and the condition of `ReplaceData` refers to the padded alias, e.g. `c#20`,
instead of the column, e.g. `c#2`. However, the runtime group filter evaluates
the condition over a new instance of the table relation, which has no such
alias.
- The aliases are not mapped to the relation columns by name, as the
`UPDATE` branch does for the second read of an `UPDATE` with a subquery,
because that would compare the stored values with the padded literals.
- Like before, `GroupBasedRowLevelOperationScanPlanning` does not push this
predicate down to the scan of the operation. Pushing it down by the column name
would have the same problem.
### Why are the changes needed?
This is a correctness issue. With the default configurations, a group-based
`DELETE` whose condition uses a `CHAR` column deletes nothing, without any
error, when the values are stored without padding and the data source evaluates
the predicate pushed down in the runtime group filter. Otherwise, it fails with
`INTERNAL_ERROR_ATTRIBUTE_NOT_FOUND`, even when the values are padded.
The runtime group filter refers to the padded alias, which the relation
does not have.
- A data source filter is translated from an attribute by its name, so the
data source compares the stored values, e.g. `hr`, with the padded literals,
e.g. `hr `. The group filter returns no group, so the `DELETE` replaces no
group. `spark.sql.readSideCharPadding` is enabled by default to read such
values, e.g. written by other systems, as padded values.
- When Spark evaluates the filter, it cannot bind the reference to the
alias.
```scala
//
spark.sql.catalog.cat=org.apache.spark.sql.connector.catalog.InMemoryRowLevelOperationTableCatalog
sql("CREATE TABLE cat.ns1.t (pk INT NOT NULL, c CHAR(3), dep CHAR(8))
PARTITIONED BY (dep)")
// Store the values without padding, like a table written by another system.
spark.conf.set("spark.sql.legacy.charVarcharAsString", "true")
sql("INSERT INTO cat.ns1.t VALUES (1, 'a', 'hr'), (2, 'b', 'software'), (3,
'x', 'hr')")
spark.conf.set("spark.sql.legacy.charVarcharAsString", "false")
// Issue 1: It should delete the 2 `hr` rows, which `SELECT` returns with
the same condition,
// but it deletes nothing.
sql("DELETE FROM cat.ns1.t WHERE dep IN ('hr', 'it')")
// Issue 2: On the same initial table, it should delete the rows of `a` and
`b`, but it fails.
// [INTERNAL_ERROR_ATTRIBUTE_NOT_FOUND] Could not find c#279 in [dep#341].
SQLSTATE: XX000
sql("DELETE FROM cat.ns1.t WHERE c IN ('a', 'b', 'c')")
```
- Issue 2 also happens when the values are stored with padding.
- The conditions of the other row-level commands do not refer to the padded
aliases, so only group-based `DELETE` has this issue.
- Both runtime group filtering
([SPARK-38959](https://issues.apache.org/jira/browse/SPARK-38959)) and
read-side `CHAR` padding
([SPARK-40697](https://issues.apache.org/jira/browse/SPARK-40697)) were added
in [v3.4.0](https://github.com/apache/spark/releases/tag/v3.4.0) (2023-04-07).
### Does this PR introduce _any_ user-facing change?
Yes. A group-based `DELETE` whose condition uses a `CHAR` column now
deletes the matching rows whose values are stored without padding, and no
longer fails with `INTERNAL_ERROR_ATTRIBUTE_NOT_FOUND`.
### How was this patch tested?
Pass the CIs with the newly added test cases. Both test cases in
`GroupBasedDeleteFromTableSuite` fail without this PR. Both also fail if the
runtime group filter maps the padded aliases to the relation columns by name,
and the second one fails if the scan of the operation pushes the condition down
that way.
```
$ build/sbt 'sql/testOnly *DeleteFromTableSuite *UpdateTableSuite
*MergeIntoTableSuite *MergeIntoTableNoCodegenSuite
*MergeIntoTableUpdateAsDeleteAndInsertSuite *MergeIntoDataFrameSuite
*CatalystRuntimeFilterSuite *RuntimePartitionFilterSuite *DeleteFilterSuite
*DynamicPartitionPruning* *KeyGroupedPartitioningSuite *CharVarcharTestSuite
*DataSourceV2FileSourceDPPSuiteAEOff *DataSourceV2FileSourceDPPSuiteAEOn
*DataSourceV2FileScanSubclassSuite'
```
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
Closes #60360 from dongjoon-hyun/SPARK-61480.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit 026f7ab366656d49bc001b35bcda7a60bf6ef0b7)
Signed-off-by: Dongjoon Hyun <[email protected]>
skip-checks: true
---
.../RowLevelOperationRuntimeGroupFiltering.scala | 8 +++--
.../connector/GroupBasedDeleteFromTableSuite.scala | 42 ++++++++++++++++++++++
2 files changed, 48 insertions(+), 2 deletions(-)
diff --git
a/sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/RowLevelOperationRuntimeGroupFiltering.scala
b/sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/RowLevelOperationRuntimeGroupFiltering.scala
index 74d86570e2d5..04860b8b05c2 100644
---
a/sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/RowLevelOperationRuntimeGroupFiltering.scala
+++
b/sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/RowLevelOperationRuntimeGroupFiltering.scala
@@ -22,7 +22,7 @@ import org.apache.spark.sql.catalyst.expressions.{Alias,
Attribute, AttributeMap
import org.apache.spark.sql.catalyst.expressions.Literal.TrueLiteral
import org.apache.spark.sql.catalyst.optimizer.RewritePredicateSubquery
import org.apache.spark.sql.catalyst.planning.{DeltaBasedRowLevelOperation,
GroupBasedRowLevelOperation}
-import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Filter,
LogicalPlan, RowLevelWrite}
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Filter,
LogicalPlan, Project, RowLevelWrite}
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.catalyst.trees.TreePattern.{REPLACE_DATA,
WRITE_DELTA}
import org.apache.spark.sql.connector.expressions.NamedReference
@@ -108,7 +108,11 @@ class
RowLevelOperationRuntimeGroupFiltering(optimizeSubqueries: Rule[LogicalPla
val matchingRowsPlan = write.operation.command match {
case DELETE =>
- Filter(cond, relation)
+ // the condition may refer to aliases computed on top of the read
relation, such as the
+ // CHAR columns padded by ApplyCharTypePadding, which must be replaced
by their definitions
+ // mapping them to the relation columns by name would compare the
stored values instead
+ val aliasMap = getAliasMap(write.query.collect { case p: Project =>
p.projectList }.flatten)
+ Filter(replaceAlias(cond, aliasMap), relation)
case UPDATE =>
// UPDATEs with subqueries can be rewritten using UNION with two
identical scan relations
diff --git
a/sql/core/src/test/scala/org/apache/spark/sql/connector/GroupBasedDeleteFromTableSuite.scala
b/sql/core/src/test/scala/org/apache/spark/sql/connector/GroupBasedDeleteFromTableSuite.scala
index f4db659c3f7e..ffd5364504f3 100644
---
a/sql/core/src/test/scala/org/apache/spark/sql/connector/GroupBasedDeleteFromTableSuite.scala
+++
b/sql/core/src/test/scala/org/apache/spark/sql/connector/GroupBasedDeleteFromTableSuite.scala
@@ -117,6 +117,48 @@ class GroupBasedDeleteFromTableSuite extends
DeleteFromTableSuiteBase {
checkDeleteMetrics(numDeletedRows = 1, numCopiedRows = 1)
}
+ test("SPARK-61480: delete with a CHAR column stored without padding") {
+ createTable("pk INT NOT NULL, c CHAR(3), dep STRING")
+ // read-side padding also covers values that are not padded in the storage
+ withSQLConf(SQLConf.LEGACY_CHAR_VARCHAR_AS_STRING.key -> "true") {
+ append("pk INT NOT NULL, c STRING, dep STRING",
+ """{ "pk": 1, "c": "a", "dep": "hr" }
+ |{ "pk": 2, "c": "b", "dep": "software" }
+ |{ "pk": 3, "c": "x", "dep": "hr" }
+ |{ "pk": 4, "c": "y", "dep": "finance" }
+ |""".stripMargin)
+ }
+
+ // the runtime group filter must compare the padded values as the
condition does
+ sql(s"DELETE FROM $tableNameAsString WHERE c IN ('a', 'b', 'c')")
+
+ checkAnswer(
+ sql(s"SELECT * FROM $tableNameAsString"),
+ Row(3, "x ", "hr") :: Row(4, "y ", "finance") :: Nil)
+ checkReplacedPartitions(Seq("hr", "software"))
+ }
+
+ test("SPARK-61480: delete with a CHAR partition column stored without
padding") {
+ createTable("pk INT NOT NULL, dep CHAR(8)")
+ withSQLConf(SQLConf.LEGACY_CHAR_VARCHAR_AS_STRING.key -> "true") {
+ append("pk INT NOT NULL, dep STRING",
+ """{ "pk": 1, "dep": "hr" }
+ |{ "pk": 2, "dep": "software" }
+ |{ "pk": 3, "dep": "hr" }
+ |{ "pk": 4, "dep": "finance" }
+ |""".stripMargin)
+ }
+
+ // a filter on the partition column that compares the stored values with
the padded literals,
+ // in the group filter or in the scan, would skip the group of the rows to
delete
+ sql(s"DELETE FROM $tableNameAsString WHERE dep IN ('hr', 'it')")
+
+ checkAnswer(
+ sql(s"SELECT * FROM $tableNameAsString"),
+ Row(2, "software") :: Row(4, "finance ") :: Nil)
+ checkReplacedPartitions(Seq("hr"))
+ }
+
test("delete with subqueries and runtime group filtering") {
withTempView("deleted_id", "deleted_dep") {
createAndInitTable("id INT, salary INT, dep STRING",
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]