dongjoon-hyun opened a new pull request, #60487:
URL: https://github.com/apache/spark/pull/60487
### What changes were proposed in this pull request?
This PR makes `PredicateUtils.toV1` convert a comparison of
`CHAR_READ_SIDE_PADDING(col, n, ' ')` with string literals of `n` characters
into a V1 `In` on the stored values of the column. This is how Spark sends a
comparison of a `CHAR(n)` column to a data source when
`spark.sql.readSideCharPadding` is enabled and
`spark.sql.charVarchar.standardSemantics.enabled` is disabled (the defaults).
The read-side padding of a value equals a literal `L` of `n` characters if
and only if the value is `L` without its trailing spaces, followed by none up
to all of those spaces. A value longer than `n` characters is not padded, so it
never matches. For `dep CHAR(8)`:
| V2 predicate | V1 filter |
| --- | --- |
| `CHAR_READ_SIDE_PADDING(dep, 8, ' ') = 'hr '` | `In(dep, ['hr', 'hr
', ..., 'hr '])` |
| `CHAR_READ_SIDE_PADDING(dep, 8, ' ') IN ('hr ', 'it ')` |
`In(dep, ['hr', ..., 'hr ', 'it', ..., 'it '])` |
| `CHAR_READ_SIDE_PADDING(dep, 8, ' ') <> 'hr '` | `Not(In(dep,
[...]))` |
| `CHAR_READ_SIDE_PADDING(dep, 8, ' ') <=> 'hr '` |
`And(IsNotNull(dep), In(dep, [...]))` |
`NULL <=> 'hr '` is false, not NULL, so `<=>` keeps an `IsNotNull`,
which matters under a `NOT`.
The predicate is not converted, and fails as before, when:
- the literal has a collation other than `UTF8_BINARY`, since a V1 filter
compares the stored values as they are, or
- the `IN` would need more than 256 values. This covers any single value of
a `CHAR(255)` column, the longest `CHAR` in Hive and MySQL, so that a wide
`CHAR` column cannot turn one comparison into a huge filter.
V2 predicates are unchanged, so data sources that accept
`CHAR_READ_SIDE_PADDING` themselves, such as the JDBC dialects of SPARK-60479,
are not affected. `toV1` is what the default `Predicate` methods of
`SupportsDelete`, `SupportsOverwrite` and `SupportsRuntimeFiltering` use to
call their V1 filter methods. An alternative is to ask the table again with V2
`IN` predicates when it rejects the padding. That would also cover V2 data
sources, but it needs a change at every call site of `canDeleteWhere`,
`canOverwrite` and the runtime filters, and calls the data source twice, while
a V2 data source can already accept `CHAR_READ_SIDE_PADDING`.
This PR also updates the documentation that said a V1 filter data source
cannot receive such a comparison: the migration guide entry of SPARK-61512, the
doc of `ApplyCharTypePadding.padDeleteConditions`, and the comments of three
tests.
### Why are the changes needed?
Since SPARK-61461 and SPARK-61512, a filter-based `DELETE` and an overwrite
by a condition (`INSERT INTO ... REPLACE WHERE`, static partition `INSERT
OVERWRITE` and `DataFrameWriterV2.overwrite`) send a comparison on a CHAR
column as `CHAR_READ_SIDE_PADDING`, so that the rows whose values are stored
without padding are not silently kept. A data source that takes V1 filters
receives them through `PredicateUtils.toV1`, which had no form for this
function. So these operations always failed on such data sources, also when the
values were stored with padding:
```sql
-- a DSv2 table whose SupportsDelete takes V1 filters
CREATE TABLE cat.t (id BIGINT, p CHAR(3)) PARTITIONED BY (id, p);
DELETE FROM cat.t WHERE p = 'a';
-- Cannot delete from table cat.t where [(CHAR_READ_SIDE_PADDING(p, 3, ' '))
= 'a '].
```
An overwrite failed the same way with `_LEGACY_ERROR_TEMP_2245`. With the
`In`, these data sources run the operation by default, and it also matches the
values stored without padding. This was suggested in the review of #60380.
### Does this PR introduce _any_ user-facing change?
Yes, for Data Source V2 tables that take V1 filters, when
`spark.sql.readSideCharPadding` is enabled and
`spark.sql.charVarchar.standardSemantics.enabled` is disabled (the defaults):
`DELETE`, `INSERT INTO ... REPLACE WHERE`, static partition `INSERT OVERWRITE`
and `DataFrameWriterV2.overwrite` with `=`, `<>`, `<=>` or `IN` on a CHAR
column send an `In` filter instead of failing. They succeed if the data source
accepts it, and then also delete or replace the rows whose values are stored
without padding.
The failures fixed here are not in a released version, since SPARK-61461 and
SPARK-61512 are only in `master` and `branch-4.x`. In released versions, such a
`DELETE` failed with `INTERNAL_ERROR` since Spark 3.4.0, and such an overwrite
sent `dep = 'hr '`, which kept the rows stored without padding.
### How was this patch tested?
Added tests:
- `V2PredicateSuite`: the conversion of `=`, `<>`, `<=>` and `IN`, and no
conversion for a non-binary collation, a literal not padded to the length, and
more than 256 values.
- `DataSourceV2SQLSuiteV1Filter`: a `DELETE` with a CHAR condition on a V1
filter table deletes the rows stored with and without padding. The in-memory
test table gets a table property `accept-in-filter` to accept an `In` filter in
a delete. It is opt-in because the row-level operation test tables inherit it,
and accepting `In` by default would turn their `DELETE ... WHERE ... IN` into
metadata-only deletes.
Both tests fail without the change of `PredicateUtils`. Each branch of the
conversion was also disabled in turn, and every variant failed the new
`V2PredicateSuite` test.
```
build/sbt 'sql/testOnly *V2PredicateSuite *DataSourceV2SQLSuite*
*DeleteFromTableSuite *UpdateTableSuite *MergeIntoTableSuite
*MergeIntoTableUpdateAsDeleteAndInsertSuite *UpdateAsDeleteAndInsertTableSuite
*RuntimeFilterSuite *DataSourceV2Enhanced*Suite *CharVarcharTestSuite
*DynamicPartitionPruning*Suite* *DataSourceV2DataFrameSuite
*DataFrameWriterV2Suite *V1WriteFallbackSuite
*DataSourceV2FileScanSubclassSuite *StreamingV2ScanPushDownSuite
*DataSourceV2StrategySuite *KeyGroupedPartitioningSuite *JDBCV2Suite'
```
passes (46 suites, 3155 tests), as do `catalyst/scalastyle`,
`catalyst/Test/scalastyle`, `sql/scalastyle` and `sql/Test/scalastyle`.
### 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]