More CommonSubplanOptimizer improvements#20168
Merged
lnkuiper merged 34 commits intoduckdb:mainfrom Dec 15, 2025
Merged
Conversation
added 30 commits
November 11, 2025 14:16
…mon subplan optimizer in filter pullup test so it still tests the right behaviour
github-actions bot
pushed a commit
to duckdb/duckdb-r
that referenced
this pull request
Feb 24, 2026
More `CommonSubplanOptimizer` improvements (duckdb/duckdb#20168)
github-actions bot
added a commit
to duckdb/duckdb-r
that referenced
this pull request
Feb 24, 2026
More `CommonSubplanOptimizer` improvements (duckdb/duckdb#20168) Co-authored-by: krlmlr <krlmlr@users.noreply.github.com>
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.
This PR improves the
CommonSubplanOptimizer, and should put it in "maintenance mode", at least for now, and I think I'm done improving it for a while.Multiple Nested Matching
This PR allows multiple subplans to be matched, rather than just a single subplan, and allows nesting. Take the following query, for example:
This query unions 6 queries together. Each query occurs twice, and is nested in the next query. With the optimizer disabled, this yields the following plan:
As we can see, there is a lot of redundance here. With this PR (and the optimizer enabled, of course), we get the following plan:
Which has 3 CTEs, 8 CTE scans, and only 3 aggregations (instead of the original 12!).
Fuzzy Plan Matching
Something that can show up in query plans is an "almost" exact subplan match. Before this PR, an exact match was required. With this PR, we can do "fuzzy" matching, where the plan is mostly the same, save for some selected columns. If we have the following query, for example:
Here, one of the unioned queries selects all columns, and the other selects just two columns. As we can see, the join (coming from the view) in the second query is "contained" in the join in first query, as the first query selects all columns that the second query needs.
We currently get the following plan (the optimizer doesn't trigger):
With the improvements to the optimizer in this PR, we now get this plan:
As we can see, the join is materialized as a CTE, and scanned twice. Columns that aren't needed are projected out after the CTE scan.
Benchmark Improvements
With the changes, we now trigger more subplan elimination on TPC-DS and TPC-H. Here are some results that I collected on my laptop.
TPC-DS SF100 improvements:
Q61: 0.61s -> 0.33s (~1.8x)
Q70: 0.81s -> 0.55s (~1.5x)
TPC-H SF100 improvements:
Q11: 0.16s -> 0.12s (~1.3x)
Notes
I needed a lot of indirection to get the column bindings to match with the fuzzy plan matching, which required a lot of
unordered_maps. To avoid doing so many allocations, I've also implementedarena_unordered_map, so that the number of allocations grow logarithmically. I'm not sure how much this helped, but it's just something I wanted to do as we are trying to reduce allocations. Overall, this optimizer now takes ~10% of total optimization time.