Repository navigation
Conversation
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>
adriangb
marked this pull request as ready for review
November 25, 2026 05:50
adriangb
requested review from
2010YOUY01
and
a balanced review from Copilot
November 25, 2026 05:50
Contributor
There was a problem hiding this comment.
🟡 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_queryto look through materialized CTE wrappers. - Add SQL regression cases for
MAXsubqueries with matching rows and unmatched rows that returnNULL.
| 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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Which issue does this PR close?
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 MATERIALIZEDreturns the correct rows.What changes are included in this PR?
strip_inner_queryindatafusion/expr/src/logical_plan/invariants.rslooks throughProjectionandSubqueryAlias. It now also looks through aMaterializedCtenode into its continuation, which holds theAggregate. 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 getNULL.Are there any user-facing changes?
The query above now runs. No API changes.
🤖 Generated with Claude Code