Skip to content

Skip granules on the left side of a JOIN using use_skip_indexes_on_data_read - #109085

Merged
shankar-iyer merged 37 commits into
ClickHouse:masterfrom
shankar-iyer:filtering_with_pk_and_skip_for_join
Jul 20, 2026
Merged

shankar-iyer merged 37 commits into
ClickHouse:masterfrom
shankar-iyer:filtering_with_pk_and_skip_for_join

Conversation

@shankar-iyer

@shankar-iyer shankar-iyer commented Jul 1, 2026 •

Copy link
Copy Markdown
Member

Use the JoinRuntimeFilter to get the join keys (a set of values or just a min-max range) and then leverage the use_skip_indexes_on_data_read infra to run index analysis (primary key index and skip index) to prune granules before reading from table parts of the left hand side.

Changelog category

  • Performance Improvement

Changelog entry

  • JOINs can now use primary key index or skip index on the left hand side table to prune granules

@shankar-iyer

Copy link
Copy Markdown
Member Author

New setting added : enable_join_runtime_filters_index_analysis

@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [215190e]

Summary: ✅


AI Review

Summary

This PR adds the experimental enable_join_runtime_filters_index_analysis path that uses JOIN runtime filters to prune left-side MergeTree granules via the primary key and selected skip indexes. The current head has fixed the earlier high-risk correctness problems around FINAL, ANTI joins, fixed-hash-table replacement, ignored skip indexes, and profile-event accounting, but two support-boundary issues still remain: distributed plans silently drop the optimization, and the new bloom-index path is still unintentionally disabled by small join_runtime_filter_exact_values_limit values.

Findings

⚠️ Majors

  • [src/Processors/QueryPlan/ReadFromMergeTree.h:503-507; src/Processors/QueryPlan/ReadFromMergeTree.cpp:5497-5502] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/109085#discussion_r3533853276] enable_join_runtime_filters_index_analysis is still accepted under make_distributed_plan, but the descriptors that drive the left-side pruning are explicitly not carried through serialize / deserialize. That makes distributed reads silently skip the optimization instead of rejecting an unsupported mode. New settings should either propagate their state end-to-end or fail with SUPPORT_IS_DISABLED; silently turning the feature into a no-op is still a contract gap.
  • [src/Processors/QueryPlan/ReadFromMergeTree.cpp:4141-4152] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/109085#discussion_r3533853281] The bloom-index path still derives its exact-values cutoff as join_runtime_filter_exact_values_limit / 100. Any user value below 100 makes bloom_filter_in_cap zero, which disables bloom skip-index pruning even for a single exact key. That is still an undocumented coupling between two unrelated knobs: lowering the runtime-filter exact-set limit for memory reasons silently turns off this new optimization.
Final Verdict

⚠️ The head looks materially better than the first revision, but the remaining support-boundary gaps above should still be closed before relying on this optimization broadly.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 1, 2026
@shankar-iyer
shankar-iyer requested a review from rschu1ze July 1, 2026 18:49
@shankar-iyer
shankar-iyer marked this pull request as draft July 1, 2026 19:00
@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 37 queries analysed

This PR adds an experimental, default-off JOIN runtime-filter feature (enable_join_runtime_filters_index_analysis) that prunes left-side granules; the only unconditional change is a reordering of SelectedRanges/SelectedMarks metric accounting, which does not affect execution time. All four flagged ClickBench queries are single-table with no JOIN, so none can exercise the new path, and their mixed-direction deltas (Q4 -14.8%, Q15 -20.6% faster; Q9 +10.2%, Q11 +16.1% slower) are run-to-run variance. The deterministic within-envelope suppressions are all consistent with the diff and stand as no_change / not_sure.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 9 not_sure 343 378 +10.2% <0.0001 join_operator: Q9 is a single-table aggregation with no JOIN to exercise the new (off-by-default) pruning path, so +10.2% is run-to-run variance. Keeping it not_sure is right.
⚠️ 11 not_sure 168 195 +16.1% <0.0001 join_operator: Q11 has no JOIN and its history is intrinsically flaky; with the JOIN-only change off by default, +16.1% is variance, not a PR effect.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: b6e4be7b-7886-4dcf-bbb5-f20973495d40
  • MIRAI run: 9320859f-e630-4e2a-b805-c61a27e9e20d
  • PR check IDs:
    • clickbench_389061_1784522748
    • clickbench_389067_1784522748
    • clickbench_389080_1784522748
    • tpch_adapted_1_official_389086_1784522748
    • tpch_adapted_1_official_389095_1784522748
    • tpch_adapted_1_official_389109_1784522748

@shankar-iyer

Copy link
Copy Markdown
Member Author

The comments from the review-bot are valid and easy to fix. I was hoping for some feedback on how to report back the pruning. Showing it in EXPLAIN indexes is not possible. Some kind of ProfileEvent in the query is a option, let me check.

@shankar-iyer

shankar-iyer commented Jul 2, 2026 •

Copy link
Copy Markdown
Member Author

The best case for this PR is a large fact table joined to a filtered dimension, where the filter is on a dimension attribute The set of surviving foreign keys turns out to be small — and if the fact table has skip index on the that key, its granules get pruned (based on distribution ofcourse).

Or if joining 2 large fact tables ordered by the same/similar sort key - depends on the range of IDs gathered by the RHS and the table insertion layout. Representative query -

  SELECT l_orderkey, sum(l_extendedprice*(1-l_discount)) AS revenue, o_orderdate, o_shippriority
  FROM customer, orders, lineitem
  WHERE c_mktsegment = 'BUILDING'
    AND c_custkey = o_custkey
    AND l_orderkey = o_orderkey
    AND o_orderdate < DATE '1995-03-15'
    AND l_shipdate  > DATE '1995-03-15'
  GROUP BY l_orderkey, o_orderdate, o_shippriority
  ORDER BY revenue DESC, o_orderdate LIMIT 10;

Some self joins should also be improved.

@shankar-iyer

Copy link
Copy Markdown
Member Author

Trying a run with default enable of the new setting.

@shankar-iyer

Copy link
Copy Markdown
Member Author

PR in action -

SELECT sum(l.l_extendedprice * (1 - l.l_discount))
FROM lineitem AS l
INNER JOIN orders AS o ON l.l_orderkey = o.o_orderkey
WHERE o.o_totalprice > 500000

Query id: fcd28b3e-05f1-496d-a635-da13b5dc202d

   ┌─sum(multiply⋯discount)))─┐
1. │             7942764.5613 │ -- 7.94 million
   └──────────────────────────┘

1 row in set. Elapsed: 0.012 sec. Processed 7.50 million rows, 37.53 MB (619.79 million rows/s., 3.10 GB/s.)
Peak memory usage: 1.22 MiB.
SELECT sum(l.l_extendedprice * (1 - l.l_discount))
FROM lineitem AS l
INNER JOIN orders AS o ON l.l_orderkey = o.o_orderkey
WHERE o.o_totalprice > 500000
SETTINGS enable_join_runtime_filters_index_analysis = 1

Query id: f310f8f0-ef5a-4169-a5ca-ead8bb086755

   ┌─sum(multiply⋯discount)))─┐
1. │             7942764.5613 │ -- 7.94 million
   └──────────────────────────┘

1 row in set. Elapsed: 0.010 sec. Processed 1.63 million rows, 14.39 MB (171.34 million rows/s., 1.51 GB/s.)
Peak memory usage: 903.78 KiB.

7.50 million rows v/s 1.63 million rows

and pruning confirmed -

2026.07.02 09:40:38.242374 [ 1402556 ] {f310f8f0-ef5a-4169-a5ca-ead8bb086755} <Debug> MergeTreeSkipIndexReader: Dynamic read-time predicate dropped 718/734 granules in part all_1_6_1

@shankar-iyer

Copy link
Copy Markdown
Member Author

@groeneai How to trigger one more run performance run like this one : #109085 (comment)

@shankar-iyer

Copy link
Copy Markdown
Member Author

Clean CI run at 35dbad4 with default enable of the new setting. Performance Comparision warnings are totally unrelated

@groeneai

groeneai commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

@shankar-iyer That report is the Cloud Performance Report (StressHouse + MIRAI) posted by clickhouse-gh[bot], generated as part of this PR's CI run. To get another one, push a commit to the branch and the new SHA's CI produces a fresh report (that is what your 35dbad4 run does). It is internal cloud-benchmark infra tied to the commit's CI, so there is no open-repo slash command or comment trigger for it; a fresh CI run on a new commit is the mechanism.

Separately, for the in-repo Performance Comparison (amd_release/arm_release, ...) checks: they run automatically after the release builds and are digest-cached on tests/performance/, ci/jobs/scripts/perf/, ci/jobs/performance_tests.py, ci/docker/performance-comparison and the release binary. A code change re-runs them; to re-run them without a code change use "Re-run jobs" on the PR's Actions page. Adding the ci-performance label limits the next CI run to perf + required builds for a faster turnaround (remove before merge, it also acts as a merge-guard).

@shankar-iyer

shankar-iyer commented Jul 2, 2026 •

Copy link
Copy Markdown
Member Author

I will re-run TPC-H, but first read looks like not much pruning.

TPC-DS is better - With only primary key ranges filtering (no skip indexes yet), 9 queries read >= 50% lesser rows, 14 queries read >= 10% lesser rows. Q64 reads 94% lesser rows. More queries should benefit after I add skip indexes on some foreign key columns of the fact tables. Complete listing for all queries

================ TPC-DS SF1 (as-is, PK path only): read_rows OFF vs ON ================
    ┌─query─┬─rows_off─┬──rows_on─┬─reduction_pct─┬─on_err─┐
 1. │ Q64   │ 18094064 │  1061824 │            94 │      0 │
 2. │ Q32   │  3047194 │   205058 │            93 │      0 │
 3. │ Q3    │  2971453 │   263760 │            91 │      0 │
 4. │ Q37   │ 13277597 │  1950389 │            85 │      0 │
 5. │ Q22   │ 11836049 │  2442153 │            79 │      0 │
 6. │ Q82   │ 14716453 │  3389245 │            77 │      0 │
 7. │ Q91   │  2295122 │   571609 │            75 │      0 │
 8. │ Q92   │  1602866 │   401666 │            75 │      0 │
 9. │ Q72   │ 15496067 │  6143131 │            60 │      0 │
10. │ Q42   │  2971453 │  1757824 │            41 │      0 │
11. │ Q52   │  2971453 │  1757824 │            41 │      0 │
12. │ Q19   │  3121465 │  1873840 │            40 │      0 │
13. │ Q55   │  2971453 │  1797556 │            40 │      0 │
14. │ Q84   │  2365534 │  1591614 │            33 │      0 │
15. │ Q62   │   792488 │   727631 │             8 │      0 │
16. │ Q83   │  1085071 │  1020214 │             6 │      0 │
17. │ Q66   │  2479880 │  2350166 │             5 │      0 │
18. │ Q17   │  4846625 │  4652054 │             4 │      0 │
19. │ Q25   │  4846625 │  4652054 │             4 │      0 │
20. │ Q29   │  4846625 │  4652054 │             4 │      0 │
21. │ Q40   │  1676669 │  1611812 │             4 │      0 │
22. │ Q50   │  3314028 │  3184314 │             4 │      0 │
23. │ Q57   │  4597809 │  4403238 │             4 │      0 │
24. │ Q94   │  1633803 │  1568900 │             4 │      0 │
25. │ Q99   │  1514628 │  1449771 │             4 │      0 │
26. │ Q54   │  5436687 │  5247699 │             3 │      0 │
27. │ Q8    │  3153465 │  3088608 │             2 │      0 │
28. │ Q16   │  3152561 │  3087704 │             2 │      0 │
29. │ Q26   │  3453697 │  3388840 │             2 │      0 │
30. │ Q34   │  3060665 │  2995808 │             2 │      0 │
31. │ Q36   │  2971465 │  2906608 │             2 │      0 │
32. │ Q43   │  2953465 │  2888608 │             2 │      0 │
33. │ Q46   │  3160665 │  3095808 │             2 │      0 │
34. │ Q47   │  8914395 │  8719824 │             2 │      0 │
35. │ Q58   │  5476965 │  5347251 │             2 │      0 │
36. │ Q61   │  6243230 │  6113516 │             2 │      0 │
37. │ Q67   │  2971465 │  2906608 │             2 │      0 │
38. │ Q68   │  3160665 │  3095808 │             2 │      0 │
39. │ Q70   │  5906930 │  5777216 │             2 │      0 │
40. │ Q73   │  3060665 │  2995808 │             2 │      0 │
41. │ Q77   │  5983118 │  5853404 │             2 │      0 │
42. │ Q79   │  3060665 │  2995808 │             2 │      0 │
43. │ Q80   │  5830487 │  5700773 │             2 │      0 │
44. │ Q7    │  4892553 │  4827696 │             1 │      0 │
45. │ Q10   │  7332543 │  7291583 │             1 │      0 │
46. │ Q21   │ 11836054 │ 11771197 │             1 │      0 │
47. │ Q27   │  4892265 │  4827408 │             1 │      0 │
48. │ Q39   │ 23672108 │ 23542394 │             1 │      0 │
49. │ Q88   │ 23792128 │ 23559680 │             1 │      0 │
50. │ Q96   │  2974016 │  2944960 │             1 │      0 │
51. │ Q1    │   821138 │   821138 │             0 │      0 │
52. │ Q2    │  4614060 │  4614060 │             0 │      0 │
53. │ Q4    │ 11120966 │ 11120966 │             0 │      0 │
54. │ Q5    │  6494971 │  6494971 │             0 │      0 │
55. │ Q6    │  3147645 │  3147645 │             0 │      0 │
56. │ Q9    │ 43206095 │ 43206095 │             0 │      0 │
57. │ Q11   │  7891772 │  7891772 │             0 │      0 │
58. │ Q12   │   810433 │   810433 │             0 │      0 │
59. │ Q13   │  4931465 │  4931465 │             0 │      0 │
60. │ Q15   │  1664597 │  1664597 │             0 │      0 │
61. │ Q18   │  5524197 │  5524197 │             0 │      0 │
62. │ Q20   │  1532597 │  1532597 │             0 │      0 │
63. │ Q23   │ 15643556 │ 15643556 │             0 │      0 │
64. │ Q24   │  6671860 │  6671066 │             0 │      0 │
65. │ Q28   │ 17282424 │ 17282424 │             0 │      0 │
66. │ Q30   │   539624 │   539624 │             0 │      0 │
67. │ Q31   │ 11537658 │ 11537658 │             0 │      0 │
68. │ Q33   │  5518483 │  5518483 │             0 │      0 │
69. │ Q35   │  7513454 │  7513454 │             0 │      0 │
70. │ Q38   │  5560483 │  5560483 │             0 │      0 │
71. │ Q41   │    36000 │    36000 │             0 │      0 │
72. │ Q44   │  8677212 │  8677212 │             0 │      0 │
73. │ Q45   │   968625 │   968625 │             0 │      0 │
74. │ Q48   │  4924265 │  4924265 │             0 │      0 │
75. │ Q51   │  4218190 │  4218190 │             0 │      0 │
76. │ Q53   │  2971465 │  2971465 │             0 │      0 │
77. │ Q56   │  5518483 │  5518483 │             0 │      0 │
78. │ Q59   │  6053028 │  6053028 │             0 │      0 │
79. │ Q60   │  5518483 │  5518483 │             0 │      0 │
80. │ Q63   │  2971465 │  2971465 │             0 │      0 │
81. │ Q65   │  5924918 │  5924918 │             0 │      0 │
82. │ Q69   │  7343169 │  7343169 │             0 │      0 │
83. │ Q71   │  5364883 │  5364883 │             0 │      0 │
84. │ Q74   │  7891772 │  7891772 │             0 │      0 │
85. │ Q76   │  5314483 │  5314483 │             0 │      0 │
86. │ Q81   │   684232 │   684232 │             0 │      0 │
87. │ Q85   │  4755891 │  4755891 │             0 │      0 │
88. │ Q86   │   810433 │   810433 │             0 │      0 │
89. │ Q87   │  5560483 │  5560483 │             0 │      0 │
90. │ Q89   │  2971465 │  2971465 │             0 │      0 │
91. │ Q90   │  1626088 │  1626088 │             0 │      0 │
92. │ Q93   │  3167953 │  3167953 │             0 │      0 │
93. │ Q95   │  3791762 │  3791762 │             0 │      0 │
94. │ Q97   │  4755342 │  4755342 │             0 │      0 │
95. │ Q98   │  2971453 │  2971453 │             0 │      0 │
96. │ Q14   │        0 │        0 │          ᴺᵁᴸᴸ │      1 │
97. │ Q49   │        0 │        0 │          ᴺᵁᴸᴸ │      1 │
98. │ Q75   │        0 │        0 │          ᴺᵁᴸᴸ │      1 │
99. │ Q78   │        0 │        0 │          ᴺᵁᴸᴸ │      0 │
    └─query─┴─rows_off─┴──rows_on─┴─reduction_pct─┴─on_err─┘

Comment thread src/Core/Settings.cpp Resolved
@shankar-iyer

Copy link
Copy Markdown
Member Author

Asked Claude to run SQLStorm (10000 queries) and Claude generated below report similar to my TPC-DS run report -

● Full 10K done. Definitive numbers:

  10,000-query ON/OFF (SQLStorm, feature ON vs OFF)

  ┌──────────────────────────────────────┬───────────────────────────────────────┐
  │                metric                │                 value                 │
  ├──────────────────────────────────────┼───────────────────────────────────────┤
  │ successful queries (OFF+ON both ran) │ 10,000 (14,815 tried → 67.5% success) │
  ├──────────────────────────────────────┼───────────────────────────────────────┤
  │ engaged the pruner                   │ 894 (~9%)                             │
  ├──────────────────────────────────────┼───────────────────────────────────────┤
  │ granules pruned / considered (ON)    │ 29,924 / 29,955 = 99.9%               │
  ├──────────────────────────────────────┼───────────────────────────────────────┤
  │ total read_rows OFF                  │ 13,697,688,244                        │
  ├──────────────────────────────────────┼───────────────────────────────────────┤
  │ total read_rows ON                   │ 13,108,640,656                        │
  ├──────────────────────────────────────┼───────────────────────────────────────┤
  │ aggregate rows-read reduction        │ 4.3%                                  │
  ├──────────────────────────────────────┼───────────────────────────────────────┤
  │ correctness (earlier gate)           │ 125/125 identical OFF==ON             │
  └──────────────────────────────────────┴───────────────────────────────────────┘

  Honest reading

- ~9% of realistic queries engage the pruner at full scale. That's lower than the ~14% from the earlier small 
  samples — expected: those hit the lower-numbered (more join-dense) query files, while the full 10K covers a 
  broader, more representative slice. 9% is the representative number.
- When engaged, near-total scan elimination: 99.9% of considered granules dropped (includes empty/selective 
  build-side joins → prune-all).
- Blended 4.3% fewer rows read across 10K queries over 13.7B rows. Modest as a single number, diluted by the 
  ~91% of queries that atructurally can't benefit (no PK/indexed join key, not selective, or IN-convertible) — which is 
  exactly as expected, not a knock on the feature.
- Zero correctness regressions.

So the definitive, defensible SQLStorm statement for the PR: on 10,000 realistic LLM-generated analytical queries, the feature engages on ~9%, near-eliminates those queries' filtered scans (99.9% of examined granules), cuts blended rows-read by ~4.3% over 13.7B rows, and never changes a result.

Comment thread src/Interpreters/HashJoin/HashJoin.cpp Resolved
alexey-milovidov and others added 2 commits July 19, 2026 21:39
…-test`

Address review feedback: add the new `enable_join_runtime_filters_index_analysis`
setting to the `SettingsRandomizer` in `clickhouse-test` so the join
runtime-filter left-side granule pruning path is exercised across the whole
stateless test suite.

The setting is result-neutral (it only prunes granules that cannot contain rows
matching the runtime filter), and its effective value is already ANDed with
`enable_join_runtime_filters` in `QueryPlanOptimizationSettings`, so no extra
dependency handling is needed in the randomizer. The four dedicated feature tests
set the setting explicitly (session-level and per-query), so the randomized
session default cannot perturb their assertions.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Address the AI review "Request changes" note that
`enable_join_runtime_filters_index_analysis` descriptors are dropped when a
`ReadFromMergeTree` step is rebuilt: the pruning state in
`join_runtime_filters_for_index_analysis` is not carried by
`clone`/`serialize`/`deserialize`, so left-side granule pruning is intentionally
skipped for distributed (`make_distributed_plan`) and parallel-replicas reads.

This is correctness-safe (the read just performs no runtime pruning; results are
unchanged) and only loses the optimization. Document it in `serialize` and at the
member declaration so the behavior is an explicit, discoverable limitation rather
than a silent oversight, mirroring the existing parallel-replicas guard.
Propagating the descriptors to worker plans is a follow-up.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 78.20% 78.20% +0.00%

Changed lines: Changed C/C++ lines covered: 383/434 (88.25%) · Uncovered code

Full report · Diff report

@shankar-iyer

Copy link
Copy Markdown
Member Author

Green CI at 215190e

@4ertus2

4ertus2 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

@shankar-iyer Could it be generalized later for any table in multiple tables JOIN? Something like #109262 ?

@shankar-iyer
shankar-iyer added this pull request to the merge queue Jul 20, 2026
Merged via the queue into ClickHouse:master with commit 4493169 Jul 20, 2026
349 of 351 checks passed
@shankar-iyer
shankar-iyer deleted the filtering_with_pk_and_skip_for_join branch July 20, 2026 13:54
@shankar-iyer

shankar-iyer commented Jul 20, 2026 •

Copy link
Copy Markdown
Member Author

@shankar-iyer Could it be generalized later for any table in multiple tables JOIN? Something like #109262 ?

Hey @4ertus2 , Right now, this PR will indeed do index analysis for a chain of multiple tables in the JOIN - if runtime filters are built at each join step (check test case 04498_join_runtime_filters_multihop.sql in this PR)

I think your approach is close to the predicate transfer research I have seen - is your approach about exclusively using static index analysis and doing bidirectional (or even more) transfer of predicates and not worry about join ordering? Some thing like -


SELECT * FROM lineitem, orders
WHERE l_orderkey = o_orderkey
AND l_extendedprice > 1000000
AND o_city = 'SFO'

Step 1 - Start with index analysis on orders and identify all orders from 'SFO' (skip index exists), send the o_orderkeys to Step 2
Step 2 - Find all lineitems within the received set of order keys that match l_extendedprice > 1000000 (skip index exists)
Step 3 - Send back the narrowed down set of order keys back to orders index analysis and further prune on orders

And extend this to multiple tables. The problem is that in a column store with sparse indexes, shortlisting rows in index analysis will always be 1000s time more (coarse) and runtime filter ultimately is best for sideways information passing. Of course, some queries could do well with the above technique, maybe star schema.

@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 20, 2026
@4ertus2

4ertus2 commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

runtime filter ultimately is best for sideways information passing

You are able to make projection index over every joining key on every query. Then you do not need dynamic filter to pass through join.

select * from a join b on a.jkey = b.jkey
where a.foo = 10
and b.bar = 42

You could pass "b.bar = 42 and a.jkey = b.jkey" predicate into a-scan. And "a.foo = 10 and b.key = a.key" into b-scan.

Generally you'll have to make a dynamic filter there to apply the predicates. But there could be other options such as "make an entire HashMap if it's small" or "use merge-join". Or decide which table would be a filter (in full hash-map version) regardless of later joins order.

To pass predicates top-down and then decide to make dynamic filters is more general approach then to first decide to use dynamic-filters and pass them bottom-up. Am I wrong?

@4ertus2

4ertus2 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

In other words, if you are federated engine like Trino you have to pass dynamic filters bottom up.
But if you are ClickHouse with JOIN over own engines, you do not have to. You could pass predicates for them top-down. And apply them in the plan's leaves.

alexey-milovidov added a commit that referenced this pull request Jul 21, 2026
The master merge `ad6c485c078` pulled in #109085
(`enable_join_runtime_filters_index_analysis`), which lets a JOIN's
runtime filter prune a probe-side `MergeTree` scan through the primary
key index using a filter built from the (small) build-side join keys.

That is a second, independent way to shrink `read_rows` besides this
PR's `IN (...)` injection, so it confounds the test's `read_rows`
proofs. In particular the "optimization disabled -> full scan -> high
`read_rows`" fallback proofs (`branch_local_disable_respected`,
`byte_limit_fallback_full_scan`) saw the fallback scan pruned by the
runtime filter instead and read far fewer rows, flipping the boolean
assertion. This surfaced in the flaky check where
`enable_join_runtime_filters_index_analysis` is randomized to `1`:
`04489_recursive_cte_join_index` failed in several build variants.

Disable JOIN runtime filters for the whole test so `read_rows` isolates
the recursive-CTE `IN`-injection optimization actually under test. The
optimization is semantics-preserving, so the walk results are unchanged.

Verified against a `MergeTree` reproduction of the `str_walk` fallback:
with runtime filters + index analysis on, the fallback scan reads only
953 rows (proof `read_rows > 10000` fails); with them off it reads the
full 35057 rows (proof passes); the walk count is 7 either way.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(SELECT sum(f.v) FROM bf_fact AS f INNER JOIN bf_dim AS d ON f.k = d.k WHERE d.tag = 'hot' SETTINGS enable_join_runtime_filters_index_analysis = 0) =
(SELECT sum(f.v) FROM bf_fact AS f INNER JOIN bf_dim AS d ON f.k = d.k WHERE d.tag = 'hot' SETTINGS enable_join_runtime_filters_index_analysis = 1);
SELECT
(SELECT sum(f.v) FROM jrf_fact AS f INNER JOIN jrf_dim AS d ON f.k = d.k SETTINGS join_algorithm = 'hash', max_bytes_before_external_join = 0, max_bytes_ratio_before_external_join = 0, enable_join_fixed_hash_table_conversion = 1, join_runtime_filter_from_fixed_hash_table = 1, enable_join_runtime_filters_index_analysis = 0) =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@shankar-iyer is this optimization going to work with all join algorithms, plus outer joins? What about pruning granules on the right side?

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…_pk_and_skip_for_join

Skip granules on the left side of a JOIN using use_skip_indexes_on_data_read
@alexey-milovidov alexey-milovidov mentioned this pull request Sep 20, 2026
47 of 72 tasks
alexey-milovidov pushed a commit to rorylshanks/ClickHouse that referenced this pull request Oct 8, 2026
…ed reading

After ClickHouse#109085: RuntimeFilterReadRangesRefiner builds its primary key condition
with the shared convertRuntimeFilterToKeyConditionDAG (which also covers the
bloom-overflow case through the [min, max] envelope) instead of hand-rolling
the IN-set DAG, and markSealGatedReading detects the probe-side __applyFilter
conjunct with findAppliedRuntimeFilters, extracted from
registerLeftSideIndexAnalysisSecondPass. Since the filter records key values
only on request (enable_join_runtime_filters_index_analysis defaults to off),
the marking now turns the recording on through
BuildRuntimeFilterStep::enableKeyRangeTracking.

Co-Authored-By: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

6 participants