[SPARK-59015][SQL] Rebind refreshed DSv2 relations to the current table schema #58298
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #58298"
general
weight: 0.35
yyanyy
What changes were proposed in this pull request?
When
V2TableRefreshUtilrefreshes aDataSourceV2RelationinALLOW_NEW_FIELDSmode, the relationis now replaced with a
Projectover a relation that exposes the current table schema: the relationstays aligned with the physical scan, while the projection recreates the captured output — names,
expression IDs, types, nullability and metadata — for the already-analyzed parent plan. A new
CapturedSchemaProjectionbuilds it, recursing through structs, arrays and maps, matching data columnsby name and metadata columns by their logical name. When the captured and current outputs already
match, the relation is returned unchanged, so an unchanged table keeps its plan and the rewrite is
idempotent under
transformDown.One shape cannot be recreated, and this PR makes it fail rather than answer wrongly: a
MAPwhosekey type gained a field. Keys that are distinct under the current key type can be equal under the
captured one, so narrowing the key back changes how many entries the map has. The rebuild goes through
Spark's map builder, which enforces key uniqueness, so the query raises
DUPLICATED_MAP_KEY. Detailsunder user-facing changes.
Why are the changes needed?
Spark refreshes versioned DSv2 tables between analysis and optimization.
V2TableRefreshUtildoesr.copy(table = currentTable), deliberately swapping in the current table while keeping the analyzedoutput, which leaves:
PushDownUtils.pruneColumnsthen callstoOutputAttrs(scan.readSchema(), relation), which builds itsname map only from
relation.output, so any field the captured output does not know about throwsjava.util.NoSuchElementException: key not found: new_column. The trigger is precisely thatScan.readSchema()field names are not a subset ofrelation.outputfield names — not merely thatreadSchemais wider than the query's project.Reporting more than was requested is allowed:
SupportsPushDownRequiredColumns.pruneColumnspermitspartial pruning, its example being a source that only prunes top-level columns. That example alone
reproduces the failure:
A one-shot query analyzes and optimizes back to back, so this window is normally invisible; it becomes
deterministic by forcing analysis on a
DataFramebefore changing the table, or via a temp viewcreated from a
Dataset. This is not Connect-specific — the refresh phase lives in commonQueryExecution.Does this PR introduce _any_ user-facing change?
Yes. Refresh exists so an already-analyzed plan runs against the table's current schema, so where the
captured output can still be reproduced the query now succeeds, and where it cannot the query fails
instead of improvising an answer.
gained a column, previously failed with
NoSuchElementExceptionorPLAN_VALIDATION_FAILED_RULE_IN_BATCH. They now return the captured schema.it is intended. With a captured
MAP<STRUCT<name: STRING>, INT>:Both keys are distinct now, but both are
{name: 'a'}in the captured schema, so the map cannot holdtwo entries there. Previously the scan was asked to narrow the key itself and returned a map holding
duplicate keys, which Spark propagated unchecked:
size(m)reported both entries while collectingthe row kept only the last. The projection now rebuilds the captured key through Spark's map builder,
so the query fails with
DUPLICATED_MAP_KEY— or keeps the last entry underspark.sql.mapKeyDedupPolicy=LAST_WIN. Narrowing a map key is a fold rather than a projection and isnot something a scan can be asked to perform; a captured key type that cannot represent the current
key set has no correct answer to return.
is identical to before.
How was this patch tested?
New
CapturedSchemaProjectionSuite(27 tests) covers the projection at the expression level: nestedstruct/array/map shapes with a field added at every level and a different ordinal shift per level; an
unchanged
VARIANTleaf, which must be carried by reference rather than rebuilt; nulls; resolver-basedname matching; captured metadata columns; captured field metadata surviving alias cleanup; a map key
that gained a field under both
spark.sql.mapKeyDedupPolicyvalues; and every rejection path.DataSourceV2DataFrameSuitegains 9 end-to-end tests that run an already-analyzed plan across a tablechange, mostly against a source that reports every data column from
readSchema(): flat and nestedwidening, a column added first, an external case-only rename, two metadata-column cases, a self-join,
and the map key above. Disabling the rebinding call in
V2TableRefreshUtilfails 8 of the 9.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code 2.1.268