Repository navigation
Expand file tree
/
Copy pathexpression.rs
More file actions
77 lines (65 loc) · 2.53 KB
/
Copy pathexpression.rs
File metadata and controls
77 lines (65 loc) · 2.53 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use smallvec::SmallVec;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::IntoArray;
use crate::arrays::ConstantArray;
use crate::arrays::ScalarFnArray;
use crate::expr::BoundExpression;
use crate::expr::Expression;
use crate::expr::expression::unbound_variable;
use crate::optimizer::ArrayOptimizer;
use crate::scalar_fn::fns::literal::Literal;
impl ArrayRef {
/// Apply a bound expression to this array, producing a new array in constant time.
pub fn apply_bound(self, expr: &BoundExpression) -> VortexResult<ArrayRef> {
let BoundExpression::Scalar {
scalar_fn,
children,
..
} = expr
else {
return Ok(self);
};
if let Some(scalar) = scalar_fn.as_opt::<Literal>() {
return Ok(ConstantArray::new(scalar.clone(), self.len()).into_array());
}
let len = self.len();
let children = children
.iter()
.map(|child| self.clone().apply_bound(child))
.collect::<VortexResult<SmallVec<[ArrayRef; 4]>>>()?;
let array = ScalarFnArray::try_new_with_len(scalar_fn.clone(), children, len)?.into_array();
array.optimize()
}
/// Apply the expression to this array, producing a new array in constant time.
pub fn apply(self, expr: &Expression) -> VortexResult<ArrayRef> {
// If the expression is a root, return self.
if expr.is_root() {
return Ok(self);
}
if let Expression::Variable(name) = expr {
return Err(unbound_variable(name));
}
// Manually convert literals to ConstantArray.
if let Some(scalar) = expr.as_opt::<Literal>() {
return Ok(ConstantArray::new(scalar.clone(), self.len()).into_array());
}
// Otherwise, collect the child arrays.
let len = self.len();
let children = expr
.children()
.iter()
.map(|e| self.clone().apply(e))
.collect::<VortexResult<SmallVec<[ArrayRef; 4]>>>()?;
// And wrap the scalar function up in an array.
let scalar_fn = expr
.as_scalar()
.vortex_expect("leaves and literals were handled above, so this is a scalar node");
let array = ScalarFnArray::try_new_with_len(scalar_fn.clone(), children, len)?.into_array();
// Optimize the resulting array's root.
array.optimize()
}
}