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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use core::fmt;
use std::fmt::{Display, Formatter};

use vortex_dtype::field::FieldPath;
use vortex_scalar::Scalar;

use crate::operators::Operator;

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(transparent)
)]
pub struct Disjunction {
    conjunctions: Vec<Conjunction>,
}

impl Disjunction {
    pub fn iter(&self) -> impl Iterator<Item = &Conjunction> {
        self.conjunctions.iter()
    }
}

impl From<Conjunction> for Disjunction {
    fn from(value: Conjunction) -> Self {
        Self {
            conjunctions: vec![value],
        }
    }
}

impl FromIterator<Predicate> for Disjunction {
    fn from_iter<T: IntoIterator<Item = Predicate>>(iter: T) -> Self {
        Self {
            conjunctions: iter
                .into_iter()
                .map(|predicate| Conjunction::from_iter([predicate]))
                .collect(),
        }
    }
}

impl FromIterator<Conjunction> for Disjunction {
    fn from_iter<T: IntoIterator<Item = Conjunction>>(iter: T) -> Self {
        Self {
            conjunctions: iter.into_iter().collect(),
        }
    }
}

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))
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(transparent)
)]
pub struct Conjunction {
    predicates: Vec<Predicate>,
}

impl Conjunction {
    pub fn iter(&self) -> impl Iterator<Item = &Predicate> {
        self.predicates.iter()
    }
}

impl From<Predicate> for Conjunction {
    fn from(value: Predicate) -> Self {
        Self {
            predicates: vec![value],
        }
    }
}

impl FromIterator<Predicate> for Conjunction {
    fn from_iter<T: IntoIterator<Item = Predicate>>(iter: T) -> Self {
        Self {
            predicates: iter.into_iter().collect(),
        }
    }
}

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))
    }
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Value {
    /// A named reference to a qualified field in a dtype.
    Field(FieldPath),
    /// A constant scalar value.
    Literal(Scalar),
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Predicate {
    pub lhs: FieldPath,
    pub op: Operator,
    pub rhs: Value,
}

pub fn lit<T: Into<Scalar>>(n: T) -> Value {
    Value::Literal(n.into())
}

impl Value {
    // NB: We rewrite predicates to be Field-op-predicate, so these methods all must
    // use the inverse operator.
    pub fn eq(self, field: impl Into<FieldPath>) -> Predicate {
        Predicate {
            lhs: field.into(),
            op: Operator::Eq,
            rhs: self,
        }
    }

    pub fn not_eq(self, field: impl Into<FieldPath>) -> Predicate {
        Predicate {
            lhs: field.into(),
            op: Operator::NotEq.inverse(),
            rhs: self,
        }
    }

    pub fn gt(self, field: impl Into<FieldPath>) -> Predicate {
        Predicate {
            lhs: field.into(),
            op: Operator::Gt.inverse(),
            rhs: self,
        }
    }

    pub fn gte(self, field: impl Into<FieldPath>) -> Predicate {
        Predicate {
            lhs: field.into(),
            op: Operator::Gte.inverse(),
            rhs: self,
        }
    }

    pub fn lt(self, field: impl Into<FieldPath>) -> Predicate {
        Predicate {
            lhs: field.into(),
            op: Operator::Lt.inverse(),
            rhs: self,
        }
    }

    pub fn lte(self, field: impl Into<FieldPath>) -> Predicate {
        Predicate {
            lhs: field.into(),
            op: Operator::Lte.inverse(),
            rhs: self,
        }
    }
}

#[cfg(test)]
mod test {
    use vortex_dtype::field::Field;

    use super::*;

    #[test]
    fn test_lit() {
        let scalar: Scalar = 1.into();
        let value: Value = lit(scalar);
        let field = Field::from("id");
        let expr = Predicate {
            lhs: FieldPath::from_iter([field]),
            op: Operator::Eq,
            rhs: value,
        };
        assert_eq!(format!("{}", expr), "($id = 1)");
    }
}