-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathexpression.rs
More file actions
73 lines (61 loc) · 2.3 KB
/
Copy pathexpression.rs
File metadata and controls
73 lines (61 loc) · 2.3 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use itertools::Itertools;
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::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 children: Vec<_> = children
.iter()
.map(|child| self.clone().apply_bound(child))
.try_collect()?;
let array =
ScalarFnArray::try_new_with_len(scalar_fn.clone(), children, self.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);
}
// 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 children: Vec<_> = expr
.children()
.iter()
.map(|e| self.clone().apply(e))
.try_collect()?;
// And wrap the scalar function up in an array.
let scalar_fn = expr
.as_scalar()
.vortex_expect("root and literal were handled above, so this is a scalar node");
let array =
ScalarFnArray::try_new_with_len(scalar_fn.clone(), children, self.len())?.into_array();
// Optimize the resulting array's root.
array.optimize()
}
}