-
Notifications
You must be signed in to change notification settings - Fork 388
feat (datafusion integration): convert datafusion expr filters to Iceberg Predicate #588
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e8bd953
adding main function and tests
9a54ef9
adding tests, removing integration test for now
afc9c13
fixing typos and lints
9f88a5a
fixing typing issue
e473471
- added support in schmema to convert Date32 to correct arrow type
9d2112d
fixing format and lic
f042ddc
reducing number of tests (17 -> 7)
d9f7e3f
fix formats
cbbf3a6
fix naming
e864fd1
refactoring to use TreeNodeVisitor
bb41f70
fixing fmt
8650476
small refactor
21a38f5
adding swapped op and fixing CR comments
c6cfa68
Merge remote-tracking branch 'upstream/main' into feat-impl-df-filters
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
crates/integrations/datafusion/src/physical_plan/predicate_converter.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // 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 datafusion::arrow::datatypes::DataType; | ||
| use datafusion::logical_expr::{BinaryExpr, Cast, Expr, Operator}; | ||
| use datafusion::scalar::ScalarValue; | ||
| use iceberg::expr::{Predicate, Reference}; | ||
| use iceberg::spec::Datum; | ||
| #[derive(Default)] | ||
| pub struct PredicateConverter; | ||
a-agmon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| impl PredicateConverter { | ||
| /// Convert a list of DataFusion expressions to an iceberg predicate. | ||
| pub fn visit_many(&self, exprs: &[Expr]) -> Option<Predicate> { | ||
| exprs | ||
| .iter() | ||
| .filter_map(|expr| self.visit(expr)) | ||
| .reduce(Predicate::and) | ||
| } | ||
|
|
||
| /// Convert a single DataFusion expression to an iceberg predicate. | ||
| /// currently only supports binary (simple) expressions | ||
| pub fn visit(&self, expr: &Expr) -> Option<Predicate> { | ||
| match expr { | ||
| Expr::BinaryExpr(binary) => self.visit_binary_expr(binary), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Convert a binary expression to an iceberg predicate. | ||
| /// | ||
| /// currently supports: | ||
| /// - column, basic op, and literal, e.g. `a = 1` | ||
| /// - column and casted literal, e.g. `a = cast(1 as bigint)` | ||
| /// - binary conditional (and, or), e.g. `a = 1 and b = 2` | ||
| fn visit_binary_expr(&self, binary: &BinaryExpr) -> Option<Predicate> { | ||
| match (&*binary.left, &binary.op, &*binary.right) { | ||
| // column, op, literal | ||
| (Expr::Column(col), op, Expr::Literal(lit)) => self.visit_column_literal(col, op, lit), | ||
| // column, op, casted literal | ||
| (Expr::Column(col), op, Expr::Cast(Cast { expr, data_type })) => { | ||
| self.visit_column_cast(col, op, expr, data_type) | ||
| } | ||
| // binary conditional (and, or) | ||
| (left, op, right) if matches!(op, Operator::And | Operator::Or) => { | ||
| self.visit_binary_conditional(left, op, right) | ||
| } | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Convert a column and casted literal to an iceberg predicate. | ||
| /// The purpose of this function is to handle the common case in which there is a filter based on a casted literal. | ||
| /// These kinds of expressions are often not pushed down by query engines though its an important case to handle | ||
| /// for iceberg scan pushdown. | ||
| fn visit_column_cast( | ||
| &self, | ||
| col: &datafusion::common::Column, | ||
| op: &Operator, | ||
| expr: &Expr, | ||
| data_type: &DataType, | ||
| ) -> Option<Predicate> { | ||
| if let (Expr::Literal(ScalarValue::Utf8(lit)), DataType::Date32) = (expr, data_type) { | ||
| let reference = Reference::new(col.name.clone()); | ||
| let datum = lit | ||
| .clone() | ||
| .and_then(|date_str| Datum::date_from_str(date_str).ok())?; | ||
| return Some(binary_op_to_predicate(reference, op, datum)); | ||
| } | ||
| None | ||
| } | ||
|
|
||
| /// Convert a binary conditional expression, i.e., (and, or), to an iceberg predicate. | ||
| /// | ||
| /// When processing an AND expression: | ||
| /// - if both expressions are valid predicates then an AND predicate is returned | ||
| /// - if either expression is None then the valid one is returned | ||
| /// | ||
| /// When processing an OR expression: | ||
| /// - only if both expressions are valid predicates then an OR predicate is returned | ||
| fn visit_binary_conditional( | ||
| &self, | ||
| left: &Expr, | ||
| op: &Operator, | ||
| right: &Expr, | ||
| ) -> Option<Predicate> { | ||
| let preds: Vec<Predicate> = vec![self.visit(left), self.visit(right)] | ||
| .into_iter() | ||
| .flatten() | ||
| .collect(); | ||
| match (op, preds.len()) { | ||
| (Operator::And, 1) => preds.first().cloned(), | ||
| (Operator::And, 2) => Some(Predicate::and(preds[0].clone(), preds[1].clone())), | ||
| (Operator::Or, 2) => Some(Predicate::or(preds[0].clone(), preds[1].clone())), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Convert a simple expression based on column and literal (x > 1) to an iceberg predicate. | ||
| fn visit_column_literal( | ||
| &self, | ||
| col: &datafusion::common::Column, | ||
| op: &Operator, | ||
| lit: &ScalarValue, | ||
| ) -> Option<Predicate> { | ||
| let reference = Reference::new(col.name.clone()); | ||
| let datum = scalar_value_to_datum(lit)?; | ||
| Some(binary_op_to_predicate(reference, op, datum)) | ||
| } | ||
| } | ||
|
|
||
| const MILLIS_PER_DAY: i64 = 24 * 60 * 60 * 1000; | ||
| /// Convert a scalar value to an iceberg datum. | ||
| fn scalar_value_to_datum(value: &ScalarValue) -> Option<Datum> { | ||
| match value { | ||
| ScalarValue::Int8(Some(v)) => Some(Datum::int(*v as i32)), | ||
| ScalarValue::Int16(Some(v)) => Some(Datum::int(*v as i32)), | ||
| ScalarValue::Int32(Some(v)) => Some(Datum::int(*v)), | ||
| ScalarValue::Int64(Some(v)) => Some(Datum::long(*v)), | ||
| ScalarValue::Float32(Some(v)) => Some(Datum::double(*v as f64)), | ||
| ScalarValue::Float64(Some(v)) => Some(Datum::double(*v)), | ||
| ScalarValue::Utf8(Some(v)) => Some(Datum::string(v.clone())), | ||
| ScalarValue::LargeUtf8(Some(v)) => Some(Datum::string(v.clone())), | ||
| ScalarValue::Date32(Some(v)) => Some(Datum::date(*v)), | ||
| ScalarValue::Date64(Some(v)) => Some(Datum::date((*v / MILLIS_PER_DAY) as i32)), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// convert the data fusion Exp to an iceberg [`Predicate`] | ||
| fn binary_op_to_predicate(reference: Reference, op: &Operator, datum: Datum) -> Predicate { | ||
| match op { | ||
| Operator::Eq => reference.equal_to(datum), | ||
| Operator::NotEq => reference.not_equal_to(datum), | ||
| Operator::Lt => reference.less_than(datum), | ||
| Operator::LtEq => reference.less_than_or_equal_to(datum), | ||
| Operator::Gt => reference.greater_than(datum), | ||
| Operator::GtEq => reference.greater_than_or_equal_to(datum), | ||
| _ => Predicate::AlwaysTrue, | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.