vortex_array/expr/lambda/
mod.rs1#[cfg(test)]
17mod tests;
18
19use std::fmt;
20use std::fmt::Display;
21use std::fmt::Formatter;
22
23use itertools::Itertools;
24use vortex_error::VortexExpect;
25use vortex_error::VortexResult;
26use vortex_error::vortex_bail;
27use vortex_error::vortex_ensure;
28
29use crate::ArrayRef;
30use crate::IntoArray;
31use crate::arrays::StructArray;
32use crate::dtype::DType;
33use crate::dtype::FieldName;
34use crate::dtype::FieldNames;
35use crate::dtype::Nullability;
36use crate::dtype::StructFields;
37use crate::expr::BoundExpression;
38use crate::expr::Expression;
39use crate::expr::get_item;
40use crate::expr::root;
41use crate::expr::traversal::NodeExt;
42use crate::expr::traversal::Transformed;
43use crate::expr::traversal::TraversalOrder;
44use crate::expr::traversal::pre_order_visit_down;
45use crate::scalar_fn::internal::row_count::RowCount;
46use crate::validity::Validity;
47
48#[derive(Clone, Debug, PartialEq, Eq, Hash)]
60pub struct Lambda {
61 params: FieldNames,
62 body: Expression,
63}
64
65impl Lambda {
66 pub fn new(
71 params: impl IntoIterator<Item = impl Into<FieldName>>,
72 body: Expression,
73 ) -> VortexResult<Self> {
74 let params: FieldNames = params.into_iter().map(Into::into).collect();
75 if let Some(param) = params.iter().duplicates().next() {
76 vortex_bail!("lambda parameter {param} is declared more than once");
77 }
78 pre_order_visit_down(&body, |node: &Expression| {
79 vortex_ensure!(
80 !reads_outside_lambda(node, params.as_ref()),
81 "lambda ({}) can only read its parameters, not {node}; reading anything else is \
82 not supported yet",
83 params.iter().format(", ")
84 );
85 Ok(TraversalOrder::Continue)
86 })?;
87
88 Ok(Self { params, body })
89 }
90
91 pub fn params(&self) -> &[FieldName] {
93 self.params.as_ref()
94 }
95
96 pub fn body(&self) -> &Expression {
98 &self.body
99 }
100
101 pub fn return_dtype(&self, param_dtypes: &[DType]) -> VortexResult<DType> {
103 let inputs = inputs_dtype(self.param_fields(param_dtypes)?);
104 self.body_over_inputs().return_dtype(&inputs)
105 }
106
107 pub fn bind(&self, param_dtypes: &[DType]) -> VortexResult<BoundLambda> {
113 let params = self.param_fields(param_dtypes)?;
114 let body = self
115 .body_over_inputs()
116 .bind(&inputs_dtype(params.clone()))?;
117 Ok(BoundLambda { params, body })
118 }
119
120 pub fn is_infallible(&self) -> bool {
123 let mut infallible = true;
124 pre_order_visit_down(&self.body, |node: &Expression| {
125 infallible &= node
126 .as_scalar()
127 .is_none_or(|scalar_fn| scalar_fn.signature().is_infallible());
128 Ok(if infallible {
129 TraversalOrder::Continue
130 } else {
131 TraversalOrder::Stop
132 })
133 })
134 .vortex_expect("expression traversal cannot fail");
135 infallible
136 }
137
138 fn param_fields(&self, param_dtypes: &[DType]) -> VortexResult<StructFields> {
139 vortex_ensure!(
140 param_dtypes.len() == self.params.len(),
141 "lambda {self} needs one dtype per parameter, got {}",
142 param_dtypes.len()
143 );
144 Ok(StructFields::new(
145 self.params.clone(),
146 param_dtypes.to_vec(),
147 ))
148 }
149
150 fn body_over_inputs(&self) -> Expression {
155 self.body
156 .clone()
157 .transform_down(|node| {
158 Ok(match &node {
159 Expression::Variable(name) => Transformed {
160 value: get_item(name.clone(), root()),
161 changed: true,
162 order: TraversalOrder::Skip,
163 },
164 _ => Transformed::no(node),
165 })
166 })
167 .vortex_expect("expression traversal cannot fail")
168 .into_inner()
169 }
170}
171
172fn inputs_dtype(params: StructFields) -> DType {
174 DType::Struct(params, Nullability::NonNullable)
175}
176
177impl Display for Lambda {
178 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
179 write!(f, "({}) -> {}", self.params.iter().format(", "), self.body)
180 }
181}
182
183#[derive(Clone, Debug, PartialEq, Eq, Hash)]
188pub struct BoundLambda {
189 params: StructFields,
190 body: BoundExpression,
191}
192
193impl BoundLambda {
194 pub fn params(&self) -> &StructFields {
196 &self.params
197 }
198
199 pub fn body(&self) -> &BoundExpression {
201 &self.body
202 }
203
204 pub fn dtype(&self) -> &DType {
206 self.body.dtype()
207 }
208
209 pub fn is_bound_to(&self, param_dtypes: &[DType]) -> bool {
211 self.params.fields().eq(param_dtypes.iter().cloned())
212 }
213
214 pub fn is_infallible(&self) -> bool {
216 let mut infallible = true;
217 pre_order_visit_down(&self.body, |node: &BoundExpression| {
218 infallible &= node
219 .as_scalar()
220 .is_none_or(|scalar_fn| scalar_fn.signature().is_infallible());
221 Ok(if infallible {
222 TraversalOrder::Continue
223 } else {
224 TraversalOrder::Stop
225 })
226 })
227 .vortex_expect("bound expression traversal cannot fail");
228 infallible
229 }
230
231 pub fn evaluate(&self, args: Vec<ArrayRef>, len: usize) -> VortexResult<ArrayRef> {
237 vortex_ensure!(
238 args.len() == self.params.nfields(),
239 "lambda {self} needs one array per parameter, got {}",
240 args.len()
241 );
242 for ((name, dtype), arg) in self
243 .params
244 .names()
245 .iter()
246 .zip(self.params.fields())
247 .zip(&args)
248 {
249 vortex_ensure!(
250 arg.dtype() == &dtype,
251 "lambda {self}: parameter {name} is bound to {dtype}, not {}",
252 arg.dtype()
253 );
254 }
255 let inputs = StructArray::try_new(
256 self.params.names().clone(),
257 args,
258 len,
259 Validity::NonNullable,
260 )?
261 .into_array();
262 inputs.apply_bound(&self.body)
263 }
264}
265
266impl Display for BoundLambda {
267 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
268 write!(
269 f,
270 "({}) -> {}",
271 self.params.names().iter().format(", "),
272 self.body
273 )
274 }
275}
276
277fn reads_outside_lambda(node: &Expression, params: &[FieldName]) -> bool {
282 match node {
283 Expression::Root => true,
284 Expression::Variable(name) => !params.contains(name),
285 Expression::Scalar { scalar_fn, .. } => {
287 scalar_fn.is::<RowCount>() || scalar_fn.id().as_ref() == "vortex.row_idx"
288 }
289 }
290}