-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathmatch_between.rs
More file actions
291 lines (260 loc) · 8.36 KB
/
match_between.rs
File metadata and controls
291 lines (260 loc) · 8.36 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use crate::expr::Expression;
use crate::expr::and_collect;
use crate::expr::forms::conjuncts;
use crate::expr::lit;
use crate::scalar_fn::ScalarFnVTableExt;
use crate::scalar_fn::fns::between::Between;
use crate::scalar_fn::fns::between::BetweenOptions;
use crate::scalar_fn::fns::between::StrictComparison;
use crate::scalar_fn::fns::binary::Binary;
use crate::scalar_fn::fns::get_item::GetItem;
use crate::scalar_fn::fns::literal::Literal;
use crate::scalar_fn::fns::operators::Operator;
/// This pass looks for expression of the form
/// `x >= a && x < b` and converts them into x between a and b`
pub fn find_between(expr: Expression) -> Expression {
// We search all pairs of cnfs to find any pair of expressions can be converted into a between
// expression.
let mut conjuncts = conjuncts(&expr);
let mut rest = vec![];
for idx in 0..conjuncts.len() {
let Some(c) = conjuncts.get(idx).cloned() else {
continue;
};
let mut matched = false;
for idx2 in (idx + 1)..conjuncts.len() {
// Since values are removed in iterations there might not be a value at idx2,
// but all values will have been considered.
let Some(c2) = conjuncts.get(idx2) else {
continue;
};
if let Some(expr) = maybe_match(&c, c2) {
rest.push(expr);
conjuncts.remove(idx2);
matched = true;
break;
}
}
if !matched {
rest.push(c.clone())
}
}
and_collect(rest).unwrap_or_else(|| lit(true))
}
fn maybe_match(lhs: &Expression, rhs: &Expression) -> Option<Expression> {
let (Some(lhs_op), Some(rhs_op)) = (lhs.as_opt::<Binary>(), rhs.as_opt::<Binary>()) else {
return None;
};
// Extract the grandchildren
let lhs_lhs = lhs.child(0);
let lhs_rhs = lhs.child(1);
let rhs_lhs = rhs.child(0);
let rhs_rhs = rhs.child(1);
// Cannot compare to self
if lhs_lhs.eq(lhs_rhs) || rhs_lhs.eq(rhs_rhs) {
return None;
}
// First, get both halves to have GetItem on the left
let lhs = match (lhs_lhs.is::<GetItem>(), lhs_rhs.is::<GetItem>()) {
(true, false) => lhs.clone(),
(false, true) => Binary.new_expr(lhs_op.swap()?, [lhs_rhs.clone(), lhs_lhs.clone()]),
_ => return None,
};
let lhs_op = lhs.as_::<Binary>();
let lhs_lhs = lhs.child(0);
let rhs = match (rhs_lhs.is::<GetItem>(), rhs_rhs.is::<GetItem>()) {
(true, false) => rhs.clone(),
(false, true) => Binary.new_expr(rhs_op.swap()?, [rhs_rhs.clone(), rhs_lhs.clone()]),
_ => return None,
};
let rhs_op = rhs.as_::<Binary>();
let rhs_lhs = rhs.child(0);
// Both conjuncts must reference the same GetItem column
if !lhs_lhs.eq(rhs_lhs) {
return None;
}
let target = lhs_lhs.clone();
// Find the lower bound
let (lower, upper) = match (lhs_op, rhs_op) {
(Operator::Lt | Operator::Lte, Operator::Gt | Operator::Gte) => (rhs, lhs),
(Operator::Gt | Operator::Gte, Operator::Lt | Operator::Lte) => (lhs, rhs),
_ => return None,
};
let lower_op = lower.as_::<Binary>();
let lower_rhs = lower.child(1);
let upper_op = upper.as_::<Binary>();
let upper_rhs = upper.child(1);
// Ensure bounds are literals
let _ = lower_rhs.as_opt::<Literal>()?;
let _ = upper_rhs.as_opt::<Literal>()?;
let lower_strict = is_strict_comparison(*lower_op)?;
let upper_strict = is_strict_comparison(*upper_op)?;
Some(Between.new_expr(
BetweenOptions {
lower_strict,
upper_strict,
},
[target, lower_rhs.clone(), upper_rhs.clone()],
))
}
fn is_strict_comparison(op: Operator) -> Option<StrictComparison> {
match op {
Operator::Lt | Operator::Gt => Some(StrictComparison::Strict),
Operator::Lte | Operator::Gte => Some(StrictComparison::NonStrict),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::find_between;
use crate::expr::and;
use crate::expr::between;
use crate::expr::col;
use crate::expr::gt;
use crate::expr::gt_eq;
use crate::expr::lit;
use crate::expr::lt;
use crate::expr::lt_eq;
use crate::scalar_fn::fns::between::BetweenOptions;
use crate::scalar_fn::fns::between::StrictComparison;
#[test]
fn test_bad_match() {
// An impossible expression
let expr = and(lt_eq(lit(100), col("x")), gt(lit(-100), col("x")));
let find = find_between(expr);
assert_eq!(
&find,
&between(
col("x"),
lit(100),
lit(-100),
BetweenOptions {
lower_strict: StrictComparison::NonStrict,
upper_strict: StrictComparison::Strict,
}
)
);
}
#[test]
fn test_match_between() {
let expr = and(lt(lit(2), col("x")), gt_eq(lit(5), col("x")));
let find = find_between(expr);
// 2 < x <= 5
assert_eq!(
&between(
col("x"),
lit(2),
lit(5),
BetweenOptions {
lower_strict: StrictComparison::Strict,
upper_strict: StrictComparison::NonStrict,
}
),
&find
);
}
#[test]
fn test_match_2_between() {
let expr = and(gt_eq(col("x"), lit(2)), lt(col("x"), lit(5)));
let find = find_between(expr);
// 2 <= x < 5
assert_eq!(
&between(
col("x"),
lit(2),
lit(5),
BetweenOptions {
lower_strict: StrictComparison::NonStrict,
upper_strict: StrictComparison::Strict,
}
),
&find
);
}
#[test]
fn test_match_3_between() {
let expr = and(gt_eq(col("x"), lit(2)), gt_eq(lit(5), col("x")));
let find = find_between(expr);
// 2 <= x < 5
assert_eq!(
&between(
col("x"),
lit(2),
lit(5),
BetweenOptions {
lower_strict: StrictComparison::NonStrict,
upper_strict: StrictComparison::NonStrict,
}
),
&find
);
}
#[test]
fn test_match_4_between() {
let expr = and(gt_eq(lit(5), col("x")), lt(lit(2), col("x")));
let find = find_between(expr);
// 2 < x <= 5
assert_eq!(
&between(
col("x"),
lit(2),
lit(5),
BetweenOptions {
lower_strict: StrictComparison::Strict,
upper_strict: StrictComparison::NonStrict,
}
),
&find
);
}
#[test]
fn test_match_5_between() {
let expr = and(
and(gt_eq(col("y"), lit(10)), gt_eq(lit(5), col("x"))),
lt(lit(2), col("x")),
);
let find = find_between(expr);
// $.y >= 10 /\ 2 < $.x <= 5
assert_eq!(
&and(
gt_eq(col("y"), lit(10)),
between(
col("x"),
lit(2),
lit(5),
BetweenOptions {
lower_strict: StrictComparison::Strict,
upper_strict: StrictComparison::NonStrict,
}
)
),
&find
);
}
#[test]
fn test_match_6_between() {
let expr = and(
and(gt_eq(lit(5), col("x")), gt_eq(col("y"), lit(10))),
lt(lit(2), col("x")),
);
let find = find_between(expr);
// $.y >= 10 /\ 2 < $.x <= 5
assert_eq!(
&and(
between(
col("x"),
lit(2),
lit(5),
BetweenOptions {
lower_strict: StrictComparison::Strict,
upper_strict: StrictComparison::NonStrict,
}
),
gt_eq(col("y"), lit(10)),
),
&find
);
}
}