-
Notifications
You must be signed in to change notification settings - Fork 208
/
range.rs
116 lines (109 loc) · 3.69 KB
/
range.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
108
109
110
111
112
113
114
115
116
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0.
//! Range filter.
use std::ops::Bound;
use super::*;
use crate::catalog::ColumnRefId;
use crate::storage::KeyRange;
/// The data type of range analysis.
///
/// If Some, the expression is a range condition.
///
/// ```text
/// a = 1
/// a > 1
/// a <= 1
/// -1 < a < 1
/// ```
pub type RangeCondition = Option<(ColumnRefId, KeyRange)>;
/// Returns all columns involved in the node.
pub fn analyze_range(egraph: &EGraph, enode: &Expr) -> RangeCondition {
use Expr::*;
let column = |i: &Id| {
egraph[*i].nodes.iter().find_map(|e| match e {
Expr::Column(c) => Some(*c),
_ => None,
})
};
let range = |i: &Id| egraph[*i].data.range.as_ref();
let constant = |i: &Id| egraph[*i].data.constant.as_ref();
match enode {
Eq([a, b]) | Gt([a, b]) | GtEq([a, b]) | Lt([a, b]) | LtEq([a, b]) => {
// normalize `v op k` to `k op v`
let (k, v, enode) = if let (Some(v), Some(k)) = (constant(a), column(b)) {
let revnode = match enode {
Eq(_) => Eq([*b, *a]),
Gt(_) => Lt([*b, *a]),
GtEq(_) => LtEq([*b, *a]),
Lt(_) => Gt([*b, *a]),
LtEq(_) => GtEq([*b, *a]),
_ => unreachable!(),
};
(k, v, revnode)
} else if let (Some(k), Some(v)) = (column(a), constant(b)) {
(k, v, enode.clone())
} else {
return None;
};
let start = match enode {
Eq(_) | GtEq(_) => Bound::Included(v.clone()),
Gt(_) => Bound::Excluded(v.clone()),
Lt(_) | LtEq(_) => Bound::Unbounded,
_ => unreachable!(),
};
let end = match enode {
Eq(_) | LtEq(_) => Bound::Included(v.clone()),
Lt(_) => Bound::Excluded(v.clone()),
Gt(_) | GtEq(_) => Bound::Unbounded,
_ => unreachable!(),
};
Some((k, KeyRange { start, end }))
}
And([a, b]) => {
let (ka, ra) = range(a)?;
let (kb, rb) = range(b)?;
if ka != kb {
return None;
}
// if both a and b have bound at start or end, return None
let start = match (&ra.start, &rb.start) {
(Bound::Unbounded, s) | (s, Bound::Unbounded) => s.clone(),
_ => return None,
};
let end = match (&ra.end, &rb.end) {
(Bound::Unbounded, s) | (s, Bound::Unbounded) => s.clone(),
_ => return None,
};
Some((*ka, KeyRange { start, end }))
}
_ => None,
}
}
#[rustfmt::skip]
pub fn filter_scan_rule() -> Vec<Rewrite> { vec![
// pushdown range condition to scan
rw!("filter-scan";
"(filter ?cond (scan ?table ?columns true))" =>
"(scan ?table ?columns ?cond)"
if is_primary_key_range("?cond")
),
rw!("filter-scan-1";
"(filter (and ?cond1 ?cond2) (scan ?table ?columns true))" =>
"(filter ?cond2 (scan ?table ?columns ?cond1))"
if is_primary_key_range("?cond1")
),
]}
/// Returns true if the expression is a primary key range.
fn is_primary_key_range(expr: &str) -> impl Fn(&mut EGraph, Id, &Subst) -> bool {
let var = var(expr);
move |egraph, _, subst| {
let Some((column, _)) = &egraph[subst[var]].data.range else {
return false;
};
egraph
.analysis
.catalog
.get_column(column)
.unwrap()
.is_primary()
}
}