Skip to content

Commit cce049c

Browse files
liurenjie1024shaeqahmed
authored andcommitted
feat: Expression system. (apache#132)
* feat: Expressions * Fix comments * Refactor expression to be more similar to iceberg model * Fix typo
1 parent fd7834d commit cce049c

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

crates/iceberg/src/expr/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! This module contains expressions.
19+
20+
mod term;
21+
pub use term::*;
22+
mod predicate;
23+
pub use predicate::*;
24+
25+
/// Predicate operators used in expressions.
26+
#[allow(missing_docs)]
27+
pub enum PredicateOperator {
28+
IsNull,
29+
NotNull,
30+
IsNan,
31+
NotNan,
32+
LessThan,
33+
LessThanOrEq,
34+
GreaterThan,
35+
GreaterThanOrEq,
36+
Eq,
37+
NotEq,
38+
In,
39+
NotIn,
40+
StartsWith,
41+
NotStartsWith,
42+
}

crates/iceberg/src/expr/predicate.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! This module contains predicate expressions.
19+
//! Predicate expressions are used to filter data, and evaluates to a boolean value. For example,
20+
//! `a > 10` is a predicate expression, and it evaluates to `true` if `a` is greater than `10`,
21+
22+
use crate::expr::{BoundReference, PredicateOperator, UnboundReference};
23+
use crate::spec::Literal;
24+
use std::collections::HashSet;
25+
26+
/// Logical expression, such as `AND`, `OR`, `NOT`.
27+
pub struct LogicalExpression<T, const N: usize> {
28+
inputs: [Box<T>; N],
29+
}
30+
31+
/// Unary predicate, for example, `a IS NULL`.
32+
pub struct UnaryExpression<T> {
33+
/// Operator of this predicate, must be single operand operator.
34+
op: PredicateOperator,
35+
/// Term of this predicate, for example, `a` in `a IS NULL`.
36+
term: T,
37+
}
38+
39+
/// Binary predicate, for example, `a > 10`.
40+
pub struct BinaryExpression<T> {
41+
/// Operator of this predicate, must be binary operator, such as `=`, `>`, `<`, etc.
42+
op: PredicateOperator,
43+
/// Term of this predicate, for example, `a` in `a > 10`.
44+
term: T,
45+
/// Literal of this predicate, for example, `10` in `a > 10`.
46+
literal: Literal,
47+
}
48+
49+
/// Set predicates, for example, `a in (1, 2, 3)`.
50+
pub struct SetExpression<T> {
51+
/// Operator of this predicate, must be set operator, such as `IN`, `NOT IN`, etc.
52+
op: PredicateOperator,
53+
/// Term of this predicate, for example, `a` in `a in (1, 2, 3)`.
54+
term: T,
55+
/// Literals of this predicate, for example, `(1, 2, 3)` in `a in (1, 2, 3)`.
56+
literals: HashSet<Literal>,
57+
}
58+
59+
/// Unbound predicate expression before binding to a schema.
60+
pub enum UnboundPredicate {
61+
/// And predicate, for example, `a > 10 AND b < 20`.
62+
And(LogicalExpression<UnboundPredicate, 2>),
63+
/// Or predicate, for example, `a > 10 OR b < 20`.
64+
Or(LogicalExpression<UnboundPredicate, 2>),
65+
/// Not predicate, for example, `NOT (a > 10)`.
66+
Not(LogicalExpression<UnboundPredicate, 1>),
67+
/// Unary expression, for example, `a IS NULL`.
68+
Unary(UnaryExpression<UnboundReference>),
69+
/// Binary expression, for example, `a > 10`.
70+
Binary(BinaryExpression<UnboundReference>),
71+
/// Set predicates, for example, `a in (1, 2, 3)`.
72+
Set(SetExpression<UnboundReference>),
73+
}
74+
75+
/// Bound predicate expression after binding to a schema.
76+
pub enum BoundPredicate {
77+
/// An expression always evaluates to true.
78+
AlwaysTrue,
79+
/// An expression always evaluates to false.
80+
AlwaysFalse,
81+
/// An expression combined by `AND`, for example, `a > 10 AND b < 20`.
82+
And(LogicalExpression<BoundPredicate, 2>),
83+
/// An expression combined by `OR`, for example, `a > 10 OR b < 20`.
84+
Or(LogicalExpression<BoundPredicate, 2>),
85+
/// An expression combined by `NOT`, for example, `NOT (a > 10)`.
86+
Not(LogicalExpression<BoundPredicate, 1>),
87+
/// Unary expression, for example, `a IS NULL`.
88+
Unary(UnaryExpression<BoundReference>),
89+
/// Binary expression, for example, `a > 10`.
90+
Binary(BinaryExpression<BoundReference>),
91+
/// Set predicates, for example, `a in (1, 2, 3)`.
92+
Set(SetExpression<BoundReference>),
93+
}

crates/iceberg/src/expr/term.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! Term definition.
19+
20+
use crate::spec::NestedFieldRef;
21+
22+
/// Unbound term before binding to a schema.
23+
pub type UnboundTerm = UnboundReference;
24+
25+
/// A named reference in an unbound expression.
26+
/// For example, `a` in `a > 10`.
27+
pub struct UnboundReference {
28+
name: String,
29+
}
30+
31+
/// A named reference in a bound expression after binding to a schema.
32+
pub struct BoundReference {
33+
field: NestedFieldRef,
34+
}
35+
36+
/// Bound term after binding to a schema.
37+
pub type BoundTerm = BoundReference;

crates/iceberg/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub use error::ErrorKind;
2828
pub use error::Result;
2929

3030
mod catalog;
31+
3132
pub use catalog::Catalog;
3233
pub use catalog::Namespace;
3334
pub use catalog::NamespaceIdent;
@@ -44,5 +45,7 @@ pub mod avro;
4445
pub mod io;
4546
pub mod spec;
4647

48+
#[allow(dead_code)]
49+
pub mod expr;
4750
pub mod transaction;
4851
pub mod transform;

0 commit comments

Comments
 (0)