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
impl Predicate
Sourcepub fn not<P>(predicate: P) -> Self
pub fn not<P>(predicate: P) -> Self
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?
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}Sourcepub fn and<L, R>(lhs: L, rhs: R) -> Self
pub fn and<L, R>(lhs: L, rhs: R) -> Self
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?
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}Sourcepub fn or<L, R>(lhs: L, rhs: R) -> Self
pub fn or<L, R>(lhs: L, rhs: R) -> Self
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?
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}Sourcepub fn eq<L, R>(lhs: L, rhs: R) -> Self
pub fn eq<L, R>(lhs: L, rhs: R) -> Self
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?
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}Sourcepub fn gt<L, R>(lhs: L, rhs: R) -> Self
pub fn gt<L, R>(lhs: L, rhs: R) -> Self
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?
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
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}Sourcepub fn lt<L, R>(lhs: L, rhs: R) -> Self
pub fn lt<L, R>(lhs: L, rhs: R) -> Self
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?
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}Sourcepub fn eval(&self, tuple: &Tuple) -> Result<bool, Error>
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.