Skip to content

Add PREWHERE granule/row ProfileEvents - #130645

Merged
leshikus merged 7 commits into
ClickHouse:masterfrom
olegkkruglov:issue-42045-prewhere-profile-events
Nov 12, 2026
Merged

leshikus merged 7 commits into
ClickHouse:masterfrom
olegkkruglov:issue-42045-prewhere-profile-events

Conversation

@olegkkruglov

@olegkkruglov olegkkruglov commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Add ProfileEvents to estimate the effect of PREWHERE, as requested in #42045:

  • GranulesSkippedByPrewhereReaders — granules fully rejected by PREWHERE (every row filtered out).
  • GranulesReadByPrewhereReaders — granules on which PREWHERE was evaluated.
  • RowsSkippedByPrewhereReaders — rows rejected by PREWHERE.

These complement the existing RowsReadByPrewhereReaders. They are logical filter counts: a rejected row or granule is not necessarily avoided by the main reader, since a fully-rejected granule is only physically skipped when its rejected rows exceed the shrink threshold. The granule counters are computed per part from the final, all-steps-applied read result, so a granule split across read blocks is counted once and counted as rejected only when all of it was filtered out — exact and independent of max_block_size.

These counters measure work actually performed at read time, so like read_rows they are non-deterministic under parallel replicas with early termination: a LIMIT query is cancelled once the initiator has enough rows, and how many granules each replica read before the cancellation reached it is a timing race. The regression test 05570_prewhere_granule_events_limit_and_skip_index is therefore scoped to a single-server read plan (no-parallel-replicas); the counters remain correct and are deterministic for full scans.

Closes: #42045

Changelog category (leave one):

  • New Feature

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

Added ProfileEvents to estimate the effect of PREWHERE: GranulesReadByPrewhereReaders, GranulesSkippedByPrewhereReaders, and RowsSkippedByPrewhereReaders.


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

Version info

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

Add ProfileEvents to estimate the effect of PREWHERE:

- `GranulesSkippedByPrewhereReaders`: granules fully dropped by PREWHERE
  (every row filtered out, so the main reader read nothing from them).
- `GranulesReadByPrewhereReaders`: granules on which PREWHERE was evaluated.
- `RowsSkippedByPrewhereReaders`: rows filtered out by PREWHERE.

Together with the existing `RowsReadByPrewhereReaders` these are the four
counters requested in the issue.

The granule counters are computed per part in `MergeTreeReadersChain` from the
final, all-steps-applied result rather than per read block: a granule can be
split across read blocks (`max_block_size` is not a multiple of
`index_granularity`) and a later PREWHERE step can drop a granule that an
earlier one kept. Pieces of a split granule are recombined by mark, so each
granule is counted once and counted as dropped only when all of it was
filtered out. This makes the counts exact and independent of `max_block_size`.

Closes: ClickHouse#42045
@cla-assistant

cla-assistant Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@leshikus
leshikus self-requested a review November 10, 2026 23:07
@leshikus leshikus added the can be tested Allows running workflows for external contributors label Nov 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8e475cb]

Summary: ✅


AI Review

Summary

Adds three ProfileEvents measuring logical row and granule rejection by query PREWHERE, with accounting across read blocks and exclusion of earlier filtering. Reviewed head 8e475cb24619a827063019836ec1908607e9938b; no unresolved findings remain. The previous completion and attribution issues are fixed. All three new stateless tests pass in the Fast test report, and 12 focused probes against that binary matched their expected counters.

Final Verdict

✅ Approve.

@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-feature Pull request with new product feature labels Nov 10, 2026

@leshikus leshikus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Multi-condition PREWHERE is the case that flaked in #81133, so maybe it worth adding such test case (or adding a note that this is not supposed to run for multi-condition case)

`GranulesSkippedByPrewhereReaders` undercounted a fully dropped granule that
was split across read blocks (CI `Fast test` reported 609 instead of 610 on
`05569_prewhere_dropped_granules_profile_events`).

`rows_per_granule` is the post-PREWHERE survivor count only for complete
granules; for an incomplete trailing granule its filtering is deferred, so it
still holds the raw rows read. The accounting read that as "survived" and lost
one drop.

Take the per-granule survivor count from `final_filter` instead, via the new
`ReadResult::survivingRowsPerGranule` (popcount of each granule's segment,
handling dense and sparse filters; falls back to `rows_per_granule` only when no
filter is pending). This is exact regardless of block/thread splitting and for
multi-condition PREWHERE.

The test now pins `max_threads` so the dropped granule deterministically lands on
a range boundary, and adds a multi-condition PREWHERE case.
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8e475cb24 with master 732256ab1 (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.62, programs/clickhouse-keeper ×0.69 (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.3 s 13.1 s +9.2 s (+235%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.3 s 7.1 s +5.1 s (+250%)
Compile time of recompiled translation units

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

Job report

…uning

Address two miscounts found by the CI AI review:

- On early termination (e.g. LIMIT) the granule counts were published only
  when the read task ran to the end, so they stayed at zero. Count a granule
  as read as soon as PREWHERE touches it, and count it dropped only once it is
  known to be read in full; a granule cut off by early termination is counted
  read but not dropped.
- Marks pruned by a skip index or the query condition cache before PREWHERE
  reads them (zero-row placeholders from `canSkipMark`) were attributed to
  PREWHERE. Track the rows actually read per granule (`raw_rows_per_granule`)
  and skip granules PREWHERE never evaluated.

Add `05570_prewhere_granule_events_limit_and_skip_index` covering both paths.
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Nov 11, 2026
Comment thread src/Common/ProfileEvents.cpp Resolved
`RowsSkippedByPrewhereReaders` and `GranulesSkippedByPrewhereReaders` count rows
and granules rejected by PREWHERE, but a rejected granule is only physically
skipped by the main reader when its rejected rows exceed the shrink threshold in
`ReadResult::optimize`; otherwise the main reader still reads them. Reword the
descriptions to describe logical filter rejection and drop the claims that the
main reader read nothing from a rejected granule or did not receive the rejected
rows.
The test asserts exact per-reader PREWHERE granule counts for a
single-server read plan. Under parallel replicas the LIMIT early-
termination case is non-deterministic: how many granules each replica
reads before the coordinator's stop signal reaches it is a timing race,
so the count varies run to run. Add the no-parallel-replicas tag.
…nation

- Finalize a dropped granule once the reader's cursor proves it fully read,
  not only at task end, so an early LIMIT still counts it.
- Mark the real query-PREWHERE step (is_query_prewhere) and count only it;
  measure granules from survivors entering PREWHERE, excluding on-fly
  mutations, lightweight deletes and row policies.

Extend 05570 and add 05571.

@leshikus leshikus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good

The completed-granule check used numReadRowsInCurrentGranule() == 0, which
left a complete, fully-rejected granule pending when the cursor stopped
partway through a later granule emptied by an earlier filter. Finalize once
the cursor advances past the pending mark. Cover it with a drop_behind case
in 05570.
@leshikus
leshikus added this pull request to the merge queue Nov 12, 2026
Merged via the queue into ClickHouse:master with commit e48df81 Nov 12, 2026
203 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 12, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@olegkkruglov @leshikus — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 23, 2026
thevar1able pushed a commit to actueleai/ClickHouse that referenced this pull request Nov 24, 2026
… runtime filter in the query condition cache"

This reverts commit 64da8a0, reversing
changes made to be89302.

Conflicts resolved by keeping the later changes in the same lines:
`is_query_prewhere` (ClickHouse#130645), the `last_reader_evaluating_defaults`
argument (ClickHouse#128452), `readsSameColumns` (ClickHouse#124880), `follows_top_k_filter`
(ClickHouse#129882), and the PREWHERE query condition cache write of ClickHouse#115522 in
its own form before it merged ClickHouse#122380.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. external Author is not a member of the ClickHouse organization pr-feature Pull request with new product feature pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

6 participants