Skip to content
Draft
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
96 changes: 64 additions & 32 deletions vortex-layout/src/layouts/list/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
use std::sync::Arc;

use vortex_array::dtype::DType;
use vortex_array::dtype::FieldNames;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::StructFields;
use vortex_array::expr::BoundExpression;
use vortex_array::expr::BoundLambda;
use vortex_array::expr::bound::get_item;
use vortex_array::expr::bound::not;
use vortex_array::expr::bound::pack;
use vortex_array::expr::traversal::NodeExt;
use vortex_array::expr::traversal::Transformed;
use vortex_array::expr::traversal::TraversalOrder;
Expand Down Expand Up @@ -64,6 +68,9 @@ pub(super) fn get_necessary_bound_list_children(expr: &BoundExpression) -> ListC
pub(super) struct ElementsSplit {
/// Evaluated by the elements reader, once per element.
pub(super) elements: BoundExpression,
/// When `elements` packs several values per element, their names. The lists are then a struct
/// with one list per name, all sharing the offsets and validity.
pub(super) fields: Option<FieldNames>,
/// 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
Expand All @@ -76,66 +83,91 @@ impl ElementsSplit {
pub(super) fn whole_elements(element_dtype: &DType, expr: &BoundExpression) -> Self {
Self {
elements: BoundExpression::new_root(element_dtype.clone()),
fields: None,
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.
/// Split an expression that reads the list through `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.
/// The elements reader evaluates each distinct λ's body, so it reads only the element fields the
/// bodies use. λ's body is bound over its inputs, `{x: element}`, and stepping into `x` puts it
/// over the elements. When the list is also read whole (anything but its validity and lengths),
/// the elements reader reads the elements too. With one value per element the lists are over it;
/// with more, the elements reader packs them, and the lists are a struct with one list per value.
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 {
let Some(DType::List(element_dtype, nullability)) = list_dtype else {
return Ok(None);
};
let [lambda] = lambdas.as_slice() else {
return Ok(None);

let infallible = lambdas.iter().all(BoundLambda::is_infallible);
let mut values = lambdas
.iter()
.map(|lambda| {
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");
step_into_struct_field(lambda.body().clone(), &param, element)
})
.collect::<VortexResult<Vec<_>>>()?;
let whole_index = if whole == ListChildrenNeeded::All {
values.push(BoundExpression::new_root(element_dtype.as_ref().clone()));
values.len() - 1
} else {
0
};

let list_of =
|value: &BoundExpression| DType::List(Arc::new(value.dtype().clone()), nullability);
let (elements, fields, lists) = if let [value] = values.as_slice() {
let lists = BoundExpression::new_root(list_of(value));
(value.clone(), None, vec![lists])
} else {
let names: FieldNames = (0..values.len()).map(|index| index.to_string()).collect();
let lists_dtype = DType::Struct(
StructFields::new(names.clone(), values.iter().map(list_of).collect()),
Nullability::NonNullable,
);
let lists = names
.iter()
.map(|name| get_item(name.clone(), BoundExpression::new_root(lists_dtype.clone())))
.collect();
let elements = pack(names.iter().cloned().zip(values), Nullability::NonNullable);
(elements, Some(names), lists)
};
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(),
let index = match list_transform_lambda(&node) {
Some(lambda) => lambdas.iter().position(|found| found == lambda),
None => node.is_root().then_some(whole_index),
};
Ok(match index {
Some(index) => Transformed {
value: lists[index].clone(),
changed: true,
order: TraversalOrder::Skip,
}
} else {
Transformed::no(node)
},
None => Transformed::no(node),
})
})?
.into_inner();

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

Expand Down
41 changes: 33 additions & 8 deletions vortex-layout/src/layouts/list/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ use vortex_array::VortexSessionExecute;
use vortex_array::arrays::ConstantArray;
use vortex_array::arrays::ListArray;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::StructArray;
use vortex_array::builtins::ArrayBuiltins;
use vortex_array::dtype::DType;
use vortex_array::dtype::FieldMask;
use vortex_array::dtype::FieldNames;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::PType;
use vortex_array::expr::BoundExpression;
use vortex_array::expr::bound;
use vortex_array::expr::root;
use vortex_array::scalar_fn::builtin::operators::Operator;
use vortex_array::validity::Validity;
Expand Down Expand Up @@ -225,6 +228,7 @@ impl ListReader {
elements,
offsets,
create_validity(validity, nullability),
split.fields.as_ref(),
&mut session.create_execution_ctx(),
)?;
lists.apply_bound(&split.outer)
Expand Down Expand Up @@ -304,6 +308,7 @@ impl ListReader {
elements,
offsets,
create_validity(validity, nullability),
split.fields.as_ref(),
&mut ctx,
)?;
lists.apply_bound(&split.outer)
Expand Down Expand Up @@ -494,11 +499,13 @@ fn kept_elements(offsets: &[u64], keep: &Mask) -> VortexResult<(Mask, BufferMut<
Ok((elements, kept_offsets))
}

/// The lists over `elements`, sharing `offsets` and `validity`.
/// The lists over `elements`, or when the elements are a pack of `fields`, a struct of one list
/// per field, all sharing `offsets` and `validity`.
fn lists_over(
elements: ArrayRef,
offsets: ArrayRef,
validity: Validity,
fields: Option<&FieldNames>,
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
let end = offsets
Expand All @@ -508,9 +515,27 @@ fn lists_over(
.vortex_expect("offset value fits in u64");
vortex_ensure_eq!(elements.len(), usize::try_from(end)?);
// SAFETY: the offsets are the file's, or `select_lists`'s prefix sums over them, so they are
// monotonic; the last one was just checked against the elements read; and the validity covers
// the same lists.
Ok(unsafe { ListArray::new_unchecked(elements, offsets, validity) }.into_array())
// monotonic; the last one was just checked against the elements read, which give one value
// per element; and the validity covers the same lists.
let Some(fields) = fields else {
return Ok(unsafe { ListArray::new_unchecked(elements, offsets, validity) }.into_array());
};
let len = offsets.len() - 1;
let root = BoundExpression::new_root(elements.dtype().clone());
let lists = fields
.iter()
.map(|field| {
let values = elements
.clone()
.apply_bound(&bound::get_item(field.clone(), root.clone()))?;
// SAFETY: as above, for each field of the elements.
Ok(
unsafe { ListArray::new_unchecked(values, offsets.clone(), validity.clone()) }
.into_array(),
)
})
.collect::<VortexResult<Vec<_>>>()?;
Ok(StructArray::try_new(fields.clone(), lists, len, Validity::NonNullable)?.into_array())
}

fn selected_row_range(mask: &Mask) -> Option<Range<usize>> {
Expand Down Expand Up @@ -1616,9 +1641,9 @@ mod tests {
"l.elements.g.elements.i",
];

/// A list read through one `list_transform` lambda reads only the element columns its body
/// uses, through nested lists too, whether or not the body can fail. Reading the list whole
/// beside a lambda, or through two lambdas, reads whole elements.
/// A list read through `list_transform` lambdas reads only the element columns their bodies
/// use, through nested lists too, whether or not the bodies can fail. Reading the list whole
/// beside a lambda reads whole elements.
#[rstest]
#[case::field(transform(get_item("f", var("x"))), &["l.offsets", "l.elements.f"])]
#[case::under_another_fn(
Expand Down Expand Up @@ -1649,7 +1674,7 @@ mod tests {
[("a", transform(get_item("a", var("x")))), ("f", transform(get_item("f", var("x"))))],
Nullability::NonNullable,
),
EVERY_LIST_COLUMN,
&["l.offsets", "l.elements.a", "l.elements.f"],
)]
#[case::same_lambda_twice(
pack(
Expand Down
Loading