sdd commented on code in PR #946: URL: https://github.com/apache/iceberg-rust/pull/946#discussion_r1983907055
########## crates/iceberg/src/expr/visitors/strict_projection.rs: ########## @@ -0,0 +1,3231 @@ +// 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 std::collections::HashMap; + +use fnv::FnvHashSet; + +use crate::expr::visitors::bound_predicate_visitor::{visit, BoundPredicateVisitor}; +use crate::expr::{BoundPredicate, BoundReference, Predicate}; +use crate::spec::{Datum, PartitionField, PartitionSpecRef}; +use crate::Error; + +// # TODO +// Remove this after delete support +#[allow(dead_code)] +pub(crate) struct StrictProjection { + partition_spec: PartitionSpecRef, + cached_parts: HashMap<i32, Vec<PartitionField>>, +} + +#[allow(dead_code)] +impl StrictProjection { + pub(crate) fn new(partition_spec: PartitionSpecRef) -> Self { + Self { + partition_spec, + cached_parts: HashMap::new(), + } + } + + fn get_parts_for_field_id(&mut self, field_id: i32) -> &Vec<PartitionField> { + if let std::collections::hash_map::Entry::Vacant(e) = self.cached_parts.entry(field_id) { + let mut parts: Vec<PartitionField> = vec![]; + for partition_spec_field in self.partition_spec.fields() { + if partition_spec_field.source_id == field_id { + parts.push(partition_spec_field.clone()) + } + } + + e.insert(parts); + } + + &self.cached_parts[&field_id] + } + + pub(crate) fn strict_project( + &mut self, + predicate: &BoundPredicate, + ) -> crate::Result<Predicate> { + visit(self, predicate) + } + + fn get_parts( + &mut self, + reference: &BoundReference, + predicate: &BoundPredicate, + ) -> Result<Predicate, Error> { + let field_id = reference.field().id; + + // This could be made a bit neater if `try_reduce` ever becomes stable + self.get_parts_for_field_id(field_id).iter().try_fold( + Predicate::AlwaysFalse, + |res, part| { + // consider (ts > 2019-01-01T01:00:00) with day(ts) and hour(ts) + // projections: d >= 2019-01-02 and h >= 2019-01-01-02 (note the inclusive bounds). + // any timestamp where either projection predicate is true must match the original + // predicate. For example, ts = 2019-01-01T03:00:00 matches the hour projection but not + // the day, but does match the original predicate. + Ok( + if let Some(pred_for_part) = + part.transform.strict_project(&part.name, predicate)? + { + if res == Predicate::AlwaysFalse { + pred_for_part + } else { + res.or(pred_for_part) + } + } else { + res + }, + ) + }, + ) + } +} + +impl BoundPredicateVisitor for StrictProjection { + type T = Predicate; + + fn always_true(&mut self) -> crate::Result<Self::T> { + Ok(Predicate::AlwaysTrue) + } + + fn always_false(&mut self) -> crate::Result<Self::T> { + Ok(Predicate::AlwaysFalse) + } + + fn and(&mut self, lhs: Self::T, rhs: Self::T) -> crate::Result<Self::T> { + Ok(lhs.and(rhs)) + } + + fn or(&mut self, lhs: Self::T, rhs: Self::T) -> crate::Result<Self::T> { + Ok(lhs.or(rhs)) + } + + fn not(&mut self, _inner: Self::T) -> crate::Result<Self::T> { + panic!("InclusiveProjection should not be performed against Predicates that contain a Not operator. Ensure that \"Rewrite Not\" gets applied to the originating Predicate before binding it.") Review Comment: I always thought it would be nice to be able to capture this invariant in the type system somehow but it is probably more work than it is worth and adds more complexity than we'd gain in value :-) Personally I'd have thought the combination of a doc comment on the struct and the `unreachable!` in the not-handler should be sufficient for anyone who is doing any reasonable level of testing to be able to deal with this during their development process. We can always revisit it if it ever does cause problems in the future rather than spending time on it now. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org