[SPARK-58116][SDP] Validate user-specified schema for tables fed by a named flow#57245
[SPARK-58116][SDP] Validate user-specified schema for tables fed by a named flow#57245anew wants to merge 3 commits into
Conversation
… 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
f686241 to
be639bc
Compare
| // 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 => |
There was a problem hiding this comment.
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.
| // 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 => |
| private def assertSchemaIncompatible(graph: DataflowGraph): Unit = { | ||
| val ex = intercept[AnalysisException](graph.validate()) | ||
| assert(ex.getCondition == "USER_SPECIFIED_AND_INFERRED_SCHEMA_NOT_COMPATIBLE") | ||
| } |
There was a problem hiding this comment.
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.
| 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)) | |
| } |
…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 |
There was a problem hiding this comment.
nit: can we remove this comment 'flowsTo' is grouped by destination... mirroring 'validateFlowStreamingness'
(seems too low level)
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
left a comment
There was a problem hiding this comment.
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.
|
One more thing worth calling out about the latent bug: keying on |
… 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>
What changes were proposed in this pull request?
GraphValidations.validateUserSpecifiedSchemaslooked 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 returnedNone, 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 siblingvalidateFlowStreamingnessvalidation.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_COMPATIBLEat 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:
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