vortex_array/
expression.rs1use smallvec::SmallVec;
5use vortex_error::VortexExpect;
6use vortex_error::VortexResult;
7
8use crate::ArrayRef;
9use crate::IntoArray;
10use crate::arrays::ConstantArray;
11use crate::arrays::ScalarFnArray;
12use crate::expr::BoundExpression;
13use crate::expr::Expression;
14use crate::expr::expression::unbound_variable;
15use crate::optimizer::ArrayOptimizer;
16use crate::scalar_fn::fns::literal::Literal;
17
18impl ArrayRef {
19 pub fn apply_bound(self, expr: &BoundExpression) -> VortexResult<ArrayRef> {
21 let BoundExpression::Scalar {
22 scalar_fn,
23 children,
24 ..
25 } = expr
26 else {
27 return Ok(self);
28 };
29
30 if let Some(scalar) = scalar_fn.as_opt::<Literal>() {
31 return Ok(ConstantArray::new(scalar.clone(), self.len()).into_array());
32 }
33
34 let len = self.len();
35 let children = children
36 .iter()
37 .map(|child| self.clone().apply_bound(child))
38 .collect::<VortexResult<SmallVec<[ArrayRef; 4]>>>()?;
39
40 let array = ScalarFnArray::try_new_with_len(scalar_fn.clone(), children, len)?.into_array();
41
42 array.optimize()
43 }
44
45 pub fn apply(self, expr: &Expression) -> VortexResult<ArrayRef> {
47 if expr.is_root() {
49 return Ok(self);
50 }
51 if let Expression::Variable(name) = expr {
52 return Err(unbound_variable(name));
53 }
54
55 if let Some(scalar) = expr.as_opt::<Literal>() {
57 return Ok(ConstantArray::new(scalar.clone(), self.len()).into_array());
58 }
59
60 let len = self.len();
62 let children = expr
63 .children()
64 .iter()
65 .map(|e| self.clone().apply(e))
66 .collect::<VortexResult<SmallVec<[ArrayRef; 4]>>>()?;
67
68 let scalar_fn = expr
70 .as_scalar()
71 .vortex_expect("leaves and literals were handled above, so this is a scalar node");
72 let array = ScalarFnArray::try_new_with_len(scalar_fn.clone(), children, len)?.into_array();
73
74 array.optimize()
76 }
77}