[Spark][Prototype] Schema evolution in DSv2 INSERT#6140
Open
johanl-db wants to merge 1 commit intodelta-io:masterfrom
Open
[Spark][Prototype] Schema evolution in DSv2 INSERT#6140johanl-db wants to merge 1 commit intodelta-io:masterfrom
johanl-db wants to merge 1 commit intodelta-io:masterfrom
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
ResolveInsertSchemaEvolutionintroduced in the linked Spark PR instead ofDeltaAnalysisNote: 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 unlessSupportsTypeEvolution/canChangeTypeare removedHow 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.