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:
   I was able to reproduce the memory-limit regression on x86-64 using 
controlled workloads similar to those in your review: 1M build rows, 8,192-row 
batches and three probe rows.
   
   A is the main merge-base, B is this PR, and C includes the dense 
counting-sort patch.
   
   | Build shape | Peak pool A / B / C (MiB) | Successful runs at 64 MiB (A / B 
/ C) |
   | --- | ---: | ---: |
   | Dense unique | 22.95 / 32.66 / 22.95 | 5 / 5 / 5 |
   | Dense duplicates | 30.58 / 32.78 / 30.58 | 5 / 5 / 5 |
   | Sparse integer unique | 53.07 / 70.48 / 70.48 | 5 / 0 / 0 |
   | Sparse integer duplicates | 53.07 / 74.37 / 74.37 | 5 / 0 / 0 |
   
   Both sparse integer cases consistently succeed on A but return 
`ResourcesExhausted` on B and C under the same memory-pool limit.
   
   The counting-sort patch restores the baseline pool watermark for the dense 
cases, but the non-dense resource regression remains.
   
   The current experiment does not isolate the individual contributions of the 
whole-build hash buffer and the group-builder reservations.
   
   [Memory report, fixtures, raw measurements and failure 
evidence](https://github.com/rusanoph/unchained-hash-join-research/blob/ab74b02eff94fd7164538bf54eb990a7e99ca6a8/datafusion/upstream-validation/pr-27746/reports/memory-analysis.md)
   
   If this is intended to be addressed in a follow-up PR, I'd be interested in 
helping with the regression tests or investigating the allocation overhead 
further.



-- 
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]

Reply via email to