Skip to content

Restructure the Cascades optimizer code following the review of #86353 - #115293

Merged
davenger merged 7 commits into
masterfrom
cascades_restructure
Aug 18, 2026
Merged

davenger merged 7 commits into
masterfrom
cascades_restructure

Conversation

@davenger

@davenger davenger commented Aug 18, 2026 •

Copy link
Copy Markdown
Member

Related: #86353

Restructures the Cascades optimizer code to address the review comments on #86353. No behavior changes: every plan-shape reference is byte-identical, and the Cascades gtests and the distributed-plan test suite pass unchanged.

  • CascadesOptimizer now owns the search engine: the memo, the rule registries, the task stack, costing, and plan extraction. The former OptimizationEnvironment fields become the new OptimizerContext, a plain per-query settings struct stored in the memo.
  • One rule per file: LocalRead, ParallelRead, ReplicatedRead, TopN, and TwoStageTopN each moved into their own source file. The helpers shared by the rules moved to RuleUtils, and the fixed model constants (task budget, fallback selectivities, row-width defaults) moved to OptimizerDefaults.h. The per-operator cost constants stay in CostConfig because a query can override them.
  • Implementation strategies are stateless, so they became per-type singletons (strategySingleton<T>), and expressions compare them by pointer instead of by name.
  • EnforcerAxis is renamed to EnforcedProperty.
  • Optimization tasks are owned by std::unique_ptr instead of std::shared_ptr.
  • Logical expressions in a group now get the same structural deduplication as physical ones, so a transformation that derives an already-known alternative does not grow the group. Covered by a new case in gtest_cascades_group_dedup.
  • Small cleanups from the review: the eligibility check in Group::selectInputImplementation is inlined into the selection lambda, DistributionPassthrough rejects an impossible variant before it copies the GroupExpression, and the repeated input-requirement construction in DistributionEnforcer is extracted into inputAtSourceDistribution.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Workflow [PR]
Sync PR [sync-upstream/pr/115293]

davenger and others added 5 commits August 18, 2026 14:53
…ontext` to the per-query settings

The engine members and methods (memo, rules, task stack, costing) move from
`OptimizerContext` into `CascadesOptimizer`, and the former
`OptimizationEnvironment` fields become the new `OptimizerContext`.

Co-Authored-By: Claude Fable 5 <[email protected]>
…zerDefaults`; strategies become per-type singletons compared by pointer; rename `EnforcerAxis` to `EnforcedProperty`

Co-Authored-By: Claude Fable 5 <[email protected]>
…he passthrough, extract `inputAtSourceDistribution`

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f451906]

Summary: ✅


AI Review

Summary

This PR is a large internal refactor of the CascadesOptimizer plus a follow-up guard that makes the optimizer explicitly single-use. I did not find any remaining correctness, safety, or performance-regression issues in the current code, and the earlier memo-reuse lifecycle problem is fixed by the new optimize_was_called check.

Missing context / blind spots
  • ⚠️ The only Praktika report currently attached to the PR is Build profile diff; I did not have external runtime CI evidence beyond the tests described in the PR body.
Final Verdict

✅ No blocking findings in the current revision.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.90% 86.80% -0.10%
Functions 92.00% 92.00% +0.00%
Branches 79.30% 79.20% -0.10%

Changed lines: Changed C/C++ lines covered: 603/659 (91.50%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 18, 2026
… after the `TwoStageTopN` split

Co-Authored-By: Claude Fable 5 <[email protected]>
@novikd novikd self-assigned this Aug 18, 2026

@novikd novikd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f45190623 with master d5011f599 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 702.06 MiB 699.04 MiB -3.02 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes

20 object files changed (+327.12 KiB total), 4 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/RuleUtils.cpp.o new 171.60 KiB +171.60 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Rules/ParallelReadImplementatio… 222.70 KiB 61.23 KiB -161.47 KiB (-72.51%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Rules/TwoStageTopN.cpp.o new 107.98 KiB +107.98 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Rules/LocalReadImplementation.c… new 90.19 KiB +90.19 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Optimizer.cpp.o 207.19 KiB 290.14 KiB +82.95 KiB (+40.04%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Rules/ReplicatedReadImplementat… new 82.20 KiB +82.20 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Rules/TopNImplementation.cpp.o 140.55 KiB 106.07 KiB -34.48 KiB (-24.53%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Task.cpp.o 212.21 KiB 196.00 KiB -16.21 KiB (-7.64%)

738 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

30 translation units recompiled, 175 s compile time in total, 26 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.20 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +24.2 s (+21%) in total before that adjustment.

Translation units without a recent master baseline:

  • src/Processors/QueryPlan/Optimizations/Cascades/RuleUtils.cpp: 10.3 s
  • src/Processors/QueryPlan/Optimizations/Cascades/Rules/LocalReadImplementation.cpp: 9.8 s
  • src/Processors/QueryPlan/Optimizations/Cascades/Rules/ReplicatedReadImplementation.cpp: 9.6 s

Job report

…instead of reusing the memo

Co-Authored-By: Claude Fable 5 <[email protected]>
@davenger
davenger added this pull request to the merge queue Aug 18, 2026
Merged via the queue into master with commit 6a4063a Aug 18, 2026
181 checks passed
@davenger
davenger deleted the cascades_restructure branch August 18, 2026 22:51
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants