dongjoon-hyun opened a new pull request, #60360:
URL: https://github.com/apache/spark/pull/60360

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


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