mbutrovich commented on code in PR #27559:
URL: https://github.com/apache/datafusion/pull/27559#discussion_r4776385161
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -305,23 +305,112 @@ fn hash_array_primitive<T>(
"hashes_buffer and array should be of equal length"
);
- if array.null_count() == 0 {
+ 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) => hash_prim_with_nulls(
+ array.values(),
+ nulls,
+ hashes_buffer,
+ random_state,
+ rehash,
+ ),
+ }
+}
+
+/// 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>(
+ values: &[N],
+ nulls: &NullBuffer,
+ hashes: &mut [u64],
+ state: &S,
+ rehash: bool,
+) 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 {
Review Comment:
> A cheaper option to try on the bot is skipping all-null chunks before
`count_ones`.
Thanks @jayzhan211! I filed #27974 to try it after this merges.
--
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]