vortex_array/arrays/varbin/compute/
min_max.rs1use itertools::Itertools;
5use vortex_dtype::DType;
6use vortex_dtype::Nullability::NonNullable;
7use vortex_error::VortexResult;
8use vortex_error::vortex_panic;
9
10use crate::accessor::ArrayAccessor;
11use crate::arrays::VarBinArray;
12use crate::arrays::VarBinVTable;
13use crate::compute::MinMaxKernel;
14use crate::compute::MinMaxKernelAdapter;
15use crate::compute::MinMaxResult;
16use crate::register_kernel;
17use crate::scalar::Scalar;
18
19impl MinMaxKernel for VarBinVTable {
20 fn min_max(&self, array: &VarBinArray) -> VortexResult<Option<MinMaxResult>> {
21 Ok(varbin_compute_min_max(array, array.dtype()))
22 }
23}
24
25register_kernel!(MinMaxKernelAdapter(VarBinVTable).lift());
26
27pub(crate) fn varbin_compute_min_max<T: ArrayAccessor<[u8]>>(
29 array: &T,
30 dtype: &DType,
31) -> Option<MinMaxResult> {
32 array.with_iterator(|iter| match iter.flatten().minmax() {
33 itertools::MinMaxResult::NoElements => None,
34 itertools::MinMaxResult::OneElement(value) => {
35 let scalar = make_scalar(dtype, value);
36 Some(MinMaxResult {
37 min: scalar.clone(),
38 max: scalar,
39 })
40 }
41 itertools::MinMaxResult::MinMax(min, max) => Some(MinMaxResult {
42 min: make_scalar(dtype, min),
43 max: make_scalar(dtype, max),
44 }),
45 })
46}
47
48fn make_scalar(dtype: &DType, value: &[u8]) -> Scalar {
50 match dtype {
51 DType::Binary(_) => Scalar::binary(value.to_vec(), NonNullable),
52 DType::Utf8(_) => {
53 let value = unsafe { str::from_utf8_unchecked(value) };
56 Scalar::utf8(value, NonNullable)
57 }
58 _ => vortex_panic!("cannot make Scalar from bytes with dtype {dtype}"),
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use vortex_buffer::BufferString;
65 use vortex_dtype::DType::Utf8;
66 use vortex_dtype::Nullability::NonNullable;
67 use vortex_dtype::Nullability::Nullable;
68
69 use crate::arrays::VarBinArray;
70 use crate::compute::MinMaxResult;
71 use crate::compute::min_max;
72 use crate::expr::stats::Stat;
73 use crate::expr::stats::StatsProvider;
74 use crate::scalar::Scalar;
75
76 #[test]
77 fn some_nulls() {
78 let array = VarBinArray::from_iter(
79 vec![
80 Some("hello world"),
81 None,
82 Some("hello world this is a long string"),
83 None,
84 ],
85 Utf8(Nullable),
86 );
87 let MinMaxResult { min, max } = min_max(array.as_ref()).unwrap().unwrap();
88
89 assert_eq!(
90 min,
91 Scalar::try_new(
92 Utf8(NonNullable),
93 Some(BufferString::from("hello world".to_string()).into()),
94 )
95 .unwrap()
96 );
97 assert_eq!(
98 max,
99 Scalar::try_new(
100 Utf8(NonNullable),
101 Some(BufferString::from("hello world this is a long string".to_string()).into()),
102 )
103 .unwrap()
104 );
105 }
106
107 #[test]
108 fn all_nulls() {
109 let array = VarBinArray::from_iter(vec![Option::<&str>::None, None, None], Utf8(Nullable));
110 let stats = array.statistics();
111 assert!(stats.get(Stat::Min).is_none());
112 assert!(stats.get(Stat::Max).is_none());
113 }
114}