Repository navigation
Fix wrong results in JOIN with shard-by-PK and query condition cache - #100926
alexey-milovidov merged 5 commits into
Conversation
|
cc @nikitamikhaylov @vdimir — this is the proper fix for the bug you identified in #100911. Instead of pinning |
Pre-PR Validation Gate (Session ef70e390)a) Deterministic repro? b) Root cause explained? c) Fix matches root cause? d) Test intent preserved? / New tests added? e) Both directions demonstrated?
|
|
Workflow [PR], commit [20801ef] Summary: ✅ AI ReviewSummaryThis PR fixes wrong query results in Missing context
ClickHouse Rules
Final Verdict
|
Bugfix validation fixThe bugfix validation was failing because the original Fix: Replaced the Verified both directions:
|
Regression test removal (commit 2acbf12bc59)Removed the
The existing test Session: cron:clickhouse-ci-task-worker:20260327-191500 |
|
Also, 04011_explain_pretty_join seems related |
When `filterPartsByQueryConditionCache` drops parts during `selectRangesToRead()`, the remaining parts have non-contiguous `part_index_in_query` values. The `apply()` function in `optimizeJoinByShards` then fails to distribute these parts back to their correct sources, because the distribution logic assumes contiguous indices. For example, if source t1 originally has parts at indices [0, 1] and index 0 is dropped by the cache (it cached that the condition evaluates to false for that part), the remaining part at index 1 gets an adjusted global index that falls outside the expected range for source t1, causing it to be assigned to no source. The result is that t1 reads 0 rows, producing wrong empty results. The fix renumbers `part_index_in_query` to be contiguous when collecting parts from each source, ensuring the distribution logic correctly assigns parts back regardless of cache filtering. Co-Authored-By: Claude Opus 4.6 <[email protected]>
2acbf12 to
76dfd7c
Compare
Pre-PR Validation Gate (Session cron:clickhouse-ci-task-worker:20260328-001500)a) Deterministic repro? b) Root cause explained? c) Fix matches root cause? d) Test intent preserved? / New test added? e) Demonstrated in both directions?
Note on previous approach: The earlier commits on this PR targeted |
|
cc @vdimir @pufit — complete rework of the fix. The previous approach (disabling query condition cache) was wrong — the cache logic is actually correct. The real bug is in |
…patibility CountMinSketch requires the datasketches-cpp library which is disabled in the fast test build (-DENABLE_LIBRARIES=0), causing "Unknown statistic type" error. Switch to tdigest which is always available. The specific statistics type doesn't affect the bug reproduction — any auto_statistics_types value triggers the statistics-based join optimization path. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Fast test fix (commit 4c2042a)The Fast test failure was caused by Fix: Changed Local verification:
@pufit — this should resolve the Fast test blocker. The mandatory regression test is now included with a compatible statistics type. Re: "04011_explain_pretty_join seems related" — that test was already fixed by PR #100962 (merged into this branch), and it pins |
pufit
left a comment
There was a problem hiding this comment.
Looks reasonable to me, but need someone else to verify
|
Merging master to pick up PR #100984 (skip indices flaky test fix, merged 2026-03-28T10:38:55Z). The |
|
@vdimir Maybe it's better to delete the shard-by-PK optimization altogether? It requires that the join key is a prefix of the PK prefix for both sides. That's typically not the case, at least not for a typical foreign key join (fact table - dimension table). |
While this pattern may not be very common, there are still valid use cases. If I remember correctly, the idea for this optimization came from a real scenario (though I’m not sure how widely it’s used). There are also examples of such cases in TPC-H Also, perhaps in the future we could extend this to support projections that share the same ORDER BY prefix. I agree that the current implementation may not be very stable yet, but it might be worth trying to fix it rather than removing it. At the moment, we have this PR and also #100861, which addresses some critical bugs. Let’s see how things evolve. As an alternative, we could consider downgrading it to experimental status for now. |
|
The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix. |
LLVM Coverage Report
Changed lines: 100.00% (8/8) | lost baseline coverage: 3 line(s) · Uncovered code |
|
Hi — this PR may need backporting to release branches Why: The query_plan_join_shard_by_pk_ranges setting was added in 25.4, so the buggy code exists in all active release branches (25.8+). While the setting defaults to false, users who opt in can get wrong JOIN results (missing rows) when query condition cache is also enabled. This is a silent wrong-results bug. If this should be backported, consider adding |
…and query condition cache
…and query condition cache
…and query condition cache
…and query condition cache
Backport #100926 to 26.1: Fix wrong results in JOIN with shard-by-PK and query condition cache
Backport #100926 to 26.2: Fix wrong results in JOIN with shard-by-PK and query condition cache
Backport #100926 to 26.3: Fix wrong results in JOIN with shard-by-PK and query condition cache
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix wrong results when JOIN with shard-by-PK optimization uses query condition cache and some parts are filtered out by cached conditions.
What this PR fixes
When
filterPartsByQueryConditionCachedrops parts duringselectRangesToRead(), the remaining parts keep their original (non-contiguous)part_index_in_queryvalues. Theapply()function inoptimizeJoinByShardsthen fails to distribute these parts back to their correct sources because the distribution logic assumes contiguous indices.Concrete example: Table t1 has two parts [index 0, index 1]. The query condition cache knows that part 0 has no matching rows for the join condition (from a previous query), so
filterPartsByQueryConditionCachedrops it. Part 1 survives with its originalpart_index_in_query = 1. When collecting parts across sources, the adjusted global index becomes1 + offset, which falls outside the expected range[offset, offset + num_surviving_parts). The distribution code assigns the part to no source, and t1 reads 0 rows — producing wrong empty results.The fix renumbers
part_index_in_queryto be contiguous when collecting parts from each source, ensuring the distribution logic correctly assigns parts back regardless of which parts were filtered by the cache.Reproduction (deterministic):
Also fixes the flaky test
02845_join_on_cond_sparsewhich was hitting this bug ~0.5-8% of the time via randomized settings.Version info
26.4.1.60426.3.5.5,26.2.8.15,26.1.8.19