Skip to main content

vortex_array/aggregate_fn/fns/min_max/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4mod bool;
5mod decimal;
6pub use decimal::valid_range;
7mod extension;
8mod fixed_size_list;
9mod primitive;
10mod varbinview;
11
12use std::sync::LazyLock;
13
14use vortex_error::VortexExpect;
15use vortex_error::VortexResult;
16use vortex_error::vortex_bail;
17use vortex_error::vortex_panic;
18use vortex_session::registry::CachedId;
19
20use self::bool::accumulate_bool;
21use self::decimal::accumulate_decimal;
22use self::extension::accumulate_extension;
23use self::fixed_size_list::accumulate_fixed_size_bytes;
24use self::primitive::accumulate_primitive;
25use self::varbinview::accumulate_varbinview;
26use crate::ArrayRef;
27use crate::Canonical;
28use crate::Columnar;
29use crate::ExecutionCtx;
30use crate::aggregate_fn::Accumulator;
31use crate::aggregate_fn::AggregateArgs;
32use crate::aggregate_fn::AggregateDTypes;
33use crate::aggregate_fn::AggregateFnId;
34use crate::aggregate_fn::AggregateFnVTable;
35use crate::aggregate_fn::DynAccumulator;
36use crate::aggregate_fn::NumericalAggregateOpts;
37use crate::aggregate_fn::fns::fixed_size_bytes::is_fixed_size_bytes;
38use crate::dtype::DType;
39use crate::dtype::FieldNames;
40use crate::dtype::Nullability;
41use crate::dtype::PType;
42use crate::dtype::StructFields;
43use crate::dtype::half::f16;
44use crate::expr::stats::Precision;
45use crate::expr::stats::Stat;
46use crate::expr::stats::StatsProvider;
47use crate::expr::stats::StatsProviderExt;
48use crate::partial_ord::partial_max;
49use crate::partial_ord::partial_min;
50use crate::scalar::Scalar;
51
52static NAMES: LazyLock<FieldNames> = LazyLock::new(|| FieldNames::from(["min", "max"]));
53
54/// The minimum and maximum non-null values of an array, or `None` if there are no non-null values.
55///
56/// NaN handling for float inputs is controlled by [`NumericalAggregateOpts`]: with `skip_nans` (the
57/// default) NaN values are ignored and the cached `Stat::Min`/`Stat::Max` statistics are consulted
58/// and updated. With `skip_nans=false`, any NaN value in a float array poisons both extrema to
59/// NaN; an exact `Stat::NaNCount` statistic shortcircuits the NaN scan in either direction. The
60/// same applies to extension arrays stored as floats.
61///
62/// The result scalars have the non-nullable version of the array dtype.
63/// This will update the stats set of the array as a side effect.
64pub fn min_max(
65    array: &ArrayRef,
66    ctx: &mut ExecutionCtx,
67    options: NumericalAggregateOpts,
68) -> VortexResult<Option<MinMaxResult>> {
69    if !options.skip_nans && has_nan_values(array.dtype()) {
70        match array.statistics().get_as::<u64>(Stat::NaNCount) {
71            // NaN-free: identical to the NaN-skipping path below, including its stat caching.
72            Precision::Exact(0) => {}
73            // At least one NaN value poisons both extrema.
74            Precision::Exact(_) => return Ok(Some(nan_minmax_result(array.dtype())?)),
75            _ => {
76                if array.is_empty() || array.valid_count(ctx)? == 0 {
77                    return Ok(None);
78                }
79                // Compute with NaN-including options; the NaN-skipping `Stat::Min`/`Stat::Max`
80                // caches are neither read nor written.
81                let mut acc = Accumulator::try_new(MinMax, options, array.dtype().clone())?;
82                acc.accumulate(array, ctx)?;
83                return MinMaxResult::from_scalar(&acc.finish()?);
84            }
85        }
86    }
87
88    // NaN-skipping path. Also reached for NaN-free not-skipping float arrays and all non-float
89    // arrays, where `skip_nans` has no effect.
90
91    // Short-circuit using cached array statistics.
92    let cached_min = array.statistics().get(Stat::Min).as_exact();
93    let cached_max = array.statistics().get(Stat::Max).as_exact();
94    if let Some((min, max)) = cached_min.zip(cached_max) {
95        let non_nullable_dtype = array.dtype().as_nonnullable();
96        return Ok(Some(MinMaxResult {
97            min: min.cast(&non_nullable_dtype)?,
98            max: max.cast(&non_nullable_dtype)?,
99        }));
100    }
101
102    // Short-circuit for empty arrays or all-null arrays.
103    if array.is_empty() || array.valid_count(ctx)? == 0 {
104        return Ok(None);
105    }
106
107    // Short-circuit for dtypes this helper cannot currently compute.
108    if !minmax_compute_supported_dtype(array.dtype()) {
109        return Ok(None);
110    }
111
112    // Compute using Accumulator<MinMax>.
113    let mut acc = Accumulator::try_new(
114        MinMax,
115        NumericalAggregateOpts::default(),
116        array.dtype().clone(),
117    )?;
118    acc.accumulate(array, ctx)?;
119    let result_scalar = acc.finish()?;
120    let result = MinMaxResult::from_scalar(&result_scalar)?;
121
122    // Cache the computed min/max as statistics.
123    if let Some(r) = &result {
124        if let Some(min_value) = r.min.value() {
125            array
126                .statistics()
127                .set(Stat::Min, Precision::Exact(min_value.clone()));
128        }
129        if let Some(max_value) = r.max.value() {
130            array
131                .statistics()
132                .set(Stat::Max, Precision::Exact(max_value.clone()));
133        }
134    }
135
136    Ok(result)
137}
138
139/// Compute the min and max of an already-executed batch without touching any statistics.
140///
141/// Aggregates that delegate to [`MinMax`] use this on the columnar form of their input, so that
142/// results are only ever cached on the array the aggregate was requested for, never on a
143/// temporary canonical array.
144pub(crate) fn columnar_min_max(
145    batch: &Columnar,
146    options: NumericalAggregateOpts,
147    ctx: &mut ExecutionCtx,
148) -> VortexResult<Option<MinMaxResult>> {
149    if !minmax_compute_supported_dtype(batch.dtype()) {
150        return Ok(None);
151    }
152
153    let dtypes = AggregateDTypes::try_new(&MinMax, &options, batch.dtype().clone())?;
154    let args = dtypes.args(&options);
155    let mut partial = MinMax.empty_partial(args)?;
156    MinMax.accumulate(args, &mut partial, batch, ctx)?;
157    MinMaxResult::from_scalar(&MinMax.to_scalar(args, &partial)?)
158}
159
160/// A `{min: NaN, max: NaN}` result for a poisoned NaN-including min/max over `dtype`.
161fn nan_minmax_result(dtype: &DType) -> VortexResult<MinMaxResult> {
162    let nan = nan_scalar(dtype)?;
163
164    Ok(MinMaxResult {
165        min: nan.clone(),
166        max: nan,
167    })
168}
169
170/// Whether `dtype` can hold NaN values: floats, and extension types stored as floats.
171pub(crate) fn has_nan_values(dtype: &DType) -> bool {
172    match dtype {
173        DType::Extension(ext_dtype) => has_nan_values(ext_dtype.storage_dtype()),
174        _ => dtype.is_float(),
175    }
176}
177
178/// A non-nullable NaN scalar of `dtype`, a float or an extension type stored as a float.
179///
180/// Returns an error if an extension dtype rejects NaN.
181pub(crate) fn nan_scalar(dtype: &DType) -> VortexResult<Scalar> {
182    if let DType::Extension(ext_dtype) = dtype {
183        return Scalar::try_extension_ref(
184            ext_dtype.with_nullability(Nullability::NonNullable),
185            nan_scalar(ext_dtype.storage_dtype())?,
186        );
187    }
188
189    Ok(match dtype.as_ptype() {
190        PType::F16 => Scalar::primitive(f16::NAN, Nullability::NonNullable),
191        PType::F32 => Scalar::primitive(f32::NAN, Nullability::NonNullable),
192        PType::F64 => Scalar::primitive(f64::NAN, Nullability::NonNullable),
193        _ => vortex_panic!("NaN scalar requested for non-float dtype {dtype}"),
194    })
195}
196
197/// Whether a scalar holds a float NaN value, directly or as the storage of an extension scalar.
198pub(crate) fn scalar_is_nan(scalar: &Scalar) -> bool {
199    if let Some(ext) = scalar.as_extension_opt() {
200        return scalar_is_nan(&ext.to_storage_scalar());
201    }
202    if !scalar.dtype().is_float() {
203        return false;
204    }
205
206    scalar.as_primitive_opt().is_some_and(|p| p.is_nan())
207}
208
209/// The minimum and maximum non-null values of an array.
210#[derive(Debug, Clone, PartialEq, Eq)]
211pub struct MinMaxResult {
212    pub min: Scalar,
213    pub max: Scalar,
214}
215
216impl MinMaxResult {
217    /// Extract a `MinMaxResult` from a struct scalar with `{min, max}` fields.
218    pub fn from_scalar(scalar: &Scalar) -> VortexResult<Option<Self>> {
219        if scalar.is_null() {
220            Ok(None)
221        } else {
222            let min = scalar
223                .as_struct()
224                .field_by_idx(0)
225                .vortex_expect("missing min field");
226            let max = scalar
227                .as_struct()
228                .field_by_idx(1)
229                .vortex_expect("missing max field");
230            Ok(Some(MinMaxResult { min, max }))
231        }
232    }
233}
234
235/// Compute the min and max of an array.
236///
237/// Returns a nullable struct scalar `{min: T, max: T}` where `T` is the non-nullable input dtype.
238/// The struct is null when the array is empty or all-null.
239///
240/// NaN handling for float inputs is controlled by [`NumericalAggregateOpts`]: with `skip_nans` (the
241/// default) NaN values are ignored, otherwise any NaN value poisons both extrema to NaN.
242#[derive(Clone, Debug)]
243pub struct MinMax;
244
245/// Partial accumulator state for min/max.
246pub struct MinMaxPartial {
247    min: Option<Scalar>,
248    max: Option<Scalar>,
249}
250
251impl MinMaxPartial {
252    /// Merge a local `MinMaxResult` into this partial state.
253    fn merge(
254        &mut self,
255        args: AggregateArgs<'_, NumericalAggregateOpts>,
256        local: Option<MinMaxResult>,
257    ) -> VortexResult<()> {
258        let Some(MinMaxResult { min, max }) = local else {
259            return Ok(());
260        };
261
262        // NaN scalars are incomparable under `partial_min`/`partial_max`, so they are handled
263        // explicitly: a NaN extremum poisons the partial state when NaNs participate, and is
264        // dropped when they are skipped.
265        if scalar_is_nan(&min) || scalar_is_nan(&max) || self.is_poisoned() {
266            if !args.options.skip_nans {
267                self.poison(args)?;
268            }
269            return Ok(());
270        }
271
272        self.min = Some(match self.min.take() {
273            Some(current) => partial_min(min, current).vortex_expect("incomparable min scalars"),
274            None => min,
275        });
276
277        self.max = Some(match self.max.take() {
278            Some(current) => partial_max(max, current).vortex_expect("incomparable max scalars"),
279            None => max,
280        });
281
282        Ok(())
283    }
284
285    /// Poison the partial state to `{min: NaN, max: NaN}`.
286    fn poison(&mut self, args: AggregateArgs<'_, NumericalAggregateOpts>) -> VortexResult<()> {
287        let nan = nan_scalar(args.dtype)?;
288        self.min = Some(nan.clone());
289        self.max = Some(nan);
290
291        Ok(())
292    }
293
294    /// Whether the partial state is poisoned to NaN.
295    fn is_poisoned(&self) -> bool {
296        self.min.as_ref().is_some_and(scalar_is_nan)
297    }
298}
299
300/// Creates the struct dtype `{min: T, max: T}` (nullable) used for min/max aggregate results.
301pub fn make_minmax_dtype(element_dtype: &DType) -> DType {
302    DType::Struct(
303        StructFields::new(
304            NAMES.clone(),
305            vec![
306                element_dtype.as_nonnullable(),
307                element_dtype.as_nonnullable(),
308            ],
309        ),
310        Nullability::Nullable,
311    )
312}
313
314fn minmax_supported_dtype(input_dtype: &DType) -> bool {
315    match input_dtype {
316        DType::Bool(_)
317        | DType::Primitive(..)
318        | DType::Decimal(..)
319        | DType::Utf8(..)
320        | DType::Binary(..)
321        | DType::Extension(..) => true,
322        DType::List(element_dtype, _) => minmax_supported_dtype(element_dtype),
323        DType::FixedSizeList(element_dtype, ..) => minmax_supported_dtype(element_dtype),
324        _ => false,
325    }
326}
327
328/// Returns whether [`min_max`] can currently compute extrema for this logical dtype.
329///
330/// This is intentionally narrower than [`minmax_supported_dtype`]. List and fixed-size-list
331/// extrema have a defined output dtype for aggregate expression lowering, but the accumulator does
332/// not yet implement lexicographic list comparison. Fixed-size lists of non-nullable `u8` are the
333/// exception: they compare by byte.
334fn minmax_compute_supported_dtype(input_dtype: &DType) -> bool {
335    matches!(
336        input_dtype,
337        DType::Bool(_)
338            | DType::Primitive(..)
339            | DType::Decimal(..)
340            | DType::Utf8(..)
341            | DType::Binary(..)
342            | DType::Extension(..)
343    ) || is_fixed_size_bytes(input_dtype)
344}
345
346impl AggregateFnVTable for MinMax {
347    type Options = NumericalAggregateOpts;
348    type Partial = MinMaxPartial;
349
350    fn id(&self) -> AggregateFnId {
351        static ID: CachedId = CachedId::new("vortex.min_max");
352        *ID
353    }
354
355    fn serialize(&self, _options: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
356        Ok(None)
357    }
358
359    fn return_dtype(&self, _options: &Self::Options, input_dtype: &DType) -> Option<DType> {
360        minmax_supported_dtype(input_dtype).then(|| make_minmax_dtype(input_dtype))
361    }
362
363    fn partial_dtype(&self, options: &Self::Options, input_dtype: &DType) -> Option<DType> {
364        self.return_dtype(options, input_dtype)
365    }
366
367    fn empty_partial(
368        &self,
369        _args: AggregateArgs<'_, Self::Options>,
370    ) -> VortexResult<Self::Partial> {
371        Ok(MinMaxPartial {
372            min: None,
373            max: None,
374        })
375    }
376
377    fn partial_from_scalar(
378        &self,
379        args: AggregateArgs<'_, Self::Options>,
380        scalar: &Scalar,
381    ) -> VortexResult<Self::Partial> {
382        let mut partial = MinMaxPartial {
383            min: None,
384            max: None,
385        };
386        // `merge` normalizes the parsed extrema: nulls stay empty and NaNs poison or drop.
387        partial.merge(args, MinMaxResult::from_scalar(scalar)?)?;
388        Ok(partial)
389    }
390
391    fn merge_partials(
392        &self,
393        args: AggregateArgs<'_, Self::Options>,
394        mut first: Self::Partial,
395        second: Self::Partial,
396    ) -> VortexResult<Self::Partial> {
397        if let (Some(min), Some(max)) = (second.min, second.max) {
398            first.merge(args, Some(MinMaxResult { min, max }))?;
399        }
400        Ok(first)
401    }
402
403    fn to_scalar(
404        &self,
405        args: AggregateArgs<'_, Self::Options>,
406        partial: &Self::Partial,
407    ) -> VortexResult<Scalar> {
408        let dtype = args.partial_dtype.clone();
409        Ok(match (&partial.min, &partial.max) {
410            (Some(min), Some(max)) => Scalar::struct_(dtype, vec![min.clone(), max.clone()]),
411            _ => Scalar::null(dtype),
412        })
413    }
414
415    #[inline]
416    fn is_saturated(
417        &self,
418        _args: AggregateArgs<'_, Self::Options>,
419        partial: &Self::Partial,
420    ) -> bool {
421        // A poisoned NaN-including min/max is fully determined.
422        partial.is_poisoned()
423    }
424
425    fn try_accumulate(
426        &self,
427        args: AggregateArgs<'_, Self::Options>,
428        partial: &mut Self::Partial,
429        batch: &ArrayRef,
430        _ctx: &mut ExecutionCtx,
431    ) -> VortexResult<bool> {
432        // NaN-aware shortcircuits only apply to NaN-including float min/max; everything else
433        // takes the default dispatch path.
434        if args.options.skip_nans || !args.dtype.is_float() {
435            return Ok(false);
436        }
437        match batch.statistics().get_as::<u64>(Stat::NaNCount) {
438            Precision::Exact(0) => {
439                // NaN-free batch: the cached NaN-skipping extrema (if any) are valid.
440                let cached_min = batch.statistics().get(Stat::Min).as_exact();
441                let cached_max = batch.statistics().get(Stat::Max).as_exact();
442                if let Some((min, max)) = cached_min.zip(cached_max) {
443                    // Cached float stats carry the (possibly nullable) array dtype; `to_scalar`
444                    // builds a struct with non-nullable fields, so normalise here.
445                    let non_nullable_dtype = args.dtype.as_nonnullable();
446                    partial.merge(
447                        args,
448                        Some(MinMaxResult {
449                            min: min.cast(&non_nullable_dtype)?,
450                            max: max.cast(&non_nullable_dtype)?,
451                        }),
452                    )?;
453                    return Ok(true);
454                }
455                Ok(false)
456            }
457            Precision::Exact(_) => {
458                // At least one NaN value poisons both extrema without scanning the batch.
459                partial.poison(args)?;
460                Ok(true)
461            }
462            _ => Ok(false),
463        }
464    }
465
466    fn accumulate(
467        &self,
468        args: AggregateArgs<'_, Self::Options>,
469        partial: &mut Self::Partial,
470        batch: &Columnar,
471        ctx: &mut ExecutionCtx,
472    ) -> VortexResult<()> {
473        match batch {
474            Columnar::Constant(c) => {
475                if c.is_empty() {
476                    return Ok(());
477                }
478
479                let scalar = c.scalar();
480                if scalar.is_null() {
481                    return Ok(());
482                }
483                // NaN float constants are skipped or poison the extrema, per the options.
484                if scalar_is_nan(scalar) {
485                    if !args.options.skip_nans {
486                        partial.poison(args)?;
487                    }
488                    return Ok(());
489                }
490                let non_nullable_dtype = scalar.dtype().as_nonnullable();
491                let cast = scalar.cast(&non_nullable_dtype)?;
492                partial.merge(
493                    args,
494                    Some(MinMaxResult {
495                        min: cast.clone(),
496                        max: cast,
497                    }),
498                )
499            }
500            Columnar::Canonical(c) => match c {
501                Canonical::Primitive(p) => accumulate_primitive(args, partial, p, ctx),
502                Canonical::Bool(b) => accumulate_bool(args, partial, b, ctx),
503                Canonical::VarBinView(v) => accumulate_varbinview(args, partial, v, ctx),
504                Canonical::Decimal(d) => accumulate_decimal(args, partial, d, ctx),
505                Canonical::Extension(e) => accumulate_extension(args, partial, e, ctx),
506                Canonical::FixedSizeList(f) if is_fixed_size_bytes(f.dtype()) => {
507                    accumulate_fixed_size_bytes(args, partial, f, ctx)
508                }
509                Canonical::Null(_) => Ok(()),
510                Canonical::Union(_) => {
511                    vortex_bail!("TODO(connor)[Union]: implement min_max for Union arrays")
512                }
513                Canonical::Struct(_)
514                | Canonical::List(_)
515                | Canonical::Map(_)
516                | Canonical::FixedSizeList(_)
517                | Canonical::Variant(_) => {
518                    vortex_bail!("Unsupported canonical type for min_max: {}", batch.dtype())
519                }
520            },
521        }
522    }
523
524    fn finalize(
525        &self,
526        _args: AggregateArgs<'_, Self::Options>,
527        partials: ArrayRef,
528    ) -> VortexResult<ArrayRef> {
529        Ok(partials)
530    }
531
532    fn finalize_scalar(
533        &self,
534        args: AggregateArgs<'_, Self::Options>,
535        partial: &Self::Partial,
536    ) -> VortexResult<Scalar> {
537        self.to_scalar(args, partial)
538    }
539}
540
541#[cfg(test)]
542mod tests {
543    use std::sync::Arc;
544    use std::sync::LazyLock;
545
546    use rstest::rstest;
547    use vortex_buffer::BitBuffer;
548    use vortex_buffer::Buffer;
549    use vortex_buffer::buffer;
550    use vortex_error::VortexExpect;
551    use vortex_error::VortexResult;
552    use vortex_session::VortexSession;
553
554    use crate::ArrayRef;
555    use crate::EmptyMetadata;
556    use crate::IntoArray as _;
557    use crate::VortexSessionExecute;
558    use crate::aggregate_fn::Accumulator;
559    use crate::aggregate_fn::AggregateDTypes;
560    use crate::aggregate_fn::AggregateFnVTable;
561    use crate::aggregate_fn::DynAccumulator;
562    use crate::aggregate_fn::NumericalAggregateOpts;
563    use crate::aggregate_fn::fns::min_max::MinMax;
564    use crate::aggregate_fn::fns::min_max::MinMaxPartial;
565    use crate::aggregate_fn::fns::min_max::MinMaxResult;
566    use crate::aggregate_fn::fns::min_max::make_minmax_dtype;
567    use crate::aggregate_fn::fns::min_max::min_max;
568    use crate::arrays::BoolArray;
569    use crate::arrays::ChunkedArray;
570    use crate::arrays::ConstantArray;
571    use crate::arrays::DecimalArray;
572    use crate::arrays::ExtensionArray;
573    use crate::arrays::FixedSizeListArray;
574    use crate::arrays::ListArray;
575    use crate::arrays::NullArray;
576    use crate::arrays::PrimitiveArray;
577    use crate::arrays::TemporalArray;
578    use crate::arrays::VarBinArray;
579    use crate::dtype::BigCast;
580    use crate::dtype::DType;
581    use crate::dtype::DecimalDType;
582    use crate::dtype::DecimalType;
583    use crate::dtype::Nullability;
584    use crate::dtype::PType;
585    use crate::dtype::extension::ExtDType;
586    use crate::dtype::extension::ExtId;
587    use crate::dtype::extension::ExtVTable;
588    use crate::expr::stats::Precision;
589    use crate::expr::stats::Stat;
590    use crate::expr::stats::StatsProvider;
591    use crate::extension::datetime::TimeUnit;
592    use crate::match_each_decimal_value_type;
593    use crate::scalar::DecimalValue;
594    use crate::scalar::Scalar;
595    use crate::scalar::ScalarValue;
596    use crate::validity::Validity;
597
598    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
599
600    #[test]
601    fn test_prim_min_max() -> VortexResult<()> {
602        let p = PrimitiveArray::new(buffer![1, 2, 3], Validity::NonNullable).into_array();
603        let mut ctx = SESSION.create_execution_ctx();
604        assert_eq!(
605            min_max(&p, &mut ctx, NumericalAggregateOpts::default())?,
606            Some(MinMaxResult {
607                min: 1.into(),
608                max: 3.into()
609            })
610        );
611        Ok(())
612    }
613
614    #[test]
615    fn test_timestamp_min_max_beyond_jiff_range() -> VortexResult<()> {
616        // Midnight on 9999-12-31 UTC.
617        const END_OF_9999_US: i64 = 253_402_214_400_000_000;
618        let timestamps = TemporalArray::new_timestamp(
619            PrimitiveArray::from_iter([0i64, END_OF_9999_US]).into_array(),
620            TimeUnit::Microseconds,
621            None,
622        )
623        .into_array();
624        let mut ctx = SESSION.create_execution_ctx();
625        let MinMaxResult { min, max } =
626            min_max(&timestamps, &mut ctx, NumericalAggregateOpts::default())?
627                .vortex_expect("two non-null timestamps have min and max");
628
629        assert_eq!(min.as_extension().to_storage_scalar(), 0i64.into());
630        assert_eq!(
631            max.as_extension().to_storage_scalar(),
632            END_OF_9999_US.into()
633        );
634
635        Ok(())
636    }
637
638    #[test]
639    fn test_prim_min_max_multiple_null_runs() -> VortexResult<()> {
640        // Several disjoint valid runs separated by nulls exercise the per-run fold; the extrema
641        // (min 1, max 9) fall in different runs.
642        let p = PrimitiveArray::from_option_iter([
643            Some(5i32),
644            Some(3),
645            None,
646            None,
647            Some(9),
648            None,
649            Some(1),
650            Some(7),
651        ])
652        .into_array();
653        let mut ctx = SESSION.create_execution_ctx();
654        assert_eq!(
655            min_max(&p, &mut ctx, NumericalAggregateOpts::default())?,
656            Some(MinMaxResult {
657                min: 1.into(),
658                max: 9.into()
659            })
660        );
661        Ok(())
662    }
663
664    #[test]
665    fn test_bool_min_max() -> VortexResult<()> {
666        let mut ctx = SESSION.create_execution_ctx();
667
668        let all_true = BoolArray::new(
669            BitBuffer::from([true, true, true].as_slice()),
670            Validity::NonNullable,
671        )
672        .into_array();
673        assert_eq!(
674            min_max(&all_true, &mut ctx, NumericalAggregateOpts::default())?,
675            Some(MinMaxResult {
676                min: true.into(),
677                max: true.into()
678            })
679        );
680
681        let all_false = BoolArray::new(
682            BitBuffer::from([false, false, false].as_slice()),
683            Validity::NonNullable,
684        )
685        .into_array();
686        assert_eq!(
687            min_max(&all_false, &mut ctx, NumericalAggregateOpts::default())?,
688            Some(MinMaxResult {
689                min: false.into(),
690                max: false.into()
691            })
692        );
693
694        let mixed = BoolArray::new(
695            BitBuffer::from([false, true, false].as_slice()),
696            Validity::NonNullable,
697        )
698        .into_array();
699        assert_eq!(
700            min_max(&mixed, &mut ctx, NumericalAggregateOpts::default())?,
701            Some(MinMaxResult {
702                min: false.into(),
703                max: true.into()
704            })
705        );
706        Ok(())
707    }
708
709    #[test]
710    fn test_null_array() -> VortexResult<()> {
711        let p = NullArray::new(1).into_array();
712        let mut ctx = SESSION.create_execution_ctx();
713        assert_eq!(
714            min_max(&p, &mut ctx, NumericalAggregateOpts::default())?,
715            None
716        );
717        Ok(())
718    }
719
720    #[test]
721    fn test_prim_nan() -> VortexResult<()> {
722        let array = PrimitiveArray::new(
723            buffer![f32::NAN, -f32::NAN, -1.0, 1.0],
724            Validity::NonNullable,
725        );
726        let mut ctx = SESSION.create_execution_ctx();
727        let result = min_max(
728            &array.into_array(),
729            &mut ctx,
730            NumericalAggregateOpts::default(),
731        )?
732        .vortex_expect("should have result");
733        assert_eq!(f32::try_from(&result.min)?, -1.0);
734        assert_eq!(f32::try_from(&result.max)?, 1.0);
735        Ok(())
736    }
737
738    #[test]
739    fn test_prim_inf() -> VortexResult<()> {
740        let array = PrimitiveArray::new(
741            buffer![f32::INFINITY, f32::NEG_INFINITY, -1.0, 1.0],
742            Validity::NonNullable,
743        );
744        let mut ctx = SESSION.create_execution_ctx();
745        let result = min_max(
746            &array.into_array(),
747            &mut ctx,
748            NumericalAggregateOpts::default(),
749        )?
750        .vortex_expect("should have result");
751        assert_eq!(f32::try_from(&result.min)?, f32::NEG_INFINITY);
752        assert_eq!(f32::try_from(&result.max)?, f32::INFINITY);
753        Ok(())
754    }
755
756    #[test]
757    fn test_multi_batch() -> VortexResult<()> {
758        let mut ctx = SESSION.create_execution_ctx();
759        let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
760        let mut acc = Accumulator::try_new(MinMax, NumericalAggregateOpts::default(), dtype)?;
761
762        let batch1 = PrimitiveArray::new(buffer![10i32, 20, 5], Validity::NonNullable).into_array();
763        acc.accumulate(&batch1, &mut ctx)?;
764
765        let batch2 = PrimitiveArray::new(buffer![3i32, 25], Validity::NonNullable).into_array();
766        acc.accumulate(&batch2, &mut ctx)?;
767
768        let result = MinMaxResult::from_scalar(&acc.finish()?)?.vortex_expect("should have result");
769        assert_eq!(result.min, Scalar::from(3i32));
770        assert_eq!(result.max, Scalar::from(25i32));
771        Ok(())
772    }
773
774    #[test]
775    fn test_finish_resets_state() -> VortexResult<()> {
776        let mut ctx = SESSION.create_execution_ctx();
777        let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
778        let mut acc = Accumulator::try_new(MinMax, NumericalAggregateOpts::default(), dtype)?;
779
780        let batch1 = PrimitiveArray::new(buffer![10i32, 20], Validity::NonNullable).into_array();
781        acc.accumulate(&batch1, &mut ctx)?;
782        let result1 =
783            MinMaxResult::from_scalar(&acc.finish()?)?.vortex_expect("should have result");
784        assert_eq!(result1.min, Scalar::from(10i32));
785        assert_eq!(result1.max, Scalar::from(20i32));
786
787        let batch2 = PrimitiveArray::new(buffer![3i32, 6, 9], Validity::NonNullable).into_array();
788        acc.accumulate(&batch2, &mut ctx)?;
789        let result2 =
790            MinMaxResult::from_scalar(&acc.finish()?)?.vortex_expect("should have result");
791        assert_eq!(result2.min, Scalar::from(3i32));
792        assert_eq!(result2.max, Scalar::from(9i32));
793        Ok(())
794    }
795
796    #[test]
797    fn test_state_merge() -> VortexResult<()> {
798        let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
799        let options = NumericalAggregateOpts::default();
800        let owned = AggregateDTypes::try_new(&MinMax, &options, dtype)?;
801        let args = owned.args(&options);
802        let partial_of = |min: i32, max: i32| MinMaxPartial {
803            min: Some(Scalar::from(min)),
804            max: Some(Scalar::from(max)),
805        };
806
807        let state = MinMax.merge_partials(args, partial_of(5, 15), partial_of(2, 10))?;
808
809        let result = MinMaxResult::from_scalar(&MinMax.to_scalar(args, &state)?)?
810            .vortex_expect("should have result");
811        assert_eq!(result.min, Scalar::from(2i32));
812        assert_eq!(result.max, Scalar::from(15i32));
813        Ok(())
814    }
815
816    #[test]
817    fn test_constant_nan() -> VortexResult<()> {
818        let scalar = Scalar::primitive(f16::NAN, Nullability::NonNullable);
819        let array = ConstantArray::new(scalar, 2).into_array();
820        let mut ctx = SESSION.create_execution_ctx();
821        assert_eq!(
822            min_max(&array, &mut ctx, NumericalAggregateOpts::default())?,
823            None
824        );
825        Ok(())
826    }
827
828    const KEEP_NANS: NumericalAggregateOpts = NumericalAggregateOpts::include_nans();
829
830    fn assert_poisoned(result: Option<MinMaxResult>) -> VortexResult<()> {
831        let result = result.vortex_expect("should have result");
832        assert!(f64::try_from(&result.min.cast(&result.min.dtype().as_nullable())?)?.is_nan());
833        assert!(f64::try_from(&result.max.cast(&result.max.dtype().as_nullable())?)?.is_nan());
834        Ok(())
835    }
836
837    #[test]
838    fn test_prim_nan_not_skipping() -> VortexResult<()> {
839        let array = PrimitiveArray::new(
840            buffer![f32::NAN, -f32::NAN, -1.0, 1.0],
841            Validity::NonNullable,
842        )
843        .into_array();
844        let mut ctx = SESSION.create_execution_ctx();
845        assert_poisoned(min_max(&array, &mut ctx, KEEP_NANS)?)
846    }
847
848    #[test]
849    fn test_prim_no_nan_not_skipping() -> VortexResult<()> {
850        let array =
851            PrimitiveArray::new(buffer![3.0f32, -1.0, 1.0], Validity::NonNullable).into_array();
852        let mut ctx = SESSION.create_execution_ctx();
853        let result = min_max(&array, &mut ctx, KEEP_NANS)?.vortex_expect("should have result");
854        assert_eq!(f32::try_from(&result.min)?, -1.0);
855        assert_eq!(f32::try_from(&result.max)?, 3.0);
856        Ok(())
857    }
858
859    #[test]
860    fn test_constant_nan_not_skipping() -> VortexResult<()> {
861        let scalar = Scalar::primitive(f64::NAN, Nullability::NonNullable);
862        let array = ConstantArray::new(scalar, 2).into_array();
863        let mut ctx = SESSION.create_execution_ctx();
864        assert_poisoned(min_max(&array, &mut ctx, KEEP_NANS)?)
865    }
866
867    #[test]
868    fn test_not_skipping_with_exact_nan_count_stat() -> VortexResult<()> {
869        // The exact NaN count agrees with the batch; including NaNs must poison both extrema.
870        let array = PrimitiveArray::new(
871            buffer![1.0f64, 2.0, 3.0, f64::NAN, f64::NAN],
872            Validity::NonNullable,
873        )
874        .into_array();
875        array
876            .statistics()
877            .set(Stat::NaNCount, Precision::Exact(ScalarValue::from(2u64)));
878        let mut ctx = SESSION.create_execution_ctx();
879        assert_poisoned(min_max(&array, &mut ctx, KEEP_NANS)?)
880    }
881
882    #[test]
883    fn test_not_skipping_with_exact_stats_when_nan_free() -> VortexResult<()> {
884        // With an exact NaNCount of zero, the exact Min/Max stats are usable as-is.
885        let array =
886            PrimitiveArray::new(buffer![1.0f64, 2.0, 3.0], Validity::NonNullable).into_array();
887        array
888            .statistics()
889            .set(Stat::NaNCount, Precision::Exact(ScalarValue::from(0u64)));
890        array
891            .statistics()
892            .set(Stat::Min, Precision::Exact(ScalarValue::from(1.0f64)));
893        array
894            .statistics()
895            .set(Stat::Max, Precision::Exact(ScalarValue::from(3.0f64)));
896        let mut ctx = SESSION.create_execution_ctx();
897        let result = min_max(&array, &mut ctx, KEEP_NANS)?.vortex_expect("should have result");
898        assert_eq!(f64::try_from(&result.min)?, 1.0);
899        assert_eq!(f64::try_from(&result.max)?, 3.0);
900        Ok(())
901    }
902
903    #[test]
904    fn test_accumulator_nan_including_nullable_cached_stats() -> VortexResult<()> {
905        // A nullable float array's cached Min/Max stats are reconstructed as nullable scalars.
906        // The NaN-including accumulator shortcircuit must normalise them to the non-nullable
907        // struct field dtype before building the result scalar.
908        let mut ctx = SESSION.create_execution_ctx();
909        let array =
910            PrimitiveArray::from_option_iter([Some(1.0f64), Some(2.0), Some(3.0)]).into_array();
911        array
912            .statistics()
913            .set(Stat::NaNCount, Precision::Exact(ScalarValue::from(0u64)));
914        array
915            .statistics()
916            .set(Stat::Min, Precision::Exact(ScalarValue::from(1.0f64)));
917        array
918            .statistics()
919            .set(Stat::Max, Precision::Exact(ScalarValue::from(3.0f64)));
920
921        let mut acc = Accumulator::try_new(MinMax, KEEP_NANS, array.dtype().clone())?;
922        acc.accumulate(&array, &mut ctx)?;
923        let result = MinMaxResult::from_scalar(&acc.finish()?)?.vortex_expect("should have result");
924        assert_eq!(f64::try_from(&result.min)?, 1.0);
925        assert_eq!(f64::try_from(&result.max)?, 3.0);
926        Ok(())
927    }
928
929    #[test]
930    fn test_multi_batch_nan_poisoning() -> VortexResult<()> {
931        let mut ctx = SESSION.create_execution_ctx();
932        let dtype = DType::Primitive(PType::F64, Nullability::NonNullable);
933        let mut acc = Accumulator::try_new(MinMax, KEEP_NANS, dtype)?;
934
935        let batch1 = PrimitiveArray::new(buffer![1.0f64, 2.0], Validity::NonNullable).into_array();
936        acc.accumulate(&batch1, &mut ctx)?;
937        assert!(!acc.is_saturated());
938
939        let batch2 = PrimitiveArray::new(buffer![f64::NAN], Validity::NonNullable).into_array();
940        acc.accumulate(&batch2, &mut ctx)?;
941        assert!(acc.is_saturated());
942
943        assert_poisoned(MinMaxResult::from_scalar(&acc.finish()?)?)
944    }
945
946    #[test]
947    fn test_chunked() -> VortexResult<()> {
948        let chunk1 = PrimitiveArray::from_option_iter([Some(5i32), None, Some(1)]);
949        let chunk2 = PrimitiveArray::from_option_iter([Some(10i32), Some(3), None]);
950        let dtype = chunk1.dtype().clone();
951        let chunked = ChunkedArray::try_new(vec![chunk1.into_array(), chunk2.into_array()], dtype)?;
952        let mut ctx = SESSION.create_execution_ctx();
953        let result = min_max(
954            &chunked.into_array(),
955            &mut ctx,
956            NumericalAggregateOpts::default(),
957        )?
958        .vortex_expect("should have result");
959        assert_eq!(result.min, Scalar::from(1i32));
960        assert_eq!(result.max, Scalar::from(10i32));
961        Ok(())
962    }
963
964    #[test]
965    fn test_all_null() -> VortexResult<()> {
966        let p = PrimitiveArray::from_option_iter::<i32, _>([None, None, None]);
967        let mut ctx = SESSION.create_execution_ctx();
968        assert_eq!(
969            min_max(&p.into_array(), &mut ctx, NumericalAggregateOpts::default())?,
970            None
971        );
972        Ok(())
973    }
974
975    #[test]
976    fn test_varbin() -> VortexResult<()> {
977        let array = VarBinArray::from_iter(
978            vec![
979                Some("hello world"),
980                None,
981                Some("hello world this is a long string"),
982                None,
983            ],
984            DType::Utf8(Nullability::Nullable),
985        );
986        let mut ctx = SESSION.create_execution_ctx();
987        let result = min_max(
988            &array.into_array(),
989            &mut ctx,
990            NumericalAggregateOpts::default(),
991        )?
992        .vortex_expect("should have result");
993        assert_eq!(
994            result.min,
995            Scalar::utf8("hello world", Nullability::NonNullable)
996        );
997        assert_eq!(
998            result.max,
999            Scalar::utf8(
1000                "hello world this is a long string",
1001                Nullability::NonNullable
1002            )
1003        );
1004        Ok(())
1005    }
1006
1007    #[test]
1008    fn test_decimal() -> VortexResult<()> {
1009        let decimal = DecimalArray::new(
1010            buffer![100i32, 2000i32, 200i32],
1011            DecimalDType::new(4, 2),
1012            Validity::from_iter([true, false, true]),
1013        );
1014        let mut ctx = SESSION.create_execution_ctx();
1015        let result = min_max(
1016            &decimal.into_array(),
1017            &mut ctx,
1018            NumericalAggregateOpts::default(),
1019        )?
1020        .vortex_expect("should have result");
1021
1022        let non_nullable_dtype = DType::Decimal(DecimalDType::new(4, 2), Nullability::NonNullable);
1023        let expected_min = Scalar::try_new(
1024            non_nullable_dtype.clone(),
1025            Some(ScalarValue::from(DecimalValue::from(100i32))),
1026        )?;
1027        let expected_max = Scalar::try_new(
1028            non_nullable_dtype,
1029            Some(ScalarValue::from(DecimalValue::from(200i32))),
1030        )?;
1031        assert_eq!(result.min, expected_min);
1032        assert_eq!(result.max, expected_max);
1033        Ok(())
1034    }
1035
1036    #[rstest]
1037    #[case::non_nullable(vec![Some(-3), Some(7), Some(5)], false, Some((-3, 7)))]
1038    #[case::all_valid(vec![Some(-3), Some(7), Some(5)], true, Some((-3, 7)))]
1039    #[case::null_extremes(vec![None, Some(-3), Some(7), None], true, Some((-3, 7)))]
1040    #[case::single_valid(vec![None, Some(5), None], true, Some((5, 5)))]
1041    #[case::single_row(vec![Some(5)], false, Some((5, 5)))]
1042    #[case::equal_values(vec![Some(5), Some(5)], true, Some((5, 5)))]
1043    #[case::all_null(vec![None, None], true, None)]
1044    #[case::empty_non_nullable(vec![], false, None)]
1045    #[case::empty_nullable(vec![], true, None)]
1046    fn test_decimal_valid_range(
1047        #[case] values: Vec<Option<i8>>,
1048        #[case] nullable: bool,
1049        #[case] expected: Option<(i8, i8)>,
1050        #[values(
1051            DecimalType::I8,
1052            DecimalType::I16,
1053            DecimalType::I32,
1054            DecimalType::I64,
1055            DecimalType::I128,
1056            DecimalType::I256
1057        )]
1058        storage: DecimalType,
1059    ) -> VortexResult<()> {
1060        let mut ctx = SESSION.create_execution_ctx();
1061        let decimal_dtype = DecimalDType::new(9, 2);
1062        let validity = if nullable {
1063            Validity::from_iter(values.iter().map(Option::is_some))
1064        } else {
1065            Validity::NonNullable
1066        };
1067        let expected = expected.map(|(min, max)| MinMaxResult {
1068            min: Scalar::decimal(min.into(), decimal_dtype, Nullability::NonNullable),
1069            max: Scalar::decimal(max.into(), decimal_dtype, Nullability::NonNullable),
1070        });
1071
1072        match_each_decimal_value_type!(storage, |T| {
1073            let buffer: Buffer<T> = values
1074                .iter()
1075                .enumerate()
1076                .map(|(row, value)| match value {
1077                    Some(value) => <T as BigCast>::from(*value)
1078                        .vortex_expect("i8 fixture values fit every decimal storage type"),
1079                    None if row % 2 == 0 => T::MIN,
1080                    None => T::MAX,
1081                })
1082                .collect();
1083            let array = DecimalArray::new(buffer, decimal_dtype, validity).into_array();
1084            let actual = min_max(&array, &mut ctx, NumericalAggregateOpts::default())?;
1085            assert_eq!(actual, expected);
1086            if let Some(actual) = actual {
1087                let dtype = DType::Decimal(decimal_dtype, Nullability::NonNullable);
1088                assert_eq!(actual.min.dtype(), &dtype);
1089                assert_eq!(actual.max.dtype(), &dtype);
1090            }
1091
1092            Ok(())
1093        })
1094    }
1095
1096    #[rstest]
1097    #[case::one_element(buffer![100i8])]
1098    #[case::min_max(buffer![1i8, 100i8])]
1099    #[case::negative_min(buffer![-100i8, 1i8])]
1100    fn test_decimal_value_outside_precision_errors(#[case] values: Buffer<i8>) -> VortexResult<()> {
1101        // 100 and -100 do not fit in decimal(2, 0), and `DecimalArray::new` does not check the
1102        // values.
1103        let decimal = DecimalArray::new(values, DecimalDType::new(2, 0), Validity::NonNullable);
1104        let mut ctx = SESSION.create_execution_ctx();
1105        let err = min_max(
1106            &decimal.into_array(),
1107            &mut ctx,
1108            NumericalAggregateOpts::default(),
1109        )
1110        .unwrap_err();
1111        assert!(
1112            err.to_string().contains("does not fit in precision"),
1113            "unexpected error: {err}"
1114        );
1115        Ok(())
1116    }
1117
1118    #[test]
1119    fn test_decimal_value_outside_precision_in_null_slot() -> VortexResult<()> {
1120        // The out-of-range value sits in a null slot, so it must be ignored.
1121        let decimal = DecimalArray::new(
1122            buffer![5i8, 100i8],
1123            DecimalDType::new(2, 0),
1124            Validity::from_iter([true, false]),
1125        );
1126        let mut ctx = SESSION.create_execution_ctx();
1127        let result = min_max(
1128            &decimal.into_array(),
1129            &mut ctx,
1130            NumericalAggregateOpts::default(),
1131        )?
1132        .vortex_expect("should have result");
1133
1134        let expected = Scalar::decimal(
1135            DecimalValue::I8(5),
1136            DecimalDType::new(2, 0),
1137            Nullability::NonNullable,
1138        );
1139        assert_eq!(result.min, expected);
1140        assert_eq!(result.max, expected);
1141        Ok(())
1142    }
1143
1144    #[test]
1145    fn list_and_fixed_size_list_return_dtype() {
1146        let element_dtype = DType::Primitive(PType::I32, Nullability::Nullable);
1147        let list_dtype = DType::List(Arc::new(element_dtype.clone()), Nullability::Nullable);
1148        let fixed_size_list_dtype =
1149            DType::FixedSizeList(Arc::new(element_dtype), 1, Nullability::Nullable);
1150
1151        assert_eq!(
1152            MinMax.return_dtype(&NumericalAggregateOpts::default(), &list_dtype),
1153            Some(make_minmax_dtype(&list_dtype))
1154        );
1155        assert_eq!(
1156            MinMax.return_dtype(&NumericalAggregateOpts::default(), &fixed_size_list_dtype),
1157            Some(make_minmax_dtype(&fixed_size_list_dtype))
1158        );
1159    }
1160
1161    #[test]
1162    fn list_and_fixed_size_list_min_max_returns_none() -> VortexResult<()> {
1163        let mut ctx = SESSION.create_execution_ctx();
1164
1165        let list_array = ListArray::try_new(
1166            buffer![1i32, 2, 3].into_array(),
1167            buffer![0u32, 2, 3].into_array(),
1168            Validity::NonNullable,
1169            &mut ctx,
1170        )?
1171        .into_array();
1172        assert_eq!(
1173            min_max(&list_array, &mut ctx, NumericalAggregateOpts::default())?,
1174            None
1175        );
1176
1177        let fixed_size_list_array = FixedSizeListArray::try_new(
1178            buffer![1i32, 2, 3, 4].into_array(),
1179            2,
1180            Validity::NonNullable,
1181            2,
1182        )?
1183        .into_array();
1184        assert_eq!(
1185            min_max(
1186                &fixed_size_list_array,
1187                &mut ctx,
1188                NumericalAggregateOpts::default()
1189            )?,
1190            None
1191        );
1192
1193        Ok(())
1194    }
1195
1196    use crate::dtype::half::f16;
1197
1198    #[test]
1199    fn test_bool_with_nulls() -> VortexResult<()> {
1200        let mut ctx = SESSION.create_execution_ctx();
1201
1202        let result = min_max(
1203            &BoolArray::from_iter(vec![Some(true), Some(true), None, None]).into_array(),
1204            &mut ctx,
1205            NumericalAggregateOpts::default(),
1206        )?;
1207        assert_eq!(
1208            result,
1209            Some(MinMaxResult {
1210                min: Scalar::bool(true, Nullability::NonNullable),
1211                max: Scalar::bool(true, Nullability::NonNullable),
1212            })
1213        );
1214
1215        let result = min_max(
1216            &BoolArray::from_iter(vec![None, Some(true), Some(true)]).into_array(),
1217            &mut ctx,
1218            NumericalAggregateOpts::default(),
1219        )?;
1220        assert_eq!(
1221            result,
1222            Some(MinMaxResult {
1223                min: Scalar::bool(true, Nullability::NonNullable),
1224                max: Scalar::bool(true, Nullability::NonNullable),
1225            })
1226        );
1227
1228        let result = min_max(
1229            &BoolArray::from_iter(vec![None, Some(true), Some(true), None]).into_array(),
1230            &mut ctx,
1231            NumericalAggregateOpts::default(),
1232        )?;
1233        assert_eq!(
1234            result,
1235            Some(MinMaxResult {
1236                min: Scalar::bool(true, Nullability::NonNullable),
1237                max: Scalar::bool(true, Nullability::NonNullable),
1238            })
1239        );
1240
1241        let result = min_max(
1242            &BoolArray::from_iter(vec![Some(false), Some(false), None, None]).into_array(),
1243            &mut ctx,
1244            NumericalAggregateOpts::default(),
1245        )?;
1246        assert_eq!(
1247            result,
1248            Some(MinMaxResult {
1249                min: Scalar::bool(false, Nullability::NonNullable),
1250                max: Scalar::bool(false, Nullability::NonNullable),
1251            })
1252        );
1253        Ok(())
1254    }
1255
1256    /// Regression test for <https://github.com/vortex-data/vortex/issues/7074>.
1257    ///
1258    /// A chunked all-true bool array with an empty first chunk returned min=false because
1259    /// `accumulate_bool` on the empty chunk incorrectly merged min=false,max=false into the
1260    /// partial state.
1261    #[test]
1262    fn test_bool_chunked_with_empty_chunk() -> VortexResult<()> {
1263        let mut ctx = SESSION.create_execution_ctx();
1264
1265        let empty = BoolArray::new(BitBuffer::from([].as_slice()), Validity::NonNullable);
1266        let chunk1 = BoolArray::new(
1267            BitBuffer::from([true, true].as_slice()),
1268            Validity::NonNullable,
1269        );
1270        let chunk2 = BoolArray::new(
1271            BitBuffer::from([true, true, true].as_slice()),
1272            Validity::NonNullable,
1273        );
1274        let chunked = ChunkedArray::try_new(
1275            vec![empty.into_array(), chunk1.into_array(), chunk2.into_array()],
1276            DType::Bool(Nullability::NonNullable),
1277        )?;
1278
1279        let result = min_max(
1280            &chunked.into_array(),
1281            &mut ctx,
1282            NumericalAggregateOpts::default(),
1283        )?;
1284        assert_eq!(
1285            result,
1286            Some(MinMaxResult {
1287                min: Scalar::bool(true, Nullability::NonNullable),
1288                max: Scalar::bool(true, Nullability::NonNullable),
1289            })
1290        );
1291        Ok(())
1292    }
1293
1294    #[test]
1295    fn empty_constant_leaves_partial_empty() -> VortexResult<()> {
1296        let mut ctx = SESSION.create_execution_ctx();
1297        let array = ConstantArray::new(Scalar::from(99i32), 0).into_array();
1298        let options = NumericalAggregateOpts::default();
1299        let mut acc = Accumulator::try_new(MinMax, options, array.dtype().clone())?;
1300
1301        acc.accumulate(&array, &mut ctx)?;
1302
1303        assert!(acc.partial_scalar()?.is_null());
1304        Ok(())
1305    }
1306
1307    /// Regression test for <https://github.com/vortex-data/vortex/issues/8145>.
1308    ///
1309    /// A chunked array whose first chunk is an *empty* constant array — as produced by
1310    /// `fill_null` on an empty all-null chunk — returned `max = u32::MAX` because
1311    /// `ChunkedArrayAggregate` accumulated the empty chunk, folding its fill scalar into the
1312    /// running min/max. Empty chunks are now skipped during chunked aggregation.
1313    #[test]
1314    fn test_chunked_with_empty_constant_chunk() -> VortexResult<()> {
1315        let mut ctx = SESSION.create_execution_ctx();
1316
1317        let empty = ConstantArray::new(Scalar::primitive(u32::MAX, Nullability::NonNullable), 0)
1318            .into_array();
1319        let chunk1 = PrimitiveArray::new(buffer![7631471u32], Validity::NonNullable).into_array();
1320        let chunk2 = PrimitiveArray::new(buffer![0u32], Validity::NonNullable).into_array();
1321        let chunked = ChunkedArray::try_new(
1322            vec![empty, chunk1, chunk2],
1323            DType::Primitive(PType::U32, Nullability::NonNullable),
1324        )?;
1325
1326        assert_eq!(
1327            min_max(
1328                &chunked.into_array(),
1329                &mut ctx,
1330                NumericalAggregateOpts::default()
1331            )?,
1332            Some(MinMaxResult {
1333                min: Scalar::primitive(0u32, Nullability::NonNullable),
1334                max: Scalar::primitive(7631471u32, Nullability::NonNullable),
1335            })
1336        );
1337        Ok(())
1338    }
1339
1340    #[test]
1341    fn test_varbin_all_nulls() -> VortexResult<()> {
1342        let array = VarBinArray::from_iter(
1343            vec![Option::<&str>::None, None, None],
1344            DType::Utf8(Nullability::Nullable),
1345        );
1346        let mut ctx = SESSION.create_execution_ctx();
1347        assert_eq!(
1348            min_max(
1349                &array.into_array(),
1350                &mut ctx,
1351                NumericalAggregateOpts::default()
1352            )?,
1353            None
1354        );
1355        Ok(())
1356    }
1357
1358    #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
1359    struct F64Ext;
1360
1361    impl ExtVTable for F64Ext {
1362        type Metadata = EmptyMetadata;
1363        type NativeValue<'a> = &'a ScalarValue;
1364
1365        #[expect(clippy::disallowed_methods, reason = "test-only id")]
1366        fn id(&self) -> ExtId {
1367            ExtId::new("test.f64_ext")
1368        }
1369
1370        fn serialize_metadata(&self, _metadata: &Self::Metadata) -> VortexResult<Vec<u8>> {
1371            Ok(vec![])
1372        }
1373
1374        fn deserialize_metadata(&self, _data: &[u8]) -> VortexResult<Self::Metadata> {
1375            Ok(EmptyMetadata)
1376        }
1377
1378        fn validate_dtype(_extension_dtype: &ExtDType<Self>) -> VortexResult<()> {
1379            Ok(())
1380        }
1381
1382        fn unpack_native<'a>(
1383            _extension_dtype: &'a ExtDType<Self>,
1384            storage_value: &'a ScalarValue,
1385        ) -> VortexResult<Self::NativeValue<'a>> {
1386            Ok(storage_value)
1387        }
1388    }
1389
1390    fn f64_ext_array(values: Buffer<f64>) -> VortexResult<ArrayRef> {
1391        let ext_dtype = ExtDType::<F64Ext>::try_new(
1392            EmptyMetadata,
1393            DType::Primitive(PType::F64, Nullability::NonNullable),
1394        )?
1395        .erased();
1396        Ok(ExtensionArray::new(ext_dtype, values.into_array()).into_array())
1397    }
1398
1399    fn ext_storage_f64(scalar: &Scalar) -> VortexResult<f64> {
1400        f64::try_from(&scalar.as_extension().to_storage_scalar())
1401    }
1402
1403    #[test]
1404    fn test_extension_nan_not_skipping() -> VortexResult<()> {
1405        let array = f64_ext_array(buffer![1.0f64, f64::NAN, 3.0])?;
1406        let mut ctx = SESSION.create_execution_ctx();
1407        let result = min_max(&array, &mut ctx, KEEP_NANS)?.vortex_expect("should have result");
1408        assert_eq!(result.min.dtype(), &array.dtype().as_nonnullable());
1409        assert!(ext_storage_f64(&result.min)?.is_nan());
1410        assert!(ext_storage_f64(&result.max)?.is_nan());
1411        // A NaN-including result is not a NaN-skipping statistic.
1412        assert!(array.statistics().get(Stat::Min).as_exact().is_none());
1413        Ok(())
1414    }
1415
1416    #[test]
1417    fn test_extension_nan_skipping() -> VortexResult<()> {
1418        let array = f64_ext_array(buffer![1.0f64, f64::NAN, 3.0])?;
1419        let mut ctx = SESSION.create_execution_ctx();
1420        let result = min_max(&array, &mut ctx, NumericalAggregateOpts::default())?
1421            .vortex_expect("should have result");
1422        assert_eq!(ext_storage_f64(&result.min)?, 1.0);
1423        assert_eq!(ext_storage_f64(&result.max)?, 3.0);
1424        Ok(())
1425    }
1426
1427    #[test]
1428    fn test_extension_nan_free_not_skipping() -> VortexResult<()> {
1429        let array = f64_ext_array(buffer![2.0f64, 1.0, 3.0])?;
1430        let mut ctx = SESSION.create_execution_ctx();
1431        let result = min_max(&array, &mut ctx, KEEP_NANS)?.vortex_expect("should have result");
1432        assert_eq!(ext_storage_f64(&result.min)?, 1.0);
1433        assert_eq!(ext_storage_f64(&result.max)?, 3.0);
1434        Ok(())
1435    }
1436}