Repository navigation
Conversation
…ordering joins optimizeJoin has no row estimate for a relation read from object storage (RelationStatisticsEstimator.cpp returns an empty RelationStats for ReadFromObjectStorageStep) or for a MergeTree relation filtered on a non-index column. computeJoinCost charged such a relation one row, via estimated_rows.value_or(1), in joinOrderCommon.h for greedy/dpsize/dphyp and in EnumeratorCheckerWithCosts::computeJoinCost for dpsub. Since a42ceae (PR ClickHouse#120176) an equi join without reliable NDV keeps the larger side, so a join of two unestimated relations costs max(1, 1) = 1 while any join touching a relation with a real estimate costs that estimate. The solver therefore joins every relation it knows nothing about first and defers the ones it measured. That is inverted: an estimate is most often available exactly for the small, filtered relations, so the plan builds the big unfiltered relation into a huge intermediate and applies the selective filter last. Before ClickHouse#120176 the fabricated 1 was latent. The old model derived NDV from the relation row count, so known(N) x unknown(1) also cost (1/N) * 1 * N = 1, every candidate tied, the greedy solver fell back to enumeration order, and the plan was the good one. Cost an absent cardinality as the largest estimated relation of the same query graph instead. With no relation estimated the surrogate is 1 and the model is byte-identical to before; with every relation estimated it is never consulted; only the mixed case changes, which is the defective one. The alternative of gating the cached hash-table row counts on a fully estimated graph was rejected: it guards the symptom, and the same inversion is reachable with no cache at all, e.g. a MergeTree table filtered on a non-index column joined to an Iceberg one. effectiveSelectivity keeps value_or(1) deliberately. It exists to be comparable with the cartesian selectivity EXPLAIN ANALYZE reports, so with unknown sides the honest value is the neutral 1.0; the surrogate would print a fabricated 1/surrogate there. This reddened Performance Comparison (arm_release, master_head, 5/6) on master and on ~48% of PR runs: iceberg_suite_tpch aborts when TPC-H Q5 crosses max_execution_time = 15, discarding the shard's whole perf verdict. Measured on the CI Iceberg SF10 dataset, two executions of Q5 in one server process (the harness's prewarm then measure), debug build, both servers capped at 32G: before: 3448 ms / 3.61 GiB, then 37173 ms / 24.42 GiB (hit the memory limit) after: 3618 ms / 3.62 GiB, then 3122 ms / 4.00 GiB, identical results The optimizer's logged plan cost for the second execution goes from 2275923 (= 2275919 + 4x1) to 11379595 (= 5 x 2275919), i.e. every candidate now ties at the surrogate and the second execution reproduces the first one's plan. The first execution's cost is unchanged at 5. The new test asserts the join order for greedy, dpsub and dphyp on a graph with three estimated relations and one unestimated one; it fails without this change on all three. No .reference in the join-order family changes, and across all 1131 stateless *join* tests the new test is the only outcome difference. Co-Authored-By: Claude Opus 5 <[email protected]>
Removed the rowsForJoinCost block (restated value_or below it; rationale is in the commit message) and reduced the unknown_relation_rows field comment to a one-line invariant.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings) plus my own cold
Severity: ❌ blocker / Two notes from the investigation that are not changes here, in case they are useful:
Session id: cron:clickhouse-review-slot-8:20260918-082800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20260918-071100 |
|
Workflow [PR], commit [5fab300] Summary: ❌
AI ReviewSummaryThis PR changes join-order costing so relations without Final Verdict✅ No new findings. LLVM Coverage ReportMeasured on commit 5fab300.
Changed lines: Changed C/C++ lines covered: 45/46 (97.83%) · Uncovered code |
Build profile diff (arm_release)Comparing commit See the job log for details. |
`EnumeratorCheckerWithCosts::accept` resolves the join's kind and strictness before costing the pair, but `computeJoinCost` ignored both and always used `estimateJoinedRows`, which returns `max(lhs, rhs)` for a non-reliable equi join. Charging an unestimated relation `unknown_relation_rows` therefore priced a semi/anti join over it as expanding to that surrogate, although such a join only filters its preserved side and its recorded cardinality is clamped accordingly. With `query_plan_optimize_join_order_algorithm = 'dpsub'` and a conflict detector (the only configuration that reorders semi/anti joins instead of swapping them), a selective `SEMI`/`ANTI` filter over an unestimated relation was deferred behind the larger join. Measured on `k_a` (10 rows) semi-joined to an unestimated relation and inner-joined to `k_big` (1e6 rows, 100k per key): the plan changed from filter-first to filter-last and peak RSS rose from 425-566 MB to 717-757 MB over three runs each, because the 1e6-row intermediate is materialised before the filter. Results are unchanged. The cost increment is now capped at the preserved side's row count when a side's row count was substituted, which is an unconditional upper bound on what a semi/anti join emits. Two narrower variants were measured and rejected: taking the increment from `estimateJoinCardinality` prices every anti join at 1 row without column statistics (1 instead of 1000 on a disjoint-key fixture), and capping unconditionally also reorders fully estimated graphs, where it moves a non-selective anti join ahead of a selective inner join. Costing a relation the optimizer cannot estimate is what this change is about, so graphs whose relations are all estimated keep the plans and costs they have today. Only DPsub under CD-A/CD-C is affected: `evaluateJoin` always passes `JoinStrictness::All`, and the greedy solver never receives a semi/anti graph because `conflictDetectorReordersSemiAnti` requires DPsub as the sole algorithm. `rowsForJoinCost` returns `UInt64` so a row count never round-trips through `double` (`estimated_rows` can be `UInt64::max()`, whose round-trip is undefined). Reported by the automated reviewer on the pull request.
|
Independent confirmation on a different workload: this also recovers a shipped TPC-DS query on MergeTree, not only TPC-H Q5 on the CI Iceberg fixture. Setup: TPC-DS adapted SF10 on MergeTree ( Shipped
Answers byte-identical in every run. A 26.9.1.1 binary on the same datadir feeds about 11.0M rows into the aggregation, so the PR restores the pre-regression pruning level here. That binary still runs One thing worth knowing given the link to #120176: on this query the regression has a second, independent trigger. Because an equi join with one unknown side now reports the known side, |
|
Reproduced both halves locally on MergeTree, with the shipped Fixture:
The solver log names your second trigger outright: on master the first pair taken is So -28.8% from relaxing the gate on master alone, next to your -30.7%, and no further win once the join order is fixed (76 -> 82 ms): not additive, agreed. I am not extending this PR to that gate. Whether a guessed estimate should feed |
CI finish ledger - 5fab300Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260918-220128 |
|
Resolved by #120923 |
|
Verified and agreed, nothing here to re-target, so I am leaving this closed. What I checked on current master: One note for whenever the heuristic re-lands, since it is a second consumer of that estimate rather than the cost model: the runtime-filter effect I measured earlier in this thread (#120762 (comment)) fires only when |
Related: #120176
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Not needed: the regression is master-only and unreleased.
Description
Performance Comparison (arm_release, master_head, 5/6)has been red on master and ~48% of PR runs since #120176 merged:iceberg_suite_tpchaborts when TPC-H Q5 crossesmax_execution_time = 15. It is user-visible on default settings too: a second execution was 11x slower at 6.8x the memory.Root cause:
computeJoinCostcharged a relation with no row estimate one row (estimated_rows.value_or(1)). Since #120176 an equi join without reliable NDV keeps the larger side, so a pair of unestimated relations costsmax(1, 1) = 1while any pair touching a measured relation costs that estimate. The solver therefore joins the relations it knows nothing about first, although estimates are usually available for the small, filtered relations.Change: cost an absent cardinality as the largest estimated relation of the same query graph. With nothing estimated the surrogate is 1 and behaviour is unchanged; with everything estimated it is never consulted.
Review follow-up: DPsub's cost path ignored the join's resolved strictness, so the surrogate priced a
SEMI/ANTIjoin over an unestimated relation as expanding to it and deferred a selective filter behind the larger join (peak RSS 425-566 MB -> 717-757 MB, same results). It is now capped at its preserved side's rows when a row count was substituted; fully estimated graphs keep today's plans.Validation, TPC-H Q5 over the CI Iceberg SF10 dataset, second execution: 37173 ms / 24.42 GiB peak (aborted on the memory limit) -> 3122 ms / 4.00 GiB, identical results. On MergeTree the shipped
tests/benchmarks/tpc-dsquery_59goes 437 ms -> 76 ms, 5.65M -> 412K rows into the aggregation, identical answers. The test pins the join order forgreedy,dpsub,dphypand for semi/anti under a conflict detector, and fails without the change; it is the only outcome change across 1131 stateless*join*tests.CI provenance (true master,
07f2c4013ce9): report.Workflow [PR]
Sync PR [sync-upstream/pr/120762]