xanderbailey commented on code in PR #2398: URL: https://github.com/apache/iceberg-rust/pull/2398#discussion_r3978093488
########## 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>> { Review Comment: Have pruned them out [8adcd72](https://github.com/apache/iceberg-rust/pull/2398/commits/8adcd728fd96968fc716b3c7eab0fd715ea57054) I chased this down and a couple of things came out differently than expected. `rewrite_not()` already runs upstream in `TableScanBuilder::with_filter` (`scan/mod.rs:184`), and binding preserves structure, so no `Not` node reaches the reader on the scan path. A second rewrite here would allocate a fresh predicate tree per task to strip nodes that aren't there. The NOT-depth guard isn't implementable as written: `BoundPredicateVisitor` is post-order, so by the time `not()` runs the inner `eq`/`in` have already inserted. I changed `Self::T` to `HashSet<i32>` instead - each node returns its subtree's field ids, `not` returns empty. That makes the collector mirror the evaluator by construction, and it holds for hand-built `FileScanTask` predicates that bypass `with_filter`. Added five tests, including one that a pass-through `not` fails. Rewriting is still the better normalization in principle, but since `with_filter` already applies it, a reader-side rewrite would only add pruning for predicates built directly onto a `FileScanTask` — recovering `NOT(a != v)` → `a = v`, which the collector guard deliberately forgoes. Happy to file a follow-up if you think that path is worth optimising. -- 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]
