Repository navigation
Explain analyze join stats - #110892
Explain analyze join stats#110892
Conversation
…to explain-analyze-parallelism
…rows - match rows statistics
…m-global) The per-processor work-intervals feature (collect_work_intervals / WorkInterval) is still in development and independent of the Join statistics changes on this branch. It now lives on the explain-analyze-parallelism-global branch. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…o explain-analyze-join-stats
|
Workflow [PR], commit [3cf26b7] Summary: ✅
AI ReviewSummaryThis PR extends PR Metadata
Findings❌ Blockers
Final VerdictChanges requested. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 1035/1134 (91.27%) · Uncovered code |
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
| if constexpr (isLeftOrFull(kind)) | ||
| { | ||
| size_t lnum = nextDistinct(left_cursor); | ||
| size_t lnum = consumeEqualRange(left_cursor, any_join_state.matched.left, matched_rows.left); |
There was a problem hiding this comment.
In consumeEqualRange we compare cursor = left_cursor against matched_range = JoinKeyRow(left_cursor, lpos), won't it always be the same row? Can't we just decide whether to increment the matched count or not based on cmp?
There was a problem hiding this comment.
I agree that in principal this is redundant. I'll remove it.
| added_columns.applyLazyDefaults(); | ||
| } | ||
| } | ||
| if constexpr (leftMatchedSource(KIND, STRICTNESS) == LeftMatchedSource::DefaultRowMarkers) |
There was a problem hiding this comment.
leftMatchedSource reconstructs the matched-left count four different ways after the fact. But the probe loop already knows the answer per left row: right_row_found in both joinRightColumns variants, and any_matched in joinRightColumnsWithAdditionalFilter — where you do count it directly. Couldn't we just accumulate matched_left there for every kind and drop the enum? The only case where key found != matched is ASOF, where findAsof can come back empty, and processMatch could return that.
Besides being uniform, it would let DefaultRowMarkers go, and with it the forced record_row_refs.
The price is one add per left row in the probe loop, always on. I'd expect it to be lost in the noise next to the hash lookup.
Was this considered and rejected for a reason I'm missing?
There was a problem hiding this comment.
right_row_found in both joinRightColumns variants, and any_matched in joinRightColumnsWithAdditionalFilter — where you do count it directly
This would require us to do intervention into the probe loop of joinRightColumns. The goal was not to add anything into the probe loop, in order for EXPLAIN ANALYZE matches=1 making the same cost in the hot loop as it was without it.
The price is one add per left row in the probe loop, always on. I'd expect it to be lost in the noise next to the hash lookup.
This was my initial attempt, but we decided not to do it -- to keep it completely pure. That is why I have built this machinery.
| IColumn::Offsets offsets; | ||
| Chunk joined_chunk = storage->getByKeys({key_col}, attribute_names, null_map, offsets); | ||
|
|
||
| if (table_join->collectAnalyzeStats()) |
There was a problem hiding this comment.
For future work: it would be worth adding information about how many rows were scanned during direct joins. For example, when joining with MergeTree, it can use an index or perform full scans. See
, similar to howProfileEvents::JoinBuildTableRowCount is calculated.
| - `compact` — see [EXPLAIN PLAN](#explain-plan) section. Default: 1. | ||
| - `pretty` — see [EXPLAIN PLAN](#explain-plan) section. Default: 1. | ||
| - `processors` — For `EXPLAIN ANALYZE`, prints an additional line per stage with the per-processor elapsed time distribution: `min`, `median`, `max`, and `sum`. Useful to spot load skew across parallel processors. Default: 0. | ||
| - `matches` — For `EXPLAIN ANALYZE`, makes join steps do the extra bookkeeping needed for the `matched`, `match rate` and `fanout` metrics in the cases where those numbers cannot be derived from what the join produces anyway. Where they can, they are reported without this option. See [Join steps](#explain-analyze-join-steps). Default: 0. |
There was a problem hiding this comment.
nit: matches is a bit cryptic on its own - matches of what, and printed or computed? Something like exact_matches would read closer to what it does, since a plain EXPLAIN ANALYZE already reports matches where it can. Feel free to ignore.
There was a problem hiding this comment.
I thought about making it exact_matches, but I don't like making names for settings for EXPLAIN long. Because people have to write the full name, and there is not auto helper for that. I think short name might be less annoying.
| constexpr bool collectsAnalyzeStats(JoinAnalyzeMode mode) { return mode != JoinAnalyzeMode::None; } | ||
| constexpr bool collectsExactMatches(JoinAnalyzeMode mode) { return mode == JoinAnalyzeMode::Exact; } |
There was a problem hiding this comment.
I'd compare mode directly rather than adding functions
|
Ci Failed: AST fuzzer (amd_debug, targeted, old_compatibility) -- Fix #113589 |
|
CI failed Stateless tests (amd_msan, WasmEdge, parallel, 2/3) -- Fix #114694 |
…s unit test `gtest_merge_join_starved_totals.cpp` was written on 2026-08-09 against the three argument `TableJoin(const Settings &, VolumePtr, TemporaryDataOnDiskScopePtr)` constructor. Commit b1a380d (PR ClickHouse#110892, merged 2026-08-14) inserted a `JoinAnalyzeMode analyze_mode_` parameter in the second position, so the call no longer resolves. The merge queue built the pull request head, which still had the old base, while MasterCI builds the post merge tree, where the signature has already changed. The test file therefore only broke after the merge, and it broke `unit_tests_dbms` in every configuration that enables tests, which stops `ninja clickhouse-bundle` before the binary is produced. `JoinAnalyzeMode::None` is what the test used to get: before the parameter existed, `analyze_mode` was left at its in class default of `None`. It also keeps the assertion meaningful. The only behavioural reader of the mode on this path is `MergeJoin::runPostBuildPhase`, whose condition is `is_right || is_full || (is_all_join && collectExactMatches())`, and the test builds a `JoinKind::Full` join, so `is_full` already selects the branch and the mode cannot change the outcome. Verified locally: the failing translation unit reproduces the CI error before the change, `unit_tests_dbms` and `clickhouse` both link after it, and `MergeJoinStarvedTotals.FullJoinKeepsRightRows` passes. To confirm the test is still a live oracle rather than merely compiling, `hasPostBuildPhase` was flipped to `false`, which reproduced the original wrong results ({(0,0),(1,0),(2,0)} instead of {(0,0),(0,3),(1,1),(2,0)}).
Related: #110668
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add information of internal state of joins to
EXPLAIN ANALYZE