Skip to content

feat: Add basic support for PartitionAndMetricsEvaluator #1189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/iceberg/src/expr/visitors/inclusive_metrics_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) struct InclusiveMetricsEvaluator<'a> {
}

impl<'a> InclusiveMetricsEvaluator<'a> {
fn new(data_file: &'a DataFile) -> Self {
pub fn new(data_file: &'a DataFile) -> Self {
InclusiveMetricsEvaluator { data_file }
}

Expand All @@ -52,6 +52,18 @@ impl<'a> InclusiveMetricsEvaluator<'a> {
visit(&mut evaluator, filter)
}

pub(crate) fn evaluate(
&mut self,
filter: &'a BoundPredicate,
include_empty_files: bool,
) -> crate::Result<bool> {
if !include_empty_files && self.data_file.record_count == 0 {
return ROWS_CANNOT_MATCH;
}

visit(self, filter)
}

fn nan_count(&self, field_id: i32) -> Option<&u64> {
self.data_file.nan_value_counts.get(&field_id)
}
Expand Down
1 change: 1 addition & 0 deletions crates/iceberg/src/expr/visitors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) mod inclusive_metrics_evaluator;
pub(crate) mod inclusive_projection;
pub(crate) mod manifest_evaluator;
pub(crate) mod page_index_evaluator;
pub(crate) mod partition_metrics_evaluator;
pub(crate) mod row_group_metrics_evaluator;
pub(crate) mod strict_metrics_evaluator;
pub(crate) mod strict_projection;
89 changes: 89 additions & 0 deletions crates/iceberg/src/expr/visitors/partition_metrics_evaluator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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 super::inclusive_metrics_evaluator::InclusiveMetricsEvaluator;
use super::strict_metrics_evaluator::StrictMetricsEvaluator;
use crate::expr::BoundPredicate;
use crate::spec::{DataFile, PartitionSpec, Schema, Struct};
use crate::Error;

/// An evaluator that checks whether rows in a file may/must match a given expression
/// this class first partially evaluates the provided expression using the partition value
/// and then checks the remaining part of the expression using metrics evaluators.
#[allow(dead_code)]
struct PartitionAndMetricsEvaluator<'a> {
schema: Schema,
metrics_evaluator: HashMap<Struct, (InclusiveMetricsEvaluator<'a>, StrictMetricsEvaluator<'a>)>,
}

// TODO: Implement residual visitor
#[allow(dead_code)]
impl<'a> PartitionAndMetricsEvaluator<'a> {
fn new(schema: Schema, _partition_spec: PartitionSpec, _predicate: BoundPredicate) -> Self {
PartitionAndMetricsEvaluator {
schema,
metrics_evaluator: HashMap::new(),
}
}

// Retrieve cached `InclusiveMetricsEvaluator` and `StrictMetricsEvaluator`
// by partition.
fn get_metrics_evaluator<'b>(
&'b mut self,
data_file: &'b DataFile,
) -> &'b mut (InclusiveMetricsEvaluator<'a>, StrictMetricsEvaluator<'a>)
where
'b: 'a,
{
let partition = data_file.partition();

if !self.metrics_evaluator.contains_key(partition) {
let inclusive = InclusiveMetricsEvaluator::new(data_file);
let strict = StrictMetricsEvaluator::new(data_file);
self.metrics_evaluator
.insert(partition.clone(), (inclusive, strict));
}

self.metrics_evaluator.get_mut(partition).unwrap()
}

pub fn rows_might_match<'b>(
&'b mut self,
filter: &'b BoundPredicate,
data_file: &'a DataFile,
) -> Result<bool, Error>
where
'b: 'a,
{
let (inclusive, _) = self.get_metrics_evaluator(data_file);
inclusive.evaluate(filter, false)
}

pub fn rows_must_match<'b>(
&'b mut self,
filter: &'b BoundPredicate,
data_file: &'a DataFile,
) -> Result<bool, Error>
where
'b: 'a,
{
let (_, strict) = self.get_metrics_evaluator(data_file);
strict.evaluate(filter)
}
}
14 changes: 11 additions & 3 deletions crates/iceberg/src/expr/visitors/strict_metrics_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ pub(crate) struct StrictMetricsEvaluator<'a> {
data_file: &'a DataFile,
}

#[allow(dead_code)]
impl<'a> StrictMetricsEvaluator<'a> {
#[allow(dead_code)]
fn new(data_file: &'a DataFile) -> Self {
pub fn new(data_file: &'a DataFile) -> Self {
StrictMetricsEvaluator { data_file }
}

/// Evaluate this `StrictMetricsEvaluator`'s filter predicate against the
/// provided [`DataFile`]'s metrics. Used by [`TableScan`] to
/// see if this `DataFile` contains data that could match
/// the scan's filter.
#[allow(dead_code)]
pub(crate) fn eval(filter: &'a BoundPredicate, data_file: &'a DataFile) -> crate::Result<bool> {
if data_file.record_count == 0 {
return ROWS_MUST_MATCH;
Expand All @@ -60,6 +59,15 @@ impl<'a> StrictMetricsEvaluator<'a> {
visit(&mut evaluator, filter)
}

/// Evaluate filter using `StrictMetricsEvaluator` instance
pub(crate) fn evaluate(&mut self, filter: &'a BoundPredicate) -> crate::Result<bool> {
if self.data_file.record_count == 0 {
return ROWS_MUST_MATCH;
}

visit(self, filter)
}

fn nan_count(&self, field_id: i32) -> Option<&u64> {
self.data_file.nan_value_counts.get(&field_id)
}
Expand Down
Loading