Repository navigation
Allow parallel_hash for ASOF JOIN - #105375
Conversation
52deb56 to
cc28346
Compare
|
For posterity / future reviewers — the rationale for why removing the
Two pieces of corroboration:
The regression test ( |
bac85fb to
3d101d1
Compare
ConcurrentHashJoin scatters right-side blocks by hashing the equality key, so rows with the same equality key always land in the same internal HashJoin partition. The per-key SortedLookupVector that backs ASOF lives entirely within that partition, so the parallel build behaves the same way as for non-ASOF strictness. The exclusion in allowParallelHashJoin appears to be defensive carryover from the original ConcurrentHashJoin PR (ClickHouse#37648, April 2022) — it shipped with the gate in place from day one with no rationale in the source or PR text, and ConcurrentHashJoin itself contains zero ASOF-specific code paths or assertions. For workloads where the ASOF equality key has low cardinality (e.g. SQL generated by visualization tools that compute multi-channel derived metrics like vector magnitude sqrt(x^2 + y^2 + z^2) and use a constant equality predicate to satisfy ASOF's syntactic requirement), this unblocks parallelism on the build phase: e.g. one of our workloads goes from ~93s wall (single-core build of a 250M-row hash table x 2 stacked ASOF joins) toward what the existing manual UNION-ALL time-shard workaround achieves (~25s with 4x CPU utilisation), per ClickHouse#73086. Adds a regression test that runs the same ASOF INNER and ASOF LEFT queries under both join_algorithm = 'hash' and 'parallel_hash' and checks per-row identity via mutual EXCEPT.
3d101d1 to
de5b21e
Compare
|
@vdimir based on your activity it looks like you're the best reviewer for this change? Let me know what you think of it. I verified the added test passes locally on macOS/aarch64 with a RelWithDebInfo build of this branch. I also verified a set of 27 other selected existing tests based on relevance to make sure this didn't cause a regression. Also verified we see in EXPLAIN PIPELINE that the parallel path is used with ASOF JOIN + SETTINGS join_algorithm = 'parallel_hash'. This would be a highly, highly impactful performance improvement for us! A very common query pattern for us is aligning time series where the timestamps don't match exactly using ASOF JOIN at scale where you really need join parallelization to have acceptable performance (100s of millions of rows+). Would really appreciate a look at this! Thanks. |
|
This sounds good, and the change is reasonable. Thank you! |
|
Workflow [PR], commit [aa697cd] Summary: ✅
AI ReviewSummaryThis PR enables Missing context / blind spots
Tests
Final VerdictStatus: Minimum required action: provide focused performance evidence for the newly enabled |
There was a problem hiding this comment.
Thanks for your PR and the detailed description!
I don't really understand whether we handle the asof key component correctly with parallel hash joins. Consider rows like {a: 1, b: 1} and {a: 1, b: 3}. If we use (a, b) for scattering, these rows may end up in different buckets. Then a probe-side row like {a: 1, b: 2} will pick only one bucket depending on where it lands, while we should inspect both.
I suppose the same issue mentioned in the bot's comment above
Probably ASOF JOIN would work correctly only if we used just the prefix keys that participate in the equality match for hashing (usually all keys except last one). But I assume we currently use all keys. Correct me if I'm wrong.
ConcurrentHashJoin::selectDispatchBlock was hashing all key columns to compute the scatter selector, including the trailing ASOF inequality column. The per-partition HashJoin's bucket key is the equality-only prefix (HashJoin::HashJoin pops the asof column from key_columns before chooseMethod()), so dispatching by the full key list sent rows with the same equality keys but different asof values to different partitions and they never met during probe. Slice off the trailing asof key from both key_columns_names and key_sizes when computing the scatter hash, encapsulated in a small getDispatchKeyShape helper so the asof-specific logic has a named home with a doc comment. The single-equality-key case happened to work even without the slice because the chosen key getter is HashMethodOneNumber, which only reads column[0] and ignored the extra column. The slice is load-bearing for multi-equality-key joins where HashMethodKeysFixed / HashMethodHashed hashes N columns based on key_sizes.size(). Extends the regression test to cover both single- and multi-equality-key shapes with per-row mutual EXCEPT against the hash algorithm.
Removing the parallel_hash gate for ASOF in allowParallelHashJoin causes EXPLAIN to print Algorithm: ConcurrentHashJoin (instead of HashJoin) for the ASOF block in this test. This is the intended new behavior — update the reference accordingly. The non-ASOF block in the same test already expected ConcurrentHashJoin, so only the single line under "Strictness: ASOF" changes.
|
@vdimir thank you for the review! Yep, looks like you were right. The test case pushed initially only tested the case with a single equality key, but looks like it didn't work with multiple equality keys. Updated the code to only consider the equality keys, not the ASOF key, for hashing, and also updated the test to test against multiple equality keys. Looks like everything passes now - the failures that I see look like flakes to me. Could you retrigger them for me? |
|
Thank you for the approval @vdimir! Do you know what's going on with the failing checks? They don't seem to be related to the code... |
|
@vdimir the bot is asking to provide numbers showing a performance improvement here, but do we need that in order to merge? I think it's pretty clear that parallelizing when there is high equality key cardinality will improve performance and we don't need to individually benchmark that, but let me know if you disagree. |
…i-shard dispatch With max_threads = 1, ConcurrentHashJoin uses a single shard and dispatchBlock returns the whole block without ever calling selectDispatchBlock, so the ASOF prefix-slicing fix would not be covered. Setting max_threads = 4 forces more than one shard so the multi-equality-key cases actually exercise selectDispatchBlock. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
After merging master, 04300_asof_join_parallel_hash collided with 04300_deltalake_ffi_panic_guard and 04300_final_sequential_partitions_null added on master. Renumbered to the free slot 04312. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Merged latest
Empirical verification on a freshly built binary (this branch, merged):
CI: the only failures are |
|
📊 Cloud Performance Report ✅ AI verdict: This PR enables the parallel-hash join algorithm for ASOF joins and only changes the ASOF-specific scatter logic; non-ASOF joins and join-free queries are functionally unchanged. The flagged TPC-H Q4 regression (+42.75%) is on a non-ASOF semi-join the PR cannot affect, and its measurements were noisy, so it is downgraded to not-sure. ClickBench Q32 runs over a single flat table with no joins, so it sits entirely off the changed path. No join-related performance impact is attributable to this change. clickbenchFlagged queries (1 of 43)
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_officialFlagged queries (1 of 22)
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. Debug info
|
After merging master, 04312 collided with 04312_iceberg_file_filter_progress, 04312_in_order_aggregation_inconsistency, and 04312_text_index_lazy_segment_cache_reuse. 04319 is the next free number. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Picked this up to keep it current (it was 711 commits behind What I did:
On the two CI failures — both unrelated, pre-existing flakies:
@groeneai, could you take a look at the |
|
@alexey-milovidov, picked this up — investigating the Confirmed signature (CIDB, 30d, 9 hits, 6 PRs, all PR runs, last 2026-06-05 13:42Z):
Root cause: Cross-translation-unit static-destructor-order race. Fix shape: Deliberately leak Session: cron:clickhouse-ci-task-worker:20260606-013800 |
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
|
Fix PR: #106620 — |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 40/41 (97.56%) | Lost baseline coverage: none · Uncovered code |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Allow
ASOF JOINto use theparallel_hashjoin algorithm, parallelizing the build across distinct equality-key values. Previously ASOF was unconditionally opted out ofparallel_hash.Detailed description / Documentation / Tests / Additional context
Partially addresses #105371 (see the scope note below — the constant / very-low-cardinality equality-key case is a follow-up).
allowParallelHashJoininsrc/Interpreters/TableJoin.cpppreviously returnedfalseforJoinStrictness::Asof. The exclusion appears to be defensive carryover from the originalConcurrentHashJoinPR (#37648, April 2022) — it shipped with the gate in place from day one with no rationale in the source or PR text.Correctness: scatter by the equality-key prefix only.
ConcurrentHashJoinscatters right-side blocks by hashing the join keys. For ASOF, the trailing key is the inequality (ASOF) key, andHashJoindeliberately excludes it from hash-map keying (createKeyGetterpops the trailing key). IfConcurrentHashJoinhashed the full key list, rows sharing the same equality keys but differing in the ASOF key could scatter to different partitions and never meet on the probe side — breaking ASOF matching for the multi-keykeys128/keys256/hashedmethods. This PR makesselectDispatchBlockhash only the equality-key prefix (dropping the trailing ASOF key), matching the per-partitionHashJoinbucket key. Thanks @vdimir and the review bot for catching this — the initial revision only exercised a single equality key and missed it.With that fix, rows with the same equality-key value always land in the same internal
HashJoinpartition, and the per-keySortedLookupVectorthat backs ASOF lives entirely within that partition, so the parallel build behaves the same way as for non-ASOF strictness.Tests.
04300_asof_join_parallel_hash.sqlruns the sameASOF INNERandASOF LEFTqueries underjoin_algorithm = 'hash'and'parallel_hash'and checks per-row equivalence via mutualEXCEPT, for both single-equality-key and multi-equality-key ((a, b, t)) shapes. The multi-key cases are the ones that fail without the prefix-only dispatch fix.02835_join_step_explainreference updated to reflect that ASOF now reportsConcurrentHashJoinin EXPLAIN.Scope — what this does and does not do. This parallelizes ASOF builds across distinct equality-key values: each scatter partition builds its own
SortedLookupVectors in parallel. It does not parallelize the build within a single equality-key value — when the equality key is constant or very low-cardinality (the motivating workload in #105371, e.g. generated SQL using a constant'a' AS kpredicate to satisfy ASOF's syntactic requirement), all rows for that key still route to oneHashJoinslot and the build on that slot remains serial. Parallelizing within a single equality key (e.g. range-partitioning the ASOF key, or a parallelSortedLookupVectorbuild) is a separate, larger change that we'll pursue in a follow-up PR; #105371 stays open to track it.Note on a related but separate gate.
optimizeJoinByShardsinsrc/Processors/QueryPlan/Optimizations/optimizeJoinByShards.cppalso has an ASOF exclusion. I haven't removed that one — it has different requirements (PK-prefix sharding) and may have a real correctness reason that needs separate investigation. Filing as a follow-up if this PR lands cleanly.