← Back to Pull requests

[SPARK-61444][SQL] Keep the result on zero rows of a correlated subquery as an expression if it fails to fold #60325

Closed
unknown closed this pull request without merging on Nov 17, 2026 13:57 UTC
View on GitHub
unknown unknown commented on Nov 11, 2026 07:42 UTC
Author

What changes were proposed in this pull request?

RewriteCorrelatedScalarSubquery.tryEvalExpr now keeps the expression when evaluating it fails, the same as it already does for an expression that is not foldable (SPARK-28441). Like ConstantFolding.tryFold in a conditional branch (SPARK-39106), it evaluates a fresh copy of a stateful expression, and tags an expression that failed with FAILED_TO_EVALUATE so that ConstantFolding does not evaluate it again. It tags a copy, so that the expression in the plan, e.g. a constant 10 / 0 in the output of the Aggregate, is still evaluated there. When the evaluation fails, it returns the expression without its aliases, as for a folded value.

The Filter case of evalSubqueryOnZeroTups (a HAVING clause) now checks whether the bound condition was evaluated to a Literal, instead of checking foldable and calling eval() again, so a condition that failed to evaluate is kept as well.

The Scaladoc of bindingExpr, evalAggExprOnZeroTups, evalAggregateOnZeroTups and evalSubqueryOnZeroTups, and the comment and assert message in DecorrelateInnerQuery that use the result, now say that a result that can't be evaluated statically is kept as an expression.

Why are the changes needed?

With ANSI mode on (the default), a correlated subquery fails while the query is optimized when its result on zero rows throws, even if every outer row has matching rows:

CREATE OR REPLACE TEMP VIEW o(k) AS VALUES (1), (2);
CREATE OR REPLACE TEMP VIEW i(k, v) AS VALUES (1, 100), (1, 200), (2, 300);

SELECT k, (SELECT 10 / COALESCE(MAX(v), 0) FROM i WHERE i.k = o.k) FROM o;
-- [DIVIDE_BY_ZERO] Division by zero. Expected (1, 0.05), (2, 0.0333...)

With spark.sql.ansi.enabled=false, the same query returns the expected rows.

To handle the COUNT bug, evalSubqueryOnZeroTups (and DecorrelateInnerQuery, through evalAggregateOnZeroTups) computes the result of the subquery on zero rows while the query is optimized, and puts it into If(IsNull(alwaysTrue), <result on zero rows>, <subquery result>), which uses it only for outer rows without matching rows. tryEvalExpr called eval() on every foldable expression without catching its errors, so an error that can only happen for such an outer row failed the whole query, even when no such row exists. ConstantFolding keeps an expression that fails to evaluate in a conditional branch for the same reason (SPARK-39106).

DecorrelateInnerQuery also handles the COUNT bug of LATERAL, EXISTS and IN subqueries (rewriteSubQueries decorrelates them with handleCountBug = true, for EXISTS and IN with the default spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled=false), so they fail in the same way. So do other errors, and errors with ANSI mode off:

| Query, every outer row has matching rows | Before | After |
|---|---|---|
| SELECT k, (SELECT 10 / COALESCE(MAX(v), 0) FROM i WHERE i.k = o.k) FROM o | DIVIDE_BY_ZERO | (1, 0.05), (2, 0.0333...) |
| SELECT k, (SELECT CAST(COALESCE(MAX(CAST(v AS STRING)), 'x') AS INT) FROM i WHERE i.k = o.k) FROM o | CAST_INVALID_INPUT | (1, 200), (2, 300) |
| SELECT k FROM o WHERE (SELECT 10 / COALESCE(MAX(v), 0) FROM i WHERE i.k = o.k) > 0.04 | DIVIDE_BY_ZERO | (1) |
| SELECT k, (SELECT COUNT() FROM i WHERE i.k = o.k HAVING 10 / COUNT() > 0) FROM o | DIVIDE_BY_ZERO | (1, 2), (2, 1) |
| SELECT k, (SELECT MAX(c) FROM (SELECT 10 / COUNT() c FROM i WHERE i.k = o.k)) FROM o | DIVIDE_BY_ZERO | (1, 5.0), (2, 10.0) |
| SELECT o.k, t.d FROM o, LATERAL (SELECT 10 / COALESCE(MAX(i.v), 0) d FROM i WHERE i.k = o.k) t | DIVIDE_BY_ZERO | (1, 0.05), (2, 0.0333...) |
| SELECT k FROM o WHERE EXISTS (SELECT 10 / COALESCE(MAX(v), 0) FROM i WHERE i.k = o.k) | DIVIDE_BY_ZERO | (1), (2) |
| SELECT k FROM o WHERE 5 IN (SELECT 1000 / COALESCE(MAX(v), 0) FROM i WHERE i.k = o.k) | DIVIDE_BY_ZERO | (1) |
| ANSI off: SELECT k, (SELECT element_at(array(1, 2), CAST(COUNT(v) AS INT)) FROM i WHERE i.k = o.k) FROM o | INVALID_INDEX_OF_ZERO | (1, 2), (2, 1) |

In the default confs, the HAVING query is handled in DecorrelateInnerQuery. With spark.sql.optimizer.decorrelateInnerQuery.enabled=false, it is handled in CASE 3 of constructLeftJoins, which puts the result on zero rows into a CaseWhen, and fails and is fixed in the same way.

Does this PR introduce _any_ user-facing change?

Yes. A correlated subquery whose result on zero rows fails to evaluate, e.g. with DIVIDE_BY_ZERO or CAST_INVALID_INPUT in ANSI mode, or INVALID_INDEX_OF_ZERO, no longer fails the query while it is optimized. When the result on zero rows is used for an outer row without matching rows, the query still fails with the same error: at runtime, or while the query is optimized if the subquery is known to be empty, e.g. ... FROM (SELECT FROM i WHERE false) x WHERE x.k = o.k, where the conditional branch goes away. With the default confs, a grouped scalar subquery does not use the result on zero rows, and neither does an EXISTS subquery, so they no longer fail at all. A failing expression that is also a column of the subquery, e.g. 10 / 0 d in LATERAL (SELECT count() c, 10 / 0 d FROM i WHERE i.k = o.k), still fails while the query is optimized. Queries that did not fail before are unchanged.

The code is the same in branch-3.5 and later. ANSI mode is on by default since 4.0.

How was this patch tested?

  • SPARK-61444: COUNT bug with a result on zero rows that fails to evaluate in SubquerySuite:

  • - With ANSI mode on, the queries in the table above where every outer row has matching rows return their rows. They cover the top Aggregate in constructLeftJoins (in SELECT and WHERE), a HAVING condition, a CAST error, a lower Aggregate in DecorrelateInnerQuery, and LATERAL, EXISTS and IN subqueries.
    - With an outer row without matching rows, the grouped scalar subquery and the EXISTS subquery return their rows. The scalar (in SELECT and WHERE), HAVING, lower Aggregate, LATERAL and IN queries fail with DIVIDE_BY_ZERO, and the CAST query with CAST_INVALID_INPUT. The test checks that executedPlan succeeds for each of them before collect() fails, i.e. that the error comes at runtime.
    - The HAVING queries also run with spark.sql.optimizer.decorrelateInnerQuery.enabled=false, so that CASE 3 of constructLeftJoins uses the result of the Filter case.
    - LATERAL (SELECT count(
    ) c, 10 / 0 d ...) over o2 and over an empty outer table fails with DIVIDE_BY_ZERO while the query is optimized, as on master.
    - With ANSI mode off, the element_at query returns its rows, and fails with INVALID_INDEX_OF_ZERO at runtime with an outer row without matching rows.
    - Without the change in subquery.scala, the test fails. It also fails if the last case of the Filter branch is preceded by case c if c.foldable => Map.empty or by case c if c.foldable => bindings.
  • build/sbt 'catalyst/testOnly RewriteSubquerySuite DecorrelateInnerQuerySuite PullupCorrelatedPredicatesSuite OptimizerSuite', build/sbt 'sql/testOnly org.apache.spark.sql.SubquerySuite' and build/sbt 'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z subquery' (no golden file changes) pass.

  • SPARK-61444: keep the result on zero rows of an Aggregate that fails to evaluate in RewriteSubquerySuite checks that evalAggregateOnZeroTups keeps 10 / coalesce(max(v), 0) with ANSI mode on as an expression without an Alias and with the FAILED_TO_EVALUATE tag, and evaluates it to null with ANSI mode off. For it and for a constant 10 / 0, it checks that the expression in the Aggregate is not tagged.

  • catalyst/scalastyle, catalyst/Test/scalastyle, sql/scalastyle and sql/Test/scalastyle pass.
  • Was this patch authored or co-authored using generative AI tooling?

    Generated-by: Claude Code (Claude Opus 5.5)

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: sarutak Review duration: 9015m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #60325"
    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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 20.5% · Safe to merge
    20.5%
    Repository