Skip to content

[SPARK-55690] Schema evolution in DSv2 AppendData, OverwriteByExpression, OverwritePartitionsDynamic#54488

Open
johanl-db wants to merge 6 commits intoapache:masterfrom
johanl-db:dsv2-schema-evolution-insert
Open

[SPARK-55690] Schema evolution in DSv2 AppendData, OverwriteByExpression, OverwritePartitionsDynamic#54488
johanl-db wants to merge 6 commits intoapache:masterfrom
johanl-db:dsv2-schema-evolution-insert

Conversation

@johanl-db
Copy link
Contributor

@johanl-db johanl-db commented Feb 25, 2026

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.

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, ..)

Copy link
Member

@szehon-ho szehon-ho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think this is a great pr! The tests coverage can be improved on various cases, but functionally its a good change:
eg

  • INSERT OVERWRITE with PartitionOverwriteMode.DYNAMIC + schema evolution
  • Case-insensitive column name matching
  • Static partition overwrite with schema evolution
  • Table without AUTOMATIC_SCHEMA_EVOLUTION capability should no-op

etc

@johanl-db johanl-db changed the title [WIP][SPARK-55690] Schema evolution in DSv2 AppendData, OverwriteByExpression, OverwritePartitionsDynamic [SPARK-55690] Schema evolution in DSv2 AppendData, OverwriteByExpression, OverwritePartitionsDynamic Feb 26, 2026
@johanl-db johanl-db requested a review from szehon-ho February 26, 2026 14:09
Copy link
Member

@szehon-ho szehon-ho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me!

suggestion: add tests like:

  • type evolution
  • 2 level structs
  • non-partitioned table
  • constraints

Also do we run the same tests Dataframe API? (I think we only test with SQL?)

updates ++ adds
}

def toFieldMap(fields: Array[StructField]): Map[String, StructField] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: private

sql(s"CREATE TABLE $t1 (id bigint, data string) USING $v2Format")
checkError(
exception = intercept[AnalysisException] {
doInsert(t1, Seq((2L, "b", true)).toDF("id", "data", "active"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

miss byName

Copy link
Member

@szehon-ho szehon-ho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

2 participants