Skip to content

Add test for JOIN runtime-filter primary-key pruning on a reverse key - #111777

Merged
nihalzp merged 2 commits into
ClickHouse:masterfrom
groeneai:test-reverse-key-join-runtime-filter-pruning
Jul 28, 2026
Merged

nihalzp merged 2 commits into
ClickHouse:masterfrom
groeneai:test-reverse-key-join-runtime-filter-pruning

Conversation

@groeneai

@groeneai groeneai commented Jul 24, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #111059

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Not for changelog.

Description

Follow-up requested by @ nihalzp on #111059 (inline review on MergeTreeIndexReadResultPool.cpp): the reverse-key primary-key index-analysis fix is backported to versions without the JOIN runtime-filter read-time pruning path, so that path was left out of the backportable test 04612_reverse_key_index_analysis. This adds the missing coverage on master only.

The enable_join_runtime_filters_index_analysis path builds a read-time KeyCondition from a join's collected probe keys in MergeTreeIndexReadResultPool and prunes the probe-side primary key. It must honor the key's sort directions: before #111059 a DESC primary-key column was analyzed as ascending, which inverts the value interval and drops granules that hold matching rows (on a set-index probe it aborts with Invalid binary search result in MergeTreeSetIndex).

The new stateless test 04628_join_runtime_filter_reverse_key joins on reverse-sorted primary keys (DESC as the second column, DESC as the leading column, and a single DESC column) and checks that:

  • the result with enable_join_runtime_filters_index_analysis = 1 equals the result with it off (pruning must not change results), and
  • pruning actually engaged (RuntimeFilterGranulesConsidered > 0 and RuntimeFilterGranulesDropped > 0), so the equality check is not vacuous.

On pre-#111059 code the test fails (wrong result / abort in the pruning path); after the fix it passes.

Follow-up to ClickHouse#111059 requested by nihalzp: the reverse-key primary-key
index-analysis fix is backported to versions without the JOIN runtime-filter
read-time pruning path (MergeTreeIndexReadResultPool), so that path is not
covered by the backportable test 04612_reverse_key_index_analysis. This adds
the missing coverage on master only.

The enable_join_runtime_filters_index_analysis path builds a read-time
KeyCondition from a join's collected probe keys and prunes the probe-side
primary key. It must honor the key sort directions: before ClickHouse#111059 a DESC
primary-key column was analyzed as ascending, which inverts the value interval
and drops granules holding matching rows (on a set-index probe it aborts with
Invalid binary search result in MergeTreeSetIndex).

The test joins on reverse-sorted primary keys (DESC second, DESC leading, and
a single DESC column) and checks that the result with the feature on equals the
result with it off, and that pruning actually engaged (RuntimeFilterGranules
Considered and Dropped both greater than zero) so the equality is not vacuous.
@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; 2 findings across 1 full pass + 1 fix round + 1 delta recheck; final content recheck: 0 findings).

# Sev Finding Verdict Evidence / action
1 ⚠️ desc_second could not detect loss of the second column's reverse ordering: r was functionally determined by g (one r per g), so ASC and DESC layouts were identical and the case passed vacuously. AGREE Confirmed against my own pre-fix run: desc_second printed on == off = 1 while only desc_first crashed. Redesigned data so every g holds all 100 r values with the hot subset selective on r.
2 ⚠️ Even after round 1, desc_second's hot subset still restricted the leading g (0..19 of 0..39), so g-pruning alone kept both oracles green; in a release build MergeTreeSetIndex fail-opens on the inverted range, hiding a non-leading-DESC regression. AGREE Verified Set.cpp:824-834 returns {true,true} under NDEBUG. Made g fully non-selective (every g hot; hot subset selects only on r >= 90) with per-shape dims, so the reverse r column is the sole pruning source. Re-verified both directions: post-fix passes 50/50; pre-fix aborts on the desc_second query (Invalid binary search result in MergeTreeSetIndex).

Severity: ❌ blocker / ⚠️ major / 💡 nit. Both findings were real and fixed; the final recheck on the strengthened test returned 0 findings.

Session id: cron:clickhouse-author-slot-3:20260724-094900

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. On a pre-#111059 build the reverse-key runtime-filter join aborts on demand (Invalid binary search result in MergeTreeSetIndex at MergeTreeIndexReadResultPool.cpp:144 -> markRangesFromPKRange -> KeyCondition::checkInRange -> MergeTreeSetIndex::checkInRange); on current master it passes.
b Root cause explained? The enable_join_runtime_filters_index_analysis path built the read-time KeyCondition from primary_key.column_names/expression, dropping the reverse-flag info, so a DESC primary-key column was analyzed as ascending. That inverts the value interval: on a range predicate it drops matching granules; on an IN-set probe it yields an invalid MergeTreeSetIndex range. #111059 fixed it to the KeyDescription overload carrying KeyOrder.
c Fix matches root cause? N/A for this PR: it is a test-only follow-up to the already-merged #111059 fix, adding coverage for exactly that mechanism on the master-only join path.
d Test intent preserved / new tests added? New stateless test. Each shape asserts both correctness (feature on == off) and that pruning actually engaged (RuntimeFilterGranulesConsidered > 0 and Dropped > 0), so the equality is not vacuous.
e Both directions demonstrated? Yes. Post-fix (current master): 50/50 pass. Pre-fix build: the desc_second query aborts / the drop oracle would fail in a release build.
f Fix is general across code paths? Covers three reverse-key shapes (DESC second column, DESC leading column, single DESC column) plus a multi-key AND join, not just the single reported shape.
g Fix generalizes across inputs? Reverse column in first/second/only position; Int32/UInt32 key types; the runtime filter exercised via both the min/max range path (multi-key, >100 distinct keys) and the IN-set path (single key, <=100 distinct keys).
h Backward compatible? N/A (test-only, no behavior change).
i Invariants and contracts preserved? N/A (test-only). The test relies only on documented feature settings and is parallel-safe (table names local to the per-test DB; query_log reads scoped to currentDatabase(); no server-global objects; no no-parallel tag).

Session id: cron:clickhouse-author-slot-3:20260724-094900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @KochetovNicolai could you review this? It is a test-only follow-up to #111059, adding the missing coverage for the JOIN runtime-filter index-analysis path (enable_join_runtime_filters_index_analysis, master-only, not in the backport) pruning a reverse-sorted (DESC) primary key.

@nihalzp nihalzp added the can be tested Allows running workflows for external contributors label Jul 24, 2026
@nihalzp nihalzp self-assigned this Jul 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [17358ee]

Summary: ✅


AI Review

Summary

This PR adds a master-only stateless regression test for reverse-key JOIN runtime-filter primary-key pruning, covering both the exact-set path and the overflow-to-range path. The current head closes the earlier coverage gap by forcing range mode and proving that it prunes differently from the exact-set branch, so I did not find a remaining correctness issue in the test itself.

PR Metadata
  • Changelog category is not semantically correct: this change adds test coverage, but it does not modify CI. Use - Not for changelog (changelog entry is not required) instead of - CI Fix or Improvement (changelog entry is not required).
  • Under Not for changelog, no user-facing changelog entry is required; the existing Not for changelog. text is fine.
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 24, 2026
The reverse-key JOIN runtime-filter test only exercised the exact IN-set
(MergeTreeSetIndex) path: each build side has fewer distinct keys than
join_runtime_filter_exact_values_limit, so buildRuntimeRangePredicate never
took its [min, max] range branch.

Make the hot subset two non-adjacent r values (10 and 90) and add, per
reverse-key shape, a range-branch workload that lowers
join_runtime_filter_exact_values_limit below the distinct-key count so the
exact-values set overflows and is released and the read-time KeyCondition is
built from the recorded range envelope. With the hot values far apart the two
predicate shapes prune differently: the exact IN-set {10, 90} drops every
granule outside those values while the [10, 90] range keeps the whole span.
The assertion checks each shape drops on both paths and that the range path
drops strictly fewer granules than the IN-set path, so it fails if pruning
silently stays on the IN-set path (equal counts) or a wrongly-inverted
reverse-key range drops nothing. Results must equal the feature-off result on
both paths.

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

groeneai commented Jul 24, 2026 •

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

Pre-push review by an independent model (engine: codex; 2 findings across 1 full pass + 1 delta re-review).

# Sev Finding Verdict Evidence / action
1 major The range-mode checks stayed green even if pruning silently stayed on the IN-set path, because contiguous hot keys 90..99 make IN(90..99) equivalent to range [90,99]. AGREE, fixed Made the hot subset two non-adjacent values {10,90} and assert, per shape, in_dropped>0 AND range_dropped>0 AND range_dropped<in_dropped. Range keeps the whole [10,90] span, IN prunes the gaps, so the strict inequality holds only when the range branch actually ran. Re-verified both directions (fixed passes 50/50; #111059-reverted build fails).
2 nit The internal contract note still described the old additive approach (limit 5, IN block untouched). AGREE, fixed Rewrote the internal contract note to match the shipped test (hot {10,90}, limit 1, combined oracle, nine reference rows). No test change.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 17358ee

Every failure has an owner: a fixing PR, or a full-effort fix task. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (amd_asan_ubsan, distributed plan, parallel) / 00162_mmap_compression_none flaky (MEMORY_LIMIT_EXCEEDED/timeout under randomized settings; 17 PRs, 0 master) a fix task is already pending (investigating at full effort; fixing-PR link to follow here)
Stateless tests (amd_asan_ubsan, distributed plan, parallel) / 00265_http_content_type_format_timezone flaky (result differs; 3 unrelated PRs, 0 master) a fix task is created (investigating at full effort; fixing-PR link to follow here)
Mergeable Check / PR rollup of the above owned via the two fix tasks above

Not PR-caused: this is a test-only PR (adds 04628_join_runtime_filter_reverse_key); neither 00162_mmap_compression_none nor 00265_http_content_type_format_timezone is touched by the diff, and both fail across unrelated PRs with 0 master hits.

Session id: cron:our-pr-ci-monitor:20260724-193000

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 17358ee

This is a test-only PR (adds stateless test 04628_join_runtime_filter_reverse_key). Neither failure is caused by it; each has an owner.

Check / test Reason Owner
Stateless tests (amd_asan_ubsan, distributed plan, parallel) / 00265_http_content_type_format_timezone flaky (3 unrelated PRs / 0 master, 30d; result differs under distributed-plan randomization) a fix task is moved to pending (investigating at full effort; fixing-PR link to follow here)
Stateless tests (amd_asan_ubsan, distributed plan, parallel) / 00162_mmap_compression_none flaky (18 unrelated PRs / 0 master, 30d; MEMORY_LIMIT_EXCEEDED / timeout under randomized settings) a fix task is moved to pending (investigating at full effort; fixing-PR link to follow here)

Session id: cron:our-pr-ci-monitor:20260724-200000

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the 00265_http_content_type_format_timezone line of the ledger above:
#112112

A correction to that row's reason, since the investigation contradicted it: the failure is not a
randomized-settings or timezone-formatting interaction. Two chained causes, and only the second
one belongs to the test.

  1. The trigger is the shard saturating the server-global memory cap, after which the server
    refuses HTTP requests before the query is parsed (authenticateUserByHTTP ->
    checkUserNameNotEmptyAndServerHasEnoughMemory, Code: 241). set_query_result never runs,
    so X-ClickHouse-Format and X-ClickHouse-Timezone are absent and the response keeps the
    default text/plain; under worse pressure the connection is reset instead. That half is
    already fixed upstream by Run distributed-plan sanitizer stateless on runner with more mem instead of cutting concurrency #111789 (larger runner), and all three recorded failing commits
    predate it.
  2. The test then swallows the evidence. Every one of its 13 invocations was
    ${CLICKHOUSE_CURL} -vsS ... 2>&1 | grep '< Header' | sort, so 2>&1 merges curl's
    diagnostics into the pipe, grep drops them, and the pipeline's status is grep/sort's.
    Measured against a listener that accepts and then resets: pipeline_rc=0, empty stdout and
    zero bytes of stderr. The only symptom left is whole header groups missing from the output,
    which is why it surfaces as a bare result differs.

That second half is what #112112 fixes, and it is why the same trigger produced opposite
classifications: of the three occurrences in the CI database over 30 days, the one that happened
to let the server's error text through was classified as a stderr failure and is retryable today
(MESSAGES_TO_RETRY already contains (total) memory limit exceeded), while the two that
swallowed it produced an unattributable diff.

@nihalzp
nihalzp added this pull request to the merge queue Jul 28, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jul 28, 2026
@nihalzp
nihalzp added this pull request to the merge queue Jul 28, 2026
Merged via the queue into ClickHouse:master with commit 84e3e36 Jul 28, 2026
352 of 353 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 Jul 28, 2026
evillique pushed a commit to evillique/ClickHouse that referenced this pull request Aug 7, 2026
…adline

The guard added in the previous commit fires on a disjunction: a non-zero `curl`
exit, OR a response with no `X-ClickHouse-Timezone` header. Its message and the
comment above the helper described only the second condition, so the first one
printed a false statement.

Measured against a listener that emits complete, correct `200` headers including
a real `X-ClickHouse-Timezone`, then a `Content-Length` larger than the body it
actually sends, then a reset: `curl` exits 56 with the header present, so only
the exit-status condition fires, and the report claimed the request never
reached query execution when it demonstrably had. That shape is caught either
way; only the wording was wrong.

Reword the single unconditional `echo` to cover both conditions and keep
`(curl exit $rc)`, which is what discriminates a header-missing response from a
transport failure. Update the comment block to state both conditions. No logic
change: the guard, the truncation, the body file, the anchored match and all 13
call sites are untouched, and the test's stdout stays byte-identical to
`origin/master`'s (sha256 7611820edd3674d2..., which is also the `.reference`
hash).

The new headline was checked against `MESSAGES_TO_RETRY` in
`tests/clickhouse-test`: it matches none of the 11 entries, so it cannot itself
make a run retryable.

Related: ClickHouse#111777

Co-Authored-By: Claude Opus 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 pr-ci 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