Skip to content

Commit 8ce1415

Browse files
committed
(improvement) remove unwrap from try_from when deserializing partial from bytes, and use constant for block_size
1 parent e204c79 commit 8ce1415

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

vortex-layout/src/layouts/zoned/aggregates/bloom_filter/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use vortex_array::aggregate_fn::AggregateFnVTable;
1717
use vortex_array::dtype::DType;
1818
use vortex_array::dtype::Nullability;
1919
use vortex_array::scalar::Scalar;
20+
use vortex_error::VortexExpect;
2021
use vortex_error::VortexResult;
2122
use vortex_error::vortex_ensure_eq;
2223
use vortex_error::vortex_err;
@@ -29,6 +30,7 @@ mod partial;
2930
pub(in crate::layouts::zoned) mod constant;
3031
pub use partial::BloomPartial;
3132

33+
use crate::layouts::zoned::aggregates::bloom_filter::partial::BLOCK_SIZE;
3234
use crate::layouts::zoned::skip_index::bloom::is_bloom_valid_dtype;
3335

3436
// 1. (joacoc) Opted for blocks_count as a simpler way to tune
@@ -82,7 +84,8 @@ const DEFAULT_BLOCKS_COUNT: usize = 256;
8284
impl Default for BloomOptions {
8385
fn default() -> Self {
8486
Self {
85-
blocks_count: NonZeroUsize::new(DEFAULT_BLOCKS_COUNT).expect("valid blocks size"),
87+
blocks_count: NonZeroUsize::new(DEFAULT_BLOCKS_COUNT)
88+
.vortex_expect("valid blocks size"),
8689
}
8790
}
8891
}
@@ -184,7 +187,7 @@ impl AggregateFnVTable for BloomFilter {
184187
///
185188
/// Basically turns each block into a single byte sequence.
186189
fn to_scalar(&self, partial: &Self::Partial) -> VortexResult<Scalar> {
187-
let mut bytes = Vec::with_capacity(partial.blocks.len() * 8 * size_of::<u32>());
190+
let mut bytes = Vec::with_capacity(partial.blocks.len() * BLOCK_SIZE);
188191
bytes.extend(
189192
partial
190193
.blocks
@@ -239,6 +242,7 @@ pub(in crate::layouts::zoned::aggregates::bloom_filter) mod test_utils {
239242
use vortex_error::vortex_ensure;
240243

241244
use super::*;
245+
use crate::layouts::zoned::aggregates::bloom_filter::partial::BLOCK_SIZE;
242246

243247
pub fn setup() -> VortexResult<ExecutionCtx> {
244248
let session = vortex_array::array_session();
@@ -255,10 +259,7 @@ pub(in crate::layouts::zoned::aggregates::bloom_filter) mod test_utils {
255259

256260
pub fn extract_bloom_blocks(state: &Scalar) -> VortexResult<Vec<[u32; 8]>> {
257261
let bytes = state.as_binary().value().expect("bloom state is non-null");
258-
vortex_ensure!(
259-
bytes.len() % (8 * size_of::<u32>()) == 0,
260-
"invalid bloom state length"
261-
);
262+
vortex_ensure!(bytes.len() % BLOCK_SIZE == 0, "invalid bloom state length");
262263
let mut blocks = Vec::with_capacity(bytes.len() / 32);
263264
for block_bytes in bytes.chunks_exact(32) {
264265
let mut block = [0u32; 8];

vortex-layout/src/layouts/zoned/aggregates/bloom_filter/partial.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use vortex_error::vortex_ensure;
2121
use vortex_error::vortex_err;
2222

2323
/// Block size in bits (8 * 4 = 32 bits)
24-
const BLOCK_SIZE: usize = 8 * size_of::<u32>();
24+
pub(super) const BLOCK_SIZE: usize = 8 * size_of::<u32>();
2525

2626
/// Represents a Split block Bloom Filter filter for a single layout zone.
2727
pub struct BloomPartial {
@@ -251,12 +251,15 @@ impl TryFrom<&[u8]> for BloomPartial {
251251
.chunks_exact(BLOCK_SIZE)
252252
.map(|chunk| {
253253
let mut block = [0u32; 8];
254-
for (word, wb) in block.iter_mut().zip(chunk.chunks_exact(4)) {
255-
*word = u32::from_le_bytes(wb.try_into().unwrap());
254+
for (lane, lane_bytes) in block.iter_mut().zip(chunk.chunks_exact(4)) {
255+
*lane = u32::from_le_bytes(lane_bytes.try_into().map_err(|_| {
256+
vortex_err!("invalid bloom filter word length: {}", lane_bytes.len())
257+
})?);
256258
}
257-
block
259+
Ok(block)
258260
})
259-
.collect();
261+
.collect::<VortexResult<Vec<_>>>()?;
262+
260263
Ok(BloomPartial { blocks })
261264
}
262265
}

0 commit comments

Comments
 (0)