rusanoph commented on code in PR #27746:
URL: https://github.com/apache/datafusion/pull/27746#discussion_r4701633879
##########
datafusion/physical-plan/src/joins/join_index/directory.rs:
##########
@@ -0,0 +1,368 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! Exact-key directory layouts, construction, and typed integer lookup.
+//!
+//! Dense integers use an array slot: keys `[10, 11, 12]` live at `key - 10`.
+//! Sparse integers such as `[10, 1_000_000]` use SwissTable with the actual
key
+//! in each entry. General keys use a cached hash and a representative build
row
+//! whose Arrow values establish equality. All three identify exact keys and
+//! return the same singleton/group representation; only row IDs are grouped.
+
+use std::mem::size_of;
+
+use arrow::array::{ArrayRef, BooleanArray};
+use arrow::buffer::BooleanBuffer;
+use datafusion_common::hash_utils::{RandomState, create_hashes};
+use datafusion_common::{NullEquality, Result, internal_err};
+use datafusion_execution::memory_pool::MemoryReservation;
+
+use super::keys::{LookupScratch, hash_scratch_bytes};
+use super::matches::{
+ GroupRows, JoinHashLookup, LookupResult, MatchHandles, MatchedGroups,
+ MatchedGroupsBuilder, RowId,
+};
+use crate::joins::array_map::ArrayMap;
+use crate::joins::utils::matchable_join_keys;
+
+mod dense;
+mod general;
+mod integer;
+
+use dense::DenseIntegerDirectory;
+use general::GeneralDirectory;
+use integer::{IntegerDirectory, integer_hasher};
+
+/// See [`JoinHashIndex`] for the design overview.
+struct Probe<'a> {
+ build_keys: &'a [ArrayRef],
+ probe_keys: &'a [ArrayRef],
+ null_equality: NullEquality,
+ random_state: &'a RandomState,
+}
+
+/// See [`JoinHashIndex`] for the design overview.
+#[derive(Debug)]
+pub(super) enum IndexWidth {
+ U32(JoinHashIndex<u32>),
+ U64(JoinHashIndex<u64>),
+}
+
+impl IndexWidth {
+ /// See [`JoinHashIndex::build`] and
+ /// [`JoinIndexMap::try_new`](super::JoinIndexMap::try_new) for shared
argument documentation.
+ pub(super) fn build(
+ keys: &[ArrayRef],
+ random_state: &RandomState,
+ null_equality: NullEquality,
+ dense_range: Option<(u64, u64)>,
+ reservation: &MemoryReservation,
+ ) -> Result<Self> {
+ let Some(first) = keys.first() else {
+ return internal_err!("A join index requires at least one key
column");
+ };
+ let num_rows = first.len();
+ let scratch = reservation.new_empty();
+ // Keep validity unions and per-column metadata charged for every
layout.
+ // Dense keys are read directly, so only hash-based directories need
hashes.
+ scratch.try_grow(num_rows.div_ceil(8) * (keys.len() + 1) + keys.len()
* 256)?;
+ let hashes = if dense_range.is_none() {
+ scratch.try_grow(num_rows * size_of::<u64>() +
hash_scratch_bytes(keys))?;
+ let mut hashes = vec![0; num_rows];
+ create_hashes(keys, random_state, &mut hashes)?;
+ hashes
+ } else {
+ Vec::new()
+ };
Review Comment:
> ### `datafusion/physical-plan/src/joins/join_index/directory.rs` lines
66-77
> This hashes the whole build side into one `Vec<u64>`, 8 bytes per row,
held for the whole build. On `main` the build hashes slices of at most
`hash_chunk_rows` rows
([exec.rs](https://github.com/apache/datafusion/blob/73043acbe3fbe681a0219c2060eb4eb6e9d47228/datafusion/physical-plan/src/joins/hash_join/exec.rs#L3404-L3448)),
which [#26793](https://github.com/apache/datafusion/pull/26793) added to keep
that buffer near one batch.
>
> Together with the up-front `MatchedGroupsBuilder` reservation (next
comment), this raises the build's peak reservation. I measured it with
`PeakRecordingPool` on a `CollectLeft` inner join with 1M `Int64` build rows in
8192-row batches and three probe rows:
>
> Build keys `main` This PR
> Hash path, unique 55.7 MB 73.9 MB
> Hash path, 100 rows per key 55.7 MB 78.0 MB
> Dense, unique 32.0 MB 34.3 MB
> Dense, 100 rows per key 32.0 MB 34.4 MB
> A build that fits under a memory limit on `main` can fail with this PR.
Could the directories insert one slice at a time, sized by `hash_chunk_rows`,
so the hash buffer stays bounded as it is on `main`?
@mbutrovich Thanks for the suggestions! I've completed the SNAP and JOB
comparison, as well as the x86-64 validation of your counting-sort patch and
the controlled memory experiments.
**Setup:** AMD Ryzen 5 7500F, WSL2, Rust 1.97.0, one logical CPU, one
execution partition.
Presets:
- A: main merge-base `6a06058`
- B: PR head `2f6c665`
- C: B + your exact counting-sort patch
### SNAP and JOB (A vs B)
| Workload | Queries | Sum of query medians (A → B) | Reduction | Geomean
A/B |
| --- | ---: | ---: | ---: | ---: |
| SNAP | 35 | 26.266 → 6.814 s | 74.1% | 2.714× |
| SNAP holdout | 18 | 2.626 → 1.623 s | 38.2% | 1.644× |
| JOB | 100 | 208.113 → 153.416 s | 26.3% | 0.996× |
All 1,918 observations succeeded with matching outputs and physical plan
text.
The JOB improvement is concentrated in the six family-17 queries, which
account for approximately 99% of the net time saved. JOB 7b regressed from
277.5 to 360.4 ms. Selective Slashdot and Amazon out-star workloads also showed
smaller slowdowns.
The counting-sort experiment confirms the direction of your M5 Max results
on x86-64. I also reproduced the resource regression for sparse integer keys
under a 64 MiB memory-pool limit.
I'll post the details in the corresponding review threads.
**Results and reproducibility:**
- [Validation
overview](https://github.com/rusanoph/unchained-hash-join-research/blob/ab74b02eff94fd7164538bf54eb990a7e99ca6a8/datafusion/upstream-validation/pr-27746/README.md)
- [Full SNAP and JOB report, including
regressions](https://github.com/rusanoph/unchained-hash-join-research/blob/ab74b02eff94fd7164538bf54eb990a7e99ca6a8/datafusion/upstream-validation/pr-27746/reports/comparative-results.md)
- [Versioned artifact
release](https://github.com/rusanoph/unchained-hash-join-research/releases/tag/upstream-pr27746-v1)
The package includes the benchmark worker, SQL workloads, raw observations,
query plans, correctness evidence, source revisions and reproduction scripts.
The published statistics can be recomputed using `python3
scripts/verify_evidence.py` without compiling DataFusion or downloading
datasets.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]