Repository navigation
Conversation
…cessors profile log Co-Authored-By: Claude Fable 5.1 <[email protected]>
…y leaves Co-Authored-By: Claude Fable 5.1 <[email protected]>
… instead of one row Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ptimizer's join estimate Co-Authored-By: Claude Fable 5.1 <[email protected]>
…a, count each predicate once Co-Authored-By: Claude Fable 5.1 <[email protected]>
…off and damped product, shared by all planners Co-Authored-By: Claude Fable 5.1 <[email protected]>
…s, reduce key NDVs through filters Co-Authored-By: Claude Fable 5.1 <[email protected]>
…e, shared by both planners Co-Authored-By: Claude Fable 5.1 <[email protected]>
…es, default provenance, saturating conversions Semi over an empty side, paste, any and asof joins follow one formula and bound in every planner; the Cascades clamps are gone. Each Cascades operator decides what an unknown or empty input means. A limit is a bound, not an estimate, and bounds nothing with ties; statistical NDVs do not bound a group count; grouping sets count the groups of every set, array joins have no bound. The statistics estimator reports every default it takes and the flag reaches the replication budget in both planners; the scan cap applies to replicated scans only; the cloud object storage read takes the budget. Float row counts convert with saturation. The rule-based rewrites keep their node estimates. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…or, one key estimate, no optional multi-key models The Cascades join, aggregation and distinct derivations call the shared `estimateJoin` and `estimateGroupStats` over the inputs as relations instead of mirroring them. The two optional models (`query_plan_join_selectivity_exponential_backoff`, `query_plan_group_count_damped_product`) and the predicate deduplication they needed are removed; the most selective key decides, as before. One saturating conversion, one saturating arithmetic, one stand-in and domain lookup, an origin enum on the join step instead of two flags, no write-only annotation flag. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…icate sections and cost noise Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [52a9aa4] Summary: ❌
AI ReviewSummaryThis PR shares cardinality estimation across join ordering, distributed planning, and Cascades; exposes estimates in Missing context / blind spots
Findings❌ Blockers
Tests
Final Verdict❌ Block. Reject stateful and server-local key expressions before exchange pushdown; repair cardinality, width, and provenance propagation at the shared boundaries above; update the serialized-plan specification; and add focused regressions for the failing traces. Existing threads already cover every finding, so no duplicate inline comments are needed. |
…y requires Co-Authored-By: Claude Fable 5.1 <[email protected]>
… is joined by its key Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
…REWHERE, widen the shard aggregation read Co-Authored-By: Claude Fable 5.1 <[email protected]>
…plan # Conflicts: # src/Processors/QueryPlan/Optimizations/optimizeJoin.cpp
…tion label Co-Authored-By: Claude Fable 5.1 <[email protected]>
…th from the estimator Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ERE move and the join algorithm Co-Authored-By: Claude Fable 5.1 <[email protected]>
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/optimizeJoin.cpp.o |
746.59 KiB | 1.24 MiB | +518.76 KiB (+69.48%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Rules/JoinEnumeration.cpp.o |
new | 132.27 KiB | +132.27 KiB |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Optimizer.cpp.o |
322.36 KiB | 441.89 KiB | +119.53 KiB (+37.08%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/RelationStatisticsEstimator.cpp.o |
146.72 KiB | 256.89 KiB | +110.17 KiB (+75.09%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/joinOrderDPSub.cpp.o |
328.14 KiB | 434.10 KiB | +105.95 KiB (+32.29%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/RelationStatistics.cpp.o |
56.70 KiB | 143.57 KiB | +86.88 KiB (+153.22%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/JoinGraphStep.cpp.o |
new | 73.76 KiB | +73.76 KiB |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/joinOrder.cpp.o |
250.45 KiB | 289.68 KiB | +39.23 KiB (+15.66%) |
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeIndexReadResultPool.cpp.o |
291.68 KiB | 323.54 KiB | +31.86 KiB (+10.92%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/joinOrderDP.cpp.o |
100.13 KiB | 131.71 KiB | +31.58 KiB (+31.54%) |
src/CMakeFiles/dbms.dir/Core/SettingsEnums.cpp.o |
2.28 MiB | 2.30 MiB | +23.57 KiB (+1.01%) |
src/CMakeFiles/dbms.dir/Core/Settings.cpp.o |
3.57 MiB | 3.59 MiB | +22.97 KiB (+0.63%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/joinOrderDPSize.cpp.o |
124.77 KiB | 105.22 KiB | -19.55 KiB (-15.67%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/joinOrderBitSet.cpp.o |
59.65 KiB | 77.92 KiB | +18.27 KiB (+30.64%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/ReplicationEligibility.cpp.o |
new | 17.52 KiB | +17.52 KiB |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/RuleUtils.cpp.o |
178.05 KiB | 195.52 KiB | +17.47 KiB (+9.81%) |
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/optimizePrewhere.cpp.o |
256.83 KiB | 273.37 KiB | +16.54 KiB (+6.44%) |
711 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
2289 translation units recompiled, 14777 s compile time in total, 2286 of them have a recent master baseline.
| Translation unit | Master | PR | Δ vs median |
|---|---|---|---|
src/Processors/QueryPlan/Optimizations/joinOrderDPSub.cpp |
5.2 s | 14.8 s | +9.3 s (+170%) |
src/Processors/QueryPlan/Optimizations/optimizeJoin.cpp |
17.0 s | 25.2 s | +7.5 s (+42%) |
src/Processors/QueryPlan/Optimizations/Cascades/GroupExpression.cpp |
3.0 s | 10.0 s | +6.9 s (+222%) |
src/Processors/QueryPlan/Optimizations/Cascades/Group.cpp |
3.3 s | 10.3 s | +6.9 s (+201%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/TopNImplementation.cpp |
3.9 s | 10.8 s | +6.6 s (+162%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/ReplicatedSubplanImplementation.cpp |
4.2 s | 10.9 s | +6.6 s (+151%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/TwoStageTopN.cpp |
4.1 s | 10.8 s | +6.6 s (+154%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/DistributionEnforcer.cpp |
4.3 s | 10.9 s | +6.5 s (+144%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/SortingEnforcer.cpp |
4.0 s | 10.6 s | +6.5 s (+154%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/JoinCommutativity.cpp |
4.3 s | 10.9 s | +6.4 s (+144%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/DistributionPassthrough.cpp |
4.2 s | 10.7 s | +6.4 s (+146%) |
src/Processors/QueryPlan/Optimizations/Cascades/Task.cpp |
4.4 s | 11.0 s | +6.3 s (+138%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rule.cpp |
4.1 s | 10.5 s | +6.3 s (+148%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/WindowImplementation.cpp |
4.3 s | 10.7 s | +6.3 s (+140%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/DistinctImplementation.cpp |
4.1 s | 10.5 s | +6.2 s (+145%) |
src/Processors/QueryPlan/Optimizations/Cascades/Memo.cpp |
3.9 s | 10.3 s | +6.2 s (+150%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/HashJoinImplementation.cpp |
5.2 s | 11.4 s | +6.0 s (+110%) |
src/Processors/QueryPlan/Optimizations/Cascades/Rules/AggregationImplementations.cpp |
6.5 s | 12.1 s | +5.4 s (+80%) |
src/Processors/QueryPlan/Optimizations/RuntimeFilterExchangeWiring.cpp |
6.4 s | 11.8 s | +5.1 s (+76%) |
Translation units without a recent master baseline:
src/Processors/QueryPlan/Optimizations/Cascades/Rules/JoinEnumeration.cpp: 11.4 s
Symbol sizes
| Binary | Symbol | Master | PR | Δ |
|---|---|---|---|---|
programs/clickhouse-keeper |
ProfileEvents::global_counters_storage |
14.31 MiB | 14.38 MiB | +64.00 KiB (+0.44%) |
programs/clickhouse |
ProfileEvents::global_counters_storage |
14.31 MiB | 14.38 MiB | +64.00 KiB (+0.44%) |
…plan The join order optimizer keeps master's rule for an equality without a key NDV from statistics: the join keeps its larger side, as a foreign key meeting a primary key. A relation with neither an estimate nor a row bound counts as one row again, a join without an estimate counts the rows its cost counted, and the greedy algorithm prefers a pair joined on a key. Co-Authored-By: Claude Fable 5.1 <[email protected]>
… cloned subplan keeps its estimates Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ion, take min(name) instead of any(name) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…0 tables in the prototype Co-Authored-By: Claude Opus 5.5 <[email protected]>
…the join enumeration prototype Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the `DPsub` cost over all splits Co-Authored-By: Claude Opus 5.5 <[email protected]>
…`: the `DPsub` winner of every set Co-Authored-By: Claude Opus 5.5 <[email protected]>
…of every set by the `DPsub` cost Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oin budget to `best_splits` Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ration-poc Co-Authored-By: Claude Opus 5.5 <[email protected]>
… join keys Adds `query_plan_join_order_distribution` and `cascades_join_enumeration = 'frontier'`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…reads by storage widths Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… read renames Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ery node Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The `hash_join_layout` tests take their inputs without an estimate from a filter, since the join order optimizer is no longer the only source of estimates. 05406 matches the label of an input without an estimate that shows its bound, 05350 the `HashJoin` name of both layouts, 05260 the join order with the new estimates. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| UInt64 rows_without_statistics = 0; | ||
| /// Every value of the column is on at most one row: the column is the only key of an aggregation. A join on it | ||
| /// matches each row of the other side at most once. Whatever can repeat a row (a join, a union) drops the flag. | ||
| bool unique = false; |
There was a problem hiding this comment.
[Major] Clear grouping-key uniqueness when an ARRAY JOIN repeats rows.
The shared estimator now sets this flag for a single grouping key, but its existing ArrayJoinStep / ExpressionStep expansion branch (RelationStatisticsEstimator.cpp:784–806) copies the input statistics and never clears unique on the columns it repeats. For example, SELECT k FROM (SELECT k, count() FROM t GROUP BY k) ARRAY JOIN range(1000) AS a emits 1000 copies of each k, yet still advertises k.unique = true. The same branch also retains the new input max_rows despite expanding the result.
DPJoinEntry consumes that flag to cap an ALL join by the opposite input's row count (joinOrder.cpp:99–102). With 100 grouped keys and 100 matching probe keys, the result contains 100000 rows but can receive a hard bound of 100. For an imprecise estimate, the new replication guard deliberately uses that false bound: an 8-byte projected key is priced at 800 bytes under a 4096-byte budget instead of 800000 value bytes.
Please invalidate uniqueness and row bounds at every row-expanding carrier in the shared estimator, including the explicit clause and expression forms, and cover an expanded grouped key feeding another join. Cascades already clears these properties for those two forms; fixing only the separate filter-DAG carrier leaves this path open. This is a concrete source trace; I could not run a compiled reproduction in this checkout.
…ion or a window above the join graph Co-Authored-By: Claude Opus 5.5 <[email protected]>
…so does Co-Authored-By: Claude Opus 5.5 <[email protected]>
| if (keys.size() == 1) | ||
| aggregation_stats.column_stats[keys.front()].unique = true; | ||
|
|
||
| auto groups = estimateGroupCount(group_keys, input_stats.estimated_rows, input_stats.max_rows); |
There was a problem hiding this comment.
[Major] Mark the multi-key grouping model as an assumption before using it to authorize replication.
When every grouping key has statistics, estimateGroupStats leaves both reliability flags false, although the default estimateGroupCount model uses only the largest individual NDV. Individual NDVs do not measure the number of key combinations.
For a stored complete 1000 × 1000 grid of two UInt64 columns, with uniq statistics covering both columns and all parts, SELECT DISTINCT a, b has 1,000,000 rows. This path estimates 1000, retains a 1,000,000-row bound, and reports a precise estimate. Both replication guards therefore count 16,000 bytes and accept a 32,000-byte budget instead of consulting the 16,000,000-byte bound. GROUP BY a, b has the same issue. This differs from the missing-key case: all per-column statistics are present, and the unmarked assumption is their correlation. The analogous multi-key join assumption is explicitly tracked by joinRowsRestOnAssumption.
Please propagate an imprecision/default flag for modeled multi-key group counts unless joint statistics or a proven bound justify the smaller result, including the Cascades DISTINCT path. Add a focused byte-budget regression with independent keys whose individual NDVs are fully measured. This is a source trace; I could not run a compiled reproduction in this checkout.
…et import keeps only needed columns Co-Authored-By: Claude Opus 5.5 <[email protected]>
…plan Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| stats.imprecise_estimate = relation_stats.imprecise_estimate; | ||
| stats.estimate_from_defaults = relation_stats.estimate_from_defaults; | ||
| stats.source = relation_stats.source; |
There was a problem hiding this comment.
[Major] Preserve unknown cardinality in the data-lake adapter.
estimateReadRowsCount deliberately returns estimated_rows = nullopt with a row bound when an Iceberg predicate has neither column statistics nor file pruning. This adapter puts that bound in estimated_row_count but leaves rows_unknown at its default false.
For example, an unpartitioned Iceberg table with 1000 rows in Avro files, without column statistics, filtered by WHERE k = 7 reaches the shared estimator with only max_rows = 1000. Cascades instead publishes a known 1000-row estimate through buildBestPlan, and downstream joins consume it through knownRows. Thus the planners disagree, and EXPLAIN estimates = 1 / the processor log report a bound as an estimate, violating the new “unknown is unknown” contract.
Please initialize the adapter through applyRelationStats before filling widths and scan size, so all cardinality/provenance fields travel together. Add a focused estimate regression for a filtered Iceberg read without column statistics, with make_distributed_plan = 1 and enable_cascades_optimizer = 1. This is source-traced, not an executed reproduction.
…plan Co-Authored-By: Claude Opus 5.5 <[email protected]>
…plan Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n_deserialization` forges Co-Authored-By: Claude Opus 5.5 <[email protected]>
…roadcast join Co-Authored-By: Claude Opus 5.5 <[email protected]>
ClickHouse chooses join orders, broadcast-or-shuffle and aggregation strategies from row count estimates. Three components make these estimates (the join order optimizer, the rule-based distributed planner and the Cascades optimizer), and until now each did it its own way. Worse, when no estimate was available, each silently invented one: one row here, zero rows or a million there. A guessed "one row" makes a join look free, so the optimizer joins it first and broadcasts it, and nothing in the system showed that the number was a guess.
This PR makes estimation one shared thing and makes it honest:
EXPLAIN estimates = 1shows the estimate and where it came from for every planner.system.processors_profile_logrecords, for every processor, the estimate of the step that created it next to the rows the step actually produced. Estimation errors can finally be measured on real queries.distributed_plan_max_bytes_to_broadcast(default 1 GB) caps the size of such a copy. An estimate that rests on guessed selectivities does not authorize a broadcast on its own; the plan has to prove with the row bound that the copy fits.The result is more predictable plans (no more decisions that hinge on an invented number), consistency between the local and the distributed planners, a way to measure how good the estimates are, and protection against the costliest mistake a wrong estimate can cause.
Several plans in the TPC-H reference test change; each change traces to an estimate that is now computed rather than assumed.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Query planners share one set of row estimation formulas, and a missing estimate is treated as unknown instead of being replaced by a guessed row count.
EXPLAIN estimates = 1shows the estimates of every planner, andsystem.processors_profile_logrecords the estimate of each plan step next to its actual row count (plan_step_estimated_rows,plan_step_estimate_source). New settingdistributed_plan_max_bytes_to_broadcastcaps the amount of data a distributed query plan copies to every node.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124320]