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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::ops::{BitAnd, BitOr, Not};
use datafusion_common::ScalarValue;
pub(crate) mod rewrite;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TriState {
True,
False,
Unknown,
}
impl TriState {
pub(crate) fn and(self, other: Self) -> Self {
match (self, other) {
(TriState::False, _) | (_, TriState::False) => TriState::False,
(TriState::True, TriState::True) => TriState::True,
_ => TriState::Unknown,
}
}
pub(crate) fn or(self, other: Self) -> Self {
match (self, other) {
(TriState::True, _) | (_, TriState::True) => TriState::True,
(TriState::False, TriState::False) => TriState::False,
_ => TriState::Unknown,
}
}
pub(crate) fn not(self) -> Self {
match self {
TriState::True => TriState::False,
TriState::False => TriState::True,
TriState::Unknown => TriState::Unknown,
}
}
}
// Operator trait implementations for ergonomic usage
impl BitAnd for TriState {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
self.and(rhs)
}
}
impl BitOr for TriState {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
self.or(rhs)
}
}
impl Not for TriState {
type Output = Self;
fn not(self) -> Self::Output {
TriState::not(self)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CmpOp {
Eq,
NotEq,
Lt,
LtEq,
Gt,
GtEq,
}
impl CmpOp {
#[cfg(feature = "datafusion")]
pub(crate) fn flip(self) -> Self {
match self {
CmpOp::Eq => CmpOp::Eq,
CmpOp::NotEq => CmpOp::NotEq,
CmpOp::Lt => CmpOp::Gt,
CmpOp::LtEq => CmpOp::GtEq,
CmpOp::Gt => CmpOp::Lt,
CmpOp::GtEq => CmpOp::LtEq,
}
}
}
/// Expression tree for Parquet metadata pruning.
///
/// Note: This enum includes internal variants for bloom filter optimization
/// (marked with `#[doc(hidden)]`). These are added automatically during compilation
/// and should not be constructed manually.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Expr {
True,
False,
Cmp {
column: String,
op: CmpOp,
value: ScalarValue,
},
Between {
column: String,
low: ScalarValue,
high: ScalarValue,
inclusive: bool,
},
InList {
column: String,
values: Vec<ScalarValue>,
},
/// Internal: Bloom filter equality check (added automatically during compilation).
///
/// This variant is an implementation detail for bloom filter optimization and
/// should not be constructed manually. Use [`Expr::eq`] instead, and bloom
/// filter variants will be added automatically during compilation when appropriate.
#[doc(hidden)]
BloomFilterEq {
column: String,
value: ScalarValue,
},
/// Internal: Bloom filter IN list check (added automatically during compilation).
///
/// This variant is an implementation detail for bloom filter optimization and
/// should not be constructed manually. Use [`Expr::in_list`] instead, and bloom
/// filter variants will be added automatically during compilation when appropriate.
#[doc(hidden)]
BloomFilterInList {
column: String,
values: Vec<ScalarValue>,
},
/// Internal: Dictionary hint equality check (added automatically during compilation).
///
/// This variant is an implementation detail for dictionary-hint optimization and
/// should not be constructed manually. Use [`Expr::eq`] instead, and dictionary hint
/// variants will be added automatically during pruning when enabled.
#[doc(hidden)]
DictionaryHintEq {
column: String,
value: ScalarValue,
},
/// Internal: Dictionary hint IN list check (added automatically during compilation).
///
/// This variant is an implementation detail for dictionary-hint optimization and
/// should not be constructed manually. Use [`Expr::in_list`] instead, and dictionary hint
/// variants will be added automatically during pruning when enabled.
#[doc(hidden)]
DictionaryHintInList {
column: String,
values: Vec<ScalarValue>,
},
StartsWith {
column: String,
prefix: String,
},
IsNull {
column: String,
negated: bool,
},
And(Vec<Expr>),
Or(Vec<Expr>),
Not(Box<Expr>),
}
impl Expr {
/// Build a comparison expression with an explicit operator.
pub fn cmp(column: impl Into<String>, op: CmpOp, value: ScalarValue) -> Self {
Expr::Cmp {
column: column.into(),
op,
value,
}
}
/// Build an equality expression (`=`).
pub fn eq(column: impl Into<String>, value: ScalarValue) -> Self {
Self::cmp(column, CmpOp::Eq, value)
}
/// Build a not-equal expression (`!=`).
pub fn not_eq(column: impl Into<String>, value: ScalarValue) -> Self {
Self::cmp(column, CmpOp::NotEq, value)
}
/// Build a less-than expression (`<`).
pub fn lt(column: impl Into<String>, value: ScalarValue) -> Self {
Self::cmp(column, CmpOp::Lt, value)
}
/// Build a less-than-or-equal expression (`<=`).
pub fn lt_eq(column: impl Into<String>, value: ScalarValue) -> Self {
Self::cmp(column, CmpOp::LtEq, value)
}
/// Build a greater-than expression (`>`).
pub fn gt(column: impl Into<String>, value: ScalarValue) -> Self {
Self::cmp(column, CmpOp::Gt, value)
}
/// Build a greater-than-or-equal expression (`>=`).
pub fn gt_eq(column: impl Into<String>, value: ScalarValue) -> Self {
Self::cmp(column, CmpOp::GtEq, value)
}
/// Build a BETWEEN expression.
pub fn between(
column: impl Into<String>,
low: ScalarValue,
high: ScalarValue,
inclusive: bool,
) -> Self {
Expr::Between {
column: column.into(),
low,
high,
inclusive,
}
}
/// Build an IN (...) expression.
pub fn in_list(column: impl Into<String>, values: Vec<ScalarValue>) -> Self {
Expr::InList {
column: column.into(),
values,
}
}
/// Build a prefix match expression (`LIKE 'prefix%'`).
pub fn starts_with(column: impl Into<String>, prefix: impl Into<String>) -> Self {
Expr::StartsWith {
column: column.into(),
prefix: prefix.into(),
}
}
/// Build an IS NULL expression.
pub fn is_null(column: impl Into<String>) -> Self {
Expr::IsNull {
column: column.into(),
negated: false,
}
}
/// Build an IS NOT NULL expression.
pub fn is_not_null(column: impl Into<String>) -> Self {
Expr::IsNull {
column: column.into(),
negated: true,
}
}
/// Build an AND expression.
pub fn and(parts: Vec<Expr>) -> Self {
Expr::And(parts)
}
/// Build an OR expression.
pub fn or(parts: Vec<Expr>) -> Self {
Expr::Or(parts)
}
/// Build a NOT expression.
pub fn not(expr: Expr) -> Self {
Expr::Not(Box::new(expr))
}
}
impl std::fmt::Display for Expr {
/// User-friendly display that hides internal bloom filter details.
///
/// Bloom filter variants are displayed as `<bloom filter>` to avoid noise
/// in user-facing output. Use `Debug` formatting (`{:?}`) to see the full
/// internal representation.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expr::True => write!(f, "TRUE"),
Expr::False => write!(f, "FALSE"),
Expr::Cmp { column, op, value } => {
let op_str = match op {
CmpOp::Eq => "=",
CmpOp::NotEq => "!=",
CmpOp::Lt => "<",
CmpOp::LtEq => "<=",
CmpOp::Gt => ">",
CmpOp::GtEq => ">=",
};
write!(f, "{} {} {:?}", column, op_str, value)
}
Expr::Between {
column,
low,
high,
inclusive,
} => {
if *inclusive {
write!(f, "{} BETWEEN {:?} AND {:?}", column, low, high)
} else {
write!(f, "{} > {:?} AND {} < {:?}", column, low, column, high)
}
}
Expr::InList { column, values } => {
write!(f, "{} IN (", column)?;
for (i, v) in values.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}", v)?;
}
write!(f, ")")
}
// Hide bloom filter variants from user-facing output
Expr::BloomFilterEq { .. } | Expr::BloomFilterInList { .. } => {
write!(f, "<bloom filter>")
}
// Hide dictionary-hint variants from user-facing output
Expr::DictionaryHintEq { .. } | Expr::DictionaryHintInList { .. } => {
write!(f, "<dictionary hint>")
}
Expr::StartsWith { column, prefix } => {
write!(f, "{} LIKE '{}%'", column, prefix)
}
Expr::IsNull { column, negated } => {
if *negated {
write!(f, "{} IS NOT NULL", column)
} else {
write!(f, "{} IS NULL", column)
}
}
Expr::And(parts) => {
if parts.is_empty() {
write!(f, "TRUE")
} else if parts.len() == 1 {
write!(f, "{}", parts[0])
} else {
write!(f, "(")?;
for (i, part) in parts.iter().enumerate() {
if i > 0 {
write!(f, " AND ")?;
}
write!(f, "{}", part)?;
}
write!(f, ")")
}
}
Expr::Or(parts) => {
if parts.is_empty() {
write!(f, "FALSE")
} else if parts.len() == 1 {
write!(f, "{}", parts[0])
} else {
write!(f, "(")?;
for (i, part) in parts.iter().enumerate() {
if i > 0 {
write!(f, " OR ")?;
}
write!(f, "{}", part)?;
}
write!(f, ")")
}
}
Expr::Not(inner) => write!(f, "NOT ({})", inner),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display_hides_bloom_filters() {
// Simple equality - clean output
let expr = Expr::eq("id", ScalarValue::Int64(Some(42)));
assert_eq!(expr.to_string(), "id = Int64(42)");
// Bloom filter variant - hidden in Display
let bloom = Expr::BloomFilterEq {
column: "id".to_string(),
value: ScalarValue::Int64(Some(42)),
};
assert_eq!(bloom.to_string(), "<bloom filter>");
// AND with bloom filter - bloom filter hidden
let compiled = Expr::and(vec![
Expr::eq("id", ScalarValue::Int64(Some(42))),
Expr::BloomFilterEq {
column: "id".to_string(),
value: ScalarValue::Int64(Some(42)),
},
]);
assert_eq!(compiled.to_string(), "(id = Int64(42) AND <bloom filter>)");
let dict = Expr::DictionaryHintEq {
column: "id".to_string(),
value: ScalarValue::Int64(Some(42)),
};
assert_eq!(dict.to_string(), "<dictionary hint>");
}
#[test]
fn test_display_formatting() {
assert_eq!(Expr::True.to_string(), "TRUE");
assert_eq!(Expr::False.to_string(), "FALSE");
assert_eq!(
Expr::gt("age", ScalarValue::Int32(Some(18))).to_string(),
"age > Int32(18)"
);
assert_eq!(
Expr::in_list(
"status",
vec![
ScalarValue::Utf8(Some("active".to_string())),
ScalarValue::Utf8(Some("pending".to_string())),
]
)
.to_string(),
"status IN (Utf8(\"active\"), Utf8(\"pending\"))"
);
assert_eq!(
Expr::is_null("deleted_at").to_string(),
"deleted_at IS NULL"
);
assert_eq!(
Expr::starts_with("name", "John").to_string(),
"name LIKE 'John%'"
);
}
}