advancedxy commented on code in PR #179: URL: https://github.com/apache/arrow-datafusion-comet/pull/179#discussion_r1522627494
########## core/src/execution/datafusion/util/spark_bloom_filter.rs: ########## @@ -0,0 +1,112 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::{ + errors::CometResult, + execution::datafusion::{ + spark_hash::spark_compatible_murmur3_hash, util::spark_bit_array::SparkBitArray, + }, +}; +use arrow_array::{ArrowNativeTypeOp, BooleanArray, Int64Array}; + +const SPARK_BLOOM_FILTER_VERSION_1: i32 = 1; + +/// A Bloom filter implementation that simulates the behavior of Spark's BloomFilter. +/// It's not a complete implementation of Spark's BloomFilter, but just add the minimum +/// methods to support mightContainsLong in the native side. +#[derive(Debug, Hash)] +pub struct SparkBloomFilter { + bits: SparkBitArray, + num_hashes: u32, +} + +/// similar to the `read_num_be_bytes` macro in `bit.rs` but read nums from bytes in big-endian +macro_rules! read_num_be_bytes { + ($ty:ty, $size:expr, $src:expr) => {{ + debug_assert!($size <= $src.len()); + let mut buffer = <$ty as $crate::common::bit::FromBytes>::Buffer::default(); + buffer.as_mut()[..$size].copy_from_slice(&$src[..$size]); + <$ty>::from_be_bytes(buffer) + }}; +} + +impl SparkBloomFilter { + pub fn new_from_buf(buf: &[u8]) -> Self { + let mut offset = 0; + let version = read_num_be_bytes!(i32, 4, buf[offset..]); + offset += 4; + assert_eq!( + version, SPARK_BLOOM_FILTER_VERSION_1, + "Unsupported BloomFilter version" + ); + let num_hashes = read_num_be_bytes!(i32, 4, buf[offset..]); + offset += 4; + let num_words = read_num_be_bytes!(i32, 4, buf[offset..]); + offset += 4; + let mut bits = vec![0u64; num_words as usize]; + for i in 0..num_words { + bits[i as usize] = read_num_be_bytes!(i64, 8, buf[offset..]) as u64; + offset += 8; + } + Self { + bits: SparkBitArray::new(bits), + num_hashes: num_hashes as u32, + } + } + + pub fn put_long(&mut self, item: i64) -> bool { + // Here we first hash the input long element into 2 int hash values, h1 and h2, then produce + // n hash values by `h1 + i * h2` with 1 <= i <= numHashFunctions. Review Comment: It's the `num_hashes` field in this struct. The comment is copied from Spark's side, let me rewording these 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]
