Skip to content

Print the q-error of each join side's estimated rows in EXPLAIN ANALYZE - #131313

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:explain-analyze-join-side-q-error
Nov 14, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:explain-analyze-join-side-q-error

Conversation

@groeneai

@groeneai groeneai commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #123553

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

EXPLAIN ANALYZE now prints the q-error of the estimated rows of each join input next to rows estimated and rows; for a filtered table read this is the q-error of its filter estimate.

Description

Asked for on #123553 (#123553 (comment)): filter estimates had no q-error in EXPLAIN ANALYZE. @ m-selmi suggested the same when approving #114792.

EXPLAIN ANALYZE printed a q-error only for a join's Output rows. The Left: and Right: lines already show the join-order optimizer's estimate of each input and its actual rows; they now also show the q-error, computed by the same resultRowsQError (max(estimated / actual, actual / estimated)):

Right: rows estimated 500.00 · rows 20.00 · q-error 25.00 · matched not collected · ...

It is printed on the join inputs because that is where the planner uses a filter estimate: a filtered table read is a leaf of the join graph, estimated by estimateReadRowsCount, and the printed side estimate is exactly the one join reordering used (including the hash-table statistics override). A Filter step outside a join has no estimate that anything uses. As with Output rows, the q-error is omitted when the estimate is missing or either value is 0. Join runtime filters lower the actual rows of the side they prune, which no estimate accounts for; the docs say so and name enable_join_runtime_filters = 0.

The output docs are updated in ParserExplainQuery.cpp. New test 05629_explain_analyze_join_side_q_error fails on master and passes here (50/50 with random settings); all 35 stateless tests using EXPLAIN ANALYZE pass unchanged.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.2273-master (included in 26.11 and later)

groeneai and others added 2 commits November 13, 2026 12:08
`EXPLAIN ANALYZE` printed a q-error only for a join's output rows. The `Left:`
and `Right:` lines already carry the join-order optimizer's estimate of each
input next to its actual rows, but without a q-error, so the worst estimates
(a filtered table read whose filter estimate is far off) had to be computed by
hand.

Each side line now reads `rows estimated E · rows A · q-error Q · matched ...`,
using the same `resultRowsQError` (max(E / A, A / E)) and the same metric as
`Output rows`. It is absent when the estimate is missing or 0 or the side read
no rows.

This is where the planner uses a filter estimate: a filtered table read is a
leaf of the join graph, estimated by `estimateReadRowsCount` in
`optimizeJoin.cpp`, and the side estimate printed here is the one join
reordering used, including the hash-table cache override. Recomputing an
estimate for `Filter` steps at EXPLAIN time would print a number nothing used.

Join runtime filters lower a side's actual rows without any estimation error;
the docs say so and name `enable_join_runtime_filters = 0` to compare the
estimate alone.

Related: ClickHouse#123553

Co-Authored-By: Claude Opus 5.5 <[email protected]>
05629 covered an exact estimate and an overestimate (500 estimated, 20
read), so a one-sided `estimated / actual` ratio would still pass. Add
`WHERE d.s = 'common'` (500 estimated, 980 read, q-error 1.96): the
underestimate direction, which is the direction of the filter errors
reported on ClickHouse#123553.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding over 2 rounds, 0 on the final delta) plus my own review (0 blocker/major).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test did not cover an underestimate (actual > estimated), so a one-sided estimated / actual ratio would pass (05629_explain_analyze_join_side_q_error.sql) AGREE, fixed @ 80d8dde New case WHERE d.s = 'common': 500 estimated, 980 read, q-error 1.96; a one-sided ratio prints 0.51 and fails it
2 💡 With the default use_hash_table_stats_for_join_reordering = 1, a smaller row count cached from an earlier run replaces a side estimate, so its q-error then compares against that count noted, not blocking This is the estimate join reordering used; the test pins the setting to 0
3 💡 Join runtime filters (default on) lower a pruned side's actual rows without any estimation error noted, not blocking Documented with enable_join_runtime_filters = 0; a filter can be pushed into either input subtree, so it cannot be attributed per side

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261113-131400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. On master the join Left:/Right: lines have no q-error; 05629_explain_analyze_join_side_q_error fails its first three checks on a master build every run.
b Root cause explained? #114792 applied resultRowsQError only to the Output rows group; the side groups got rows estimated next to rows but no q-error.
c Fix matches root cause? Yes. prependSideEstimate applies the same function to each side's estimate and actual rows and inserts MetricKey::QError after Rows. No estimate is recomputed.
d Test intent preserved / new tests added? New SQL test (exact, over-estimate, under-estimate, 0-row and no-estimate cases); no existing test or reference changed.
e Both directions demonstrated? Fails on the master binary, on a build with the insertion removed, and on a one-sided E / A q-error (the under-estimate case); passes with the change; 50/50 with random settings.
f Fix is general across code paths? Checked hash, parallel_hash, grace_hash, partial_merge (its extra Right: fields stay after the q-error), full_sorting_merge, LEFT, CROSS, swapped sides and a 3-table reorder. direct and the Join engine have no estimate and are unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Exact (1.00), over-estimate (25.00), under-estimate (1.96), 0 actual rows and no estimate (no q-error).
h Backward compatible? (maintainer-approved exception only) One more field on an EXPLAIN ANALYZE line, no setting; all 35 EXPLAIN ANALYZE stateless tests pass unchanged.
i Invariants and contracts preserved? Side rows are read before any insertion, as before; the order of the other metrics is unchanged; execution, estimates and EXPLAIN PLAN are untouched.

Session id: cron:clickhouse-impl-slot-41:20261113-114300

@clickhouse-gh

clickhouse-gh Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [80d8dde]

Summary: ✅


AI Review

Summary

Adds per-input cardinality q-error to EXPLAIN ANALYZE, using the existing calculation and omission rules, and updates the source documentation. Reviewed the PR diff, current implementation, and all prior discussion at head 80d8dde844a964521a73fcced40cf376459058eb. No blocker or major findings. The focused stateless test covers exact estimates, overestimates, underestimates, zero actual rows, and absent estimates; it passes in the PR's Fast test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 13, 2026
@antoniofilipovic antoniofilipovic self-assigned this Nov 13, 2026
@clickhouse-gh clickhouse-gh Bot added can be tested Allows running workflows for external contributors comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... labels Nov 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 80d8dde84 with master 06ad563d2 (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

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.63, programs/clickhouse-keeper ×0.64 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 6.0 s 11.9 s +8.1 s (+214%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.5 s 7.4 s +5.2 s (+236%)
Compile time of recompiled translation units

8 translation units recompiled, 11 s compile time in total, 8 of them have a recent master baseline.

Job report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 13, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 13, 2026
Merged via the queue into ClickHouse:master with commit 032dcfb Nov 14, 2026
606 of 608 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 14, 2026
@clickhouse-gh clickhouse-gh Bot added comp-explain EXPLAIN and query profiling output: EXPLAIN PLAN / PIPELINE / AST / SYNTAX / ESTIMATE, pretty EXP... and removed comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-explain EXPLAIN and query profiling output: EXPLAIN PLAN / PIPELINE / AST / SYNTAX / ESTIMATE, pretty EXP... pr-improvement Pull request with some product improvements 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.

4 participants