[SPARK-58779][SQL] Make InlineCTE tolerate a CTE reference without its definition during analysis #58012
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 345 lines diff across 4 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.350
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #58012"
general
weight: 0.35
vladimirg-db
What changes were proposed in this pull request?
InlineCTEnow guards its twocteMap(ref.cteId)lookups (inbuildCTEMapandinlineCTE) so that aCTERelationRefwhoseCTERelationDefis not present in the plan being processed is handled instead of throwing a bareNoSuchElementException. A newisAnalysisflag (defaulttrue) distinguishes the two kinds of caller:CheckAnalysis.checkAnalysis, which runsInlineCTE(alwaysInline = true, keepDanglingRelations = true)), such an out-of-scope reference is tolerated and left for the scope that owns the definition to resolve.isAnalysis = false), the plan is complete, so a reference with no definition indicates a corrupt plan and raisesSparkException.internalError.The optimizer's
InlineCTE()call inOptimizer.scalais updated toInlineCTE(isAnalysis = false); the analysis-time callers keep the default.Why are the changes needed?
buildCTEMapandinlineCTEdid unguardedcteMap(ref.cteId)lookups that threwNoSuchElementExceptionwhen aCTERelationRef's definition is not in the plan being processed.CheckAnalysis.checkAnalysisrunsInlineCTEand only catchesAnalysisException, so theNoSuchElementExceptionpropagated uncaught.This happens when
checkAnalysisruns on a subplan that references an outer CTE - e.g.ResolveSQLTableFunctionscallscheckAnalysison a resolved SQL table function whose argument is a scalar subquery referencing an outer CTE:The resolved function plan contains CTERelationRef(cte), while its CTERelationDef lives in the outer WithCTE (outside the subplan that checkAnalysis is validating), so the lookup throws.
Does this PR introduce any user-facing change?
Yes. A query such as the one above previously failed analysis with an internal NoSuchElementException (both on released versions and on master); with this change it analyzes and executes correctly. There is no API or result-schema change.
How was this patch tested?
New tests:
Was this patch authored or co-authored using generative AI tooling?
Yes. Generated-by: Claude Code (Anthropic).