Skip to content

Support for granule pruning by (_part_starting_offset + _part_offset) in join runtime filter - #129677

Merged
shankar-iyer merged 9 commits into
ClickHouse:masterfrom
shankar-iyer:runtime_filter_part_offset_pruning
Nov 10, 2026
Merged

shankar-iyer merged 9 commits into
ClickHouse:masterfrom
shankar-iyer:runtime_filter_part_offset_pruning

Conversation

@shankar-iyer

@shankar-iyer shankar-iyer commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

A JOIN runtime filter on the global row index : _part_starting_offset + _part_offset now prunes granules of the probe side (enable_join_runtime_filters_index_analysis). A self-join that fetches the full rows found by a subquery, for example the best row per group from argMax(_part_starting_offset + _part_offset, ...), or rows picked by LIMIT BY or a window function, reads only the granules of those rows instead of the whole table.

The key is recognized in either operand order, and it is not used when a physical column shadows _part_offset or _part_starting_offset, or for projection parts. Exact pruning is limited to join_runtime_filter_exact_values_limit keys; above that, the filter falls back to the range of the keys. A key under a CAST that can change values is not used for pruning.

Changelog category :

  • Performance Improvement

Changelog entry

JOIN runtime filters now skip granules when the join key is the global row index _part_starting_offset + _part_offset, so a self-join that fetches the rows found by a subquery reads only the granules of those rows


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

Version info

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

shankar-iyer and others added 2 commits November 6, 2026 04:12
A JOIN runtime filter on `_part_starting_offset + _part_offset` now prunes
granules of the probe side, so a self-join that fetches the rows found by a
subquery reads only the granules of those rows.

Also, a join key under a cast that can change values (e.g. narrowing) is no
longer used for pruning: it dropped matching rows, also for primary key columns.

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

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [937804e]

Summary: ✅


AI Review

Summary

Reviewed PR head 937804e4a24fbe79e5cbdbfa2183e0684fc77026. The change adds granule pruning for JOIN runtime filters on _part_starting_offset + _part_offset, using exact keys or their range. No new findings. The earlier bot findings are addressed in the current code.

Consumers of the new descriptor form were traced through:

  • collectAppliedJoinRuntimeFilters, registration, cloning, parallel-replica read replacement, and normal-projection replacement: preserve and revalidate is_global_row_index.
  • ReadFromMergeTree, buildRuntimeRangePredicate, and MergeTreeSkipIndexReader: separate global-row predicates from physical-key predicates and exclude projection parts from global-row pruning.
  • markSealGatedReading and RuntimeFilterReadRangesRefiner: only physical primary-key descriptors reach seal-gated refinement.
  • MergeTreeIndexBuildContext and MergeTreeIndexReadResultPool: preserve part starting offsets and defer runtime-dependent index evaluation until reader initialization.

Both added stateless tests, 05493 and 05495, pass in the PR-head Fast test report, including assertions for granule reduction. Eleven additional local comparisons using that head's binary returned identical results with pruning disabled and enabled, covering mixed primary-key/global-row filters, multiple global-row descriptors, casts, aliases, an empty primary key, a shadowed _part_starting_offset, and a projection storing parent row offsets. EXPLAIN confirmed the projection was used.

Final Verdict

Status: ✅ Approve. No new inline comments or thread changes are needed.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... labels Nov 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 937804e4a with master f4435fcbb (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.

Object file sizes

13 object files changed (+60.63 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeIndexReadResultPool.cpp.o 264.95 KiB 291.68 KiB +26.73 KiB (+10.09%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/ReadFromMergeTree.cpp.o 2.53 MiB 2.55 MiB +16.80 KiB (+0.65%)

719 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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.92, programs/clickhouse-keeper ×1.06 (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&) 7.5 s 16.1 s +9.1 s (+132%)
Compile time of recompiled translation units

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

Job report

…test

`canBeSafelyCast` rejects an unsigned integer widened into a larger signed
one (e.g. `UInt32` to `Int64`), which keeps every value and is the common
type of an unsigned and a signed join key, so such joins lost their pruning.
Accept it in the check for value-changing casts.

`05493` no longer checks exact granule counts: a part can be analyzed more
than once by a query, which adds its granules to the counters again.

Related: ClickHouse#129677

Co-Authored-By: Claude Opus 5.5 <[email protected]>
shankar-iyer and others added 2 commits November 6, 2026 09:25
…structor

Accept `Date` to `Date32` and `Date`, `DateTime` (and `Date32` up to scale 8)
to `DateTime64` in the check for value-changing casts: they keep every value
and are the common type of mixed date join keys, which lost their pruning.

Set the global row index pruning of `MergeTreeSkipIndexReader` with a setter
instead of a constructor argument, so the constructor stays unchanged for its
other callers.

Related: ClickHouse#129677
Related: ClickHouse/clickhouse-private#85905

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reader recognizes the `_part_offset + _part_starting_offset` node of the
pruning predicate by its name, so a column with that name (which
`CREATE TABLE ... AS SELECT` creates without an alias) was taken for it by the
primary key and skip indexes, and matching rows were dropped. Such a table is
no longer pruned by the global row index. The name is now one constant,
checked where the node is built.

Related: ClickHouse#129677

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@shankar-iyer shankar-iyer changed the title Support for pruning by (_part_starting_offset + _part_offset) in join runtime filter Support for granule pruning by (_part_starting_offset + _part_offset) in join runtime filter Nov 6, 2026
@shankar-iyer

Copy link
Copy Markdown
Member Author

This relies on the table's snapshot taken at start of the query is used in all sub-queries reading from that table (enable_shared_storage_snapshot_in_query = 1)

@diegomestre2 diegomestre2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Really cool feature @shankar-iyer , I left a few comments to clarify and suggest some changes. Could you add one query with enable_parallel_replicas = 1 that checks only that the result is correct? That would make sure it also works there. Let me know when it is ready for a second round.

The primary key and skip indexes get a predicate on their own keys only, and
the global row index a predicate on it only. The conditions recognize their keys
by name, so with one predicate a column named like the global row index
expression, also cast to the join key type, was taken for it and matching rows
were dropped. This replaces the guard on the column name.

The key expression of the global row index is built once per read instead of
per part, and the check of its name is no longer debug-only. Add a test with
parallel replicas and tests for cast-wrapped column names.

Related: ClickHouse#129677

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

Copy link
Copy Markdown
Member Author

Added the parallel replicas check in 8e3e5a8: two queries in 05493 with enable_parallel_replicas = 1, with and without the initiator's local plan, that check the result only. Both return the same rows as the local read. Each replica numbers parts from its own snapshot, so this covers replicas reading the same table, as in the local test cluster.

The other comments are addressed in the threads. Ready for another look.

shankar-iyer and others added 2 commits November 9, 2026 15:06
…ed reading

`markSealGatedReading` matched descriptors to primary key columns by
`key_column_name`, which is only a display name for the global row index. A
primary key column with that name let the seal-gated reader prune by the global
row index expression against the physical key, dropping matching rows.

Related: ClickHouse#129677

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

Copy link
Copy Markdown
Member Author

Fixing conflicts after #128128 got merged.

…_offset_pruning

# Conflicts:
#	src/Processors/QueryPlan/Optimizations/actionsDAGUtils.cpp
@shankar-iyer

Copy link
Copy Markdown
Member Author

Clean CI run at commit 937804e

@diegomestre2 diegomestre2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great work! No more remarks form my side! It was a fun review!

@shankar-iyer

Copy link
Copy Markdown
Member Author

Great work! No more remarks form my side! It was a fun review!

Thanks, appreciate it!

@shankar-iyer
shankar-iyer added this pull request to the merge queue Nov 10, 2026
Merged via the queue into ClickHouse:master with commit 8d62282 Nov 10, 2026
188 checks passed
@shankar-iyer
shankar-iyer deleted the runtime_filter_part_offset_pruning branch November 10, 2026 14:39
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 10, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 10, 2026
…e-filter-file-pruning

Resolve the conflict in `collectAppliedJoinRuntimeFilters` with ClickHouse#129677,
which added `is_global_row_index` to the MergeTree registration: MergeTree
gets the new argument, object storage reads skip global row index
descriptors (`_part_starting_offset + _part_offset` is a MergeTree key,
not a column of the data files).

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

comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-performance Pull request with some performance 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.

3 participants