Skip to content

Commit

Permalink
feat: add BoundPredicateVisitor. Add AlwaysTrue and AlwaysFalse to Pr…
Browse files Browse the repository at this point in the history
…edicate (apache#334)
  • Loading branch information
sdd authored Apr 16, 2024
1 parent 8847f9c commit 9160bba
Show file tree
Hide file tree
Showing 4 changed files with 696 additions and 4 deletions.
8 changes: 4 additions & 4 deletions crates/iceberg/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
//! This module contains expressions.
mod term;

use std::fmt::{Display, Formatter};

pub use term::*;
pub(crate) mod accessor;
mod predicate;
pub(crate) mod visitors;
pub use predicate::*;

use crate::spec::SchemaRef;
pub use predicate::*;
use std::fmt::{Display, Formatter};

/// Predicate operators used in expressions.
///
/// The discriminant of this enum is used for determining the type of the operator, see
/// [`PredicateOperator::is_unary`], [`PredicateOperator::is_binary`], [`PredicateOperator::is_set`]
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
#[repr(u16)]
pub enum PredicateOperator {
// Unary operators
Expand Down
30 changes: 30 additions & 0 deletions crates/iceberg/src/expr/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ impl<T> UnaryExpression<T> {
pub(crate) fn op(&self) -> PredicateOperator {
self.op
}

pub(crate) fn term(&self) -> &T {
&self.term
}
}

/// Binary predicate, for example, `a > 10`.
Expand Down Expand Up @@ -147,12 +151,17 @@ impl<T> BinaryExpression<T> {
debug_assert!(op.is_binary());
Self { op, term, literal }
}

pub(crate) fn op(&self) -> PredicateOperator {
self.op
}
pub(crate) fn literal(&self) -> &Datum {
&self.literal
}

pub(crate) fn term(&self) -> &T {
&self.term
}
}

impl<T: Display> Display for BinaryExpression<T> {
Expand Down Expand Up @@ -200,12 +209,17 @@ impl<T> SetExpression<T> {
debug_assert!(op.is_set());
Self { op, term, literals }
}

pub(crate) fn op(&self) -> PredicateOperator {
self.op
}
pub(crate) fn literals(&self) -> &FnvHashSet<Datum> {
&self.literals
}

pub(crate) fn term(&self) -> &T {
&self.term
}
}

impl<T: Bind> Bind for SetExpression<T> {
Expand All @@ -232,6 +246,10 @@ impl<T: Display + Debug> Display for SetExpression<T> {
/// Unbound predicate expression before binding to a schema.
#[derive(Debug, PartialEq)]
pub enum Predicate {
/// AlwaysTrue predicate, for example, `TRUE`.
AlwaysTrue,
/// AlwaysFalse predicate, for example, `FALSE`.
AlwaysFalse,
/// And predicate, for example, `a > 10 AND b < 20`.
And(LogicalExpression<Predicate, 2>),
/// Or predicate, for example, `a > 10 OR b < 20`.
Expand Down Expand Up @@ -382,13 +400,21 @@ impl Bind for Predicate {
bound_literals,
)))
}
Predicate::AlwaysTrue => Ok(BoundPredicate::AlwaysTrue),
Predicate::AlwaysFalse => Ok(BoundPredicate::AlwaysFalse),
}
}
}

impl Display for Predicate {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Predicate::AlwaysTrue => {
write!(f, "TRUE")
}
Predicate::AlwaysFalse => {
write!(f, "FALSE")
}
Predicate::And(expr) => {
write!(f, "({}) AND ({})", expr.inputs()[0], expr.inputs()[1])
}
Expand Down Expand Up @@ -476,6 +502,8 @@ impl Predicate {
/// ```
pub fn negate(self) -> Predicate {
match self {
Predicate::AlwaysTrue => Predicate::AlwaysFalse,
Predicate::AlwaysFalse => Predicate::AlwaysTrue,
Predicate::And(expr) => Predicate::Or(LogicalExpression::new(
expr.inputs.map(|expr| Box::new(expr.negate())),
)),
Expand Down Expand Up @@ -540,6 +568,8 @@ impl Predicate {
Predicate::Unary(expr) => Predicate::Unary(expr),
Predicate::Binary(expr) => Predicate::Binary(expr),
Predicate::Set(expr) => Predicate::Set(expr),
Predicate::AlwaysTrue => Predicate::AlwaysTrue,
Predicate::AlwaysFalse => Predicate::AlwaysFalse,
}
}
}
Expand Down
Loading

0 comments on commit 9160bba

Please sign in to comment.