#[cfg(test)]
mod tests;
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use itertools::Itertools;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use crate::ArrayRef;
use crate::IntoArray;
use crate::arrays::StructArray;
use crate::dtype::DType;
use crate::dtype::FieldName;
use crate::dtype::FieldNames;
use crate::dtype::Nullability;
use crate::dtype::StructFields;
use crate::expr::BoundExpression;
use crate::expr::Expression;
use crate::expr::get_item;
use crate::expr::root;
use crate::expr::traversal::NodeExt;
use crate::expr::traversal::Transformed;
use crate::expr::traversal::TraversalOrder;
use crate::expr::traversal::pre_order_visit_down;
use crate::scalar_fn::internal::row_count::RowCount;
use crate::validity::Validity;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Lambda {
params: FieldNames,
body: Expression,
}
impl Lambda {
pub fn new(
params: impl IntoIterator<Item = impl Into<FieldName>>,
body: Expression,
) -> VortexResult<Self> {
let params: FieldNames = params.into_iter().map(Into::into).collect();
if let Some(param) = params.iter().duplicates().next() {
vortex_bail!("lambda parameter {param} is declared more than once");
}
pre_order_visit_down(&body, |node: &Expression| {
vortex_ensure!(
!reads_outside_lambda(node, params.as_ref()),
"lambda ({}) can only read its parameters, not {node}; reading anything else is \
not supported yet",
params.iter().format(", ")
);
Ok(TraversalOrder::Continue)
})?;
Ok(Self { params, body })
}
pub fn params(&self) -> &[FieldName] {
self.params.as_ref()
}
pub fn body(&self) -> &Expression {
&self.body
}
pub fn return_dtype(&self, param_dtypes: &[DType]) -> VortexResult<DType> {
let inputs = inputs_dtype(self.param_fields(param_dtypes)?);
self.body_over_inputs().return_dtype(&inputs)
}
pub fn bind(&self, param_dtypes: &[DType]) -> VortexResult<BoundLambda> {
let params = self.param_fields(param_dtypes)?;
let body = self
.body_over_inputs()
.bind(&inputs_dtype(params.clone()))?;
Ok(BoundLambda { params, body })
}
pub fn is_infallible(&self) -> bool {
let mut infallible = true;
pre_order_visit_down(&self.body, |node: &Expression| {
infallible &= node
.as_scalar()
.is_none_or(|scalar_fn| scalar_fn.signature().is_infallible());
Ok(if infallible {
TraversalOrder::Continue
} else {
TraversalOrder::Stop
})
})
.vortex_expect("expression traversal cannot fail");
infallible
}
fn param_fields(&self, param_dtypes: &[DType]) -> VortexResult<StructFields> {
vortex_ensure!(
param_dtypes.len() == self.params.len(),
"lambda {self} needs one dtype per parameter, got {}",
param_dtypes.len()
);
Ok(StructFields::new(
self.params.clone(),
param_dtypes.to_vec(),
))
}
fn body_over_inputs(&self) -> Expression {
self.body
.clone()
.transform_down(|node| {
Ok(match &node {
Expression::Variable(name) => Transformed {
value: get_item(name.clone(), root()),
changed: true,
order: TraversalOrder::Skip,
},
_ => Transformed::no(node),
})
})
.vortex_expect("expression traversal cannot fail")
.into_inner()
}
}
fn inputs_dtype(params: StructFields) -> DType {
DType::Struct(params, Nullability::NonNullable)
}
impl Display for Lambda {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "({}) -> {}", self.params.iter().format(", "), self.body)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BoundLambda {
params: StructFields,
body: BoundExpression,
}
impl BoundLambda {
pub fn params(&self) -> &StructFields {
&self.params
}
pub fn body(&self) -> &BoundExpression {
&self.body
}
pub fn dtype(&self) -> &DType {
self.body.dtype()
}
pub fn is_bound_to(&self, param_dtypes: &[DType]) -> bool {
self.params.fields().eq(param_dtypes.iter().cloned())
}
pub fn is_infallible(&self) -> bool {
let mut infallible = true;
pre_order_visit_down(&self.body, |node: &BoundExpression| {
infallible &= node
.as_scalar()
.is_none_or(|scalar_fn| scalar_fn.signature().is_infallible());
Ok(if infallible {
TraversalOrder::Continue
} else {
TraversalOrder::Stop
})
})
.vortex_expect("bound expression traversal cannot fail");
infallible
}
pub fn evaluate(&self, args: Vec<ArrayRef>, len: usize) -> VortexResult<ArrayRef> {
vortex_ensure!(
args.len() == self.params.nfields(),
"lambda {self} needs one array per parameter, got {}",
args.len()
);
for ((name, dtype), arg) in self
.params
.names()
.iter()
.zip(self.params.fields())
.zip(&args)
{
vortex_ensure!(
arg.dtype() == &dtype,
"lambda {self}: parameter {name} is bound to {dtype}, not {}",
arg.dtype()
);
}
let inputs = StructArray::try_new(
self.params.names().clone(),
args,
len,
Validity::NonNullable,
)?
.into_array();
inputs.apply_bound(&self.body)
}
}
impl Display for BoundLambda {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"({}) -> {}",
self.params.names().iter().format(", "),
self.body
)
}
}
fn reads_outside_lambda(node: &Expression, params: &[FieldName]) -> bool {
match node {
Expression::Root => true,
Expression::Variable(name) => !params.contains(name),
Expression::Scalar { scalar_fn, .. } => {
scalar_fn.is::<RowCount>() || scalar_fn.id().as_ref() == "vortex.row_idx"
}
}
}