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

   ### What changes were proposed in this pull request?
   
   This PR fixes a correctness issue. An enforced CHECK constraint on a DSv2 
table lets in rows that violate it, and rejects rows that satisfy it, when the 
condition compares a CHAR column with a string literal.
   
   On write, every CHECK condition is now resolved against the output of the 
write query, with the CHAR and VARCHAR types of the table columns put back. So 
`ApplyCharTypePadding` pads the literal, as it does for a read.
   
   In `TableConstraintResolution`:
   - `withCharVarcharTypes` puts the CHAR and VARCHAR type of each table column 
(`Table.columns()`) back on the attribute of the same name in the write query 
output. `TableOutputResolver` removes this type from that output on purpose, 
and the output of the target relation does not have it either.
   - A write builds a CHECK in one of three ways: it converts 
`Check.predicate()` back, it parses `predicateSql` under the legacy config, or 
it parses `predicateSql` under the fixed configs. All three now resolve against 
that output.
   
   The predicate that `CREATE TABLE` stores is not changed. In the first-class 
CHAR modes (`spark.sql.preserveCharVarcharTypeInfo` and 
`spark.sql.charVarchar.standardSemantics.enabled`), a plain `SELECT` does not 
pad the literal either, so CHECK keeps matching reads there.
   
   ### Why are the changes needed?
   
   CHECK constraints give wrong results. A CHAR value is padded on write: 
`'CLOSED'` in a `CHAR(10)` column is stored as `'CLOSED    '`. A read pads the 
literal it compares the column with, but a CHECK on write does not:
   - `CREATE TABLE` and `REPLACE TABLE` resolve the condition against columns 
that have no CHAR type. So they store the predicate `status <> 'CLOSED'`, not 
`status <> 'CLOSED    '` as `ALTER TABLE ... ADD CONSTRAINT` does.
   - A write resolves the condition, converted back from the predicate or 
parsed from `predicateSql`, against the write query output, which has no CHAR 
type. So nothing pads the literal.
     - A condition with a part that has no V2 form, such as `current_date()`, 
has no predicate, so the write always parses `predicateSql`.
     - `ALTER TABLE` is affected too when the condition has a function that 
cannot be converted back, such as `upper(...)`.
   
   So `'CLOSED    ' <> 'CLOSED'` is true, and the row passes. `'OPEN      ' = 
'OPEN'` is false, and the row fails. `INSERT`, `UPDATE` and `MERGE` are all 
affected. This has been the case since CHECK constraints were added in Apache 
Spark [v4.1.0](https://github.com/apache/spark/releases/tag/v4.1.0) 
(2025-12-11).
   
   ```sql
   -- With a DSv2 catalog that supports CHECK constraints
   CREATE TABLE t1 (id INT, status CHAR(10), CONSTRAINT c1 CHECK (status <> 
'CLOSED'));
   -- Issue 1: It should fail with CHECK_CONSTRAINT_VIOLATION, but it inserts 
the row.
   INSERT INTO t1 VALUES (1, 'CLOSED');
   
   CREATE TABLE t2 (id INT, status CHAR(10), CONSTRAINT c2 CHECK (status = 
'OPEN'));
   -- Issue 2: It should insert the row, but it fails with 
CHECK_CONSTRAINT_VIOLATION.
   INSERT INTO t2 VALUES (1, 'OPEN');
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. An enforced CHECK constraint that compares a CHAR column with a string 
literal now rejects the rows that violate it and accepts the rows that satisfy 
it. Since the fix is on the write side, this also holds for tables created by 
an earlier version, without recreating them.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case, which fails without this fix.
   
   ### 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