-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathoptimize.rs
More file actions
291 lines (255 loc) · 9.85 KB
/
Copy pathoptimize.rs
File metadata and controls
291 lines (255 loc) · 9.85 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 std::cell::RefCell;
use itertools::Itertools;
use vortex_error::VortexResult;
use vortex_error::vortex_err;
use vortex_utils::aliases::hash_map::HashMap;
use crate::dtype::DType;
use crate::expr::Expression;
use crate::expr::transform::match_between::find_between;
use crate::scalar_fn::ExpressionReduceNode;
use crate::scalar_fn::SimplifyCtx;
impl Expression {
/// Optimize the root expression node only, iterating to convergence.
///
/// This applies optimization rules repeatedly until no more changes occur:
/// 1. `simplify_untyped` - type-independent simplifications
/// 2. `simplify` - type-aware simplifications
/// 3. `reduce` - abstract reduction rules via `ReduceNode`
pub fn optimize(&self, scope: &DType) -> VortexResult<Expression> {
let cache = SimplifyCache::new(scope);
Ok(self.try_optimize(&cache)?.unwrap_or_else(|| self.clone()))
}
/// Apply this node's own untyped simplification rule, if it has one.
///
/// Non-scalar nodes carry no rules, so they never simplify.
fn simplify_untyped_node(&self) -> VortexResult<Option<Expression>> {
match self {
Expression::Scalar { scalar_fn, .. } => scalar_fn.simplify_untyped(self),
_ => Ok(None),
}
}
/// Apply this node's own type-aware simplification rule, if it has one.
fn simplify_node(&self, ctx: &dyn SimplifyCtx) -> VortexResult<Option<Expression>> {
match self {
Expression::Scalar { scalar_fn, .. } => scalar_fn.simplify(self, ctx),
_ => Ok(None),
}
}
/// Apply this node's own abstract reduction rule, if it has one.
fn reduce_node<'a>(
&self,
node: &ExpressionReduceNode<'a>,
) -> VortexResult<Option<ExpressionReduceNode<'a>>> {
match self {
Expression::Scalar { scalar_fn, .. } => scalar_fn.reduce_expression(node),
_ => Ok(None),
}
}
/// Try to optimize the root expression node only, returning None if no optimizations applied.
fn try_optimize(&self, cache: &SimplifyCache<'_>) -> VortexResult<Option<Expression>> {
// Copy-on-write: `current` stays None until a rule fires, so unchanged nodes (the common
// case) are never cloned.
let mut current: Option<Expression> = None;
let mut loop_counter = 0;
loop {
if loop_counter > 100 {
vortex_error::vortex_bail!(
"Exceeded maximum optimization iterations (possible infinite loop)"
);
}
loop_counter += 1;
let expr = current.as_ref().unwrap_or(self);
let mut changed = false;
// Try simplify_untyped
if let Some(simplified) = expr.simplify_untyped_node()? {
current = Some(simplified);
changed = true;
}
// Try simplify (typed)
let expr = current.as_ref().unwrap_or(self);
if let Some(simplified) = expr.simplify_node(cache)? {
current = Some(simplified);
changed = true;
}
// Try reduce via ReduceNode. The node borrows the expression and scope, so
// constructing it is free; the block scopes the borrows so `current` can be updated.
let reduced = {
let expr = current.as_ref().unwrap_or(self);
let reduce_node = ExpressionReduceNode::new(expr, cache.scope);
expr.reduce_node(&reduce_node)?
.map(ExpressionReduceNode::into_expression)
};
if let Some(reduced_expr) = reduced {
current = Some(reduced_expr);
changed = true;
}
if !changed {
break;
}
}
Ok(current)
}
/// Optimize the entire expression tree recursively.
///
/// Optimizes children first (bottom-up), then optimizes the root.
pub fn optimize_recursive(&self, scope: &DType) -> VortexResult<Expression> {
Ok(self
.clone()
.try_optimize_recursive(scope)?
.unwrap_or_else(|| self.clone()))
}
/// Try to optimize the entire expression tree recursively.
pub fn try_optimize_recursive(&self, scope: &DType) -> VortexResult<Option<Expression>> {
let cache = SimplifyCache::new(scope);
let result = self.try_optimize_recursive_inner(&cache)?;
// Apply the between optimization once at the top level only.
// TODO(ngates): remove the "between" optimization, or rewrite it to not always convert
// to CNF?
Ok(Some(find_between(result.unwrap_or_else(|| self.clone()))))
}
fn try_optimize_recursive_inner(
&self,
cache: &SimplifyCache<'_>,
) -> VortexResult<Option<Expression>> {
// First optimize the root
let mut current = self.try_optimize(cache)?;
// Then recursively optimize children. The new children vector is only allocated once a
// child actually changes, so fully-optimized subtrees cost no allocations.
let expr = current.as_ref().unwrap_or(self);
let children = expr.children();
let mut new_children: Option<Vec<Expression>> = None;
for (idx, child) in children.iter().enumerate() {
if let Some(optimized) = child.try_optimize_recursive_inner(cache)? {
new_children
.get_or_insert_with(|| children[..idx].to_vec())
.push(optimized);
} else if let Some(new_children) = new_children.as_mut() {
new_children.push(child.clone());
}
}
if let Some(new_children) = new_children {
let updated = expr.clone().with_children(new_children)?;
// After updating children, try to optimize root again
current = Some(updated.try_optimize(cache)?.unwrap_or(updated));
}
Ok(current)
}
}
struct SimplifyCache<'a> {
scope: &'a DType,
dtype_cache: RefCell<HashMap<Expression, DType>>,
}
impl<'a> SimplifyCache<'a> {
fn new(scope: &'a DType) -> Self {
Self {
scope,
dtype_cache: RefCell::new(HashMap::new()),
}
}
}
impl SimplifyCtx for SimplifyCache<'_> {
fn return_dtype(&self, expr: &Expression) -> VortexResult<DType> {
// If the expression is "root", return the scope dtype
if expr.is_root() {
return Ok(self.scope.clone());
}
if let Some(dtype) = self.dtype_cache.borrow().get(expr) {
return Ok(dtype.clone());
}
// Otherwise, compute dtype from children
let input_dtypes: Vec<_> = expr
.children()
.iter()
.map(|c| self.return_dtype(c))
.try_collect()?;
let dtype = expr
.as_scalar()
.ok_or_else(|| vortex_err!("cannot type a non-scalar expression: {expr}"))?
.return_dtype(&input_dtypes)?;
self.dtype_cache
.borrow_mut()
.insert(expr.clone(), dtype.clone());
Ok(dtype)
}
}
#[cfg(test)]
mod tests {
use vortex_error::VortexResult;
use vortex_error::vortex_err;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::dtype::StructFields;
use crate::expr::cast;
use crate::expr::col;
use crate::expr::eq;
use crate::expr::get_item;
use crate::expr::lit;
use crate::expr::lt_eq;
use crate::expr::or;
use crate::expr::root;
use crate::expr::test_harness::struct_dtype;
use crate::scalar::Scalar;
use crate::scalar_fn::fns::literal::Literal;
#[test]
fn optimize_or_chain_correctness() -> VortexResult<()> {
let expr = or(
eq(get_item("x", root()), lit(1i32)),
eq(get_item("x", root()), lit(2i32)),
);
let scope = DType::Struct(
StructFields::new(
["x"].into(),
vec![DType::Primitive(PType::I32, Nullability::NonNullable)],
),
Nullability::NonNullable,
);
let optimized = expr.optimize_recursive(&scope)?;
let s = optimized.to_string();
assert!(s.contains("$.x"), "expected $.x in {s}");
assert!(s.contains("1i32") || s.contains('1'), "expected 1 in {s}");
assert!(s.contains("2i32") || s.contains('2'), "expected 2 in {s}");
Ok(())
}
#[test]
fn optimize_folds_cast_of_literal_in_comparison() -> VortexResult<()> {
let expr = lt_eq(
get_item("x", root()),
cast(
lit(3i32),
DType::Primitive(PType::F64, Nullability::NonNullable),
),
);
let scope = DType::Struct(
StructFields::new(
["x"].into(),
vec![DType::Primitive(PType::F64, Nullability::NonNullable)],
),
Nullability::NonNullable,
);
let optimized = expr.optimize_recursive(&scope)?;
// Prune rules pattern-match a bare Literal on the comparison RHS; a cast wrapper
// silently disables pruning.
let rhs = optimized
.child(1)
.as_opt::<Literal>()
.ok_or_else(|| vortex_err!("expected a bare literal RHS, got {optimized}"))?;
assert_eq!(rhs, &Scalar::primitive(3.0f64, Nullability::NonNullable));
Ok(())
}
#[test]
fn optimization_folds_a_literal_cast() -> VortexResult<()> {
let expr = lt_eq(
col("a"),
cast(
lit(3_i32),
DType::Primitive(PType::I64, Nullability::NonNullable),
),
);
let optimized = expr.optimize_recursive(&struct_dtype())?;
assert_ne!(optimized, expr, "casting a literal should fold");
Ok(())
}
}