Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions vortex-layout/src/layouts/list/expr.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::sync::Arc;

use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability;
use vortex_array::expr::BoundExpression;
use vortex_array::expr::BoundLambda;
use vortex_array::expr::bound::not;
use vortex_array::expr::traversal::NodeExt;
use vortex_array::expr::traversal::Transformed;
use vortex_array::expr::traversal::TraversalOrder;
use vortex_array::scalar_fn::builtin::is_not_null::IsNotNull;
use vortex_array::scalar_fn::builtin::is_null::IsNull;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_functions::list_length::ListLength;
use vortex_functions::list_transform::ListTransform;
use vortex_functions::list_transform::ListTransformOptions;

use crate::layouts::struct_::Item;
use crate::layouts::struct_::step_into_struct_field;

/// The minimal set of list children an expression needs for evaluation.
///
Expand Down Expand Up @@ -47,6 +59,124 @@ pub(super) fn get_necessary_bound_list_children(expr: &BoundExpression) -> ListC
.unwrap_or(ListChildrenNeeded::Validity)
}

/// How a read of the list splits between its elements reader and the lists built over what that
/// reader returns.
pub(super) struct ElementsSplit {
/// Evaluated by the elements reader, once per element.
pub(super) elements: BoundExpression,
/// The expression over the lists.
pub(super) outer: BoundExpression,
/// Whether nothing in `elements` can fail. A fallible one must not see the elements of null
/// lists, which can hold anything.
pub(super) infallible: bool,
}

impl ElementsSplit {
/// Read whole elements and evaluate `expr` over the list.
pub(super) fn whole_elements(element_dtype: &DType, expr: &BoundExpression) -> Self {
Self {
elements: BoundExpression::new_root(element_dtype.clone()),
outer: expr.clone(),
infallible: true,
}
}
}

/// Split an expression that reads the list only through one `list_transform($, λ)` into what the
/// elements reader evaluates and the expression over the lists of its values.
///
/// The elements reader evaluates λ's body, so it reads only the element fields the body uses.
/// The body is bound over its inputs, `{x: element}`, and stepping into `x` puts it over the
/// elements. Several distinct lambdas, or the list also read whole (anything but its validity and
/// lengths), read whole elements.
pub(super) fn split_list_transform(expr: &BoundExpression) -> VortexResult<Option<ElementsSplit>> {
let mut lambdas = Vec::new();
let mut whole = ListChildrenNeeded::Validity;
let mut list_dtype = None;
collect_list_uses(expr, &mut lambdas, &mut whole, &mut list_dtype);
let Some(DType::List(_, nullability)) = list_dtype else {
return Ok(None);
};
let [lambda] = lambdas.as_slice() else {
return Ok(None);
};
if whole == ListChildrenNeeded::All {
return Ok(None);
}

let param = Item::Field(lambda.params().names()[0].clone());
let element = lambda
.params()
.field_by_index(0)
.vortex_expect("list_transform's lambda takes the element");
let elements = step_into_struct_field(lambda.body().clone(), &param, element)?;
let lists = BoundExpression::new_root(DType::List(
Arc::new(elements.dtype().clone()),
nullability,
));

// Every `list_transform($, λ)` is this lambda, and any other `$` is under `is_null` or
// `list_length`, which the lists of λ's values answer alike.
let outer = expr
.clone()
.transform_down(|node| {
Ok(if list_transform_lambda(&node).is_some() || node.is_root() {
Transformed {
value: lists.clone(),
changed: true,
order: TraversalOrder::Skip,
}
} else {
Transformed::no(node)
})
})?
.into_inner();

Ok(Some(ElementsSplit {
elements,
outer,
infallible: lambda.is_infallible(),
}))
}

/// The bound lambda of a `list_transform($, λ)` node.
fn list_transform_lambda(node: &BoundExpression) -> Option<&BoundLambda> {
match node.as_opt::<ListTransform>() {
Some(ListTransformOptions::Bound(lambda)) if node.child(0).is_root() => Some(lambda),
_ => None,
}
}

/// Collect the distinct lambdas of `list_transform($, λ)` nodes and what the rest of the
/// expression needs of the list. `list_dtype` is set once a lambda is found.
fn collect_list_uses(
expr: &BoundExpression,
lambdas: &mut Vec<BoundLambda>,
whole: &mut ListChildrenNeeded,
list_dtype: &mut Option<DType>,
) {
if let Some(lambda) = list_transform_lambda(expr) {
if !lambdas.contains(lambda) {
lambdas.push(lambda.clone());
}
*list_dtype = Some(expr.child(0).dtype().clone());
return;
}
let needs = if is_bound_null_root(expr) {
ListChildrenNeeded::Validity
} else if is_bound_list_length_root(expr) {
ListChildrenNeeded::OffsetsAndValidity
} else if expr.is_root() {
ListChildrenNeeded::All
} else {
for child in expr.children() {
collect_list_uses(child, lambdas, whole, list_dtype);
}
return;
};
*whole = (*whole).max(needs);
}

fn is_bound_null_root(expr: &BoundExpression) -> bool {
(expr.as_scalar().is_some_and(|f| f.is::<IsNull>())
|| expr.as_scalar().is_some_and(|f| f.is::<IsNotNull>()))
Expand Down
Loading
Loading