kumarUjjawal commented on code in PR #27025:
URL: https://github.com/apache/datafusion/pull/27025#discussion_r4667544363
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -98,6 +98,38 @@ pub fn combine_hashes(l: u64, r: u64) -> u64 {
/// to this size.
const MAX_BUFFER_SIZE: usize = 524_288;
+#[cfg(not(feature = "force_hash_collisions"))]
+fn dictionary_nulls(array: &dyn AnyDictionaryArray) -> (bool, bool) {
+ (
+ array.keys().null_count() != 0,
+ array.values().null_count() != 0,
+ )
+}
+
+#[cfg(not(feature = "force_hash_collisions"))]
+fn dictionary_overwrites_hash_buffer(array: &dyn Array) -> bool {
+ let Some(array) = array.as_any_dictionary_opt() else {
+ return false;
+ };
+ let (has_null_keys, has_null_values) = dictionary_nulls(array);
+ if has_null_keys {
+ return false;
+ }
+ if !has_null_values {
+ return true;
+ }
+
+ // Oversized dictionaries are compacted before scattering. Only inspect
+ // logical nulls when compaction can remove all unreferenced null values.
+ array.values().len() > MAX_DICTIONARY_VALUES_PER_ROW * array.len()
Review Comment:
No test covers the new `logical_null_count() == 0` guard on the
oversized-dictionary reuse branch. Deleting it, which lets `with_hashes` skip
`clear()` when a key references a null value, would not fail any test.**
Failure scenario: Edit line 125 to drop `&& array.logical_null_count() ==
0`, then run `with_hashes` on a 1-row dictionary with key [0] and values [None,
...65 strings] after `dirty_hash_buffer`. The gate returns true and the buffer
is not cleared. The compacted dictionary still has a null value, so the
null-values scatter skips that row, and the callback receives u64::MAX instead
of the null hash. The only oversized test
(`test_dictionary_with_unreferenced_null_reuses_buffer`) references a non-null
value. The `null_values` case in `assert_matches` is not oversized (3 values <=
64*11 rows), so the `&&` short-circuits before reaching the check. Nothing
fails.
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -623,8 +701,12 @@ fn dispatch_dictionary_scatter<K: ArrowDictionaryKeyType>(
hashes_buffer: &mut [u64],
multi_col: bool,
) {
- let has_null_keys = array.keys().null_count() != 0;
- let has_null_values = array.values().null_count() != 0;
+ // `create_hashes` hashes the prefix selected by the output buffer length.
+ assert!(
Review Comment:
The dispatcher still asserts only `<=`, so dictionaries alone accept a
shorter buffer and hash just a prefix.
Failure scenario: `create_hashes([&dict], rs, &mut vec![0; dict.len() - 1])`
succeeds and hashes a prefix. The same call with an Int32 or Utf8 column panics
in `hash_array_primitive`/`hash_array`. The test at the `short` buffer case
(`check_non_null_dictionary_hash_fast_path`) locks in this dictionary-only
leniency, so the inconsistent contract the reviewer flagged is still there.
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -98,6 +98,38 @@ pub fn combine_hashes(l: u64, r: u64) -> u64 {
/// to this size.
const MAX_BUFFER_SIZE: usize = 524_288;
+#[cfg(not(feature = "force_hash_collisions"))]
+fn dictionary_nulls(array: &dyn AnyDictionaryArray) -> (bool, bool) {
+ (
+ array.keys().null_count() != 0,
+ array.values().null_count() != 0,
+ )
+}
+
+#[cfg(not(feature = "force_hash_collisions"))]
+fn dictionary_overwrites_hash_buffer(array: &dyn Array) -> bool {
+ let Some(array) = array.as_any_dictionary_opt() else {
+ return false;
+ };
+ let (has_null_keys, has_null_values) = dictionary_nulls(array);
+ if has_null_keys {
+ return false;
+ }
+ if !has_null_values {
+ return true;
+ }
+
+ // Oversized dictionaries are compacted before scattering. Only inspect
+ // logical nulls when compaction can remove all unreferenced null values.
+ array.values().len() > MAX_DICTIONARY_VALUES_PER_ROW * array.len()
+ && array.logical_null_count() == 0
Review Comment:
`logical_null_count()` builds a full validity bitmap for the dictionary on
every `with_hashes` call just to get one boolean, and for nested-dictionary
values it walks every value.
Failure scenario: Take a sliced 8192-row dictionary whose values array has
nulls and holds about 1M entries, which is the oversized case this branch
targets. `DictionaryArray::logical_nulls` allocates a BooleanBufferBuilder for
all rows and probes every key, and `compact_oversized_dictionary` then repeats
an O(rows) pass. If the values are themselves a dictionary with null keys,
`values.logical_nulls()` iterates all ~1M inner keys per batch, the very work
compaction exists to avoid. This can cost more than the 64KB memset that
skipping `clear()` saves.
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -98,6 +98,38 @@ pub fn combine_hashes(l: u64, r: u64) -> u64 {
/// to this size.
const MAX_BUFFER_SIZE: usize = 524_288;
+#[cfg(not(feature = "force_hash_collisions"))]
+fn dictionary_nulls(array: &dyn AnyDictionaryArray) -> (bool, bool) {
+ (
+ array.keys().null_count() != 0,
+ array.values().null_count() != 0,
+ )
+}
+
+#[cfg(not(feature = "force_hash_collisions"))]
+fn dictionary_overwrites_hash_buffer(array: &dyn Array) -> bool {
+ let Some(array) = array.as_any_dictionary_opt() else {
+ return false;
+ };
+ let (has_null_keys, has_null_values) = dictionary_nulls(array);
+ if has_null_keys {
+ return false;
+ }
+ if !has_null_values {
+ return true;
+ }
+
+ // Oversized dictionaries are compacted before scattering. Only inspect
+ // logical nulls when compaction can remove all unreferenced null values.
+ array.values().len() > MAX_DICTIONARY_VALUES_PER_ROW * array.len()
+ && array.logical_null_count() == 0
Review Comment:
The reuse gate again uses a logical null test
(`array.logical_null_count()`), while the scatter dispatch uses physical
`null_count` (`dictionary_nulls`).
Failure scenario: Whether `with_hashes` may skip `clear()` again depends on
two different null predicates staying consistent: logical nulls of the
uncompacted dictionary versus physical `null_count` of the compacted one. A
later change to arrow's `logical_nulls` for some value type, or to
`compact_dictionary`, can make the gate return true while dispatch takes a null
path that leaves slots unwritten. Callers would then get the previous batch's
hashes. This is the same coupling kumarUjjawal flagged.
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -544,6 +582,42 @@ fn hash_generic_byte_view_array<T: ByteViewType>(
}
}
+/// Scatters non-null dictionary hashes in independent lanes to overlap
key-indexed loads.
+#[cfg(not(feature = "force_hash_collisions"))]
+#[inline(always)]
+fn scatter_non_null_dictionary<K: ArrowDictionaryKeyType, const MULTI_COL:
bool>(
+ array: &DictionaryArray<K>,
+ dict_hashes: &[u64],
+ hashes: &mut [u64],
+) {
+ const LANES: usize = 8;
+ let keys = array.keys().values();
+ let keys = &keys[..hashes.len()];
+
+ let (key_chunks, key_tail) = keys.as_chunks::<LANES>();
+ let (hash_chunks, hash_tail) = hashes.as_chunks_mut::<LANES>();
+ for (keys, hashes) in key_chunks.iter().zip(hash_chunks) {
+ let gathered: [u64; LANES] =
+ std::array::from_fn(|lane| dict_hashes[keys[lane].as_usize()]);
+ if MULTI_COL {
+ for (hash, dict_hash) in hashes.iter_mut().zip(gathered) {
+ *hash = combine_hashes(dict_hash, *hash);
+ }
+ } else {
+ *hashes = gathered;
+ }
+ }
+
+ for (hash, key) in hash_tail.iter_mut().zip(key_tail) {
Review Comment:
The tail loop again copy-pastes the `if MULTI_COL {
combine_hashes(dict_hash, *hash) } else { dict_hash }` rule, and `scatter_row`
holds a third copy (lines 649-653).
Failure scenario: The combine rule now lives in three places: the chunk
body, the tail, and `scatter_row`. If one copy changes, for example by swapping
`combine_hashes` argument order, rows in the 8-lane chunks, the tail rows, and
dictionaries with nulls would hash equal values differently. Hash joins and
repartitioning would silently mismatch rows.
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -98,6 +98,38 @@ pub fn combine_hashes(l: u64, r: u64) -> u64 {
/// to this size.
const MAX_BUFFER_SIZE: usize = 524_288;
+#[cfg(not(feature = "force_hash_collisions"))]
+fn dictionary_nulls(array: &dyn AnyDictionaryArray) -> (bool, bool) {
+ (
+ array.keys().null_count() != 0,
+ array.values().null_count() != 0,
+ )
+}
+
+#[cfg(not(feature = "force_hash_collisions"))]
+fn dictionary_overwrites_hash_buffer(array: &dyn Array) -> bool {
+ let Some(array) = array.as_any_dictionary_opt() else {
+ return false;
+ };
+ let (has_null_keys, has_null_values) = dictionary_nulls(array);
+ if has_null_keys {
+ return false;
+ }
+ if !has_null_values {
+ return true;
+ }
+
+ // Oversized dictionaries are compacted before scattering. Only inspect
+ // logical nulls when compaction can remove all unreferenced null values.
+ array.values().len() > MAX_DICTIONARY_VALUES_PER_ROW * array.len()
Review Comment:
The gate copies `compact_oversized_dictionary`'s threshold (`values.len() >
MAX_DICTIONARY_VALUES_PER_ROW * len`) even though correctness does not need it.
Failure scenario: When there are no null keys and `logical_null_count() ==
0`, the null-values scatter writes every row anyway, because every referenced
value is valid. Compaction is therefore not what makes reuse safe, and the
comment on lines 122-123 is misleading. Meanwhile a filtered dictionary with
100 rows and 200 values, one unreferenced null, is not oversized, so it is
still cleared and takes the slower null-values path. The copied threshold also
breaks silently if `compact_oversized_dictionary` changes, for example to the
`saturating_mul` form used in multi_group_by.
##########
datafusion/common/src/hash_utils.rs:
##########
Review Comment:
https://github.com/apache/datafusion/blob/286f893ce3d0117f3f4e4861314b901040b9d351/datafusion/common/src/hash_utils.rs#L777
`scatter_dictionary_hashes`, a public internal API that returns `Result`,
now panics in release builds through the new dispatcher `assert!` when
`hashes_buffer` is longer than the array. Before this change it silently
stopped at the last key, and it already returns an internal error for a wrong
`dict_hashes` length.
Failure scenario: A workspace caller passes a `hashes` slice sized to a
batch longer than the dictionary column. Before this PR the zip stopped at
`keys.len()`; now the whole aggregation thread panics with 'dictionary scatter
requires at least one key per hash slot' instead of getting an `_internal_err!`
it could propagate. This is a behavior change to a `pub` item that the PR
description does not mention.
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -150,18 +182,24 @@ where
// Peek at the first array to determine buffer size without fully
collecting
let mut iter = arrays.into_iter().peekable();
- // Get the required size from the first array
- let required_size = match iter.peek() {
- Some(arr) => arr.as_dyn_array().len(),
+ // Get the required size and initialization policy from the first array
Review Comment:
we can trim the comments
--
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]