Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions datafusion/common/src/hash_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,51 @@ fn dispatch_dictionary_scatter<K: ArrowDictionaryKeyType>(
}
}

/// Scatters precomputed dictionary value hashes through `array`'s keys.
///
/// # Public Only for Internal Use:
///
/// This is not a public API and is for internal use only; see [API policy] for details.
///
/// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
#[doc(hidden)]
#[cfg(not(feature = "force_hash_collisions"))]
pub fn scatter_dictionary_hashes<K: ArrowDictionaryKeyType>(
array: &DictionaryArray<K>,
dict_hashes: &[u64],
hashes_buffer: &mut [u64],
multi_col: bool,
) -> Result<()> {
if dict_hashes.len() != array.values().len() {
return _internal_err!(
"Expected {} dictionary hashes, got {}",
array.values().len(),
dict_hashes.len()
);
}
dispatch_dictionary_scatter(array, dict_hashes, hashes_buffer, multi_col);
Ok(())
}

/// Hashes one column into `hashes_buffer` exactly as [`create_hashes`] does
/// for that column; `rehash` is `true` for every column after the first.
///
/// # Public Only for Internal Use:
///
/// This is not a public API and is for internal use only; see [API policy] for details.
///
/// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
#[doc(hidden)]
#[cfg(not(feature = "force_hash_collisions"))]
pub fn hash_column(
array: &dyn Array,
random_state: &impl HashState,
hashes_buffer: &mut [u64],
rehash: bool,
) -> Result<()> {
hash_single_array(array, random_state, hashes_buffer, rehash)
}

/// Hash the values in a dictionary array.
#[cfg(not(feature = "force_hash_collisions"))]
fn hash_dictionary<K: ArrowDictionaryKeyType>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub struct DictionaryGroupValuesColumn<K: ArrowDictionaryKeyType + Send + Sync>
/// were built for. A matching live array reuses both caches without
/// retaining an input dictionary after its batch is released.
cached_values: Option<Weak<dyn Array>>,
/// Whether the outer table guarantees each append creates a new group, so
/// inner slots and group indices are identical.
values_are_group_unique: bool,
#[cfg(test)]
hash_builds: usize,
_phantom: PhantomData<K>,
}

Expand All @@ -71,6 +76,9 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> DictionaryGroupValuesColumn<K> {
val_to_inner: Vec::default(),
val_hashes: Vec::default(),
cached_values: None,
values_are_group_unique: false,
#[cfg(test)]
hash_builds: 0,
_phantom: PhantomData,
}
}
Expand Down Expand Up @@ -157,6 +165,10 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> DictionaryGroupValuesColumn<K> {
}

fn hash_values(&mut self, values: &ArrayRef) {
#[cfg(test)]
{
self.hash_builds += 1;
}
self.cached_values = None;
self.val_hashes.clear();
self.val_hashes.resize(values.len(), 0);
Expand All @@ -171,7 +183,8 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> DictionaryGroupValuesColumn<K> {
fn is_cached(&self, values: &ArrayRef) -> bool {
self.cached_values
.as_ref()
.is_some_and(|cached| std::ptr::addr_eq(cached.as_ptr(), Arc::as_ptr(values)))
.and_then(Weak::upgrade)
.is_some_and(|cached| Arc::ptr_eq(&cached, values))
}

/// Makes `val_hashes` and `val_to_inner` correspond to `dict_values`,
Expand All @@ -192,6 +205,17 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> DictionaryGroupValuesColumn<K> {
self.cached_values = Some(Arc::downgrade(dict_values));
}

fn insert_value(&mut self, dict_values: &ArrayRef, val_idx: usize) -> Result<usize> {
if self.values_are_group_unique {
let slot = self.group_to_inner.len();
debug_assert_eq!(slot, self.inner.len());
self.inner.append_val(dict_values, val_idx)?;
Ok(slot)
} else {
self.find_or_insert_value(dict_values, val_idx, self.val_hashes[val_idx])
}
}

fn find_or_insert_value(
&mut self,
dict_values: &ArrayRef,
Expand Down Expand Up @@ -331,13 +355,61 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> GroupColumn
Some(val_idx) => {
let dict_values = dict.values();
self.sync_value_cache(dict_values);
self.find_or_insert_value(dict_values, val_idx, self.val_hashes[val_idx])?
self.insert_value(dict_values, val_idx)?
}
};
self.group_to_inner.push(inner_slot);
self.check_key_overflow()
}

fn dictionary_hashes<'a>(
&'a mut self,
values: &ArrayRef,
group_unique: bool,
) -> Option<&'a [u64]> {
self.values_are_group_unique = group_unique;
self.sync_value_cache(values);
Some(&self.val_hashes)
}

fn try_map_dictionary_groups(
&self,
array: &ArrayRef,
groups: &mut Vec<usize>,
) -> bool {
if !self.values_are_group_unique {
return false;
}

let dictionary = array.as_dictionary::<K>();
if !self.is_cached(dictionary.values()) {
return false;
}

let keys = dictionary.keys();
let raw_keys = keys.values();
let nulls = keys.nulls();
groups.clear();
for row in 0..dictionary.len() {
let group = if nulls.is_some_and(|nulls| nulls.is_null(row)) {
self.null_inner_slot.unwrap_or(usize::MAX)
} else {
self.val_to_inner[raw_keys[row].as_usize()]
};
if group == usize::MAX {
Comment thread
Rich-T-kid marked this conversation as resolved.
Resolved
groups.clear();
return false;
}
groups.push(group);
}
true
}

#[cfg(test)]
fn dictionary_hash_builds(&self) -> usize {
self.hash_builds
}

fn vectorized_equal_to(
&mut self,
lhs_rows: &[usize],
Expand Down Expand Up @@ -452,11 +524,7 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> GroupColumn
self.val_to_inner[val_idx] = if dict_values.is_null(val_idx) {
self.find_or_insert_null()?
} else {
self.find_or_insert_value(
dict_values,
val_idx,
self.val_hashes[val_idx],
)?
self.insert_value(dict_values, val_idx)?
};
}
self.group_to_inner.push(self.val_to_inner[val_idx]);
Expand All @@ -472,11 +540,7 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> GroupColumn
self.val_to_inner[val_idx] = if dict_values.is_null(val_idx) {
self.find_or_insert_null()?
} else {
self.find_or_insert_value(
dict_values,
val_idx,
self.val_hashes[val_idx],
)?
self.insert_value(dict_values, val_idx)?
};
}
self.val_to_inner[val_idx]
Expand Down Expand Up @@ -571,34 +635,28 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> GroupColumn
let emitted: ArrayRef =
Arc::new(DictionaryArray::<K>::new(emitted_keys, compact_emit_values));

// Null deferred to last so null_inner_slot is always the highest index
// and check_key_overflow can subtract it without a false overflow.
// Preserve group order so inner slots remain equal to group indices
// when this is the only grouping column.
let remaining = self.group_to_inner[n..].to_vec();
let mut old_to_new = vec![usize::MAX; old_inner_len];
let mut new_to_old: Vec<usize> = Vec::new();
let mut null_old_slot: Option<usize> = None;
for &old in &remaining {
if all_inner_values.is_null(old) {
if null_old_slot.is_none() {
null_old_slot = Some(old);
}
continue;
}
if old_to_new[old] == usize::MAX {
old_to_new[old] = new_to_old.len();
new_to_old.push(old);
}
}
if let Some(old) = null_old_slot {
old_to_new[old] = new_to_old.len();
new_to_old.push(old);
}

self.value_dedup = HashTable::new();
self.value_dedup_size = 0;
self.null_inner_slot = None;

self.hash_values(&all_inner_values);
if self.values_are_group_unique {
Comment thread
Rich-T-kid marked this conversation as resolved.
Resolved
self.cached_values = None;
self.val_hashes.clear();
} else {
self.hash_values(&all_inner_values);
}

for (new_slot, &old_slot) in new_to_old.iter().enumerate() {
if all_inner_values.is_null(old_slot) {
Expand All @@ -610,11 +668,13 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> GroupColumn
self.inner
.append_val(&all_inner_values, old_slot)
.expect("append value failed in take_n");
self.value_dedup.insert_accounted(
(self.val_hashes[old_slot], new_slot),
|&(entry_hash, _)| entry_hash,
&mut self.value_dedup_size,
);
if !self.values_are_group_unique {
self.value_dedup.insert_accounted(
(self.val_hashes[old_slot], new_slot),
|&(entry_hash, _)| entry_hash,
&mut self.value_dedup_size,
);
}
}
}

Expand Down
Loading
Loading