Skip to content

Commit a295f83

Browse files
committed
feat: add BoundPredicateVisitor. Add AlwaysTrue and AlwaysFalse to Predicate
1 parent ca9de89 commit a295f83

File tree

4 files changed

+374
-4
lines changed

4 files changed

+374
-4
lines changed

crates/iceberg/src/expr/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818
//! This module contains expressions.
1919
2020
mod term;
21-
22-
use std::fmt::{Display, Formatter};
23-
2421
pub use term::*;
2522
pub(crate) mod accessor;
2623
mod predicate;
24+
pub(crate) mod visitors;
25+
pub use predicate::*;
2726

2827
use crate::spec::SchemaRef;
29-
pub use predicate::*;
28+
use std::fmt::{Display, Formatter};
3029

3130
/// Predicate operators used in expressions.
3231
///
3332
/// The discriminant of this enum is used for determining the type of the operator, see
3433
/// [`PredicateOperator::is_unary`], [`PredicateOperator::is_binary`], [`PredicateOperator::is_set`]
3534
#[allow(missing_docs)]
3635
#[derive(Debug, Clone, Copy, PartialEq)]
36+
#[non_exhaustive]
3737
#[repr(u16)]
3838
pub enum PredicateOperator {
3939
// Unary operators

crates/iceberg/src/expr/predicate.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ impl<T> UnaryExpression<T> {
119119
pub(crate) fn op(&self) -> PredicateOperator {
120120
self.op
121121
}
122+
123+
pub(crate) fn term(&self) -> &T {
124+
&self.term
125+
}
122126
}
123127

124128
/// Binary predicate, for example, `a > 10`.
@@ -147,12 +151,17 @@ impl<T> BinaryExpression<T> {
147151
debug_assert!(op.is_binary());
148152
Self { op, term, literal }
149153
}
154+
150155
pub(crate) fn op(&self) -> PredicateOperator {
151156
self.op
152157
}
153158
pub(crate) fn literal(&self) -> &Datum {
154159
&self.literal
155160
}
161+
162+
pub(crate) fn term(&self) -> &T {
163+
&self.term
164+
}
156165
}
157166

158167
impl<T: Display> Display for BinaryExpression<T> {
@@ -200,12 +209,17 @@ impl<T> SetExpression<T> {
200209
debug_assert!(op.is_set());
201210
Self { op, term, literals }
202211
}
212+
203213
pub(crate) fn op(&self) -> PredicateOperator {
204214
self.op
205215
}
206216
pub(crate) fn literals(&self) -> &FnvHashSet<Datum> {
207217
&self.literals
208218
}
219+
220+
pub(crate) fn term(&self) -> &T {
221+
&self.term
222+
}
209223
}
210224

211225
impl<T: Bind> Bind for SetExpression<T> {
@@ -232,6 +246,10 @@ impl<T: Display + Debug> Display for SetExpression<T> {
232246
/// Unbound predicate expression before binding to a schema.
233247
#[derive(Debug, PartialEq)]
234248
pub enum Predicate {
249+
/// AlwaysTrue predicate, for example, `TRUE`.
250+
AlwaysTrue,
251+
/// AlwaysFalse predicate, for example, `FALSE`.
252+
AlwaysFalse,
235253
/// And predicate, for example, `a > 10 AND b < 20`.
236254
And(LogicalExpression<Predicate, 2>),
237255
/// Or predicate, for example, `a > 10 OR b < 20`.
@@ -382,13 +400,21 @@ impl Bind for Predicate {
382400
bound_literals,
383401
)))
384402
}
403+
Predicate::AlwaysTrue => Ok(BoundPredicate::AlwaysTrue),
404+
Predicate::AlwaysFalse => Ok(BoundPredicate::AlwaysFalse),
385405
}
386406
}
387407
}
388408

389409
impl Display for Predicate {
390410
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
391411
match self {
412+
Predicate::AlwaysTrue => {
413+
write!(f, "TRUE")
414+
}
415+
Predicate::AlwaysFalse => {
416+
write!(f, "FALSE")
417+
}
392418
Predicate::And(expr) => {
393419
write!(f, "({}) AND ({})", expr.inputs()[0], expr.inputs()[1])
394420
}
@@ -476,6 +502,8 @@ impl Predicate {
476502
/// ```
477503
pub fn negate(self) -> Predicate {
478504
match self {
505+
Predicate::AlwaysTrue => Predicate::AlwaysFalse,
506+
Predicate::AlwaysFalse => Predicate::AlwaysTrue,
479507
Predicate::And(expr) => Predicate::Or(LogicalExpression::new(
480508
expr.inputs.map(|expr| Box::new(expr.negate())),
481509
)),
@@ -540,6 +568,8 @@ impl Predicate {
540568
Predicate::Unary(expr) => Predicate::Unary(expr),
541569
Predicate::Binary(expr) => Predicate::Binary(expr),
542570
Predicate::Set(expr) => Predicate::Set(expr),
571+
Predicate::AlwaysTrue => Predicate::AlwaysTrue,
572+
Predicate::AlwaysFalse => Predicate::AlwaysFalse,
543573
}
544574
}
545575
}

0 commit comments

Comments
 (0)