Skip to main content

Predicate

Enum Predicate 

Source
pub enum Predicate {
    Not(Box<Predicate>),
    And(Box<Predicate>, Box<Predicate>),
    Or(Box<Predicate>, Box<Predicate>),
    Eq(Expression, Expression),
    Gt(Expression, Expression),
    Lt(Expression, Expression),
}
Expand description

Represents a boolean condition evaluated against a tuple.

§Example

use darwen::prelude::{AttributeName, Predicate, Scalar};

let predicate = Predicate::gt(AttributeName::from("age"), Scalar::Integer(18));

assert!(matches!(predicate, Predicate::Gt(_, _)));

Variants§

§

Not(Box<Predicate>)

Logical negation of a predicate.

§

And(Box<Predicate>, Box<Predicate>)

Logical conjunction of two predicates.

Both operands are always evaluated; this operator does not short-circuit.

§

Or(Box<Predicate>, Box<Predicate>)

Logical disjunction of two predicates.

Both operands are always evaluated; this operator does not short-circuit.

§

Eq(Expression, Expression)

Equality comparison between two expressions.

= is only valid for operands of the same scalar type: INTEGER = INTEGER, BOOLEAN = BOOLEAN, STRING = STRING, and BINARY = BINARY. Mixed-type comparisons return an error.

§

Gt(Expression, Expression)

Greater-than comparison between two expressions.

> is only valid for INTEGER > INTEGER. All other comparisons return an error.

§

Lt(Expression, Expression)

Less-than comparison between two expressions.

< is only valid for INTEGER < INTEGER. All other comparisons return an error.

Implementations§

Source§

impl Predicate

Source

pub fn not<P>(predicate: P) -> Self
where P: Into<Predicate>,

Creates a negated predicate.

The nested predicate is always evaluated when the resulting predicate is evaluated.

§Example
use darwen::{tuple, prelude::{AttributeName, Predicate, Scalar}};

let tuple = tuple!(active = true)?;
let predicate = Predicate::not(Predicate::eq(
    AttributeName::from("active"),
    Scalar::Boolean(false),
));

assert!(predicate.eval(&tuple)?);
Examples found in repository?
examples/predicate.rs (lines 124-127)
70fn main() {
71    let users = users();
72
73    println!("Source relation");
74    println!("{users}");
75
76    show(
77        "Predicate::eq(city, \"Berlin\")",
78        &users,
79        Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
80    );
81
82    show(
83        "Predicate::eq(city, home_city)",
84        &users,
85        Predicate::eq(
86            AttributeName::from("city"),
87            AttributeName::from("home_city"),
88        ),
89    );
90
91    show(
92        "Predicate::gt(age, 20)",
93        &users,
94        Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
95    );
96
97    show(
98        "Predicate::lt(age, 21)",
99        &users,
100        Predicate::lt(AttributeName::from("age"), Scalar::from(21_i64)),
101    );
102
103    show(
104        "Predicate::and(gt(age, 20), eq(city, \"Berlin\"))",
105        &users,
106        Predicate::and(
107            Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
108            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
109        ),
110    );
111
112    show(
113        "Predicate::or(eq(city, \"Berlin\"), eq(city, \"Paris\"))",
114        &users,
115        Predicate::or(
116            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
117            Predicate::eq(AttributeName::from("city"), Scalar::from("Paris")),
118        ),
119    );
120
121    show(
122        "Predicate::not(eq(city, \"Tbilisi\"))",
123        &users,
124        Predicate::not(Predicate::eq(
125            AttributeName::from("city"),
126            Scalar::from("Tbilisi"),
127        )),
128    );
129}
Source

pub fn and<L, R>(lhs: L, rhs: R) -> Self
where L: Into<Predicate>, R: Into<Predicate>,

Creates a conjunction of two predicates.

Both operands are always evaluated; this operator does not short-circuit.

§Example
use darwen::{tuple, prelude::{AttributeName, Predicate, Scalar}};

let tuple = tuple!(age = 21, active = true)?;
let predicate = Predicate::and(
    Predicate::gt(AttributeName::from("age"), Scalar::Integer(18)),
    Predicate::eq(AttributeName::from("active"), Scalar::Boolean(true)),
);

assert!(predicate.eval(&tuple)?);
Examples found in repository?
examples/predicate.rs (lines 106-109)
70fn main() {
71    let users = users();
72
73    println!("Source relation");
74    println!("{users}");
75
76    show(
77        "Predicate::eq(city, \"Berlin\")",
78        &users,
79        Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
80    );
81
82    show(
83        "Predicate::eq(city, home_city)",
84        &users,
85        Predicate::eq(
86            AttributeName::from("city"),
87            AttributeName::from("home_city"),
88        ),
89    );
90
91    show(
92        "Predicate::gt(age, 20)",
93        &users,
94        Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
95    );
96
97    show(
98        "Predicate::lt(age, 21)",
99        &users,
100        Predicate::lt(AttributeName::from("age"), Scalar::from(21_i64)),
101    );
102
103    show(
104        "Predicate::and(gt(age, 20), eq(city, \"Berlin\"))",
105        &users,
106        Predicate::and(
107            Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
108            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
109        ),
110    );
111
112    show(
113        "Predicate::or(eq(city, \"Berlin\"), eq(city, \"Paris\"))",
114        &users,
115        Predicate::or(
116            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
117            Predicate::eq(AttributeName::from("city"), Scalar::from("Paris")),
118        ),
119    );
120
121    show(
122        "Predicate::not(eq(city, \"Tbilisi\"))",
123        &users,
124        Predicate::not(Predicate::eq(
125            AttributeName::from("city"),
126            Scalar::from("Tbilisi"),
127        )),
128    );
129}
Source

pub fn or<L, R>(lhs: L, rhs: R) -> Self
where L: Into<Predicate>, R: Into<Predicate>,

Creates a disjunction of two predicates.

Both operands are always evaluated; this operator does not short-circuit.

§Example
use darwen::{tuple, prelude::{AttributeName, Predicate, Scalar}};

let tuple = tuple!(city = "Berlin")?;
let predicate = Predicate::or(
    Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
    Predicate::eq(AttributeName::from("city"), Scalar::from("Paris")),
);

assert!(predicate.eval(&tuple)?);
Examples found in repository?
examples/predicate.rs (lines 115-118)
70fn main() {
71    let users = users();
72
73    println!("Source relation");
74    println!("{users}");
75
76    show(
77        "Predicate::eq(city, \"Berlin\")",
78        &users,
79        Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
80    );
81
82    show(
83        "Predicate::eq(city, home_city)",
84        &users,
85        Predicate::eq(
86            AttributeName::from("city"),
87            AttributeName::from("home_city"),
88        ),
89    );
90
91    show(
92        "Predicate::gt(age, 20)",
93        &users,
94        Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
95    );
96
97    show(
98        "Predicate::lt(age, 21)",
99        &users,
100        Predicate::lt(AttributeName::from("age"), Scalar::from(21_i64)),
101    );
102
103    show(
104        "Predicate::and(gt(age, 20), eq(city, \"Berlin\"))",
105        &users,
106        Predicate::and(
107            Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
108            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
109        ),
110    );
111
112    show(
113        "Predicate::or(eq(city, \"Berlin\"), eq(city, \"Paris\"))",
114        &users,
115        Predicate::or(
116            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
117            Predicate::eq(AttributeName::from("city"), Scalar::from("Paris")),
118        ),
119    );
120
121    show(
122        "Predicate::not(eq(city, \"Tbilisi\"))",
123        &users,
124        Predicate::not(Predicate::eq(
125            AttributeName::from("city"),
126            Scalar::from("Tbilisi"),
127        )),
128    );
129}
Source

pub fn eq<L, R>(lhs: L, rhs: R) -> Self
where L: Into<Expression>, R: Into<Expression>,

Creates an equality predicate from two expressions.

= is only valid for operands of the same scalar type: INTEGER = INTEGER, BOOLEAN = BOOLEAN, STRING = STRING, and BINARY = BINARY. Mixed-type comparisons return an error.

§Example
use darwen::{tuple, prelude::{AttributeName, Predicate, Scalar}};

let tuple = tuple!(name = "Monica")?;
let predicate = Predicate::eq(AttributeName::from("name"), Scalar::from("Monica"));

assert!(predicate.eval(&tuple)?);

Passing an owned String directly is intentionally rejected to avoid ambiguity between attribute names and string constants.

use darwen::prelude::{AttributeName, Predicate};

let city_name = String::from("Berlin");
let _predicate = Predicate::eq(AttributeName::from("city"), city_name);
Examples found in repository?
examples/predicate.rs (line 79)
70fn main() {
71    let users = users();
72
73    println!("Source relation");
74    println!("{users}");
75
76    show(
77        "Predicate::eq(city, \"Berlin\")",
78        &users,
79        Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
80    );
81
82    show(
83        "Predicate::eq(city, home_city)",
84        &users,
85        Predicate::eq(
86            AttributeName::from("city"),
87            AttributeName::from("home_city"),
88        ),
89    );
90
91    show(
92        "Predicate::gt(age, 20)",
93        &users,
94        Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
95    );
96
97    show(
98        "Predicate::lt(age, 21)",
99        &users,
100        Predicate::lt(AttributeName::from("age"), Scalar::from(21_i64)),
101    );
102
103    show(
104        "Predicate::and(gt(age, 20), eq(city, \"Berlin\"))",
105        &users,
106        Predicate::and(
107            Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
108            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
109        ),
110    );
111
112    show(
113        "Predicate::or(eq(city, \"Berlin\"), eq(city, \"Paris\"))",
114        &users,
115        Predicate::or(
116            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
117            Predicate::eq(AttributeName::from("city"), Scalar::from("Paris")),
118        ),
119    );
120
121    show(
122        "Predicate::not(eq(city, \"Tbilisi\"))",
123        &users,
124        Predicate::not(Predicate::eq(
125            AttributeName::from("city"),
126            Scalar::from("Tbilisi"),
127        )),
128    );
129}
Source

pub fn gt<L, R>(lhs: L, rhs: R) -> Self
where L: Into<Expression>, R: Into<Expression>,

Creates a greater-than predicate from two expressions.

> is only valid for INTEGER > INTEGER. All other comparisons return an error.

§Example
use darwen::{tuple, prelude::{AttributeName, Predicate, Scalar}};

let tuple = tuple!(age = 21)?;
let predicate = Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64));

assert!(predicate.eval(&tuple)?);
Examples found in repository?
examples/restrict.rs (lines 28-31)
22fn main() {
23    let users = users();
24
25    println!(
26        "{}",
27        users
28            .restrict(&Predicate::gt(
29                Expression::Attribute(AttributeName::from("age")),
30                Expression::Const(Scalar::Integer(20))
31            ))
32            .unwrap()
33    )
34}
More examples
Hide additional examples
examples/predicate.rs (line 94)
70fn main() {
71    let users = users();
72
73    println!("Source relation");
74    println!("{users}");
75
76    show(
77        "Predicate::eq(city, \"Berlin\")",
78        &users,
79        Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
80    );
81
82    show(
83        "Predicate::eq(city, home_city)",
84        &users,
85        Predicate::eq(
86            AttributeName::from("city"),
87            AttributeName::from("home_city"),
88        ),
89    );
90
91    show(
92        "Predicate::gt(age, 20)",
93        &users,
94        Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
95    );
96
97    show(
98        "Predicate::lt(age, 21)",
99        &users,
100        Predicate::lt(AttributeName::from("age"), Scalar::from(21_i64)),
101    );
102
103    show(
104        "Predicate::and(gt(age, 20), eq(city, \"Berlin\"))",
105        &users,
106        Predicate::and(
107            Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
108            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
109        ),
110    );
111
112    show(
113        "Predicate::or(eq(city, \"Berlin\"), eq(city, \"Paris\"))",
114        &users,
115        Predicate::or(
116            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
117            Predicate::eq(AttributeName::from("city"), Scalar::from("Paris")),
118        ),
119    );
120
121    show(
122        "Predicate::not(eq(city, \"Tbilisi\"))",
123        &users,
124        Predicate::not(Predicate::eq(
125            AttributeName::from("city"),
126            Scalar::from("Tbilisi"),
127        )),
128    );
129}
Source

pub fn lt<L, R>(lhs: L, rhs: R) -> Self
where L: Into<Expression>, R: Into<Expression>,

Creates a less-than predicate from two expressions.

< is only valid for INTEGER < INTEGER. All other comparisons return an error.

§Example
use darwen::{tuple, prelude::{AttributeName, Predicate, Scalar}};

let tuple = tuple!(age = 21)?;
let predicate = Predicate::lt(AttributeName::from("age"), Scalar::from(30_i64));

assert!(predicate.eval(&tuple)?);
Examples found in repository?
examples/predicate.rs (line 100)
70fn main() {
71    let users = users();
72
73    println!("Source relation");
74    println!("{users}");
75
76    show(
77        "Predicate::eq(city, \"Berlin\")",
78        &users,
79        Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
80    );
81
82    show(
83        "Predicate::eq(city, home_city)",
84        &users,
85        Predicate::eq(
86            AttributeName::from("city"),
87            AttributeName::from("home_city"),
88        ),
89    );
90
91    show(
92        "Predicate::gt(age, 20)",
93        &users,
94        Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
95    );
96
97    show(
98        "Predicate::lt(age, 21)",
99        &users,
100        Predicate::lt(AttributeName::from("age"), Scalar::from(21_i64)),
101    );
102
103    show(
104        "Predicate::and(gt(age, 20), eq(city, \"Berlin\"))",
105        &users,
106        Predicate::and(
107            Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
108            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
109        ),
110    );
111
112    show(
113        "Predicate::or(eq(city, \"Berlin\"), eq(city, \"Paris\"))",
114        &users,
115        Predicate::or(
116            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
117            Predicate::eq(AttributeName::from("city"), Scalar::from("Paris")),
118        ),
119    );
120
121    show(
122        "Predicate::not(eq(city, \"Tbilisi\"))",
123        &users,
124        Predicate::not(Predicate::eq(
125            AttributeName::from("city"),
126            Scalar::from("Tbilisi"),
127        )),
128    );
129}
Source

pub fn eval(&self, tuple: &Tuple) -> Result<bool, Error>

Evaluates a predicate against a tuple.

§Example
use darwen::prelude::{AttributeName, Predicate, Scalar, TupleBuilder};

let tuple = TupleBuilder::new()
    .with_value(AttributeName::from("age"), Scalar::Integer(21))
    .build()?;

let predicate = Predicate::gt(AttributeName::from("age"), Scalar::Integer(18));

assert!(predicate.eval(&tuple)?);
§Errors

Returns Error::AttributeNotFound if one of the predicate expressions references an attribute that does not exist in the tuple. Returns Error::ScalarTypeMismatch if Eq compares operands of different scalar types. Returns Error::NonComparableTypes if Gt or Lt is used with non-integer operands.

Trait Implementations§

Source§

impl Debug for Predicate

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.