Skip to content

[SPARK-58116][SDP] Validate user-specified schema for tables fed by a named flow#57245

Closed
anew wants to merge 3 commits into
apache:masterfrom
anew:fix-autocdc-schema-validation-keying
Closed

[SPARK-58116][SDP] Validate user-specified schema for tables fed by a named flow#57245
anew wants to merge 3 commits into
apache:masterfrom
anew:fix-autocdc-schema-validation-keying

Conversation

@anew

@anew anew commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

GraphValidations.validateUserSpecifiedSchemas looked up a table by the incoming flow's own identifier (table.get(f.identifier)). That only matches when the flow is an implicit/default flow whose identifier equals its destination table's. For a named flow (e.g. CREATE FLOW <name> AS AUTO CDC INTO <target>, or any explicitly-named flow) the lookup returned None, so the table's declared schema was never validated against the inferred schema.

This changes the lookup to key on f.destinationIdentifier (with .distinct, since multiple flows can share a destination), matching the sibling validateFlowStreamingness validation.

Why are the changes needed?

An incompatible user-declared schema on a named-flow table went undetected at graph-validation time and surfaced only as a confusing mid-stream runtime failure at materialization.

Does this introduce any user-facing change?

Yes: an incompatible user-specified schema on a table fed by a named flow now fails with USER_SPECIFIED_AND_INFERRED_SCHEMA_NOT_COMPATIBLE at validation time, as it already did for the implicit-flow form.

How was this patch tested?

New UserSpecifiedSchemaValidationSuite covers validateUserSpecifiedSchemas across the flow-identity axis (implicit flow, where the flow identifier equals the destination table's, vs. named flow, where it differs) and the schema-compatibility axis, for both plain and AUTO CDC flows:

  • Plain flow (inferred schema == the source's data columns), implicit and named: a compatible declared schema is accepted; one missing a data column is rejected.
  • AUTO CDC flow (inferred schema == the data columns plus the appended reserved __spark_autocdc_metadata column), implicit and named: a data-only declared schema is rejected (it omits the metadata column); one that also includes the metadata column is accepted.

The named-flow cases fail without the fix (the table lookup returns None, so validation is skipped). The AUTO CDC positive cases derive the expected schema from the resolved graph's inferred schema rather than hardcoding the reserved metadata struct. Also ran ConnectValid/Invalid, SqlPipeline, and AutoCdc pipeline suites green.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.8

… named flow

### What changes were proposed in this pull request?

`GraphValidations.validateUserSpecifiedSchemas` looked up a table by the incoming flow's own
identifier (`table.get(f.identifier)`). That only matches when the flow is an implicit/default
flow whose identifier equals its destination table's. For a named flow (e.g.
`CREATE FLOW <name> AS AUTO CDC INTO <target>`, or any explicitly-named flow) the lookup returned
`None`, so the table's declared schema was never validated against the inferred schema.

This changes the lookup to key on `f.destinationIdentifier` (with `.distinct`, since multiple
flows can share a destination), matching the sibling `validateFlowStreamingness` validation.

### Why are the changes needed?

An incompatible user-declared schema on a named-flow table went undetected at graph-validation
time and surfaced only as a confusing mid-stream runtime failure at materialization.

### Does this introduce _any_ user-facing change?

Yes: an incompatible user-specified schema on a table fed by a named flow now fails with
`USER_SPECIFIED_AND_INFERRED_SCHEMA_NOT_COMPATIBLE` at validation time, as it already did for the
implicit-flow form.

### How was this patch tested?

New `UserSpecifiedSchemaValidationSuite` asserts the error is raised for both the implicit- and
named-flow forms; confirmed the named-flow case fails without the fix. Ran ConnectValid/Invalid,
SqlPipeline, and AutoCdc pipeline suites (130 tests) green.

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.8

Co-authored-by: Isaac
@anew
anew force-pushed the fix-autocdc-schema-validation-keying branch from f686241 to be639bc Compare July 14, 2026 23:57
Comment on lines +256 to +261
// Look up the destination table of each flow, not the table matching the flow's own
// identifier. The two coincide only for an implicit/default flow (whose identifier equals its
// destination table's); for a named flow (e.g. `CREATE FLOW <name> AS AUTO CDC INTO <target>`)
// they differ, and keying on the flow identifier would silently skip validation. `distinct`
// collapses the multiple flows that can share a single destination.
flows.flatMap(f => table.get(f.destinationIdentifier)).distinct.foreach { t: TableElement =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

flowsTo is already grouped by destinationIdentifier and its keys are distinct, so we can iterate it directly and drop .distinct. This also mirrors the sibling validateFlowStreamingness, which walks flowsTo, keeping the two validations structurally aligned.

Suggested change
// Look up the destination table of each flow, not the table matching the flow's own
// identifier. The two coincide only for an implicit/default flow (whose identifier equals its
// destination table's); for a named flow (e.g. `CREATE FLOW <name> AS AUTO CDC INTO <target>`)
// they differ, and keying on the flow identifier would silently skip validation. `distinct`
// collapses the multiple flows that can share a single destination.
flows.flatMap(f => table.get(f.destinationIdentifier)).distinct.foreach { t: TableElement =>
flowsTo.keys.flatMap(table.get).foreach { t: TableElement =>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes good catch

Comment on lines +105 to +108
private def assertSchemaIncompatible(graph: DataflowGraph): Unit = {
val ex = intercept[AnalysisException](graph.validate())
assert(ex.getCondition == "USER_SPECIFIED_AND_INFERRED_SCHEMA_NOT_COMPATIBLE")
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since the fix is that the error is now attributed to the right table for named flows, assert the message actually names the target too, not just the error condition. targetIdentifier is already in scope, so no signature change is needed.

Suggested change
private def assertSchemaIncompatible(graph: DataflowGraph): Unit = {
val ex = intercept[AnalysisException](graph.validate())
assert(ex.getCondition == "USER_SPECIFIED_AND_INFERRED_SCHEMA_NOT_COMPATIBLE")
}
private def assertSchemaIncompatible(graph: DataflowGraph): Unit = {
val ex = intercept[AnalysisException](graph.validate())
assert(ex.getCondition == "USER_SPECIFIED_AND_INFERRED_SCHEMA_NOT_COMPATIBLE")
assert(ex.getMessage.contains(targetIdentifier.unquotedString))
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok that makes sense

…t name

Iterate `flowsTo.keys` directly (its keys are already distinct destination
identifiers) instead of collecting flows and calling `.distinct`, mirroring
the sibling `validateFlowStreamingness`. Also assert the incompatibility error
message names the target table, verifying the fix attributes the error to the
right table for named flows.

Co-authored-by: Isaac
// Look up tables by their destination identifier, not by the flow's own identifier. The two
// coincide only for an implicit/default flow (whose identifier equals its destination
// table's); for a named flow (e.g. `CREATE FLOW <name> AS AUTO CDC INTO <target>`) they
// differ, and keying on the flow identifier would silently skip validation. `flowsTo` is

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: can we remove this comment 'flowsTo' is grouped by destination... mirroring 'validateFlowStreamingness'

(seems too low level)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok

Drop the sentence describing how `flowsTo` is grouped and that it mirrors
`validateFlowStreamingness`; keep the rationale for keying on the destination.

Co-authored-by: Isaac

@szehon-ho szehon-ho left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Nice catch on this latent bug: keying the table lookup on the flow's own identifier meant named flows (e.g. CREATE FLOW <name> AS AUTO CDC INTO <target>) silently skipped user-specified schema validation, since the flow and destination identifiers only coincide for implicit flows. Keying on the destination via flowsTo fixes it and aligns with the sibling validateFlowStreamingness. The test matrix over the flow-identity and schema-compatibility axes (plain and AUTO CDC) is thorough.

@szehon-ho

Copy link
Copy Markdown
Member

One more thing worth calling out about the latent bug: keying on f.identifier didn't only skip validation for named flows, it could validate the wrong table. If a named flow's identifier happened to collide with some other table's identifier, the old table.get(f.identifier) would pull up that unrelated table and validate it, while the flow's actual destination table went unchecked. Keying on the destination identifier removes that misattribution too.

@szehon-ho szehon-ho closed this in 2f3ff08 Jul 16, 2026
szehon-ho pushed a commit that referenced this pull request Jul 16, 2026
… named flow

### What changes were proposed in this pull request?

`GraphValidations.validateUserSpecifiedSchemas` looked up a table by the incoming flow's own identifier (`table.get(f.identifier)`). That only matches when the flow is an implicit/default flow whose identifier equals its destination table's. For a named flow (e.g. `CREATE FLOW <name> AS AUTO CDC INTO <target>`, or any explicitly-named flow) the lookup returned `None`, so the table's declared schema was never validated against the inferred schema.

This changes the lookup to key on `f.destinationIdentifier` (with `.distinct`, since multiple flows can share a destination), matching the sibling `validateFlowStreamingness` validation.

### Why are the changes needed?

An incompatible user-declared schema on a named-flow table went undetected at graph-validation time and surfaced only as a confusing mid-stream runtime failure at materialization.

### Does this introduce _any_ user-facing change?

Yes: an incompatible user-specified schema on a table fed by a named flow now fails with `USER_SPECIFIED_AND_INFERRED_SCHEMA_NOT_COMPATIBLE` at validation time, as it already did for the implicit-flow form.

### How was this patch tested?

New UserSpecifiedSchemaValidationSuite covers validateUserSpecifiedSchemas across the flow-identity axis (implicit flow, where the flow identifier equals the destination table's, vs. named flow, where it differs) and the schema-compatibility axis, for both plain and AUTO CDC flows:

  - Plain flow (inferred schema == the source's data columns), implicit and named: a compatible declared schema is accepted; one missing a data column is rejected.
  - AUTO CDC flow (inferred schema == the data columns plus the appended reserved __spark_autocdc_metadata column), implicit and named: a data-only declared schema is rejected (it omits the metadata column); one that also includes the metadata column is accepted.

The named-flow cases fail without the fix (the table lookup returns None, so validation is skipped). The AUTO CDC positive cases derive the expected schema from the resolved graph's inferred schema rather than hardcoding the reserved metadata struct. Also ran ConnectValid/Invalid, SqlPipeline, and AutoCdc pipeline suites green.

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.8

Closes #57245 from anew/fix-autocdc-schema-validation-keying.

Authored-by: Andreas Neumann <anew@apache.org>
Signed-off-by: Szehon Ho <szehon.apache@gmail.com>
(cherry picked from commit 2f3ff08)
Signed-off-by: Szehon Ho <szehon.apache@gmail.com>
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