Skip to main content

vortex_array/arrays/primitive/compute/
cast.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use num_traits::AsPrimitive;
5use num_traits::CheckedMul;
6use num_traits::NumCast;
7use vortex_buffer::Buffer;
8use vortex_buffer::BufferMut;
9use vortex_compute::lane_kernels::IndexedSinkExt;
10use vortex_compute::lane_kernels::IndexedSourceExt;
11use vortex_compute::lane_kernels::ReinterpretSink;
12use vortex_error::VortexError;
13use vortex_error::VortexResult;
14use vortex_error::vortex_bail;
15use vortex_error::vortex_err;
16use vortex_mask::Mask;
17
18use crate::ArrayRef;
19use crate::ExecutionCtx;
20use crate::IntoArray;
21use crate::aggregate_fn;
22use crate::array::ArrayView;
23use crate::arrays::ConstantArray;
24use crate::arrays::DecimalArray;
25use crate::arrays::Primitive;
26use crate::arrays::PrimitiveArray;
27use crate::arrays::primitive::PrimitiveArrayExt;
28use crate::dtype::BigCast;
29use crate::dtype::DType;
30use crate::dtype::DecimalDType;
31use crate::dtype::DecimalType;
32use crate::dtype::IntegerPType;
33use crate::dtype::NativeDecimalType;
34use crate::dtype::NativePType;
35use crate::dtype::Nullability;
36use crate::dtype::PType;
37use crate::dtype::ToI256;
38use crate::dtype::i256;
39use crate::expr::stats::Stat;
40use crate::expr::stats::StatsProvider;
41use crate::match_each_decimal_value_type;
42use crate::match_each_integer_ptype;
43use crate::match_each_native_ptype;
44use crate::match_each_signed_integer_ptype;
45use crate::scalar::DecimalValue;
46use crate::scalar::Scalar;
47use crate::scalar_fn::fns::cast::CastKernel;
48use crate::scalar_fn::fns::cast::CastReduce;
49use crate::validity::Validity;
50
51impl CastReduce for Primitive {
52    fn cast(array: ArrayView<'_, Primitive>, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
53        // Only the same ptype is reducible without execution; type changes need the kernel
54        // to verify values fit in the target range.
55        let DType::Primitive(new_ptype, new_nullability) = dtype else {
56            return Ok(None);
57        };
58        if *new_ptype != array.ptype() {
59            return Ok(None);
60        }
61
62        let Some(new_validity) = array
63            .validity()?
64            .trivially_cast_nullability(*new_nullability, array.len())?
65        else {
66            return Ok(None);
67        };
68
69        // SAFETY: validity and data buffer still have same length.
70        Ok(Some(unsafe {
71            PrimitiveArray::new_unchecked_from_handle(
72                array.buffer_handle().clone(),
73                array.ptype(),
74                new_validity,
75            )
76            .into_array()
77        }))
78    }
79}
80
81impl CastKernel for Primitive {
82    fn cast(
83        array: ArrayView<'_, Primitive>,
84        dtype: &DType,
85        ctx: &mut ExecutionCtx,
86    ) -> VortexResult<Option<ArrayRef>> {
87        if let DType::Decimal(decimal_dtype, nullability) = dtype {
88            return cast_to_decimal(array, *decimal_dtype, *nullability, ctx).map(Some);
89        }
90        let DType::Primitive(new_ptype, new_nullability) = dtype else {
91            return Ok(None);
92        };
93        let (new_ptype, new_nullability) = (*new_ptype, *new_nullability);
94        let src_ptype = array.ptype();
95
96        let new_validity = array
97            .validity()?
98            .cast_nullability(new_nullability, array.len(), ctx)?;
99
100        // Same bit representation: either the same ptype (only the nullability changed) or two
101        // same-width integers (identical layout under 2's complement). The only non-trivial case
102        // is the sign change between same-width ints, which still needs a value-range check.
103        let same_rep = src_ptype == new_ptype
104            || (src_ptype.is_int()
105                && new_ptype.is_int()
106                && src_ptype.byte_width() == new_ptype.byte_width());
107        if same_rep {
108            if !values_fit_in(array, new_ptype, ctx, true) {
109                vortex_bail!(
110                    "Cannot cast {} to {} — values exceed target range",
111                    src_ptype,
112                    new_ptype,
113                );
114            }
115            return Ok(Some(reinterpret(array, new_ptype, new_validity)));
116        }
117
118        // Different bit rep: cast each element. `cast_values` picks a pure or checked loop based
119        // on whether the conversion is statically infallible.
120        Ok(Some(match_each_native_ptype!(new_ptype, |T| {
121            match_each_native_ptype!(src_ptype, |F| {
122                cast_values::<F, T>(array, new_validity, ctx)?
123            })
124        })))
125    }
126}
127
128fn cast_to_decimal(
129    array: ArrayView<'_, Primitive>,
130    decimal_dtype: DecimalDType,
131    nullability: Nullability,
132    ctx: &mut ExecutionCtx,
133) -> VortexResult<ArrayRef> {
134    if !array.ptype().is_int() {
135        vortex_bail!(
136            "Cannot cast floating primitive {} to decimal {}",
137            array.ptype(),
138            decimal_dtype
139        );
140    }
141
142    let source_validity = array.validity()?;
143    let validity = source_validity
144        .clone()
145        .cast_nullability(nullability, array.len(), ctx)?;
146    let values_type = decimal_dtype.canonical_values_type();
147
148    if decimal_dtype.scale() == 0
149        && signed_primitive_decimal_type(array.ptype()) == Some(values_type)
150    {
151        return match_each_signed_integer_ptype!(array.ptype(), |S| {
152            cast_unscaled_same_width_signed_integer_to_decimal::<S>(
153                array,
154                decimal_dtype,
155                validity,
156                &source_validity,
157                ctx,
158            )
159        });
160    }
161
162    let valid_values = source_validity.execute_mask(array.len(), ctx)?;
163    if !array.is_empty() && matches!(valid_values, Mask::AllFalse(_)) {
164        return Ok(ConstantArray::new(
165            Scalar::null(DType::Decimal(decimal_dtype, nullability)),
166            array.len(),
167        )
168        .into_array());
169    }
170    match_each_integer_ptype!(array.ptype(), |S| {
171        match_each_decimal_value_type!(values_type, |T| {
172            cast_integer_values_to_decimal::<S, T>(array, decimal_dtype, validity, &valid_values)
173        })
174    })
175}
176
177fn cast_unscaled_same_width_signed_integer_to_decimal<S>(
178    array: ArrayView<'_, Primitive>,
179    decimal_dtype: DecimalDType,
180    validity: Validity,
181    source_validity: &Validity,
182    ctx: &mut ExecutionCtx,
183) -> VortexResult<ArrayRef>
184where
185    S: IntegerPType + NativeDecimalType + ToI256,
186{
187    let values = array.as_slice::<S>();
188    let target_dtype = DType::Decimal(decimal_dtype, Nullability::NonNullable);
189    if !cached_values_fit_in(array, &target_dtype).unwrap_or(false) {
190        let valid_values = source_validity.execute_mask(array.len(), ctx)?;
191        validate_unscaled_signed_integer_values_to_decimal(values, decimal_dtype, &valid_values)
192            .map_err(|idx| primitive_to_decimal_cast_error(values[idx], decimal_dtype))?;
193    }
194
195    // SAFETY: `S::DECIMAL_TYPE` has the same physical representation as the source ptype, and
196    // either exact min/max statistics or the validation above prove every valid value fits.
197    Ok(unsafe {
198        DecimalArray::new_unchecked_handle(
199            array.buffer_handle().clone(),
200            S::DECIMAL_TYPE,
201            decimal_dtype,
202            validity,
203        )
204        .into_array()
205    })
206}
207
208fn validate_unscaled_signed_integer_values_to_decimal<S>(
209    values: &[S],
210    decimal_dtype: DecimalDType,
211    valid_values: &Mask,
212) -> Result<(), usize>
213where
214    S: NativeDecimalType,
215{
216    let fits = |value| decimal_value_fits_precision(value, decimal_dtype);
217    match valid_values {
218        Mask::AllTrue(_) => values
219            .iter()
220            .position(|&value| !fits(value))
221            .map_or(Ok(()), Err),
222        Mask::AllFalse(_) => Ok(()),
223        Mask::Values(mask) => {
224            let mut first_failure = None;
225            mask.bit_buffer().for_each_set_index(|idx| {
226                if first_failure.is_none() && !fits(values[idx]) {
227                    first_failure = Some(idx);
228                }
229            });
230            first_failure.map_or(Ok(()), Err)
231        }
232    }
233}
234
235fn cast_integer_values_to_decimal<S, T>(
236    array: ArrayView<'_, Primitive>,
237    decimal_dtype: DecimalDType,
238    validity: Validity,
239    valid_values: &Mask,
240) -> VortexResult<ArrayRef>
241where
242    S: IntegerPType + ToI256,
243    T: NativeDecimalType + CheckedMul,
244{
245    let scale = decimal_dtype.scale();
246    let buffer = if scale == 0 {
247        cast_unscaled_integer_values_to_decimal::<S, T>(array, decimal_dtype, valid_values)?
248    } else if scale > 0 {
249        cast_scaled_up_integer_values_to_decimal::<S, T>(array, decimal_dtype, valid_values)?
250    } else {
251        cast_scaled_down_integer_values_to_decimal::<S, T>(array, decimal_dtype, valid_values)?
252    };
253
254    Ok(DecimalArray::new(buffer, decimal_dtype, validity).into_array())
255}
256
257fn cast_unscaled_integer_values_to_decimal<S, T>(
258    array: ArrayView<'_, Primitive>,
259    decimal_dtype: DecimalDType,
260    valid_values: &Mask,
261) -> VortexResult<Buffer<T>>
262where
263    S: IntegerPType + ToI256,
264    T: NativeDecimalType,
265{
266    let values = array.as_slice::<S>();
267    cast_integer_values_to_decimal_buffer(values, decimal_dtype, valid_values, |value| {
268        let value = <T as BigCast>::from(value)?;
269        decimal_value_fits_precision(value, decimal_dtype).then_some(value)
270    })
271}
272
273fn cast_scaled_up_integer_values_to_decimal<S, T>(
274    array: ArrayView<'_, Primitive>,
275    decimal_dtype: DecimalDType,
276    valid_values: &Mask,
277) -> VortexResult<Buffer<T>>
278where
279    S: IntegerPType + ToI256,
280    T: NativeDecimalType + CheckedMul,
281{
282    let values = array.as_slice::<S>();
283    let scale_factor = decimal_scale_factor::<T>(decimal_dtype.scale())?;
284    cast_integer_values_to_decimal_buffer(values, decimal_dtype, valid_values, |value| {
285        let value = <T as BigCast>::from(value)?;
286        let value = value.checked_mul(&scale_factor)?;
287        decimal_value_fits_precision(value, decimal_dtype).then_some(value)
288    })
289}
290
291fn cast_scaled_down_integer_values_to_decimal<S, T>(
292    array: ArrayView<'_, Primitive>,
293    decimal_dtype: DecimalDType,
294    valid_values: &Mask,
295) -> VortexResult<Buffer<T>>
296where
297    S: IntegerPType + ToI256,
298    T: NativeDecimalType,
299{
300    let values = array.as_slice::<S>();
301    if decimal_dtype.scale().unsigned_abs() >= primitive_max_decimal_digits(array.ptype()) {
302        // The scale factor exceeds every source value, so only zero can be exactly rescaled.
303        return cast_integer_values_to_decimal_buffer(
304            values,
305            decimal_dtype,
306            valid_values,
307            |value| (value == S::default()).then_some(T::default()),
308        );
309    }
310
311    // Scaling down can shrink a value into a narrower target type, so first select the smallest
312    // signed carrier that can represent both the source and target.
313    let carrier_type = primitive_decimal_carrier_type(array.ptype()).max(T::DECIMAL_TYPE);
314    match_each_decimal_value_type!(carrier_type, |W| {
315        // `S` and `T` fix the carrier, so only one arm can match. The const condition lets the
316        // compiler skip codegen of the kernel for the other arms.
317        if const { is_scaled_down_carrier::<S, T, W>() } {
318            let scale_factor = decimal_scale_factor::<W>(decimal_dtype.scale())?;
319            cast_integer_values_to_decimal_buffer(values, decimal_dtype, valid_values, |value| {
320                let value = <W as BigCast>::from(value)?;
321                let quotient = value / scale_factor;
322                let value = (quotient * scale_factor == value).then_some(quotient)?;
323                let value = <T as BigCast>::from(value)?;
324                decimal_value_fits_precision(value, decimal_dtype).then_some(value)
325            })
326        } else {
327            unreachable!(
328                "{carrier_type} is not the scaled-down carrier for {}",
329                S::PTYPE
330            )
331        }
332    })
333}
334
335/// Whether `W` is the carrier that a scaled-down cast from `S` to `T` selects.
336const fn is_scaled_down_carrier<S, T, W>() -> bool
337where
338    S: NativePType,
339    T: NativeDecimalType,
340    W: NativeDecimalType,
341{
342    let carrier = primitive_decimal_carrier_type(S::PTYPE) as u8;
343    let target = T::DECIMAL_TYPE as u8;
344    let selected = if carrier > target { carrier } else { target };
345
346    W::DECIMAL_TYPE as u8 == selected
347}
348
349fn cast_integer_values_to_decimal_buffer<S, T>(
350    values: &[S],
351    decimal_dtype: DecimalDType,
352    valid_values: &Mask,
353    cast: impl Fn(S) -> Option<T>,
354) -> VortexResult<Buffer<T>>
355where
356    S: IntegerPType + ToI256,
357    T: NativeDecimalType,
358{
359    cast_primitive_to_decimal_buffer(values, valid_values, cast)
360        .map_err(|idx| primitive_to_decimal_cast_error(values[idx], decimal_dtype))
361}
362
363/// Decimal physical type with the same representation, when `ptype` is signed.
364fn signed_primitive_decimal_type(ptype: PType) -> Option<DecimalType> {
365    match ptype {
366        PType::I8 => Some(DecimalType::I8),
367        PType::I16 => Some(DecimalType::I16),
368        PType::I32 => Some(DecimalType::I32),
369        PType::I64 => Some(DecimalType::I64),
370        PType::U8 | PType::U16 | PType::U32 | PType::U64 => None,
371        PType::F16 | PType::F32 | PType::F64 => {
372            unreachable!("floating primitives are rejected before selecting a decimal type")
373        }
374    }
375}
376
377/// The smallest signed decimal physical type that represents every `ptype` value.
378#[expect(
379    clippy::panic,
380    reason = "`unreachable!` with a message is not available in a const fn"
381)]
382const fn primitive_decimal_carrier_type(ptype: PType) -> DecimalType {
383    match ptype {
384        PType::I8 => DecimalType::I8,
385        PType::U8 | PType::I16 => DecimalType::I16,
386        PType::U16 | PType::I32 => DecimalType::I32,
387        PType::U32 | PType::I64 => DecimalType::I64,
388        PType::U64 => DecimalType::I128,
389        PType::F16 | PType::F32 | PType::F64 => {
390            panic!("floating primitives are rejected before selecting a decimal carrier")
391        }
392    }
393}
394
395/// Maximum decimal digits in `ptype`; scales at least this large can exactly rescale only zero.
396fn primitive_max_decimal_digits(ptype: PType) -> u8 {
397    match ptype {
398        PType::U8 | PType::I8 => 3,
399        PType::U16 | PType::I16 => 5,
400        PType::U32 | PType::I32 => 10,
401        PType::U64 => 20,
402        PType::I64 => 19,
403        PType::F16 | PType::F32 | PType::F64 => {
404            unreachable!("floating primitives are rejected before inspecting decimal digits")
405        }
406    }
407}
408
409fn decimal_scale_factor<T>(scale: i8) -> VortexResult<T>
410where
411    T: NativeDecimalType + CheckedMul,
412{
413    let exponent = if scale > 0 {
414        scale as u32
415    } else {
416        (-(scale as i16)) as u32
417    };
418    let ten = <T as BigCast>::from(10i8).ok_or_else(|| scale_factor_error(scale))?;
419    let mut factor = <T as BigCast>::from(1i8).ok_or_else(|| scale_factor_error(scale))?;
420    for _ in 0..exponent {
421        factor = factor
422            .checked_mul(&ten)
423            .ok_or_else(|| scale_factor_error(scale))?;
424    }
425    Ok(factor)
426}
427
428#[cold]
429#[inline(never)]
430fn scale_factor_error(scale: i8) -> VortexError {
431    vortex_err!("Cannot create decimal scale factor for scale {scale}")
432}
433
434fn decimal_value_fits_precision<T: NativeDecimalType>(
435    value: T,
436    decimal_dtype: DecimalDType,
437) -> bool {
438    let precision = decimal_dtype.precision() as usize;
439    value >= T::MIN_BY_PRECISION[precision] && value <= T::MAX_BY_PRECISION[precision]
440}
441
442fn cast_primitive_to_decimal_buffer<S, T>(
443    values: &[S],
444    valid_values: &Mask,
445    cast: impl Fn(S) -> Option<T>,
446) -> Result<Buffer<T>, usize>
447where
448    S: NativePType,
449    T: NativeDecimalType,
450{
451    if matches!(valid_values, Mask::AllFalse(_)) {
452        return Ok(BufferMut::<T>::zeroed(values.len()).freeze());
453    }
454
455    let mut buffer = BufferMut::<T>::with_capacity(values.len());
456    match valid_values {
457        Mask::AllTrue(_) => {
458            values.try_map_into(&mut buffer.spare_capacity_mut()[..values.len()], &cast)?;
459        }
460        Mask::Values(mask) => {
461            values.try_map_masked_into(
462                mask.bit_buffer(),
463                &mut buffer.spare_capacity_mut()[..values.len()],
464                &cast,
465            )?;
466        }
467        Mask::AllFalse(_) => unreachable!("all-null values are handled before allocating"),
468    }
469    // SAFETY: the selected map kernel initialized every lane before returning Ok.
470    unsafe { buffer.set_len(values.len()) };
471    Ok(buffer.freeze())
472}
473
474#[cold]
475#[inline(never)]
476fn primitive_to_decimal_cast_error<S>(value: S, decimal_dtype: DecimalDType) -> VortexError
477where
478    S: IntegerPType + ToI256,
479{
480    let Some(value) = <i256 as BigCast>::from(value) else {
481        return vortex_err!(
482            "primitive value cannot be represented while casting to {}",
483            decimal_dtype
484        );
485    };
486
487    match DecimalValue::rescale_i256(value, 0, decimal_dtype.scale())
488        .and_then(|value| DecimalValue::try_from_i256(value, decimal_dtype))
489    {
490        Err(error) => error,
491        Ok(_) => {
492            debug_assert!(
493                false,
494                "primitive-to-decimal fast path rejected a value that the scalar cast accepts"
495            );
496            vortex_err!(
497                "primitive value cannot be represented while casting to {}",
498                decimal_dtype
499            )
500        }
501    }
502}
503
504/// Cast Primitive values from `F` to `T`.
505fn cast_values<F, T>(
506    array: ArrayView<'_, Primitive>,
507    new_validity: Validity,
508    ctx: &mut ExecutionCtx,
509) -> VortexResult<ArrayRef>
510where
511    F: NativePType + AsPrimitive<T>,
512    T: NativePType,
513{
514    let overflow = || {
515        vortex_err!(
516            "Cannot cast {} to {} — value exceeds target range",
517            F::PTYPE,
518            T::PTYPE,
519        )
520    };
521
522    // Returns `true` if every value of `from` is representable in `to` without loss.
523    fn casts_losslessly_to(from: PType, to: PType) -> bool {
524        if from == to {
525            return true;
526        }
527        if (from.is_unsigned_int() && to.is_unsigned_int())
528            || (from.is_signed_int() && to.is_signed_int())
529            || (from.is_float() && to.is_float())
530        {
531            return from.byte_width() <= to.byte_width();
532        }
533        if from.is_unsigned_int() && to.is_signed_int() {
534            return from.byte_width() < to.byte_width();
535        }
536        if from.is_int() && to.is_float() {
537            let minimum_float_width = match from.byte_width() {
538                1 => 2,
539                2 => 4,
540                4 => 8,
541                _ => return false,
542            };
543            return to.byte_width() >= minimum_float_width;
544        }
545        false
546    }
547
548    // Skip the fallible kernel when type widening or (cached) min/max prove every value fits.
549    let target_dtype = DType::Primitive(T::PTYPE, Nullability::NonNullable);
550    let infallible = casts_losslessly_to(F::PTYPE, T::PTYPE)
551        || cached_values_fit_in(array, &target_dtype).unwrap_or(false);
552
553    let len = array.len();
554
555    // If F and T have the same byte width, try to take unique ownership of the buffer.
556    let same_bit_width = F::PTYPE.byte_width() == T::PTYPE.byte_width();
557    let owned: Option<BufferMut<F>> = same_bit_width
558        .then(|| array.into_owned().try_into_buffer_mut::<F>().ok())
559        .flatten();
560    let values: &[F] = array.as_slice::<F>();
561
562    if infallible {
563        return match owned {
564            Some(mut buf) => {
565                ReinterpretSink::<F, T>::new(buf.as_mut_slice()).map_into_in_place(|v: F| v.as_());
566                // SAFETY: same size + alignment for NativePType
567                let result: BufferMut<T> = unsafe { buf.transmute::<T>() };
568                Ok(PrimitiveArray::new(result.freeze(), new_validity).into_array())
569            }
570            None => {
571                let mut buffer = BufferMut::<T>::with_capacity(len);
572                values.map_into(&mut buffer.spare_capacity_mut()[..len], |v| v.as_());
573                // SAFETY: map_into initializes every lane.
574                unsafe { buffer.set_len(len) };
575                Ok(PrimitiveArray::new(buffer.freeze(), new_validity).into_array())
576            }
577        };
578    }
579
580    let mask = array.validity()?.execute_mask(len, ctx)?;
581    if len > 0 && matches!(mask, Mask::AllFalse(_)) {
582        return Ok(ConstantArray::new(
583            Scalar::null(DType::Primitive(T::PTYPE, Nullability::Nullable)),
584            len,
585        )
586        .into_array());
587    }
588
589    let buffer: Buffer<T> = match (&mask, owned) {
590        (Mask::AllTrue(_), Some(mut buf)) => {
591            ReinterpretSink::<F, T>::new(buf.as_mut_slice())
592                .try_map_in_place(|v: F| <T as NumCast>::from(v))
593                .map_err(|_| overflow())?;
594            // SAFETY: same size + alignment for NativePType
595            let result: BufferMut<T> = unsafe { buf.transmute::<T>() };
596            result.freeze()
597        }
598        (Mask::AllTrue(_), None) => {
599            let mut buffer = BufferMut::<T>::with_capacity(len);
600            values
601                .try_map_into(&mut buffer.spare_capacity_mut()[..len], |v| {
602                    <T as NumCast>::from(v)
603                })
604                .map_err(|_| overflow())?;
605            // SAFETY: initialized every lane.
606            unsafe { buffer.set_len(len) };
607            buffer.freeze()
608        }
609        (Mask::AllFalse(_), _) => BufferMut::<T>::zeroed(len).freeze(),
610        (Mask::Values(m), Some(mut buf)) => {
611            ReinterpretSink::<F, T>::new(buf.as_mut_slice())
612                .try_map_masked_in_place(m.bit_buffer(), |v: F| <T as NumCast>::from(v))
613                .map_err(|_| overflow())?;
614            // SAFETY: same size + alignment for NativePType
615            let result: BufferMut<T> = unsafe { buf.transmute::<T>() };
616            result.freeze()
617        }
618        (Mask::Values(m), None) => {
619            let mut buffer = BufferMut::<T>::with_capacity(len);
620            values
621                .try_map_masked_into(
622                    m.bit_buffer(),
623                    &mut buffer.spare_capacity_mut()[..len],
624                    |v| <T as NumCast>::from(v),
625                )
626                .map_err(|_| overflow())?;
627            // SAFETY: initialized every lane.
628            unsafe { buffer.set_len(len) };
629            buffer.freeze()
630        }
631    };
632
633    Ok(PrimitiveArray::new(buffer, new_validity).into_array())
634}
635
636fn reinterpret(
637    array: ArrayView<'_, Primitive>,
638    new_ptype: PType,
639    new_validity: Validity,
640) -> ArrayRef {
641    // SAFETY: caller has verified the bit representation is compatible and that validity length
642    // still matches the buffer length.
643    unsafe {
644        PrimitiveArray::new_unchecked_from_handle(
645            array.buffer_handle().clone(),
646            new_ptype,
647            new_validity,
648        )
649    }
650    .into_array()
651}
652
653/// Returns `true` if all valid values in `array` are representable as `target_ptype`.
654///
655/// Cached min/max statistics are consulted first. If either bound is missing, the function either
656/// computes them with a single pass (when `compute` is `true`) or returns `false` so the caller
657/// can fall back to a slower path (when `compute` is `false`).
658fn values_fit_in(
659    array: ArrayView<'_, Primitive>,
660    target_ptype: PType,
661    ctx: &mut ExecutionCtx,
662    compute: bool,
663) -> bool {
664    let target_dtype = DType::Primitive(target_ptype, Nullability::NonNullable);
665    if let Some(fits) = cached_values_fit_in(array, &target_dtype) {
666        return fits;
667    }
668    if !compute {
669        return false;
670    }
671    aggregate_fn::fns::min_max::min_max(
672        array.array(),
673        ctx,
674        aggregate_fn::NumericalAggregateOpts::default(),
675    )
676    .ok()
677    .flatten()
678    .is_none_or(|mm| mm.min.cast(&target_dtype).is_ok() && mm.max.cast(&target_dtype).is_ok())
679}
680
681/// Cached-only check: returns `Some(fits)` if both `Min` and `Max` are present as `Exact` in the
682/// stats cache, otherwise `None`.
683fn cached_values_fit_in(array: ArrayView<'_, Primitive>, target_dtype: &DType) -> Option<bool> {
684    let stats = array.array().statistics();
685    let min = stats.get(Stat::Min).as_exact()?;
686    let max = stats.get(Stat::Max).as_exact()?;
687    Some(min.cast(target_dtype).is_ok() && max.cast(target_dtype).is_ok())
688}
689
690#[cfg(test)]
691mod test {
692    use std::sync::LazyLock;
693
694    use rstest::rstest;
695    use vortex_buffer::BitBuffer;
696    use vortex_buffer::buffer;
697    use vortex_error::VortexResult;
698    use vortex_mask::Mask;
699    use vortex_session::VortexSession;
700
701    use crate::ArrayRef;
702    use crate::Canonical;
703    use crate::IntoArray;
704    use crate::VortexSessionExecute;
705    use crate::arrays::DecimalArray;
706    use crate::arrays::PrimitiveArray;
707    use crate::assert_arrays_eq;
708    use crate::builtins::ArrayBuiltins;
709    use crate::compute::conformance::cast::test_cast_conformance;
710    use crate::dtype::DType;
711    use crate::dtype::DecimalDType;
712    use crate::dtype::DecimalType;
713    use crate::dtype::Nullability;
714    use crate::dtype::PType;
715    use crate::dtype::i256;
716    use crate::expr::stats::Stat;
717    use crate::match_each_decimal_value_type;
718    use crate::validity::Validity;
719
720    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
721
722    #[test]
723    fn cast_u32_u8() {
724        let mut ctx = SESSION.create_execution_ctx();
725        let arr = buffer![0u32, 10, 200].into_array();
726
727        // cast from u32 to u8
728        let p = arr
729            .cast(PType::U8.into())
730            .unwrap()
731            .execute::<PrimitiveArray>(&mut ctx)
732            .unwrap();
733        assert_arrays_eq!(p, PrimitiveArray::from_iter([0u8, 10, 200]), &mut ctx);
734        assert!(matches!(p.validity(), Ok(Validity::NonNullable)));
735
736        // to nullable
737        let p = p
738            .into_array()
739            .cast(DType::Primitive(PType::U8, Nullability::Nullable))
740            .unwrap()
741            .execute::<PrimitiveArray>(&mut ctx)
742            .unwrap();
743        assert_arrays_eq!(
744            p,
745            PrimitiveArray::new(buffer![0u8, 10, 200], Validity::AllValid),
746            &mut ctx
747        );
748        assert!(matches!(p.validity(), Ok(Validity::AllValid)));
749
750        // back to non-nullable
751        let p = p
752            .into_array()
753            .cast(DType::Primitive(PType::U8, Nullability::NonNullable))
754            .unwrap()
755            .execute::<PrimitiveArray>(&mut ctx)
756            .unwrap();
757        assert_arrays_eq!(p, PrimitiveArray::from_iter([0u8, 10, 200]), &mut ctx);
758        assert!(matches!(p.validity(), Ok(Validity::NonNullable)));
759
760        // to nullable u32
761        let p = p
762            .into_array()
763            .cast(DType::Primitive(PType::U32, Nullability::Nullable))
764            .unwrap()
765            .execute::<PrimitiveArray>(&mut ctx)
766            .unwrap();
767        assert_arrays_eq!(
768            p,
769            PrimitiveArray::new(buffer![0u32, 10, 200], Validity::AllValid),
770            &mut ctx
771        );
772        assert!(matches!(p.validity(), Ok(Validity::AllValid)));
773
774        // to non-nullable u8
775        let p = p
776            .into_array()
777            .cast(DType::Primitive(PType::U8, Nullability::NonNullable))
778            .unwrap()
779            .execute::<PrimitiveArray>(&mut ctx)
780            .unwrap();
781        assert_arrays_eq!(p, PrimitiveArray::from_iter([0u8, 10, 200]), &mut ctx);
782        assert!(matches!(p.validity(), Ok(Validity::NonNullable)));
783    }
784
785    #[test]
786    fn cast_u32_f32() {
787        let mut ctx = SESSION.create_execution_ctx();
788        let arr = buffer![0u32, 10, 200].into_array();
789        let u8arr = arr.cast(PType::F32.into()).unwrap();
790        assert_arrays_eq!(
791            u8arr,
792            PrimitiveArray::from_iter([0.0f32, 10., 200.]),
793            &mut ctx
794        );
795    }
796
797    #[test]
798    fn cast_integer_to_decimal_rescales() -> VortexResult<()> {
799        let mut ctx = SESSION.create_execution_ctx();
800        let decimal_dtype = DecimalDType::new(5, 2);
801        let casted = PrimitiveArray::from_iter([42i32, -7])
802            .into_array()
803            .cast(DType::Decimal(decimal_dtype, Nullability::NonNullable))?
804            .execute::<DecimalArray>(&mut ctx)?;
805
806        assert_eq!(
807            casted.dtype(),
808            &DType::Decimal(decimal_dtype, Nullability::NonNullable)
809        );
810        assert_eq!(casted.values_type(), DecimalType::I32);
811        assert_eq!(casted.buffer::<i32>().as_ref(), &[4_200, -700]);
812        Ok(())
813    }
814
815    #[test]
816    fn cast_same_width_signed_integer_to_decimal_reuses_buffer() -> VortexResult<()> {
817        let mut ctx = SESSION.create_execution_ctx();
818        let source = PrimitiveArray::from_iter([42i32, -7]);
819        let source_ptr = source.as_slice::<i32>().as_ptr();
820        let casted = source
821            .into_array()
822            .cast(DType::Decimal(
823                DecimalDType::new(9, 0),
824                Nullability::NonNullable,
825            ))?
826            .execute::<DecimalArray>(&mut ctx)?;
827
828        assert_eq!(casted.buffer::<i32>().as_ptr(), source_ptr);
829        assert_eq!(casted.buffer::<i32>().as_ref(), &[42, -7]);
830        assert_arrays_eq!(
831            &casted,
832            DecimalArray::new(
833                buffer![42i32, -7],
834                DecimalDType::new(9, 0),
835                Validity::NonNullable
836            ),
837            &mut ctx
838        );
839        Ok(())
840    }
841
842    #[test]
843    fn cast_same_width_signed_integer_to_decimal_reuses_buffer_with_cached_bounds()
844    -> VortexResult<()> {
845        let mut ctx = SESSION.create_execution_ctx();
846        let source = PrimitiveArray::from_iter([42i32, -7]);
847        let source_ptr = source.as_slice::<i32>().as_ptr();
848        let source = source.into_array();
849        source
850            .statistics()
851            .compute_all(&[Stat::Min, Stat::Max], &mut ctx)?;
852        let casted = source
853            .cast(DType::Decimal(
854                DecimalDType::new(9, 0),
855                Nullability::NonNullable,
856            ))?
857            .execute::<DecimalArray>(&mut ctx)?;
858
859        assert_eq!(casted.buffer::<i32>().as_ptr(), source_ptr);
860        assert_arrays_eq!(
861            &casted,
862            DecimalArray::new(
863                buffer![42i32, -7],
864                DecimalDType::new(9, 0),
865                Validity::NonNullable
866            ),
867            &mut ctx
868        );
869        Ok(())
870    }
871
872    #[test]
873    fn cast_same_width_signed_integer_to_decimal_ignores_out_of_range_nulls() -> VortexResult<()> {
874        let mut ctx = SESSION.create_execution_ctx();
875        let source = PrimitiveArray::new(buffer![i32::MAX, 42], Validity::from_iter([false, true]));
876        let source_ptr = source.as_slice::<i32>().as_ptr();
877        let casted = source
878            .into_array()
879            .cast(DType::Decimal(
880                DecimalDType::new(9, 0),
881                Nullability::Nullable,
882            ))?
883            .execute::<DecimalArray>(&mut ctx)?;
884
885        assert_eq!(casted.buffer::<i32>().as_ptr(), source_ptr);
886        assert_eq!(
887            casted.validity()?.execute_mask(casted.len(), &mut ctx)?,
888            Mask::from(BitBuffer::from(vec![false, true]))
889        );
890        assert_arrays_eq!(
891            &casted,
892            DecimalArray::from_option_iter([None, Some(42i32)], DecimalDType::new(9, 0)),
893            &mut ctx
894        );
895        Ok(())
896    }
897
898    #[test]
899    fn cast_same_width_signed_integer_to_decimal_checks_precision() -> VortexResult<()> {
900        let mut ctx = SESSION.create_execution_ctx();
901        let casted = PrimitiveArray::from_iter([i32::MAX])
902            .into_array()
903            .cast(DType::Decimal(
904                DecimalDType::new(9, 0),
905                Nullability::NonNullable,
906            ))?;
907
908        let error = casted.execute::<DecimalArray>(&mut ctx).unwrap_err();
909        assert!(error.to_string().contains("does not fit in precision"));
910        Ok(())
911    }
912
913    #[test]
914    fn cast_u64_to_decimal() -> VortexResult<()> {
915        let mut ctx = SESSION.create_execution_ctx();
916        let decimal_dtype = DecimalDType::new(20, 0);
917        let casted = PrimitiveArray::from_iter([u64::MAX])
918            .into_array()
919            .cast(DType::Decimal(decimal_dtype, Nullability::NonNullable))?
920            .execute::<DecimalArray>(&mut ctx)?;
921
922        assert_eq!(
923            casted.dtype(),
924            &DType::Decimal(decimal_dtype, Nullability::NonNullable)
925        );
926        assert_eq!(casted.values_type(), DecimalType::I128);
927        assert_eq!(casted.buffer::<i128>().as_ref(), &[i128::from(u64::MAX)]);
928        Ok(())
929    }
930
931    #[test]
932    fn cast_integer_to_decimal_reports_scale_up_overflow() -> VortexResult<()> {
933        let mut ctx = SESSION.create_execution_ctx();
934        let dtype = DType::Decimal(DecimalDType::new(38, 20), Nullability::NonNullable);
935        let casted = PrimitiveArray::from_iter([u64::MAX])
936            .into_array()
937            .cast(dtype)?;
938        let actual = casted.execute::<DecimalArray>(&mut ctx).unwrap_err();
939
940        assert!(
941            actual
942                .to_string()
943                .contains("does not fit in precision of decimal(38,20)")
944        );
945        Ok(())
946    }
947
948    #[test]
949    fn cast_u8_to_negative_scale_decimal() -> VortexResult<()> {
950        let mut ctx = SESSION.create_execution_ctx();
951        let decimal_dtype = DecimalDType::new(2, -2);
952        let casted = PrimitiveArray::from_iter([200u8])
953            .into_array()
954            .cast(DType::Decimal(decimal_dtype, Nullability::NonNullable))?
955            .execute::<DecimalArray>(&mut ctx)?;
956
957        assert_eq!(
958            casted.dtype(),
959            &DType::Decimal(decimal_dtype, Nullability::NonNullable)
960        );
961        assert_eq!(casted.values_type(), DecimalType::I8);
962        assert_eq!(casted.buffer::<i8>().as_ref(), &[2]);
963        Ok(())
964    }
965
966    #[test]
967    fn cast_u64_to_negative_scale_decimal() -> VortexResult<()> {
968        let mut ctx = SESSION.create_execution_ctx();
969        let decimal_dtype = DecimalDType::new(1, -19);
970        let casted = PrimitiveArray::from_iter([10_000_000_000_000_000_000u64])
971            .into_array()
972            .cast(DType::Decimal(decimal_dtype, Nullability::NonNullable))?
973            .execute::<DecimalArray>(&mut ctx)?;
974
975        assert_eq!(
976            casted.dtype(),
977            &DType::Decimal(decimal_dtype, Nullability::NonNullable)
978        );
979        assert_eq!(casted.values_type(), DecimalType::I8);
980        assert_eq!(casted.buffer::<i8>().as_ref(), &[1]);
981        Ok(())
982    }
983
984    #[test]
985    fn cast_integer_to_i256_decimal() -> VortexResult<()> {
986        let mut ctx = SESSION.create_execution_ctx();
987        let decimal_dtype = DecimalDType::new(39, 2);
988        let casted = PrimitiveArray::from_iter([42i64])
989            .into_array()
990            .cast(DType::Decimal(decimal_dtype, Nullability::NonNullable))?
991            .execute::<DecimalArray>(&mut ctx)?;
992
993        assert_eq!(
994            casted.dtype(),
995            &DType::Decimal(decimal_dtype, Nullability::NonNullable)
996        );
997        assert_eq!(casted.values_type(), DecimalType::I256);
998        assert_eq!(casted.buffer::<i256>().as_ref(), &[i256::from_i128(4_200)]);
999        Ok(())
1000    }
1001
1002    #[test]
1003    fn cast_all_null_integer_to_decimal() -> VortexResult<()> {
1004        let mut ctx = SESSION.create_execution_ctx();
1005        let decimal_dtype = DecimalDType::new(39, 2);
1006        let casted = PrimitiveArray::new(buffer![i64::MAX, i64::MIN], Validity::AllInvalid)
1007            .into_array()
1008            .cast(DType::Decimal(decimal_dtype, Nullability::Nullable))?
1009            .execute::<DecimalArray>(&mut ctx)?;
1010
1011        assert!(matches!(casted.validity(), Ok(Validity::AllInvalid)));
1012        assert_arrays_eq!(
1013            &casted,
1014            DecimalArray::from_option_iter([None::<i256>; 2], decimal_dtype),
1015            &mut ctx
1016        );
1017        Ok(())
1018    }
1019
1020    #[test]
1021    fn cast_integer_to_negative_scale_decimal_requires_exact_rescale() -> VortexResult<()> {
1022        let mut ctx = SESSION.create_execution_ctx();
1023        let decimal_dtype = DecimalDType::new(3, -2);
1024        let casted = PrimitiveArray::from_iter([1_200i32, -500])
1025            .into_array()
1026            .cast(DType::Decimal(decimal_dtype, Nullability::NonNullable))?
1027            .execute::<DecimalArray>(&mut ctx)?;
1028
1029        assert_eq!(
1030            casted.dtype(),
1031            &DType::Decimal(decimal_dtype, Nullability::NonNullable)
1032        );
1033        assert_eq!(casted.buffer::<i16>().as_ref(), &[12, -5]);
1034
1035        let error = PrimitiveArray::from_iter([42i32])
1036            .into_array()
1037            .cast(DType::Decimal(decimal_dtype, Nullability::NonNullable))?
1038            .execute::<DecimalArray>(&mut ctx)
1039            .unwrap_err();
1040        assert!(error.to_string().contains("would lose precision"));
1041        Ok(())
1042    }
1043
1044    #[rstest]
1045    fn cast_integer_to_negative_scale_decimal_every_width(
1046        #[values(
1047            PType::U8, PType::U16, PType::U32, PType::U64, PType::I8, PType::I16, PType::I32,
1048            PType::I64
1049        )]
1050        ptype: PType,
1051        #[values(2, 4, 9, 18, 38, 76)] precision: u8,
1052    ) -> VortexResult<()> {
1053        let mut ctx = SESSION.create_execution_ctx();
1054        let decimal_dtype = DecimalDType::new(precision, -1);
1055        let casted = PrimitiveArray::from_iter([100u8, 30])
1056            .into_array()
1057            .cast(DType::Primitive(ptype, Nullability::NonNullable))?
1058            .cast(DType::Decimal(decimal_dtype, Nullability::NonNullable))?
1059            .execute::<DecimalArray>(&mut ctx)?;
1060
1061        assert_eq!(casted.values_type(), decimal_dtype.canonical_values_type());
1062        match_each_decimal_value_type!(casted.values_type(), |T| {
1063            let expected = [10i8, 3].map(<T as crate::dtype::BigCast>::from);
1064            let actual = casted
1065                .buffer::<T>()
1066                .iter()
1067                .map(|&value| Some(value))
1068                .collect::<Vec<_>>();
1069            assert_eq!(actual, expected);
1070        });
1071        Ok(())
1072    }
1073
1074    #[test]
1075    fn cast_zero_to_large_negative_scale_decimal() -> VortexResult<()> {
1076        let mut ctx = SESSION.create_execution_ctx();
1077        let decimal_dtype = DecimalDType::new(3, -128);
1078        let casted = PrimitiveArray::from_iter([0i32])
1079            .into_array()
1080            .cast(DType::Decimal(decimal_dtype, Nullability::NonNullable))?
1081            .execute::<DecimalArray>(&mut ctx)?;
1082
1083        assert_eq!(
1084            casted.dtype(),
1085            &DType::Decimal(decimal_dtype, Nullability::NonNullable)
1086        );
1087        assert_eq!(casted.buffer::<i16>().as_ref(), &[0]);
1088        Ok(())
1089    }
1090
1091    #[test]
1092    fn cast_integer_to_decimal_ignores_out_of_range_null_lanes() -> VortexResult<()> {
1093        let mut ctx = SESSION.create_execution_ctx();
1094        let decimal_dtype = DecimalDType::new(3, 1);
1095        let casted = PrimitiveArray::new(buffer![999i32, 42], Validity::from_iter([false, true]))
1096            .into_array()
1097            .cast(DType::Decimal(decimal_dtype, Nullability::Nullable))?
1098            .execute::<DecimalArray>(&mut ctx)?;
1099
1100        assert_eq!(
1101            casted.dtype(),
1102            &DType::Decimal(decimal_dtype, Nullability::Nullable)
1103        );
1104        assert_eq!(casted.buffer::<i16>().as_ref()[1], 420);
1105        assert_eq!(
1106            casted.validity()?.execute_mask(casted.len(), &mut ctx)?,
1107            Mask::from(BitBuffer::from(vec![false, true]))
1108        );
1109        Ok(())
1110    }
1111
1112    #[test]
1113    fn cast_integer_to_decimal_checks_precision() -> VortexResult<()> {
1114        let mut ctx = SESSION.create_execution_ctx();
1115        let casted = PrimitiveArray::from_iter([100i32])
1116            .into_array()
1117            .cast(DType::Decimal(
1118                DecimalDType::new(2, 1),
1119                Nullability::NonNullable,
1120            ))?;
1121
1122        let error = casted.execute::<DecimalArray>(&mut ctx).unwrap_err();
1123        assert!(error.to_string().contains("does not fit in precision"));
1124        Ok(())
1125    }
1126
1127    #[test]
1128    fn cast_floating_primitive_to_decimal_fails() -> VortexResult<()> {
1129        let mut ctx = SESSION.create_execution_ctx();
1130        let casted = PrimitiveArray::from_iter([1.0f64])
1131            .into_array()
1132            .cast(DType::Decimal(
1133                DecimalDType::new(3, 1),
1134                Nullability::NonNullable,
1135            ))?;
1136
1137        let error = casted.execute::<DecimalArray>(&mut ctx).unwrap_err();
1138        assert!(
1139            error
1140                .to_string()
1141                .contains("Cannot cast floating primitive f64 to decimal decimal(3,1)")
1142        );
1143        Ok(())
1144    }
1145
1146    #[test]
1147    fn cast_i32_u32() {
1148        let arr = buffer![-1i32].into_array();
1149        let error = arr
1150            .cast(PType::U32.into())
1151            .and_then(|a| {
1152                a.execute::<Canonical>(&mut SESSION.create_execution_ctx())
1153                    .map(|c| c.into_array())
1154            })
1155            .unwrap_err();
1156        assert!(error.to_string().contains("values exceed target range"));
1157    }
1158
1159    #[test]
1160    fn cast_array_with_nulls_to_nonnullable() {
1161        let arr = PrimitiveArray::from_option_iter([Some(-1i32), None, Some(10)]);
1162        let err = arr
1163            .into_array()
1164            .cast(PType::I32.into())
1165            .and_then(|a| {
1166                a.execute::<Canonical>(&mut SESSION.create_execution_ctx())
1167                    .map(|c| c.into_array())
1168            })
1169            .unwrap_err();
1170
1171        assert!(
1172            err.to_string()
1173                .contains("Cannot cast array with invalid values to non-nullable type.")
1174        );
1175    }
1176
1177    #[test]
1178    fn cast_with_invalid_nulls() {
1179        let mut ctx = SESSION.create_execution_ctx();
1180        let arr = PrimitiveArray::new(
1181            buffer![-1i32, 0, 10],
1182            Validity::from_iter([false, true, true]),
1183        );
1184        let p = arr
1185            .into_array()
1186            .cast(DType::Primitive(PType::U32, Nullability::Nullable))
1187            .unwrap();
1188        assert_arrays_eq!(
1189            p,
1190            PrimitiveArray::from_option_iter([None, Some(0u32), Some(10)]),
1191            &mut ctx
1192        );
1193        assert_eq!(
1194            p.validity()
1195                .unwrap()
1196                .execute_mask(p.len(), &mut SESSION.create_execution_ctx())
1197                .unwrap(),
1198            Mask::from(BitBuffer::from(vec![false, true, true]))
1199        );
1200    }
1201
1202    /// Same-width integer cast where all values fit: should reinterpret the
1203    /// buffer without allocation (pointer identity).
1204    #[test]
1205    fn cast_same_width_int_reinterprets_buffer() -> VortexResult<()> {
1206        let mut ctx = SESSION.create_execution_ctx();
1207        let src = PrimitiveArray::from_iter([0u32, 10, 100]);
1208        let src_ptr = src.as_slice::<u32>().as_ptr();
1209
1210        let dst = src
1211            .into_array()
1212            .cast(PType::I32.into())?
1213            .execute::<PrimitiveArray>(&mut ctx)?;
1214        let dst_ptr = dst.as_slice::<i32>().as_ptr();
1215
1216        // Zero-copy: the data pointer should be identical.
1217        assert_eq!(src_ptr as usize, dst_ptr as usize);
1218        assert_arrays_eq!(dst, PrimitiveArray::from_iter([0i32, 10, 100]), &mut ctx);
1219        Ok(())
1220    }
1221
1222    /// Same-width integer cast where values don't fit: should fall through
1223    /// to the allocating path and produce an error.
1224    #[test]
1225    fn cast_same_width_int_out_of_range_errors() {
1226        let arr = buffer![u32::MAX].into_array();
1227        let err = arr
1228            .cast(PType::I32.into())
1229            .and_then(|a| {
1230                a.execute::<Canonical>(&mut SESSION.create_execution_ctx())
1231                    .map(|c| c.into_array())
1232            })
1233            .unwrap_err();
1234        assert!(
1235            err.to_string().contains("values exceed target range"),
1236            "{err}"
1237        );
1238    }
1239
1240    /// All-null array cast between same-width types should succeed without
1241    /// touching the buffer contents.
1242    #[test]
1243    fn cast_same_width_all_null() -> VortexResult<()> {
1244        let mut ctx = SESSION.create_execution_ctx();
1245        let arr = PrimitiveArray::new(buffer![0xFFu8, 0xFF], Validity::AllInvalid);
1246        let casted = arr
1247            .into_array()
1248            .cast(DType::Primitive(PType::I8, Nullability::Nullable))?
1249            .execute::<PrimitiveArray>(&mut ctx)?;
1250        assert_arrays_eq!(
1251            &casted,
1252            PrimitiveArray::from_option_iter([None::<i8>; 2]),
1253            &mut ctx
1254        );
1255        assert!(matches!(casted.validity(), Ok(Validity::AllInvalid)));
1256        Ok(())
1257    }
1258
1259    /// Same-width integer cast with nullable values: out-of-range nulls should
1260    /// not prevent the cast from succeeding.
1261    #[test]
1262    fn cast_same_width_int_nullable_with_out_of_range_nulls() -> VortexResult<()> {
1263        let mut ctx = SESSION.create_execution_ctx();
1264        // The null position holds u32::MAX which doesn't fit in i32, but it's
1265        // masked as invalid so the cast should still succeed via reinterpret.
1266        let arr = PrimitiveArray::new(
1267            buffer![u32::MAX, 0u32, 42u32],
1268            Validity::from_iter([false, true, true]),
1269        );
1270        let casted = arr
1271            .into_array()
1272            .cast(DType::Primitive(PType::I32, Nullability::Nullable))?;
1273        assert_arrays_eq!(
1274            casted,
1275            PrimitiveArray::from_option_iter([None, Some(0i32), Some(42)]),
1276            &mut ctx
1277        );
1278        Ok(())
1279    }
1280
1281    #[test]
1282    fn cast_u32_to_u8_with_out_of_range_nulls() -> VortexResult<()> {
1283        let mut ctx = SESSION.create_execution_ctx();
1284        let arr = PrimitiveArray::new(
1285            buffer![1000u32, 10u32, 42u32],
1286            Validity::from_iter([false, true, true]),
1287        );
1288        let casted = arr
1289            .into_array()
1290            .cast(DType::Primitive(PType::U8, Nullability::Nullable))?;
1291        assert_arrays_eq!(
1292            casted,
1293            PrimitiveArray::from_option_iter([None, Some(10u8), Some(42)]),
1294            &mut ctx
1295        );
1296        Ok(())
1297    }
1298
1299    #[rstest]
1300    #[case(buffer![0u8, 1, 2, 3, 255].into_array())]
1301    #[case(buffer![0u16, 100, 1000, 65535].into_array())]
1302    #[case(buffer![0u32, 100, 1000, 1000000].into_array())]
1303    #[case(buffer![0u64, 100, 1000, 1000000000].into_array())]
1304    #[case(buffer![-128i8, -1, 0, 1, 127].into_array())]
1305    #[case(buffer![-1000i16, -1, 0, 1, 1000].into_array())]
1306    #[case(buffer![-1000000i32, -1, 0, 1, 1000000].into_array())]
1307    #[case(buffer![-1000000000i64, -1, 0, 1, 1000000000].into_array())]
1308    #[case(buffer![0.0f32, 1.5, -2.5, 100.0, 1e6].into_array())]
1309    #[case(buffer![f32::NAN, f32::INFINITY, f32::NEG_INFINITY, 0.0f32].into_array())]
1310    #[case(buffer![0.0f64, 1.5, -2.5, 100.0, 1e12].into_array())]
1311    #[case(buffer![f64::NAN, f64::INFINITY, f64::NEG_INFINITY, 0.0f64].into_array())]
1312    #[case(PrimitiveArray::from_option_iter([Some(1u8), None, Some(255), Some(0), None]).into_array())]
1313    #[case(PrimitiveArray::from_option_iter([Some(1i32), None, Some(-100), Some(0), None]).into_array())]
1314    #[case(buffer![42u32].into_array())]
1315    fn test_cast_primitive_conformance(#[case] array: ArrayRef) {
1316        test_cast_conformance(&array, &mut SESSION.create_execution_ctx());
1317    }
1318}