dongjoon-hyun opened a new pull request, #60380:
URL: https://github.com/apache/spark/pull/60380
### What changes were proposed in this pull request?
This PR fixes a correctness issue in `INSERT INTO ... REPLACE WHERE` and
static partition overwrite (`INSERT OVERWRITE ... PARTITION`) on a DSv2 table
whose CHAR values are stored without padding.
`ApplyCharTypePadding` now pads the target CHAR columns in the `deleteExpr`
of `OverwriteByExpression`, as it already does for the condition of a
`DeleteFromTable` that is not rewritten as a row-level operation (SPARK-61461,
#60344):
- Each target attribute with a raw CHAR type in `deleteExpr` is replaced
with `readSidePadding(<stored attribute>)`. The predicate passed to
`canOverwrite` becomes `CHAR_READ_SIDE_PADDING(dep, 8, ' ') = 'hr '`
instead of `dep = 'hr '`.
- `DeleteFromTable` and `OverwriteByExpression` share the same transform,
`padRawTypedColumns`.
The test-only `InMemoryTableWithV2Filter` gets an opt-in table property,
`accept-char-read-side-padding`. With it, an overwrite accepts and evaluates
`CHAR_READ_SIDE_PADDING(col, n, ' ') = | <=> literal`, as a connector that pads
the stored values would do.
Tradeoffs:
- An overwrite by expression has no row-level fallback. A connector that
does not accept `CHAR_READ_SIDE_PADDING` now rejects `REPLACE WHERE` and static
partition overwrite with a CHAR condition. It fails with
`_LEGACY_ERROR_TEMP_2245` (`Table does not support overwrite by expression`),
also for data stored with padding, which was replaced correctly before. This is
the same as `DELETE` (SPARK-61461) and as the existing behavior with
`spark.sql.readSideCharPadding=false`, where the condition gets `RPAD`. No
built-in connector implements `SupportsOverwrite` or `SupportsOverwriteV2`.
- `IS [NOT] NULL` on a CHAR column still translates to `IS_NULL(col)` /
`IS_NOT_NULL(col)` and works as before.
- `SupportsOverwrite` (V1 filters) converts the predicates with
`PredicateUtils.toV1`. Since SPARK-61462 (#60345), `toV1` does not drop the
non-convertible side of an `OR`. So `REPLACE WHERE dep = 'hr' OR id = 2` is
rejected instead of replacing only the rows with `id = 2`.
### Why are the changes needed?
With `spark.sql.readSideCharPadding` enabled (the default), a DSv2 table
that stores CHAR values without padding is read with padding applied on top of
the scan. The table of `OverwriteByExpression` is not a child of the command,
so it gets no read-side padding, and only the literal in `deleteExpr` is
padded: `dep = 'hr'` becomes `dep = 'hr '`. The source compares it with
the stored `hr`, so the old rows are not deleted while the new rows are
appended, without any error.
This is a correctness issue.
Static partition overwrite builds the same `OverwriteByExpression` with `dep
<=> 'hr '`, so it has the same problem.
```sql
-- t is a DSv2 table whose source implements SupportsOverwriteV2 and stores
CHAR values as written
CREATE TABLE t (id BIGINT, dep CHAR(8)) USING ... PARTITIONED BY (dep);
SET spark.sql.legacy.charVarcharAsString=true;
INSERT INTO t VALUES (1, 'hr');
SET spark.sql.legacy.charVarcharAsString=false;
INSERT INTO t REPLACE WHERE dep = 'hr' VALUES (2, 'hr');
SELECT * FROM t;
-- Issue 1: It should return only [2, hr ], but it also returns [1, hr
].
```
### Does this PR introduce _any_ user-facing change?
Yes.
- On a connector that accepts `CHAR_READ_SIDE_PADDING`, `REPLACE WHERE` and
static partition overwrite with a condition on a CHAR column now replace the
rows stored without padding instead of silently keeping them.
- On a connector that does not accept `CHAR_READ_SIDE_PADDING`, they now
fail with `_LEGACY_ERROR_TEMP_2245` instead of silently keeping such rows. This
also applies to data stored with padding, as described above.
### How was this patch tested?
Added test cases to `DataSourceV2SQLSuite`:
- In the base suite, which runs on both the V1 filter and the V2 filter
catalogs:
- `REPLACE WHERE` on a CHAR partition column stored without padding fails
and leaves the table unchanged. This fails on master, where the old row is
silently kept.
- `REPLACE WHERE dep = 'hr' OR id = 2` fails and leaves the table
unchanged. This guards the `toV1` change of SPARK-61462 for `SupportsOverwrite`.
- In `DataSourceV2SQLSuiteV2Filter`, with `accept-char-read-side-padding`:
`REPLACE WHERE` and static partition overwrite replace the rows stored with and
without padding. This fails on master, where the rows stored without padding
are kept.
Also ran `DataSourceV2SQLSuiteV1Filter`, `DataSourceV2SQLSuiteV2Filter`,
`DataSourceV2DataFrameSuite`, `DataFrameWriterV2Suite`, `V1WriteFallbackSuite`,
the `*CharVarcharTestSuite` suites, and the DELETE, UPDATE and MERGE suites,
and scalastyle on `sql/core` and `catalyst`.
### 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]