mbutrovich commented on code in PR #27559:
URL: https://github.com/apache/datafusion/pull/27559#discussion_r4776386949
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -305,23 +305,115 @@ fn hash_array_primitive<T>(
"hashes_buffer and array should be of equal length"
);
- if array.null_count() == 0 {
- if rehash {
- hash_prim_rehash_dense(array.values(), hashes_buffer,
random_state);
+ match array.nulls().filter(|nulls| nulls.null_count() > 0) {
+ None if rehash => {
+ hash_prim_rehash_dense(array.values(), hashes_buffer, random_state)
+ }
+ None => hash_prim_fresh_dense(array.values(), hashes_buffer,
random_state),
+ // Null rows keep their previous hash, so an all-null array, such as a
+ // column that ROLLUP or GROUPING SETS nulls out, changes nothing
+ Some(nulls) if nulls.null_count() == nulls.len() => {}
+ Some(nulls) if rehash => hash_prim_with_nulls::<_, _, true>(
+ array.values(),
+ nulls,
+ hashes_buffer,
+ random_state,
+ ),
+ Some(nulls) => hash_prim_with_nulls::<_, _, false>(
+ array.values(),
+ nulls,
+ hashes_buffer,
+ random_state,
+ ),
+ }
+}
+
+/// The positions of the set bits of `bits`, lowest first
+#[cfg(not(feature = "force_hash_collisions"))]
+fn set_bits(mut bits: u64) -> impl Iterator<Item = usize> {
+ std::iter::from_fn(move || {
+ (bits != 0).then(|| {
+ let i = bits.trailing_zeros() as usize;
+ bits &= bits - 1;
+ i
+ })
+ })
+}
+
+/// The fewest valid rows in a 64-row chunk for which hashing every row of the
+/// chunk and restoring its null rows costs less than hashing only its valid
rows
+/// one at a time.
+/// The null densities in the `with_hashes` benchmark cluster around where
random
+/// chunks cross this count, so rerun it when changing the value.
+#[cfg(not(feature = "force_hash_collisions"))]
+const MIN_VALID_FOR_DENSE: u32 = 48;
+
+/// Hashes 64 rows at a time. A chunk with at least [`MIN_VALID_FOR_DENSE`]
+/// valid rows is hashed in place with the dense loops, which have no branch
per
+/// row, and the previous hashes of its null rows are saved before and written
+/// back after. A sparser chunk, and the last `len % 64` rows, hash only their
+/// valid rows.
+#[cfg(not(feature = "force_hash_collisions"))]
+fn hash_prim_with_nulls<N, S, const REHASH: bool>(
+ values: &[N],
+ nulls: &NullBuffer,
+ hashes: &mut [u64],
+ state: &S,
+) where
+ N: HashValue + Copy,
+ S: HashState,
+{
+ let chunks = nulls.inner().bit_chunks();
+ let (value_chunks, value_tail) = values.as_chunks::<64>();
+ let (hash_chunks, hash_tail) = hashes.as_chunks_mut::<64>();
+ let rows = value_chunks.iter().zip(hash_chunks.iter_mut());
+ for ((values, hashes), valid) in rows.zip(chunks.iter()) {
+ if valid.count_ones() < MIN_VALID_FOR_DENSE {
+ hash_prim_valid::<_, _, REHASH>(values, hashes, valid, state);
+ continue;
+ }
+ // The check above leaves at most `64 - MIN_VALID_FOR_DENSE` null rows,
+ // so `kept` has room for every one of them
+ let mut kept = [0u64; 64 - MIN_VALID_FOR_DENSE as usize];
+ for (kept, i) in kept.iter_mut().zip(set_bits(!valid)) {
+ *kept = hashes[i];
+ }
+ if REHASH {
+ hash_prim_rehash_dense(values, hashes, state);
} else {
- hash_prim_fresh_dense(array.values(), hashes_buffer, random_state);
+ hash_prim_fresh_dense(values, hashes, state);
Review Comment:
> Could you file an issue for the slow loop with `x86-64-v3` and link it
here?
Filed #27973.
--
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]