|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_array::expr::Expression; |
| 5 | +use vortex_array::expr::is_root; |
| 6 | +use vortex_array::expr::not; |
| 7 | +use vortex_array::expr::root; |
| 8 | +use vortex_array::scalar_fn::fns::is_not_null::IsNotNull; |
| 9 | +use vortex_array::scalar_fn::fns::is_null::IsNull; |
| 10 | +use vortex_array::scalar_fn::fns::list_length::ListLength; |
| 11 | +use vortex_error::VortexResult; |
| 12 | + |
| 13 | +/// The minimal set of list children an expression needs for evaluation. |
| 14 | +/// |
| 15 | +/// For example: |
| 16 | +/// - `is_null(root())` only needs the validity child. |
| 17 | +/// - `list_length(root())` only needs the offsets and validity children. |
| 18 | +/// - `root()` needs elements, offsets, and validity children. |
| 19 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 20 | +pub(super) enum ListChildrenNeeded { |
| 21 | + /// Only the validity child is needed (`is_null` / `is_not_null`). |
| 22 | + Validity, |
| 23 | + /// Only the offsets and validity children are needed (`list_length`). |
| 24 | + OffsetsAndValidity, |
| 25 | + /// All children are needed. |
| 26 | + All, |
| 27 | +} |
| 28 | + |
| 29 | +/// The minimal set of list children needed to evaluate `expr`, where `root()` is a field with list dtype. |
| 30 | +pub(super) fn get_necessary_list_children(expr: &Expression) -> ListChildrenNeeded { |
| 31 | + if is_null_root(expr) { |
| 32 | + return ListChildrenNeeded::Validity; |
| 33 | + } |
| 34 | + |
| 35 | + if is_list_length_root(expr) { |
| 36 | + return ListChildrenNeeded::OffsetsAndValidity; |
| 37 | + } |
| 38 | + |
| 39 | + if is_root(expr) { |
| 40 | + return ListChildrenNeeded::All; |
| 41 | + } |
| 42 | + |
| 43 | + // Otherwise the requirement is the max over the operands. Childless expressions that never |
| 44 | + // touch the list, such as literals, fall back to the cheapest usable child. |
| 45 | + expr.children() |
| 46 | + .iter() |
| 47 | + .map(get_necessary_list_children) |
| 48 | + .max() |
| 49 | + .unwrap_or(ListChildrenNeeded::Validity) |
| 50 | +} |
| 51 | + |
| 52 | +fn is_null_root(expr: &Expression) -> bool { |
| 53 | + (expr.is::<IsNull>() || expr.is::<IsNotNull>()) |
| 54 | + && expr.children().len() == 1 |
| 55 | + && is_root(expr.child(0)) |
| 56 | +} |
| 57 | + |
| 58 | +fn is_list_length_root(expr: &Expression) -> bool { |
| 59 | + expr.is::<ListLength>() && expr.children().len() == 1 && is_root(expr.child(0)) |
| 60 | +} |
| 61 | + |
| 62 | +/// Rewrite a validity-class expression so it can be evaluated against the list's validity bool |
| 63 | +/// array (`true` == valid row): `is_not_null(root())` becomes `root()` and `is_null(root())` |
| 64 | +/// becomes `not(root())`. All other nodes are rebuilt with rewritten children. |
| 65 | +pub(super) fn rewrite_validity_expr(expr: &Expression) -> VortexResult<Expression> { |
| 66 | + if expr.is::<IsNotNull>() && expr.children().len() == 1 && is_root(expr.child(0)) { |
| 67 | + return Ok(root()); |
| 68 | + } |
| 69 | + if expr.is::<IsNull>() && expr.children().len() == 1 && is_root(expr.child(0)) { |
| 70 | + return Ok(not(root())); |
| 71 | + } |
| 72 | + let children = expr |
| 73 | + .children() |
| 74 | + .iter() |
| 75 | + .map(rewrite_validity_expr) |
| 76 | + .collect::<VortexResult<Vec<_>>>()?; |
| 77 | + expr.clone().with_children(children) |
| 78 | +} |
| 79 | + |
| 80 | +/// Rewrite an offsets-class expression so it can be evaluated against an array of list lengths. |
| 81 | +/// `list_length(root())` becomes `root()`. Other references to `root()` are left intact: for |
| 82 | +/// offsets-class expressions they can only be validity checks, and the lengths array carries the |
| 83 | +/// same validity as the original list. |
| 84 | +pub(super) fn rewrite_offsets_expr(expr: &Expression) -> VortexResult<Expression> { |
| 85 | + if is_list_length_root(expr) { |
| 86 | + return Ok(root()); |
| 87 | + } |
| 88 | + |
| 89 | + let children = expr |
| 90 | + .children() |
| 91 | + .iter() |
| 92 | + .map(rewrite_offsets_expr) |
| 93 | + .collect::<VortexResult<Vec<_>>>()?; |
| 94 | + expr.clone().with_children(children) |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod tests { |
| 99 | + use rstest::rstest; |
| 100 | + use vortex_array::dtype::DType; |
| 101 | + use vortex_array::dtype::Nullability; |
| 102 | + use vortex_array::dtype::PType; |
| 103 | + use vortex_array::expr::cast; |
| 104 | + use vortex_array::expr::eq; |
| 105 | + use vortex_array::expr::gt; |
| 106 | + use vortex_array::expr::is_not_null; |
| 107 | + use vortex_array::expr::is_null; |
| 108 | + use vortex_array::expr::list_length; |
| 109 | + use vortex_array::expr::lit; |
| 110 | + use vortex_array::expr::not; |
| 111 | + use vortex_array::expr::root; |
| 112 | + |
| 113 | + use super::*; |
| 114 | + |
| 115 | + /// `get_necessary_list_children` keys off the deepest list child an expression touches; `All` |
| 116 | + /// is the always-correct default for anything not specifically recognized. |
| 117 | + #[rstest] |
| 118 | + // `is_null` / `is_not_null` of the list itself need only validity. |
| 119 | + #[case::is_null(is_null(root()), ListChildrenNeeded::Validity)] |
| 120 | + #[case::is_not_null(is_not_null(root()), ListChildrenNeeded::Validity)] |
| 121 | + // Compound over validity-only operands stays validity. |
| 122 | + #[case::not_is_null(not(is_null(root())), ListChildrenNeeded::Validity)] |
| 123 | + // A list-independent (constant) expression falls to the cheapest usable child. |
| 124 | + #[case::constant(lit(5), ListChildrenNeeded::Validity)] |
| 125 | + // `list_length(root())` needs offsets and validity, but not elements. |
| 126 | + #[case::list_length(list_length(root()), ListChildrenNeeded::OffsetsAndValidity)] |
| 127 | + // Compound over offsets-only operands stays offsets. |
| 128 | + #[case::list_length_filter( |
| 129 | + gt(list_length(root()), lit(1u64)), |
| 130 | + ListChildrenNeeded::OffsetsAndValidity |
| 131 | + )] |
| 132 | + #[case::cast_list_length( |
| 133 | + cast( |
| 134 | + list_length(root()), |
| 135 | + DType::Primitive(PType::I64, Nullability::Nullable), |
| 136 | + ), |
| 137 | + ListChildrenNeeded::OffsetsAndValidity |
| 138 | + )] |
| 139 | + // A bare list reference needs the elements. |
| 140 | + #[case::bare_root(root(), ListChildrenNeeded::All)] |
| 141 | + // Any other fn over the list needs the elements. |
| 142 | + #[case::not_root(not(root()), ListChildrenNeeded::All)] |
| 143 | + // `is_null` only short-circuits to validity when its argument is the list itself. |
| 144 | + #[case::is_null_of_derived(is_null(not(root())), ListChildrenNeeded::All)] |
| 145 | + // Max over operands: validity + elements => elements. |
| 146 | + #[case::validity_and_elements(eq(is_null(root()), root()), ListChildrenNeeded::All)] |
| 147 | + fn classify_expr_class(#[case] expr: Expression, #[case] expected: ListChildrenNeeded) { |
| 148 | + assert_eq!(get_necessary_list_children(&expr), expected); |
| 149 | + } |
| 150 | +} |
0 commit comments