fix: Optimize projections in recursive CTEs#22476
Open
nuno-faria wants to merge 3 commits into
Open
Conversation
nuno-faria
commented
May 23, 2026
| } | ||
|
|
||
| #[test] | ||
| fn recursive_cte_with_unused_columns() -> Result<()> { |
Contributor
Author
There was a problem hiding this comment.
This test was very similar to the now named recursive_cte_outer_projection_pushdown.
nuno-faria
commented
May 23, 2026
Comment on lines
+845
to
+861
| 06)------Cross Join: | ||
| 07)--------Projection: | ||
| 08)----------Filter: recursive_cte.val < Int64(2) | ||
| 09)------------TableScan: recursive_cte projection=[val] | ||
| 10)--------SubqueryAlias: sub_cte | ||
| 11)----------EmptyRelation: rows=1 | ||
| physical_plan | ||
| 01)RecursiveQueryExec: name=recursive_cte, is_distinct=false | ||
| 02)--ProjectionExec: expr=[1 as val] | ||
| 03)----PlaceholderRowExec | ||
| 04)--ProjectionExec: expr=[2 as val] | ||
| 05)----CrossJoinExec | ||
| 06)------CoalescePartitionsExec | ||
| 07)--------FilterExec: val@0 < 2 | ||
| 07)--------FilterExec: val@0 < 2, projection=[] | ||
| 08)----------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 | ||
| 09)------------WorkTableExec: name=recursive_cte | ||
| 10)------ProjectionExec: expr=[2 as val] | ||
| 11)--------PlaceholderRowExec | ||
| 10)------PlaceholderRowExec |
Contributor
Author
There was a problem hiding this comment.
Projection is now optimized here.
nuno-faria
commented
May 23, 2026
Comment on lines
+1207
to
+1213
| 03)----TableScan: closure projection=[start, end] | ||
| 04)----Projection: l.start, r.end | ||
| 05)------Inner Join: l.end = r.start | ||
| 06)--------SubqueryAlias: l | ||
| 07)----------TableScan: trans projection=[start, end] | ||
| 08)--------SubqueryAlias: r | ||
| 09)----------TableScan: closure projection=[start, end] |
Contributor
|
This PR tests the recursive CTE projection fix for UNION ALL, but not for recursive UNION where an outer-unselected column still affects distinctness. This matters because pruning that column would collapse distinct rows incorrectly. The test is below. |
Co-authored-by: Bruce Ritchie <bruce.ritchie@gmail.com>
Contributor
Author
|
Thanks @Omega359. I've added the union test as well. |
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
Optimize projections for the static and recursive terms of recursive CTEs just like regular subqueries. The previous implementation optimized projections based on the outer columns, which could cause bugs.
This new implementation still ensures that #16684 remains fixed.
What changes are included in this PR?
optimize_projectionsto be applied to the static and recursive terms of a recursive query.Are these changes tested?
Yes.
Are there any user-facing changes?
Maybe if the user relied on projections being pushed down from the outer query to the recursive CTE, but this can be fixed by removing those unnecessary projections directly in the CTE.