mbutrovich commented on code in PR #27746:
URL: https://github.com/apache/datafusion/pull/27746#discussion_r4786921847
##########
datafusion/physical-plan/src/joins/join_index/directory/general.rs:
##########
@@ -0,0 +1,336 @@
+// 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
+
+//! General-key directory with batched comparison of candidate representatives.
+//!
+//! A hash match is only a candidate. Compare representatives column by column,
+//! once per key rather than per duplicate. If `A` and `B` share a hash,
rejecting
+//! representative `A` must retry the directory: another entry may hold `B`.
+//! Both paths resolve candidates into handles. Unique builds convert these to
+//! row pairs at return; duplicate keys retain handles for later expansion.
+
+use std::mem::size_of;
+use std::sync::Arc;
+
+use arrow::array::ArrayRef;
+use arrow::buffer::NullBuffer;
+use arrow::compute::SortOptions;
+use datafusion_common::float_semantics::{has_float_leaf, normalize};
+use datafusion_common::utils::memory::estimate_memory_size;
+use datafusion_common::{NullEquality, Result};
+use datafusion_execution::memory_pool::MemoryReservation;
+use hashbrown::{HashTable, hash_table::Entry};
+
+use super::super::keys::{LookupScratch, compare_column, hash_probe_keys};
+use super::super::matches::{
+ HandleBuilder, JoinHashLookup, MatchHandle, MatchedGroupsBuilder, RowId,
+};
+use super::super::reserve;
+use super::Probe;
+use crate::joins::utils::{JoinKeyComparator, matchable_join_keys};
+
+/// One directory entry per equal join key(s).
+///
+/// See [`JoinHashIndex`](super::JoinHashIndex) for the design overview.
+///
+/// # Example
+///
+/// Build keys `[A, B, A]`, with zero-based row IDs, produce:
+///
+/// ```text
+/// key | hash | representative_row | group_plus_one | matches()
+/// ----+------+--------------------+----------------+------------------------
+/// A | h(A) | 0 | 1 | Group(0) -> rows [0, 2]
+/// B | h(B) | 1 | 0 | One(1) -> row 1
+/// ```
+///
+/// `h(key)` is the key's hash. `representative_row` indexes the concatenated
+/// build batch, letting lookup read the actual key to verify equality.
+/// Duplicate members are stored separately; `group_plus_one = 1` refers to
+/// group 0, while zero means the representative is the only matching row.
+#[derive(Clone, Copy, Debug)]
+struct KeyEntry<T> {
+ hash: u64,
+ representative_row: T,
+ // Zero means One(representative_row); otherwise this is Group(id - 1).
+ group_plus_one: T,
+}
+
+impl<T: RowId> KeyEntry<T> {
+ fn matches(&self) -> MatchHandle<T> {
+ match self.group_plus_one.as_index() {
+ 0 => MatchHandle::One(self.representative_row),
+ group => MatchHandle::Group(T::from_index(group - 1)),
+ }
+ }
+}
+
+/// Hash candidates identify representative build rows whose keys establish
equality.
+///
+/// See [`JoinHashIndex`](super::JoinHashIndex) for the design overview.
+#[derive(Debug)]
+pub(super) struct GeneralDirectory<T> {
+ table: HashTable<KeyEntry<T>>,
+ /// Every matchable build key has at most one row, including NULL.
+ build_unique: bool,
+}
+
+impl<T: RowId> GeneralDirectory<T> {
+ /// Only exact matches share a group, even when different keys have the
same hash.
+ pub(super) fn build(
+ keys: &[ArrayRef],
+ hashes: &[u64],
+ valid: Option<&NullBuffer>,
+ reservation: &MemoryReservation,
+ groups: &mut MatchedGroupsBuilder<T>,
+ ) -> Result<Self> {
+ reservation.try_grow(estimate_memory_size::<KeyEntry<T>>(
+ hashes.len(),
+ size_of::<HashTable<KeyEntry<T>>>(),
+ )?)?;
+ let mut table: HashTable<KeyEntry<T>> =
HashTable::with_capacity(hashes.len());
+ // NULL != NULL rows are excluded before comparison. Using NULL
+ // equality here avoids rebuilding dictionary logical-null masks;
+ // it does not change equality of nested children.
+ let comparator = JoinKeyComparator::new_with_normalized_left(
+ keys,
+ keys,
+ &vec![SortOptions::default(); keys.len()],
+ NullEquality::NullEqualsNull,
+ )?;
+ for (row, &hash) in hashes.iter().enumerate() {
+ if valid.is_some_and(|valid| valid.is_null(row)) {
+ continue;
+ }
+ match table.entry(
+ hash,
+ |entry| {
+ entry.hash == hash
+ &&
comparator.is_equal(entry.representative_row.as_index(), row)
+ },
+ |entry| entry.hash,
+ ) {
+ Entry::Occupied(mut entry) => {
+ let entry = entry.get_mut();
+ let mut handle = entry.matches();
+ groups.add_duplicate(&mut handle, T::from_index(row));
+ let MatchHandle::Group(group) = handle else {
+ unreachable!()
+ };
+ entry.group_plus_one = T::from_index(group.as_index() + 1);
+ }
+ Entry::Vacant(entry) => {
+ let row = T::from_index(row);
+ entry.insert(KeyEntry {
+ hash,
+ representative_row: row,
+ group_plus_one: T::default(),
+ });
+ }
+ }
+ }
Review Comment:
> The `hj` benchmark is extended as suggested in
https://github.com/apache/datafusion/pull/27766
>
> The result is approximately `main: 20ms, PR: 23ms` on my Mac.
Thanks for writing Q29. #27766 hasn't merged yet, so the bot runs in #27857
don't include it. That means "No reproducible regressions ... in HJ" in the
description doesn't cover the one query built to show this cost. Once #27766 is
in, could you run the `hj` bot here and list Q29 next to the IMDB regressions?
--
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]