dongjoon-hyun opened a new pull request, #60738: URL: https://github.com/apache/spark/pull/60738
### What changes were proposed in this pull request? This PR fixes a correctness issue. On a DSv2 table, a generated column whose expression compares a CHAR column with a string literal gets a wrong value when Spark fills it on write. A write that provides the right value is rejected, and a write that provides a wrong value is accepted. On write, the generation expression is now resolved against the output of the write query, with the CHAR and VARCHAR types of the table columns put back, as SPARK-62070 does for CHECK constraints. So `ApplyCharTypePadding` pads the literal, as it does for a read. A write under `spark.sql.legacy.charVarcharAsString` does not pad the values, so it keeps the output as it is. - `CharVarcharUtils.withCharVarcharTypes` puts the CHAR and VARCHAR type of each table column (`Table.columns()`) back on the attribute of the same name. It is the helper that SPARK-62070 added to `TableConstraintResolution`, moved so that CHECK constraints and generated columns share it. - `GeneratedColumn.ExpressionReader.analyze` resolves a generation expression against that output. Both the value Spark fills (`WriteOutputResolution`) and the check of a provided value (`TableConstraintResolution`) are analyzed there, whether the column has a V2 expression or only SQL text. 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 a generated column keeps matching reads there. ### Why are the changes needed? Generated columns get wrong values. A CHAR value is padded on write: `'OPEN'` in a `CHAR(10)` column is stored as `'OPEN '`. A read pads the literal it compares the column with, but a write resolves the generation expression against the write query output, which has no CHAR type, because `TableOutputResolver` removes it on purpose. So nothing pads the literal, and `'OPEN ' = 'OPEN'` is false: - A value that Spark fills is `false`, while `SELECT status = 'OPEN'` on the stored row returns `true`. - A provided `true` fails with `CHECK_CONSTRAINT_VIOLATION`, and a provided `false` is stored. `INSERT`, `INSERT OVERWRITE` and `DataFrameWriterV2` writes are all affected. This has been the case since Spark started to fill and check generated columns on DSv2 writes in Apache Spark [v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) (2026-10-27). ```sql -- With a DSv2 catalog whose tables have the GENERATE_COLUMN_VALUES_ON_WRITE capability CREATE TABLE t (id INT, status CHAR(10), is_open BOOLEAN GENERATED ALWAYS AS (status = 'OPEN')); -- Issue 1: It should store is_open = true, but it stores false. INSERT INTO t (id, status) VALUES (1, 'OPEN'); -- Issue 2: It should insert the row, but it fails with CHECK_CONSTRAINT_VIOLATION. INSERT INTO t VALUES (2, 'OPEN', true); -- Issue 3: It should fail with CHECK_CONSTRAINT_VIOLATION, but it inserts the row. INSERT INTO t VALUES (3, 'OPEN', false); ``` ### Does this PR introduce _any_ user-facing change? Yes. A generated column that compares a CHAR column with a string literal now gets the value that a read of the stored row computes, and a provided value is checked against that value. Rows that an earlier version stored with a wrong value keep it, and a write that provides that value again, such as `INSERT INTO ... SELECT` from the table, now fails with `CHECK_CONSTRAINT_VIOLATION`. ### 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]
