-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathdisplay.rs
107 lines (94 loc) · 3.16 KB
/
display.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use core::fmt;
use std::fmt::{Display, Formatter};
use crate::expressions::{Conjunction, Disjunction, Predicate, Value};
use crate::operators::Operator;
impl Display for Disjunction {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.conjunctions
.iter()
.map(|v| format!("{}", v))
.intersperse("\nOR \n".to_string())
.try_for_each(|s| write!(f, "{}", s))
}
}
impl Display for Conjunction {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.predicates
.iter()
.map(|v| format!("{}", v))
.intersperse(" AND ".to_string())
.try_for_each(|s| write!(f, "{}", s))
}
}
impl Display for Predicate {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "({} {} {})", self.left, self.op, self.right)
}
}
impl Display for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Value::Field(field_path) => Display::fmt(field_path, f),
Value::Literal(scalar) => Display::fmt(&scalar, f),
}
}
}
impl Display for Operator {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let display = match &self {
Operator::Eq => "=",
Operator::NotEq => "!=",
Operator::Gt => ">",
Operator::Gte => ">=",
Operator::Lt => "<",
Operator::Lte => "<=",
};
write!(f, "{display}")
}
}
#[cfg(test)]
mod tests {
use vortex_dtype::field_paths::{field, FieldPath};
use crate::expressions::{lit, Conjunction, Disjunction};
use crate::field_paths::FieldPathOperations;
#[test]
fn test_predicate_formatting() {
let f1 = field("field");
assert_eq!(format!("{}", f1.clone().lt(lit(1u32))), "($field < 1)");
assert_eq!(format!("{}", f1.clone().gte(lit(1u32))), "($field >= 1)");
assert_eq!(format!("{}", !f1.clone().lte(lit(1u32))), "($field > 1)");
assert_eq!(format!("{}", !lit(1u32).lte(f1)), "($field <= 1)");
// nested field path
let f2 = FieldPath::builder().join("field").join(0).build();
assert_eq!(format!("{}", !f2.lte(lit(1u32))), "($field.[0] > 1)");
}
#[test]
fn test_dnf_formatting() {
let path = FieldPath::builder().join(2).join("col1").build();
let d1 = Conjunction {
predicates: vec![
lit(1u32).lt(path.clone()),
path.clone().gte(lit(1u32)),
!lit(1u32).lte(path),
],
};
let path2 = FieldPath::builder().join("col1").join(2).build();
let d2 = Conjunction {
predicates: vec![
lit(2u32).lt(path2),
lit(3u32).gte(field(2)),
!lit(5u32).lte(field("col2")),
],
};
let dnf = Disjunction {
conjunctions: vec![d1, d2],
};
let string = format!("{}", dnf);
print!("{}", string);
assert_eq!(
string,
"([2].$col1 >= 1) AND ([2].$col1 >= 1) AND ([2].$col1 <= 1)\nOR \
\n($col1.[2] >= 2) AND ([2] < 3) AND ($col2 <= 5)"
);
}
}