Skip to content

[Spark][Prototype] Schema evolution in DSv2 INSERT#6140

Open
johanl-db wants to merge 1 commit intodelta-io:masterfrom
johanl-db:dsv2-schema-evolution-insert
Open

[Spark][Prototype] Schema evolution in DSv2 INSERT#6140
johanl-db wants to merge 1 commit intodelta-io:masterfrom
johanl-db:dsv2-schema-evolution-insert

Conversation

@johanl-db
Copy link
Copy Markdown
Collaborator

Description

This requires apache/spark#54488 in Spark

Demonstrate using Spark DSv2 code path to handle schema evolution for INSERT operations instead of Delta.
That is: rely on the new rule ResolveInsertSchemaEvolution introduced in the linked Spark PR instead of DeltaAnalysis

Note: this actually goes one step further and includes changes for type evolution (see SupportsTypeEvolution), which I haven't pushed to Spark yet, so it won't build unless SupportsTypeEvolution / canChangeType are removed

How was this patch tested?

Building locally together with apache/spark#54488 and running tests.
After triaging them, all failures are somewhat expected - e.g. negative tests covering incorrect null struct expansion if the corresponding is disabled, or different error class returned.

aokolnychyi pushed a commit to apache/spark that referenced this pull request Mar 16, 2026
…ion, OverwritePartitionsDynamic

### What changes were proposed in this pull request?
Adds support for schema evolution during INSERT operations (AppendData, OverwriteByExpression, OverwritePartitionsDynamic)

When the table reports capability `AUTOMATIC_SCHEMA_EVOLUTION`, a new analyzer rule `ResolveInsertSchemaEvolution` collects new columns and nested fields present in the source query but not in the table schema, and adds them to the target table by calling `catalog.alterTable()`

Identifying new columns/fields respects the resolution semantics of INSERT operations: matching fields by-name vs by-position.

This builds on previous from szehon-ho , in particular #51698.
The first two commits move this previous code around to reuse it, the core of the implementation is in the [third commit](7be9d2a).

### Why are the changes needed?
The `WITH SCHEMA EVOLUTION` syntax for SQL inserts was added recently: #53732. This actually implements schema evolution behind this syntax.

### Does this PR introduce _any_ user-facing change?
Yes, when the `WITH SCHEMA EVOLUTION` clause is specified in SQL INSERT operations, new columns and nested fields in the source data will be added to the target table - assuming the data source supports schema evolution (capability AUTOMATIC_SCHEMA_EVOLUTION):

```
CREATE TABLE target (id INT);
INSERT INTO target VALUES (1);
INSERT WITH SCHEMA EVOLUTION INTO target SELECT 2 AS id, "two" AS value;

SELECT * FROM target;
| id | value |
|----|-------|
| 1  |  null |
| 2  | "two" |
```

### How was this patch tested?
Added basic testing in `DataSourceV2SQLSuite`.
Integrated with Delta and ran extensive Delta test harness for schema evolution against this implementation.
See delta-io/delta#6140. A number of expected failures for tests that would need to be updated on Delta side (different error class returned, negative tests checking something specifically doesn't work if a fix is disabled, ..)

Closes #54488 from johanl-db/dsv2-schema-evolution-insert.

Authored-by: Johan Lasperas <[email protected]>
Signed-off-by: Anton Okolnychyi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant