[SPARK-58517][SDP] Honor spark.sql.caseSensitive in table schema evolution - #57722
Open
anew wants to merge 1 commit into
Open
[SPARK-58517][SDP] Honor spark.sql.caseSensitive in table schema evolution#57722anew wants to merge 1 commit into
anew wants to merge 1 commit into
Conversation
…ution DatasetManager's schema evolution merged and diffed schemas case-sensitively regardless of the session's spark.sql.caseSensitive. Under the default (case-insensitive) session, a target column `value` and an incoming `Value` were treated as distinct, so the target was evolved to carry both -- a schema self-inconsistent under the resolver the rest of the engine uses. The corrupt two-column target then failed downstream (AutoCDC SCD2: COLUMN_ALREADY_EXISTS from ResolveUnion over the reconcile unionByName; SCD1: AMBIGUOUS_REFERENCE in the MERGE plan). This is not AutoCDC-specific -- it affects any SDP table that evolves its schema under a case-insensitive session. Thread the session's case-sensitivity into SchemaMergingUtils.mergeSchemas and SchemaInferenceUtils.diffSchemas (both default to case-sensitive to preserve the schema-inference callers) and forward it from DatasetManager.evolveTable. With case-insensitivity, StructType.merge folds `Value` onto the existing `value` (the left field's name wins) so no duplicate column is added, and diffSchemas matches on the normalized name while addressing the column by its persisted name (no spurious drop-then-add, no rename). Tests: unit coverage in SchemaInferenceUtilsSuite (case-sensitive vs -insensitive diff, and that a case-insensitive type change addresses the current column name); an end-to-end MaterializeTablesSuite pair (no-op under case-insensitive, add under case-sensitive). Updated AutoCdcScd1SchemaEvolutionSuite's case-only test to assert the fixed no-op-merge behavior, and refreshed two now-stale comments. Co-authored-by: Opus 4.8
anew
force-pushed
the
spark-58517-schema-evolution-case-sensitivity
branch
from
August 3, 2026 22:34
67eb4ec to
76144f5
Compare
uros-b
approved these changes
Aug 4, 2026
Member
|
Thank you @anew! Adding @szehon-ho |
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.
What changes were proposed in this pull request?
Declarative Pipelines' table schema evolution in
DatasetManager.evolveTablecompared schemas case-sensitively regardless of the session'sspark.sql.caseSensitive. Under the default (case-insensitive) session, a target columnvalueand an incoming columnValuewere treated as distinct, so the target was evolved to carry both columns -- a schema that is internally inconsistent under the resolver the rest of the engine uses.The root cause is on two utilities on the evolution path, each taking the case-sensitive default and never forwarding the session conf:
SchemaMergingUtils.mergeSchemascalledStructType.merge(tableSchema, dataSchema)positionally, leavingStructType.merge'scaseSensitive: Boolean = trueparameter at its default.SchemaInferenceUtils.diffSchemasbuiltname -> fieldmaps and computedtargetFields.keySet.diff(currentFields.keySet)-- an exact-string set difference, also case-sensitive.This PR threads case-sensitivity through both:
mergeSchemasanddiffSchemasgain acaseSensitive: Boolean = trueparameter. The default preserves existing behavior for the schema-inference callers (inferSchemaFromFlows,DataflowGraph.inferredSchema), which intentionally merge case-sensitively.DatasetManager.evolveTableforwardscontext.spark.sessionState.conf.caseSensitiveAnalysisat both call sites (the target table and the AutoCDC auxiliary table).StructType.mergefolds the incomingValueonto the existingvalue(the left field's name/position wins), so no duplicate column is added;diffSchemasmatches on the normalized name while emitting the column's already-persisted name, so a case-only difference is an in-place no-op rather than a spurious drop-then-add, and no rename is produced.This is not AutoCDC-specific --
SchemaMergingUtils/SchemaInferenceUtilsare general pipeline-table utilities, so the fix covers any SDP table that evolves its schema under a case-insensitive session.Why are the changes needed?
It is a correctness bug. Evolving a table under the default case-insensitive session with a column that differs only in case from an existing one produced a target schema with two columns (
valueandValue) that the engine's own case-insensitive resolver cannot tell apart. The corrupt schema then failed downstream where that resolver runs:COLUMN_ALREADY_EXISTS, raised byResolveUnion.checkColumnNamesover theunionByNameinScd2ForeachBatchHandler.reconcileMicrobatch(the affected-rows union reads the now-two-column target back).AMBIGUOUS_REFERENCE, deeper in the MERGE plan (no reconcile union).The same user mistake thus surfaced as two different, confusing error conditions -- both downstream symptoms of the corrupt evolved schema rather than the root cause.
Does this PR introduce any user-facing change?
Yes, a bug fix (only within the unreleased
master/branch-4.x; the buggy behavior was never in a release). Under case-insensitive resolution (Spark's default), a pipeline that emits a column differing only in case from an existing target column now evolves the table as a no-op (the incoming value is written to the existing column) instead of corrupting the schema and failing withCOLUMN_ALREADY_EXISTS(SCD2) orAMBIGUOUS_REFERENCE(SCD1). Underspark.sql.caseSensitive=true, the two names remain distinct and the new column is added, as before.How was this patch tested?
Added and updated tests:
SchemaInferenceUtilsSuite-- new unit tests fordiffSchemas: case-only difference is distinct under case-sensitive, a no-op under case-insensitive, and a case-insensitive type change addresses the current (persisted) column name.MaterializeTablesSuite-- an end-to-end pair (non-AutoCDC path) asserting noalterTableis issued and the persisted column keeps its original case under case-insensitive resolution, versus a column being added under case-sensitive.AutoCdcScd1SchemaEvolutionSuite-- rewrote the case-only-difference test from asserting the oldAMBIGUOUS_REFERENCEfailure to asserting the fixed no-op-merge success; refreshed the suite header and a now-stale comment inAutoCdcScd1KeyDriftSuite.Ran the full
pipelinesmodule test suite. All pass except two failures (TriggeredGraphExecutionSuite: "stream failure on deletes and updates", "stopping a pipeline mid-execution") that I confirmed are pre-existing on cleanmaster(they fail identically with this change stashed), so this PR introduces no regressions.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 4.8