Skip to content
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

feat: Expression system. #132

Merged
merged 4 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions crates/iceberg/src/expr/bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.

//! This module defines bound expressions.

use crate::expr::Operator;
use crate::spec::{Literal, NestedFieldRef};

/// Bound expression.
pub enum Bound {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I'm unsure about is having a Literal as a bound. It feels counterintuitive to me since you cannot bind a literal.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for completeness of the model of an expression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we follow a model closer to what's done in the Java library? Even though literals are technically the leaf they don't extend Expression.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this model is not familiar to people in iceberg community. I think it's worth to refactor it to a model easier to read.

/// Constants such as 1, 'a', true, false, null.
Literal(Literal),
/// Reference to some field in schema
Reference {
/// Original name
name: String,
liurenjie1024 marked this conversation as resolved.
Show resolved Hide resolved
/// Nested field found in schema.
field: NestedFieldRef,
},
/// Expressions
Expr {
/// Operator for this expression, such as `AND` or `OR`.
liurenjie1024 marked this conversation as resolved.
Show resolved Hide resolved
op: Operator,
/// Arguments for this expression. For example, `a > b` would have `a` and `b` as [`inputs`]
inputs: Vec<Bound>,
},
}
49 changes: 49 additions & 0 deletions crates/iceberg/src/expr/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

//! This module contains expressions used in apache iceberg.
liurenjie1024 marked this conversation as resolved.
Show resolved Hide resolved

mod bound;
pub use bound::*;
amogh-jahagirdar marked this conversation as resolved.
Show resolved Hide resolved
mod unbound;
pub use unbound::*;

/// Operators used in expressions.
#[allow(missing_docs)]
pub enum Operator {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is an Enum appropriate here? In Python we carry for example the invert on the Operators itself: https://github.com/apache/iceberg-python/blob/main/pyiceberg/expressions/__init__.py#L491-L498

Not(IsNan('a')) -> IsNotNan('a')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The design here is a little different from python. Expressions are classified into two categories:
Unbound expressions and bounded expressions. Expression are modeled as trees, and currently there are only two kinds leaf nodes: literal and column ref. Expr variant are intermediate nodes in the tree, which contains operators and inputs. Let's use (a > 1) AND (b < c) as an example:

Expr(op: AND)
|__Expr(op: GT)
     |__Ref(a)
     |__Literal(1)
|__Expr(op: LT)
     |__Ref(b)
     |__Ref(c) 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the short review last time, let me give a bit more context here. I hope it helps to understand my concern.

The main challenge with using statistics to prune datafiles is that they are truncated. This is both the case for Iceberg statistics and Parquet statistics as well. By default on Iceberg a truncate[16] is applied to keep the manifests file sizes within reasonable bounds without losing the ability to prune string columns (strings are the main one, but could apply to other types as well).

If we start pruning based on two columns we need to have a big test suite to make sure that everything works correctly (there are a lot of edge cases such as UTF8) overflow. I also don't think that comparing two columns is the most commonly used scenario, so I'd rather postpone it for now.

What I think that's going to be asked for in the foreseeable future is applying transforms, such as cast(created_at AS date) == '2021-01-01'.

IsNull,
NotNull,
IsNan,
NotNan,
LessThan,
LessThanOrEq,
GreaterThan,
GreaterThanOrEq,
Eq,
NotEq,
In,
NotIn,
Not,
And,
Or,
StartsWith,
NotStartsWith,
Count,
CountStar,
Max,
Min,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are aggregations, do we want them in here as well? Mostly because they don't evaluate as a boolean expression on its own.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see java implementation has aggregations: https://github.com/apache/iceberg/blob/8271791061e72dbf554028e477746e89fca9ec02/api/src/main/java/org/apache/iceberg/expressions/Expression.java#L47

I think this is meaningful since we can do optimization with aggregations. For example, select max(a) for t we don't need to touch data file if there are min/max index for column a in manifest file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those aggregations are tricky, since we need to if the min/max aren't truncated. I also see value in count(passengers) > 2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that aggregation pushdown optimizations can be very powerful but I'd argue that we don't add the expression until we actually use those. It makes maintaining public APIs easier until we have a need.

That was the same model followed in the Java expression case (the count/max/min expression was only added when Spark aggregation pushdown was worked on). Let me know what you think @liurenjie1024 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me. We can remove this first.

}
45 changes: 45 additions & 0 deletions crates/iceberg/src/expr/unbound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.

//! This module defines unbound expressions.

use crate::error::Result;
use crate::expr::bound::Bound;
use crate::expr::Operator;
use crate::spec::{Literal, Schema};

/// Unbound expressions.
pub enum Unbound {
/// Constants, such as 1, 'a', true, false, null.
Literal(Literal),
liurenjie1024 marked this conversation as resolved.
Show resolved Hide resolved
/// References to fields, such as `col` or `tbl.col`.
Reference(String),
/// Expressions
Expr {
/// Operator for this expression, such as `AND` or `OR`.
operator: Operator,
/// Arguments of this express.
liurenjie1024 marked this conversation as resolved.
Show resolved Hide resolved
inputs: Vec<Unbound>,
},
}

impl Unbound {
/// Bind expression to schema.
pub fn bind(&self, _schema: &Schema, _case_sensitive: bool) -> Result<Bound> {
todo!()
}
}
1 change: 1 addition & 0 deletions crates/iceberg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ mod avro;
pub mod io;
pub mod spec;

pub mod expr;
pub mod transaction;
pub mod transform;
Loading