Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip invisible row for chunk::get_hash_values #15696

Merged
merged 7 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/batch/src/task/hash_shuffle_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ fn generate_hash_values(chunk: &DataChunk, hash_info: &HashInfo) -> BatchResult<
.map(|idx| *idx as usize)
.collect::<Vec<_>>(),
hasher_builder,
// TODO(st1page): https://github.com/risingwavelabs/risingwave/issues/15705
false,
)
.iter_mut()
.map(|hash_value| hash_value.value() as usize % output_count)
Expand Down
2 changes: 1 addition & 1 deletion src/common/src/array/compact_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl StreamChunkCompactor {
.map(|c| {
let hash_values = c
.data_chunk()
.get_hash_values(&key_indices, Crc32FastBuilder)
.get_hash_values(&key_indices, Crc32FastBuilder, true)
.into_iter()
.map(|hash| hash.value())
.collect_vec();
Expand Down
15 changes: 11 additions & 4 deletions src/common/src/array/data_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,25 @@ impl DataChunk {
Ok(outputs)
}

/// Compute hash values for each row.
/// Compute hash values for each row. The number of the returning `HashCodes` is `self.capacity()`.
/// When `skip_invisible_row` is true, the `HashCode` for the invisible rows is arbitrary.
st1page marked this conversation as resolved.
Show resolved Hide resolved
pub fn get_hash_values<H: BuildHasher>(
&self,
column_idxes: &[usize],
hasher_builder: H,
skip_invisible_row: bool,
st1page marked this conversation as resolved.
Show resolved Hide resolved
) -> Vec<HashCode<H>> {
let mut states = Vec::with_capacity(self.capacity());
states.resize_with(self.capacity(), || hasher_builder.build_hasher());
let len = self.capacity();
let mut states = Vec::with_capacity(len);
states.resize_with(len, || hasher_builder.build_hasher());
// Compute hash for the specified columns.
for column_idx in column_idxes {
let array = self.column_at(*column_idx);
array.hash_vec(&mut states[..]);
if skip_invisible_row {
array.hash_vec(&mut states[..], self.visibility());
} else {
array.hash_vec(&mut states[..], &Bitmap::ones(len));
}
}
finalize_hashers(&states[..])
.into_iter()
Expand Down
14 changes: 8 additions & 6 deletions src/common/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ pub trait Array:
}
}

fn hash_vec<H: Hasher>(&self, hashers: &mut [H]) {
fn hash_vec<H: Hasher>(&self, hashers: &mut [H], vis: &Bitmap) {
assert_eq!(hashers.len(), self.len());
for (idx, state) in hashers.iter_mut().enumerate() {
self.hash_at(idx, state);
for idx in vis.iter_ones() {
self.hash_at(idx, &mut hashers[idx]);
}
}

Expand Down Expand Up @@ -554,8 +554,8 @@ impl ArrayImpl {
dispatch_array_variants!(self, inner, { inner.hash_at(idx, state) })
}

pub fn hash_vec<H: Hasher>(&self, hashers: &mut [H]) {
dispatch_array_variants!(self, inner, { inner.hash_vec(hashers) })
pub fn hash_vec<H: Hasher>(&self, hashers: &mut [H], vis: &Bitmap) {
dispatch_array_variants!(self, inner, { inner.hash_vec(hashers, vis) })
}

/// Select some elements from `Array` based on `visibility` bitmap.
Expand Down Expand Up @@ -711,6 +711,7 @@ mod test_util {
use std::hash::{BuildHasher, Hasher};

use super::Array;
use crate::buffer::Bitmap;
use crate::util::iter_util::ZipEqFast;

pub fn hash_finish<H: Hasher>(hashers: &[H]) -> Vec<u64> {
Expand All @@ -732,8 +733,9 @@ mod test_util {
arr.hash_at(i, state)
}
});
let vis = Bitmap::ones(len);
arrs.iter()
.for_each(|arr| arr.hash_vec(&mut states_vec[..]));
.for_each(|arr| arr.hash_vec(&mut states_vec[..], &vis));
itertools::cons_tuples(
expects
.iter()
Expand Down
3 changes: 2 additions & 1 deletion src/common/src/hash/consistent_hash/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ impl VirtualNode {
}

data_chunk
.get_hash_values(keys, Crc32FastBuilder)
// TODO(st1page): https://github.com/risingwavelabs/risingwave/issues/15705
.get_hash_values(keys, Crc32FastBuilder, false)
.into_iter()
.map(|hash| hash.into())
.collect()
Expand Down
2 changes: 1 addition & 1 deletion src/common/src/hash/key_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl<S: KeyStorage, N: NullBitmap> HashKey for HashKeyImpl<S, N> {
type Bitmap = N;

fn build_many(column_indices: &[usize], data_chunk: &DataChunk) -> Vec<Self> {
let hash_codes = data_chunk.get_hash_values(column_indices, XxHash64Builder);
let hash_codes = data_chunk.get_hash_values(column_indices, XxHash64Builder, true);

let mut serializers = {
let buffers = if S::Buffer::alloc() {
Expand Down
Loading