← Back to Pull requests

[SPARK-58779][SQL] Make InlineCTE tolerate a CTE reference without its definition during analysis #58012

Closed
unknown closed this pull request without merging on Aug 20, 2026 13:56 UTC
View on GitHub
unknown unknown commented on Aug 14, 2026 19:42 UTC
Author

What changes were proposed in this pull request?

InlineCTE now guards its two cteMap(ref.cteId) lookups (in buildCTEMap and inlineCTE) so that a CTERelationRef whose CTERelationDef is not present in the plan being processed is handled instead of throwing a bare NoSuchElementException. A new isAnalysis flag (default true) distinguishes the two kinds of caller:

  • During analysis (CheckAnalysis.checkAnalysis, which runs InlineCTE(alwaysInline = true, keepDanglingRelations = true)), such an out-of-scope reference is tolerated and left for the scope that owns the definition to resolve.

  • In the optimizer (isAnalysis = false), the plan is complete, so a reference with no definition indicates a corrupt plan and raises SparkException.internalError.
  • The optimizer's InlineCTE() call in Optimizer.scala is updated to InlineCTE(isAnalysis = false); the analysis-time callers keep the default.

    Why are the changes needed?

    buildCTEMap and inlineCTE did unguarded cteMap(ref.cteId) lookups that threw NoSuchElementException when a CTERelationRef's definition is not in the plan being processed. CheckAnalysis.checkAnalysis runs InlineCTE and only catches AnalysisException, so the NoSuchElementException propagated uncaught.

    This happens when checkAnalysis runs on a subplan that references an outer CTE - e.g. ResolveSQLTableFunctions calls checkAnalysis on a resolved SQL table function whose argument is a scalar subquery referencing an outer CTE:

    CREATE FUNCTION my_tvf(a INT, b INT) RETURNS TABLE(x INT) RETURN SELECT a + b AS x;
    
    WITH cte AS (SELECT 1 AS c1, 2 AS c2)
    SELECT * FROM my_tvf((SELECT c1 FROM cte), (SELECT c2 FROM 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:

  • SQLFunctionSuite - end-to-end: a SQL table function called with scalar-subquery arguments that reference a CTE now succeeds (previously threw NoSuchElementException during analysis).

  • InlineCTESuite - unit: the optimizer path (isAnalysis = false) raises INTERNAL_ERROR on a CTERelationRef with no definition, while the analysis path (isAnalysis = true) tolerates it.
  • Was this patch authored or co-authored using generative AI tooling?

    Yes. Generated-by: Claude Code (Anthropic).

    Diff: 345 lines (+258 / -87) across 4 files Reviewer: vladimirg-db Review duration: 8294m
    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
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.4% (MEDIUM) · Higher = larger blast radius
    42.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.7% · Safe to merge
    16.7%
    Repository