Skip to content

fix: allow a materialized CTE in a correlated scalar subquery - #27737

Open
adriangb wants to merge 1 commit into
apache:mainfrom
adriangb:fix-materialized-cte-scalar-subquery
Open

adriangb wants to merge 1 commit into
apache:mainfrom
adriangb:fix-materialized-cte-scalar-subquery

Conversation

@adriangb

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • No issue filed. The bug and fix are small and described below.

Rationale for this change

This query fails with "Correlated scalar subquery must be aggregated to return at most one row". The same query with NOT MATERIALIZED returns the correct rows.

SELECT column1, (WITH c AS MATERIALIZED (SELECT column1 x, column2 y FROM t)
                 SELECT max(y) FROM c WHERE c.x = t.column1) FROM t;

What changes are included in this PR?

strip_inner_query in datafusion/expr/src/logical_plan/invariants.rs looks through Projection and SubqueryAlias. It now also looks through a MaterializedCte node into its continuation, which holds the Aggregate. Decorrelation (scalar_subquery_to_join) already handles the resulting plan.

What is the testing strategy for this PR?

New cases in cte_materialized.slt: one with a match for every outer row, and one where some outer rows have no match and get NULL.

Are there any user-facing changes?

The query above now runs. No API changes.

🤖 Generated with Claude Code

The analyzer rejected a correlated scalar subquery whose body is a
`WITH ... AS MATERIALIZED` query. `strip_inner_query` did not look
through the `MaterializedCte` root, so it did not find the `Aggregate`
in the continuation. It now looks into the continuation. Decorrelation
already handles the plan and returns the same rows as NOT MATERIALIZED.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Nov 25, 2026
@adriangb
adriangb marked this pull request as ready for review November 25, 2026 05:50
@adriangb
adriangb requested review from 2010YOUY01 and a balanced review from Copilot November 25, 2026 05:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The newly accepted CTE wrapper loses count-expression metadata, causing correlated COUNT queries to return NULL instead of 0 for unmatched rows; the metadata must be propagated and regression coverage added.

1 open issue
What changed in this PR

This PR updates logical-plan validation to allow correlated scalar subqueries whose aggregate is inside a materialized CTE’s continuation.

Changes:

  • Extend strip_inner_query to look through materialized CTE wrappers.
  • Add SQL regression cases for MAX subqueries with matching rows and unmatched rows that return NULL.
File Description
datafusion/​sqllogictest/​test_files/​cte_materialized.slt Adds correlated scalar aggregate regression cases.
datafusion/​expr/​src/​logical_plan/​invariants.rs Traverses the CTE continuation when validating scalar subqueries.

Review effort: Balanced

Comment on lines +419 to +420
LogicalPlan::MaterializedCte(MaterializedCtePlan::Cte(cte)) => {
strip_inner_query(cte.continuation.as_ref())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants