dongjoon-hyun opened a new pull request, #60075:
URL: https://github.com/apache/spark/pull/60075
### What changes were proposed in this pull request?
`GeneratedColumnExpression.validate` now rejects a generation expression
whose data type names its struct fields differently from the column type, in
name or in order. It compares the two types with
`DataType.equalsStructurallyByName` under the session resolver, as
`ResolveUnion` does, so the check also covers a struct nested in another
struct, an array, or a map. `CREATE TABLE` and `REPLACE TABLE` fail with
`UNSUPPORTED_EXPRESSION_GENERATED_COLUMN`:
```sql
CREATE TABLE testcat.t (a INT, b INT,
s STRUCT<x: INT, y: INT> GENERATED ALWAYS AS (named_struct('y', a, 'x',
b))) USING foo;
```
```
[UNSUPPORTED_EXPRESSION_GENERATED_COLUMN] Cannot create generated column s
with generation expression named_struct('y', a, 'x', b) because generation
expression data type struct<y:int,x:int> names its struct fields differently
from column data type struct<x:int,y:int>. SQLSTATE: 42621
```
An expression that names the fields as the column type does, such as
`named_struct('x', b, 'y', a)`, is accepted as before. To line the fields up by
position, cast the expression to the column type, for example `CAST(struct(a,
b) AS STRUCT<x: INT, y: INT>)`.
### Why are the changes needed?
A write reads a struct generation expression in two ways that line up the
fields differently. When Spark auto-fills a generated column of a Data Source
V2 table, store assignment in `TableOutputResolver` matches the fields of the
struct to the column type by name. When the write provides the value, the check
that `ResolveTableConstraints` adds compares it with the expression by
position. `validate` only checks `Cast.canUpCast`, which ignores field names,
so it accepts an expression on which the two disagree. With the table above:
```sql
INSERT INTO testcat.t (a, b) VALUES (1, 2); --
auto-fills s = {x: 2, y: 1}
INSERT INTO testcat.t SELECT * FROM testcat.t; --
CHECK_CONSTRAINT_VIOLATION
INSERT INTO testcat.t VALUES (5, 6, named_struct('x', 6, 'y', 5)); --
CHECK_CONSTRAINT_VIOLATION
INSERT INTO testcat.t VALUES (5, 6, named_struct('y', 5, 'x', 6)); --
passes, stores s = {x: 5, y: 6}
```
A row that Spark auto-filled fails the check when a later write carries it,
and the check accepts only the value lined up by position, which is not the
value Spark auto-fills for the same `a` and `b`. A struct nested in another
struct or in an array behaves the same. If the expression uses other field
names, such as `struct(a, b)` for the column above, every auto-fill already
fails with `INCOMPATIBLE_DATA_FOR_TABLE.CANNOT_FIND_DATA`, while the check
accepts a provided value.
The other way to make the two agree is to line up the fields by name in the
check, as store assignment does. That needs the resolved type of the
expression, but the check is built unresolved and analyzed later, so it would
need a new expression or rule and a copy of the by-name logic of
`TableOutputResolver`. Even then, an expression with other field names could
not be auto-filled. Rejecting the expression in `validate` reports the problem
at `CREATE TABLE` instead of on a later write, and leaves only expressions on
which both line up the fields the same way.
This was reported in [a review comment on
#59965](https://github.com/apache/spark/pull/59965#discussion_r4453208371).
### Does this PR introduce _any_ user-facing change?
Yes. `CREATE TABLE` and `REPLACE TABLE` fail with
`UNSUPPORTED_EXPRESSION_GENERATED_COLUMN` on such a generation expression,
which Spark 4.3 and earlier accept. Like the other checks of `validate`, this
applies to any catalog that supports generated columns. The migration guide has
a new entry. Auto-filling and checking generated columns on write are not in a
released version, so no released version stored values this way.
### How was this patch tested?
Added a test to `GeneratedColumnWriteSuite`. The `CREATE TABLE` above fails,
and with the fields named as in the column type, an auto-filled row passes the
check when `INSERT INTO ... SELECT * FROM` writes it again. Without the fix,
the test fails because the `CREATE TABLE` succeeds.
Also checked on `testcat` before and after the fix:
| Generation expression | Column type | Before | After |
|---|---|---|---|
| `named_struct('y', a, 'x', b)` | `STRUCT<x: INT, y: INT>` | auto-fill
`[2,1]`, rewrite fails the check | rejected |
| same, with `writeTo(...).append()` | same | auto-fill `[2,1]` | rejected |
| `array(named_struct('y', a, 'x', b))` | `ARRAY<STRUCT<x: INT, y: INT>>` |
auto-fill `[[2,1]]`, rewrite fails the check | rejected |
| `named_struct('n', named_struct('y', a, 'x', b))` | `STRUCT<n: STRUCT<x:
INT, y: INT>>` | auto-fill `[[2,1]]`, rewrite fails the check | rejected |
| `map(a, named_struct('y', a, 'x', b))` | `MAP<INT, STRUCT<x: INT, y:
INT>>` | auto-fill `{1 -> [2,1]}`, rewrite fails with `INVALID_ORDERING_TYPE` |
rejected |
| `struct(a, b)` or `named_struct('p', a, 'q', b)` | `STRUCT<x: INT, y:
INT>` | auto-fill fails with `CANNOT_FIND_DATA` | rejected |
| `named_struct('y', a, 'x', b)` | `STRUCT<x: BIGINT, y: BIGINT>` |
auto-fill `[2,1]`, rewrite fails with `BINARY_OP_DIFF_TYPES` | rejected |
| `named_struct('x', b, 'y', a)` | `STRUCT<x: INT, y: INT>` | auto-fill
`[2,1]`, rewrite passes | same |
| `named_struct('X', b, 'Y', a)`, case-insensitive | same | auto-fill
`[2,1]`, rewrite passes | same |
| `CAST(named_struct('y', a, 'x', b) AS STRUCT<x: INT, y: INT>)` | same |
auto-fill `[1,2]`, rewrite passes | same |
Related suites pass: `GeneratedColumnExpressionSuite`, `DDLParserSuite`,
`ColumnSuite`, `DataTypeSuite` in `catalyst`, and `GeneratedColumnWriteSuite`,
`DataSourceV2SQLSuiteV1Filter`, `DataSourceV2SQLSuiteV2Filter`,
`DataSourceV2DataFrameSuite`, `CollationSuite` in `sql`.
### 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]