Skip to content

Commit 7ade901

Browse files
committed
update list_transform tests for develop
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
1 parent cd55cf5 commit 7ade901

2 files changed

Lines changed: 37 additions & 19 deletions

File tree

‎vortex-array/src/scalar_fn/fns/list_transform/tests.rs‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

44
use std::sync::Arc;
5+
use std::sync::LazyLock;
56

67
use rstest::rstest;
78
use vortex_buffer::buffer;
89
use vortex_error::VortexResult;
10+
use vortex_session::VortexSession;
911

1012
use super::ListTransform;
1113
use super::ListTransformOptions;
@@ -14,7 +16,6 @@ use crate::ArrayRef;
1416
use crate::IntoArray;
1517
use crate::RecursiveCanonical;
1618
use crate::VortexSessionExecute;
17-
use crate::array_session;
1819
use crate::arrays::BoolArray;
1920
use crate::arrays::Dict;
2021
use crate::arrays::DictArray;
@@ -42,12 +43,13 @@ use crate::expr::list_transform;
4243
use crate::expr::lit;
4344
use crate::expr::proto::ExprSerializeProtoExt;
4445
use crate::expr::root;
45-
use crate::expr::transform::replace;
4646
use crate::expr::var;
4747
use crate::scalar::Scalar;
4848
use crate::scalar_fn::ScalarFnVTable;
4949
use crate::validity::Validity;
5050

51+
static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
52+
5153
/// `[[1, 2], null (hiding [i32::MAX]), [3, null]]`
5254
fn lists() -> VortexResult<ArrayRef> {
5355
list_of(
@@ -68,7 +70,7 @@ fn rows(l: ArrayRef) -> VortexResult<ArrayRef> {
6870
}
6971

7072
fn list_of(values: ArrayRef, offsets: &[u32], validity: Validity) -> VortexResult<ArrayRef> {
71-
let mut ctx = array_session().create_execution_ctx();
73+
let mut ctx = SESSION.create_execution_ctx();
7274
Ok(ListArray::try_new(
7375
values,
7476
PrimitiveArray::from_iter(offsets.iter().copied()).into_array(),
@@ -94,7 +96,7 @@ fn plus_one() -> VortexResult<Lambda> {
9496

9597
fn assert_applies(input: ArrayRef, expr: &Expression, expected: ArrayRef) -> VortexResult<()> {
9698
let actual = input.apply(expr)?;
97-
let mut ctx = array_session().create_execution_ctx();
99+
let mut ctx = SESSION.create_execution_ctx();
98100
assert_arrays_eq!(actual, expected, &mut ctx);
99101
Ok(())
100102
}
@@ -160,7 +162,7 @@ fn sliced_list_with_leftovers() -> VortexResult<ArrayRef> {
160162

161163
/// `[[1], [2]]` over `[MAX, 1, 2]`: the lists are back to back, after a leftover.
162164
fn list_view_with_a_leftover_in_front() -> VortexResult<ArrayRef> {
163-
let mut ctx = array_session().create_execution_ctx();
165+
let mut ctx = SESSION.create_execution_ctx();
164166
let view = ListViewArray::try_new(
165167
buffer![i32::MAX, 1_i32, 2].into_array(),
166168
buffer![1_u32, 2].into_array(),
@@ -174,7 +176,7 @@ fn list_view_with_a_leftover_in_front() -> VortexResult<ArrayRef> {
174176

175177
/// `[[1], [2]]` over `[1, MAX, 2]`: the lists skip an element.
176178
fn list_view_with_a_gap() -> VortexResult<ArrayRef> {
177-
let mut ctx = array_session().create_execution_ctx();
179+
let mut ctx = SESSION.create_execution_ctx();
178180
Ok(ListViewArray::try_new(
179181
buffer![1_i32, i32::MAX, 2].into_array(),
180182
buffer![0_u32, 2].into_array(),
@@ -204,7 +206,7 @@ fn a_lambda_that_can_fail_sees_only_the_lists_elements(
204206
#[test]
205207
fn a_lambda_that_can_fail_runs_on_a_slice_without_its_leftovers() -> VortexResult<()> {
206208
// Canonicalizing the result drops the leftovers too, so look at what the lambda runs on.
207-
let mut ctx = array_session().create_execution_ctx();
209+
let mut ctx = SESSION.create_execution_ctx();
208210
let lists = Lists::visible_elements_only(sliced_list_with_leftovers()?, &mut ctx)?;
209211
lists.with_elements(|elements| {
210212
assert_arrays_eq!(elements.clone(), buffer![1_i32, 2].into_array(), &mut ctx);
@@ -217,7 +219,7 @@ fn a_lambda_that_can_fail_runs_on_a_slice_without_its_leftovers() -> VortexResul
217219
fn a_lambda_that_cannot_fail_runs_on_the_elements_in_place() -> VortexResult<()> {
218220
// `x = 3` cannot fail, so it runs over the whole elements buffer, the null list's `i32::MAX`
219221
// included, and nothing is copied.
220-
let mut ctx = array_session().create_execution_ctx();
222+
let mut ctx = SESSION.create_execution_ctx();
221223
let result = lists()?
222224
.apply(&list_transform(
223225
root(),
@@ -251,7 +253,7 @@ fn a_failure_in_a_valid_list_is_an_error() -> VortexResult<()> {
251253
&[0, 1, 2],
252254
Validity::from_iter([false, true]),
253255
)?;
254-
let mut ctx = array_session().create_execution_ctx();
256+
let mut ctx = SESSION.create_execution_ctx();
255257
let error = lists
256258
.apply(&list_transform(root(), plus_one()?))?
257259
.execute::<RecursiveCanonical>(&mut ctx)
@@ -403,7 +405,7 @@ fn three_nested_lambdas() -> VortexResult<()> {
403405
fn sliced_list_views() -> VortexResult<()> {
404406
// `[[1, 2], [3], [4, 5]]` as a list view, sliced to `[[3], [4, 5]]`, which leaves unused
405407
// elements.
406-
let mut ctx = array_session().create_execution_ctx();
408+
let mut ctx = SESSION.create_execution_ctx();
407409
let view = list_of(
408410
buffer![1_i32, 2, 3, 4, 5].into_array(),
409411
&[0, 2, 3, 5],
@@ -423,7 +425,7 @@ fn sliced_list_views() -> VortexResult<()> {
423425
#[test]
424426
fn overlapping_list_views() -> VortexResult<()> {
425427
// `[[3, 4], [1], [2, 3]]`, sharing element 3.
426-
let mut ctx = array_session().create_execution_ctx();
428+
let mut ctx = SESSION.create_execution_ctx();
427429
let view = ListViewArray::try_new(
428430
buffer![1_i32, 2, 3, 4].into_array(),
429431
buffer![2_u32, 0, 1].into_array(),
@@ -518,7 +520,7 @@ fn applying_the_bound_node() -> VortexResult<()> {
518520
let expected = transformed(
519521
PrimitiveArray::from_option_iter([Some(2_i32), Some(3), Some(4), None]).into_array(),
520522
)?;
521-
let mut ctx = array_session().create_execution_ctx();
523+
let mut ctx = SESSION.create_execution_ctx();
522524
assert_arrays_eq!(input.apply_bound(&bound)?, expected, &mut ctx);
523525
Ok(())
524526
}
@@ -552,19 +554,16 @@ fn a_lambda_bound_to_another_element_dtype_is_an_error() -> VortexResult<()> {
552554
}
553555

554556
#[test]
555-
fn replacing_the_root_rereads_the_list() -> VortexResult<()> {
556-
// Rows `{s: {l}}`: re-rooting the transform at `$.s` changes where the list is read. The body
557-
// is in the options, which rewrites of the expression leave alone.
557+
fn a_list_in_a_nested_field() -> VortexResult<()> {
558+
// Rows `{s: {l}}`.
558559
let input = StructArray::try_new(
559560
FieldNames::from(["s"]),
560561
vec![rows(lists()?)?],
561562
3,
562563
Validity::NonNullable,
563564
)?
564565
.into_array();
565-
let expr = list_transform(col("l"), plus_one()?);
566-
assert_eq!(replace(expr.clone(), &var("x"), lit(0_i32)), expr);
567-
let expr = replace(expr, &root(), col("s"));
566+
let expr = list_transform(get_item("l", col("s")), plus_one()?);
568567
assert_eq!(
569568
expr.to_string(),
570569
"vortex.list.transform($.s.l, (x) -> (@x + 1i32))"
@@ -592,7 +591,7 @@ fn an_infallible_transform_pushes_into_dictionary_values() -> VortexResult<()> {
592591
&[0, 2, 3, 5, 6],
593592
Validity::NonNullable,
594593
)?;
595-
let mut ctx = array_session().create_execution_ctx();
594+
let mut ctx = SESSION.create_execution_ctx();
596595
assert_arrays_eq!(actual, expected, &mut ctx);
597596
Ok(())
598597
}

‎vortex-expr/src/transform/replace.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ pub fn replace(expr: Expression, needle: &Expression, replacement: Expression) -
2828
#[cfg(test)]
2929
mod test {
3030
use vortex_array::dtype::Nullability::NonNullable;
31+
use vortex_array::expr::Lambda;
32+
use vortex_array::expr::checked_add;
33+
use vortex_array::expr::col;
3134
use vortex_array::expr::get_item;
35+
use vortex_array::expr::list_transform;
3236
use vortex_array::expr::lit;
3337
use vortex_array::expr::pack;
38+
use vortex_array::expr::root;
39+
use vortex_array::expr::var;
40+
use vortex_error::VortexResult;
3441

3542
use super::replace;
3643

@@ -51,4 +58,16 @@ mod test {
5158
let replaced_expr = replace(e, &needle, replacement);
5259
assert_eq!(replaced_expr.to_string(), "pack(a: 1i32, b: 42i32)");
5360
}
61+
62+
#[test]
63+
fn test_replace_leaves_a_lambda_alone() -> VortexResult<()> {
64+
// The lambda is in `list_transform`'s options, not among its children.
65+
let expr = list_transform(col("l"), Lambda::new(["x"], checked_add(var("x"), lit(1)))?);
66+
assert_eq!(replace(expr.clone(), &var("x"), lit(0)), expr);
67+
assert_eq!(
68+
replace(expr, &root(), col("s")).to_string(),
69+
"vortex.list.transform($.s.l, (x) -> (@x + 1i32))"
70+
);
71+
Ok(())
72+
}
5473
}

0 commit comments

Comments
 (0)