xanderbailey commented on code in PR #2398: URL: https://github.com/apache/iceberg-rust/pull/2398#discussion_r3978517656
########## crates/iceberg/src/expr/visitors/bloom_filter_evaluator.rs: ########## @@ -0,0 +1,1325 @@ +// 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. + +//! Evaluates predicates against Parquet bloom filters to determine whether +//! a row group can be skipped. + +use std::collections::{HashMap, HashSet}; + +use fnv::FnvHashSet; +use parquet::basic::Type as PhysicalType; +use parquet::bloom_filter::Sbbf; +use parquet::data_type::ByteArray; + +use crate::Result; +use crate::expr::visitors::bound_predicate_visitor::{BoundPredicateVisitor, visit}; +use crate::expr::{BoundPredicate, BoundReference}; +use crate::spec::decimal_utils::decimal_to_fixed_length_bytes_exact; +use crate::spec::{Datum, PrimitiveLiteral}; + +const ROW_GROUP_MIGHT_MATCH: Result<bool> = Ok(true); +const ROW_GROUP_CANT_MATCH: Result<bool> = Ok(false); + +/// A column's bloom filter for one row group, together with the file's physical +/// encoding of that column. A probe must be encoded the way the writer encoded +/// the values it inserted, so the encoding travels with the filter. +pub(crate) struct ColumnBloomFilter { + sbbf: Sbbf, + physical_type: PhysicalType, + /// `type_length` from the file's column descriptor. Only meaningful for + /// `FIXED_LEN_BYTE_ARRAY`. + type_length: i32, +} + +impl ColumnBloomFilter { + pub(crate) fn new(sbbf: Sbbf, physical_type: PhysicalType, type_length: i32) -> Self { + Self { + sbbf, + physical_type, + type_length, + } + } +} + +pub(crate) struct BloomFilterEvaluator<'a> { + /// Maps Iceberg field_id -> bloom filter for this row group + bloom_filters: &'a HashMap<i32, ColumnBloomFilter>, +} + +impl<'a> BloomFilterEvaluator<'a> { + /// Evaluate the predicate against the provided bloom filters. + /// Returns `false` if the row group definitely does not match, + /// `true` if it might match. + pub(crate) fn eval( + filter: &BoundPredicate, + bloom_filters: &HashMap<i32, ColumnBloomFilter>, + ) -> Result<bool> { + if bloom_filters.is_empty() { + return ROW_GROUP_MIGHT_MATCH; + } + + let mut evaluator = BloomFilterEvaluator { bloom_filters }; + visit(&mut evaluator, filter) + } + + fn check_datum(&self, reference: &BoundReference, datum: &Datum) -> bool { + let field_id = reference.field().id; + let Some(column) = self.bloom_filters.get(&field_id) else { + // No bloom filter for this column — conservatively might match + return true; + }; + + check_in_bloom_filter(column, datum) + } +} + +/// Collects field IDs that appear in `eq` or `in` predicates — the only +/// predicate types that benefit from bloom filter checks. +pub(crate) fn collect_bloom_filter_field_ids(predicate: &BoundPredicate) -> Result<HashSet<i32>> { + let mut visitor = BloomFilterFieldIdCollector { + field_ids: HashSet::new(), + }; + visit(&mut visitor, predicate)?; + Ok(visitor.field_ids) +} + +struct BloomFilterFieldIdCollector { + field_ids: HashSet<i32>, +} + +impl BoundPredicateVisitor for BloomFilterFieldIdCollector { + type T = (); + + fn always_true(&mut self) -> Result<()> { + Ok(()) + } + + fn always_false(&mut self) -> Result<()> { + Ok(()) + } + + fn and(&mut self, _lhs: (), _rhs: ()) -> Result<()> { + Ok(()) + } + + fn or(&mut self, _lhs: (), _rhs: ()) -> Result<()> { + Ok(()) + } + + fn not(&mut self, _inner: ()) -> Result<()> { + Ok(()) + } + + fn is_null(&mut self, _r: &BoundReference, _p: &BoundPredicate) -> Result<()> { + Ok(()) + } + + fn not_null(&mut self, _r: &BoundReference, _p: &BoundPredicate) -> Result<()> { + Ok(()) + } + + fn is_nan(&mut self, _r: &BoundReference, _p: &BoundPredicate) -> Result<()> { + Ok(()) + } + + fn not_nan(&mut self, _r: &BoundReference, _p: &BoundPredicate) -> Result<()> { + Ok(()) + } + + fn less_than(&mut self, _r: &BoundReference, _l: &Datum, _p: &BoundPredicate) -> Result<()> { + Ok(()) + } + + fn less_than_or_eq( + &mut self, + _r: &BoundReference, + _l: &Datum, + _p: &BoundPredicate, + ) -> Result<()> { + Ok(()) + } + + fn greater_than(&mut self, _r: &BoundReference, _l: &Datum, _p: &BoundPredicate) -> Result<()> { + Ok(()) + } + + fn greater_than_or_eq( + &mut self, + _r: &BoundReference, + _l: &Datum, + _p: &BoundPredicate, + ) -> Result<()> { + Ok(()) + } + + fn eq(&mut self, r: &BoundReference, _l: &Datum, _p: &BoundPredicate) -> Result<()> { + self.field_ids.insert(r.field().id); + Ok(()) + } + + fn not_eq(&mut self, _r: &BoundReference, _l: &Datum, _p: &BoundPredicate) -> Result<()> { + Ok(()) + } + + fn starts_with(&mut self, _r: &BoundReference, _l: &Datum, _p: &BoundPredicate) -> Result<()> { + Ok(()) + } + + fn not_starts_with( + &mut self, + _r: &BoundReference, + _l: &Datum, + _p: &BoundPredicate, + ) -> Result<()> { + Ok(()) + } + + fn r#in( + &mut self, + r: &BoundReference, + _literals: &FnvHashSet<Datum>, + _p: &BoundPredicate, + ) -> Result<()> { + self.field_ids.insert(r.field().id); + Ok(()) + } + + fn not_in( + &mut self, + _r: &BoundReference, + _literals: &FnvHashSet<Datum>, + _p: &BoundPredicate, + ) -> Result<()> { + Ok(()) + } +} + +/// Check whether a datum value might be present in the bloom filter. +/// +/// The value must be checked using the same physical encoding the Parquet +/// writer used when inserting into the bloom filter. We use the actual +/// physical type from the column metadata to ensure correctness regardless +/// of which writer produced the file. +fn check_in_bloom_filter(column: &ColumnBloomFilter, datum: &Datum) -> bool { + let ColumnBloomFilter { + sbbf, + physical_type, + type_length, + } = column; + let physical_type = *physical_type; + + match datum.literal() { + PrimitiveLiteral::Boolean(v) => sbbf.check(v), + // A promoted column (int -> long, float -> double) keeps its original + // physical width in files written before the promotion, and the writer + // hashed that width, so probe at the file's width rather than the + // predicate's. + PrimitiveLiteral::Int(v) => match physical_type { + PhysicalType::INT32 => sbbf.check(v), + PhysicalType::INT64 => sbbf.check(&i64::from(*v)), + _ => true, + }, + PrimitiveLiteral::Long(v) => match physical_type { + PhysicalType::INT64 => sbbf.check(v), + PhysicalType::INT32 => match i32::try_from(*v) { + Ok(narrowed) => sbbf.check(&narrowed), + // Out of range for the column, so it cannot be present. + Err(_) => true, + }, + _ => true, + }, + PrimitiveLiteral::Float(v) => match physical_type { Review Comment: [cfbf233](https://github.com/apache/iceberg-rust/pull/2398/commits/cfbf233fd8dadad447db1af16fb33be552c57a92) -- 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]
