Skip to content

Commit 6c9b339

Browse files
committed
test: cover bool, binary, decimal, slice/take, and empty list extrema
Signed-off-by: Matt Katz <[email protected]>
1 parent 716d31e commit 6c9b339

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

  • vortex-array/src/scalar_fn/fns/list_min_max

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ use crate::aggregate_fn::NumericalAggregateOpts;
2020
use crate::array_session;
2121
use crate::arrays::BoolArray;
2222
use crate::arrays::ConstantArray;
23+
use crate::arrays::DecimalArray;
2324
use crate::arrays::FixedSizeListArray;
2425
use crate::arrays::ListArray;
2526
use crate::arrays::ListViewArray;
2627
use crate::arrays::PrimitiveArray;
2728
use crate::arrays::VarBinArray;
2829
use crate::assert_arrays_eq;
2930
use crate::dtype::DType;
31+
use crate::dtype::DecimalDType;
3032
use crate::dtype::Nullability;
3133
use crate::dtype::PType;
3234
use crate::dtype::half::f16;
@@ -336,6 +338,94 @@ fn string_extrema() -> VortexResult<()> {
336338
Ok(())
337339
}
338340

341+
#[test]
342+
fn binary_extrema() -> VortexResult<()> {
343+
let elements = VarBinArray::from_iter(
344+
[
345+
Some(b"pear".as_slice()),
346+
None,
347+
Some(b"apple"),
348+
Some(b"zebra"),
349+
],
350+
DType::Binary(Nullability::Nullable),
351+
)
352+
.into_array();
353+
let list = ListArray::try_new(
354+
elements,
355+
buffer![0u32, 3, 4].into_array(),
356+
Validity::NonNullable,
357+
)?
358+
.into_array();
359+
let minimum = list.clone().apply(&list_min(root()))?;
360+
let maximum = list.apply(&list_max(root()))?;
361+
let mut ctx = array_session().create_execution_ctx();
362+
363+
let expected_min = VarBinArray::from_iter(
364+
[Some(b"apple".as_slice()), Some(b"zebra")],
365+
DType::Binary(Nullability::Nullable),
366+
);
367+
let expected_max = VarBinArray::from_iter(
368+
[Some(b"pear".as_slice()), Some(b"zebra")],
369+
DType::Binary(Nullability::Nullable),
370+
);
371+
assert_arrays_eq!(minimum, expected_min, &mut ctx);
372+
assert_arrays_eq!(maximum, expected_max, &mut ctx);
373+
Ok(())
374+
}
375+
376+
#[test]
377+
fn bool_extrema() -> VortexResult<()> {
378+
let elements = BoolArray::from_iter([true, true, false, true]);
379+
let list = ListArray::try_new(
380+
elements.into_array(),
381+
buffer![0u32, 3, 4].into_array(),
382+
Validity::NonNullable,
383+
)?
384+
.into_array();
385+
let minimum = list.clone().apply(&list_min(root()))?;
386+
let maximum = list.apply(&list_max(root()))?;
387+
let mut ctx = array_session().create_execution_ctx();
388+
389+
let expected_min = BoolArray::from_iter([Some(false), Some(true)]);
390+
let expected_max = BoolArray::from_iter([Some(true), Some(true)]);
391+
assert_arrays_eq!(minimum, expected_min, &mut ctx);
392+
assert_arrays_eq!(maximum, expected_max, &mut ctx);
393+
Ok(())
394+
}
395+
396+
#[test]
397+
fn decimal_extrema() -> VortexResult<()> {
398+
let elements = DecimalArray::new(
399+
buffer![100i128, 250, 50, 999],
400+
DecimalDType::new(10, 2),
401+
Validity::NonNullable,
402+
)
403+
.into_array();
404+
let list = ListArray::try_new(
405+
elements,
406+
buffer![0u32, 3, 4].into_array(),
407+
Validity::NonNullable,
408+
)?
409+
.into_array();
410+
let minimum = list.clone().apply(&list_min(root()))?;
411+
let maximum = list.apply(&list_max(root()))?;
412+
let mut ctx = array_session().create_execution_ctx();
413+
414+
let expected_min = DecimalArray::new(
415+
buffer![50i128, 999],
416+
DecimalDType::new(10, 2),
417+
Validity::AllValid,
418+
);
419+
let expected_max = DecimalArray::new(
420+
buffer![250i128, 999],
421+
DecimalDType::new(10, 2),
422+
Validity::AllValid,
423+
);
424+
assert_arrays_eq!(minimum, expected_min, &mut ctx);
425+
assert_arrays_eq!(maximum, expected_max, &mut ctx);
426+
Ok(())
427+
}
428+
339429
#[test]
340430
fn nan_options() -> VortexResult<()> {
341431
let elements = PrimitiveArray::from_iter([1.0f64, f64::NAN, 2.0, f64::NAN, f64::NAN]);
@@ -443,6 +533,49 @@ fn constant_and_null_list_extrema() -> VortexResult<()> {
443533
Ok(())
444534
}
445535

536+
#[test]
537+
fn sliced_and_taken_extrema() -> VortexResult<()> {
538+
let list = ListArray::try_new(
539+
create_list_elements(),
540+
buffer![0u32, 2, 5, 5, 7].into_array(),
541+
Validity::NonNullable,
542+
)?
543+
.into_array();
544+
let mut ctx = array_session().create_execution_ctx();
545+
546+
let sliced = list.slice(1..4)?;
547+
let minimum = sliced.apply(&list_min(root()))?;
548+
let expected_min = PrimitiveArray::from_option_iter::<i32, _>([Some(3), None, Some(6)]);
549+
assert_arrays_eq!(minimum, expected_min, &mut ctx);
550+
551+
let taken = list.take(buffer![3u64, 0, 2].into_array())?;
552+
let maximum = taken.apply(&list_max(root()))?;
553+
let expected_max = PrimitiveArray::from_option_iter::<i32, _>([Some(6), Some(2), None]);
554+
assert_arrays_eq!(maximum, expected_max, &mut ctx);
555+
Ok(())
556+
}
557+
558+
#[test]
559+
fn empty_array_extrema() -> VortexResult<()> {
560+
let elements = PrimitiveArray::from_iter([0i32; 0]);
561+
let list = ListArray::try_new(
562+
elements.into_array(),
563+
buffer![0u32].into_array(),
564+
Validity::NonNullable,
565+
)?
566+
.into_array();
567+
let mut ctx = array_session().create_execution_ctx();
568+
569+
for expression in [list_min(root()), list_max(root())] {
570+
let result = list
571+
.clone()
572+
.apply(&expression)?
573+
.execute::<PrimitiveArray>(&mut ctx)?;
574+
assert_eq!(result.len(), 0);
575+
}
576+
Ok(())
577+
}
578+
446579
#[test]
447580
fn dtype_validation() -> VortexResult<()> {
448581
let opts = NumericalAggregateOpts::default();

0 commit comments

Comments
 (0)