-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathexpression.rs
More file actions
76 lines (65 loc) · 2.6 KB
/
Copy pathexpression.rs
File metadata and controls
76 lines (65 loc) · 2.6 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use itertools::Itertools;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
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 (scalar_fn, children) = match expr {
// Root evaluates to the scope, which is this array.
BoundExpression::Root { .. } => return Ok(self),
BoundExpression::Variable { .. } => {
vortex_bail!("cannot apply detached variable")
}
BoundExpression::Scalar {
scalar_fn,
children,
..
} => (scalar_fn, children),
};
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> {
let scalar_fn = match expr {
// Root evaluates to the scope, which is this array.
Expression::Root => return Ok(self),
Expression::Variable(..) => {
vortex_bail!("cannot apply detached variable")
}
Expression::Scalar { scalar_fn, .. } => scalar_fn,
};
// 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()?;
let array =
ScalarFnArray::try_new_with_len(scalar_fn.clone(), children, self.len())?.into_array();
// Optimize the resulting array's root.
array.optimize()
}
}