Skip to main content

arrow_cast/cast/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Cast kernels to convert [`ArrayRef`]  between supported datatypes.
19//!
20//! See [`cast_with_options`] for more information on specific conversions.
21//!
22//! Example:
23//!
24//! ```
25//! # use arrow_array::*;
26//! # use arrow_cast::cast;
27//! # use arrow_schema::DataType;
28//! # use std::sync::Arc;
29//! # use arrow_array::types::Float64Type;
30//! # use arrow_array::cast::AsArray;
31//! // int32 to float64
32//! let a = Int32Array::from(vec![5, 6, 7]);
33//! let b = cast(&a, &DataType::Float64).unwrap();
34//! let c = b.as_primitive::<Float64Type>();
35//! assert_eq!(5.0, c.value(0));
36//! assert_eq!(6.0, c.value(1));
37//! assert_eq!(7.0, c.value(2));
38//! ```
39
40mod binary;
41mod decimal;
42mod dictionary;
43mod list;
44mod map;
45mod run_array;
46mod string;
47mod structs;
48mod temporal;
49mod union;
50
51use crate::cast::binary::*;
52use crate::cast::decimal::*;
53use crate::cast::dictionary::*;
54use crate::cast::list::*;
55use crate::cast::map::*;
56use crate::cast::run_array::*;
57use crate::cast::string::*;
58use crate::cast::structs::*;
59use crate::cast::temporal::*;
60pub use crate::cast::union::*;
61
62use chrono::{TimeZone, Utc};
63use std::cmp::Ordering;
64use std::sync::Arc;
65
66use crate::display::{ArrayFormatter, FormatOptions};
67use crate::parse::{
68    Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
69    string_to_datetime,
70};
71use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
72use arrow_buffer::{ArrowNativeType, OffsetBuffer, i256};
73use arrow_data::ArrayData;
74use arrow_data::transform::MutableArrayData;
75use arrow_schema::*;
76use arrow_select::take::take;
77use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
78
79#[expect(deprecated)]
80pub use decimal::parse_string_to_decimal_native;
81pub use decimal::{DecimalCast, rescale_decimal, single_float_to_decimal};
82pub use string::cast_single_string_to_boolean_default;
83
84/// Integers below 2^53 convert to an `f64` exactly.
85const F64_EXACT_INT_LIMIT: f64 = 9_007_199_254_740_992.0;
86/// Integers below 2^24 convert to an `f32` exactly.
87const F32_EXACT_INT_LIMIT: f64 = 16_777_216.0;
88
89/// Lossy conversion from decimal to float.
90///
91/// Returns the `f64` nearest to the decimal's exact value, rounding once, for any
92/// scale that fits in an `i8` -- as every Arrow decimal's does.
93///
94/// Conversion is lossy and follows standard floating point semantics. Values
95/// that exceed the representable range become `INFINITY` or `-INFINITY` without
96/// returning an error.
97#[inline(always)]
98pub fn single_decimal_to_float_lossy<D, F>(f: &F, x: D::Native, scale: i32) -> f64
99where
100    D: DecimalType,
101    F: Fn(D::Native) -> f64,
102{
103    let unscaled = f(x);
104    // Both operands are exact in this range, so this rounds once. A negative scale
105    // multiplies, since `10^-scale` is the exact power of ten there.
106    if (-22..=22).contains(&scale) && unscaled.abs() < F64_EXACT_INT_LIMIT {
107        return if scale >= 0 {
108            unscaled / 10_f64.powi(scale)
109        } else {
110            unscaled * 10_f64.powi(-scale)
111        };
112    }
113    match i8::try_from(scale) {
114        Ok(scale) => decimal_to_f64_rounded_once::<D>(x, scale, unscaled),
115        Err(_) => unscaled / 10_f64.powi(scale),
116    }
117}
118
119/// Rounds once for the values the arithmetic cannot convert exactly, by parsing
120/// the decimal's own text.
121#[cold]
122#[inline(never)]
123fn decimal_to_f64_rounded_once<D: DecimalType>(x: D::Native, scale: i8, unscaled: f64) -> f64 {
124    // `format_decimal` always writes a decimal literal, so the parse cannot fail.
125    // The fallback is there to keep that from being a panic, not because it runs.
126    D::format_decimal(x, u8::MAX, scale)
127        .parse::<f64>()
128        .unwrap_or_else(|_| unscaled / 10_f64.powi(scale.into()))
129}
130
131/// As [`decimal_to_f64_rounded_once`], but narrowing to `f32` in one step.
132/// Rounding to `f64` first and then to `f32` rounds twice: a decimal just above an
133/// `f32` midpoint can collapse onto that midpoint in `f64`, and round-half-even
134/// then sends it the wrong way.
135#[cold]
136#[inline(never)]
137fn decimal_to_f32_rounded_once<D: DecimalType>(x: D::Native, scale: i8, unscaled: f64) -> f32 {
138    // Unreachable, as in [`decimal_to_f64_rounded_once`].
139    D::format_decimal(x, u8::MAX, scale)
140        .parse::<f32>()
141        .unwrap_or_else(|_| unscaled as f32 / 10_f32.powi(scale.into()))
142}
143
144/// Casts a decimal array to `Float64`, rounding each value once.
145fn cast_decimal_to_f64<D, F>(
146    array: &dyn Array,
147    as_float: &F,
148    scale: i8,
149) -> Result<ArrayRef, ArrowError>
150where
151    D: DecimalType + ArrowPrimitiveType,
152    F: Fn(D::Native) -> f64,
153{
154    let array = array.as_primitive::<D>();
155    // No power of ten outside this range is exactly representable.
156    if !(-22..=22).contains(&scale) {
157        let values = array
158            .unary::<_, Float64Type>(|x| decimal_to_f64_rounded_once::<D>(x, scale, as_float(x)));
159        return Ok(Arc::new(values));
160    }
161    let pow = 10_f64.powi(scale.unsigned_abs().into());
162    let values = if scale >= 0 {
163        array.unary::<_, Float64Type>(|x| {
164            let unscaled = as_float(x);
165            if unscaled.abs() < F64_EXACT_INT_LIMIT {
166                unscaled / pow
167            } else {
168                decimal_to_f64_rounded_once::<D>(x, scale, unscaled)
169            }
170        })
171    } else {
172        array.unary::<_, Float64Type>(|x| {
173            let unscaled = as_float(x);
174            if unscaled.abs() < F64_EXACT_INT_LIMIT {
175                unscaled * pow
176            } else {
177                decimal_to_f64_rounded_once::<D>(x, scale, unscaled)
178            }
179        })
180    };
181    Ok(Arc::new(values))
182}
183
184/// Casts a decimal array to `Float32`, rounding each value once. Same shape as
185/// [`cast_decimal_to_f64`], with the smaller bounds an `f32` allows: integers are
186/// exact below 2^24, and `10^k` only up to `k = 10`.
187fn cast_decimal_to_f32<D, F>(
188    array: &dyn Array,
189    as_float: &F,
190    scale: i8,
191) -> Result<ArrayRef, ArrowError>
192where
193    D: DecimalType + ArrowPrimitiveType,
194    F: Fn(D::Native) -> f64,
195{
196    let array = array.as_primitive::<D>();
197    // No power of ten outside this range is exactly representable.
198    if !(-10..=10).contains(&scale) {
199        let values = array
200            .unary::<_, Float32Type>(|x| decimal_to_f32_rounded_once::<D>(x, scale, as_float(x)));
201        return Ok(Arc::new(values));
202    }
203    let pow = 10_f32.powi(scale.unsigned_abs().into());
204    let values = if scale >= 0 {
205        array.unary::<_, Float32Type>(|x| {
206            let unscaled = as_float(x);
207            if unscaled.abs() < F32_EXACT_INT_LIMIT {
208                unscaled as f32 / pow
209            } else {
210                decimal_to_f32_rounded_once::<D>(x, scale, unscaled)
211            }
212        })
213    } else {
214        array.unary::<_, Float32Type>(|x| {
215            let unscaled = as_float(x);
216            if unscaled.abs() < F32_EXACT_INT_LIMIT {
217                unscaled as f32 * pow
218            } else {
219                decimal_to_f32_rounded_once::<D>(x, scale, unscaled)
220            }
221        })
222    };
223    Ok(Arc::new(values))
224}
225
226/// CastOptions provides a way to override the default cast behaviors
227#[derive(Debug, Clone, PartialEq, Eq, Hash)]
228pub struct CastOptions<'a> {
229    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
230    pub safe: bool,
231    /// Formatting options when casting from temporal types to string
232    pub format_options: FormatOptions<'a>,
233}
234
235impl Default for CastOptions<'_> {
236    fn default() -> Self {
237        Self {
238            safe: true,
239            format_options: FormatOptions::default(),
240        }
241    }
242}
243
244/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
245///
246/// See [`cast_with_options`] for more information
247pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
248    use self::DataType::*;
249    use self::IntervalUnit::*;
250    use self::TimeUnit::*;
251    if from_type == to_type {
252        return true;
253    }
254
255    match (from_type, to_type) {
256        (Null, _) => true,
257        // Dictionary/List conditions should be put in front of others
258        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
259            can_cast_types(from_value_type, to_value_type)
260        }
261        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
262        (Union(fields, _), _) => union::resolve_child_array(fields, to_type).is_some(),
263        (_, Union(_, _)) => false,
264        (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
265        (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
266        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
267        (
268            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
269            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
270        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
271        (
272            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
273            Utf8 | LargeUtf8 | Utf8View,
274        ) => can_cast_types(list_from.data_type(), to_type),
275        (
276            FixedSizeList(list_from, _),
277            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
278        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
279        (
280            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
281            FixedSizeList(list_to, _),
282        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
283        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
284            can_cast_types(inner.data_type(), inner_to.data_type())
285        }
286        (_, List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to)) => {
287            can_cast_types(from_type, list_to.data_type())
288        }
289        (_, FixedSizeList(list_to, size)) if *size == 1 => {
290            can_cast_types(from_type, list_to.data_type())
291        }
292        (FixedSizeList(list_from, size), _) if *size == 1 => {
293            can_cast_types(list_from.data_type(), to_type)
294        }
295        (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
296            if ordered_from == ordered_to =>
297        {
298            match (
299                key_field(from_entries),
300                key_field(to_entries),
301                value_field(from_entries),
302                value_field(to_entries),
303            ) {
304                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
305                    can_cast_types(from_key.data_type(), to_key.data_type())
306                        && can_cast_types(from_value.data_type(), to_value.data_type())
307                }
308                _ => false,
309            }
310        }
311        // cast one decimal type to another decimal type
312        (
313            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
314            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
315        ) => true,
316        // unsigned integer to decimal
317        (
318            UInt8 | UInt16 | UInt32 | UInt64,
319            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
320        ) => true,
321        // signed numeric to decimal
322        (
323            Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
324            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
325        ) => true,
326        // decimal to unsigned numeric
327        (
328            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
329            UInt8 | UInt16 | UInt32 | UInt64,
330        ) => true,
331        // decimal to signed numeric
332        (
333            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
334            Null | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
335        ) => true,
336        // decimal to string
337        (
338            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
339            Utf8View | Utf8 | LargeUtf8,
340        ) => true,
341        // string to decimal
342        (
343            Utf8View | Utf8 | LargeUtf8,
344            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
345        ) => true,
346        (Struct(from_fields), Struct(to_fields)) => {
347            if from_fields.len() != to_fields.len() {
348                return false;
349            }
350
351            // fast path, all field names are in the same order and same number of fields
352            if from_fields
353                .iter()
354                .zip(to_fields.iter())
355                .all(|(f1, f2)| f1.name() == f2.name())
356            {
357                return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
358                    // Assume that nullability between two structs are compatible, if not,
359                    // cast kernel will return error.
360                    can_cast_types(f1.data_type(), f2.data_type())
361                });
362            }
363
364            // slow path, we match the fields by name
365            if to_fields.iter().all(|to_field| {
366                from_fields
367                    .iter()
368                    .find(|from_field| from_field.name() == to_field.name())
369                    .is_some_and(|from_field| {
370                        // Assume that nullability between two structs are compatible, if not,
371                        // cast kernel will return error.
372                        can_cast_types(from_field.data_type(), to_field.data_type())
373                    })
374            }) {
375                return true;
376            }
377
378            // if we couldn't match by name, we try to see if they can be matched by position
379            from_fields
380                .iter()
381                .zip(to_fields.iter())
382                .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
383        }
384        (Struct(_), _) => false,
385        (_, Struct(_)) => false,
386        (_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
387        (Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
388
389        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
390            true
391        }
392        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
393            true
394        }
395        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
396        (
397            Utf8 | LargeUtf8 | Utf8View,
398            Binary
399            | LargeBinary
400            | Utf8
401            | LargeUtf8
402            | Date32
403            | Date64
404            | Time32(Second | Millisecond)
405            | Time64(Microsecond | Nanosecond)
406            | Timestamp(Second | Millisecond | Microsecond | Nanosecond, _)
407            | Interval(_)
408            | BinaryView,
409        ) => true,
410        (Utf8 | LargeUtf8, Utf8View) => true,
411        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
412        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
413        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
414
415        (_, Binary | LargeBinary) => from_type.is_integer(),
416
417        // start numeric casts
418        (
419            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
420            | Float64,
421            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
422            | Float64,
423        ) => true,
424        // end numeric casts
425
426        // temporal casts
427        (Int32, Date32 | Date64 | Time32(_)) => true,
428        (Date32, Int32 | Int64) => true,
429        (Time32(_), Int32 | Int64) => true,
430        (Int64, Date64 | Date32 | Time64(_)) => true,
431        (Date64, Int64 | Int32) => true,
432        (Time64(_), Int64) => true,
433        (Date32 | Date64, Date32 | Date64) => true,
434        // time casts
435        (Time32(_), Time32(_)) => true,
436        (Time32(_), Time64(_)) => true,
437        (Time64(_), Time64(_)) => true,
438        (Time64(_), Time32(to_unit)) => {
439            matches!(to_unit, Second | Millisecond)
440        }
441        (Timestamp(_, _), _) if to_type.is_numeric() => true,
442        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
443        (Date64, Timestamp(_, _)) => true,
444        (Date32, Timestamp(_, _)) => true,
445        (
446            Timestamp(_, _),
447            Timestamp(_, _)
448            | Date32
449            | Date64
450            | Time32(Second | Millisecond)
451            | Time64(Microsecond | Nanosecond),
452        ) => true,
453        (_, Duration(_)) if from_type.is_numeric() => true,
454        (Duration(_), _) if to_type.is_numeric() => true,
455        (Duration(_), Duration(_)) => true,
456        // No `(Interval(_), Int64)` arm: `cast_with_options` implements no such cast.
457        (Int32, Interval(to_type)) => match to_type {
458            YearMonth => true,
459            DayTime => false,
460            MonthDayNano => false,
461        },
462        (Duration(_), Interval(MonthDayNano)) => true,
463        (Interval(MonthDayNano), Duration(_)) => true,
464        (Interval(YearMonth), Interval(MonthDayNano)) => true,
465        (Interval(DayTime), Interval(MonthDayNano)) => true,
466        (_, _) => false,
467    }
468}
469
470/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
471///
472/// See [`cast_with_options`] for more information
473pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
474    cast_with_options(array, to_type, &CastOptions::default())
475}
476
477/// Convert an integer to a decimal native value without wrapping.
478///
479/// `AsPrimitive` / `as` silently truncates when the source is wider than `M`
480/// (for example `5_000_000_000i64 as i32`). All integer sources fit in `i128`
481/// losslessly, which [`DecimalCast`] then converts to the decimal native type
482/// with a range check. For types that always fit (e.g. `i64` to `Decimal128`) this
483/// should get optimized to being equivalent to `i64 as i128`.
484fn integer_to_decimal_native<I, M>(value: I) -> Option<M>
485where
486    I: Into<i128>,
487    M: DecimalCast,
488{
489    M::from_decimal(value.into())
490}
491
492/// Returns true if every value of `array` has at most `digits` decimal digits.
493///
494/// If the source type cannot hold that many digits, every value fits;
495/// otherwise the data is scanned, including null slots, since `unary`
496/// converts those as well.
497fn integers_fit_digits<T>(array: &PrimitiveArray<T>, digits: u32) -> bool
498where
499    T: ArrowPrimitiveType,
500    T::Native: ArrowNativeTypeOp,
501{
502    let Ok(bound) = T::Native::usize_as(10).pow_checked(digits) else {
503        // The type's maximum is below the bound, and its minimum, one larger
504        // in magnitude, is a power of two that no power of ten can equal.
505        return true;
506    };
507    let hi = bound.sub_wrapping(T::Native::ONE);
508    // Unsigned types have no negative values to bound.
509    let lo = T::Native::ZERO.sub_checked(hi).unwrap_or(T::Native::ZERO);
510
511    // The scan works in chunks, so an array that does not fit is rejected after
512    // its first failing chunk. Each chunk is checked without branching, so it
513    // vectorizes.
514    array
515        .values()
516        .chunks(64)
517        .all(|chunk| chunk.iter().fold(true, |ok, &v| ok & (v >= lo) & (v <= hi)))
518}
519
520fn cast_integer_to_decimal<
521    T: ArrowPrimitiveType,
522    D: DecimalType + ArrowPrimitiveType<Native = M>,
523    M,
524>(
525    array: &PrimitiveArray<T>,
526    precision: u8,
527    scale: i8,
528    cast_options: &CastOptions,
529) -> Result<ArrayRef, ArrowError>
530where
531    <T as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + Into<i128>,
532    M: ArrowNativeTypeOp + DecimalCast,
533{
534    let overflow = |v: T::Native| {
535        ArrowError::CastError(format!(
536            "Cannot cast to {}({precision}, {scale}). Overflowing on {v:?}",
537            D::PREFIX,
538        ))
539    };
540
541    // An invalid precision or scale would let the bound below admit values the
542    // decimal native type cannot hold, so reject it before choosing a kernel.
543    validate_decimal_precision_and_scale::<D>(precision, scale)?;
544
545    // Scan for values that don't fit in the target type before choosing a
546    // kernel: in the common case that all values fit, the cast is infallible
547    // and we can use the `unary` kernel, which is very fast. Empirically, the
548    // cost of doing the out-of-range scan is small relative to the win from
549    // using a faster kernel.
550    //
551    // A value fits when it has at most `precision - scale` decimal digits: a
552    // nonnegative scale appends that many trailing zeros, and a negative scale
553    // removes digits first. Validation guarantees the difference is not
554    // negative.
555    let digits = (precision as i32 - scale as i32) as u32;
556    let all_fit = integers_fit_digits(array, digits);
557
558    let array = if scale < 0 {
559        // Compute the scale factor once in the source type. Scaling before the
560        // checked conversion permits values that only fit the decimal native
561        // type after scaling.
562        //
563        // scale_factor is always > 0, so div_wrapping(scale_factor) can never
564        // overflow/panic
565        let scale_factor = T::Native::usize_as(10)
566            .pow_checked(scale.unsigned_abs() as u32)
567            .ok();
568
569        match (scale_factor, cast_options.safe) {
570            (Some(scale_factor), _) if all_fit => array.unary::<_, D>(|v| {
571                integer_to_decimal_native::<_, M>(v.div_wrapping(scale_factor))
572                    .expect("value fits the decimal")
573            }),
574            (Some(scale_factor), true) => array.unary_opt::<_, D>(|v| {
575                let v = integer_to_decimal_native::<_, M>(v.div_wrapping(scale_factor))?;
576                (D::is_valid_decimal_precision(v, precision)).then_some(v)
577            }),
578            (Some(scale_factor), false) => array.try_unary::<_, D, _>(|v| {
579                let v = integer_to_decimal_native::<_, M>(v.div_wrapping(scale_factor))
580                    .ok_or_else(|| overflow(v))?;
581                D::validate_decimal_precision(v, precision, scale).map(|()| v)
582            })?,
583            // A scale factor that overflows the source type is larger than all
584            // source values, so integer division produces zero.
585            //
586            // For a well formed decimal scale, this path should never be reachable.
587            (None, _) => array.unary::<_, D>(|_| M::ZERO),
588        }
589    } else {
590        let scale_factor = M::usize_as(10)
591            .pow_checked(scale.unsigned_abs() as u32)
592            .map_err(|_| {
593                ArrowError::CastError(format!(
594                    "Cannot cast to {:?}({}, {}). The scale causes overflow.",
595                    D::PREFIX,
596                    precision,
597                    scale,
598                ))
599            })?;
600
601        if all_fit {
602            array.unary::<_, D>(|v| {
603                integer_to_decimal_native::<_, M>(v)
604                    .expect("value fits the decimal")
605                    .mul_wrapping(scale_factor)
606            })
607        } else if cast_options.safe {
608            array.unary_opt::<_, D>(|v| {
609                let v = integer_to_decimal_native::<_, M>(v)
610                    .and_then(|v| v.mul_checked(scale_factor).ok())?;
611                (D::is_valid_decimal_precision(v, precision)).then_some(v)
612            })
613        } else {
614            array.try_unary::<_, D, _>(|v| {
615                let v = integer_to_decimal_native::<_, M>(v)
616                    .ok_or_else(|| overflow(v))
617                    .and_then(|v| v.mul_checked(scale_factor))?;
618                D::validate_decimal_precision(v, precision, scale).map(|()| v)
619            })?
620        }
621    };
622
623    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
624}
625
626/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
627fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
628    array: &dyn Array,
629) -> Result<ArrayRef, ArrowError> {
630    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
631}
632
633/// Try to cast `array` to `to_type` if possible.
634///
635/// Returns a new Array with type `to_type` if possible.
636///
637/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
638///
639/// # Behavior
640/// * `Boolean` to `Utf8`, `LargeUtf8`, or `Utf8View`: `true` => `"true"`, `false` => `"false"`
641/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
642///   short variants are accepted, other strings return null or error
643/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
644///   in integer casts return null
645/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
646/// * `List` to `List`: the underlying data type is cast
647/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
648///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
649/// * Primitive to `List`: a list array with 1 value per slot is created
650/// * `Date32` and `Date64`: precision lost when going to higher interval
651/// * `Time32` and `Time64`: precision lost when going to higher interval
652/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
653/// * `Timestamp` to `Date32`, `Time32`, or `Time64`: timestamps without a timezone
654///   support the full `i64` range; timestamps with a timezone must lie within Chrono's
655///   date range. If safe is true, a timestamp outside that range or a `Date32` day
656///   count that does not fit in `i32` becomes NULL, otherwise an error is returned
657/// * Temporal to/from backing Primitive: zero-copy with data type change
658/// * `Float16/Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
659///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
660/// * `Decimal` to `Float16/Float32/Float64` is lossy and values outside the representable
661///   range become `INFINITY` or `-INFINITY` without error.
662///
663/// Unsupported Casts (check with `can_cast_types` before calling):
664/// * To or from `StructArray`
665/// * `List` to `Primitive`
666/// * `Interval` and `Duration`
667///
668/// # Durations and Intervals
669///
670/// Casting integer types directly to interval types such as
671/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
672/// is ambiguous. For example, the integer  could represent either nanoseconds
673/// or months.
674///
675/// To cast an integer type to an interval type, first convert to a Duration
676/// type, and then cast that to the desired interval type.
677///
678/// For example, to convert an `Int64` representing nanoseconds to an
679/// `IntervalMonthDayNano` you would first convert the `Int64` to a
680/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
681///
682/// # Timestamps and Timezones
683///
684/// Timestamps are stored with an optional timezone in Arrow.
685///
686/// ## Casting timestamps to a timestamp without timezone / UTC
687/// ```
688/// # use arrow_array::Int64Array;
689/// # use arrow_array::types::TimestampSecondType;
690/// # use arrow_cast::{cast, display};
691/// # use arrow_array::cast::AsArray;
692/// # use arrow_schema::{DataType, TimeUnit};
693/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
694/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
695/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
696/// let b = cast(&a, &data_type).unwrap();
697/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
698/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
699/// // use display to show them (note has no trailing Z)
700/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
701/// ```
702///
703/// ## Casting timestamps to a timestamp with timezone
704///
705/// Similarly to the previous example, if you cast numeric values to a timestamp
706/// with timezone, the cast kernel will not change the underlying values
707/// but display and other functions will interpret them as being in the provided timezone.
708///
709/// ```
710/// # use arrow_array::Int64Array;
711/// # use arrow_array::types::TimestampSecondType;
712/// # use arrow_cast::{cast, display};
713/// # use arrow_array::cast::AsArray;
714/// # use arrow_schema::{DataType, TimeUnit};
715/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
716/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
717/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
718/// let b = cast(&a, &data_type).unwrap();
719/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
720/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
721/// // displayed in the target timezone (note the offset -05:00)
722/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
723/// ```
724/// # Casting timestamps without timezone to timestamps with timezone
725///
726/// When casting from a timestamp without timezone to a timestamp with
727/// timezone, the cast kernel interprets the timestamp values as being in
728/// the destination timezone and then adjusts the underlying value to UTC as required
729///
730/// However, note that when casting from a timestamp with timezone BACK to a
731/// timestamp without timezone the cast kernel does not adjust the values.
732///
733/// Thus round trip casting a timestamp without timezone to a timestamp with
734/// timezone and back to a timestamp without timezone results in different
735/// values than the starting values.
736///
737/// ```
738/// # use arrow_array::Int64Array;
739/// # use arrow_array::types::{TimestampSecondType};
740/// # use arrow_cast::{cast, display};
741/// # use arrow_array::cast::AsArray;
742/// # use arrow_schema::{DataType, TimeUnit};
743/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
744/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
745/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
746/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
747/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
748/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
749/// // displayed without a timezone (note lack of offset or Z)
750/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
751///
752/// // Convert timestamps without a timezone to timestamps with a timezone
753/// let c = cast(&b, &data_type_tz).unwrap();
754/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
755/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
756/// // displayed with the target timezone offset (-05:00)
757/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
758///
759/// // Convert from timestamp with timezone back to timestamp without timezone
760/// let d = cast(&c, &data_type).unwrap();
761/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
762/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
763/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
764/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
765/// ```
766pub fn cast_with_options(
767    array: &dyn Array,
768    to_type: &DataType,
769    cast_options: &CastOptions,
770) -> Result<ArrayRef, ArrowError> {
771    use DataType::*;
772    let from_type = array.data_type();
773    // clone array if types are the same
774    if from_type == to_type {
775        return Ok(make_array(array.to_data()));
776    }
777    match (from_type, to_type) {
778        (Null, _) => Ok(new_null_array(to_type, array.len())),
779        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
780            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
781            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
782            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
783            _ => Err(ArrowError::CastError(format!(
784                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
785            ))),
786        },
787        (_, RunEndEncoded(index_type, value_type)) => {
788            let array_ref = make_array(array.to_data());
789            match index_type.data_type() {
790                Int16 => cast_to_run_end_encoded::<Int16Type>(
791                    &array_ref,
792                    value_type.data_type(),
793                    cast_options,
794                ),
795                Int32 => cast_to_run_end_encoded::<Int32Type>(
796                    &array_ref,
797                    value_type.data_type(),
798                    cast_options,
799                ),
800                Int64 => cast_to_run_end_encoded::<Int64Type>(
801                    &array_ref,
802                    value_type.data_type(),
803                    cast_options,
804                ),
805                _ => Err(ArrowError::CastError(format!(
806                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
807                ))),
808            }
809        }
810        (Union(_, _), _) => union_extract_by_type(
811            array.as_any().downcast_ref::<UnionArray>().unwrap(),
812            to_type,
813            cast_options,
814        ),
815        (_, Union(_, _)) => Err(ArrowError::CastError(format!(
816            "Casting from {from_type} to {to_type} not supported"
817        ))),
818        (Dictionary(index_type, _), _) => match **index_type {
819            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
820            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
821            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
822            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
823            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
824            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
825            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
826            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
827            _ => Err(ArrowError::CastError(format!(
828                "Casting from dictionary type {from_type} to {to_type} not supported",
829            ))),
830        },
831        (_, Dictionary(index_type, value_type)) => match **index_type {
832            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
833            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
834            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
835            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
836            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
837            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
838            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
839            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
840            _ => Err(ArrowError::CastError(format!(
841                "Casting from type {from_type} to dictionary type {to_type} not supported",
842            ))),
843        },
844        // Casting between lists of same types (cast inner values)
845        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
846        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
847        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
848            if size_from != size_to {
849                return Err(ArrowError::CastError(
850                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
851                ));
852            }
853            let array = array.as_fixed_size_list();
854            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
855            let nulls = array.nulls().cloned();
856            let len = array.len();
857            Ok(Arc::new(FixedSizeListArray::try_new_with_length(
858                list_to.clone(),
859                *size_from,
860                values,
861                nulls,
862                len,
863            )?))
864        }
865        (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
866        (LargeListView(_), LargeListView(to)) => {
867            cast_list_view_values::<i64>(array, to, cast_options)
868        }
869        // Casting between different types of lists
870        // List
871        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
872        (List(_), FixedSizeList(field, size)) => {
873            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
874        }
875        (List(_), ListView(list_to)) => {
876            cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
877        }
878        (List(_), LargeListView(list_to)) => {
879            cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
880        }
881        // LargeList
882        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
883        (LargeList(_), FixedSizeList(field, size)) => {
884            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
885        }
886        (LargeList(_), ListView(list_to)) => {
887            cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
888        }
889        (LargeList(_), LargeListView(list_to)) => {
890            cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
891        }
892        // ListView
893        (ListView(_), List(list_to)) => {
894            cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
895        }
896        (ListView(_), LargeList(list_to)) => {
897            cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
898        }
899        (ListView(_), LargeListView(list_to)) => {
900            cast_list_view::<i32, i64>(array, list_to, cast_options)
901        }
902        (ListView(_), FixedSizeList(field, size)) => {
903            cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
904        }
905        // LargeListView
906        (LargeListView(_), LargeList(list_to)) => {
907            cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
908        }
909        (LargeListView(_), List(list_to)) => {
910            cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
911        }
912        (LargeListView(_), ListView(list_to)) => {
913            cast_list_view::<i64, i32>(array, list_to, cast_options)
914        }
915        (LargeListView(_), FixedSizeList(field, size)) => {
916            cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
917        }
918        // FixedSizeList
919        (FixedSizeList(_, _), List(list_to)) => {
920            cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
921        }
922        (FixedSizeList(_, _), LargeList(list_to)) => {
923            cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
924        }
925        (FixedSizeList(_, _), ListView(list_to)) => {
926            cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
927        }
928        (FixedSizeList(_, _), LargeListView(list_to)) => {
929            cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
930        }
931        // List to/from other types
932        (FixedSizeList(_, size), _) if *size == 1 => {
933            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
934        }
935        // NOTE: we could support FSL to string here too but might be confusing
936        //       since behaviour for size 1 would be different (see arm above)
937        (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
938            Utf8 => value_to_string::<i32>(array, cast_options),
939            LargeUtf8 => value_to_string::<i64>(array, cast_options),
940            Utf8View => value_to_string_view(array, cast_options),
941            dt => Err(ArrowError::CastError(format!(
942                "Cannot cast LIST to non-list data type {dt}"
943            ))),
944        },
945        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
946        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
947        (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
948        (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
949        (_, FixedSizeList(to, size)) if *size == 1 => {
950            let values = cast_with_options(array, to.data_type(), cast_options)?;
951            let list = FixedSizeListArray::try_new(to.clone(), 1, values, None)?;
952            Ok(Arc::new(list))
953        }
954        // Map
955        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
956            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
957        }
958        // Decimal to decimal, same width
959        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
960            cast_decimal_to_decimal_same_type::<Decimal32Type>(
961                array.as_primitive(),
962                *p1,
963                *s1,
964                *p2,
965                *s2,
966                cast_options,
967            )
968        }
969        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
970            cast_decimal_to_decimal_same_type::<Decimal64Type>(
971                array.as_primitive(),
972                *p1,
973                *s1,
974                *p2,
975                *s2,
976                cast_options,
977            )
978        }
979        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
980            cast_decimal_to_decimal_same_type::<Decimal128Type>(
981                array.as_primitive(),
982                *p1,
983                *s1,
984                *p2,
985                *s2,
986                cast_options,
987            )
988        }
989        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
990            cast_decimal_to_decimal_same_type::<Decimal256Type>(
991                array.as_primitive(),
992                *p1,
993                *s1,
994                *p2,
995                *s2,
996                cast_options,
997            )
998        }
999        // Decimal to decimal, different width
1000        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
1001            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
1002                array.as_primitive(),
1003                *p1,
1004                *s1,
1005                *p2,
1006                *s2,
1007                cast_options,
1008            )
1009        }
1010        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
1011            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
1012                array.as_primitive(),
1013                *p1,
1014                *s1,
1015                *p2,
1016                *s2,
1017                cast_options,
1018            )
1019        }
1020        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1021            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1022                array.as_primitive(),
1023                *p1,
1024                *s1,
1025                *p2,
1026                *s2,
1027                cast_options,
1028            )
1029        }
1030        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1031            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1032                array.as_primitive(),
1033                *p1,
1034                *s1,
1035                *p2,
1036                *s2,
1037                cast_options,
1038            )
1039        }
1040        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1041            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1042                array.as_primitive(),
1043                *p1,
1044                *s1,
1045                *p2,
1046                *s2,
1047                cast_options,
1048            )
1049        }
1050        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1051            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1052                array.as_primitive(),
1053                *p1,
1054                *s1,
1055                *p2,
1056                *s2,
1057                cast_options,
1058            )
1059        }
1060        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1061            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1062                array.as_primitive(),
1063                *p1,
1064                *s1,
1065                *p2,
1066                *s2,
1067                cast_options,
1068            )
1069        }
1070        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1071            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1072                array.as_primitive(),
1073                *p1,
1074                *s1,
1075                *p2,
1076                *s2,
1077                cast_options,
1078            )
1079        }
1080        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1081            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1082                array.as_primitive(),
1083                *p1,
1084                *s1,
1085                *p2,
1086                *s2,
1087                cast_options,
1088            )
1089        }
1090        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1091            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1092                array.as_primitive(),
1093                *p1,
1094                *s1,
1095                *p2,
1096                *s2,
1097                cast_options,
1098            )
1099        }
1100        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1101            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1102                array.as_primitive(),
1103                *p1,
1104                *s1,
1105                *p2,
1106                *s2,
1107                cast_options,
1108            )
1109        }
1110        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1111            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1112                array.as_primitive(),
1113                *p1,
1114                *s1,
1115                *p2,
1116                *s2,
1117                cast_options,
1118            )
1119        }
1120        // Decimal to non-decimal
1121        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1122            cast_from_decimal::<Decimal32Type, _>(
1123                array,
1124                scale,
1125                from_type,
1126                to_type,
1127                |x: i32| x as f64,
1128                cast_options,
1129            )
1130        }
1131        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1132            cast_from_decimal::<Decimal64Type, _>(
1133                array,
1134                scale,
1135                from_type,
1136                to_type,
1137                |x: i64| x as f64,
1138                cast_options,
1139            )
1140        }
1141        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1142            cast_from_decimal::<Decimal128Type, _>(
1143                array,
1144                scale,
1145                from_type,
1146                to_type,
1147                |x: i128| x as f64,
1148                cast_options,
1149            )
1150        }
1151        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1152            cast_from_decimal::<Decimal256Type, _>(
1153                array,
1154                scale,
1155                from_type,
1156                to_type,
1157                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1158                cast_options,
1159            )
1160        }
1161        // Non-decimal to decimal
1162        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1163            cast_to_decimal::<Decimal32Type, _>(
1164                array,
1165                precision,
1166                scale,
1167                from_type,
1168                to_type,
1169                cast_options,
1170            )
1171        }
1172        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1173            cast_to_decimal::<Decimal64Type, _>(
1174                array,
1175                precision,
1176                scale,
1177                from_type,
1178                to_type,
1179                cast_options,
1180            )
1181        }
1182        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1183            cast_to_decimal::<Decimal128Type, _>(
1184                array,
1185                precision,
1186                scale,
1187                from_type,
1188                to_type,
1189                cast_options,
1190            )
1191        }
1192        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1193            cast_to_decimal::<Decimal256Type, _>(
1194                array,
1195                precision,
1196                scale,
1197                from_type,
1198                to_type,
1199                cast_options,
1200            )
1201        }
1202        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1203            array.as_struct(),
1204            from_fields.clone(),
1205            to_fields.clone(),
1206            cast_options,
1207        ),
1208        (Struct(_), _) => Err(ArrowError::CastError(format!(
1209            "Casting from {from_type} to {to_type} not supported"
1210        ))),
1211        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1212            "Casting from {from_type} to {to_type} not supported"
1213        ))),
1214        (_, Boolean) => match from_type {
1215            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1216            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1217            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1218            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1219            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1220            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1221            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1222            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1223            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1224            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1225            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1226            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1227            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1228            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1229            _ => Err(ArrowError::CastError(format!(
1230                "Casting from {from_type} to {to_type} not supported",
1231            ))),
1232        },
1233        (Boolean, _) => match to_type {
1234            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1235            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1236            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1237            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1238            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1239            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1240            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1241            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1242            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1243            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1244            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1245            Utf8View => cast_bool_to_string::<StringViewArray>(array),
1246            Utf8 => cast_bool_to_string::<StringArray>(array),
1247            LargeUtf8 => cast_bool_to_string::<LargeStringArray>(array),
1248            _ => Err(ArrowError::CastError(format!(
1249                "Casting from {from_type} to {to_type} not supported",
1250            ))),
1251        },
1252        (Utf8, _) => match to_type {
1253            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1254            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1255            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1256            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1257            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1258            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1259            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1260            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1261            Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1262            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1263            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1264            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1265            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1266            Binary => Ok(Arc::new(BinaryArray::from(
1267                array.as_string::<i32>().clone(),
1268            ))),
1269            LargeBinary => {
1270                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1271                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1272            }
1273            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1274            BinaryView => Ok(Arc::new(
1275                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1276            )),
1277            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1278            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1279            Time32(TimeUnit::Millisecond) => {
1280                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1281            }
1282            Time64(TimeUnit::Microsecond) => {
1283                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1284            }
1285            Time64(TimeUnit::Nanosecond) => {
1286                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1287            }
1288            Timestamp(TimeUnit::Second, to_tz) => cast_string_to_timestamp::<
1289                i32,
1290                TimestampSecondType,
1291            >(array, to_tz.as_ref(), cast_options),
1292            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1293                i32,
1294                TimestampMillisecondType,
1295            >(
1296                array, to_tz.as_ref(), cast_options
1297            ),
1298            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1299                i32,
1300                TimestampMicrosecondType,
1301            >(
1302                array, to_tz.as_ref(), cast_options
1303            ),
1304            Timestamp(TimeUnit::Nanosecond, to_tz) => cast_string_to_timestamp::<
1305                i32,
1306                TimestampNanosecondType,
1307            >(
1308                array, to_tz.as_ref(), cast_options
1309            ),
1310            Interval(IntervalUnit::YearMonth) => {
1311                cast_string_to_year_month_interval::<i32>(array, cast_options)
1312            }
1313            Interval(IntervalUnit::DayTime) => {
1314                cast_string_to_day_time_interval::<i32>(array, cast_options)
1315            }
1316            Interval(IntervalUnit::MonthDayNano) => {
1317                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1318            }
1319            _ => Err(ArrowError::CastError(format!(
1320                "Casting from {from_type} to {to_type} not supported",
1321            ))),
1322        },
1323        (Utf8View, _) => match to_type {
1324            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1325            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1326            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1327            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1328            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1329            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1330            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1331            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1332            Float16 => parse_string_view::<Float16Type>(array, cast_options),
1333            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1334            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1335            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1336            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1337            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1338            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1339            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1340            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1341            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1342            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1343            Time32(TimeUnit::Millisecond) => {
1344                parse_string_view::<Time32MillisecondType>(array, cast_options)
1345            }
1346            Time64(TimeUnit::Microsecond) => {
1347                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1348            }
1349            Time64(TimeUnit::Nanosecond) => {
1350                parse_string_view::<Time64NanosecondType>(array, cast_options)
1351            }
1352            Timestamp(TimeUnit::Second, to_tz) => {
1353                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz.as_ref(), cast_options)
1354            }
1355            Timestamp(TimeUnit::Millisecond, to_tz) => cast_view_to_timestamp::<
1356                TimestampMillisecondType,
1357            >(
1358                array, to_tz.as_ref(), cast_options
1359            ),
1360            Timestamp(TimeUnit::Microsecond, to_tz) => cast_view_to_timestamp::<
1361                TimestampMicrosecondType,
1362            >(
1363                array, to_tz.as_ref(), cast_options
1364            ),
1365            Timestamp(TimeUnit::Nanosecond, to_tz) => cast_view_to_timestamp::<
1366                TimestampNanosecondType,
1367            >(
1368                array, to_tz.as_ref(), cast_options
1369            ),
1370            Interval(IntervalUnit::YearMonth) => {
1371                cast_view_to_year_month_interval(array, cast_options)
1372            }
1373            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1374            Interval(IntervalUnit::MonthDayNano) => {
1375                cast_view_to_month_day_nano_interval(array, cast_options)
1376            }
1377            _ => Err(ArrowError::CastError(format!(
1378                "Casting from {from_type} to {to_type} not supported",
1379            ))),
1380        },
1381        (LargeUtf8, _) => match to_type {
1382            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1383            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1384            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1385            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1386            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1387            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1388            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1389            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1390            Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1391            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1392            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1393            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1394            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1395            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1396            Binary => {
1397                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1398                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1399            }
1400            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1401                array.as_string::<i64>().clone(),
1402            ))),
1403            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1404            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1405                array
1406                    .as_string::<i64>()
1407                    .into_iter()
1408                    .map(|x| x.map(|x| x.as_bytes()))
1409                    .collect::<Vec<_>>(),
1410            ))),
1411            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1412            Time32(TimeUnit::Millisecond) => {
1413                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1414            }
1415            Time64(TimeUnit::Microsecond) => {
1416                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1417            }
1418            Time64(TimeUnit::Nanosecond) => {
1419                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1420            }
1421            Timestamp(TimeUnit::Second, to_tz) => cast_string_to_timestamp::<
1422                i64,
1423                TimestampSecondType,
1424            >(array, to_tz.as_ref(), cast_options),
1425            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1426                i64,
1427                TimestampMillisecondType,
1428            >(
1429                array, to_tz.as_ref(), cast_options
1430            ),
1431            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1432                i64,
1433                TimestampMicrosecondType,
1434            >(
1435                array, to_tz.as_ref(), cast_options
1436            ),
1437            Timestamp(TimeUnit::Nanosecond, to_tz) => cast_string_to_timestamp::<
1438                i64,
1439                TimestampNanosecondType,
1440            >(
1441                array, to_tz.as_ref(), cast_options
1442            ),
1443            Interval(IntervalUnit::YearMonth) => {
1444                cast_string_to_year_month_interval::<i64>(array, cast_options)
1445            }
1446            Interval(IntervalUnit::DayTime) => {
1447                cast_string_to_day_time_interval::<i64>(array, cast_options)
1448            }
1449            Interval(IntervalUnit::MonthDayNano) => {
1450                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1451            }
1452            _ => Err(ArrowError::CastError(format!(
1453                "Casting from {from_type} to {to_type} not supported",
1454            ))),
1455        },
1456        (Binary, _) => match to_type {
1457            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1458            LargeUtf8 => {
1459                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1460                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1461            }
1462            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1463            FixedSizeBinary(size) => {
1464                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1465            }
1466            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1467            Utf8View => Ok(Arc::new(StringViewArray::from(
1468                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1469            ))),
1470            _ => Err(ArrowError::CastError(format!(
1471                "Casting from {from_type} to {to_type} not supported",
1472            ))),
1473        },
1474        (LargeBinary, _) => match to_type {
1475            Utf8 => {
1476                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1477                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1478            }
1479            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1480            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1481            FixedSizeBinary(size) => {
1482                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1483            }
1484            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1485            Utf8View => {
1486                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1487                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1488            }
1489            _ => Err(ArrowError::CastError(format!(
1490                "Casting from {from_type} to {to_type} not supported",
1491            ))),
1492        },
1493        (FixedSizeBinary(size), _) => match to_type {
1494            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1495            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1496            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1497            _ => Err(ArrowError::CastError(format!(
1498                "Casting from {from_type} to {to_type} not supported",
1499            ))),
1500        },
1501        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1502        (BinaryView, LargeBinary) => {
1503            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1504        }
1505        (BinaryView, Utf8) => {
1506            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1507            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1508        }
1509        (BinaryView, LargeUtf8) => {
1510            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1511            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1512        }
1513        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1514        (BinaryView, _) => Err(ArrowError::CastError(format!(
1515            "Casting from {from_type} to {to_type} not supported",
1516        ))),
1517        (from_type, Utf8View) if from_type.is_primitive() => {
1518            value_to_string_view(array, cast_options)
1519        }
1520        (from_type, LargeUtf8) if from_type.is_primitive() => {
1521            value_to_string::<i64>(array, cast_options)
1522        }
1523        (from_type, Utf8) if from_type.is_primitive() => {
1524            value_to_string::<i32>(array, cast_options)
1525        }
1526        (from_type, Binary) if from_type.is_integer() => match from_type {
1527            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1528            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1529            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1530            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1531            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1532            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1533            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1534            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1535            _ => unreachable!(),
1536        },
1537        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1538            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1539            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1540            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1541            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1542            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1543            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1544            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1545            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1546            _ => unreachable!(),
1547        },
1548        // start numeric casts
1549        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1550        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1551        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1552        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1553        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1554        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1555        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1556        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1557        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1558        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1559
1560        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1561        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1562        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1563        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1564        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1565        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1566        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1567        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1568        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1569        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1570
1571        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1572        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1573        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1574        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1575        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1576        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1577        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1578        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1579        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1580        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1581
1582        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1583        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1584        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1585        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1586        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1587        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1588        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1589        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1590        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1591        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1592
1593        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1594        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1595        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1596        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1597        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1598        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1599        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1600        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1601        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1602        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1603
1604        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1605        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1606        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1607        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1608        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1609        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1610        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1611        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1612        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1613        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1614
1615        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1616        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1617        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1618        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1619        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1620        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1621        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1622        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1623        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1624        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1625
1626        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1627        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1628        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1629        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1630        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1631        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1632        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1633        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1634        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1635        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1636
1637        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1638        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1639        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1640        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1641        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1642        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1643        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1644        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1645        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1646        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1647
1648        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1649        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1650        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1651        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1652        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1653        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1654        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1655        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1656        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1657        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1658
1659        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1660        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1661        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1662        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1663        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1664        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1665        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1666        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1667        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1668        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1669        // end numeric casts
1670
1671        // temporal casts
1672        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1673        (Int32, Date64) => cast_with_options(
1674            &cast_with_options(array, &Date32, cast_options)?,
1675            &Date64,
1676            cast_options,
1677        ),
1678        (Int32, Time32(TimeUnit::Second)) => {
1679            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1680        }
1681        (Int32, Time32(TimeUnit::Millisecond)) => {
1682            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1683        }
1684        // No support for microsecond/nanosecond with i32
1685        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1686        (Date32, Int64) => cast_with_options(
1687            &cast_with_options(array, &Int32, cast_options)?,
1688            &Int64,
1689            cast_options,
1690        ),
1691        (Time32(TimeUnit::Second), Int32) => {
1692            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1693        }
1694        (Time32(TimeUnit::Millisecond), Int32) => {
1695            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1696        }
1697        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1698            &cast_with_options(array, &Int32, cast_options)?,
1699            &Int64,
1700            cast_options,
1701        ),
1702        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1703            &cast_with_options(array, &Int32, cast_options)?,
1704            &Int64,
1705            cast_options,
1706        ),
1707        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1708        (Int64, Date32) => cast_with_options(
1709            &cast_with_options(array, &Int32, cast_options)?,
1710            &Date32,
1711            cast_options,
1712        ),
1713        // No support for second/milliseconds with i64
1714        (Int64, Time64(TimeUnit::Microsecond)) => {
1715            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1716        }
1717        (Int64, Time64(TimeUnit::Nanosecond)) => {
1718            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1719        }
1720
1721        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1722        (Date64, Int32) => cast_with_options(
1723            &cast_with_options(array, &Int64, cast_options)?,
1724            &Int32,
1725            cast_options,
1726        ),
1727        (Time64(TimeUnit::Microsecond), Int64) => {
1728            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1729        }
1730        (Time64(TimeUnit::Nanosecond), Int64) => {
1731            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1732        }
1733        (Date32, Date64) => Ok(Arc::new(
1734            array
1735                .as_primitive::<Date32Type>()
1736                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1737        )),
1738        (Date64, Date32) => {
1739            let array = array.as_primitive::<Date64Type>();
1740            let result = if cast_options.safe {
1741                array.unary_opt::<_, Date32Type>(|x| i32::try_from(x / MILLISECONDS_IN_DAY).ok())
1742            } else {
1743                array.try_unary::<_, Date32Type, _>(|x| {
1744                    i32::try_from(x / MILLISECONDS_IN_DAY).map_err(|_| {
1745                        ArrowError::CastError(format!(
1746                            "Cannot cast Date64 value {x} to Date32 without overflow"
1747                        ))
1748                    })
1749                })?
1750            };
1751            Ok(Arc::new(result))
1752        }
1753
1754        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => {
1755            let array = array.as_primitive::<Time32SecondType>();
1756            let result = if cast_options.safe {
1757                array.unary_opt::<_, Time32MillisecondType>(|x| x.checked_mul(MILLISECONDS as i32))
1758            } else {
1759                array.try_unary::<_, Time32MillisecondType, _>(|x| {
1760                    x.mul_checked(MILLISECONDS as i32)
1761                })?
1762            };
1763            Ok(Arc::new(result))
1764        }
1765        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1766            array
1767                .as_primitive::<Time32SecondType>()
1768                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1769        )),
1770        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1771            array
1772                .as_primitive::<Time32SecondType>()
1773                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1774        )),
1775
1776        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1777            array
1778                .as_primitive::<Time32MillisecondType>()
1779                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1780        )),
1781        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1782            array
1783                .as_primitive::<Time32MillisecondType>()
1784                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1785        )),
1786        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1787            array
1788                .as_primitive::<Time32MillisecondType>()
1789                .unary::<_, Time64NanosecondType>(|x| x as i64 * (NANOSECONDS / MILLISECONDS)),
1790        )),
1791
1792        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => {
1793            cast_time64_to_time32::<Time64MicrosecondType, Time32SecondType>(
1794                array,
1795                MICROSECONDS,
1796                cast_options,
1797            )
1798        }
1799        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => {
1800            cast_time64_to_time32::<Time64MicrosecondType, Time32MillisecondType>(
1801                array,
1802                MICROSECONDS / MILLISECONDS,
1803                cast_options,
1804            )
1805        }
1806        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => {
1807            let array = array.as_primitive::<Time64MicrosecondType>();
1808            let result = if cast_options.safe {
1809                array.unary_opt::<_, Time64NanosecondType>(|x| {
1810                    x.checked_mul(NANOSECONDS / MICROSECONDS)
1811                })
1812            } else {
1813                array.try_unary::<_, Time64NanosecondType, _>(|x| {
1814                    x.mul_checked(NANOSECONDS / MICROSECONDS)
1815                })?
1816            };
1817            Ok(Arc::new(result))
1818        }
1819
1820        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => {
1821            cast_time64_to_time32::<Time64NanosecondType, Time32SecondType>(
1822                array,
1823                NANOSECONDS,
1824                cast_options,
1825            )
1826        }
1827        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => {
1828            cast_time64_to_time32::<Time64NanosecondType, Time32MillisecondType>(
1829                array,
1830                NANOSECONDS / MILLISECONDS,
1831                cast_options,
1832            )
1833        }
1834        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1835            array
1836                .as_primitive::<Time64NanosecondType>()
1837                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1838        )),
1839
1840        // Timestamp to integer/floating/decimals
1841        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1842            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1843            cast_with_options(&array, to_type, cast_options)
1844        }
1845        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1846            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1847            cast_with_options(&array, to_type, cast_options)
1848        }
1849        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1850            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1851            cast_with_options(&array, to_type, cast_options)
1852        }
1853        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1854            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1855            cast_with_options(&array, to_type, cast_options)
1856        }
1857
1858        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1859            let array = cast_with_options(array, &Int64, cast_options)?;
1860            Ok(make_timestamp_array(
1861                array.as_primitive(),
1862                *unit,
1863                tz.clone(),
1864            ))
1865        }
1866
1867        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1868            let array = cast_with_options(array, &Int64, cast_options)?;
1869            let time_array = array.as_primitive::<Int64Type>();
1870            let from_size = time_unit_multiple(from_unit);
1871            let to_size = time_unit_multiple(to_unit);
1872            // we either divide or multiply, depending on size of each unit
1873            // units are never the same when the types are the same
1874            let converted = match from_size.cmp(&to_size) {
1875                Ordering::Greater => {
1876                    let divisor = from_size / to_size;
1877                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1878                }
1879                Ordering::Equal => time_array.clone(),
1880                Ordering::Less => {
1881                    let mul = to_size / from_size;
1882                    if cast_options.safe {
1883                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1884                    } else {
1885                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1886                    }
1887                }
1888            };
1889            // Normalize timezone
1890            let adjusted = match (from_tz, to_tz) {
1891                // Only this case needs to be adjusted because we're casting from
1892                // unknown time offset to some time offset, we want the time to be
1893                // unchanged.
1894                //
1895                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1896                //
1897                // For IANA timezones a wall clock reading is not always a unique
1898                // instant: a "fall back" DST transition makes an hour occur twice
1899                // and a "spring forward" transition skips an hour entirely. See
1900                // `resolve_local_offset` for how those readings are resolved.
1901                (None, Some(to_tz)) => {
1902                    let to_tz: Tz = to_tz.parse()?;
1903                    match to_unit {
1904                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1905                            converted,
1906                            &to_tz,
1907                            cast_options,
1908                        )?,
1909                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1910                            TimestampMillisecondType,
1911                        >(
1912                            converted, &to_tz, cast_options
1913                        )?,
1914                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1915                            TimestampMicrosecondType,
1916                        >(
1917                            converted, &to_tz, cast_options
1918                        )?,
1919                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1920                            TimestampNanosecondType,
1921                        >(
1922                            converted, &to_tz, cast_options
1923                        )?,
1924                    }
1925                }
1926                _ => converted,
1927            };
1928            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1929        }
1930        (Timestamp(TimeUnit::Microsecond, _), Date32) => timestamp_to_date32(
1931            array.as_primitive::<TimestampMicrosecondType>(),
1932            cast_options,
1933        ),
1934        (Timestamp(TimeUnit::Millisecond, _), Date32) => timestamp_to_date32(
1935            array.as_primitive::<TimestampMillisecondType>(),
1936            cast_options,
1937        ),
1938        (Timestamp(TimeUnit::Second, _), Date32) => {
1939            timestamp_to_date32(array.as_primitive::<TimestampSecondType>(), cast_options)
1940        }
1941        (Timestamp(TimeUnit::Nanosecond, _), Date32) => timestamp_to_date32(
1942            array.as_primitive::<TimestampNanosecondType>(),
1943            cast_options,
1944        ),
1945        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1946            true => {
1947                // change error to None
1948                array
1949                    .as_primitive::<TimestampSecondType>()
1950                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1951            }
1952            false => array
1953                .as_primitive::<TimestampSecondType>()
1954                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1955        })),
1956        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1957            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1958        }
1959        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1960            array
1961                .as_primitive::<TimestampMicrosecondType>()
1962                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1963        )),
1964        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1965            array
1966                .as_primitive::<TimestampNanosecondType>()
1967                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1968        )),
1969        (Timestamp(TimeUnit::Second, _), Time32(_) | Time64(_)) => {
1970            cast_timestamp_to_time::<TimestampSecondType>(array, to_type, cast_options)
1971        }
1972        (Timestamp(TimeUnit::Millisecond, _), Time32(_) | Time64(_)) => {
1973            cast_timestamp_to_time::<TimestampMillisecondType>(array, to_type, cast_options)
1974        }
1975        (Timestamp(TimeUnit::Microsecond, _), Time32(_) | Time64(_)) => {
1976            cast_timestamp_to_time::<TimestampMicrosecondType>(array, to_type, cast_options)
1977        }
1978        (Timestamp(TimeUnit::Nanosecond, _), Time32(_) | Time64(_)) => {
1979            cast_timestamp_to_time::<TimestampNanosecondType>(array, to_type, cast_options)
1980        }
1981        (Date64, Timestamp(TimeUnit::Second, _)) => {
1982            let array = array
1983                .as_primitive::<Date64Type>()
1984                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
1985
1986            cast_with_options(&array, to_type, cast_options)
1987        }
1988        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
1989            let array = array
1990                .as_primitive::<Date64Type>()
1991                .reinterpret_cast::<TimestampMillisecondType>();
1992
1993            cast_with_options(&array, to_type, cast_options)
1994        }
1995
1996        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
1997            let array = array
1998                .as_primitive::<Date64Type>()
1999                .reinterpret_cast::<TimestampMillisecondType>();
2000
2001            cast_with_options(&array, to_type, cast_options)
2002        }
2003        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2004            let array = array
2005                .as_primitive::<Date64Type>()
2006                .reinterpret_cast::<TimestampMillisecondType>();
2007
2008            cast_with_options(&array, to_type, cast_options)
2009        }
2010        (Date32, Timestamp(TimeUnit::Second, _)) => {
2011            let array = array
2012                .as_primitive::<Date32Type>()
2013                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2014
2015            cast_with_options(&array, to_type, cast_options)
2016        }
2017        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2018            let array = array
2019                .as_primitive::<Date32Type>()
2020                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2021
2022            cast_with_options(&array, to_type, cast_options)
2023        }
2024        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2025            let date_array = array.as_primitive::<Date32Type>();
2026            let converted = if cast_options.safe {
2027                date_array.unary_opt::<_, TimestampMicrosecondType>(|x| {
2028                    (x as i64).checked_mul(MICROSECONDS_IN_DAY)
2029                })
2030            } else {
2031                date_array.try_unary::<_, TimestampMicrosecondType, _>(|x| {
2032                    (x as i64).mul_checked(MICROSECONDS_IN_DAY)
2033                })?
2034            };
2035            cast_with_options(&converted, to_type, cast_options)
2036        }
2037        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2038            let date_array = array.as_primitive::<Date32Type>();
2039            let converted = if cast_options.safe {
2040                date_array.unary_opt::<_, TimestampNanosecondType>(|x| {
2041                    (x as i64).checked_mul(NANOSECONDS_IN_DAY)
2042                })
2043            } else {
2044                date_array.try_unary::<_, TimestampNanosecondType, _>(|x| {
2045                    (x as i64).mul_checked(NANOSECONDS_IN_DAY)
2046                })?
2047            };
2048            cast_with_options(&converted, to_type, cast_options)
2049        }
2050
2051        (_, Duration(unit)) if from_type.is_numeric() => {
2052            let array = cast_with_options(array, &Int64, cast_options)?;
2053            Ok(make_duration_array(array.as_primitive(), *unit))
2054        }
2055        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2056            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2057            cast_with_options(&array, to_type, cast_options)
2058        }
2059        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2060            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2061            cast_with_options(&array, to_type, cast_options)
2062        }
2063        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2064            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2065            cast_with_options(&array, to_type, cast_options)
2066        }
2067        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2068            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2069            cast_with_options(&array, to_type, cast_options)
2070        }
2071
2072        (Duration(from_unit), Duration(to_unit)) => {
2073            let array = cast_with_options(array, &Int64, cast_options)?;
2074            let time_array = array.as_primitive::<Int64Type>();
2075            let from_size = time_unit_multiple(from_unit);
2076            let to_size = time_unit_multiple(to_unit);
2077            // we either divide or multiply, depending on size of each unit
2078            // units are never the same when the types are the same
2079            let converted = match from_size.cmp(&to_size) {
2080                Ordering::Greater => {
2081                    let divisor = from_size / to_size;
2082                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2083                }
2084                Ordering::Equal => time_array.clone(),
2085                Ordering::Less => {
2086                    let mul = to_size / from_size;
2087                    if cast_options.safe {
2088                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2089                    } else {
2090                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2091                    }
2092                }
2093            };
2094            Ok(make_duration_array(&converted, *to_unit))
2095        }
2096
2097        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2098            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2099        }
2100        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2101            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2102        }
2103        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2104            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2105        }
2106        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2107            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2108        }
2109        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2110            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2111        }
2112        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2113            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2114        }
2115        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2116            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2117        }
2118        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2119            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2120        }
2121        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2122            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2123        }
2124        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2125            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2126        }
2127        (Int32, Interval(IntervalUnit::YearMonth)) => {
2128            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2129        }
2130        (_, _) => Err(ArrowError::CastError(format!(
2131            "Casting from {from_type} to {to_type} not supported",
2132        ))),
2133    }
2134}
2135
2136fn cast_from_decimal<D, F>(
2137    array: &dyn Array,
2138    scale: &i8,
2139    from_type: &DataType,
2140    to_type: &DataType,
2141    as_float: F,
2142    cast_options: &CastOptions,
2143) -> Result<ArrayRef, ArrowError>
2144where
2145    D: DecimalType + ArrowPrimitiveType,
2146    <D as ArrowPrimitiveType>::Native: ToPrimitive,
2147    F: Fn(D::Native) -> f64,
2148{
2149    use DataType::*;
2150    // cast decimal to other type
2151    match to_type {
2152        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, *scale, cast_options),
2153        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, *scale, cast_options),
2154        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, *scale, cast_options),
2155        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, *scale, cast_options),
2156        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, *scale, cast_options),
2157        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, *scale, cast_options),
2158        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, *scale, cast_options),
2159        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, *scale, cast_options),
2160        Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
2161            half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
2162                &as_float,
2163                x,
2164                <i32 as From<i8>>::from(*scale),
2165            ))
2166        }),
2167        Float32 => cast_decimal_to_f32::<D, F>(array, &as_float, *scale),
2168        Float64 => cast_decimal_to_f64::<D, F>(array, &as_float, *scale),
2169        Utf8View => value_to_string_view(array, cast_options),
2170        Utf8 => value_to_string::<i32>(array, cast_options),
2171        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2172        Null => Ok(new_null_array(to_type, array.len())),
2173        _ => Err(ArrowError::CastError(format!(
2174            "Casting from {from_type} to {to_type} not supported"
2175        ))),
2176    }
2177}
2178
2179fn cast_to_decimal<D, M>(
2180    array: &dyn Array,
2181    precision: &u8,
2182    scale: &i8,
2183    from_type: &DataType,
2184    to_type: &DataType,
2185    cast_options: &CastOptions,
2186) -> Result<ArrayRef, ArrowError>
2187where
2188    D: DecimalType + ArrowPrimitiveType<Native = M>,
2189    M: ArrowNativeTypeOp + DecimalCast,
2190{
2191    use DataType::*;
2192    // cast data to decimal
2193    match from_type {
2194        UInt8 => cast_integer_to_decimal::<_, D, M>(
2195            array.as_primitive::<UInt8Type>(),
2196            *precision,
2197            *scale,
2198            cast_options,
2199        ),
2200        UInt16 => cast_integer_to_decimal::<_, D, _>(
2201            array.as_primitive::<UInt16Type>(),
2202            *precision,
2203            *scale,
2204            cast_options,
2205        ),
2206        UInt32 => cast_integer_to_decimal::<_, D, _>(
2207            array.as_primitive::<UInt32Type>(),
2208            *precision,
2209            *scale,
2210            cast_options,
2211        ),
2212        UInt64 => cast_integer_to_decimal::<_, D, _>(
2213            array.as_primitive::<UInt64Type>(),
2214            *precision,
2215            *scale,
2216            cast_options,
2217        ),
2218        Int8 => cast_integer_to_decimal::<_, D, _>(
2219            array.as_primitive::<Int8Type>(),
2220            *precision,
2221            *scale,
2222            cast_options,
2223        ),
2224        Int16 => cast_integer_to_decimal::<_, D, _>(
2225            array.as_primitive::<Int16Type>(),
2226            *precision,
2227            *scale,
2228            cast_options,
2229        ),
2230        Int32 => cast_integer_to_decimal::<_, D, _>(
2231            array.as_primitive::<Int32Type>(),
2232            *precision,
2233            *scale,
2234            cast_options,
2235        ),
2236        Int64 => cast_integer_to_decimal::<_, D, _>(
2237            array.as_primitive::<Int64Type>(),
2238            *precision,
2239            *scale,
2240            cast_options,
2241        ),
2242        Float16 => cast_floating_point_to_decimal::<_, D>(
2243            array.as_primitive::<Float16Type>(),
2244            *precision,
2245            *scale,
2246            cast_options,
2247        ),
2248        Float32 => cast_floating_point_to_decimal::<_, D>(
2249            array.as_primitive::<Float32Type>(),
2250            *precision,
2251            *scale,
2252            cast_options,
2253        ),
2254        Float64 => cast_floating_point_to_decimal::<_, D>(
2255            array.as_primitive::<Float64Type>(),
2256            *precision,
2257            *scale,
2258            cast_options,
2259        ),
2260        Utf8View | Utf8 => {
2261            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2262        }
2263        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2264        Null => Ok(new_null_array(to_type, array.len())),
2265        _ => Err(ArrowError::CastError(format!(
2266            "Casting from {from_type} to {to_type} not supported"
2267        ))),
2268    }
2269}
2270
2271/// Number of `T` units in one day
2272const fn units_per_day<T: ArrowTimestampType>() -> i64 {
2273    SECONDS_IN_DAY * time_unit_multiple(&T::UNIT)
2274}
2275
2276/// Get the time unit as a multiple of a second
2277const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2278    match unit {
2279        TimeUnit::Second => 1,
2280        TimeUnit::Millisecond => MILLISECONDS,
2281        TimeUnit::Microsecond => MICROSECONDS,
2282        TimeUnit::Nanosecond => NANOSECONDS,
2283    }
2284}
2285
2286fn cast_time64_to_time32<FROM, TO>(
2287    array: &dyn Array,
2288    divisor: i64,
2289    cast_options: &CastOptions,
2290) -> Result<ArrayRef, ArrowError>
2291where
2292    FROM: ArrowPrimitiveType<Native = i64>,
2293    TO: ArrowPrimitiveType<Native = i32>,
2294{
2295    let array = array.as_primitive::<FROM>();
2296    let result = if cast_options.safe {
2297        array.unary_opt::<_, TO>(|value| i32::try_from(value / divisor).ok())
2298    } else {
2299        array.try_unary::<_, TO, _>(|value| {
2300            let value = value / divisor;
2301            i32::try_from(value).map_err(|_| {
2302                ArrowError::CastError(format!(
2303                    "Can't cast value {value:?} to type {}",
2304                    TO::DATA_TYPE
2305                ))
2306            })
2307        })?
2308    };
2309
2310    Ok(Arc::new(result))
2311}
2312
2313/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2314fn cast_numeric_arrays<FROM, TO>(
2315    from: &dyn Array,
2316    cast_options: &CastOptions,
2317) -> Result<ArrayRef, ArrowError>
2318where
2319    FROM: ArrowPrimitiveType,
2320    TO: ArrowPrimitiveType,
2321    FROM::Native: NumCast + NumericNative,
2322    TO::Native: NumCast + NumericNative,
2323{
2324    let from = from.as_primitive::<FROM>();
2325    let array: PrimitiveArray<TO> = if const {
2326        is_infallible_numeric_cast(
2327            <FROM::Native as NumericNative>::KIND,
2328            <TO::Native as NumericNative>::KIND,
2329        )
2330    } {
2331        // This cast cannot fail, so the fastest kernel, `unary`, can be used.
2332        from.unary(|v| num_cast(v).expect("numeric cast is infallible"))
2333    } else if cast_options.safe {
2334        // If the value can't be cast to the `TO::Native`, return null
2335        from.unary_opt(num_cast)
2336    } else {
2337        // If the value can't be cast to the `TO::Native`, return error
2338        from.try_unary(|v| {
2339            num_cast(v).ok_or_else(|| {
2340                ArrowError::CastError(format!("Can't cast value {v:?} to type {}", TO::DATA_TYPE))
2341            })
2342        })?
2343    };
2344    Ok(Arc::new(array))
2345}
2346
2347/// Properties of a numeric native type that decide whether [`num_cast`] to
2348/// another numeric type can fail.
2349#[derive(Clone, Copy)]
2350struct NumericKind {
2351    float: bool,
2352    signed: bool,
2353    bits: u32,
2354}
2355
2356/// A native type with a [`NumericKind`].
2357trait NumericNative {
2358    const KIND: NumericKind;
2359}
2360
2361macro_rules! numeric_native {
2362    ($($t:ty => $float:literal, $signed:literal;)*) => {$(
2363        impl NumericNative for $t {
2364            const KIND: NumericKind = NumericKind {
2365                float: $float,
2366                signed: $signed,
2367                bits: 8 * std::mem::size_of::<$t>() as u32,
2368            };
2369        }
2370    )*};
2371}
2372
2373numeric_native! {
2374    u8 => false, false;
2375    u16 => false, false;
2376    u32 => false, false;
2377    u64 => false, false;
2378    i8 => false, true;
2379    i16 => false, true;
2380    i32 => false, true;
2381    i64 => false, true;
2382    half::f16 => true, true;
2383    f32 => true, true;
2384    f64 => true, true;
2385}
2386
2387/// Returns true if [`num_cast`] succeeds for every value of `from` when casting
2388/// to `to`: every cast to a float, and integer casts whose target holds every
2389/// source value.
2390///
2391/// This must hold for every bit pattern of the source type, not only for the
2392/// valid values of a particular array, because [`PrimitiveArray::unary`] applies
2393/// the conversion to null slots as well, and their contents are arbitrary.
2394const fn is_infallible_numeric_cast(from: NumericKind, to: NumericKind) -> bool {
2395    if to.float {
2396        true
2397    } else if from.float {
2398        false
2399    } else if from.signed == to.signed {
2400        to.bits >= from.bits
2401    } else {
2402        // Only an unsigned source fits a strictly wider signed target.
2403        !from.signed && to.bits > from.bits
2404    }
2405}
2406
2407/// Natural cast between numeric types
2408/// Return None if the input `value` can't be casted to type `O`.
2409#[inline]
2410pub fn num_cast<I, O>(value: I) -> Option<O>
2411where
2412    I: NumCast,
2413    O: NumCast,
2414{
2415    num_traits::cast::cast::<I, O>(value)
2416}
2417
2418/// Cast numeric types to Boolean
2419///
2420/// Any zero value returns `false` while non-zero returns `true`
2421fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2422where
2423    FROM: ArrowPrimitiveType,
2424{
2425    Ok(Arc::new(BooleanArray::from_unary(
2426        from.as_primitive::<FROM>(),
2427        cast_num_to_bool,
2428    )))
2429}
2430
2431/// Cast numeric types to boolean
2432#[inline]
2433pub fn cast_num_to_bool<I>(value: I) -> bool
2434where
2435    I: Default + PartialEq,
2436{
2437    value != I::default()
2438}
2439
2440/// Cast Boolean types to numeric
2441///
2442/// `false` returns 0 while `true` returns 1
2443fn cast_bool_to_numeric<TO>(
2444    from: &dyn Array,
2445    cast_options: &CastOptions,
2446) -> Result<ArrayRef, ArrowError>
2447where
2448    TO: ArrowPrimitiveType,
2449{
2450    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2451        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2452        cast_options,
2453    )))
2454}
2455
2456fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2457where
2458    T: ArrowPrimitiveType,
2459{
2460    let to_numeric = |bits: u64, i: usize| {
2461        if bits & (1 << i) != 0 {
2462            T::Native::ONE
2463        } else {
2464            T::Native::ZERO
2465        }
2466    };
2467    // Unpack a 64-bit word at a time: the fixed-size inner loop helps the
2468    // compiler vectorize the conversion.
2469    let chunks = from.values().bit_chunks();
2470    let mut values = Vec::with_capacity(from.len());
2471    for bits in &chunks {
2472        values.extend((0..64).map(|i| to_numeric(bits, i)));
2473    }
2474    let bits = chunks.remainder_bits();
2475    values.extend((0..chunks.remainder_len()).map(|i| to_numeric(bits, i)));
2476    PrimitiveArray::new(values.into(), from.nulls().cloned())
2477}
2478
2479/// Cast single bool value to numeric value.
2480#[inline]
2481pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2482where
2483    O: num_traits::NumCast + Default,
2484{
2485    if value {
2486        // a workaround to cast a primitive to type O, infallible
2487        num_traits::cast::cast(1)
2488    } else {
2489        Some(O::default())
2490    }
2491}
2492
2493#[cfg(test)]
2494mod tests {
2495    use super::*;
2496    use crate::parse::parse_decimal;
2497    use DataType::*;
2498    use arrow_array::{Int64Array, RunArray, StringArray};
2499    use arrow_buffer::{BooleanBuffer, IntervalDayTime, NullBuffer, i256};
2500    use arrow_schema::{DataType, Field};
2501    use chrono::{FixedOffset, NaiveDate, NaiveTime};
2502    use half::f16;
2503    use std::sync::Arc;
2504
2505    #[derive(Clone)]
2506    struct DecimalCastTestConfig {
2507        input_prec: u8,
2508        input_scale: i8,
2509        input_repr: i128,
2510        output_prec: u8,
2511        output_scale: i8,
2512        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2513                                                    // template where the "{}" will be
2514                                                    // replaced with the decimal type name
2515                                                    // (e.g. Decimal128)
2516    }
2517
2518    macro_rules! generate_cast_test_case {
2519        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2520            let output =
2521                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2522
2523            // assert cast type
2524            let input_array_type = $INPUT_ARRAY.data_type();
2525            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2526            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2527            assert_eq!($OUTPUT_TYPE, result.data_type());
2528            assert_eq!(result.as_ref(), &output);
2529
2530            let cast_option = CastOptions {
2531                safe: false,
2532                format_options: FormatOptions::default(),
2533            };
2534            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2535            assert_eq!($OUTPUT_TYPE, result.data_type());
2536            assert_eq!(result.as_ref(), &output);
2537        };
2538    }
2539
2540    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2541    where
2542        I: DecimalType,
2543        O: DecimalType,
2544        I::Native: DecimalCast,
2545        O::Native: DecimalCast,
2546    {
2547        let array = vec![I::Native::from_decimal(t.input_repr)];
2548        let array = array
2549            .into_iter()
2550            .collect::<PrimitiveArray<I>>()
2551            .with_precision_and_scale(t.input_prec, t.input_scale)
2552            .unwrap();
2553        let input_type = array.data_type();
2554        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2555        assert!(can_cast_types(input_type, &output_type));
2556
2557        let options = CastOptions {
2558            safe: false,
2559            ..Default::default()
2560        };
2561        let result = cast_with_options(&array, &output_type, &options);
2562
2563        match t.expected_output_repr {
2564            Ok(v) => {
2565                let expected_array = vec![O::Native::from_decimal(v)];
2566                let expected_array = expected_array
2567                    .into_iter()
2568                    .collect::<PrimitiveArray<O>>()
2569                    .with_precision_and_scale(t.output_prec, t.output_scale)
2570                    .unwrap();
2571                assert_eq!(*result.unwrap(), expected_array);
2572            }
2573            Err(expected_output_message_template) => {
2574                assert!(result.is_err());
2575                let expected_error_message =
2576                    expected_output_message_template.replace("{}", O::PREFIX);
2577                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2578            }
2579        }
2580    }
2581
2582    fn create_decimal32_array(
2583        array: Vec<Option<i32>>,
2584        precision: u8,
2585        scale: i8,
2586    ) -> Result<Decimal32Array, ArrowError> {
2587        array
2588            .into_iter()
2589            .collect::<Decimal32Array>()
2590            .with_precision_and_scale(precision, scale)
2591    }
2592
2593    fn create_decimal64_array(
2594        array: Vec<Option<i64>>,
2595        precision: u8,
2596        scale: i8,
2597    ) -> Result<Decimal64Array, ArrowError> {
2598        array
2599            .into_iter()
2600            .collect::<Decimal64Array>()
2601            .with_precision_and_scale(precision, scale)
2602    }
2603
2604    fn create_decimal128_array(
2605        array: Vec<Option<i128>>,
2606        precision: u8,
2607        scale: i8,
2608    ) -> Result<Decimal128Array, ArrowError> {
2609        array
2610            .into_iter()
2611            .collect::<Decimal128Array>()
2612            .with_precision_and_scale(precision, scale)
2613    }
2614
2615    fn create_decimal256_array(
2616        array: Vec<Option<i256>>,
2617        precision: u8,
2618        scale: i8,
2619    ) -> Result<Decimal256Array, ArrowError> {
2620        array
2621            .into_iter()
2622            .collect::<Decimal256Array>()
2623            .with_precision_and_scale(precision, scale)
2624    }
2625
2626    #[test]
2627    #[should_panic(
2628        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2629    )]
2630    fn test_cast_decimal_to_decimal_round_with_error() {
2631        // decimal256 to decimal128 overflow
2632        let array = vec![
2633            Some(i256::from_i128(1123454)),
2634            Some(i256::from_i128(2123456)),
2635            Some(i256::from_i128(-3123453)),
2636            Some(i256::from_i128(-3123456)),
2637            None,
2638            Some(i256::MAX),
2639            Some(i256::MIN),
2640        ];
2641        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2642        let array = Arc::new(input_decimal_array) as ArrayRef;
2643        let input_type = DataType::Decimal256(76, 4);
2644        let output_type = DataType::Decimal128(20, 3);
2645        assert!(can_cast_types(&input_type, &output_type));
2646        generate_cast_test_case!(
2647            &array,
2648            Decimal128Array,
2649            &output_type,
2650            vec![
2651                Some(112345_i128),
2652                Some(212346_i128),
2653                Some(-312345_i128),
2654                Some(-312346_i128),
2655                None,
2656                None,
2657                None,
2658            ]
2659        );
2660    }
2661
2662    #[test]
2663    fn test_cast_decimal_to_decimal_round() {
2664        let array = vec![
2665            Some(1123454),
2666            Some(2123456),
2667            Some(-3123453),
2668            Some(-3123456),
2669            None,
2670        ];
2671        let array = create_decimal128_array(array, 20, 4).unwrap();
2672        // decimal128 to decimal128
2673        let input_type = DataType::Decimal128(20, 4);
2674        let output_type = DataType::Decimal128(20, 3);
2675        assert!(can_cast_types(&input_type, &output_type));
2676        generate_cast_test_case!(
2677            &array,
2678            Decimal128Array,
2679            &output_type,
2680            vec![
2681                Some(112345_i128),
2682                Some(212346_i128),
2683                Some(-312345_i128),
2684                Some(-312346_i128),
2685                None
2686            ]
2687        );
2688
2689        // decimal128 to decimal256
2690        let input_type = DataType::Decimal128(20, 4);
2691        let output_type = DataType::Decimal256(20, 3);
2692        assert!(can_cast_types(&input_type, &output_type));
2693        generate_cast_test_case!(
2694            &array,
2695            Decimal256Array,
2696            &output_type,
2697            vec![
2698                Some(i256::from_i128(112345_i128)),
2699                Some(i256::from_i128(212346_i128)),
2700                Some(i256::from_i128(-312345_i128)),
2701                Some(i256::from_i128(-312346_i128)),
2702                None
2703            ]
2704        );
2705
2706        // decimal256
2707        let array = vec![
2708            Some(i256::from_i128(1123454)),
2709            Some(i256::from_i128(2123456)),
2710            Some(i256::from_i128(-3123453)),
2711            Some(i256::from_i128(-3123456)),
2712            None,
2713        ];
2714        let array = create_decimal256_array(array, 20, 4).unwrap();
2715
2716        // decimal256 to decimal256
2717        let input_type = DataType::Decimal256(20, 4);
2718        let output_type = DataType::Decimal256(20, 3);
2719        assert!(can_cast_types(&input_type, &output_type));
2720        generate_cast_test_case!(
2721            &array,
2722            Decimal256Array,
2723            &output_type,
2724            vec![
2725                Some(i256::from_i128(112345_i128)),
2726                Some(i256::from_i128(212346_i128)),
2727                Some(i256::from_i128(-312345_i128)),
2728                Some(i256::from_i128(-312346_i128)),
2729                None
2730            ]
2731        );
2732        // decimal256 to decimal128
2733        let input_type = DataType::Decimal256(20, 4);
2734        let output_type = DataType::Decimal128(20, 3);
2735        assert!(can_cast_types(&input_type, &output_type));
2736        generate_cast_test_case!(
2737            &array,
2738            Decimal128Array,
2739            &output_type,
2740            vec![
2741                Some(112345_i128),
2742                Some(212346_i128),
2743                Some(-312345_i128),
2744                Some(-312346_i128),
2745                None
2746            ]
2747        );
2748    }
2749
2750    #[test]
2751    fn test_cast_decimal32_to_decimal32() {
2752        // test changing precision
2753        let input_type = DataType::Decimal32(9, 3);
2754        let output_type = DataType::Decimal32(9, 4);
2755        assert!(can_cast_types(&input_type, &output_type));
2756        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2757        let array = create_decimal32_array(array, 9, 3).unwrap();
2758        generate_cast_test_case!(
2759            &array,
2760            Decimal32Array,
2761            &output_type,
2762            vec![
2763                Some(11234560_i32),
2764                Some(21234560_i32),
2765                Some(31234560_i32),
2766                None
2767            ]
2768        );
2769        // negative test
2770        let array = vec![Some(123456), None];
2771        let array = create_decimal32_array(array, 9, 0).unwrap();
2772        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
2773        assert!(result_safe.is_ok());
2774        let options = CastOptions {
2775            safe: false,
2776            ..Default::default()
2777        };
2778
2779        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
2780        assert_eq!(
2781            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
2782            result_unsafe.unwrap_err().to_string()
2783        );
2784    }
2785
2786    #[test]
2787    fn test_cast_decimal64_to_decimal64() {
2788        // test changing precision
2789        let input_type = DataType::Decimal64(17, 3);
2790        let output_type = DataType::Decimal64(17, 4);
2791        assert!(can_cast_types(&input_type, &output_type));
2792        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2793        let array = create_decimal64_array(array, 17, 3).unwrap();
2794        generate_cast_test_case!(
2795            &array,
2796            Decimal64Array,
2797            &output_type,
2798            vec![
2799                Some(11234560_i64),
2800                Some(21234560_i64),
2801                Some(31234560_i64),
2802                None
2803            ]
2804        );
2805        // negative test
2806        let array = vec![Some(123456), None];
2807        let array = create_decimal64_array(array, 9, 0).unwrap();
2808        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
2809        assert!(result_safe.is_ok());
2810        let options = CastOptions {
2811            safe: false,
2812            ..Default::default()
2813        };
2814
2815        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
2816        assert_eq!(
2817            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
2818            result_unsafe.unwrap_err().to_string()
2819        );
2820    }
2821
2822    #[test]
2823    fn test_cast_decimal128_to_decimal128() {
2824        // test changing precision
2825        let input_type = DataType::Decimal128(20, 3);
2826        let output_type = DataType::Decimal128(20, 4);
2827        assert!(can_cast_types(&input_type, &output_type));
2828        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2829        let array = create_decimal128_array(array, 20, 3).unwrap();
2830        generate_cast_test_case!(
2831            &array,
2832            Decimal128Array,
2833            &output_type,
2834            vec![
2835                Some(11234560_i128),
2836                Some(21234560_i128),
2837                Some(31234560_i128),
2838                None
2839            ]
2840        );
2841        // negative test
2842        let array = vec![Some(123456), None];
2843        let array = create_decimal128_array(array, 10, 0).unwrap();
2844        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
2845        assert!(result_safe.is_ok());
2846        let options = CastOptions {
2847            safe: false,
2848            ..Default::default()
2849        };
2850
2851        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
2852        assert_eq!(
2853            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
2854            result_unsafe.unwrap_err().to_string()
2855        );
2856    }
2857
2858    #[test]
2859    fn test_cast_decimal32_to_decimal32_dict() {
2860        let p = 9;
2861        let s = 3;
2862        let input_type = DataType::Decimal32(p, s);
2863        let output_type = DataType::Dictionary(
2864            Box::new(DataType::Int32),
2865            Box::new(DataType::Decimal32(p, s)),
2866        );
2867        assert!(can_cast_types(&input_type, &output_type));
2868        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2869        let array = create_decimal32_array(array, p, s).unwrap();
2870        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2871        assert_eq!(cast_array.data_type(), &output_type);
2872    }
2873
2874    #[test]
2875    fn test_cast_decimal64_to_decimal64_dict() {
2876        let p = 15;
2877        let s = 3;
2878        let input_type = DataType::Decimal64(p, s);
2879        let output_type = DataType::Dictionary(
2880            Box::new(DataType::Int32),
2881            Box::new(DataType::Decimal64(p, s)),
2882        );
2883        assert!(can_cast_types(&input_type, &output_type));
2884        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2885        let array = create_decimal64_array(array, p, s).unwrap();
2886        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2887        assert_eq!(cast_array.data_type(), &output_type);
2888    }
2889
2890    #[test]
2891    fn test_cast_decimal128_to_decimal128_dict() {
2892        let p = 20;
2893        let s = 3;
2894        let input_type = DataType::Decimal128(p, s);
2895        let output_type = DataType::Dictionary(
2896            Box::new(DataType::Int32),
2897            Box::new(DataType::Decimal128(p, s)),
2898        );
2899        assert!(can_cast_types(&input_type, &output_type));
2900        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2901        let array = create_decimal128_array(array, p, s).unwrap();
2902        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2903        assert_eq!(cast_array.data_type(), &output_type);
2904    }
2905
2906    #[test]
2907    fn test_cast_decimal256_to_decimal256_dict() {
2908        let p = 20;
2909        let s = 3;
2910        let input_type = DataType::Decimal256(p, s);
2911        let output_type = DataType::Dictionary(
2912            Box::new(DataType::Int32),
2913            Box::new(DataType::Decimal256(p, s)),
2914        );
2915        assert!(can_cast_types(&input_type, &output_type));
2916        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2917        let array = create_decimal128_array(array, p, s).unwrap();
2918        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2919        assert_eq!(cast_array.data_type(), &output_type);
2920    }
2921
2922    #[test]
2923    fn test_cast_decimal32_to_decimal32_overflow() {
2924        let input_type = DataType::Decimal32(9, 3);
2925        let output_type = DataType::Decimal32(9, 9);
2926        assert!(can_cast_types(&input_type, &output_type));
2927
2928        let array = vec![Some(i32::MAX)];
2929        let array = create_decimal32_array(array, 9, 3).unwrap();
2930        let result = cast_with_options(
2931            &array,
2932            &output_type,
2933            &CastOptions {
2934                safe: false,
2935                format_options: FormatOptions::default(),
2936            },
2937        );
2938        assert_eq!(
2939            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
2940            result.unwrap_err().to_string()
2941        );
2942    }
2943
2944    #[test]
2945    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
2946        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
2947        let array = create_decimal32_array(array, 9, 3).unwrap();
2948
2949        // Divide out all digits of precision -- rounding could still produce +/- 1
2950        let output_type = DataType::Decimal32(9, -6);
2951        assert!(can_cast_types(array.data_type(), &output_type));
2952        generate_cast_test_case!(
2953            &array,
2954            Decimal32Array,
2955            &output_type,
2956            vec![Some(-1), Some(0), Some(1), None]
2957        );
2958
2959        // Divide out more digits than we have precision -- all-zero result
2960        let output_type = DataType::Decimal32(9, -7);
2961        assert!(can_cast_types(array.data_type(), &output_type));
2962        generate_cast_test_case!(
2963            &array,
2964            Decimal32Array,
2965            &output_type,
2966            vec![Some(0), Some(0), Some(0), None]
2967        );
2968    }
2969
2970    #[test]
2971    fn test_cast_decimal64_to_decimal64_overflow() {
2972        let input_type = DataType::Decimal64(18, 3);
2973        let output_type = DataType::Decimal64(18, 18);
2974        assert!(can_cast_types(&input_type, &output_type));
2975
2976        let array = vec![Some(i64::MAX)];
2977        let array = create_decimal64_array(array, 18, 3).unwrap();
2978        let result = cast_with_options(
2979            &array,
2980            &output_type,
2981            &CastOptions {
2982                safe: false,
2983                format_options: FormatOptions::default(),
2984            },
2985        );
2986        assert_eq!(
2987            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
2988            result.unwrap_err().to_string()
2989        );
2990    }
2991
2992    #[test]
2993    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
2994        let array = vec![
2995            Some(-999999999999999999),
2996            Some(0),
2997            Some(999999999999999999),
2998            None,
2999        ];
3000        let array = create_decimal64_array(array, 18, 3).unwrap();
3001
3002        // Divide out all digits of precision -- rounding could still produce +/- 1
3003        let output_type = DataType::Decimal64(18, -15);
3004        assert!(can_cast_types(array.data_type(), &output_type));
3005        generate_cast_test_case!(
3006            &array,
3007            Decimal64Array,
3008            &output_type,
3009            vec![Some(-1), Some(0), Some(1), None]
3010        );
3011
3012        // Divide out more digits than we have precision -- all-zero result
3013        let output_type = DataType::Decimal64(18, -16);
3014        assert!(can_cast_types(array.data_type(), &output_type));
3015        generate_cast_test_case!(
3016            &array,
3017            Decimal64Array,
3018            &output_type,
3019            vec![Some(0), Some(0), Some(0), None]
3020        );
3021    }
3022
3023    #[test]
3024    fn test_cast_floating_to_decimals() {
3025        for output_type in [
3026            DataType::Decimal32(9, 3),
3027            DataType::Decimal64(9, 3),
3028            DataType::Decimal128(9, 3),
3029            DataType::Decimal256(9, 3),
3030        ] {
3031            let input_type = DataType::Float64;
3032            assert!(can_cast_types(&input_type, &output_type));
3033
3034            let array = vec![Some(1.1_f64)];
3035            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3036            let result = cast_with_options(
3037                &array,
3038                &output_type,
3039                &CastOptions {
3040                    safe: false,
3041                    format_options: FormatOptions::default(),
3042                },
3043            );
3044            assert!(
3045                result.is_ok(),
3046                "Failed to cast to {output_type} with: {}",
3047                result.unwrap_err()
3048            );
3049        }
3050    }
3051
3052    #[test]
3053    #[cfg_attr(miri, ignore)] // Takes too long
3054    fn test_cast_float16_to_decimals() {
3055        let array = Float16Array::from(vec![
3056            Some(f16::from_f32(1.25)),
3057            Some(f16::from_f32(-2.5)),
3058            Some(f16::from_f32(1.125)),
3059            Some(f16::from_f32(-1.125)),
3060            Some(f16::from_f32(0.0)),
3061            None,
3062        ]);
3063
3064        generate_cast_test_case!(
3065            &array,
3066            Decimal32Array,
3067            &DataType::Decimal32(9, 2),
3068            vec![
3069                Some(125_i32),
3070                Some(-250_i32),
3071                Some(113_i32),
3072                Some(-113_i32),
3073                Some(0_i32),
3074                None
3075            ]
3076        );
3077        generate_cast_test_case!(
3078            &array,
3079            Decimal64Array,
3080            &DataType::Decimal64(18, 2),
3081            vec![
3082                Some(125_i64),
3083                Some(-250_i64),
3084                Some(113_i64),
3085                Some(-113_i64),
3086                Some(0_i64),
3087                None
3088            ]
3089        );
3090        generate_cast_test_case!(
3091            &array,
3092            Decimal128Array,
3093            &DataType::Decimal128(38, 2),
3094            vec![
3095                Some(125_i128),
3096                Some(-250_i128),
3097                Some(113_i128),
3098                Some(-113_i128),
3099                Some(0_i128),
3100                None
3101            ]
3102        );
3103        generate_cast_test_case!(
3104            &array,
3105            Decimal256Array,
3106            &DataType::Decimal256(76, 2),
3107            vec![
3108                Some(i256::from_i128(125_i128)),
3109                Some(i256::from_i128(-250_i128)),
3110                Some(i256::from_i128(113_i128)),
3111                Some(i256::from_i128(-113_i128)),
3112                Some(i256::from_i128(0_i128)),
3113                None
3114            ]
3115        );
3116
3117        let array = Float16Array::from(vec![
3118            Some(f16::from_f32(1250.0)),
3119            Some(f16::from_f32(-1250.0)),
3120            Some(f16::from_f32(1249.0)),
3121            None,
3122        ]);
3123        generate_cast_test_case!(
3124            &array,
3125            Decimal128Array,
3126            &DataType::Decimal128(5, -2),
3127            vec![Some(13_i128), Some(-13_i128), Some(12_i128), None]
3128        );
3129    }
3130
3131    #[test]
3132    fn test_cast_decimal128_to_decimal128_overflow() {
3133        let input_type = DataType::Decimal128(38, 3);
3134        let output_type = DataType::Decimal128(38, 38);
3135        assert!(can_cast_types(&input_type, &output_type));
3136
3137        let array = vec![Some(i128::MAX)];
3138        let array = create_decimal128_array(array, 38, 3).unwrap();
3139        let result = cast_with_options(
3140            &array,
3141            &output_type,
3142            &CastOptions {
3143                safe: false,
3144                format_options: FormatOptions::default(),
3145            },
3146        );
3147        assert_eq!(
3148            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3149            result.unwrap_err().to_string()
3150        );
3151    }
3152
3153    #[test]
3154    fn test_cast_decimal128_to_decimal256_overflow() {
3155        let input_type = DataType::Decimal128(38, 3);
3156        let output_type = DataType::Decimal256(76, 76);
3157        assert!(can_cast_types(&input_type, &output_type));
3158
3159        let array = vec![Some(i128::MAX)];
3160        let array = create_decimal128_array(array, 38, 3).unwrap();
3161        let result = cast_with_options(
3162            &array,
3163            &output_type,
3164            &CastOptions {
3165                safe: false,
3166                format_options: FormatOptions::default(),
3167            },
3168        );
3169        assert_eq!(
3170            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3171            result.unwrap_err().to_string()
3172        );
3173    }
3174
3175    #[test]
3176    fn test_cast_decimal32_to_decimal256() {
3177        let input_type = DataType::Decimal32(8, 3);
3178        let output_type = DataType::Decimal256(20, 4);
3179        assert!(can_cast_types(&input_type, &output_type));
3180        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3181        let array = create_decimal32_array(array, 8, 3).unwrap();
3182        generate_cast_test_case!(
3183            &array,
3184            Decimal256Array,
3185            &output_type,
3186            vec![
3187                Some(i256::from_i128(11234560_i128)),
3188                Some(i256::from_i128(21234560_i128)),
3189                Some(i256::from_i128(31234560_i128)),
3190                None
3191            ]
3192        );
3193    }
3194    #[test]
3195    fn test_cast_decimal64_to_decimal256() {
3196        let input_type = DataType::Decimal64(12, 3);
3197        let output_type = DataType::Decimal256(20, 4);
3198        assert!(can_cast_types(&input_type, &output_type));
3199        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3200        let array = create_decimal64_array(array, 12, 3).unwrap();
3201        generate_cast_test_case!(
3202            &array,
3203            Decimal256Array,
3204            &output_type,
3205            vec![
3206                Some(i256::from_i128(11234560_i128)),
3207                Some(i256::from_i128(21234560_i128)),
3208                Some(i256::from_i128(31234560_i128)),
3209                None
3210            ]
3211        );
3212    }
3213    #[test]
3214    fn test_cast_decimal128_to_decimal256() {
3215        let input_type = DataType::Decimal128(20, 3);
3216        let output_type = DataType::Decimal256(20, 4);
3217        assert!(can_cast_types(&input_type, &output_type));
3218        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3219        let array = create_decimal128_array(array, 20, 3).unwrap();
3220        generate_cast_test_case!(
3221            &array,
3222            Decimal256Array,
3223            &output_type,
3224            vec![
3225                Some(i256::from_i128(11234560_i128)),
3226                Some(i256::from_i128(21234560_i128)),
3227                Some(i256::from_i128(31234560_i128)),
3228                None
3229            ]
3230        );
3231    }
3232
3233    #[test]
3234    fn test_cast_decimal256_to_decimal128_overflow() {
3235        let input_type = DataType::Decimal256(76, 5);
3236        let output_type = DataType::Decimal128(38, 7);
3237        assert!(can_cast_types(&input_type, &output_type));
3238        let array = vec![Some(i256::from_i128(i128::MAX))];
3239        let array = create_decimal256_array(array, 76, 5).unwrap();
3240        let result = cast_with_options(
3241            &array,
3242            &output_type,
3243            &CastOptions {
3244                safe: false,
3245                format_options: FormatOptions::default(),
3246            },
3247        );
3248        assert_eq!(
3249            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3250            result.unwrap_err().to_string()
3251        );
3252    }
3253
3254    #[test]
3255    fn test_cast_decimal256_to_decimal256_overflow() {
3256        let input_type = DataType::Decimal256(76, 5);
3257        let output_type = DataType::Decimal256(76, 55);
3258        assert!(can_cast_types(&input_type, &output_type));
3259        let array = vec![Some(i256::from_i128(i128::MAX))];
3260        let array = create_decimal256_array(array, 76, 5).unwrap();
3261        let result = cast_with_options(
3262            &array,
3263            &output_type,
3264            &CastOptions {
3265                safe: false,
3266                format_options: FormatOptions::default(),
3267            },
3268        );
3269        assert_eq!(
3270            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3271            result.unwrap_err().to_string()
3272        );
3273    }
3274
3275    #[test]
3276    fn test_cast_decimal256_to_decimal128() {
3277        let input_type = DataType::Decimal256(20, 3);
3278        let output_type = DataType::Decimal128(20, 4);
3279        assert!(can_cast_types(&input_type, &output_type));
3280        let array = vec![
3281            Some(i256::from_i128(1123456)),
3282            Some(i256::from_i128(2123456)),
3283            Some(i256::from_i128(3123456)),
3284            None,
3285        ];
3286        let array = create_decimal256_array(array, 20, 3).unwrap();
3287        generate_cast_test_case!(
3288            &array,
3289            Decimal128Array,
3290            &output_type,
3291            vec![
3292                Some(11234560_i128),
3293                Some(21234560_i128),
3294                Some(31234560_i128),
3295                None
3296            ]
3297        );
3298    }
3299
3300    #[test]
3301    fn test_cast_decimal256_to_decimal256() {
3302        let input_type = DataType::Decimal256(20, 3);
3303        let output_type = DataType::Decimal256(20, 4);
3304        assert!(can_cast_types(&input_type, &output_type));
3305        let array = vec![
3306            Some(i256::from_i128(1123456)),
3307            Some(i256::from_i128(2123456)),
3308            Some(i256::from_i128(3123456)),
3309            None,
3310        ];
3311        let array = create_decimal256_array(array, 20, 3).unwrap();
3312        generate_cast_test_case!(
3313            &array,
3314            Decimal256Array,
3315            &output_type,
3316            vec![
3317                Some(i256::from_i128(11234560_i128)),
3318                Some(i256::from_i128(21234560_i128)),
3319                Some(i256::from_i128(31234560_i128)),
3320                None
3321            ]
3322        );
3323    }
3324
3325    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3326    where
3327        T: ArrowPrimitiveType + DecimalType,
3328    {
3329        // u8
3330        generate_cast_test_case!(
3331            array,
3332            UInt8Array,
3333            &DataType::UInt8,
3334            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3335        );
3336        // u16
3337        generate_cast_test_case!(
3338            array,
3339            UInt16Array,
3340            &DataType::UInt16,
3341            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3342        );
3343        // u32
3344        generate_cast_test_case!(
3345            array,
3346            UInt32Array,
3347            &DataType::UInt32,
3348            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3349        );
3350        // u64
3351        generate_cast_test_case!(
3352            array,
3353            UInt64Array,
3354            &DataType::UInt64,
3355            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3356        );
3357        // i8
3358        generate_cast_test_case!(
3359            array,
3360            Int8Array,
3361            &DataType::Int8,
3362            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3363        );
3364        // i16
3365        generate_cast_test_case!(
3366            array,
3367            Int16Array,
3368            &DataType::Int16,
3369            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3370        );
3371        // i32
3372        generate_cast_test_case!(
3373            array,
3374            Int32Array,
3375            &DataType::Int32,
3376            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3377        );
3378        // i64
3379        generate_cast_test_case!(
3380            array,
3381            Int64Array,
3382            &DataType::Int64,
3383            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3384        );
3385        // f16
3386        generate_cast_test_case!(
3387            array,
3388            Float16Array,
3389            &DataType::Float16,
3390            vec![
3391                Some(f16::from_f32(1.25)),
3392                Some(f16::from_f32(2.25)),
3393                Some(f16::from_f32(3.25)),
3394                None,
3395                Some(f16::from_f32(5.25))
3396            ]
3397        );
3398        // f32
3399        generate_cast_test_case!(
3400            array,
3401            Float32Array,
3402            &DataType::Float32,
3403            vec![
3404                Some(1.25_f32),
3405                Some(2.25_f32),
3406                Some(3.25_f32),
3407                None,
3408                Some(5.25_f32)
3409            ]
3410        );
3411        // f64
3412        generate_cast_test_case!(
3413            array,
3414            Float64Array,
3415            &DataType::Float64,
3416            vec![
3417                Some(1.25_f64),
3418                Some(2.25_f64),
3419                Some(3.25_f64),
3420                None,
3421                Some(5.25_f64)
3422            ]
3423        );
3424    }
3425
3426    #[test]
3427    #[cfg_attr(miri, ignore)] // Takes too long
3428    fn test_cast_decimal32_to_numeric() {
3429        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3430        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3431
3432        generate_decimal_to_numeric_cast_test_case(&array);
3433    }
3434
3435    #[test]
3436    #[cfg_attr(miri, ignore)] // Takes too long
3437    fn test_cast_decimal64_to_numeric() {
3438        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3439        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3440
3441        generate_decimal_to_numeric_cast_test_case(&array);
3442    }
3443
3444    #[test]
3445    #[cfg_attr(miri, ignore)] // Takes too long
3446    fn test_cast_decimal128_to_numeric() {
3447        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3448        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3449
3450        generate_decimal_to_numeric_cast_test_case(&array);
3451
3452        // overflow test: out of range of max u8
3453        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3454        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3455        let casted_array = cast_with_options(
3456            &array,
3457            &DataType::UInt8,
3458            &CastOptions {
3459                safe: false,
3460                format_options: FormatOptions::default(),
3461            },
3462        );
3463        assert_eq!(
3464            "Cast error: value of 513 is out of range UInt8".to_string(),
3465            casted_array.unwrap_err().to_string()
3466        );
3467
3468        let casted_array = cast_with_options(
3469            &array,
3470            &DataType::UInt8,
3471            &CastOptions {
3472                safe: true,
3473                format_options: FormatOptions::default(),
3474            },
3475        );
3476        assert!(casted_array.is_ok());
3477        assert!(casted_array.unwrap().is_null(0));
3478
3479        // overflow test: out of range of max i8
3480        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3481        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3482        let casted_array = cast_with_options(
3483            &array,
3484            &DataType::Int8,
3485            &CastOptions {
3486                safe: false,
3487                format_options: FormatOptions::default(),
3488            },
3489        );
3490        assert_eq!(
3491            "Cast error: value of 244 is out of range Int8".to_string(),
3492            casted_array.unwrap_err().to_string()
3493        );
3494
3495        let casted_array = cast_with_options(
3496            &array,
3497            &DataType::Int8,
3498            &CastOptions {
3499                safe: true,
3500                format_options: FormatOptions::default(),
3501            },
3502        );
3503        assert!(casted_array.is_ok());
3504        assert!(casted_array.unwrap().is_null(0));
3505
3506        // loss the precision: convert decimal to f32、f64
3507        // f32
3508        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3509        let value_array: Vec<Option<i128>> = vec![
3510            Some(125),
3511            Some(225),
3512            Some(325),
3513            None,
3514            Some(525),
3515            Some(112345678),
3516            Some(112345679),
3517        ];
3518        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3519        generate_cast_test_case!(
3520            &array,
3521            Float32Array,
3522            &DataType::Float32,
3523            vec![
3524                Some(1.25_f32),
3525                Some(2.25_f32),
3526                Some(3.25_f32),
3527                None,
3528                Some(5.25_f32),
3529                Some(1_123_456.7_f32),
3530                Some(1_123_456.7_f32)
3531            ]
3532        );
3533
3534        // f64
3535        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3536        let value_array: Vec<Option<i128>> = vec![
3537            Some(125),
3538            Some(225),
3539            Some(325),
3540            None,
3541            Some(525),
3542            Some(112345678901234568),
3543            Some(112345678901234560),
3544        ];
3545        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3546        generate_cast_test_case!(
3547            &array,
3548            Float64Array,
3549            &DataType::Float64,
3550            vec![
3551                Some(1.25_f64),
3552                Some(2.25_f64),
3553                Some(3.25_f64),
3554                None,
3555                Some(5.25_f64),
3556                Some(1_123_456_789_012_345.6_f64),
3557                Some(1_123_456_789_012_345.6_f64),
3558            ]
3559        );
3560    }
3561
3562    #[test]
3563    #[cfg_attr(miri, ignore)] // Takes too long
3564    fn test_cast_decimal256_to_numeric() {
3565        let value_array: Vec<Option<i256>> = vec![
3566            Some(i256::from_i128(125)),
3567            Some(i256::from_i128(225)),
3568            Some(i256::from_i128(325)),
3569            None,
3570            Some(i256::from_i128(525)),
3571        ];
3572        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3573        // u8
3574        generate_cast_test_case!(
3575            &array,
3576            UInt8Array,
3577            &DataType::UInt8,
3578            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3579        );
3580        // u16
3581        generate_cast_test_case!(
3582            &array,
3583            UInt16Array,
3584            &DataType::UInt16,
3585            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3586        );
3587        // u32
3588        generate_cast_test_case!(
3589            &array,
3590            UInt32Array,
3591            &DataType::UInt32,
3592            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3593        );
3594        // u64
3595        generate_cast_test_case!(
3596            &array,
3597            UInt64Array,
3598            &DataType::UInt64,
3599            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3600        );
3601        // i8
3602        generate_cast_test_case!(
3603            &array,
3604            Int8Array,
3605            &DataType::Int8,
3606            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3607        );
3608        // i16
3609        generate_cast_test_case!(
3610            &array,
3611            Int16Array,
3612            &DataType::Int16,
3613            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3614        );
3615        // i32
3616        generate_cast_test_case!(
3617            &array,
3618            Int32Array,
3619            &DataType::Int32,
3620            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3621        );
3622        // i64
3623        generate_cast_test_case!(
3624            &array,
3625            Int64Array,
3626            &DataType::Int64,
3627            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3628        );
3629        // f16
3630        generate_cast_test_case!(
3631            &array,
3632            Float16Array,
3633            &DataType::Float16,
3634            vec![
3635                Some(f16::from_f32(1.25)),
3636                Some(f16::from_f32(2.25)),
3637                Some(f16::from_f32(3.25)),
3638                None,
3639                Some(f16::from_f32(5.25))
3640            ]
3641        );
3642        // f32
3643        generate_cast_test_case!(
3644            &array,
3645            Float32Array,
3646            &DataType::Float32,
3647            vec![
3648                Some(1.25_f32),
3649                Some(2.25_f32),
3650                Some(3.25_f32),
3651                None,
3652                Some(5.25_f32)
3653            ]
3654        );
3655        // f64
3656        generate_cast_test_case!(
3657            &array,
3658            Float64Array,
3659            &DataType::Float64,
3660            vec![
3661                Some(1.25_f64),
3662                Some(2.25_f64),
3663                Some(3.25_f64),
3664                None,
3665                Some(5.25_f64)
3666            ]
3667        );
3668
3669        // overflow test: out of range of max i8
3670        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3671        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3672        let casted_array = cast_with_options(
3673            &array,
3674            &DataType::Int8,
3675            &CastOptions {
3676                safe: false,
3677                format_options: FormatOptions::default(),
3678            },
3679        );
3680        assert_eq!(
3681            "Cast error: value of 244 is out of range Int8".to_string(),
3682            casted_array.unwrap_err().to_string()
3683        );
3684
3685        let casted_array = cast_with_options(
3686            &array,
3687            &DataType::Int8,
3688            &CastOptions {
3689                safe: true,
3690                format_options: FormatOptions::default(),
3691            },
3692        );
3693        assert!(casted_array.is_ok());
3694        assert!(casted_array.unwrap().is_null(0));
3695
3696        // overflow test: values whose low 64 bits fit the target type
3697        // https://github.com/apache/arrow-rs/issues/10855
3698        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128((1i128 << 64) + 5))];
3699        let array = create_decimal256_array(value_array, 76, 0).unwrap();
3700        let casted_array = cast_with_options(
3701            &array,
3702            &DataType::Int64,
3703            &CastOptions {
3704                safe: false,
3705                format_options: FormatOptions::default(),
3706            },
3707        );
3708        assert_eq!(
3709            "Cast error: value of 18446744073709551621 is out of range Int64".to_string(),
3710            casted_array.unwrap_err().to_string()
3711        );
3712
3713        let casted_array = cast_with_options(
3714            &array,
3715            &DataType::Int64,
3716            &CastOptions {
3717                safe: true,
3718                format_options: FormatOptions::default(),
3719            },
3720        );
3721        assert!(casted_array.is_ok());
3722        assert!(casted_array.unwrap().is_null(0));
3723
3724        // loss the precision: convert decimal to f32、f64
3725        // f32
3726        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3727        let value_array: Vec<Option<i256>> = vec![
3728            Some(i256::from_i128(125)),
3729            Some(i256::from_i128(225)),
3730            Some(i256::from_i128(325)),
3731            None,
3732            Some(i256::from_i128(525)),
3733            Some(i256::from_i128(112345678)),
3734            Some(i256::from_i128(112345679)),
3735        ];
3736        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3737        generate_cast_test_case!(
3738            &array,
3739            Float32Array,
3740            &DataType::Float32,
3741            vec![
3742                Some(1.25_f32),
3743                Some(2.25_f32),
3744                Some(3.25_f32),
3745                None,
3746                Some(5.25_f32),
3747                Some(1_123_456.7_f32),
3748                Some(1_123_456.7_f32)
3749            ]
3750        );
3751
3752        // f64
3753        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3754        let value_array: Vec<Option<i256>> = vec![
3755            Some(i256::from_i128(125)),
3756            Some(i256::from_i128(225)),
3757            Some(i256::from_i128(325)),
3758            None,
3759            Some(i256::from_i128(525)),
3760            Some(i256::from_i128(112345678901234568)),
3761            Some(i256::from_i128(112345678901234560)),
3762        ];
3763        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3764        generate_cast_test_case!(
3765            &array,
3766            Float64Array,
3767            &DataType::Float64,
3768            vec![
3769                Some(1.25_f64),
3770                Some(2.25_f64),
3771                Some(3.25_f64),
3772                None,
3773                Some(5.25_f64),
3774                Some(1_123_456_789_012_345.6_f64),
3775                Some(1_123_456_789_012_345.6_f64),
3776            ]
3777        );
3778    }
3779
3780    #[test]
3781    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
3782    fn test_cast_decimal128_to_float16_overflow() {
3783        let array = create_decimal128_array(
3784            vec![
3785                Some(6_550_400_i128),
3786                Some(100_000_000_i128),
3787                Some(-100_000_000_i128),
3788                None,
3789            ],
3790            10,
3791            2,
3792        )
3793        .unwrap();
3794
3795        generate_cast_test_case!(
3796            &array,
3797            Float16Array,
3798            &DataType::Float16,
3799            vec![
3800                Some(f16::from_f64(65504.0)),
3801                Some(f16::INFINITY),
3802                Some(f16::NEG_INFINITY),
3803                None
3804            ]
3805        );
3806    }
3807
3808    #[test]
3809    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
3810    fn test_cast_decimal256_to_float16_overflow() {
3811        let array = create_decimal256_array(
3812            vec![
3813                Some(i256::from_i128(6_550_400_i128)),
3814                Some(i256::from_i128(100_000_000_i128)),
3815                Some(i256::from_i128(-100_000_000_i128)),
3816                None,
3817            ],
3818            10,
3819            2,
3820        )
3821        .unwrap();
3822
3823        generate_cast_test_case!(
3824            &array,
3825            Float16Array,
3826            &DataType::Float16,
3827            vec![
3828                Some(f16::from_f64(65504.0)),
3829                Some(f16::INFINITY),
3830                Some(f16::NEG_INFINITY),
3831                None
3832            ]
3833        );
3834    }
3835
3836    #[test]
3837    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
3838    fn test_cast_decimal_to_numeric_negative_scale() {
3839        let value_array: Vec<Option<i256>> = vec![
3840            Some(i256::from_i128(125)),
3841            Some(i256::from_i128(225)),
3842            Some(i256::from_i128(325)),
3843            None,
3844            Some(i256::from_i128(525)),
3845        ];
3846        let array = create_decimal256_array(value_array, 38, -1).unwrap();
3847
3848        generate_cast_test_case!(
3849            &array,
3850            Int64Array,
3851            &DataType::Int64,
3852            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
3853        );
3854
3855        let value_array: Vec<Option<i128>> = vec![Some(12), Some(-12), None];
3856        let array = create_decimal128_array(value_array, 10, -2).unwrap();
3857        generate_cast_test_case!(
3858            &array,
3859            Float16Array,
3860            &DataType::Float16,
3861            vec![
3862                Some(f16::from_f32(1200.0)),
3863                Some(f16::from_f32(-1200.0)),
3864                None
3865            ]
3866        );
3867
3868        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3869        let array = create_decimal32_array(value_array, 8, -2).unwrap();
3870        generate_cast_test_case!(
3871            &array,
3872            Int64Array,
3873            &DataType::Int64,
3874            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
3875        );
3876
3877        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
3878        let array = create_decimal32_array(value_array, 9, -9).unwrap();
3879        generate_cast_test_case!(
3880            &array,
3881            Int64Array,
3882            &DataType::Int64,
3883            vec![Some(2_000_000_000), Some(1_000_000_000), None]
3884        );
3885
3886        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3887        let array = create_decimal64_array(value_array, 18, -3).unwrap();
3888        generate_cast_test_case!(
3889            &array,
3890            Int64Array,
3891            &DataType::Int64,
3892            vec![
3893                Some(125_000),
3894                Some(225_000),
3895                Some(325_000),
3896                None,
3897                Some(525_000)
3898            ]
3899        );
3900
3901        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
3902        let array = create_decimal64_array(value_array, 18, -10).unwrap();
3903        generate_cast_test_case!(
3904            &array,
3905            Int64Array,
3906            &DataType::Int64,
3907            vec![Some(120_000_000_000), Some(340_000_000_000), None]
3908        );
3909
3910        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3911        let array = create_decimal128_array(value_array, 38, -4).unwrap();
3912        generate_cast_test_case!(
3913            &array,
3914            Int64Array,
3915            &DataType::Int64,
3916            vec![
3917                Some(1_250_000),
3918                Some(2_250_000),
3919                Some(3_250_000),
3920                None,
3921                Some(5_250_000)
3922            ]
3923        );
3924
3925        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
3926        let array = create_decimal128_array(value_array, 38, -18).unwrap();
3927        generate_cast_test_case!(
3928            &array,
3929            Int64Array,
3930            &DataType::Int64,
3931            vec![
3932                Some(9_000_000_000_000_000_000),
3933                Some(1_000_000_000_000_000_000),
3934                None
3935            ]
3936        );
3937
3938        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
3939        let casted_array = cast_with_options(
3940            &array,
3941            &DataType::Int64,
3942            &CastOptions {
3943                safe: false,
3944                format_options: FormatOptions::default(),
3945            },
3946        );
3947        assert_eq!(
3948            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
3949            casted_array.unwrap_err().to_string()
3950        );
3951
3952        let casted_array = cast_with_options(
3953            &array,
3954            &DataType::Int64,
3955            &CastOptions {
3956                safe: true,
3957                format_options: FormatOptions::default(),
3958            },
3959        );
3960        assert!(casted_array.is_ok());
3961        assert!(casted_array.unwrap().is_null(0));
3962
3963        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
3964        let casted_array = cast_with_options(
3965            &array,
3966            &DataType::Int8,
3967            &CastOptions {
3968                safe: false,
3969                format_options: FormatOptions::default(),
3970            },
3971        );
3972        assert_eq!(
3973            "Cast error: value of 130 is out of range Int8".to_string(),
3974            casted_array.unwrap_err().to_string()
3975        );
3976
3977        let casted_array = cast_with_options(
3978            &array,
3979            &DataType::Int8,
3980            &CastOptions {
3981                safe: true,
3982                format_options: FormatOptions::default(),
3983            },
3984        );
3985        assert!(casted_array.is_ok());
3986        assert!(casted_array.unwrap().is_null(0));
3987    }
3988
3989    #[test]
3990    fn test_cast_numeric_to_decimal128() {
3991        let decimal_type = DataType::Decimal128(38, 6);
3992        // u8, u16, u32, u64
3993        let input_arrays = vec![
3994            Arc::new(UInt8Array::from(vec![
3995                Some(1),
3996                Some(2),
3997                Some(3),
3998                None,
3999                Some(5),
4000            ])) as ArrayRef, // u8
4001            Arc::new(UInt16Array::from(vec![
4002                Some(1),
4003                Some(2),
4004                Some(3),
4005                None,
4006                Some(5),
4007            ])) as ArrayRef, // u16
4008            Arc::new(UInt32Array::from(vec![
4009                Some(1),
4010                Some(2),
4011                Some(3),
4012                None,
4013                Some(5),
4014            ])) as ArrayRef, // u32
4015            Arc::new(UInt64Array::from(vec![
4016                Some(1),
4017                Some(2),
4018                Some(3),
4019                None,
4020                Some(5),
4021            ])) as ArrayRef, // u64
4022        ];
4023
4024        for array in input_arrays {
4025            generate_cast_test_case!(
4026                &array,
4027                Decimal128Array,
4028                &decimal_type,
4029                vec![
4030                    Some(1000000_i128),
4031                    Some(2000000_i128),
4032                    Some(3000000_i128),
4033                    None,
4034                    Some(5000000_i128)
4035                ]
4036            );
4037        }
4038
4039        // i8, i16, i32, i64
4040        let input_arrays = vec![
4041            Arc::new(Int8Array::from(vec![
4042                Some(1),
4043                Some(2),
4044                Some(3),
4045                None,
4046                Some(5),
4047            ])) as ArrayRef, // i8
4048            Arc::new(Int16Array::from(vec![
4049                Some(1),
4050                Some(2),
4051                Some(3),
4052                None,
4053                Some(5),
4054            ])) as ArrayRef, // i16
4055            Arc::new(Int32Array::from(vec![
4056                Some(1),
4057                Some(2),
4058                Some(3),
4059                None,
4060                Some(5),
4061            ])) as ArrayRef, // i32
4062            Arc::new(Int64Array::from(vec![
4063                Some(1),
4064                Some(2),
4065                Some(3),
4066                None,
4067                Some(5),
4068            ])) as ArrayRef, // i64
4069        ];
4070        for array in input_arrays {
4071            generate_cast_test_case!(
4072                &array,
4073                Decimal128Array,
4074                &decimal_type,
4075                vec![
4076                    Some(1000000_i128),
4077                    Some(2000000_i128),
4078                    Some(3000000_i128),
4079                    None,
4080                    Some(5000000_i128)
4081                ]
4082            );
4083        }
4084
4085        // test u8 to decimal type with overflow the result type
4086        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4087        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4088        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4089        assert!(casted_array.is_ok());
4090        let array = casted_array.unwrap();
4091        let array: &Decimal128Array = array.as_primitive();
4092        assert!(array.is_null(4));
4093
4094        // test i8 to decimal type with overflow the result type
4095        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4096        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4097        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4098        assert!(casted_array.is_ok());
4099        let array = casted_array.unwrap();
4100        let array: &Decimal128Array = array.as_primitive();
4101        assert!(array.is_null(4));
4102
4103        // test f32 to decimal type
4104        let array = Float32Array::from(vec![
4105            Some(1.1),
4106            Some(2.2),
4107            Some(4.4),
4108            None,
4109            Some(1.123_456_4), // round down
4110            Some(1.123_456_7), // round up
4111        ]);
4112        let array = Arc::new(array) as ArrayRef;
4113        generate_cast_test_case!(
4114            &array,
4115            Decimal128Array,
4116            &decimal_type,
4117            vec![
4118                Some(1100000_i128),
4119                Some(2200000_i128),
4120                Some(4400000_i128),
4121                None,
4122                Some(1123456_i128), // round down
4123                Some(1123457_i128), // round up
4124            ]
4125        );
4126
4127        // test f64 to decimal type
4128        let array = Float64Array::from(vec![
4129            Some(1.1),
4130            Some(2.2),
4131            Some(4.4),
4132            None,
4133            Some(1.123_456_489_123_4),     // round up
4134            Some(1.123_456_789_123_4),     // round up
4135            Some(1.123_456_489_012_345_6), // round down
4136            Some(1.123_456_789_012_345_6), // round up
4137        ]);
4138        generate_cast_test_case!(
4139            &array,
4140            Decimal128Array,
4141            &decimal_type,
4142            vec![
4143                Some(1100000_i128),
4144                Some(2200000_i128),
4145                Some(4400000_i128),
4146                None,
4147                Some(1123456_i128), // round down
4148                Some(1123457_i128), // round up
4149                Some(1123456_i128), // round down
4150                Some(1123457_i128), // round up
4151            ]
4152        );
4153    }
4154
4155    #[test]
4156    fn test_cast_numeric_to_decimal256() {
4157        let decimal_type = DataType::Decimal256(76, 6);
4158        // u8, u16, u32, u64
4159        let input_arrays = vec![
4160            Arc::new(UInt8Array::from(vec![
4161                Some(1),
4162                Some(2),
4163                Some(3),
4164                None,
4165                Some(5),
4166            ])) as ArrayRef, // u8
4167            Arc::new(UInt16Array::from(vec![
4168                Some(1),
4169                Some(2),
4170                Some(3),
4171                None,
4172                Some(5),
4173            ])) as ArrayRef, // u16
4174            Arc::new(UInt32Array::from(vec![
4175                Some(1),
4176                Some(2),
4177                Some(3),
4178                None,
4179                Some(5),
4180            ])) as ArrayRef, // u32
4181            Arc::new(UInt64Array::from(vec![
4182                Some(1),
4183                Some(2),
4184                Some(3),
4185                None,
4186                Some(5),
4187            ])) as ArrayRef, // u64
4188        ];
4189
4190        for array in input_arrays {
4191            generate_cast_test_case!(
4192                &array,
4193                Decimal256Array,
4194                &decimal_type,
4195                vec![
4196                    Some(i256::from_i128(1000000_i128)),
4197                    Some(i256::from_i128(2000000_i128)),
4198                    Some(i256::from_i128(3000000_i128)),
4199                    None,
4200                    Some(i256::from_i128(5000000_i128))
4201                ]
4202            );
4203        }
4204
4205        // i8, i16, i32, i64
4206        let input_arrays = vec![
4207            Arc::new(Int8Array::from(vec![
4208                Some(1),
4209                Some(2),
4210                Some(3),
4211                None,
4212                Some(5),
4213            ])) as ArrayRef, // i8
4214            Arc::new(Int16Array::from(vec![
4215                Some(1),
4216                Some(2),
4217                Some(3),
4218                None,
4219                Some(5),
4220            ])) as ArrayRef, // i16
4221            Arc::new(Int32Array::from(vec![
4222                Some(1),
4223                Some(2),
4224                Some(3),
4225                None,
4226                Some(5),
4227            ])) as ArrayRef, // i32
4228            Arc::new(Int64Array::from(vec![
4229                Some(1),
4230                Some(2),
4231                Some(3),
4232                None,
4233                Some(5),
4234            ])) as ArrayRef, // i64
4235        ];
4236        for array in input_arrays {
4237            generate_cast_test_case!(
4238                &array,
4239                Decimal256Array,
4240                &decimal_type,
4241                vec![
4242                    Some(i256::from_i128(1000000_i128)),
4243                    Some(i256::from_i128(2000000_i128)),
4244                    Some(i256::from_i128(3000000_i128)),
4245                    None,
4246                    Some(i256::from_i128(5000000_i128))
4247                ]
4248            );
4249        }
4250
4251        // test i8 to decimal type with overflow the result type
4252        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4253        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4254        let array = Arc::new(array) as ArrayRef;
4255        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4256        assert!(casted_array.is_ok());
4257        let array = casted_array.unwrap();
4258        let array: &Decimal256Array = array.as_primitive();
4259        assert!(array.is_null(4));
4260
4261        // test f32 to decimal type
4262        let array = Float32Array::from(vec![
4263            Some(1.1),
4264            Some(2.2),
4265            Some(4.4),
4266            None,
4267            Some(1.123_456_4), // round down
4268            Some(1.123_456_7), // round up
4269        ]);
4270        generate_cast_test_case!(
4271            &array,
4272            Decimal256Array,
4273            &decimal_type,
4274            vec![
4275                Some(i256::from_i128(1100000_i128)),
4276                Some(i256::from_i128(2200000_i128)),
4277                Some(i256::from_i128(4400000_i128)),
4278                None,
4279                Some(i256::from_i128(1123456_i128)), // round down
4280                Some(i256::from_i128(1123457_i128)), // round up
4281            ]
4282        );
4283
4284        // test f64 to decimal type
4285        let array = Float64Array::from(vec![
4286            Some(1.1),
4287            Some(2.2),
4288            Some(4.4),
4289            None,
4290            Some(1.123_456_489_123_4),     // round down
4291            Some(1.123_456_789_123_4),     // round up
4292            Some(1.123_456_489_012_345_6), // round down
4293            Some(1.123_456_789_012_345_6), // round up
4294        ]);
4295        generate_cast_test_case!(
4296            &array,
4297            Decimal256Array,
4298            &decimal_type,
4299            vec![
4300                Some(i256::from_i128(1100000_i128)),
4301                Some(i256::from_i128(2200000_i128)),
4302                Some(i256::from_i128(4400000_i128)),
4303                None,
4304                Some(i256::from_i128(1123456_i128)), // round down
4305                Some(i256::from_i128(1123457_i128)), // round up
4306                Some(i256::from_i128(1123456_i128)), // round down
4307                Some(i256::from_i128(1123457_i128)), // round up
4308            ]
4309        );
4310    }
4311
4312    /// Checks the fast-path classification against `num_cast` for every pair of
4313    /// numeric types. `num_cast` fails only on values outside the target's range,
4314    /// so each type's extremes decide, plus NaN for floats.
4315    #[test]
4316    fn test_is_infallible_numeric_cast() {
4317        macro_rules! check {
4318            ($from:ty => $values:expr, [$($to:ty),*]) => {$(
4319                let infallible = is_infallible_numeric_cast(
4320                    <<$from as ArrowPrimitiveType>::Native as NumericNative>::KIND,
4321                    <<$to as ArrowPrimitiveType>::Native as NumericNative>::KIND,
4322                );
4323                let succeeds = $values
4324                    .iter()
4325                    .all(|&v| num_cast::<_, <$to as ArrowPrimitiveType>::Native>(v).is_some());
4326                assert_eq!(
4327                    infallible,
4328                    succeeds,
4329                    "{} to {}",
4330                    <$from as ArrowPrimitiveType>::DATA_TYPE,
4331                    <$to as ArrowPrimitiveType>::DATA_TYPE
4332                );
4333            )*};
4334        }
4335        macro_rules! check_all {
4336            ($($from:ty => $values:expr),* $(,)?) => {$(
4337                check!($from => $values, [
4338                    UInt8Type, UInt16Type, UInt32Type, UInt64Type, Int8Type, Int16Type,
4339                    Int32Type, Int64Type, Float16Type, Float32Type, Float64Type
4340                ]);
4341            )*};
4342        }
4343        check_all! {
4344            UInt8Type => [u8::MIN, u8::MAX],
4345            UInt16Type => [u16::MIN, u16::MAX],
4346            UInt32Type => [u32::MIN, u32::MAX],
4347            UInt64Type => [u64::MIN, u64::MAX],
4348            Int8Type => [i8::MIN, i8::MAX],
4349            Int16Type => [i16::MIN, i16::MAX],
4350            Int32Type => [i32::MIN, i32::MAX],
4351            Int64Type => [i64::MIN, i64::MAX],
4352            Float16Type => [f16::MIN, f16::MAX, f16::NAN],
4353            Float32Type => [f32::MIN, f32::MAX, f32::NAN],
4354            Float64Type => [f64::MIN, f64::MAX, f64::NAN],
4355        }
4356    }
4357
4358    #[test]
4359    fn test_cast_i32_to_f64() {
4360        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4361        let b = cast(&array, &DataType::Float64).unwrap();
4362        let c = b.as_primitive::<Float64Type>();
4363        assert_eq!(5.0, c.value(0));
4364        assert_eq!(6.0, c.value(1));
4365        assert_eq!(7.0, c.value(2));
4366        assert_eq!(8.0, c.value(3));
4367        assert_eq!(9.0, c.value(4));
4368    }
4369
4370    #[test]
4371    fn test_cast_i32_to_u8() {
4372        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4373        let b = cast(&array, &DataType::UInt8).unwrap();
4374        let c = b.as_primitive::<UInt8Type>();
4375        assert!(!c.is_valid(0));
4376        assert_eq!(6, c.value(1));
4377        assert!(!c.is_valid(2));
4378        assert_eq!(8, c.value(3));
4379        // overflows return None
4380        assert!(!c.is_valid(4));
4381    }
4382
4383    #[test]
4384    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4385    fn test_cast_int32_to_u8_with_error() {
4386        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4387        // overflow with the error
4388        let cast_option = CastOptions {
4389            safe: false,
4390            format_options: FormatOptions::default(),
4391        };
4392        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4393        assert!(result.is_err());
4394        result.unwrap();
4395    }
4396
4397    #[test]
4398    fn test_cast_i32_to_u8_sliced() {
4399        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4400        assert_eq!(0, array.offset());
4401        let array = array.slice(2, 3);
4402        let b = cast(&array, &DataType::UInt8).unwrap();
4403        assert_eq!(3, b.len());
4404        let c = b.as_primitive::<UInt8Type>();
4405        assert!(!c.is_valid(0));
4406        assert_eq!(8, c.value(1));
4407        // overflows return None
4408        assert!(!c.is_valid(2));
4409    }
4410
4411    #[test]
4412    fn test_cast_i32_to_i32() {
4413        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4414        let b = cast(&array, &DataType::Int32).unwrap();
4415        let c = b.as_primitive::<Int32Type>();
4416        assert_eq!(5, c.value(0));
4417        assert_eq!(6, c.value(1));
4418        assert_eq!(7, c.value(2));
4419        assert_eq!(8, c.value(3));
4420        assert_eq!(9, c.value(4));
4421    }
4422
4423    #[test]
4424    fn test_cast_int_to_utf8view() {
4425        let inputs = vec![
4426            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4427            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4428            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4429            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4430            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4431            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4432            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4433            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4434        ];
4435        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4436            None,
4437            Some("8"),
4438            Some("9"),
4439            Some("10"),
4440        ]));
4441
4442        for array in inputs {
4443            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4444            let arr = cast(&array, &DataType::Utf8View).unwrap();
4445            assert_eq!(expected.as_ref(), arr.as_ref());
4446        }
4447    }
4448
4449    #[test]
4450    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4451    fn test_cast_float_to_utf8view() {
4452        let inputs = vec![
4453            Arc::new(Float16Array::from(vec![
4454                Some(f16::from_f64(1.5)),
4455                Some(f16::from_f64(2.5)),
4456                None,
4457            ])) as ArrayRef,
4458            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4459            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4460        ];
4461
4462        let expected: ArrayRef =
4463            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4464
4465        for array in inputs {
4466            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4467            let arr = cast(&array, &DataType::Utf8View).unwrap();
4468            assert_eq!(expected.as_ref(), arr.as_ref());
4469        }
4470    }
4471
4472    #[test]
4473    fn test_cast_utf8_to_i32() {
4474        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4475        let b = cast(&array, &DataType::Int32).unwrap();
4476        let c = b.as_primitive::<Int32Type>();
4477        assert_eq!(5, c.value(0));
4478        assert_eq!(6, c.value(1));
4479        assert!(!c.is_valid(2));
4480        assert_eq!(8, c.value(3));
4481        assert!(!c.is_valid(4));
4482    }
4483
4484    #[test]
4485    fn test_cast_utf8view_to_i32() {
4486        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4487        let b = cast(&array, &DataType::Int32).unwrap();
4488        let c = b.as_primitive::<Int32Type>();
4489        assert_eq!(5, c.value(0));
4490        assert_eq!(6, c.value(1));
4491        assert!(!c.is_valid(2));
4492        assert_eq!(8, c.value(3));
4493        assert!(!c.is_valid(4));
4494    }
4495
4496    #[test]
4497    fn test_cast_utf8view_to_f32() {
4498        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4499        let b = cast(&array, &DataType::Float32).unwrap();
4500        let c = b.as_primitive::<Float32Type>();
4501        assert_eq!(3.0, c.value(0));
4502        assert_eq!(4.56, c.value(1));
4503        assert!(!c.is_valid(2));
4504        assert_eq!(8.9, c.value(3));
4505    }
4506
4507    #[test]
4508    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4509    fn test_cast_string_to_f16() {
4510        let arrays = [
4511            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4512            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4513            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4514        ];
4515        for array in arrays {
4516            let b = cast(&array, &DataType::Float16).unwrap();
4517            let c = b.as_primitive::<Float16Type>();
4518            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4519            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4520            assert!(!c.is_valid(2));
4521            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4522        }
4523    }
4524
4525    #[test]
4526    fn test_cast_utf8view_to_decimal128() {
4527        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4528        let arr = Arc::new(array) as ArrayRef;
4529        generate_cast_test_case!(
4530            &arr,
4531            Decimal128Array,
4532            &DataType::Decimal128(4, 2),
4533            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4534        );
4535    }
4536
4537    #[test]
4538    fn test_cast_with_options_utf8_to_i32() {
4539        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4540        let result = cast_with_options(
4541            &array,
4542            &DataType::Int32,
4543            &CastOptions {
4544                safe: false,
4545                format_options: FormatOptions::default(),
4546            },
4547        );
4548        match result {
4549            Ok(_) => panic!("expected error"),
4550            Err(e) => {
4551                assert!(
4552                    e.to_string()
4553                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4554                    "Error: {e}"
4555                )
4556            }
4557        }
4558    }
4559
4560    #[test]
4561    fn test_cast_utf8_to_bool() {
4562        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4563        let casted = cast(&strings, &DataType::Boolean).unwrap();
4564        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4565        assert_eq!(*as_boolean_array(&casted), expected);
4566    }
4567
4568    #[test]
4569    fn test_cast_utf8view_to_bool() {
4570        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4571        let casted = cast(&strings, &DataType::Boolean).unwrap();
4572        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4573        assert_eq!(*as_boolean_array(&casted), expected);
4574    }
4575
4576    #[test]
4577    fn test_cast_with_options_utf8_to_bool() {
4578        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4579        let casted = cast_with_options(
4580            &strings,
4581            &DataType::Boolean,
4582            &CastOptions {
4583                safe: false,
4584                format_options: FormatOptions::default(),
4585            },
4586        );
4587        match casted {
4588            Ok(_) => panic!("expected error"),
4589            Err(e) => {
4590                assert!(
4591                    e.to_string().contains(
4592                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4593                    )
4594                )
4595            }
4596        }
4597    }
4598
4599    #[test]
4600    fn test_cast_bool_to_i32() {
4601        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4602        let b = cast(&array, &DataType::Int32).unwrap();
4603        let c = b.as_primitive::<Int32Type>();
4604        assert_eq!(1, c.value(0));
4605        assert_eq!(0, c.value(1));
4606        assert!(!c.is_valid(2));
4607    }
4608
4609    #[test]
4610    fn test_cast_bool_numeric_sliced() {
4611        for nulls in [
4612            None,
4613            Some(NullBuffer::from(
4614                [false, true, true, false, true, true].repeat(12),
4615            )),
4616            Some(NullBuffer::new_null(72)),
4617        ] {
4618            // Include true values under nulls and slices crossing a bitmap word boundary.
4619            let booleans = BooleanArray::new(
4620                BooleanBuffer::from([true, false, true, false, true, false].repeat(12)),
4621                nulls.clone(),
4622            );
4623            let numbers = Int32Array::new([1, 0, 1, 0, 1, 0].repeat(12).into(), nulls);
4624            for data_type in [
4625                Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Float16, Float32, Float64,
4626            ] {
4627                let expected = cast(&numbers, &data_type).unwrap();
4628                for offset in [0, 3, 63, 72] {
4629                    let input = booleans.slice(offset, 72 - offset);
4630                    let expected = expected.slice(offset, 72 - offset);
4631                    let actual = cast(&input, &data_type).unwrap();
4632                    assert_eq!(actual.as_ref(), expected.as_ref());
4633
4634                    let actual = cast(expected.as_ref(), &Boolean).unwrap();
4635                    assert_eq!(actual.as_ref(), &input);
4636                }
4637            }
4638        }
4639    }
4640
4641    #[test]
4642    fn test_cast_float_to_bool_special_values() {
4643        let numbers = Float64Array::from(vec![
4644            0.0,
4645            -0.0,
4646            1.5,
4647            -1.5,
4648            f64::NAN,
4649            f64::INFINITY,
4650            f64::NEG_INFINITY,
4651        ]);
4652        let expected = BooleanArray::from(vec![false, false, true, true, true, true, true]);
4653        for data_type in [Float16, Float32, Float64] {
4654            let input = cast(&numbers, &data_type).unwrap();
4655            let actual = cast(input.as_ref(), &Boolean).unwrap();
4656            assert_eq!(actual.as_ref(), &expected);
4657        }
4658    }
4659
4660    #[test]
4661    fn test_cast_bool_to_string() {
4662        for nulls in [
4663            None,
4664            Some(NullBuffer::from([false, false, true, true].repeat(18))),
4665            Some(NullBuffer::new_null(72)),
4666        ] {
4667            // Include both Boolean values under nulls, an unaligned slice, and an empty slice.
4668            let array = BooleanArray::new(BooleanBuffer::from([true, false].repeat(36)), nulls);
4669            let expected: Vec<_> = ["true", "false"]
4670                .repeat(36)
4671                .into_iter()
4672                .enumerate()
4673                .map(|(i, value)| array.is_valid(i).then_some(value))
4674                .collect();
4675            for (offset, len) in [(0, 72), (3, 65), (72, 0)] {
4676                let input = array.slice(offset, len);
4677                for data_type in [Utf8, LargeUtf8, Utf8View] {
4678                    let actual = cast(&input, &data_type).unwrap();
4679                    let values: Vec<_> = match data_type {
4680                        Utf8 => actual.as_string::<i32>().iter().collect(),
4681                        LargeUtf8 => actual.as_string::<i64>().iter().collect(),
4682                        Utf8View => actual.as_string_view().iter().collect(),
4683                        _ => unreachable!(),
4684                    };
4685                    assert_eq!(values, expected[offset..offset + len]);
4686                }
4687            }
4688        }
4689    }
4690
4691    #[test]
4692    fn test_cast_bool_to_f64() {
4693        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4694        let b = cast(&array, &DataType::Float64).unwrap();
4695        let c = b.as_primitive::<Float64Type>();
4696        assert_eq!(1.0, c.value(0));
4697        assert_eq!(0.0, c.value(1));
4698        assert!(!c.is_valid(2));
4699    }
4700
4701    #[test]
4702    fn test_cast_integer_to_timestamp() {
4703        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4704        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4705
4706        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4707        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4708
4709        assert_eq!(&actual, &expected);
4710
4711        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4712        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4713
4714        assert_eq!(&actual, &expected);
4715
4716        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4717        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4718
4719        assert_eq!(&actual, &expected);
4720
4721        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4722        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4723
4724        assert_eq!(&actual, &expected);
4725
4726        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4727        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4728
4729        assert_eq!(&actual, &expected);
4730
4731        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4732        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4733
4734        assert_eq!(&actual, &expected);
4735
4736        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4737        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4738
4739        assert_eq!(&actual, &expected);
4740    }
4741
4742    #[test]
4743    fn test_cast_timestamp_to_integer() {
4744        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4745            .with_timezone("UTC".to_string());
4746        let expected = cast(&array, &DataType::Int64).unwrap();
4747
4748        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4749        assert_eq!(&actual, &expected);
4750
4751        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4752        assert_eq!(&actual, &expected);
4753
4754        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4755        assert_eq!(&actual, &expected);
4756
4757        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4758        assert_eq!(&actual, &expected);
4759
4760        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4761        assert_eq!(&actual, &expected);
4762
4763        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4764        assert_eq!(&actual, &expected);
4765
4766        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4767        assert_eq!(&actual, &expected);
4768    }
4769
4770    #[test]
4771    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4772    fn test_cast_floating_to_timestamp() {
4773        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4774        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4775
4776        let array = Float16Array::from(vec![
4777            Some(f16::from_f32(2.0)),
4778            Some(f16::from_f32(10.6)),
4779            None,
4780        ]);
4781        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4782
4783        assert_eq!(&actual, &expected);
4784
4785        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4786        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4787
4788        assert_eq!(&actual, &expected);
4789
4790        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4791        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4792
4793        assert_eq!(&actual, &expected);
4794    }
4795
4796    #[test]
4797    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4798    fn test_cast_timestamp_to_floating() {
4799        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4800            .with_timezone("UTC".to_string());
4801        let expected = cast(&array, &DataType::Int64).unwrap();
4802
4803        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4804        assert_eq!(&actual, &expected);
4805
4806        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4807        assert_eq!(&actual, &expected);
4808
4809        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4810        assert_eq!(&actual, &expected);
4811    }
4812
4813    #[test]
4814    fn test_cast_decimal_to_timestamp() {
4815        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4816        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4817
4818        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4819            .with_precision_and_scale(4, 2)
4820            .unwrap();
4821        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4822
4823        assert_eq!(&actual, &expected);
4824
4825        let array = Decimal256Array::from(vec![
4826            Some(i256::from_i128(2000)),
4827            Some(i256::from_i128(10000)),
4828            None,
4829        ])
4830        .with_precision_and_scale(5, 3)
4831        .unwrap();
4832        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4833
4834        assert_eq!(&actual, &expected);
4835    }
4836
4837    #[test]
4838    fn test_cast_timestamp_to_decimal() {
4839        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4840            .with_timezone("UTC".to_string());
4841        let expected = cast(&array, &DataType::Int64).unwrap();
4842
4843        let actual = cast(
4844            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4845            &DataType::Int64,
4846        )
4847        .unwrap();
4848        assert_eq!(&actual, &expected);
4849
4850        let actual = cast(
4851            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4852            &DataType::Int64,
4853        )
4854        .unwrap();
4855        assert_eq!(&actual, &expected);
4856    }
4857
4858    #[test]
4859    fn test_cast_date32_to_date64() {
4860        let a = Date32Array::from(vec![10000, 17890]);
4861        let array = Arc::new(a) as ArrayRef;
4862        let b = cast(&array, &DataType::Date64).unwrap();
4863        let c = b.as_primitive::<Date64Type>();
4864        assert_eq!(864000000000, c.value(0));
4865        assert_eq!(1545696000000, c.value(1));
4866    }
4867
4868    #[test]
4869    fn test_cast_date64_to_date32() {
4870        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4871        let array = Arc::new(a) as ArrayRef;
4872        let b = cast(&array, &DataType::Date32).unwrap();
4873        let c = b.as_primitive::<Date32Type>();
4874        assert_eq!(10000, c.value(0));
4875        assert_eq!(17890, c.value(1));
4876        assert!(c.is_null(2));
4877    }
4878
4879    #[test]
4880    fn test_cast_date64_to_date32_overflow() {
4881        let a = Date64Array::from(vec![i64::MAX]);
4882        let array = Arc::new(a) as ArrayRef;
4883
4884        let b = cast(&array, &DataType::Date32).unwrap();
4885        let c = b.as_primitive::<Date32Type>();
4886        assert!(c.is_null(0));
4887
4888        let options = CastOptions {
4889            safe: false,
4890            ..Default::default()
4891        };
4892        let err = cast_with_options(&array, &DataType::Date32, &options).unwrap_err();
4893        assert!(
4894            err.to_string().contains("Cannot cast Date64 value"),
4895            "{err}"
4896        );
4897    }
4898
4899    #[test]
4900    fn test_cast_string_to_integral_overflow() {
4901        let str = Arc::new(StringArray::from(vec![
4902            Some("123"),
4903            Some("-123"),
4904            Some("86374"),
4905            None,
4906        ])) as ArrayRef;
4907
4908        let options = CastOptions {
4909            safe: true,
4910            format_options: FormatOptions::default(),
4911        };
4912        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4913        let expected =
4914            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4915        assert_eq!(&res, &expected);
4916    }
4917
4918    #[test]
4919    fn test_cast_string_to_timestamp() {
4920        let a0 = Arc::new(StringViewArray::from(vec![
4921            Some("2020-09-08T12:00:00.123456789+00:00"),
4922            Some("Not a valid date"),
4923            None,
4924        ])) as ArrayRef;
4925        let a1 = Arc::new(StringArray::from(vec![
4926            Some("2020-09-08T12:00:00.123456789+00:00"),
4927            Some("Not a valid date"),
4928            None,
4929        ])) as ArrayRef;
4930        let a2 = Arc::new(LargeStringArray::from(vec![
4931            Some("2020-09-08T12:00:00.123456789+00:00"),
4932            Some("Not a valid date"),
4933            None,
4934        ])) as ArrayRef;
4935        for array in &[a0, a1, a2] {
4936            for time_unit in &[
4937                TimeUnit::Second,
4938                TimeUnit::Millisecond,
4939                TimeUnit::Microsecond,
4940                TimeUnit::Nanosecond,
4941            ] {
4942                let to_type = DataType::Timestamp(*time_unit, None);
4943                let b = cast(array, &to_type).unwrap();
4944
4945                match time_unit {
4946                    TimeUnit::Second => {
4947                        let c = b.as_primitive::<TimestampSecondType>();
4948                        assert_eq!(1599566400, c.value(0));
4949                        assert!(c.is_null(1));
4950                        assert!(c.is_null(2));
4951                    }
4952                    TimeUnit::Millisecond => {
4953                        let c = b
4954                            .as_any()
4955                            .downcast_ref::<TimestampMillisecondArray>()
4956                            .unwrap();
4957                        assert_eq!(1599566400123, c.value(0));
4958                        assert!(c.is_null(1));
4959                        assert!(c.is_null(2));
4960                    }
4961                    TimeUnit::Microsecond => {
4962                        let c = b
4963                            .as_any()
4964                            .downcast_ref::<TimestampMicrosecondArray>()
4965                            .unwrap();
4966                        assert_eq!(1599566400123456, c.value(0));
4967                        assert!(c.is_null(1));
4968                        assert!(c.is_null(2));
4969                    }
4970                    TimeUnit::Nanosecond => {
4971                        let c = b
4972                            .as_any()
4973                            .downcast_ref::<TimestampNanosecondArray>()
4974                            .unwrap();
4975                        assert_eq!(1599566400123456789, c.value(0));
4976                        assert!(c.is_null(1));
4977                        assert!(c.is_null(2));
4978                    }
4979                }
4980
4981                let options = CastOptions {
4982                    safe: false,
4983                    format_options: FormatOptions::default(),
4984                };
4985                let err = cast_with_options(array, &to_type, &options).unwrap_err();
4986                assert_eq!(
4987                    err.to_string(),
4988                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4989                );
4990            }
4991        }
4992    }
4993
4994    #[test]
4995    fn test_cast_string_to_timestamp_overflow() {
4996        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4997        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4998        let result = result.as_primitive::<TimestampSecondType>();
4999        assert_eq!(result.values(), &[247112596800]);
5000    }
5001
5002    #[test]
5003    fn test_cast_string_to_date32_with_surrounding_whitespace() {
5004        let array = StringArray::from(vec![" 2026-09-29 ", "2026-09-29"]);
5005
5006        let result = cast(&array, &DataType::Date32).unwrap();
5007        let result = result.as_primitive::<Date32Type>();
5008
5009        assert!(!result.is_null(0));
5010        assert_eq!(result.value(0), result.value(1));
5011    }
5012
5013    #[test]
5014    fn test_cast_string_to_date32() {
5015        let a0 = Arc::new(StringViewArray::from(vec![
5016            Some("2018-12-25"),
5017            Some("Not a valid date"),
5018            None,
5019        ])) as ArrayRef;
5020        let a1 = Arc::new(StringArray::from(vec![
5021            Some("2018-12-25"),
5022            Some("Not a valid date"),
5023            None,
5024        ])) as ArrayRef;
5025        let a2 = Arc::new(LargeStringArray::from(vec![
5026            Some("2018-12-25"),
5027            Some("Not a valid date"),
5028            None,
5029        ])) as ArrayRef;
5030        for array in &[a0, a1, a2] {
5031            let to_type = DataType::Date32;
5032            let b = cast(array, &to_type).unwrap();
5033            let c = b.as_primitive::<Date32Type>();
5034            assert_eq!(17890, c.value(0));
5035            assert!(c.is_null(1));
5036            assert!(c.is_null(2));
5037
5038            let options = CastOptions {
5039                safe: false,
5040                format_options: FormatOptions::default(),
5041            };
5042            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5043            assert_eq!(
5044                err.to_string(),
5045                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5046            );
5047        }
5048    }
5049
5050    #[test]
5051    fn test_cast_string_with_large_date_to_date32() {
5052        let array = Arc::new(StringArray::from(vec![
5053            Some("+10999-12-31"),
5054            Some("-0010-02-28"),
5055            Some("0010-02-28"),
5056            Some("0000-01-01"),
5057            Some("-0000-01-01"),
5058            Some("-0001-01-01"),
5059        ])) as ArrayRef;
5060        let to_type = DataType::Date32;
5061        let options = CastOptions {
5062            safe: false,
5063            format_options: FormatOptions::default(),
5064        };
5065        let b = cast_with_options(&array, &to_type, &options).unwrap();
5066        let c = b.as_primitive::<Date32Type>();
5067        assert_eq!(3298139, c.value(0)); // 10999-12-31
5068        assert_eq!(-723122, c.value(1)); // -0010-02-28
5069        assert_eq!(-715817, c.value(2)); // 0010-02-28
5070        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5071        assert_eq!(-719528, c.value(3)); // 0000-01-01
5072        assert_eq!(-719528, c.value(4)); // -0000-01-01
5073        assert_eq!(-719893, c.value(5)); // -0001-01-01
5074    }
5075
5076    #[test]
5077    fn test_cast_invalid_string_with_large_date_to_date32() {
5078        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5079        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5080        let to_type = DataType::Date32;
5081        let options = CastOptions {
5082            safe: false,
5083            format_options: FormatOptions::default(),
5084        };
5085        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5086        assert_eq!(
5087            err.to_string(),
5088            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5089        );
5090    }
5091
5092    #[test]
5093    fn test_cast_string_format_yyyymmdd_to_date32() {
5094        let a0 = Arc::new(StringViewArray::from(vec![
5095            Some("2020-12-25"),
5096            Some("20201117"),
5097        ])) as ArrayRef;
5098        let a1 = Arc::new(StringArray::from(vec![
5099            Some("2020-12-25"),
5100            Some("20201117"),
5101        ])) as ArrayRef;
5102        let a2 = Arc::new(LargeStringArray::from(vec![
5103            Some("2020-12-25"),
5104            Some("20201117"),
5105        ])) as ArrayRef;
5106
5107        for array in &[a0, a1, a2] {
5108            let to_type = DataType::Date32;
5109            let options = CastOptions {
5110                safe: false,
5111                format_options: FormatOptions::default(),
5112            };
5113            let result = cast_with_options(&array, &to_type, &options).unwrap();
5114            let c = result.as_primitive::<Date32Type>();
5115            assert_eq!(
5116                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5117                c.value_as_date(0)
5118            );
5119            assert_eq!(
5120                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5121                c.value_as_date(1)
5122            );
5123        }
5124    }
5125
5126    #[test]
5127    fn test_cast_string_to_time32second() {
5128        let a0 = Arc::new(StringViewArray::from(vec![
5129            Some("08:08:35.091323414"),
5130            Some("08:08:60.091323414"), // leap second
5131            Some("08:08:61.091323414"), // not valid
5132            Some("Not a valid time"),
5133            None,
5134        ])) as ArrayRef;
5135        let a1 = Arc::new(StringArray::from(vec![
5136            Some("08:08:35.091323414"),
5137            Some("08:08:60.091323414"), // leap second
5138            Some("08:08:61.091323414"), // not valid
5139            Some("Not a valid time"),
5140            None,
5141        ])) as ArrayRef;
5142        let a2 = Arc::new(LargeStringArray::from(vec![
5143            Some("08:08:35.091323414"),
5144            Some("08:08:60.091323414"), // leap second
5145            Some("08:08:61.091323414"), // not valid
5146            Some("Not a valid time"),
5147            None,
5148        ])) as ArrayRef;
5149        for array in &[a0, a1, a2] {
5150            let to_type = DataType::Time32(TimeUnit::Second);
5151            let b = cast(array, &to_type).unwrap();
5152            let c = b.as_primitive::<Time32SecondType>();
5153            assert_eq!(29315, c.value(0));
5154            assert_eq!(29340, c.value(1));
5155            assert!(c.is_null(2));
5156            assert!(c.is_null(3));
5157            assert!(c.is_null(4));
5158
5159            let options = CastOptions {
5160                safe: false,
5161                format_options: FormatOptions::default(),
5162            };
5163            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5164            assert_eq!(
5165                err.to_string(),
5166                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5167            );
5168        }
5169    }
5170
5171    #[test]
5172    fn test_cast_string_to_time32millisecond() {
5173        let a0 = Arc::new(StringViewArray::from(vec![
5174            Some("08:08:35.091323414"),
5175            Some("08:08:60.091323414"), // leap second
5176            Some("08:08:61.091323414"), // not valid
5177            Some("Not a valid time"),
5178            None,
5179        ])) as ArrayRef;
5180        let a1 = Arc::new(StringArray::from(vec![
5181            Some("08:08:35.091323414"),
5182            Some("08:08:60.091323414"), // leap second
5183            Some("08:08:61.091323414"), // not valid
5184            Some("Not a valid time"),
5185            None,
5186        ])) as ArrayRef;
5187        let a2 = Arc::new(LargeStringArray::from(vec![
5188            Some("08:08:35.091323414"),
5189            Some("08:08:60.091323414"), // leap second
5190            Some("08:08:61.091323414"), // not valid
5191            Some("Not a valid time"),
5192            None,
5193        ])) as ArrayRef;
5194        for array in &[a0, a1, a2] {
5195            let to_type = DataType::Time32(TimeUnit::Millisecond);
5196            let b = cast(array, &to_type).unwrap();
5197            let c = b.as_primitive::<Time32MillisecondType>();
5198            assert_eq!(29315091, c.value(0));
5199            assert_eq!(29340091, c.value(1));
5200            assert!(c.is_null(2));
5201            assert!(c.is_null(3));
5202            assert!(c.is_null(4));
5203
5204            let options = CastOptions {
5205                safe: false,
5206                format_options: FormatOptions::default(),
5207            };
5208            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5209            assert_eq!(
5210                err.to_string(),
5211                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5212            );
5213        }
5214    }
5215
5216    #[test]
5217    fn test_cast_string_to_time64microsecond() {
5218        let a0 = Arc::new(StringViewArray::from(vec![
5219            Some("08:08:35.091323414"),
5220            Some("Not a valid time"),
5221            None,
5222        ])) as ArrayRef;
5223        let a1 = Arc::new(StringArray::from(vec![
5224            Some("08:08:35.091323414"),
5225            Some("Not a valid time"),
5226            None,
5227        ])) as ArrayRef;
5228        let a2 = Arc::new(LargeStringArray::from(vec![
5229            Some("08:08:35.091323414"),
5230            Some("Not a valid time"),
5231            None,
5232        ])) as ArrayRef;
5233        for array in &[a0, a1, a2] {
5234            let to_type = DataType::Time64(TimeUnit::Microsecond);
5235            let b = cast(array, &to_type).unwrap();
5236            let c = b.as_primitive::<Time64MicrosecondType>();
5237            assert_eq!(29315091323, c.value(0));
5238            assert!(c.is_null(1));
5239            assert!(c.is_null(2));
5240
5241            let options = CastOptions {
5242                safe: false,
5243                format_options: FormatOptions::default(),
5244            };
5245            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5246            assert_eq!(
5247                err.to_string(),
5248                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5249            );
5250        }
5251    }
5252
5253    #[test]
5254    fn test_cast_string_to_time64nanosecond() {
5255        let a0 = Arc::new(StringViewArray::from(vec![
5256            Some("08:08:35.091323414"),
5257            Some("Not a valid time"),
5258            None,
5259        ])) as ArrayRef;
5260        let a1 = Arc::new(StringArray::from(vec![
5261            Some("08:08:35.091323414"),
5262            Some("Not a valid time"),
5263            None,
5264        ])) as ArrayRef;
5265        let a2 = Arc::new(LargeStringArray::from(vec![
5266            Some("08:08:35.091323414"),
5267            Some("Not a valid time"),
5268            None,
5269        ])) as ArrayRef;
5270        for array in &[a0, a1, a2] {
5271            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5272            let b = cast(array, &to_type).unwrap();
5273            let c = b.as_primitive::<Time64NanosecondType>();
5274            assert_eq!(29315091323414, c.value(0));
5275            assert!(c.is_null(1));
5276            assert!(c.is_null(2));
5277
5278            let options = CastOptions {
5279                safe: false,
5280                format_options: FormatOptions::default(),
5281            };
5282            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5283            assert_eq!(
5284                err.to_string(),
5285                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5286            );
5287        }
5288    }
5289
5290    #[test]
5291    fn test_cast_string_to_date64() {
5292        let a0 = Arc::new(StringViewArray::from(vec![
5293            Some("2020-09-08T12:00:00"),
5294            Some("Not a valid date"),
5295            None,
5296        ])) as ArrayRef;
5297        let a1 = Arc::new(StringArray::from(vec![
5298            Some("2020-09-08T12:00:00"),
5299            Some("Not a valid date"),
5300            None,
5301        ])) as ArrayRef;
5302        let a2 = Arc::new(LargeStringArray::from(vec![
5303            Some("2020-09-08T12:00:00"),
5304            Some("Not a valid date"),
5305            None,
5306        ])) as ArrayRef;
5307        for array in &[a0, a1, a2] {
5308            let to_type = DataType::Date64;
5309            let b = cast(array, &to_type).unwrap();
5310            let c = b.as_primitive::<Date64Type>();
5311            assert_eq!(1599566400000, c.value(0));
5312            assert!(c.is_null(1));
5313            assert!(c.is_null(2));
5314
5315            let options = CastOptions {
5316                safe: false,
5317                format_options: FormatOptions::default(),
5318            };
5319            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5320            assert_eq!(
5321                err.to_string(),
5322                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5323            );
5324        }
5325    }
5326
5327    macro_rules! test_safe_string_to_interval {
5328        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5329            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5330
5331            let options = CastOptions {
5332                safe: true,
5333                format_options: FormatOptions::default(),
5334            };
5335
5336            let target_interval_array = cast_with_options(
5337                &source_string_array.clone(),
5338                &DataType::Interval($interval_unit),
5339                &options,
5340            )
5341            .unwrap()
5342            .as_any()
5343            .downcast_ref::<$array_ty>()
5344            .unwrap()
5345            .clone() as $array_ty;
5346
5347            let target_string_array =
5348                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5349                    .unwrap()
5350                    .as_any()
5351                    .downcast_ref::<StringArray>()
5352                    .unwrap()
5353                    .clone();
5354
5355            let expect_string_array = StringArray::from($expect_vec);
5356
5357            assert_eq!(target_string_array, expect_string_array);
5358
5359            let target_large_string_array =
5360                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5361                    .unwrap()
5362                    .as_any()
5363                    .downcast_ref::<LargeStringArray>()
5364                    .unwrap()
5365                    .clone();
5366
5367            let expect_large_string_array = LargeStringArray::from($expect_vec);
5368
5369            assert_eq!(target_large_string_array, expect_large_string_array);
5370        };
5371    }
5372
5373    #[test]
5374    fn test_cast_string_to_interval_year_month() {
5375        test_safe_string_to_interval!(
5376            vec![
5377                Some("1 year 1 month"),
5378                Some("1.5 years 13 month"),
5379                Some("30 days"),
5380                Some("31 days"),
5381                Some("2 months 31 days"),
5382                Some("2 months 31 days 1 second"),
5383                Some("foobar"),
5384            ],
5385            IntervalUnit::YearMonth,
5386            IntervalYearMonthArray,
5387            vec![
5388                Some("1 years 1 mons"),
5389                Some("2 years 7 mons"),
5390                None,
5391                None,
5392                None,
5393                None,
5394                None,
5395            ]
5396        );
5397    }
5398
5399    #[test]
5400    fn test_cast_string_to_interval_day_time() {
5401        test_safe_string_to_interval!(
5402            vec![
5403                Some("1 year 1 month"),
5404                Some("1.5 years 13 month"),
5405                Some("30 days"),
5406                Some("1 day 2 second 3.5 milliseconds"),
5407                Some("foobar"),
5408            ],
5409            IntervalUnit::DayTime,
5410            IntervalDayTimeArray,
5411            vec![
5412                Some("390 days"),
5413                Some("930 days"),
5414                Some("30 days"),
5415                None,
5416                None,
5417            ]
5418        );
5419    }
5420
5421    #[test]
5422    fn test_cast_string_to_interval_month_day_nano() {
5423        test_safe_string_to_interval!(
5424            vec![
5425                Some("1 year 1 month 1 day"),
5426                None,
5427                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5428                Some("3 days"),
5429                Some("8 seconds"),
5430                None,
5431                Some("1 day 29800 milliseconds"),
5432                Some("3 months 1 second"),
5433                Some("6 minutes 120 second"),
5434                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5435                Some("foobar"),
5436            ],
5437            IntervalUnit::MonthDayNano,
5438            IntervalMonthDayNanoArray,
5439            vec![
5440                Some("13 mons 1 days"),
5441                None,
5442                Some("31 mons 35 days 0.001400000 secs"),
5443                Some("3 days"),
5444                Some("8.000000000 secs"),
5445                None,
5446                Some("1 days 29.800000000 secs"),
5447                Some("3 mons 1.000000000 secs"),
5448                Some("8 mins"),
5449                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5450                None,
5451            ]
5452        );
5453    }
5454
5455    macro_rules! test_unsafe_string_to_interval_err {
5456        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5457            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5458            let options = CastOptions {
5459                safe: false,
5460                format_options: FormatOptions::default(),
5461            };
5462            let arrow_err = cast_with_options(
5463                &string_array.clone(),
5464                &DataType::Interval($interval_unit),
5465                &options,
5466            )
5467            .unwrap_err();
5468            assert_eq!($error_msg, arrow_err.to_string());
5469        };
5470    }
5471
5472    #[test]
5473    fn test_cast_string_to_interval_err() {
5474        test_unsafe_string_to_interval_err!(
5475            vec![Some("foobar")],
5476            IntervalUnit::YearMonth,
5477            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5478        );
5479        test_unsafe_string_to_interval_err!(
5480            vec![Some("foobar")],
5481            IntervalUnit::DayTime,
5482            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5483        );
5484        test_unsafe_string_to_interval_err!(
5485            vec![Some("foobar")],
5486            IntervalUnit::MonthDayNano,
5487            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5488        );
5489        test_unsafe_string_to_interval_err!(
5490            vec![Some("2 months 31 days 1 second")],
5491            IntervalUnit::YearMonth,
5492            "Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."
5493        );
5494        test_unsafe_string_to_interval_err!(
5495            vec![Some("1 day 1.5 milliseconds")],
5496            IntervalUnit::DayTime,
5497            "Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"
5498        );
5499
5500        // overflow
5501        test_unsafe_string_to_interval_err!(
5502            vec![Some(format!(
5503                "{} century {} year {} month",
5504                i64::MAX - 2,
5505                i64::MAX - 2,
5506                i64::MAX - 2
5507            ))],
5508            IntervalUnit::DayTime,
5509            format!(
5510                "Arithmetic overflow: Overflow happened on: {} * 100",
5511                i64::MAX - 2
5512            )
5513        );
5514        test_unsafe_string_to_interval_err!(
5515            vec![Some(format!(
5516                "{} year {} month {} day",
5517                i64::MAX - 2,
5518                i64::MAX - 2,
5519                i64::MAX - 2
5520            ))],
5521            IntervalUnit::MonthDayNano,
5522            format!(
5523                "Arithmetic overflow: Overflow happened on: {} * 12",
5524                i64::MAX - 2
5525            )
5526        );
5527    }
5528
5529    #[test]
5530    fn test_cast_binary_to_fixed_size_binary() {
5531        let bytes_1 = b"Hiiii".as_slice();
5532        let bytes_2 = b"Hello".as_slice();
5533
5534        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5535        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5536        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5537
5538        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5539        let down_cast = array_ref
5540            .as_any()
5541            .downcast_ref::<FixedSizeBinaryArray>()
5542            .unwrap();
5543        assert_eq!(bytes_1, down_cast.value(0));
5544        assert_eq!(bytes_2, down_cast.value(1));
5545        assert!(down_cast.is_null(2));
5546
5547        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5548        let down_cast = array_ref
5549            .as_any()
5550            .downcast_ref::<FixedSizeBinaryArray>()
5551            .unwrap();
5552        assert_eq!(bytes_1, down_cast.value(0));
5553        assert_eq!(bytes_2, down_cast.value(1));
5554        assert!(down_cast.is_null(2));
5555
5556        // test error cases when the length of binary are not same
5557        let bytes_1 = b"Hi".as_slice();
5558        let bytes_2 = b"Hello".as_slice();
5559
5560        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5561        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5562        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5563
5564        let array_ref = cast_with_options(
5565            &a1,
5566            &DataType::FixedSizeBinary(5),
5567            &CastOptions {
5568                safe: false,
5569                format_options: FormatOptions::default(),
5570            },
5571        );
5572        assert!(array_ref.is_err());
5573
5574        let array_ref = cast_with_options(
5575            &a2,
5576            &DataType::FixedSizeBinary(5),
5577            &CastOptions {
5578                safe: false,
5579                format_options: FormatOptions::default(),
5580            },
5581        );
5582        assert!(array_ref.is_err());
5583    }
5584
5585    #[test]
5586    fn test_fixed_size_binary_to_binary() {
5587        let bytes_1 = b"Hiiii".as_slice();
5588        let bytes_2 = b"Hello".as_slice();
5589
5590        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5591        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5592
5593        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5594        let down_cast = array_ref.as_binary::<i32>();
5595        assert_eq!(bytes_1, down_cast.value(0));
5596        assert_eq!(bytes_2, down_cast.value(1));
5597        assert!(down_cast.is_null(2));
5598
5599        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5600        let down_cast = array_ref.as_binary::<i64>();
5601        assert_eq!(bytes_1, down_cast.value(0));
5602        assert_eq!(bytes_2, down_cast.value(1));
5603        assert!(down_cast.is_null(2));
5604
5605        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5606        let down_cast = array_ref.as_binary_view();
5607        assert_eq!(bytes_1, down_cast.value(0));
5608        assert_eq!(bytes_2, down_cast.value(1));
5609        assert!(down_cast.is_null(2));
5610    }
5611
5612    #[test]
5613    fn test_fixed_size_binary_to_dictionary() {
5614        let bytes_1 = b"Hiiii".as_slice();
5615        let bytes_2 = b"Hello".as_slice();
5616
5617        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5618        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5619
5620        let cast_type = DataType::Dictionary(
5621            Box::new(DataType::Int8),
5622            Box::new(DataType::FixedSizeBinary(5)),
5623        );
5624        let cast_array = cast(&a1, &cast_type).unwrap();
5625        assert_eq!(cast_array.data_type(), &cast_type);
5626        assert_eq!(
5627            array_to_strings(&cast_array),
5628            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5629        );
5630        // dictionary should only have two distinct values
5631        let dict_array = cast_array.as_dictionary::<Int8Type>();
5632        assert_eq!(dict_array.values().len(), 2);
5633    }
5634
5635    #[test]
5636    fn test_binary_to_dictionary() {
5637        let mut builder = GenericBinaryBuilder::<i32>::new();
5638        builder.append_value(b"hello");
5639        builder.append_value(b"hiiii");
5640        builder.append_value(b"hiiii"); // duplicate
5641        builder.append_null();
5642        builder.append_value(b"rustt");
5643
5644        let a1 = builder.finish();
5645
5646        let cast_type = DataType::Dictionary(
5647            Box::new(DataType::Int8),
5648            Box::new(DataType::FixedSizeBinary(5)),
5649        );
5650        let cast_array = cast(&a1, &cast_type).unwrap();
5651        assert_eq!(cast_array.data_type(), &cast_type);
5652        assert_eq!(
5653            array_to_strings(&cast_array),
5654            vec![
5655                "68656c6c6f",
5656                "6869696969",
5657                "6869696969",
5658                "null",
5659                "7275737474"
5660            ]
5661        );
5662        // dictionary should only have three distinct values
5663        let dict_array = cast_array.as_dictionary::<Int8Type>();
5664        assert_eq!(dict_array.values().len(), 3);
5665    }
5666
5667    #[test]
5668    fn test_cast_string_array_to_dict_utf8_view() {
5669        let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5670
5671        let cast_type =
5672            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5673        assert!(can_cast_types(array.data_type(), &cast_type));
5674        let cast_array = cast(&array, &cast_type).unwrap();
5675        assert_eq!(cast_array.data_type(), &cast_type);
5676
5677        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5678        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5679        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5680
5681        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5682        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5683        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5684
5685        let keys = dict_array.keys();
5686        assert!(keys.is_null(1));
5687        assert_eq!(keys.value(0), keys.value(3));
5688        assert_ne!(keys.value(0), keys.value(2));
5689    }
5690
5691    #[test]
5692    fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
5693        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
5694
5695        let cast_type =
5696            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5697        assert!(can_cast_types(array.data_type(), &cast_type));
5698        let cast_array = cast(&array, &cast_type).unwrap();
5699        assert_eq!(cast_array.data_type(), &cast_type);
5700
5701        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5702        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5703        assert_eq!(dict_array.values().len(), 2);
5704
5705        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5706        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5707        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
5708
5709        let keys = dict_array.keys();
5710        assert!(keys.is_null(1));
5711        assert_eq!(keys.value(0), keys.value(3));
5712        assert_ne!(keys.value(0), keys.value(2));
5713    }
5714
5715    #[test]
5716    fn test_cast_string_view_array_to_dict_utf8_view() {
5717        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5718
5719        let cast_type =
5720            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5721        assert!(can_cast_types(array.data_type(), &cast_type));
5722        let cast_array = cast(&array, &cast_type).unwrap();
5723        assert_eq!(cast_array.data_type(), &cast_type);
5724
5725        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5726        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5727        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5728
5729        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5730        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5731        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5732
5733        let keys = dict_array.keys();
5734        assert!(keys.is_null(1));
5735        assert_eq!(keys.value(0), keys.value(3));
5736        assert_ne!(keys.value(0), keys.value(2));
5737    }
5738
5739    #[test]
5740    fn test_cast_string_view_slice_to_dict_utf8_view() {
5741        let array = StringViewArray::from(vec![
5742            Some("zero"),
5743            Some("one"),
5744            None,
5745            Some("three"),
5746            Some("one"),
5747        ]);
5748        let view = array.slice(1, 4);
5749
5750        let cast_type =
5751            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5752        assert!(can_cast_types(view.data_type(), &cast_type));
5753        let cast_array = cast(&view, &cast_type).unwrap();
5754        assert_eq!(cast_array.data_type(), &cast_type);
5755
5756        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5757        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5758        assert_eq!(dict_array.values().len(), 2);
5759
5760        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5761        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5762        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5763
5764        let keys = dict_array.keys();
5765        assert!(keys.is_null(1));
5766        assert_eq!(keys.value(0), keys.value(3));
5767        assert_ne!(keys.value(0), keys.value(2));
5768    }
5769
5770    #[test]
5771    fn test_cast_binary_array_to_dict_binary_view() {
5772        let mut builder = GenericBinaryBuilder::<i32>::new();
5773        builder.append_value(b"hello");
5774        builder.append_value(b"hiiii");
5775        builder.append_value(b"hiiii"); // duplicate
5776        builder.append_null();
5777        builder.append_value(b"rustt");
5778
5779        let array = builder.finish();
5780
5781        let cast_type =
5782            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5783        assert!(can_cast_types(array.data_type(), &cast_type));
5784        let cast_array = cast(&array, &cast_type).unwrap();
5785        assert_eq!(cast_array.data_type(), &cast_type);
5786
5787        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5788        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5789        assert_eq!(dict_array.values().len(), 3);
5790
5791        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5792        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5793        assert_eq!(
5794            actual,
5795            vec![
5796                Some(b"hello".as_slice()),
5797                Some(b"hiiii".as_slice()),
5798                Some(b"hiiii".as_slice()),
5799                None,
5800                Some(b"rustt".as_slice())
5801            ]
5802        );
5803
5804        let keys = dict_array.keys();
5805        assert!(keys.is_null(3));
5806        assert_eq!(keys.value(1), keys.value(2));
5807        assert_ne!(keys.value(0), keys.value(1));
5808    }
5809
5810    #[test]
5811    fn test_cast_binary_view_array_to_dict_binary_view() {
5812        let view = BinaryViewArray::from_iter([
5813            Some(b"hello".as_slice()),
5814            Some(b"hiiii".as_slice()),
5815            Some(b"hiiii".as_slice()), // duplicate
5816            None,
5817            Some(b"rustt".as_slice()),
5818        ]);
5819
5820        let cast_type =
5821            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5822        assert!(can_cast_types(view.data_type(), &cast_type));
5823        let cast_array = cast(&view, &cast_type).unwrap();
5824        assert_eq!(cast_array.data_type(), &cast_type);
5825
5826        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5827        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5828        assert_eq!(dict_array.values().len(), 3);
5829
5830        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5831        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5832        assert_eq!(
5833            actual,
5834            vec![
5835                Some(b"hello".as_slice()),
5836                Some(b"hiiii".as_slice()),
5837                Some(b"hiiii".as_slice()),
5838                None,
5839                Some(b"rustt".as_slice())
5840            ]
5841        );
5842
5843        let keys = dict_array.keys();
5844        assert!(keys.is_null(3));
5845        assert_eq!(keys.value(1), keys.value(2));
5846        assert_ne!(keys.value(0), keys.value(1));
5847    }
5848
5849    #[test]
5850    fn test_cast_binary_view_slice_to_dict_binary_view() {
5851        let view = BinaryViewArray::from_iter([
5852            Some(b"hello".as_slice()),
5853            Some(b"hiiii".as_slice()),
5854            Some(b"hiiii".as_slice()), // duplicate
5855            None,
5856            Some(b"rustt".as_slice()),
5857        ]);
5858        let sliced = view.slice(1, 4);
5859
5860        let cast_type =
5861            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5862        assert!(can_cast_types(sliced.data_type(), &cast_type));
5863        let cast_array = cast(&sliced, &cast_type).unwrap();
5864        assert_eq!(cast_array.data_type(), &cast_type);
5865
5866        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5867        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5868        assert_eq!(dict_array.values().len(), 2);
5869
5870        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5871        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5872        assert_eq!(
5873            actual,
5874            vec![
5875                Some(b"hiiii".as_slice()),
5876                Some(b"hiiii".as_slice()),
5877                None,
5878                Some(b"rustt".as_slice())
5879            ]
5880        );
5881
5882        let keys = dict_array.keys();
5883        assert!(keys.is_null(2));
5884        assert_eq!(keys.value(0), keys.value(1));
5885        assert_ne!(keys.value(0), keys.value(3));
5886    }
5887
5888    #[test]
5889    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
5890        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
5891
5892        let cast_type =
5893            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
5894        assert!(can_cast_types(array.data_type(), &cast_type));
5895        let err = cast(&array, &cast_type).unwrap_err();
5896        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
5897    }
5898
5899    #[test]
5900    fn test_cast_large_string_array_to_dict_utf8_view() {
5901        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5902
5903        let cast_type =
5904            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5905        assert!(can_cast_types(array.data_type(), &cast_type));
5906        let cast_array = cast(&array, &cast_type).unwrap();
5907        assert_eq!(cast_array.data_type(), &cast_type);
5908
5909        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5910        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5911        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5912
5913        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5914        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5915        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5916
5917        let keys = dict_array.keys();
5918        assert!(keys.is_null(1));
5919        assert_eq!(keys.value(0), keys.value(3));
5920        assert_ne!(keys.value(0), keys.value(2));
5921    }
5922
5923    #[test]
5924    fn test_cast_large_binary_array_to_dict_binary_view() {
5925        let mut builder = GenericBinaryBuilder::<i64>::new();
5926        builder.append_value(b"hello");
5927        builder.append_value(b"world");
5928        builder.append_value(b"hello"); // duplicate
5929        builder.append_null();
5930
5931        let array = builder.finish();
5932
5933        let cast_type =
5934            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5935        assert!(can_cast_types(array.data_type(), &cast_type));
5936        let cast_array = cast(&array, &cast_type).unwrap();
5937        assert_eq!(cast_array.data_type(), &cast_type);
5938
5939        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5940        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5941        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
5942
5943        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5944        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5945        assert_eq!(
5946            actual,
5947            vec![
5948                Some(b"hello".as_slice()),
5949                Some(b"world".as_slice()),
5950                Some(b"hello".as_slice()),
5951                None
5952            ]
5953        );
5954
5955        let keys = dict_array.keys();
5956        assert!(keys.is_null(3));
5957        assert_eq!(keys.value(0), keys.value(2));
5958        assert_ne!(keys.value(0), keys.value(1));
5959    }
5960
5961    #[test]
5962    fn test_cast_struct_array_to_dict_struct() {
5963        // Cast a StructArray into Dictionary<UInt32, Struct{…}>. The dictionary
5964        // value type's child fields may differ from the source's (here:
5965        // Utf8 source → Utf8View child for `name`), so the per-field cast
5966        // must run before identity keys are emitted. This is the "as long as
5967        // the struct can be cast to the dict value" contract.
5968        let names = StringArray::from(vec![Some("alpha"), None, Some("gamma")]);
5969        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
5970        let source = StructArray::from(vec![
5971            (
5972                Arc::new(Field::new("name", DataType::Utf8, true)),
5973                Arc::new(names) as ArrayRef,
5974            ),
5975            (
5976                Arc::new(Field::new("id", DataType::Int32, false)),
5977                Arc::new(ids) as ArrayRef,
5978            ),
5979        ]);
5980
5981        let target_value_type = DataType::Struct(
5982            vec![
5983                Field::new("name", DataType::Utf8View, true),
5984                Field::new("id", DataType::Int64, false),
5985            ]
5986            .into(),
5987        );
5988        let cast_type = DataType::Dictionary(
5989            Box::new(DataType::UInt32),
5990            Box::new(target_value_type.clone()),
5991        );
5992        assert!(can_cast_types(source.data_type(), &cast_type));
5993
5994        let cast_array = cast(&source, &cast_type).unwrap();
5995        assert_eq!(cast_array.data_type(), &cast_type);
5996        assert_eq!(cast_array.len(), 3);
5997
5998        let dict = cast_array.as_dictionary::<UInt32Type>();
5999        assert_eq!(dict.values().data_type(), &target_value_type);
6000        // No dedup is performed for struct values — one row, one key.
6001        assert_eq!(dict.values().len(), 3);
6002
6003        // Source row 1 was a `Utf8`-null in the `name` field but the whole
6004        // struct row was valid (StructArray::from above takes per-field
6005        // nulls only). The dictionary's logical null mask therefore mirrors
6006        // the source struct's row-level null mask — all rows valid here.
6007        let keys = dict.keys();
6008        assert_eq!(keys.values(), &[0u32, 1, 2]);
6009        assert_eq!(keys.null_count(), 0);
6010
6011        let struct_values = dict.values().as_struct();
6012        let names_out = struct_values
6013            .column_by_name("name")
6014            .unwrap()
6015            .as_string_view();
6016        assert_eq!(names_out.value(0), "alpha");
6017        assert!(names_out.is_null(1));
6018        assert_eq!(names_out.value(2), "gamma");
6019        let ids_out = struct_values
6020            .column_by_name("id")
6021            .unwrap()
6022            .as_primitive::<Int64Type>();
6023        assert_eq!(ids_out.values(), &[1i64, 2, 3]);
6024    }
6025
6026    #[test]
6027    fn test_cast_struct_array_to_dict_struct_row_nulls() {
6028        // Row-level nulls on the source struct must surface as null keys on
6029        // the dictionary, since the dictionary's logical null mask is
6030        // determined by the keys.
6031        let names = StringArray::from(vec![Some("alpha"), Some("beta"), Some("gamma")]);
6032        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6033        let source = StructArray::try_new(
6034            vec![
6035                Field::new("name", DataType::Utf8, true),
6036                Field::new("id", DataType::Int32, false),
6037            ]
6038            .into(),
6039            vec![Arc::new(names) as ArrayRef, Arc::new(ids) as ArrayRef],
6040            Some(NullBuffer::from(vec![true, false, true])),
6041        )
6042        .unwrap();
6043
6044        let target_value_type = DataType::Struct(
6045            vec![
6046                Field::new("name", DataType::Utf8, true),
6047                Field::new("id", DataType::Int32, false),
6048            ]
6049            .into(),
6050        );
6051        let cast_type =
6052            DataType::Dictionary(Box::new(DataType::UInt32), Box::new(target_value_type));
6053
6054        let cast_array = cast(&source, &cast_type).unwrap();
6055        let dict = cast_array.as_dictionary::<UInt32Type>();
6056        assert_eq!(dict.len(), 3);
6057        let keys = dict.keys();
6058        assert!(!keys.is_null(0));
6059        assert!(keys.is_null(1));
6060        assert!(!keys.is_null(2));
6061    }
6062
6063    #[test]
6064    fn test_cast_struct_array_to_dict_struct_key_overflow() {
6065        // Source has 300 rows but the dictionary key type is UInt8 (max 255).
6066        // We must return a CastError instead of silently truncating.
6067        let n = 300;
6068        let names = StringArray::from((0..n).map(|i| Some(format!("v{i}"))).collect::<Vec<_>>());
6069        let source = StructArray::from(vec![(
6070            Arc::new(Field::new("name", DataType::Utf8, true)),
6071            Arc::new(names) as ArrayRef,
6072        )]);
6073
6074        let cast_type = DataType::Dictionary(
6075            Box::new(DataType::UInt8),
6076            Box::new(DataType::Struct(
6077                vec![Field::new("name", DataType::Utf8, true)].into(),
6078            )),
6079        );
6080        let err = cast(&source, &cast_type).unwrap_err().to_string();
6081        assert!(
6082            err.contains("Cannot fit") && err.contains("dictionary keys"),
6083            "expected key-overflow error, got: {err}"
6084        );
6085    }
6086
6087    #[test]
6088    fn test_cast_empty_string_array_to_dict_utf8_view() {
6089        let array = StringArray::from(Vec::<Option<&str>>::new());
6090
6091        let cast_type =
6092            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6093        assert!(can_cast_types(array.data_type(), &cast_type));
6094        let cast_array = cast(&array, &cast_type).unwrap();
6095        assert_eq!(cast_array.data_type(), &cast_type);
6096        assert_eq!(cast_array.len(), 0);
6097    }
6098
6099    #[test]
6100    fn test_cast_empty_binary_array_to_dict_binary_view() {
6101        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6102
6103        let cast_type =
6104            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6105        assert!(can_cast_types(array.data_type(), &cast_type));
6106        let cast_array = cast(&array, &cast_type).unwrap();
6107        assert_eq!(cast_array.data_type(), &cast_type);
6108        assert_eq!(cast_array.len(), 0);
6109    }
6110
6111    #[test]
6112    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6113        let array = StringArray::from(vec![None::<&str>, None, None]);
6114
6115        let cast_type =
6116            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6117        assert!(can_cast_types(array.data_type(), &cast_type));
6118        let cast_array = cast(&array, &cast_type).unwrap();
6119        assert_eq!(cast_array.data_type(), &cast_type);
6120        assert_eq!(cast_array.null_count(), 3);
6121
6122        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6123        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6124        assert_eq!(dict_array.values().len(), 0);
6125        assert_eq!(dict_array.keys().null_count(), 3);
6126
6127        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6128        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6129        assert_eq!(actual, vec![None, None, None]);
6130    }
6131
6132    #[test]
6133    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6134        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6135
6136        let cast_type =
6137            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6138        assert!(can_cast_types(array.data_type(), &cast_type));
6139        let cast_array = cast(&array, &cast_type).unwrap();
6140        assert_eq!(cast_array.data_type(), &cast_type);
6141        assert_eq!(cast_array.null_count(), 3);
6142
6143        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6144        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6145        assert_eq!(dict_array.values().len(), 0);
6146        assert_eq!(dict_array.keys().null_count(), 3);
6147
6148        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6149        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6150        assert_eq!(actual, vec![None, None, None]);
6151    }
6152
6153    #[test]
6154    fn test_numeric_to_binary() {
6155        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6156
6157        let array_ref = cast(&a, &DataType::Binary).unwrap();
6158        let down_cast = array_ref.as_binary::<i32>();
6159        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6160        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6161        assert!(down_cast.is_null(2));
6162
6163        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6164
6165        let array_ref = cast(&a, &DataType::Binary).unwrap();
6166        let down_cast = array_ref.as_binary::<i32>();
6167        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6168        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6169        assert!(down_cast.is_null(2));
6170    }
6171
6172    #[test]
6173    fn test_numeric_to_large_binary() {
6174        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6175
6176        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6177        let down_cast = array_ref.as_binary::<i64>();
6178        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6179        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6180        assert!(down_cast.is_null(2));
6181
6182        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6183
6184        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6185        let down_cast = array_ref.as_binary::<i64>();
6186        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6187        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6188        assert!(down_cast.is_null(2));
6189    }
6190
6191    #[test]
6192    fn test_cast_date32_to_int32() {
6193        let array = Date32Array::from(vec![10000, 17890]);
6194        let b = cast(&array, &DataType::Int32).unwrap();
6195        let c = b.as_primitive::<Int32Type>();
6196        assert_eq!(10000, c.value(0));
6197        assert_eq!(17890, c.value(1));
6198    }
6199
6200    #[test]
6201    fn test_cast_int32_to_date32() {
6202        let array = Int32Array::from(vec![10000, 17890]);
6203        let b = cast(&array, &DataType::Date32).unwrap();
6204        let c = b.as_primitive::<Date32Type>();
6205        assert_eq!(10000, c.value(0));
6206        assert_eq!(17890, c.value(1));
6207    }
6208
6209    /// Compares arithmetic casts with Chrono for representable dates, including
6210    /// negative and fractional boundaries, nulls, and nonzero-offset slices.
6211    fn check_timestamp_casts<T: ArrowTimestampType>() {
6212        use rand::rngs::StdRng;
6213        use rand::{RngExt, SeedableRng};
6214
6215        let units = time_unit_multiple(&T::UNIT);
6216        let around = |x: i64| [x - 1, x, x + 1, -x - 1, -x, -x + 1];
6217        let mut values = vec![i64::MIN, i64::MAX];
6218        for base in [0, 60, 3_600, 86_400] {
6219            for seconds in around(base) {
6220                for fraction in [-1, 0, 1, units - 1] {
6221                    values.push(seconds * units + fraction);
6222                }
6223            }
6224        }
6225        for boundary in [1_000, 1_000_000] {
6226            values.extend(around(boundary));
6227        }
6228        let mut rng = StdRng::seed_from_u64(42);
6229        for _ in 0..256 {
6230            values.push(rng.random());
6231            let seconds = rng.random_range(-2_000_000_000..2_000_000_000);
6232            values.push(seconds * units + rng.random_range(0..units));
6233        }
6234        values.retain(|&v| as_datetime::<T>(v).is_some());
6235        let no_nulls = PrimitiveArray::<T>::new(values.clone().into(), None);
6236        let mixed = PrimitiveArray::<T>::from_iter(
6237            values
6238                .iter()
6239                .enumerate()
6240                .map(|(i, &v)| (i % 5 != 0).then_some(v)),
6241        );
6242        for array in [no_nulls, mixed.slice(1, mixed.len() - 2)] {
6243            let dates: Vec<_> = array
6244                .iter()
6245                .map(|v| v.map(|v| as_datetime::<T>(v).unwrap()))
6246                .collect();
6247            let expected: [ArrayRef; 5] = [
6248                Arc::new(Date32Array::from_iter(
6249                    dates
6250                        .iter()
6251                        .map(|d| d.map(|d| Date32Type::from_naive_date(d.date()))),
6252                )),
6253                Arc::new(Time32SecondArray::from_iter(
6254                    dates.iter().map(|d| d.map(|d| time_to_time32s(d.time()))),
6255                )),
6256                Arc::new(Time32MillisecondArray::from_iter(
6257                    dates.iter().map(|d| d.map(|d| time_to_time32ms(d.time()))),
6258                )),
6259                Arc::new(Time64MicrosecondArray::from_iter(
6260                    dates.iter().map(|d| d.map(|d| time_to_time64us(d.time()))),
6261                )),
6262                Arc::new(Time64NanosecondArray::from_iter(
6263                    dates.iter().map(|d| d.map(|d| time_to_time64ns(d.time()))),
6264                )),
6265            ];
6266            for expected in expected {
6267                let actual = cast(&array, expected.data_type()).unwrap();
6268                assert_eq!(actual.as_ref(), expected.as_ref(), "{}", T::DATA_TYPE);
6269            }
6270        }
6271    }
6272
6273    #[test]
6274    fn test_cast_timestamp_without_timezone_matches_chrono() {
6275        check_timestamp_casts::<TimestampSecondType>();
6276        check_timestamp_casts::<TimestampMillisecondType>();
6277        check_timestamp_casts::<TimestampMicrosecondType>();
6278        check_timestamp_casts::<TimestampNanosecondType>();
6279    }
6280
6281    #[test]
6282    fn test_cast_timestamp_to_time_extremes() {
6283        fn check<T: ArrowTimestampType>(min: (u32, u32, u32, u32), max: (u32, u32, u32, u32)) {
6284            let time = |(h, m, s, ns)| NaiveTime::from_hms_nano_opt(h, m, s, ns).unwrap();
6285            let times = [Some(time(min)), None, Some(time(max))];
6286            let array = PrimitiveArray::<T>::from_iter([Some(i64::MIN), None, Some(i64::MAX)]);
6287            let expected: [ArrayRef; 4] = [
6288                Arc::new(Time32SecondArray::from_iter(
6289                    times.map(|v| v.map(time_to_time32s)),
6290                )),
6291                Arc::new(Time32MillisecondArray::from_iter(
6292                    times.map(|v| v.map(time_to_time32ms)),
6293                )),
6294                Arc::new(Time64MicrosecondArray::from_iter(
6295                    times.map(|v| v.map(time_to_time64us)),
6296                )),
6297                Arc::new(Time64NanosecondArray::from_iter(
6298                    times.map(|v| v.map(time_to_time64ns)),
6299                )),
6300            ];
6301            for expected in expected {
6302                assert_eq!(
6303                    cast(&array, expected.data_type()).unwrap().as_ref(),
6304                    expected.as_ref()
6305                );
6306            }
6307        }
6308        check::<TimestampSecondType>((8, 29, 52, 0), (15, 30, 7, 0));
6309        check::<TimestampMillisecondType>((16, 47, 4, 192_000_000), (7, 12, 55, 807_000_000));
6310        check::<TimestampMicrosecondType>((19, 59, 5, 224_192_000), (4, 0, 54, 775_807_000));
6311        // Nanosecond extremes are representable in Chrono and covered above.
6312    }
6313
6314    #[test]
6315    fn test_cast_timestamp_to_date32_range() {
6316        fn check<T: ArrowTimestampType>() {
6317            let day = SECONDS_IN_DAY * time_unit_multiple(&T::UNIT);
6318            let first = i32::MIN as i64 * day;
6319            let last = (i32::MAX as i64 + 1) * day - 1;
6320            let array = PrimitiveArray::<T>::new(
6321                vec![first, i64::MAX, last].into(),
6322                Some(NullBuffer::from(vec![true, false, true])),
6323            );
6324            let expected = Date32Array::from(vec![Some(i32::MIN), None, Some(i32::MAX)]);
6325            for safe in [true, false] {
6326                let options = CastOptions {
6327                    safe,
6328                    ..Default::default()
6329                };
6330                assert_eq!(
6331                    cast_with_options(&array, &Date32, &options)
6332                        .unwrap()
6333                        .as_ref(),
6334                    &expected
6335                );
6336            }
6337            // Day counts that do not fit in i32 become null when safe, otherwise an error.
6338            let array = PrimitiveArray::<T>::from_iter_values([
6339                first - 1,
6340                last,
6341                last + 1,
6342                i64::MIN,
6343                i64::MAX,
6344            ]);
6345            let expected = Date32Array::from(vec![None, Some(i32::MAX), None, None, None]);
6346            assert_eq!(cast(&array, &Date32).unwrap().as_ref(), &expected);
6347            let options = CastOptions {
6348                safe: false,
6349                ..Default::default()
6350            };
6351            assert!(cast_with_options(&array, &Date32, &options).is_err());
6352        }
6353        check::<TimestampSecondType>();
6354        check::<TimestampMillisecondType>();
6355        // The entire microsecond domain fits in Date32, including dates outside Chrono's range.
6356        let array = TimestampMicrosecondArray::from(vec![Some(i64::MIN), None, Some(i64::MAX)]);
6357        assert_eq!(
6358            cast(&array, &Date32).unwrap().as_ref(),
6359            &Date32Array::from(vec![Some(-106_751_992), None, Some(106_751_991)]),
6360        );
6361    }
6362
6363    #[test]
6364    fn test_cast_timestamp_date_time_timezone_validation() {
6365        let invalid = TimestampSecondArray::from(vec![0]).with_timezone("invalid timezone");
6366        let extreme = TimestampSecondArray::from(vec![0, i64::MAX]).with_timezone("+00:00");
6367        let options = CastOptions {
6368            safe: false,
6369            ..Default::default()
6370        };
6371        for target in [
6372            Date32,
6373            Time32(TimeUnit::Second),
6374            Time32(TimeUnit::Millisecond),
6375            Time64(TimeUnit::Microsecond),
6376            Time64(TimeUnit::Nanosecond),
6377        ] {
6378            // A timezone that fails to parse is an error even when the cast is safe.
6379            assert!(cast(&invalid, &target).is_err());
6380            // A timestamp outside Chrono's date range is null when safe, otherwise an error.
6381            let actual = cast(&extreme, &target).unwrap();
6382            assert!(actual.is_valid(0));
6383            assert!(actual.is_null(1));
6384            assert!(cast_with_options(&extreme, &target, &options).is_err());
6385        }
6386    }
6387
6388    #[test]
6389    fn test_cast_timestamp_to_date32() {
6390        let array =
6391            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6392                .with_timezone("+00:00".to_string());
6393        let b = cast(&array, &DataType::Date32).unwrap();
6394        let c = b.as_primitive::<Date32Type>();
6395        assert_eq!(10000, c.value(0));
6396        assert_eq!(17890, c.value(1));
6397        assert!(c.is_null(2));
6398    }
6399    #[test]
6400    fn test_cast_timestamp_to_date32_zone() {
6401        let strings = StringArray::from_iter([
6402            Some("1970-01-01T00:00:01"),
6403            Some("1970-01-01T23:59:59"),
6404            None,
6405            Some("2020-03-01T02:00:23+00:00"),
6406        ]);
6407        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6408        let timestamps = cast(&strings, &dt).unwrap();
6409        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6410
6411        let c = dates.as_primitive::<Date32Type>();
6412        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6413        assert_eq!(c.value_as_date(0).unwrap(), expected);
6414        assert_eq!(c.value_as_date(1).unwrap(), expected);
6415        assert!(c.is_null(2));
6416        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6417        assert_eq!(c.value_as_date(3).unwrap(), expected);
6418    }
6419    #[test]
6420    fn test_cast_timestamp_to_date64() {
6421        let array =
6422            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6423        let b = cast(&array, &DataType::Date64).unwrap();
6424        let c = b.as_primitive::<Date64Type>();
6425        assert_eq!(864000000005, c.value(0));
6426        assert_eq!(1545696000001, c.value(1));
6427        assert!(c.is_null(2));
6428
6429        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6430        let b = cast(&array, &DataType::Date64).unwrap();
6431        let c = b.as_primitive::<Date64Type>();
6432        assert_eq!(864000000005000, c.value(0));
6433        assert_eq!(1545696000001000, c.value(1));
6434
6435        // test overflow, safe cast
6436        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6437        let b = cast(&array, &DataType::Date64).unwrap();
6438        assert!(b.is_null(0));
6439        // test overflow, unsafe cast
6440        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6441        let options = CastOptions {
6442            safe: false,
6443            format_options: FormatOptions::default(),
6444        };
6445        let b = cast_with_options(&array, &DataType::Date64, &options);
6446        assert!(b.is_err());
6447    }
6448
6449    #[test]
6450    fn test_cast_timestamp_to_time64() {
6451        // test timestamp secs
6452        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6453            .with_timezone("+01:00".to_string());
6454        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6455        let c = b.as_primitive::<Time64MicrosecondType>();
6456        assert_eq!(3605000000, c.value(0));
6457        assert_eq!(3601000000, c.value(1));
6458        assert!(c.is_null(2));
6459        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6460        let c = b.as_primitive::<Time64NanosecondType>();
6461        assert_eq!(3605000000000, c.value(0));
6462        assert_eq!(3601000000000, c.value(1));
6463        assert!(c.is_null(2));
6464
6465        // test timestamp milliseconds
6466        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6467            .with_timezone("+01:00".to_string());
6468        let array = Arc::new(a) as ArrayRef;
6469        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6470        let c = b.as_primitive::<Time64MicrosecondType>();
6471        assert_eq!(3605000000, c.value(0));
6472        assert_eq!(3601000000, c.value(1));
6473        assert!(c.is_null(2));
6474        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6475        let c = b.as_primitive::<Time64NanosecondType>();
6476        assert_eq!(3605000000000, c.value(0));
6477        assert_eq!(3601000000000, c.value(1));
6478        assert!(c.is_null(2));
6479
6480        // test timestamp microseconds
6481        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6482            .with_timezone("+01:00".to_string());
6483        let array = Arc::new(a) as ArrayRef;
6484        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6485        let c = b.as_primitive::<Time64MicrosecondType>();
6486        assert_eq!(3605000000, c.value(0));
6487        assert_eq!(3601000000, c.value(1));
6488        assert!(c.is_null(2));
6489        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6490        let c = b.as_primitive::<Time64NanosecondType>();
6491        assert_eq!(3605000000000, c.value(0));
6492        assert_eq!(3601000000000, c.value(1));
6493        assert!(c.is_null(2));
6494
6495        // Test fractional timestamps on both sides of the epoch.
6496        let a = TimestampNanosecondArray::from(vec![Some(86_405_123_456_789), Some(-1), None])
6497            .with_timezone("+01:00".to_string());
6498        let array = Arc::new(a) as ArrayRef;
6499        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6500        let c = b.as_primitive::<Time64MicrosecondType>();
6501        assert_eq!(3_605_123_456, c.value(0));
6502        assert_eq!(3_599_999_999, c.value(1));
6503        assert!(c.is_null(2));
6504        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6505        let c = b.as_primitive::<Time64NanosecondType>();
6506        assert_eq!(3_605_123_456_789, c.value(0));
6507        assert_eq!(3_599_999_999_999, c.value(1));
6508        assert!(c.is_null(2));
6509
6510        // test overflow, safe cast
6511        let a =
6512            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6513        let array = Arc::new(a) as ArrayRef;
6514        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6515        assert!(b.is_null(0));
6516        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6517        assert!(b.is_null(0));
6518        // test overflow, unsafe cast
6519        let options = CastOptions {
6520            safe: false,
6521            ..Default::default()
6522        };
6523        let b = cast_with_options(&array, &DataType::Time64(TimeUnit::Microsecond), &options);
6524        assert!(b.is_err());
6525        let b = cast_with_options(&array, &DataType::Time64(TimeUnit::Nanosecond), &options);
6526        assert!(b.is_err());
6527        // Time64 only supports microseconds and nanoseconds
6528        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6529        assert!(b.is_err());
6530    }
6531
6532    #[test]
6533    fn test_cast_timestamp_to_time32() {
6534        // test timestamp secs
6535        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6536            .with_timezone("+01:00".to_string());
6537        let array = Arc::new(a) as ArrayRef;
6538        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6539        let c = b.as_primitive::<Time32SecondType>();
6540        assert_eq!(3605, c.value(0));
6541        assert_eq!(3601, c.value(1));
6542        assert!(c.is_null(2));
6543        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6544        let c = b.as_primitive::<Time32MillisecondType>();
6545        assert_eq!(3605000, c.value(0));
6546        assert_eq!(3601000, c.value(1));
6547        assert!(c.is_null(2));
6548
6549        // test timestamp milliseconds
6550        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6551            .with_timezone("+01:00".to_string());
6552        let array = Arc::new(a) as ArrayRef;
6553        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6554        let c = b.as_primitive::<Time32SecondType>();
6555        assert_eq!(3605, c.value(0));
6556        assert_eq!(3601, c.value(1));
6557        assert!(c.is_null(2));
6558        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6559        let c = b.as_primitive::<Time32MillisecondType>();
6560        assert_eq!(3605000, c.value(0));
6561        assert_eq!(3601000, c.value(1));
6562        assert!(c.is_null(2));
6563
6564        // test timestamp microseconds
6565        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6566            .with_timezone("+01:00".to_string());
6567        let array = Arc::new(a) as ArrayRef;
6568        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6569        let c = b.as_primitive::<Time32SecondType>();
6570        assert_eq!(3605, c.value(0));
6571        assert_eq!(3601, c.value(1));
6572        assert!(c.is_null(2));
6573        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6574        let c = b.as_primitive::<Time32MillisecondType>();
6575        assert_eq!(3605000, c.value(0));
6576        assert_eq!(3601000, c.value(1));
6577        assert!(c.is_null(2));
6578
6579        // Test fractional timestamps on both sides of the epoch.
6580        let a = TimestampNanosecondArray::from(vec![Some(86_405_123_456_789), Some(-1), None])
6581            .with_timezone("+01:00".to_string());
6582        let array = Arc::new(a) as ArrayRef;
6583        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6584        let c = b.as_primitive::<Time32SecondType>();
6585        assert_eq!(3605, c.value(0));
6586        assert_eq!(3599, c.value(1));
6587        assert!(c.is_null(2));
6588        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6589        let c = b.as_primitive::<Time32MillisecondType>();
6590        assert_eq!(3_605_123, c.value(0));
6591        assert_eq!(3_599_999, c.value(1));
6592        assert!(c.is_null(2));
6593
6594        // test overflow, safe cast
6595        let a =
6596            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6597        let array = Arc::new(a) as ArrayRef;
6598        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6599        assert!(b.is_null(0));
6600        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6601        assert!(b.is_null(0));
6602        // test overflow, unsafe cast
6603        let options = CastOptions {
6604            safe: false,
6605            ..Default::default()
6606        };
6607        let b = cast_with_options(&array, &DataType::Time32(TimeUnit::Second), &options);
6608        assert!(b.is_err());
6609        let b = cast_with_options(&array, &DataType::Time32(TimeUnit::Millisecond), &options);
6610        assert!(b.is_err());
6611    }
6612
6613    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6614    #[test]
6615    fn test_cast_timestamp_with_timezone_1() {
6616        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6617            Some("2000-01-01T00:00:00.123456789"),
6618            Some("2010-01-01T00:00:00.123456789"),
6619            None,
6620        ]));
6621        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6622        let timestamp_array = cast(&string_array, &to_type).unwrap();
6623
6624        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6625        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6626
6627        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6628        let result = string_array.as_string::<i32>();
6629        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6630        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6631        assert!(result.is_null(2));
6632    }
6633
6634    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6635    #[test]
6636    fn test_cast_timestamp_with_timezone_2() {
6637        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6638            Some("2000-01-01T07:00:00.123456789"),
6639            Some("2010-01-01T07:00:00.123456789"),
6640            None,
6641        ]));
6642        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6643        let timestamp_array = cast(&string_array, &to_type).unwrap();
6644
6645        // Check intermediate representation is correct
6646        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6647        let result = string_array.as_string::<i32>();
6648        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6649        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6650        assert!(result.is_null(2));
6651
6652        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6653        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6654
6655        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6656        let result = string_array.as_string::<i32>();
6657        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6658        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6659        assert!(result.is_null(2));
6660    }
6661
6662    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6663    #[test]
6664    fn test_cast_timestamp_with_timezone_3() {
6665        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6666            Some("2000-01-01T07:00:00.123456789"),
6667            Some("2010-01-01T07:00:00.123456789"),
6668            None,
6669        ]));
6670        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6671        let timestamp_array = cast(&string_array, &to_type).unwrap();
6672
6673        // Check intermediate representation is correct
6674        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6675        let result = string_array.as_string::<i32>();
6676        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6677        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6678        assert!(result.is_null(2));
6679
6680        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6681        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6682
6683        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6684        let result = string_array.as_string::<i32>();
6685        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6686        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6687        assert!(result.is_null(2));
6688    }
6689
6690    /// The i64 value of a `Timestamp(Second, None)` holding the given wall clock
6691    /// reading (a naive timestamp is stored as if it were UTC).
6692    fn naive_seconds(y: i32, m: u32, d: u32, h: u32, min: u32) -> i64 {
6693        NaiveDate::from_ymd_opt(y, m, d)
6694            .unwrap()
6695            .and_hms_opt(h, min, 0)
6696            .unwrap()
6697            .and_utc()
6698            .timestamp()
6699    }
6700
6701    /// The instant, in seconds since the epoch, denoted by the given wall clock
6702    /// reading at a fixed offset of `offset_hours`.
6703    fn instant_seconds(y: i32, m: u32, d: u32, h: u32, min: u32, offset_hours: i32) -> i64 {
6704        let offset = FixedOffset::east_opt(offset_hours * 3600).unwrap();
6705        NaiveDate::from_ymd_opt(y, m, d)
6706            .unwrap()
6707            .and_hms_opt(h, min, 0)
6708            .unwrap()
6709            .and_local_timezone(offset)
6710            .unwrap()
6711            .timestamp()
6712    }
6713
6714    // Cast Timestamp(_, None) -> Timestamp(_, Some(IANA timezone)) across DST
6715    // transitions. See `resolve_local_offset`.
6716    #[test]
6717    fn test_cast_timestamp_to_named_timezone_dst() {
6718        // Unambiguous, EDT (-04:00) is in effect.
6719        let unambiguous = naive_seconds(2024, 11, 1, 0, 0);
6720        // Ambiguous: the clocks go back at 2024-11-03T02:00 EDT, so 01:30
6721        // happens twice, first at -04:00 and then at -05:00.
6722        let ambiguous = naive_seconds(2024, 11, 3, 1, 30);
6723        // Nonexistent: the clocks go forward at 2024-03-10T02:00 EST, so 02:30
6724        // never happens.
6725        let nonexistent = naive_seconds(2024, 3, 10, 2, 30);
6726        assert_eq!(
6727            [unambiguous, ambiguous, nonexistent],
6728            [1_730_419_200, 1_730_597_400, 1_710_037_800]
6729        );
6730
6731        let array = TimestampSecondArray::from(vec![
6732            Some(unambiguous),
6733            Some(ambiguous),
6734            Some(nonexistent),
6735            None,
6736        ]);
6737        let to_type = DataType::Timestamp(TimeUnit::Second, Some("America/New_York".into()));
6738        let options = CastOptions {
6739            safe: false,
6740            ..Default::default()
6741        };
6742
6743        let b = cast_with_options(&array, &to_type, &options).unwrap();
6744        assert_eq!(b.data_type(), &to_type);
6745        let c = b.as_primitive::<TimestampSecondType>();
6746
6747        assert_eq!(c.value(0), instant_seconds(2024, 11, 1, 0, 0, -4));
6748        assert_eq!(c.value(0), 1_730_433_600);
6749        // The later of the two candidates, i.e. EST rather than EDT.
6750        assert_eq!(c.value(1), instant_seconds(2024, 11, 3, 1, 30, -5));
6751        assert_eq!(c.value(1), 1_730_615_400);
6752        // Shifted forward by the one hour gap: 02:30 EST is 03:30 EDT.
6753        assert_eq!(c.value(2), instant_seconds(2024, 3, 10, 2, 30, -5));
6754        assert_eq!(c.value(2), instant_seconds(2024, 3, 10, 3, 30, -4));
6755        assert_eq!(c.value(2), 1_710_055_800);
6756        assert!(c.is_null(3));
6757    }
6758
6759    // The same values must not become null when `safe` casting is requested.
6760    #[test]
6761    fn test_cast_timestamp_to_named_timezone_dst_safe() {
6762        let array = TimestampSecondArray::from(vec![
6763            Some(naive_seconds(2024, 11, 1, 0, 0)),
6764            Some(naive_seconds(2024, 11, 3, 1, 30)),
6765            Some(naive_seconds(2024, 3, 10, 2, 30)),
6766            None,
6767        ]);
6768        let to_type = DataType::Timestamp(TimeUnit::Second, Some("America/New_York".into()));
6769        let options = CastOptions {
6770            safe: true,
6771            ..Default::default()
6772        };
6773
6774        let b = cast_with_options(&array, &to_type, &options).unwrap();
6775        assert_eq!(b.data_type(), &to_type);
6776        let c = b.as_primitive::<TimestampSecondType>();
6777        assert_eq!(c.null_count(), 1);
6778        assert_eq!(c.value(0), 1_730_433_600);
6779        assert_eq!(c.value(1), 1_730_615_400);
6780        assert_eq!(c.value(2), 1_710_055_800);
6781        assert!(c.is_null(3));
6782    }
6783
6784    // Southern hemisphere: the transitions run the other way around.
6785    #[test]
6786    fn test_cast_timestamp_to_named_timezone_dst_southern_hemisphere() {
6787        // Ambiguous: clocks go back at 2024-04-07T03:00 AEDT (+11:00 -> +10:00).
6788        let ambiguous = naive_seconds(2024, 4, 7, 2, 30);
6789        // Nonexistent: clocks go forward at 2024-10-06T02:00 AEST (+10:00 -> +11:00).
6790        let nonexistent = naive_seconds(2024, 10, 6, 2, 30);
6791
6792        let array = TimestampSecondArray::from(vec![Some(ambiguous), Some(nonexistent)]);
6793        let to_type = DataType::Timestamp(TimeUnit::Second, Some("Australia/Sydney".into()));
6794        let options = CastOptions {
6795            safe: false,
6796            ..Default::default()
6797        };
6798
6799        let b = cast_with_options(&array, &to_type, &options).unwrap();
6800        assert_eq!(b.data_type(), &to_type);
6801        let c = b.as_primitive::<TimestampSecondType>();
6802
6803        // The later of the two candidates, i.e. AEST (+10:00).
6804        assert_eq!(c.value(0), instant_seconds(2024, 4, 7, 2, 30, 10));
6805        assert_eq!(c.value(0), 1_712_421_000);
6806        // Shifted forward by the one hour gap: 02:30 AEST is 03:30 AEDT.
6807        assert_eq!(c.value(1), instant_seconds(2024, 10, 6, 2, 30, 10));
6808        assert_eq!(c.value(1), instant_seconds(2024, 10, 6, 3, 30, 11));
6809        assert_eq!(c.value(1), 1_728_145_800);
6810    }
6811
6812    // The resolution is independent of the time unit.
6813    #[test]
6814    fn test_cast_timestamp_to_named_timezone_dst_nanosecond() {
6815        let ambiguous = naive_seconds(2024, 11, 3, 1, 30) * 1_000_000_000 + 123_456_789;
6816        let nonexistent = naive_seconds(2024, 3, 10, 2, 30) * 1_000_000_000 + 123_456_789;
6817        let array = TimestampNanosecondArray::from(vec![Some(ambiguous), Some(nonexistent)]);
6818        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, Some("America/New_York".into()));
6819        let options = CastOptions {
6820            safe: false,
6821            ..Default::default()
6822        };
6823
6824        let b = cast_with_options(&array, &to_type, &options).unwrap();
6825        assert_eq!(b.data_type(), &to_type);
6826        let c = b.as_primitive::<TimestampNanosecondType>();
6827        assert_eq!(c.value(0), 1_730_615_400 * 1_000_000_000 + 123_456_789);
6828        assert_eq!(c.value(1), 1_710_055_800 * 1_000_000_000 + 123_456_789);
6829    }
6830
6831    // Unit conversion still composes with the timezone adjustment.
6832    #[test]
6833    fn test_cast_timestamp_to_named_timezone_dst_changing_unit() {
6834        let array = TimestampSecondArray::from(vec![
6835            Some(naive_seconds(2024, 11, 3, 1, 30)),
6836            Some(naive_seconds(2024, 3, 10, 2, 30)),
6837        ]);
6838        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("America/New_York".into()));
6839        let options = CastOptions {
6840            safe: false,
6841            ..Default::default()
6842        };
6843
6844        let b = cast_with_options(&array, &to_type, &options).unwrap();
6845        assert_eq!(b.data_type(), &to_type);
6846        let c = b.as_primitive::<TimestampMillisecondType>();
6847        assert_eq!(c.value(0), 1_730_615_400_000);
6848        assert_eq!(c.value(1), 1_710_055_800_000);
6849    }
6850
6851    // A fixed offset has no transitions, so the same readings are unaffected.
6852    #[test]
6853    fn test_cast_timestamp_to_fixed_offset_timezone_unaffected() {
6854        let array = TimestampSecondArray::from(vec![
6855            Some(naive_seconds(2024, 11, 1, 0, 0)),
6856            Some(naive_seconds(2024, 11, 3, 1, 30)),
6857            Some(naive_seconds(2024, 3, 10, 2, 30)),
6858            None,
6859        ]);
6860        let to_type = DataType::Timestamp(TimeUnit::Second, Some("+08:00".into()));
6861        let options = CastOptions {
6862            safe: false,
6863            ..Default::default()
6864        };
6865
6866        let b = cast_with_options(&array, &to_type, &options).unwrap();
6867        assert_eq!(b.data_type(), &to_type);
6868        let c = b.as_primitive::<TimestampSecondType>();
6869        assert_eq!(c.value(0), instant_seconds(2024, 11, 1, 0, 0, 8));
6870        assert_eq!(c.value(1), instant_seconds(2024, 11, 3, 1, 30, 8));
6871        assert_eq!(c.value(2), instant_seconds(2024, 3, 10, 2, 30, 8));
6872        assert!(c.is_null(3));
6873    }
6874
6875    #[test]
6876    fn test_cast_date64_to_timestamp() {
6877        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6878        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6879        let c = b.as_primitive::<TimestampSecondType>();
6880        assert_eq!(864000000, c.value(0));
6881        assert_eq!(1545696000, c.value(1));
6882        assert!(c.is_null(2));
6883    }
6884
6885    #[test]
6886    fn test_cast_date64_to_timestamp_ms() {
6887        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6888        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6889        let c = b
6890            .as_any()
6891            .downcast_ref::<TimestampMillisecondArray>()
6892            .unwrap();
6893        assert_eq!(864000000005, c.value(0));
6894        assert_eq!(1545696000001, c.value(1));
6895        assert!(c.is_null(2));
6896    }
6897
6898    #[test]
6899    fn test_cast_date64_to_timestamp_us() {
6900        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6901        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6902        let c = b
6903            .as_any()
6904            .downcast_ref::<TimestampMicrosecondArray>()
6905            .unwrap();
6906        assert_eq!(864000000005000, c.value(0));
6907        assert_eq!(1545696000001000, c.value(1));
6908        assert!(c.is_null(2));
6909    }
6910
6911    #[test]
6912    fn test_cast_date64_to_timestamp_ns() {
6913        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6914        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6915        let c = b
6916            .as_any()
6917            .downcast_ref::<TimestampNanosecondArray>()
6918            .unwrap();
6919        assert_eq!(864000000005000000, c.value(0));
6920        assert_eq!(1545696000001000000, c.value(1));
6921        assert!(c.is_null(2));
6922    }
6923
6924    #[test]
6925    fn test_cast_timestamp_to_i64() {
6926        let array =
6927            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6928                .with_timezone("UTC".to_string());
6929        let b = cast(&array, &DataType::Int64).unwrap();
6930        let c = b.as_primitive::<Int64Type>();
6931        assert_eq!(&DataType::Int64, c.data_type());
6932        assert_eq!(864000000005, c.value(0));
6933        assert_eq!(1545696000001, c.value(1));
6934        assert!(c.is_null(2));
6935    }
6936
6937    macro_rules! assert_cast {
6938        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6939            assert!(can_cast_types($array.data_type(), &$datatype));
6940            let out = cast(&$array, &$datatype).unwrap();
6941            let actual = out
6942                .as_any()
6943                .downcast_ref::<$output_array_type>()
6944                .unwrap()
6945                .into_iter()
6946                .collect::<Vec<_>>();
6947            assert_eq!(actual, $expected);
6948        }};
6949        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6950            assert!(can_cast_types($array.data_type(), &$datatype));
6951            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6952            let actual = out
6953                .as_any()
6954                .downcast_ref::<$output_array_type>()
6955                .unwrap()
6956                .into_iter()
6957                .collect::<Vec<_>>();
6958            assert_eq!(actual, $expected);
6959        }};
6960    }
6961
6962    #[test]
6963    fn test_cast_date32_to_string() {
6964        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6965        let expected = vec![
6966            Some("1970-01-01"),
6967            Some("1997-05-19"),
6968            Some("2005-09-10"),
6969            Some("2018-12-25"),
6970            None,
6971        ];
6972
6973        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6974        assert_cast!(array, DataType::Utf8, StringArray, expected);
6975        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6976    }
6977
6978    #[test]
6979    fn test_cast_date64_to_string() {
6980        let array = Date64Array::from(vec![
6981            Some(0),
6982            Some(10000 * 86400000),
6983            Some(13036 * 86400000),
6984            Some(17890 * 86400000),
6985            None,
6986        ]);
6987        let expected = vec![
6988            Some("1970-01-01T00:00:00"),
6989            Some("1997-05-19T00:00:00"),
6990            Some("2005-09-10T00:00:00"),
6991            Some("2018-12-25T00:00:00"),
6992            None,
6993        ];
6994
6995        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6996        assert_cast!(array, DataType::Utf8, StringArray, expected);
6997        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6998    }
6999
7000    #[test]
7001    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
7002        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7003        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
7004        let array = Arc::new(a) as ArrayRef;
7005
7006        let b = cast(
7007            &array,
7008            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7009        )
7010        .unwrap();
7011        let c = b.as_primitive::<TimestampSecondType>();
7012        let string_array = cast(&c, &DataType::Utf8).unwrap();
7013        let result = string_array.as_string::<i32>();
7014        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7015
7016        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7017        let c = b.as_primitive::<TimestampSecondType>();
7018        let string_array = cast(&c, &DataType::Utf8).unwrap();
7019        let result = string_array.as_string::<i32>();
7020        assert_eq!("2021-01-01T00:00:00", result.value(0));
7021    }
7022
7023    #[test]
7024    fn test_cast_date32_to_timestamp_with_timezone() {
7025        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7026        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7027        let array = Arc::new(a) as ArrayRef;
7028        let b = cast(
7029            &array,
7030            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7031        )
7032        .unwrap();
7033        let c = b.as_primitive::<TimestampSecondType>();
7034        assert_eq!(1609438500, c.value(0));
7035        assert_eq!(1640974500, c.value(1));
7036        assert!(c.is_null(2));
7037
7038        let string_array = cast(&c, &DataType::Utf8).unwrap();
7039        let result = string_array.as_string::<i32>();
7040        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7041        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7042    }
7043
7044    #[test]
7045    fn test_cast_date32_to_timestamp_with_timezone_ms() {
7046        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7047        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7048        let array = Arc::new(a) as ArrayRef;
7049        let b = cast(
7050            &array,
7051            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7052        )
7053        .unwrap();
7054        let c = b.as_primitive::<TimestampMillisecondType>();
7055        assert_eq!(1609438500000, c.value(0));
7056        assert_eq!(1640974500000, c.value(1));
7057        assert!(c.is_null(2));
7058
7059        let string_array = cast(&c, &DataType::Utf8).unwrap();
7060        let result = string_array.as_string::<i32>();
7061        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7062        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7063    }
7064
7065    #[test]
7066    fn test_cast_date32_to_timestamp_with_timezone_us() {
7067        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7068        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7069        let array = Arc::new(a) as ArrayRef;
7070        let b = cast(
7071            &array,
7072            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7073        )
7074        .unwrap();
7075        let c = b.as_primitive::<TimestampMicrosecondType>();
7076        assert_eq!(1609438500000000, c.value(0));
7077        assert_eq!(1640974500000000, c.value(1));
7078        assert!(c.is_null(2));
7079
7080        let string_array = cast(&c, &DataType::Utf8).unwrap();
7081        let result = string_array.as_string::<i32>();
7082        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7083        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7084    }
7085
7086    #[test]
7087    fn test_cast_date32_to_timestamp_with_timezone_ns() {
7088        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7089        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7090        let array = Arc::new(a) as ArrayRef;
7091        let b = cast(
7092            &array,
7093            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7094        )
7095        .unwrap();
7096        let c = b.as_primitive::<TimestampNanosecondType>();
7097        assert_eq!(1609438500000000000, c.value(0));
7098        assert_eq!(1640974500000000000, c.value(1));
7099        assert!(c.is_null(2));
7100
7101        let string_array = cast(&c, &DataType::Utf8).unwrap();
7102        let result = string_array.as_string::<i32>();
7103        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7104        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7105    }
7106
7107    #[test]
7108    fn test_cast_date64_to_timestamp_with_timezone() {
7109        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7110        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7111        let b = cast(
7112            &array,
7113            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7114        )
7115        .unwrap();
7116
7117        let c = b.as_primitive::<TimestampSecondType>();
7118        assert_eq!(863979300, c.value(0));
7119        assert_eq!(1545675300, c.value(1));
7120        assert!(c.is_null(2));
7121
7122        let string_array = cast(&c, &DataType::Utf8).unwrap();
7123        let result = string_array.as_string::<i32>();
7124        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
7125        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
7126    }
7127
7128    #[test]
7129    fn test_cast_date64_to_timestamp_with_timezone_ms() {
7130        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7131        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7132        let b = cast(
7133            &array,
7134            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7135        )
7136        .unwrap();
7137
7138        let c = b.as_primitive::<TimestampMillisecondType>();
7139        assert_eq!(863979300005, c.value(0));
7140        assert_eq!(1545675300001, c.value(1));
7141        assert!(c.is_null(2));
7142
7143        let string_array = cast(&c, &DataType::Utf8).unwrap();
7144        let result = string_array.as_string::<i32>();
7145        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7146        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7147    }
7148
7149    #[test]
7150    fn test_cast_date64_to_timestamp_with_timezone_us() {
7151        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7152        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7153        let b = cast(
7154            &array,
7155            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7156        )
7157        .unwrap();
7158
7159        let c = b.as_primitive::<TimestampMicrosecondType>();
7160        assert_eq!(863979300005000, c.value(0));
7161        assert_eq!(1545675300001000, c.value(1));
7162        assert!(c.is_null(2));
7163
7164        let string_array = cast(&c, &DataType::Utf8).unwrap();
7165        let result = string_array.as_string::<i32>();
7166        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7167        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7168    }
7169
7170    #[test]
7171    fn test_cast_date64_to_timestamp_with_timezone_ns() {
7172        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7173        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7174        let b = cast(
7175            &array,
7176            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7177        )
7178        .unwrap();
7179
7180        let c = b.as_primitive::<TimestampNanosecondType>();
7181        assert_eq!(863979300005000000, c.value(0));
7182        assert_eq!(1545675300001000000, c.value(1));
7183        assert!(c.is_null(2));
7184
7185        let string_array = cast(&c, &DataType::Utf8).unwrap();
7186        let result = string_array.as_string::<i32>();
7187        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7188        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7189    }
7190
7191    #[test]
7192    fn test_cast_timestamp_to_strings() {
7193        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7194        let array =
7195            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7196        let expected = vec![
7197            Some("1997-05-19T00:00:03.005"),
7198            Some("2018-12-25T00:00:02.001"),
7199            None,
7200        ];
7201
7202        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7203        assert_cast!(array, DataType::Utf8, StringArray, expected);
7204        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7205    }
7206
7207    #[test]
7208    fn test_cast_timestamp_to_strings_opt() {
7209        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
7210        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7211        let cast_options = CastOptions {
7212            safe: true,
7213            format_options: FormatOptions::default()
7214                .with_timestamp_format(Some(ts_format))
7215                .with_timestamp_tz_format(Some(ts_format)),
7216        };
7217
7218        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7219        let array_without_tz =
7220            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7221        let expected = vec![
7222            Some("1997-05-19 00:00:03.005000"),
7223            Some("2018-12-25 00:00:02.001000"),
7224            None,
7225        ];
7226        assert_cast!(
7227            array_without_tz,
7228            DataType::Utf8View,
7229            StringViewArray,
7230            cast_options,
7231            expected
7232        );
7233        assert_cast!(
7234            array_without_tz,
7235            DataType::Utf8,
7236            StringArray,
7237            cast_options,
7238            expected
7239        );
7240        assert_cast!(
7241            array_without_tz,
7242            DataType::LargeUtf8,
7243            LargeStringArray,
7244            cast_options,
7245            expected
7246        );
7247
7248        let array_with_tz =
7249            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
7250                .with_timezone(tz.to_string());
7251        let expected = vec![
7252            Some("1997-05-19 05:45:03.005000"),
7253            Some("2018-12-25 05:45:02.001000"),
7254            None,
7255        ];
7256        assert_cast!(
7257            array_with_tz,
7258            DataType::Utf8View,
7259            StringViewArray,
7260            cast_options,
7261            expected
7262        );
7263        assert_cast!(
7264            array_with_tz,
7265            DataType::Utf8,
7266            StringArray,
7267            cast_options,
7268            expected
7269        );
7270        assert_cast!(
7271            array_with_tz,
7272            DataType::LargeUtf8,
7273            LargeStringArray,
7274            cast_options,
7275            expected
7276        );
7277    }
7278
7279    #[test]
7280    fn test_cast_between_timestamps() {
7281        let array =
7282            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7283        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7284        let c = b.as_primitive::<TimestampSecondType>();
7285        assert_eq!(864000003, c.value(0));
7286        assert_eq!(1545696002, c.value(1));
7287        assert!(c.is_null(2));
7288    }
7289
7290    #[test]
7291    fn test_cast_duration_to_i64() {
7292        let base = vec![5, 6, 7, 8, 100000000];
7293
7294        let duration_arrays = vec![
7295            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
7296            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
7297            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
7298            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
7299        ];
7300
7301        for arr in duration_arrays {
7302            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
7303            let result = cast(&arr, &DataType::Int64).unwrap();
7304            let result = result.as_primitive::<Int64Type>();
7305            assert_eq!(base.as_slice(), result.values());
7306        }
7307    }
7308
7309    #[test]
7310    fn test_cast_between_durations_and_numerics() {
7311        fn test_cast_between_durations<FromType, ToType>()
7312        where
7313            FromType: ArrowPrimitiveType<Native = i64>,
7314            ToType: ArrowPrimitiveType<Native = i64>,
7315            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
7316        {
7317            let DataType::Duration(from_unit) = FromType::DATA_TYPE else {
7318                panic!("Expected a duration type")
7319            };
7320            let DataType::Duration(to_unit) = ToType::DATA_TYPE else {
7321                panic!("Expected a duration type")
7322            };
7323            let from_size = time_unit_multiple(&from_unit);
7324            let to_size = time_unit_multiple(&to_unit);
7325
7326            let (v1_before, v2_before) = (8640003005, 1696002001);
7327            let (v1_after, v2_after) = if from_size >= to_size {
7328                (
7329                    v1_before / (from_size / to_size),
7330                    v2_before / (from_size / to_size),
7331                )
7332            } else {
7333                (
7334                    v1_before * (to_size / from_size),
7335                    v2_before * (to_size / from_size),
7336                )
7337            };
7338
7339            let array =
7340                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
7341            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
7342            let c = b.as_primitive::<ToType>();
7343            assert_eq!(v1_after, c.value(0));
7344            assert_eq!(v2_after, c.value(1));
7345            assert!(c.is_null(2));
7346        }
7347
7348        // between each individual duration type
7349        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
7350        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
7351        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
7352        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
7353        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
7354        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
7355        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
7356        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
7357        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
7358        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
7359        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
7360        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
7361
7362        // cast failed
7363        let array = DurationSecondArray::from(vec![
7364            Some(i64::MAX),
7365            Some(8640203410378005),
7366            Some(10241096),
7367            None,
7368        ]);
7369        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
7370        let c = b.as_primitive::<DurationNanosecondType>();
7371        assert!(c.is_null(0));
7372        assert!(c.is_null(1));
7373        assert_eq!(10241096000000000, c.value(2));
7374        assert!(c.is_null(3));
7375
7376        // durations to numerics
7377        let array = DurationSecondArray::from(vec![
7378            Some(i64::MAX),
7379            Some(8640203410378005),
7380            Some(10241096),
7381            None,
7382        ]);
7383        let b = cast(&array, &DataType::Int64).unwrap();
7384        let c = b.as_primitive::<Int64Type>();
7385        assert_eq!(i64::MAX, c.value(0));
7386        assert_eq!(8640203410378005, c.value(1));
7387        assert_eq!(10241096, c.value(2));
7388        assert!(c.is_null(3));
7389
7390        let b = cast(&array, &DataType::Int32).unwrap();
7391        let c = b.as_primitive::<Int32Type>();
7392        assert_eq!(0, c.value(0));
7393        assert_eq!(0, c.value(1));
7394        assert_eq!(10241096, c.value(2));
7395        assert!(c.is_null(3));
7396
7397        // numerics to durations
7398        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7399        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7400        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7401        assert_eq!(i32::MAX as i64, c.value(0));
7402        assert_eq!(802034103, c.value(1));
7403        assert_eq!(10241096, c.value(2));
7404        assert!(c.is_null(3));
7405    }
7406
7407    #[test]
7408    fn test_cast_to_strings() {
7409        let a = Int32Array::from(vec![1, 2, 3]);
7410        let out = cast(&a, &DataType::Utf8).unwrap();
7411        let out = out
7412            .as_any()
7413            .downcast_ref::<StringArray>()
7414            .unwrap()
7415            .into_iter()
7416            .collect::<Vec<_>>();
7417        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7418        let out = cast(&a, &DataType::LargeUtf8).unwrap();
7419        let out = out
7420            .as_any()
7421            .downcast_ref::<LargeStringArray>()
7422            .unwrap()
7423            .into_iter()
7424            .collect::<Vec<_>>();
7425        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7426    }
7427
7428    #[test]
7429    fn test_str_to_str_casts() {
7430        for data in [
7431            vec![Some("foo"), Some("bar"), Some("ham")],
7432            vec![Some("foo"), None, Some("bar")],
7433        ] {
7434            let a = LargeStringArray::from(data.clone());
7435            let to = cast(&a, &DataType::Utf8).unwrap();
7436            let expect = a
7437                .as_any()
7438                .downcast_ref::<LargeStringArray>()
7439                .unwrap()
7440                .into_iter()
7441                .collect::<Vec<_>>();
7442            let out = to
7443                .as_any()
7444                .downcast_ref::<StringArray>()
7445                .unwrap()
7446                .into_iter()
7447                .collect::<Vec<_>>();
7448            assert_eq!(expect, out);
7449
7450            let a = StringArray::from(data);
7451            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7452            let expect = a
7453                .as_any()
7454                .downcast_ref::<StringArray>()
7455                .unwrap()
7456                .into_iter()
7457                .collect::<Vec<_>>();
7458            let out = to
7459                .as_any()
7460                .downcast_ref::<LargeStringArray>()
7461                .unwrap()
7462                .into_iter()
7463                .collect::<Vec<_>>();
7464            assert_eq!(expect, out);
7465        }
7466    }
7467
7468    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7469        Some("hello"),
7470        Some("repeated"),
7471        None,
7472        Some("large payload over 12 bytes"),
7473        Some("repeated"),
7474    ];
7475
7476    #[test]
7477    fn test_string_view_to_binary_view() {
7478        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7479
7480        assert!(can_cast_types(
7481            string_view_array.data_type(),
7482            &DataType::BinaryView
7483        ));
7484
7485        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7486        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7487
7488        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7489        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7490    }
7491
7492    #[test]
7493    fn test_binary_view_to_string_view() {
7494        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7495
7496        assert!(can_cast_types(
7497            binary_view_array.data_type(),
7498            &DataType::Utf8View
7499        ));
7500
7501        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7502        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7503
7504        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7505        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7506    }
7507
7508    #[test]
7509    fn test_binary_view_to_string_view_with_invalid_utf8() {
7510        let binary_view_array = BinaryViewArray::from_iter(vec![
7511            Some(b"valid".as_slice()),
7512            Some(&[0xff]),
7513            Some(b"utf8".as_slice()),
7514            None,
7515        ]);
7516
7517        let strict_options = CastOptions {
7518            safe: false,
7519            ..Default::default()
7520        };
7521
7522        assert!(
7523            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7524        );
7525
7526        let safe_options = CastOptions {
7527            safe: true,
7528            ..Default::default()
7529        };
7530
7531        let string_view_array =
7532            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7533        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7534
7535        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7536
7537        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7538    }
7539
7540    #[test]
7541    fn test_string_to_view() {
7542        _test_string_to_view::<i32>();
7543        _test_string_to_view::<i64>();
7544    }
7545
7546    fn _test_string_to_view<O>()
7547    where
7548        O: OffsetSizeTrait,
7549    {
7550        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7551
7552        assert!(can_cast_types(
7553            string_array.data_type(),
7554            &DataType::Utf8View
7555        ));
7556
7557        assert!(can_cast_types(
7558            string_array.data_type(),
7559            &DataType::BinaryView
7560        ));
7561
7562        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7563        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7564
7565        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7566        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7567
7568        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7569        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7570
7571        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7572        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7573    }
7574
7575    #[test]
7576    fn test_binary_to_view() {
7577        _test_binary_to_view::<i32>();
7578        _test_binary_to_view::<i64>();
7579    }
7580
7581    fn _test_binary_to_view<O>()
7582    where
7583        O: OffsetSizeTrait,
7584    {
7585        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7586
7587        assert!(can_cast_types(
7588            binary_array.data_type(),
7589            &DataType::Utf8View
7590        ));
7591
7592        assert!(can_cast_types(
7593            binary_array.data_type(),
7594            &DataType::BinaryView
7595        ));
7596
7597        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7598        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7599
7600        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7601        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7602
7603        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7604        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7605
7606        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7607        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7608    }
7609
7610    #[test]
7611    fn test_dict_to_view() {
7612        let values = StringArray::from_iter(VIEW_TEST_DATA);
7613        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7614        let string_dict_array =
7615            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7616        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7617
7618        let string_view_array = {
7619            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7620            for v in typed_dict {
7621                builder.append_option(v);
7622            }
7623            builder.finish()
7624        };
7625        let expected_string_array_type = string_view_array.data_type();
7626        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7627        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7628        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7629
7630        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7631        let binary_dict_array =
7632            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7633        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7634
7635        let binary_view_array = {
7636            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7637            for v in typed_binary_dict {
7638                builder.append_option(v);
7639            }
7640            builder.finish()
7641        };
7642        let expected_binary_array_type = binary_view_array.data_type();
7643        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7644        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7645        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7646    }
7647
7648    #[test]
7649    fn test_dict_to_view_null_dictionary_value_is_null() {
7650        // Ensure we preserve nulls in the values
7651        let keys = Int32Array::from_iter([Some(0), Some(1), Some(2), None, Some(1)]);
7652
7653        let values = StringArray::from(vec![Some("aa"), None, Some("a value over twelve bytes")]);
7654        let dict = DictionaryArray::<Int32Type>::try_new(keys.clone(), Arc::new(values)).unwrap();
7655        let casted = cast(&dict, &DataType::Utf8View).unwrap();
7656        assert_eq!(
7657            casted.as_string_view().iter().collect::<Vec<_>>(),
7658            vec![
7659                Some("aa"),
7660                None,
7661                Some("a value over twelve bytes"),
7662                None,
7663                None
7664            ]
7665        );
7666        // the same input cast to Utf8 goes through `unpack_dictionary` and always agreed
7667        let reference = cast(&dict, &DataType::Utf8).unwrap();
7668        assert_eq!(
7669            casted.as_string_view().iter().collect::<Vec<_>>(),
7670            reference.as_string::<i32>().iter().collect::<Vec<_>>()
7671        );
7672
7673        let values = BinaryArray::from_opt_vec(vec![
7674            Some(b"aa".as_slice()),
7675            None,
7676            Some(b"a value over twelve bytes"),
7677        ]);
7678        let dict = DictionaryArray::<Int32Type>::try_new(keys, Arc::new(values)).unwrap();
7679        let casted = cast(&dict, &DataType::BinaryView).unwrap();
7680        assert_eq!(
7681            casted.as_binary_view().iter().collect::<Vec<_>>(),
7682            vec![
7683                Some(b"aa".as_slice()),
7684                None,
7685                Some(b"a value over twelve bytes"),
7686                None,
7687                None
7688            ]
7689        );
7690    }
7691
7692    #[test]
7693    fn test_view_to_dict() {
7694        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7695        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7696        let casted_type = string_dict_array.data_type();
7697        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7698        assert_eq!(casted_dict_array.data_type(), casted_type);
7699        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7700
7701        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7702        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7703        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7704        let binary_dict_array =
7705            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7706        let casted_type = binary_dict_array.data_type();
7707        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7708        assert_eq!(casted_binary_array.data_type(), casted_type);
7709        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7710    }
7711
7712    #[test]
7713    fn test_view_to_string() {
7714        _test_view_to_string::<i32>();
7715        _test_view_to_string::<i64>();
7716    }
7717
7718    fn _test_view_to_string<O>()
7719    where
7720        O: OffsetSizeTrait,
7721    {
7722        let string_view_array = {
7723            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7724            for s in &VIEW_TEST_DATA {
7725                builder.append_option(*s);
7726            }
7727            builder.finish()
7728        };
7729
7730        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7731
7732        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7733        let expected_type = expected_string_array.data_type();
7734
7735        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7736        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7737
7738        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7739        assert_eq!(string_view_casted_array.data_type(), expected_type);
7740        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7741
7742        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7743        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7744        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7745    }
7746
7747    #[test]
7748    fn test_view_to_binary() {
7749        _test_view_to_binary::<i32>();
7750        _test_view_to_binary::<i64>();
7751    }
7752
7753    fn _test_view_to_binary<O>()
7754    where
7755        O: OffsetSizeTrait,
7756    {
7757        let view_array = {
7758            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7759            for s in &VIEW_TEST_DATA {
7760                builder.append_option(*s);
7761            }
7762            builder.finish()
7763        };
7764
7765        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7766        let expected_type = expected_binary_array.data_type();
7767
7768        assert!(can_cast_types(view_array.data_type(), expected_type));
7769
7770        let binary_array = cast(&view_array, expected_type).unwrap();
7771        assert_eq!(binary_array.data_type(), expected_type);
7772
7773        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7774    }
7775
7776    #[test]
7777    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
7778    fn test_cast_from_f64() {
7779        let f64_values: Vec<f64> = vec![
7780            i64::MIN as f64,
7781            i32::MIN as f64,
7782            i16::MIN as f64,
7783            i8::MIN as f64,
7784            0_f64,
7785            u8::MAX as f64,
7786            u16::MAX as f64,
7787            u32::MAX as f64,
7788            u64::MAX as f64,
7789        ];
7790        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7791
7792        let f64_expected = vec![
7793            -9223372036854776000.0,
7794            -2147483648.0,
7795            -32768.0,
7796            -128.0,
7797            0.0,
7798            255.0,
7799            65535.0,
7800            4294967295.0,
7801            18446744073709552000.0,
7802        ];
7803        assert_eq!(
7804            f64_expected,
7805            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7806                .iter()
7807                .map(|i| i.parse::<f64>().unwrap())
7808                .collect::<Vec<f64>>()
7809        );
7810
7811        let f32_expected = vec![
7812            -9223372000000000000.0,
7813            -2147483600.0,
7814            -32768.0,
7815            -128.0,
7816            0.0,
7817            255.0,
7818            65535.0,
7819            4294967300.0,
7820            18446744000000000000.0,
7821        ];
7822        assert_eq!(
7823            f32_expected,
7824            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7825                .iter()
7826                .map(|i| i.parse::<f32>().unwrap())
7827                .collect::<Vec<f32>>()
7828        );
7829
7830        let f16_expected = vec![
7831            f16::from_f64(-9223372000000000000.0),
7832            f16::from_f64(-2147483600.0),
7833            f16::from_f64(-32768.0),
7834            f16::from_f64(-128.0),
7835            f16::from_f64(0.0),
7836            f16::from_f64(255.0),
7837            f16::from_f64(65535.0),
7838            f16::from_f64(4294967300.0),
7839            f16::from_f64(18446744000000000000.0),
7840        ];
7841        assert_eq!(
7842            f16_expected,
7843            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7844                .iter()
7845                .map(|i| i.parse::<f16>().unwrap())
7846                .collect::<Vec<f16>>()
7847        );
7848
7849        let i64_expected = vec![
7850            "-9223372036854775808",
7851            "-2147483648",
7852            "-32768",
7853            "-128",
7854            "0",
7855            "255",
7856            "65535",
7857            "4294967295",
7858            "null",
7859        ];
7860        assert_eq!(
7861            i64_expected,
7862            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7863        );
7864
7865        let i32_expected = vec![
7866            "null",
7867            "-2147483648",
7868            "-32768",
7869            "-128",
7870            "0",
7871            "255",
7872            "65535",
7873            "null",
7874            "null",
7875        ];
7876        assert_eq!(
7877            i32_expected,
7878            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7879        );
7880
7881        let i16_expected = vec![
7882            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7883        ];
7884        assert_eq!(
7885            i16_expected,
7886            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7887        );
7888
7889        let i8_expected = vec![
7890            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7891        ];
7892        assert_eq!(
7893            i8_expected,
7894            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7895        );
7896
7897        let u64_expected = vec![
7898            "null",
7899            "null",
7900            "null",
7901            "null",
7902            "0",
7903            "255",
7904            "65535",
7905            "4294967295",
7906            "null",
7907        ];
7908        assert_eq!(
7909            u64_expected,
7910            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7911        );
7912
7913        let u32_expected = vec![
7914            "null",
7915            "null",
7916            "null",
7917            "null",
7918            "0",
7919            "255",
7920            "65535",
7921            "4294967295",
7922            "null",
7923        ];
7924        assert_eq!(
7925            u32_expected,
7926            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7927        );
7928
7929        let u16_expected = vec![
7930            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7931        ];
7932        assert_eq!(
7933            u16_expected,
7934            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7935        );
7936
7937        let u8_expected = vec![
7938            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7939        ];
7940        assert_eq!(
7941            u8_expected,
7942            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7943        );
7944    }
7945
7946    #[test]
7947    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
7948    fn test_cast_from_f32() {
7949        let f32_values: Vec<f32> = vec![
7950            i32::MIN as f32,
7951            i32::MIN as f32,
7952            i16::MIN as f32,
7953            i8::MIN as f32,
7954            0_f32,
7955            u8::MAX as f32,
7956            u16::MAX as f32,
7957            u32::MAX as f32,
7958            u32::MAX as f32,
7959        ];
7960        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7961
7962        let f64_expected = vec![
7963            "-2147483648.0",
7964            "-2147483648.0",
7965            "-32768.0",
7966            "-128.0",
7967            "0.0",
7968            "255.0",
7969            "65535.0",
7970            "4294967296.0",
7971            "4294967296.0",
7972        ];
7973        assert_eq!(
7974            f64_expected,
7975            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7976        );
7977
7978        let f32_expected = vec![
7979            "-2147483600.0",
7980            "-2147483600.0",
7981            "-32768.0",
7982            "-128.0",
7983            "0.0",
7984            "255.0",
7985            "65535.0",
7986            "4294967300.0",
7987            "4294967300.0",
7988        ];
7989        assert_eq!(
7990            f32_expected,
7991            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7992        );
7993
7994        let f16_expected = vec![
7995            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7996        ];
7997        assert_eq!(
7998            f16_expected,
7999            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
8000        );
8001
8002        let i64_expected = vec![
8003            "-2147483648",
8004            "-2147483648",
8005            "-32768",
8006            "-128",
8007            "0",
8008            "255",
8009            "65535",
8010            "4294967296",
8011            "4294967296",
8012        ];
8013        assert_eq!(
8014            i64_expected,
8015            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
8016        );
8017
8018        let i32_expected = vec![
8019            "-2147483648",
8020            "-2147483648",
8021            "-32768",
8022            "-128",
8023            "0",
8024            "255",
8025            "65535",
8026            "null",
8027            "null",
8028        ];
8029        assert_eq!(
8030            i32_expected,
8031            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
8032        );
8033
8034        let i16_expected = vec![
8035            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
8036        ];
8037        assert_eq!(
8038            i16_expected,
8039            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
8040        );
8041
8042        let i8_expected = vec![
8043            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
8044        ];
8045        assert_eq!(
8046            i8_expected,
8047            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
8048        );
8049
8050        let u64_expected = vec![
8051            "null",
8052            "null",
8053            "null",
8054            "null",
8055            "0",
8056            "255",
8057            "65535",
8058            "4294967296",
8059            "4294967296",
8060        ];
8061        assert_eq!(
8062            u64_expected,
8063            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
8064        );
8065
8066        let u32_expected = vec![
8067            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8068        ];
8069        assert_eq!(
8070            u32_expected,
8071            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
8072        );
8073
8074        let u16_expected = vec![
8075            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8076        ];
8077        assert_eq!(
8078            u16_expected,
8079            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
8080        );
8081
8082        let u8_expected = vec![
8083            "null", "null", "null", "null", "0", "255", "null", "null", "null",
8084        ];
8085        assert_eq!(
8086            u8_expected,
8087            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
8088        );
8089    }
8090
8091    #[test]
8092    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8093    fn test_cast_from_uint64() {
8094        let u64_values: Vec<u64> = vec![
8095            0,
8096            u8::MAX as u64,
8097            u16::MAX as u64,
8098            u32::MAX as u64,
8099            u64::MAX,
8100        ];
8101        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
8102
8103        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
8104        assert_eq!(
8105            f64_expected,
8106            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
8107                .iter()
8108                .map(|i| i.parse::<f64>().unwrap())
8109                .collect::<Vec<f64>>()
8110        );
8111
8112        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
8113        assert_eq!(
8114            f32_expected,
8115            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
8116                .iter()
8117                .map(|i| i.parse::<f32>().unwrap())
8118                .collect::<Vec<f32>>()
8119        );
8120
8121        let f16_expected = vec![
8122            f16::from_f64(0.0),
8123            f16::from_f64(255.0),
8124            f16::from_f64(65535.0),
8125            f16::from_f64(4294967300.0),
8126            f16::from_f64(18446744000000000000.0),
8127        ];
8128        assert_eq!(
8129            f16_expected,
8130            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
8131                .iter()
8132                .map(|i| i.parse::<f16>().unwrap())
8133                .collect::<Vec<f16>>()
8134        );
8135
8136        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
8137        assert_eq!(
8138            i64_expected,
8139            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
8140        );
8141
8142        let i32_expected = vec!["0", "255", "65535", "null", "null"];
8143        assert_eq!(
8144            i32_expected,
8145            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
8146        );
8147
8148        let i16_expected = vec!["0", "255", "null", "null", "null"];
8149        assert_eq!(
8150            i16_expected,
8151            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
8152        );
8153
8154        let i8_expected = vec!["0", "null", "null", "null", "null"];
8155        assert_eq!(
8156            i8_expected,
8157            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
8158        );
8159
8160        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
8161        assert_eq!(
8162            u64_expected,
8163            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
8164        );
8165
8166        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
8167        assert_eq!(
8168            u32_expected,
8169            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
8170        );
8171
8172        let u16_expected = vec!["0", "255", "65535", "null", "null"];
8173        assert_eq!(
8174            u16_expected,
8175            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
8176        );
8177
8178        let u8_expected = vec!["0", "255", "null", "null", "null"];
8179        assert_eq!(
8180            u8_expected,
8181            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
8182        );
8183    }
8184
8185    #[test]
8186    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8187    fn test_cast_from_uint32() {
8188        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
8189        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
8190
8191        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
8192        assert_eq!(
8193            f64_expected,
8194            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
8195        );
8196
8197        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
8198        assert_eq!(
8199            f32_expected,
8200            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
8201        );
8202
8203        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
8204        assert_eq!(
8205            f16_expected,
8206            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
8207        );
8208
8209        let i64_expected = vec!["0", "255", "65535", "4294967295"];
8210        assert_eq!(
8211            i64_expected,
8212            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
8213        );
8214
8215        let i32_expected = vec!["0", "255", "65535", "null"];
8216        assert_eq!(
8217            i32_expected,
8218            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
8219        );
8220
8221        let i16_expected = vec!["0", "255", "null", "null"];
8222        assert_eq!(
8223            i16_expected,
8224            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
8225        );
8226
8227        let i8_expected = vec!["0", "null", "null", "null"];
8228        assert_eq!(
8229            i8_expected,
8230            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
8231        );
8232
8233        let u64_expected = vec!["0", "255", "65535", "4294967295"];
8234        assert_eq!(
8235            u64_expected,
8236            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
8237        );
8238
8239        let u32_expected = vec!["0", "255", "65535", "4294967295"];
8240        assert_eq!(
8241            u32_expected,
8242            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
8243        );
8244
8245        let u16_expected = vec!["0", "255", "65535", "null"];
8246        assert_eq!(
8247            u16_expected,
8248            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
8249        );
8250
8251        let u8_expected = vec!["0", "255", "null", "null"];
8252        assert_eq!(
8253            u8_expected,
8254            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
8255        );
8256    }
8257
8258    #[test]
8259    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8260    fn test_cast_from_uint16() {
8261        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
8262        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
8263
8264        let f64_expected = vec!["0.0", "255.0", "65535.0"];
8265        assert_eq!(
8266            f64_expected,
8267            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
8268        );
8269
8270        let f32_expected = vec!["0.0", "255.0", "65535.0"];
8271        assert_eq!(
8272            f32_expected,
8273            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
8274        );
8275
8276        let f16_expected = vec!["0.0", "255.0", "inf"];
8277        assert_eq!(
8278            f16_expected,
8279            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
8280        );
8281
8282        let i64_expected = vec!["0", "255", "65535"];
8283        assert_eq!(
8284            i64_expected,
8285            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
8286        );
8287
8288        let i32_expected = vec!["0", "255", "65535"];
8289        assert_eq!(
8290            i32_expected,
8291            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
8292        );
8293
8294        let i16_expected = vec!["0", "255", "null"];
8295        assert_eq!(
8296            i16_expected,
8297            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
8298        );
8299
8300        let i8_expected = vec!["0", "null", "null"];
8301        assert_eq!(
8302            i8_expected,
8303            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
8304        );
8305
8306        let u64_expected = vec!["0", "255", "65535"];
8307        assert_eq!(
8308            u64_expected,
8309            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
8310        );
8311
8312        let u32_expected = vec!["0", "255", "65535"];
8313        assert_eq!(
8314            u32_expected,
8315            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
8316        );
8317
8318        let u16_expected = vec!["0", "255", "65535"];
8319        assert_eq!(
8320            u16_expected,
8321            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
8322        );
8323
8324        let u8_expected = vec!["0", "255", "null"];
8325        assert_eq!(
8326            u8_expected,
8327            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
8328        );
8329    }
8330
8331    #[test]
8332    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8333    fn test_cast_from_uint8() {
8334        let u8_values: Vec<u8> = vec![0, u8::MAX];
8335        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
8336
8337        let f64_expected = vec!["0.0", "255.0"];
8338        assert_eq!(
8339            f64_expected,
8340            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
8341        );
8342
8343        let f32_expected = vec!["0.0", "255.0"];
8344        assert_eq!(
8345            f32_expected,
8346            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
8347        );
8348
8349        let f16_expected = vec!["0.0", "255.0"];
8350        assert_eq!(
8351            f16_expected,
8352            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
8353        );
8354
8355        let i64_expected = vec!["0", "255"];
8356        assert_eq!(
8357            i64_expected,
8358            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
8359        );
8360
8361        let i32_expected = vec!["0", "255"];
8362        assert_eq!(
8363            i32_expected,
8364            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
8365        );
8366
8367        let i16_expected = vec!["0", "255"];
8368        assert_eq!(
8369            i16_expected,
8370            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
8371        );
8372
8373        let i8_expected = vec!["0", "null"];
8374        assert_eq!(
8375            i8_expected,
8376            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
8377        );
8378
8379        let u64_expected = vec!["0", "255"];
8380        assert_eq!(
8381            u64_expected,
8382            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
8383        );
8384
8385        let u32_expected = vec!["0", "255"];
8386        assert_eq!(
8387            u32_expected,
8388            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
8389        );
8390
8391        let u16_expected = vec!["0", "255"];
8392        assert_eq!(
8393            u16_expected,
8394            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
8395        );
8396
8397        let u8_expected = vec!["0", "255"];
8398        assert_eq!(
8399            u8_expected,
8400            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
8401        );
8402    }
8403
8404    #[test]
8405    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8406    fn test_cast_from_int64() {
8407        let i64_values: Vec<i64> = vec![
8408            i64::MIN,
8409            i32::MIN as i64,
8410            i16::MIN as i64,
8411            i8::MIN as i64,
8412            0,
8413            i8::MAX as i64,
8414            i16::MAX as i64,
8415            i32::MAX as i64,
8416            i64::MAX,
8417        ];
8418        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
8419
8420        let f64_expected = vec![
8421            -9223372036854776000.0,
8422            -2147483648.0,
8423            -32768.0,
8424            -128.0,
8425            0.0,
8426            127.0,
8427            32767.0,
8428            2147483647.0,
8429            9223372036854776000.0,
8430        ];
8431        assert_eq!(
8432            f64_expected,
8433            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
8434                .iter()
8435                .map(|i| i.parse::<f64>().unwrap())
8436                .collect::<Vec<f64>>()
8437        );
8438
8439        let f32_expected = vec![
8440            -9223372000000000000.0,
8441            -2147483600.0,
8442            -32768.0,
8443            -128.0,
8444            0.0,
8445            127.0,
8446            32767.0,
8447            2147483600.0,
8448            9223372000000000000.0,
8449        ];
8450        assert_eq!(
8451            f32_expected,
8452            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8453                .iter()
8454                .map(|i| i.parse::<f32>().unwrap())
8455                .collect::<Vec<f32>>()
8456        );
8457
8458        let f16_expected = vec![
8459            f16::from_f64(-9223372000000000000.0),
8460            f16::from_f64(-2147483600.0),
8461            f16::from_f64(-32768.0),
8462            f16::from_f64(-128.0),
8463            f16::from_f64(0.0),
8464            f16::from_f64(127.0),
8465            f16::from_f64(32767.0),
8466            f16::from_f64(2147483600.0),
8467            f16::from_f64(9223372000000000000.0),
8468        ];
8469        assert_eq!(
8470            f16_expected,
8471            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8472                .iter()
8473                .map(|i| i.parse::<f16>().unwrap())
8474                .collect::<Vec<f16>>()
8475        );
8476
8477        let i64_expected = vec![
8478            "-9223372036854775808",
8479            "-2147483648",
8480            "-32768",
8481            "-128",
8482            "0",
8483            "127",
8484            "32767",
8485            "2147483647",
8486            "9223372036854775807",
8487        ];
8488        assert_eq!(
8489            i64_expected,
8490            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8491        );
8492
8493        let i32_expected = vec![
8494            "null",
8495            "-2147483648",
8496            "-32768",
8497            "-128",
8498            "0",
8499            "127",
8500            "32767",
8501            "2147483647",
8502            "null",
8503        ];
8504        assert_eq!(
8505            i32_expected,
8506            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8507        );
8508
8509        assert_eq!(
8510            i32_expected,
8511            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8512        );
8513
8514        let i16_expected = vec![
8515            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8516        ];
8517        assert_eq!(
8518            i16_expected,
8519            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8520        );
8521
8522        let i8_expected = vec![
8523            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8524        ];
8525        assert_eq!(
8526            i8_expected,
8527            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8528        );
8529
8530        let u64_expected = vec![
8531            "null",
8532            "null",
8533            "null",
8534            "null",
8535            "0",
8536            "127",
8537            "32767",
8538            "2147483647",
8539            "9223372036854775807",
8540        ];
8541        assert_eq!(
8542            u64_expected,
8543            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8544        );
8545
8546        let u32_expected = vec![
8547            "null",
8548            "null",
8549            "null",
8550            "null",
8551            "0",
8552            "127",
8553            "32767",
8554            "2147483647",
8555            "null",
8556        ];
8557        assert_eq!(
8558            u32_expected,
8559            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8560        );
8561
8562        let u16_expected = vec![
8563            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8564        ];
8565        assert_eq!(
8566            u16_expected,
8567            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8568        );
8569
8570        let u8_expected = vec![
8571            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8572        ];
8573        assert_eq!(
8574            u8_expected,
8575            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8576        );
8577    }
8578
8579    #[test]
8580    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8581    fn test_cast_from_int32() {
8582        let i32_values: Vec<i32> = vec![
8583            i32::MIN,
8584            i16::MIN as i32,
8585            i8::MIN as i32,
8586            0,
8587            i8::MAX as i32,
8588            i16::MAX as i32,
8589            i32::MAX,
8590        ];
8591        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8592
8593        let f64_expected = vec![
8594            "-2147483648.0",
8595            "-32768.0",
8596            "-128.0",
8597            "0.0",
8598            "127.0",
8599            "32767.0",
8600            "2147483647.0",
8601        ];
8602        assert_eq!(
8603            f64_expected,
8604            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8605        );
8606
8607        let f32_expected = vec![
8608            "-2147483600.0",
8609            "-32768.0",
8610            "-128.0",
8611            "0.0",
8612            "127.0",
8613            "32767.0",
8614            "2147483600.0",
8615        ];
8616        assert_eq!(
8617            f32_expected,
8618            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8619        );
8620
8621        let f16_expected = vec![
8622            f16::from_f64(-2147483600.0),
8623            f16::from_f64(-32768.0),
8624            f16::from_f64(-128.0),
8625            f16::from_f64(0.0),
8626            f16::from_f64(127.0),
8627            f16::from_f64(32767.0),
8628            f16::from_f64(2147483600.0),
8629        ];
8630        assert_eq!(
8631            f16_expected,
8632            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8633                .iter()
8634                .map(|i| i.parse::<f16>().unwrap())
8635                .collect::<Vec<f16>>()
8636        );
8637
8638        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8639        assert_eq!(
8640            i16_expected,
8641            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8642        );
8643
8644        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8645        assert_eq!(
8646            i8_expected,
8647            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8648        );
8649
8650        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8651        assert_eq!(
8652            u64_expected,
8653            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8654        );
8655
8656        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8657        assert_eq!(
8658            u32_expected,
8659            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8660        );
8661
8662        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8663        assert_eq!(
8664            u16_expected,
8665            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8666        );
8667
8668        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8669        assert_eq!(
8670            u8_expected,
8671            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8672        );
8673
8674        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8675        let i64_expected = vec![
8676            "-185542587187200000",
8677            "-2831155200000",
8678            "-11059200000",
8679            "0",
8680            "10972800000",
8681            "2831068800000",
8682            "185542587100800000",
8683        ];
8684        assert_eq!(
8685            i64_expected,
8686            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8687        );
8688    }
8689
8690    #[test]
8691    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8692    fn test_cast_from_int16() {
8693        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8694        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8695
8696        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8697        assert_eq!(
8698            f64_expected,
8699            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8700        );
8701
8702        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8703        assert_eq!(
8704            f32_expected,
8705            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8706        );
8707
8708        let f16_expected = vec![
8709            f16::from_f64(-32768.0),
8710            f16::from_f64(-128.0),
8711            f16::from_f64(0.0),
8712            f16::from_f64(127.0),
8713            f16::from_f64(32767.0),
8714        ];
8715        assert_eq!(
8716            f16_expected,
8717            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8718                .iter()
8719                .map(|i| i.parse::<f16>().unwrap())
8720                .collect::<Vec<f16>>()
8721        );
8722
8723        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8724        assert_eq!(
8725            i64_expected,
8726            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8727        );
8728
8729        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8730        assert_eq!(
8731            i32_expected,
8732            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8733        );
8734
8735        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8736        assert_eq!(
8737            i16_expected,
8738            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8739        );
8740
8741        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8742        assert_eq!(
8743            i8_expected,
8744            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8745        );
8746
8747        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8748        assert_eq!(
8749            u64_expected,
8750            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8751        );
8752
8753        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8754        assert_eq!(
8755            u32_expected,
8756            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8757        );
8758
8759        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8760        assert_eq!(
8761            u16_expected,
8762            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8763        );
8764
8765        let u8_expected = vec!["null", "null", "0", "127", "null"];
8766        assert_eq!(
8767            u8_expected,
8768            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8769        );
8770    }
8771
8772    #[test]
8773    fn test_cast_from_date32() {
8774        let i32_values: Vec<i32> = vec![
8775            i32::MIN,
8776            i16::MIN as i32,
8777            i8::MIN as i32,
8778            0,
8779            i8::MAX as i32,
8780            i16::MAX as i32,
8781            i32::MAX,
8782        ];
8783        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8784
8785        let i64_expected = vec![
8786            "-2147483648",
8787            "-32768",
8788            "-128",
8789            "0",
8790            "127",
8791            "32767",
8792            "2147483647",
8793        ];
8794        assert_eq!(
8795            i64_expected,
8796            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8797        );
8798    }
8799
8800    #[test]
8801    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8802    fn test_cast_from_int8() {
8803        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8804        let i8_array = Int8Array::from(i8_values);
8805
8806        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8807        assert_eq!(
8808            f64_expected,
8809            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8810        );
8811
8812        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8813        assert_eq!(
8814            f32_expected,
8815            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8816        );
8817
8818        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8819        assert_eq!(
8820            f16_expected,
8821            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8822        );
8823
8824        let i64_expected = vec!["-128", "0", "127"];
8825        assert_eq!(
8826            i64_expected,
8827            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8828        );
8829
8830        let i32_expected = vec!["-128", "0", "127"];
8831        assert_eq!(
8832            i32_expected,
8833            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8834        );
8835
8836        let i16_expected = vec!["-128", "0", "127"];
8837        assert_eq!(
8838            i16_expected,
8839            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8840        );
8841
8842        let i8_expected = vec!["-128", "0", "127"];
8843        assert_eq!(
8844            i8_expected,
8845            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8846        );
8847
8848        let u64_expected = vec!["null", "0", "127"];
8849        assert_eq!(
8850            u64_expected,
8851            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8852        );
8853
8854        let u32_expected = vec!["null", "0", "127"];
8855        assert_eq!(
8856            u32_expected,
8857            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8858        );
8859
8860        let u16_expected = vec!["null", "0", "127"];
8861        assert_eq!(
8862            u16_expected,
8863            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8864        );
8865
8866        let u8_expected = vec!["null", "0", "127"];
8867        assert_eq!(
8868            u8_expected,
8869            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8870        );
8871    }
8872
8873    /// Convert `array` into a vector of strings by casting to data type dt
8874    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8875    where
8876        T: ArrowPrimitiveType,
8877    {
8878        let c = cast(array, dt).unwrap();
8879        let a = c.as_primitive::<T>();
8880        let mut v: Vec<String> = vec![];
8881        for i in 0..array.len() {
8882            if a.is_null(i) {
8883                v.push("null".to_string())
8884            } else {
8885                v.push(format!("{:?}", a.value(i)));
8886            }
8887        }
8888        v
8889    }
8890
8891    #[test]
8892    fn test_cast_utf8_dict() {
8893        // FROM a dictionary with of Utf8 values
8894        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8895        builder.append("one").unwrap();
8896        builder.append_null();
8897        builder.append("three").unwrap();
8898        let array: ArrayRef = Arc::new(builder.finish());
8899
8900        let expected = vec!["one", "null", "three"];
8901
8902        // Test casting TO StringArray
8903        let cast_type = Utf8;
8904        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8905        assert_eq!(cast_array.data_type(), &cast_type);
8906        assert_eq!(array_to_strings(&cast_array), expected);
8907
8908        // Test casting TO Dictionary (with different index sizes)
8909
8910        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8911        let cast_array = cast(&array, &cast_type).expect("cast failed");
8912        assert_eq!(cast_array.data_type(), &cast_type);
8913        assert_eq!(array_to_strings(&cast_array), expected);
8914
8915        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8916        let cast_array = cast(&array, &cast_type).expect("cast failed");
8917        assert_eq!(cast_array.data_type(), &cast_type);
8918        assert_eq!(array_to_strings(&cast_array), expected);
8919
8920        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8921        let cast_array = cast(&array, &cast_type).expect("cast failed");
8922        assert_eq!(cast_array.data_type(), &cast_type);
8923        assert_eq!(array_to_strings(&cast_array), expected);
8924
8925        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8926        let cast_array = cast(&array, &cast_type).expect("cast failed");
8927        assert_eq!(cast_array.data_type(), &cast_type);
8928        assert_eq!(array_to_strings(&cast_array), expected);
8929
8930        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8931        let cast_array = cast(&array, &cast_type).expect("cast failed");
8932        assert_eq!(cast_array.data_type(), &cast_type);
8933        assert_eq!(array_to_strings(&cast_array), expected);
8934
8935        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8936        let cast_array = cast(&array, &cast_type).expect("cast failed");
8937        assert_eq!(cast_array.data_type(), &cast_type);
8938        assert_eq!(array_to_strings(&cast_array), expected);
8939
8940        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8941        let cast_array = cast(&array, &cast_type).expect("cast failed");
8942        assert_eq!(cast_array.data_type(), &cast_type);
8943        assert_eq!(array_to_strings(&cast_array), expected);
8944    }
8945
8946    #[test]
8947    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8948        // test converting from an array that has indexes of a type
8949        // that are out of bounds for a particular other kind of
8950        // index.
8951
8952        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8953
8954        // add 200 distinct values (which can be stored by a
8955        // dictionary indexed by int32, but not a dictionary indexed
8956        // with int8)
8957        for i in 0..200 {
8958            builder.append(i).unwrap();
8959        }
8960        let array: ArrayRef = Arc::new(builder.finish());
8961
8962        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8963        let res = cast(&array, &cast_type);
8964        assert!(res.is_err());
8965        let actual_error = format!("{res:?}");
8966        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8967        assert!(
8968            actual_error.contains(expected_error),
8969            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8970        );
8971    }
8972
8973    #[test]
8974    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8975        // Same test as test_cast_dict_to_dict_bad_index_value but use
8976        // string values (and encode the expected behavior here);
8977
8978        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8979
8980        // add 200 distinct values (which can be stored by a
8981        // dictionary indexed by int32, but not a dictionary indexed
8982        // with int8)
8983        for i in 0..200 {
8984            let val = format!("val{i}");
8985            builder.append(&val).unwrap();
8986        }
8987        let array = builder.finish();
8988
8989        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8990        let res = cast(&array, &cast_type);
8991        assert!(res.is_err());
8992        let actual_error = format!("{res:?}");
8993        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8994        assert!(
8995            actual_error.contains(expected_error),
8996            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8997        );
8998    }
8999
9000    #[test]
9001    fn test_cast_nested_dictionary_to_dictionary_reuses_values() {
9002        let inner = DictionaryArray::<Int32Type>::new(
9003            Int32Array::from(vec![Some(0), None, Some(1)]),
9004            Arc::new(StringArray::from(vec!["x", "y"])),
9005        );
9006        let nested = DictionaryArray::<Int32Type>::new(
9007            Int32Array::from(vec![Some(0), Some(1), Some(2), None, Some(0)]),
9008            Arc::new(inner),
9009        );
9010
9011        let result = cast(&nested, &Dictionary(Box::new(Int32), Box::new(Utf8))).unwrap();
9012        let result = result.as_dictionary::<Int32Type>();
9013
9014        assert_eq!(
9015            result.keys(),
9016            &Int32Array::from(vec![Some(0), None, Some(1), None, Some(0)])
9017        );
9018        assert_eq!(
9019            result.values().as_string::<i32>(),
9020            &StringArray::from(vec!["x", "y"])
9021        );
9022        let logical: Vec<Option<&str>> = result
9023            .downcast_dict::<StringArray>()
9024            .unwrap()
9025            .into_iter()
9026            .collect();
9027        assert_eq!(logical, vec![Some("x"), None, Some("y"), None, Some("x")]);
9028    }
9029
9030    #[test]
9031    fn test_cast_primitive_dict() {
9032        // FROM a dictionary with of INT32 values
9033        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
9034        builder.append(1).unwrap();
9035        builder.append_null();
9036        builder.append(3).unwrap();
9037        let array: ArrayRef = Arc::new(builder.finish());
9038
9039        let expected = vec!["1", "null", "3"];
9040
9041        // Test casting TO PrimitiveArray, different dictionary type
9042        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
9043        assert_eq!(array_to_strings(&cast_array), expected);
9044        assert_eq!(cast_array.data_type(), &Utf8);
9045
9046        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
9047        assert_eq!(array_to_strings(&cast_array), expected);
9048        assert_eq!(cast_array.data_type(), &Int64);
9049    }
9050
9051    #[test]
9052    fn test_cast_primitive_array_to_dict() {
9053        let mut builder = PrimitiveBuilder::<Int32Type>::new();
9054        builder.append_value(1);
9055        builder.append_null();
9056        builder.append_value(3);
9057        let array: ArrayRef = Arc::new(builder.finish());
9058
9059        let expected = vec!["1", "null", "3"];
9060
9061        // Cast to a dictionary (same value type, Int32)
9062        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
9063        let cast_array = cast(&array, &cast_type).expect("cast failed");
9064        assert_eq!(cast_array.data_type(), &cast_type);
9065        assert_eq!(array_to_strings(&cast_array), expected);
9066
9067        // Cast to a dictionary (different value type, Int8)
9068        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
9069        let cast_array = cast(&array, &cast_type).expect("cast failed");
9070        assert_eq!(cast_array.data_type(), &cast_type);
9071        assert_eq!(array_to_strings(&cast_array), expected);
9072    }
9073
9074    #[test]
9075    fn test_cast_time_array_to_dict() {
9076        use DataType::*;
9077
9078        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
9079
9080        let expected = vec!["1972-09-27", "null", "1975-06-24"];
9081
9082        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
9083        let cast_array = cast(&array, &cast_type).expect("cast failed");
9084        assert_eq!(cast_array.data_type(), &cast_type);
9085        assert_eq!(array_to_strings(&cast_array), expected);
9086    }
9087
9088    #[test]
9089    fn test_cast_timestamp_array_to_dict() {
9090        use DataType::*;
9091
9092        let array = Arc::new(
9093            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
9094        ) as ArrayRef;
9095
9096        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
9097
9098        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
9099        let cast_array = cast(&array, &cast_type).expect("cast failed");
9100        assert_eq!(cast_array.data_type(), &cast_type);
9101        assert_eq!(array_to_strings(&cast_array), expected);
9102    }
9103
9104    #[test]
9105    fn test_cast_string_array_to_dict() {
9106        use DataType::*;
9107
9108        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
9109
9110        let expected = vec!["one", "null", "three"];
9111
9112        // Cast to a dictionary (same value type, Utf8)
9113        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
9114        let cast_array = cast(&array, &cast_type).expect("cast failed");
9115        assert_eq!(cast_array.data_type(), &cast_type);
9116        assert_eq!(array_to_strings(&cast_array), expected);
9117    }
9118
9119    #[test]
9120    fn test_cast_null_array_to_from_decimal_array() {
9121        let data_type = DataType::Decimal128(12, 4);
9122        let array = new_null_array(&DataType::Null, 4);
9123        assert_eq!(array.data_type(), &DataType::Null);
9124        let cast_array = cast(&array, &data_type).expect("cast failed");
9125        assert_eq!(cast_array.data_type(), &data_type);
9126        for i in 0..4 {
9127            assert!(cast_array.is_null(i));
9128        }
9129
9130        let array = new_null_array(&data_type, 4);
9131        assert_eq!(array.data_type(), &data_type);
9132        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
9133        assert_eq!(cast_array.data_type(), &DataType::Null);
9134        assert_eq!(cast_array.len(), 4);
9135        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
9136    }
9137
9138    #[test]
9139    fn test_cast_null_array_from_and_to_primitive_array() {
9140        macro_rules! typed_test {
9141            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
9142                {
9143                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
9144                    let expected = $ARR_TYPE::from(vec![None; 6]);
9145                    let cast_type = DataType::$DATATYPE;
9146                    let cast_array = cast(&array, &cast_type).expect("cast failed");
9147                    let cast_array = cast_array.as_primitive::<$TYPE>();
9148                    assert_eq!(cast_array.data_type(), &cast_type);
9149                    assert_eq!(cast_array, &expected);
9150                }
9151            }};
9152        }
9153
9154        typed_test!(Int16Array, Int16, Int16Type);
9155        typed_test!(Int32Array, Int32, Int32Type);
9156        typed_test!(Int64Array, Int64, Int64Type);
9157
9158        typed_test!(UInt16Array, UInt16, UInt16Type);
9159        typed_test!(UInt32Array, UInt32, UInt32Type);
9160        typed_test!(UInt64Array, UInt64, UInt64Type);
9161
9162        typed_test!(Float16Array, Float16, Float16Type);
9163        typed_test!(Float32Array, Float32, Float32Type);
9164        typed_test!(Float64Array, Float64, Float64Type);
9165
9166        typed_test!(Date32Array, Date32, Date32Type);
9167        typed_test!(Date64Array, Date64, Date64Type);
9168    }
9169
9170    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
9171        // Cast from null to data_type
9172        let array = new_null_array(&DataType::Null, 4);
9173        assert_eq!(array.data_type(), &DataType::Null);
9174        let cast_array = cast(&array, data_type).expect("cast failed");
9175        assert_eq!(cast_array.data_type(), data_type);
9176        for i in 0..4 {
9177            if is_complex {
9178                assert!(cast_array.logical_nulls().unwrap().is_null(i));
9179            } else {
9180                assert!(cast_array.is_null(i));
9181            }
9182        }
9183    }
9184
9185    fn cast_from_null_to_other(data_type: &DataType) {
9186        cast_from_null_to_other_base(data_type, false);
9187    }
9188
9189    fn cast_from_null_to_other_complex(data_type: &DataType) {
9190        cast_from_null_to_other_base(data_type, true);
9191    }
9192
9193    #[test]
9194    fn test_cast_null_from_and_to_variable_sized() {
9195        cast_from_null_to_other(&DataType::Utf8);
9196        cast_from_null_to_other(&DataType::LargeUtf8);
9197        cast_from_null_to_other(&DataType::Binary);
9198        cast_from_null_to_other(&DataType::LargeBinary);
9199    }
9200
9201    #[test]
9202    fn test_cast_null_from_and_to_nested_type() {
9203        // Cast null from and to map
9204        let data_type = DataType::Map(
9205            Arc::new(Field::new_struct(
9206                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
9207                vec![
9208                    Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
9209                    Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Int32, true),
9210                ],
9211                false,
9212            )),
9213            false,
9214        );
9215        cast_from_null_to_other(&data_type);
9216
9217        // Cast null from and to list
9218        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
9219        cast_from_null_to_other(&data_type);
9220        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9221        cast_from_null_to_other(&data_type);
9222        let data_type =
9223            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
9224        cast_from_null_to_other(&data_type);
9225
9226        // Cast null from and to dictionary
9227        let values = vec![None, None, None, None] as Vec<Option<&str>>;
9228        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
9229        let array = Arc::new(array) as ArrayRef;
9230        let data_type = array.data_type().to_owned();
9231        cast_from_null_to_other(&data_type);
9232
9233        // Cast null from and to struct
9234        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
9235        cast_from_null_to_other(&data_type);
9236
9237        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
9238        cast_from_null_to_other(&target_type);
9239
9240        let target_type =
9241            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
9242        cast_from_null_to_other(&target_type);
9243
9244        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
9245        let target_type = DataType::Union(fields, UnionMode::Sparse);
9246        cast_from_null_to_other_complex(&target_type);
9247
9248        let target_type = DataType::RunEndEncoded(
9249            Arc::new(Field::new("item", DataType::Int32, true)),
9250            Arc::new(Field::new("item", DataType::Int32, true)),
9251        );
9252        cast_from_null_to_other_complex(&target_type);
9253    }
9254
9255    /// Print the `DictionaryArray` `array` as a vector of strings
9256    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
9257        let options = FormatOptions::new().with_null("null");
9258        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
9259        (0..array.len())
9260            .map(|i| formatter.value(i).to_string())
9261            .collect()
9262    }
9263
9264    #[test]
9265    fn test_cast_utf8_to_date32() {
9266        use chrono::NaiveDate;
9267        let from_ymd = chrono::NaiveDate::from_ymd_opt;
9268        let since = chrono::NaiveDate::signed_duration_since;
9269
9270        let a = StringArray::from(vec![
9271            "2000-01-01",          // valid date with leading 0s
9272            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
9273            "2000-2-2",            // valid date without leading 0s
9274            "2000-00-00",          // invalid month and day
9275            "2000",                // just a year is invalid
9276        ]);
9277        let array = Arc::new(a) as ArrayRef;
9278        let b = cast(&array, &DataType::Date32).unwrap();
9279        let c = b.as_primitive::<Date32Type>();
9280
9281        // test valid inputs
9282        let date_value = since(
9283            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
9284            from_ymd(1970, 1, 1).unwrap(),
9285        )
9286        .num_days() as i32;
9287        assert!(c.is_valid(0)); // "2000-01-01"
9288        assert_eq!(date_value, c.value(0));
9289
9290        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
9291        assert_eq!(date_value, c.value(1));
9292
9293        let date_value = since(
9294            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
9295            from_ymd(1970, 1, 1).unwrap(),
9296        )
9297        .num_days() as i32;
9298        assert!(c.is_valid(2)); // "2000-2-2"
9299        assert_eq!(date_value, c.value(2));
9300
9301        // test invalid inputs
9302        assert!(!c.is_valid(3)); // "2000-00-00"
9303        assert!(!c.is_valid(4)); // "2000"
9304    }
9305
9306    #[test]
9307    fn test_cast_utf8_to_date64() {
9308        let a = StringArray::from(vec![
9309            "2000-01-01T12:00:00", // date + time valid
9310            "2020-12-15T12:34:56", // date + time valid
9311            "2020-2-2T12:34:56",   // valid date time without leading 0s
9312            "2000-00-00T12:00:00", // invalid month and day
9313            "2000-01-01 12:00:00", // missing the 'T'
9314            "2000-01-01",          // just a date is invalid
9315        ]);
9316        let array = Arc::new(a) as ArrayRef;
9317        let b = cast(&array, &DataType::Date64).unwrap();
9318        let c = b.as_primitive::<Date64Type>();
9319
9320        // test valid inputs
9321        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
9322        assert_eq!(946728000000, c.value(0));
9323        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
9324        assert_eq!(1608035696000, c.value(1));
9325        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
9326
9327        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
9328        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
9329        assert_eq!(946728000000, c.value(4));
9330        assert!(c.is_valid(5)); // "2000-01-01"
9331        assert_eq!(946684800000, c.value(5));
9332    }
9333
9334    #[test]
9335    fn test_utf8_cast_offsets() {
9336        // test if offset of the array is taken into account during cast
9337        let str_array = StringArray::from(vec!["a", "b", "c"]);
9338        let str_array = str_array.slice(1, 2);
9339
9340        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9341
9342        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9343        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9344        assert_eq!(strs, &["b", "c"])
9345    }
9346
9347    #[test]
9348    #[cfg_attr(miri, ignore)] // Takes too long
9349    fn test_cast_f64_to_decimal128() {
9350        // to reproduce https://github.com/apache/arrow-rs/issues/2997
9351
9352        let decimal_type = DataType::Decimal128(18, 2);
9353        let array = Float64Array::from(vec![
9354            Some(0.0699999999),
9355            Some(0.0659999999),
9356            Some(0.0650000000),
9357            Some(0.0649999999),
9358        ]);
9359        let array = Arc::new(array) as ArrayRef;
9360        generate_cast_test_case!(
9361            &array,
9362            Decimal128Array,
9363            &decimal_type,
9364            vec![
9365                Some(7_i128), // round up
9366                Some(7_i128), // round up
9367                Some(7_i128), // round up
9368                Some(6_i128), // round down
9369            ]
9370        );
9371
9372        let decimal_type = DataType::Decimal128(18, 3);
9373        let array = Float64Array::from(vec![
9374            Some(0.0699999999),
9375            Some(0.0659999999),
9376            Some(0.0650000000),
9377            Some(0.0649999999),
9378        ]);
9379        let array = Arc::new(array) as ArrayRef;
9380        generate_cast_test_case!(
9381            &array,
9382            Decimal128Array,
9383            &decimal_type,
9384            vec![
9385                Some(70_i128), // round up
9386                Some(66_i128), // round up
9387                Some(65_i128), // round down
9388                Some(65_i128), // round up
9389            ]
9390        );
9391    }
9392
9393    /// Casts that cannot overflow, whether the source type or the data decides,
9394    /// must match the checked conversion at the extremes, and a value beyond the
9395    /// bound must still become null or an error.
9396    #[test]
9397    fn test_cast_integer_to_decimal_bounds() {
9398        // Every i32 has at most ten digits, so the type decides.
9399        let array = Int32Array::from(vec![Some(i32::MIN), None, Some(-1), Some(i32::MAX)]);
9400        generate_cast_test_case!(
9401            &array,
9402            Decimal128Array,
9403            &DataType::Decimal128(12, 2),
9404            vec![
9405                Some(i32::MIN as i128 * 100),
9406                None,
9407                Some(-100),
9408                Some(i32::MAX as i128 * 100)
9409            ]
9410        );
9411        // An i64 can exceed sixteen digits, so the data decides.
9412        let bound = 9_999_999_999_999_999_i64;
9413        let array = Int64Array::from(vec![Some(-bound), None, Some(bound)]);
9414        generate_cast_test_case!(
9415            &array,
9416            Decimal128Array,
9417            &DataType::Decimal128(18, 2),
9418            vec![Some(-bound as i128 * 100), None, Some(bound as i128 * 100)]
9419        );
9420        // A negative scale divides first, truncating toward zero.
9421        let array = Int32Array::from(vec![Some(-1_234_567), Some(9_999_999)]);
9422        generate_cast_test_case!(
9423            &array,
9424            Decimal32Array,
9425            &DataType::Decimal32(5, -2),
9426            vec![Some(-12_345), Some(99_999)]
9427        );
9428        // One value beyond the bound sends the whole array down the checked path.
9429        let array = Int64Array::from(vec![Some(bound + 1), Some(1)]);
9430        let result = cast(&array, &DataType::Decimal128(18, 2)).unwrap();
9431        let result = result.as_primitive::<Decimal128Type>();
9432        assert!(result.is_null(0));
9433        assert_eq!(result.value(1), 100);
9434        let options = CastOptions {
9435            safe: false,
9436            ..Default::default()
9437        };
9438        assert!(cast_with_options(&array, &DataType::Decimal128(18, 2), &options).is_err());
9439        // A precision beyond the target type is an error even when the values fit it.
9440        let array = Int64Array::from(vec![5_000_000_000]);
9441        for safe in [true, false] {
9442            let options = CastOptions {
9443                safe,
9444                ..Default::default()
9445            };
9446            assert!(cast_with_options(&array, &DataType::Decimal32(10, 0), &options).is_err());
9447        }
9448    }
9449
9450    #[test]
9451    fn test_cast_numeric_to_decimal128_overflow() {
9452        let array = Int64Array::from(vec![i64::MAX]);
9453        let array = Arc::new(array) as ArrayRef;
9454        let casted_array = cast_with_options(
9455            &array,
9456            &DataType::Decimal128(38, 30),
9457            &CastOptions {
9458                safe: true,
9459                format_options: FormatOptions::default(),
9460            },
9461        );
9462        assert!(casted_array.is_ok());
9463        assert!(casted_array.unwrap().is_null(0));
9464
9465        let casted_array = cast_with_options(
9466            &array,
9467            &DataType::Decimal128(38, 30),
9468            &CastOptions {
9469                safe: false,
9470                format_options: FormatOptions::default(),
9471            },
9472        );
9473        assert!(casted_array.is_err());
9474    }
9475
9476    #[test]
9477    fn test_cast_numeric_to_decimal256_overflow() {
9478        let array = Int64Array::from(vec![i64::MAX]);
9479        let array = Arc::new(array) as ArrayRef;
9480        let casted_array = cast_with_options(
9481            &array,
9482            &DataType::Decimal256(76, 76),
9483            &CastOptions {
9484                safe: true,
9485                format_options: FormatOptions::default(),
9486            },
9487        );
9488        assert!(casted_array.is_ok());
9489        assert!(casted_array.unwrap().is_null(0));
9490
9491        let casted_array = cast_with_options(
9492            &array,
9493            &DataType::Decimal256(76, 76),
9494            &CastOptions {
9495                safe: false,
9496                format_options: FormatOptions::default(),
9497            },
9498        );
9499        assert!(casted_array.is_err());
9500    }
9501
9502    #[test]
9503    fn test_cast_integer_to_decimal32_does_not_truncate() {
9504        let array = Int64Array::from(vec![5_000_000_000i64, 10_000_000_000, 42]);
9505        let safe = CastOptions {
9506            safe: true,
9507            format_options: FormatOptions::default(),
9508        };
9509        let unsafe_opts = CastOptions {
9510            safe: false,
9511            format_options: FormatOptions::default(),
9512        };
9513
9514        let result = cast_with_options(&array, &DataType::Decimal32(9, 0), &safe).unwrap();
9515        let result = result.as_primitive::<Decimal32Type>();
9516        assert!(
9517            result.is_null(0),
9518            "5e9 must not wrap to {}",
9519            result.value(0)
9520        );
9521        assert!(result.is_null(1));
9522        assert_eq!(result.value(2), 42);
9523
9524        let err = cast_with_options(&array, &DataType::Decimal32(9, 0), &unsafe_opts)
9525            .unwrap_err()
9526            .to_string();
9527        assert_eq!(
9528            err,
9529            "Cast error: Cannot cast to Decimal32(9, 0). Overflowing on 5000000000"
9530        );
9531
9532        let result = cast_with_options(&array, &DataType::Decimal128(9, 0), &safe).unwrap();
9533        let result = result.as_primitive::<Decimal128Type>();
9534        assert!(result.is_null(0));
9535        assert!(result.is_null(1));
9536        assert_eq!(result.value(2), 42);
9537    }
9538
9539    #[test]
9540    fn test_cast_integer_to_decimal32_scales_before_narrowing() {
9541        let array = Int64Array::from(vec![5_000_000_000i64]);
9542        let safe = CastOptions {
9543            safe: true,
9544            format_options: FormatOptions::default(),
9545        };
9546        let unsafe_opts = CastOptions {
9547            safe: false,
9548            format_options: FormatOptions::default(),
9549        };
9550        let data_type = DataType::Decimal32(9, -1);
9551
9552        let result = cast_with_options(&array, &data_type, &safe).unwrap();
9553        let result = result.as_primitive::<Decimal32Type>();
9554        assert_eq!(result.value(0), 500_000_000);
9555
9556        let result = cast_with_options(&array, &data_type, &unsafe_opts).unwrap();
9557        let result = result.as_primitive::<Decimal32Type>();
9558        assert_eq!(result.value(0), 500_000_000);
9559    }
9560
9561    #[test]
9562    fn test_cast_uint_to_decimal32_does_not_wrap() {
9563        let array = UInt32Array::from(vec![4_000_000_000u32]);
9564        let safe = CastOptions {
9565            safe: true,
9566            format_options: FormatOptions::default(),
9567        };
9568        let unsafe_opts = CastOptions {
9569            safe: false,
9570            format_options: FormatOptions::default(),
9571        };
9572
9573        let result = cast_with_options(&array, &DataType::Decimal32(9, 0), &safe).unwrap();
9574        let result = result.as_primitive::<Decimal32Type>();
9575        assert!(
9576            result.is_null(0),
9577            "u32 4e9 must not wrap to {}",
9578            result.value(0)
9579        );
9580
9581        let err = cast_with_options(&array, &DataType::Decimal32(9, 0), &unsafe_opts)
9582            .unwrap_err()
9583            .to_string();
9584        assert_eq!(
9585            err,
9586            "Cast error: Cannot cast to Decimal32(9, 0). Overflowing on 4000000000"
9587        );
9588
9589        let result = cast_with_options(&array, &DataType::Decimal128(9, 0), &safe).unwrap();
9590        assert!(result.is_null(0));
9591        assert!(cast_with_options(&array, &DataType::Decimal128(9, 0), &unsafe_opts).is_err());
9592    }
9593
9594    #[test]
9595    fn test_cast_uint64_max_to_decimal64_does_not_wrap() {
9596        let array = UInt64Array::from(vec![u64::MAX]);
9597        let unsafe_opts = CastOptions {
9598            safe: false,
9599            format_options: FormatOptions::default(),
9600        };
9601
9602        let err = cast_with_options(&array, &DataType::Decimal64(18, 0), &unsafe_opts)
9603            .unwrap_err()
9604            .to_string();
9605        assert_eq!(
9606            err,
9607            "Cast error: Cannot cast to Decimal64(18, 0). Overflowing on 18446744073709551615"
9608        );
9609
9610        assert!(cast_with_options(&array, &DataType::Decimal128(18, 0), &unsafe_opts).is_err());
9611    }
9612
9613    #[test]
9614    fn test_cast_floating_point_to_decimal128_precision_overflow() {
9615        let array = Float64Array::from(vec![1.1]);
9616        let array = Arc::new(array) as ArrayRef;
9617        let casted_array = cast_with_options(
9618            &array,
9619            &DataType::Decimal128(2, 2),
9620            &CastOptions {
9621                safe: true,
9622                format_options: FormatOptions::default(),
9623            },
9624        );
9625        assert!(casted_array.is_ok());
9626        assert!(casted_array.unwrap().is_null(0));
9627
9628        let casted_array = cast_with_options(
9629            &array,
9630            &DataType::Decimal128(2, 2),
9631            &CastOptions {
9632                safe: false,
9633                format_options: FormatOptions::default(),
9634            },
9635        );
9636        let err = casted_array.unwrap_err().to_string();
9637        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
9638        assert!(
9639            err.contains(expected_error),
9640            "did not find expected error '{expected_error}' in actual error '{err}'"
9641        );
9642    }
9643
9644    #[test]
9645    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
9646    fn test_cast_float16_to_decimal128_precision_overflow() {
9647        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
9648        let array = Arc::new(array) as ArrayRef;
9649        let casted_array = cast_with_options(
9650            &array,
9651            &DataType::Decimal128(2, 2),
9652            &CastOptions {
9653                safe: true,
9654                format_options: FormatOptions::default(),
9655            },
9656        );
9657        assert!(casted_array.is_ok());
9658        assert!(casted_array.unwrap().is_null(0));
9659
9660        let casted_array = cast_with_options(
9661            &array,
9662            &DataType::Decimal128(2, 2),
9663            &CastOptions {
9664                safe: false,
9665                format_options: FormatOptions::default(),
9666            },
9667        );
9668        let err = casted_array.unwrap_err().to_string();
9669        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
9670        assert_eq!(err, expected_error);
9671    }
9672
9673    #[test]
9674    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
9675    fn test_cast_float16_to_decimal256_precision_overflow() {
9676        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
9677        let array = Arc::new(array) as ArrayRef;
9678        let casted_array = cast_with_options(
9679            &array,
9680            &DataType::Decimal256(2, 2),
9681            &CastOptions {
9682                safe: true,
9683                format_options: FormatOptions::default(),
9684            },
9685        );
9686        assert!(casted_array.is_ok());
9687        assert!(casted_array.unwrap().is_null(0));
9688
9689        let casted_array = cast_with_options(
9690            &array,
9691            &DataType::Decimal256(2, 2),
9692            &CastOptions {
9693                safe: false,
9694                format_options: FormatOptions::default(),
9695            },
9696        );
9697        let err = casted_array.unwrap_err().to_string();
9698        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
9699        assert_eq!(err, expected_error);
9700    }
9701
9702    #[test]
9703    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
9704    fn test_cast_float16_to_decimal128_non_finite() {
9705        let array = Float16Array::from(vec![f16::NAN, f16::INFINITY, f16::NEG_INFINITY]);
9706        let array = Arc::new(array) as ArrayRef;
9707        let casted_array = cast_with_options(
9708            &array,
9709            &DataType::Decimal128(38, 2),
9710            &CastOptions {
9711                safe: true,
9712                format_options: FormatOptions::default(),
9713            },
9714        )
9715        .unwrap();
9716
9717        assert!(casted_array.is_null(0));
9718        assert!(casted_array.is_null(1));
9719        assert!(casted_array.is_null(2));
9720
9721        let casted_array = cast_with_options(
9722            &array,
9723            &DataType::Decimal128(38, 2),
9724            &CastOptions {
9725                safe: false,
9726                format_options: FormatOptions::default(),
9727            },
9728        );
9729        let err = casted_array.unwrap_err().to_string();
9730        let expected_error = "Cannot cast to Decimal128(38, 2)";
9731        assert!(
9732            err.contains(expected_error),
9733            "did not find expected error '{expected_error}' in actual error '{err}'"
9734        );
9735    }
9736
9737    #[test]
9738    fn test_cast_floating_point_to_decimal256_precision_overflow() {
9739        let array = Float64Array::from(vec![1.1]);
9740        let array = Arc::new(array) as ArrayRef;
9741        let casted_array = cast_with_options(
9742            &array,
9743            &DataType::Decimal256(2, 2),
9744            &CastOptions {
9745                safe: true,
9746                format_options: FormatOptions::default(),
9747            },
9748        );
9749        assert!(casted_array.is_ok());
9750        assert!(casted_array.unwrap().is_null(0));
9751
9752        let casted_array = cast_with_options(
9753            &array,
9754            &DataType::Decimal256(2, 2),
9755            &CastOptions {
9756                safe: false,
9757                format_options: FormatOptions::default(),
9758            },
9759        );
9760        let err = casted_array.unwrap_err().to_string();
9761        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
9762        assert_eq!(err, expected_error);
9763    }
9764
9765    #[test]
9766    fn test_cast_floating_point_to_decimal128_overflow() {
9767        let array = Float64Array::from(vec![f64::MAX]);
9768        let array = Arc::new(array) as ArrayRef;
9769        let casted_array = cast_with_options(
9770            &array,
9771            &DataType::Decimal128(38, 30),
9772            &CastOptions {
9773                safe: true,
9774                format_options: FormatOptions::default(),
9775            },
9776        );
9777        assert!(casted_array.is_ok());
9778        assert!(casted_array.unwrap().is_null(0));
9779
9780        let casted_array = cast_with_options(
9781            &array,
9782            &DataType::Decimal128(38, 30),
9783            &CastOptions {
9784                safe: false,
9785                format_options: FormatOptions::default(),
9786            },
9787        );
9788        let err = casted_array.unwrap_err().to_string();
9789        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
9790        assert!(
9791            err.contains(expected_error),
9792            "did not find expected error '{expected_error}' in actual error '{err}'"
9793        );
9794    }
9795
9796    #[test]
9797    fn test_cast_floating_point_to_decimal256_overflow() {
9798        let array = Float64Array::from(vec![f64::MAX]);
9799        let array = Arc::new(array) as ArrayRef;
9800        let casted_array = cast_with_options(
9801            &array,
9802            &DataType::Decimal256(76, 50),
9803            &CastOptions {
9804                safe: true,
9805                format_options: FormatOptions::default(),
9806            },
9807        );
9808        assert!(casted_array.is_ok());
9809        assert!(casted_array.unwrap().is_null(0));
9810
9811        let casted_array = cast_with_options(
9812            &array,
9813            &DataType::Decimal256(76, 50),
9814            &CastOptions {
9815                safe: false,
9816                format_options: FormatOptions::default(),
9817            },
9818        );
9819        let err = casted_array.unwrap_err().to_string();
9820        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
9821        assert!(
9822            err.contains(expected_error),
9823            "did not find expected error '{expected_error}' in actual error '{err}'"
9824        );
9825    }
9826    #[test]
9827    fn test_cast_decimal256_to_f64_no_overflow() {
9828        // Test casting i256::MAX: should produce a large finite positive value
9829        let array = vec![Some(i256::MAX)];
9830        let array = create_decimal256_array(array, 76, 2).unwrap();
9831        let array = Arc::new(array) as ArrayRef;
9832
9833        let result = cast(&array, &DataType::Float64).unwrap();
9834        let result = result.as_primitive::<Float64Type>();
9835        assert!(result.value(0).is_finite());
9836        assert!(result.value(0) > 0.0); // Positive result
9837
9838        // Test casting i256::MIN: should produce a large finite negative value
9839        let array = vec![Some(i256::MIN)];
9840        let array = create_decimal256_array(array, 76, 2).unwrap();
9841        let array = Arc::new(array) as ArrayRef;
9842
9843        let result = cast(&array, &DataType::Float64).unwrap();
9844        let result = result.as_primitive::<Float64Type>();
9845        assert!(result.value(0).is_finite());
9846        assert!(result.value(0) < 0.0); // Negative result
9847    }
9848
9849    #[test]
9850    fn test_cast_decimal256_negative_scale_metadata() {
9851        let input = Decimal256Array::from(vec![
9852            Some(i256::ZERO),
9853            Some(i256::ONE),
9854            Some(i256::MINUS_ONE),
9855            None,
9856        ])
9857        .with_precision_and_scale(76, -51)
9858        .unwrap();
9859        let value = i256::from_i128(10).pow_wrapping(51);
9860        let expected =
9861            Decimal256Array::from(vec![Some(i256::ZERO), Some(value), Some(-value), None])
9862                .with_precision_and_scale(76, 0)
9863                .unwrap();
9864        let options = CastOptions {
9865            safe: false,
9866            ..Default::default()
9867        };
9868        let output_type = DataType::Decimal256(76, 0);
9869        let result = cast(&input, &output_type).unwrap();
9870        assert_eq!(result.as_primitive::<Decimal256Type>(), &expected);
9871        let result = cast_with_options(&input, &output_type, &options).unwrap();
9872        assert_eq!(result.as_primitive::<Decimal256Type>(), &expected);
9873
9874        // The precision plus scale increase crosses i8::MAX at -52.
9875        let input = input.with_precision_and_scale(76, -52).unwrap();
9876        let value = i256::from_i128(10).pow_wrapping(52);
9877        let expected =
9878            Decimal256Array::from(vec![Some(i256::ZERO), Some(value), Some(-value), None])
9879                .with_precision_and_scale(76, 0)
9880                .unwrap();
9881        let result = cast(&input, &output_type).unwrap();
9882        assert_eq!(result.as_primitive::<Decimal256Type>(), &expected);
9883        let result = cast_with_options(&input, &output_type, &options).unwrap();
9884        assert_eq!(result.as_primitive::<Decimal256Type>(), &expected);
9885    }
9886
9887    #[test]
9888    fn test_cast_decimal256_negative_scale_precision_overflow() {
9889        let input = Decimal256Array::from(vec![Some(i256::ZERO), Some(i256::ONE), None])
9890            .with_precision_and_scale(76, -76)
9891            .unwrap();
9892        let output_type = DataType::Decimal256(76, 0);
9893        let expected = Decimal256Array::from(vec![Some(i256::ZERO), None, None])
9894            .with_precision_and_scale(76, 0)
9895            .unwrap();
9896        let result = cast(&input, &output_type).unwrap();
9897        assert_eq!(result.as_primitive::<Decimal256Type>(), &expected);
9898
9899        let options = CastOptions {
9900            safe: false,
9901            ..Default::default()
9902        };
9903        let error = cast_with_options(&input, &output_type, &options).unwrap_err();
9904        let value = i256::from_i128(10).pow_wrapping(76);
9905        assert_eq!(
9906            error.to_string(),
9907            format!(
9908                "Invalid argument error: {value} is too large to store in a Decimal256 of precision 76. Max is {}",
9909                value - i256::ONE
9910            )
9911        );
9912    }
9913
9914    #[test]
9915    fn test_cast_decimal256_negative_scale_native_overflow() {
9916        let input = Decimal256Array::from(vec![Some(i256::from_i128(6)), None])
9917            .with_precision_and_scale(76, -76)
9918            .unwrap();
9919        let output_type = DataType::Decimal256(76, 0);
9920        let expected = Decimal256Array::from(vec![None, None])
9921            .with_precision_and_scale(76, 0)
9922            .unwrap();
9923        let result = cast(&input, &output_type).unwrap();
9924        assert_eq!(result.as_primitive::<Decimal256Type>(), &expected);
9925
9926        let options = CastOptions {
9927            safe: false,
9928            ..Default::default()
9929        };
9930        let error = cast_with_options(&input, &output_type, &options).unwrap_err();
9931        assert_eq!(
9932            error.to_string(),
9933            "Cast error: Cannot cast to Decimal256(76, 0). Overflowing on 6"
9934        );
9935    }
9936
9937    #[test]
9938    fn test_cast_decimal256_extreme_scale_difference() {
9939        let input = Decimal256Array::from(vec![Some(i256::ONE), Some(i256::MINUS_ONE), None])
9940            .with_precision_and_scale(76, i8::MIN)
9941            .unwrap();
9942        for safe in [true, false] {
9943            let options = CastOptions {
9944                safe,
9945                ..Default::default()
9946            };
9947            let error =
9948                cast_with_options(&input, &DataType::Decimal256(76, 76), &options).unwrap_err();
9949            assert_eq!(
9950                error.to_string(),
9951                "Cast error: Cannot cast to Decimal256(76, 76). Value overflows for output scale"
9952            );
9953        }
9954
9955        let input = input.with_precision_and_scale(76, 76).unwrap();
9956        let expected = Decimal256Array::from(vec![Some(i256::ZERO), Some(i256::ZERO), None])
9957            .with_precision_and_scale(76, i8::MIN)
9958            .unwrap();
9959        for safe in [true, false] {
9960            let options = CastOptions {
9961                safe,
9962                ..Default::default()
9963            };
9964            let result =
9965                cast_with_options(&input, &DataType::Decimal256(76, i8::MIN), &options).unwrap();
9966            assert_eq!(result.as_primitive::<Decimal256Type>(), &expected);
9967        }
9968    }
9969
9970    #[test]
9971    fn test_cast_decimal128_to_decimal128_negative_scale() {
9972        let input_type = DataType::Decimal128(20, 0);
9973        let output_type = DataType::Decimal128(20, -1);
9974        assert!(can_cast_types(&input_type, &output_type));
9975        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
9976        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
9977        let array = Arc::new(input_decimal_array) as ArrayRef;
9978        generate_cast_test_case!(
9979            &array,
9980            Decimal128Array,
9981            &output_type,
9982            vec![
9983                Some(112345_i128),
9984                Some(212346_i128),
9985                Some(312346_i128),
9986                None
9987            ]
9988        );
9989
9990        let casted_array = cast(&array, &output_type).unwrap();
9991        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9992
9993        assert_eq!("1123450", decimal_arr.value_as_string(0));
9994        assert_eq!("2123460", decimal_arr.value_as_string(1));
9995        assert_eq!("3123460", decimal_arr.value_as_string(2));
9996    }
9997
9998    #[test]
9999    fn decimal128_min_max_to_f64() {
10000        // Ensure Decimal128 i128::MIN/MAX round-trip cast
10001        let min128 = i128::MIN;
10002        let max128 = i128::MAX;
10003        assert_eq!(min128 as f64, min128 as f64);
10004        assert_eq!(max128 as f64, max128 as f64);
10005    }
10006
10007    #[test]
10008    fn test_cast_numeric_to_decimal128_negative() {
10009        let decimal_type = DataType::Decimal128(38, -1);
10010        let array = Arc::new(Int32Array::from(vec![
10011            Some(1123456),
10012            Some(2123456),
10013            Some(3123456),
10014        ])) as ArrayRef;
10015
10016        let casted_array = cast(&array, &decimal_type).unwrap();
10017        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10018
10019        assert_eq!("1123450", decimal_arr.value_as_string(0));
10020        assert_eq!("2123450", decimal_arr.value_as_string(1));
10021        assert_eq!("3123450", decimal_arr.value_as_string(2));
10022
10023        let array = Arc::new(Float32Array::from(vec![
10024            Some(1123.456),
10025            Some(2123.456),
10026            Some(3123.456),
10027        ])) as ArrayRef;
10028
10029        let casted_array = cast(&array, &decimal_type).unwrap();
10030        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10031
10032        assert_eq!("1120", decimal_arr.value_as_string(0));
10033        assert_eq!("2120", decimal_arr.value_as_string(1));
10034        assert_eq!("3120", decimal_arr.value_as_string(2));
10035    }
10036
10037    #[test]
10038    fn test_cast_decimal128_to_decimal128_negative() {
10039        let input_type = DataType::Decimal128(10, -1);
10040        let output_type = DataType::Decimal128(10, -2);
10041        assert!(can_cast_types(&input_type, &output_type));
10042        let array = vec![Some(123)];
10043        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10044        let array = Arc::new(input_decimal_array) as ArrayRef;
10045        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10046
10047        let casted_array = cast(&array, &output_type).unwrap();
10048        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10049
10050        assert_eq!("1200", decimal_arr.value_as_string(0));
10051
10052        let array = vec![Some(125)];
10053        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10054        let array = Arc::new(input_decimal_array) as ArrayRef;
10055        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10056
10057        let casted_array = cast(&array, &output_type).unwrap();
10058        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10059
10060        assert_eq!("1300", decimal_arr.value_as_string(0));
10061    }
10062
10063    #[test]
10064    fn test_cast_decimal128_to_decimal256_negative() {
10065        let input_type = DataType::Decimal128(10, 3);
10066        let output_type = DataType::Decimal256(10, 5);
10067        assert!(can_cast_types(&input_type, &output_type));
10068        let array = vec![Some(123456), Some(-123456)];
10069        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10070        let array = Arc::new(input_decimal_array) as ArrayRef;
10071
10072        let hundred = i256::from_i128(100);
10073        generate_cast_test_case!(
10074            &array,
10075            Decimal256Array,
10076            &output_type,
10077            vec![
10078                Some(i256::from_i128(123456).mul_wrapping(hundred)),
10079                Some(i256::from_i128(-123456).mul_wrapping(hundred))
10080            ]
10081        );
10082    }
10083
10084    #[test]
10085    fn test_parse_decimal_and_format() {
10086        assert_eq!(
10087            Decimal128Type::format_decimal(
10088                parse_decimal::<Decimal128Type>("123.45", 38, 2).unwrap(),
10089                38,
10090                2,
10091            ),
10092            "123.45"
10093        );
10094        assert_eq!(
10095            Decimal128Type::format_decimal(
10096                parse_decimal::<Decimal128Type>("12345", 38, 2).unwrap(),
10097                38,
10098                2,
10099            ),
10100            "12345.00"
10101        );
10102        assert_eq!(
10103            Decimal128Type::format_decimal(
10104                parse_decimal::<Decimal128Type>("0.12345", 38, 2).unwrap(),
10105                38,
10106                2,
10107            ),
10108            "0.12"
10109        );
10110        assert_eq!(
10111            Decimal128Type::format_decimal(
10112                parse_decimal::<Decimal128Type>(".12345", 38, 2).unwrap(),
10113                38,
10114                2,
10115            ),
10116            "0.12"
10117        );
10118        assert_eq!(
10119            Decimal128Type::format_decimal(
10120                parse_decimal::<Decimal128Type>(".1265", 38, 2).unwrap(),
10121                38,
10122                2,
10123            ),
10124            "0.13"
10125        );
10126        assert_eq!(
10127            Decimal128Type::format_decimal(
10128                parse_decimal::<Decimal128Type>(".1265", 38, 2).unwrap(),
10129                38,
10130                2,
10131            ),
10132            "0.13"
10133        );
10134
10135        assert_eq!(
10136            Decimal256Type::format_decimal(
10137                parse_decimal::<Decimal256Type>("123.45", 76, 3).unwrap(),
10138                38,
10139                3,
10140            ),
10141            "123.450"
10142        );
10143        assert_eq!(
10144            Decimal256Type::format_decimal(
10145                parse_decimal::<Decimal256Type>("12345", 76, 3).unwrap(),
10146                38,
10147                3,
10148            ),
10149            "12345.000"
10150        );
10151        assert_eq!(
10152            Decimal256Type::format_decimal(
10153                parse_decimal::<Decimal256Type>("0.12345", 76, 3).unwrap(),
10154                38,
10155                3,
10156            ),
10157            "0.123"
10158        );
10159        assert_eq!(
10160            Decimal256Type::format_decimal(
10161                parse_decimal::<Decimal256Type>(".12345", 76, 3).unwrap(),
10162                38,
10163                3,
10164            ),
10165            "0.123"
10166        );
10167        assert_eq!(
10168            Decimal256Type::format_decimal(
10169                parse_decimal::<Decimal256Type>(".1265", 76, 3).unwrap(),
10170                38,
10171                3,
10172            ),
10173            "0.127"
10174        );
10175    }
10176
10177    fn test_cast_string_to_decimal(array: ArrayRef) {
10178        // Decimal128
10179        let output_type = DataType::Decimal128(38, 2);
10180        assert!(can_cast_types(array.data_type(), &output_type));
10181
10182        let casted_array = cast(&array, &output_type).unwrap();
10183        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10184
10185        assert_eq!("123.45", decimal_arr.value_as_string(0));
10186        assert_eq!("1.23", decimal_arr.value_as_string(1));
10187        assert_eq!("0.12", decimal_arr.value_as_string(2));
10188        assert_eq!("0.13", decimal_arr.value_as_string(3));
10189        assert_eq!("1.26", decimal_arr.value_as_string(4));
10190        assert_eq!("12345.00", decimal_arr.value_as_string(5));
10191        assert_eq!("12345.00", decimal_arr.value_as_string(6));
10192        assert_eq!("0.12", decimal_arr.value_as_string(7));
10193        assert_eq!("12.23", decimal_arr.value_as_string(8));
10194        assert!(decimal_arr.is_null(9));
10195        assert!(decimal_arr.is_null(10));
10196        assert!(decimal_arr.is_null(11));
10197        assert!(decimal_arr.is_null(12));
10198        assert_eq!("-1.23", decimal_arr.value_as_string(13));
10199        assert_eq!("-1.24", decimal_arr.value_as_string(14));
10200        assert_eq!("0.00", decimal_arr.value_as_string(15));
10201        assert_eq!("-123.00", decimal_arr.value_as_string(16));
10202        assert_eq!("-123.23", decimal_arr.value_as_string(17));
10203        assert_eq!("-0.12", decimal_arr.value_as_string(18));
10204        assert_eq!("1.23", decimal_arr.value_as_string(19));
10205        assert_eq!("1.24", decimal_arr.value_as_string(20));
10206        assert_eq!("0.00", decimal_arr.value_as_string(21));
10207        assert_eq!("123.00", decimal_arr.value_as_string(22));
10208        assert_eq!("123.23", decimal_arr.value_as_string(23));
10209        assert_eq!("0.12", decimal_arr.value_as_string(24));
10210        assert!(decimal_arr.is_null(25));
10211        assert!(decimal_arr.is_null(26));
10212        assert!(decimal_arr.is_null(27));
10213        assert_eq!("0.00", decimal_arr.value_as_string(28));
10214        assert_eq!("0.00", decimal_arr.value_as_string(29));
10215        assert_eq!("12345.00", decimal_arr.value_as_string(30));
10216        assert_eq!(decimal_arr.len(), 31);
10217
10218        // Decimal256
10219        let output_type = DataType::Decimal256(76, 3);
10220        assert!(can_cast_types(array.data_type(), &output_type));
10221
10222        let casted_array = cast(&array, &output_type).unwrap();
10223        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10224
10225        assert_eq!("123.450", decimal_arr.value_as_string(0));
10226        assert_eq!("1.235", decimal_arr.value_as_string(1));
10227        assert_eq!("0.123", decimal_arr.value_as_string(2));
10228        assert_eq!("0.127", decimal_arr.value_as_string(3));
10229        assert_eq!("1.263", decimal_arr.value_as_string(4));
10230        assert_eq!("12345.000", decimal_arr.value_as_string(5));
10231        assert_eq!("12345.000", decimal_arr.value_as_string(6));
10232        assert_eq!("0.123", decimal_arr.value_as_string(7));
10233        assert_eq!("12.234", decimal_arr.value_as_string(8));
10234        assert!(decimal_arr.is_null(9));
10235        assert!(decimal_arr.is_null(10));
10236        assert!(decimal_arr.is_null(11));
10237        assert!(decimal_arr.is_null(12));
10238        assert_eq!("-1.235", decimal_arr.value_as_string(13));
10239        assert_eq!("-1.236", decimal_arr.value_as_string(14));
10240        assert_eq!("0.000", decimal_arr.value_as_string(15));
10241        assert_eq!("-123.000", decimal_arr.value_as_string(16));
10242        assert_eq!("-123.234", decimal_arr.value_as_string(17));
10243        assert_eq!("-0.123", decimal_arr.value_as_string(18));
10244        assert_eq!("1.235", decimal_arr.value_as_string(19));
10245        assert_eq!("1.236", decimal_arr.value_as_string(20));
10246        assert_eq!("0.000", decimal_arr.value_as_string(21));
10247        assert_eq!("123.000", decimal_arr.value_as_string(22));
10248        assert_eq!("123.234", decimal_arr.value_as_string(23));
10249        assert_eq!("0.123", decimal_arr.value_as_string(24));
10250        assert!(decimal_arr.is_null(25));
10251        assert!(decimal_arr.is_null(26));
10252        assert!(decimal_arr.is_null(27));
10253        assert_eq!("0.000", decimal_arr.value_as_string(28));
10254        assert_eq!("0.000", decimal_arr.value_as_string(29));
10255        assert_eq!("12345.000", decimal_arr.value_as_string(30));
10256        assert_eq!(decimal_arr.len(), 31);
10257    }
10258
10259    #[test]
10260    fn test_cast_utf8_to_decimal() {
10261        let str_array = StringArray::from(vec![
10262            Some("123.45"),
10263            Some("1.2345"),
10264            Some("0.12345"),
10265            Some("0.1267"),
10266            Some("1.263"),
10267            Some("12345.0"),
10268            Some("12345"),
10269            Some("000.123"),
10270            Some("12.234000"),
10271            None,
10272            Some(""),
10273            Some(" "),
10274            None,
10275            Some("-1.23499999"),
10276            Some("-1.23599999"),
10277            Some("-0.00001"),
10278            Some("-123"),
10279            Some("-123.234000"),
10280            Some("-000.123"),
10281            Some("+1.23499999"),
10282            Some("+1.23599999"),
10283            Some("+0.00001"),
10284            Some("+123"),
10285            Some("+123.234000"),
10286            Some("+000.123"),
10287            Some("1.-23499999"),
10288            Some("-1.-23499999"),
10289            Some("--1.23499999"),
10290            Some("0"),
10291            Some("000.000"),
10292            Some("0000000000000000012345.000"),
10293        ]);
10294        let array = Arc::new(str_array) as ArrayRef;
10295
10296        test_cast_string_to_decimal(array);
10297
10298        let test_cases = [
10299            (None, None),
10300            (Some(""), None),
10301            (Some("   "), None),
10302            (Some("0"), Some("0")),
10303            (Some("000.000"), Some("0")),
10304            (Some("12345"), Some("12345")),
10305            (Some("000000000000000000000000000012345"), Some("12345")),
10306            (Some("-123"), Some("-123")),
10307            (Some("+123"), Some("123")),
10308        ];
10309        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10310        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10311
10312        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
10313        test_cast_string_to_decimal_scale_zero(array, &expected);
10314    }
10315
10316    #[test]
10317    fn test_cast_large_utf8_to_decimal() {
10318        let str_array = LargeStringArray::from(vec![
10319            Some("123.45"),
10320            Some("1.2345"),
10321            Some("0.12345"),
10322            Some("0.1267"),
10323            Some("1.263"),
10324            Some("12345.0"),
10325            Some("12345"),
10326            Some("000.123"),
10327            Some("12.234000"),
10328            None,
10329            Some(""),
10330            Some(" "),
10331            None,
10332            Some("-1.23499999"),
10333            Some("-1.23599999"),
10334            Some("-0.00001"),
10335            Some("-123"),
10336            Some("-123.234000"),
10337            Some("-000.123"),
10338            Some("+1.23499999"),
10339            Some("+1.23599999"),
10340            Some("+0.00001"),
10341            Some("+123"),
10342            Some("+123.234000"),
10343            Some("+000.123"),
10344            Some("1.-23499999"),
10345            Some("-1.-23499999"),
10346            Some("--1.23499999"),
10347            Some("0"),
10348            Some("000.000"),
10349            Some("0000000000000000012345.000"),
10350        ]);
10351        let array = Arc::new(str_array) as ArrayRef;
10352
10353        test_cast_string_to_decimal(array);
10354
10355        let test_cases = [
10356            (None, None),
10357            (Some(""), None),
10358            (Some("   "), None),
10359            (Some("0"), Some("0")),
10360            (Some("000.000"), Some("0")),
10361            (Some("12345"), Some("12345")),
10362            (Some("000000000000000000000000000012345"), Some("12345")),
10363            (Some("-123"), Some("-123")),
10364            (Some("+123"), Some("123")),
10365        ];
10366        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10367        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10368
10369        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
10370        test_cast_string_to_decimal_scale_zero(array, &expected);
10371    }
10372
10373    fn test_cast_string_to_decimal_scale_zero(
10374        array: ArrayRef,
10375        expected_as_string: &[Option<&str>],
10376    ) {
10377        // Decimal128
10378        let output_type = DataType::Decimal128(38, 0);
10379        assert!(can_cast_types(array.data_type(), &output_type));
10380        let casted_array = cast(&array, &output_type).unwrap();
10381        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10382        assert_decimal_array_contents(decimal_arr, expected_as_string);
10383
10384        // Decimal256
10385        let output_type = DataType::Decimal256(76, 0);
10386        assert!(can_cast_types(array.data_type(), &output_type));
10387        let casted_array = cast(&array, &output_type).unwrap();
10388        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10389        assert_decimal_array_contents(decimal_arr, expected_as_string);
10390    }
10391
10392    fn assert_decimal_array_contents<T>(
10393        array: &PrimitiveArray<T>,
10394        expected_as_string: &[Option<&str>],
10395    ) where
10396        T: DecimalType + ArrowPrimitiveType,
10397    {
10398        assert_eq!(array.len(), expected_as_string.len());
10399        for (i, expected) in expected_as_string.iter().enumerate() {
10400            let actual = if array.is_null(i) {
10401                None
10402            } else {
10403                Some(array.value_as_string(i))
10404            };
10405            let actual = actual.as_ref().map(|s| s.as_ref());
10406            assert_eq!(*expected, actual, "Expected at position {i}");
10407        }
10408    }
10409
10410    #[test]
10411    fn test_cast_invalid_utf8_to_decimal() {
10412        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
10413        let array = Arc::new(str_array) as ArrayRef;
10414
10415        // Safe cast
10416        let output_type = DataType::Decimal128(38, 2);
10417        let casted_array = cast(&array, &output_type).unwrap();
10418        assert!(casted_array.is_null(0));
10419        assert!(casted_array.is_null(1));
10420
10421        let output_type = DataType::Decimal256(76, 2);
10422        let casted_array = cast(&array, &output_type).unwrap();
10423        assert!(casted_array.is_null(0));
10424        assert!(casted_array.is_null(1));
10425
10426        // Non-safe cast
10427        let output_type = DataType::Decimal128(38, 2);
10428        let str_array = StringArray::from(vec!["4.4.5"]);
10429        let array = Arc::new(str_array) as ArrayRef;
10430        let option = CastOptions {
10431            safe: false,
10432            format_options: FormatOptions::default(),
10433        };
10434        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10435        assert!(
10436            casted_err
10437                .to_string()
10438                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 2) type")
10439        );
10440
10441        let str_array = StringArray::from(vec![". 0.123"]);
10442        let array = Arc::new(str_array) as ArrayRef;
10443        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10444        assert!(
10445            casted_err
10446                .to_string()
10447                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 2) type")
10448        );
10449
10450        let str_array = StringArray::from(vec![""]);
10451        let array = Arc::new(str_array) as ArrayRef;
10452        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10453        assert!(
10454            casted_err
10455                .to_string()
10456                .contains("Cannot cast string '' to value of Decimal128(38, 2) type")
10457        );
10458    }
10459
10460    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
10461        let output_type = DataType::Decimal128(38, 2);
10462        let casted_array = cast(&overflow_array, &output_type).unwrap();
10463        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10464
10465        assert!(decimal_arr.is_null(0));
10466        assert!(decimal_arr.is_null(1));
10467        assert!(decimal_arr.is_null(2));
10468        assert_eq!(
10469            "999999999999999999999999999999999999.99",
10470            decimal_arr.value_as_string(3)
10471        );
10472        assert_eq!(
10473            "100000000000000000000000000000000000.00",
10474            decimal_arr.value_as_string(4)
10475        );
10476    }
10477
10478    #[test]
10479    fn test_cast_string_to_decimal128_precision_overflow() {
10480        let array = StringArray::from(vec!["1000".to_string()]);
10481        let array = Arc::new(array) as ArrayRef;
10482        let casted_array = cast_with_options(
10483            &array,
10484            &DataType::Decimal128(10, 8),
10485            &CastOptions {
10486                safe: true,
10487                format_options: FormatOptions::default(),
10488            },
10489        );
10490        assert!(casted_array.is_ok());
10491        assert!(casted_array.unwrap().is_null(0));
10492
10493        let err = cast_with_options(
10494            &array,
10495            &DataType::Decimal128(10, 8),
10496            &CastOptions {
10497                safe: false,
10498                format_options: FormatOptions::default(),
10499            },
10500        );
10501        assert_eq!(
10502            "Cast error: Cannot cast string '1000' to value of Decimal128(10, 8) type: value does not fit",
10503            err.unwrap_err().to_string()
10504        );
10505    }
10506
10507    #[test]
10508    fn test_cast_utf8_to_decimal128_overflow() {
10509        let overflow_str_array = StringArray::from(vec![
10510            i128::MAX.to_string(),
10511            i128::MIN.to_string(),
10512            "99999999999999999999999999999999999999".to_string(),
10513            "999999999999999999999999999999999999.99".to_string(),
10514            "99999999999999999999999999999999999.999".to_string(),
10515        ]);
10516        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10517
10518        test_cast_string_to_decimal128_overflow(overflow_array);
10519    }
10520
10521    #[test]
10522    fn test_cast_large_utf8_to_decimal128_overflow() {
10523        let overflow_str_array = LargeStringArray::from(vec![
10524            i128::MAX.to_string(),
10525            i128::MIN.to_string(),
10526            "99999999999999999999999999999999999999".to_string(),
10527            "999999999999999999999999999999999999.99".to_string(),
10528            "99999999999999999999999999999999999.999".to_string(),
10529        ]);
10530        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10531
10532        test_cast_string_to_decimal128_overflow(overflow_array);
10533    }
10534
10535    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
10536        let output_type = DataType::Decimal256(76, 2);
10537        let casted_array = cast(&overflow_array, &output_type).unwrap();
10538        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10539
10540        assert_eq!(
10541            "170141183460469231731687303715884105727.00",
10542            decimal_arr.value_as_string(0)
10543        );
10544        assert_eq!(
10545            "-170141183460469231731687303715884105728.00",
10546            decimal_arr.value_as_string(1)
10547        );
10548        assert_eq!(
10549            "99999999999999999999999999999999999999.00",
10550            decimal_arr.value_as_string(2)
10551        );
10552        assert_eq!(
10553            "999999999999999999999999999999999999.99",
10554            decimal_arr.value_as_string(3)
10555        );
10556        assert_eq!(
10557            "100000000000000000000000000000000000.00",
10558            decimal_arr.value_as_string(4)
10559        );
10560        assert!(decimal_arr.is_null(5));
10561        assert!(decimal_arr.is_null(6));
10562    }
10563
10564    #[test]
10565    fn test_cast_string_to_decimal256_precision_overflow() {
10566        let array = StringArray::from(vec!["1000".to_string()]);
10567        let array = Arc::new(array) as ArrayRef;
10568        let casted_array = cast_with_options(
10569            &array,
10570            &DataType::Decimal256(10, 8),
10571            &CastOptions {
10572                safe: true,
10573                format_options: FormatOptions::default(),
10574            },
10575        );
10576        assert!(casted_array.is_ok());
10577        assert!(casted_array.unwrap().is_null(0));
10578
10579        let err = cast_with_options(
10580            &array,
10581            &DataType::Decimal256(10, 8),
10582            &CastOptions {
10583                safe: false,
10584                format_options: FormatOptions::default(),
10585            },
10586        );
10587        assert_eq!(
10588            "Cast error: Cannot cast string '1000' to value of Decimal256(10, 8) type: value does not fit",
10589            err.unwrap_err().to_string()
10590        );
10591    }
10592
10593    #[test]
10594    fn test_cast_utf8_to_decimal256_overflow() {
10595        let overflow_str_array = StringArray::from(vec![
10596            i128::MAX.to_string(),
10597            i128::MIN.to_string(),
10598            "99999999999999999999999999999999999999".to_string(),
10599            "999999999999999999999999999999999999.99".to_string(),
10600            "99999999999999999999999999999999999.999".to_string(),
10601            i256::MAX.to_string(),
10602            i256::MIN.to_string(),
10603        ]);
10604        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10605
10606        test_cast_string_to_decimal256_overflow(overflow_array);
10607    }
10608
10609    #[test]
10610    fn test_cast_large_utf8_to_decimal256_overflow() {
10611        let overflow_str_array = LargeStringArray::from(vec![
10612            i128::MAX.to_string(),
10613            i128::MIN.to_string(),
10614            "99999999999999999999999999999999999999".to_string(),
10615            "999999999999999999999999999999999999.99".to_string(),
10616            "99999999999999999999999999999999999.999".to_string(),
10617            i256::MAX.to_string(),
10618            i256::MIN.to_string(),
10619        ]);
10620        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10621
10622        test_cast_string_to_decimal256_overflow(overflow_array);
10623    }
10624
10625    #[test]
10626    fn test_cast_outside_supported_range_for_nanoseconds() {
10627        const EXPECTED_ERROR_MESSAGE: &str = "The dates that can be represented as nanoseconds have to be between 1677-09-21T00:12:44.0 and 2262-04-11T23:47:16.854775804";
10628
10629        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
10630
10631        let cast_options = CastOptions {
10632            safe: false,
10633            format_options: FormatOptions::default(),
10634        };
10635
10636        let result =
10637            cast_string_to_timestamp::<i32, TimestampNanosecondType>(&array, None, &cast_options);
10638
10639        let err = result.unwrap_err();
10640        assert_eq!(
10641            err.to_string(),
10642            format!(
10643                "Cast error: Overflow converting {} to Nanosecond. {}",
10644                array.value(0),
10645                EXPECTED_ERROR_MESSAGE
10646            )
10647        );
10648    }
10649
10650    #[test]
10651    fn test_cast_date32_to_timestamp() {
10652        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10653        let array = Arc::new(a) as ArrayRef;
10654        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10655        let c = b.as_primitive::<TimestampSecondType>();
10656        assert_eq!(1609459200, c.value(0));
10657        assert_eq!(1640995200, c.value(1));
10658        assert!(c.is_null(2));
10659    }
10660
10661    #[test]
10662    fn test_cast_date32_to_timestamp_ms() {
10663        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10664        let array = Arc::new(a) as ArrayRef;
10665        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10666        let c = b
10667            .as_any()
10668            .downcast_ref::<TimestampMillisecondArray>()
10669            .unwrap();
10670        assert_eq!(1609459200000, c.value(0));
10671        assert_eq!(1640995200000, c.value(1));
10672        assert!(c.is_null(2));
10673    }
10674
10675    #[test]
10676    fn test_cast_date32_to_timestamp_us() {
10677        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10678        let array = Arc::new(a) as ArrayRef;
10679        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10680        let c = b
10681            .as_any()
10682            .downcast_ref::<TimestampMicrosecondArray>()
10683            .unwrap();
10684        assert_eq!(1609459200000000, c.value(0));
10685        assert_eq!(1640995200000000, c.value(1));
10686        assert!(c.is_null(2));
10687    }
10688
10689    #[test]
10690    fn test_cast_date32_to_timestamp_ns() {
10691        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10692        let array = Arc::new(a) as ArrayRef;
10693        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10694        let c = b
10695            .as_any()
10696            .downcast_ref::<TimestampNanosecondArray>()
10697            .unwrap();
10698        assert_eq!(1609459200000000000, c.value(0));
10699        assert_eq!(1640995200000000000, c.value(1));
10700        assert!(c.is_null(2));
10701    }
10702
10703    #[test]
10704    fn test_cast_date32_to_timestamp_us_overflow() {
10705        const MAX_DAYS_MICROS: i32 = (i64::MAX / MICROSECONDS_IN_DAY) as i32;
10706        let a = Date32Array::from(vec![Some(MAX_DAYS_MICROS), Some(MAX_DAYS_MICROS + 1), None]);
10707        let array = Arc::new(a) as ArrayRef;
10708        let err = cast_with_options(
10709            &array,
10710            &DataType::Timestamp(TimeUnit::Microsecond, None),
10711            &CastOptions {
10712                safe: false,
10713                format_options: FormatOptions::default(),
10714            },
10715        );
10716        assert!(err.is_err());
10717
10718        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10719        let c = b.as_primitive::<TimestampMicrosecondType>();
10720        assert_eq!(MAX_DAYS_MICROS as i64 * MICROSECONDS_IN_DAY, c.value(0));
10721        assert!(c.is_null(1));
10722        assert!(c.is_null(2));
10723    }
10724
10725    #[test]
10726    fn test_cast_date32_to_timestamp_ns_overflow() {
10727        // 2262-04-11, 2062-04-12
10728        let upper_limit = 106_751;
10729        let a = Date32Array::from(vec![Some(upper_limit), Some(upper_limit + 1), None]);
10730        let array = Arc::new(a) as ArrayRef;
10731        let err = cast_with_options(
10732            &array,
10733            &DataType::Timestamp(TimeUnit::Nanosecond, None),
10734            &CastOptions {
10735                safe: false,
10736                format_options: FormatOptions::default(),
10737            },
10738        );
10739        assert!(err.is_err());
10740
10741        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10742        let c = b.as_primitive::<TimestampNanosecondType>();
10743        assert_eq!(upper_limit as i64 * NANOSECONDS_IN_DAY, c.value(0));
10744        assert!(c.is_null(1));
10745        assert!(c.is_null(2));
10746    }
10747
10748    #[test]
10749    fn test_timezone_cast() {
10750        let a = StringArray::from(vec![
10751            "2000-01-01T12:00:00", // date + time valid
10752            "2020-12-15T12:34:56", // date + time valid
10753        ]);
10754        let array = Arc::new(a) as ArrayRef;
10755        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10756        let v = b.as_primitive::<TimestampNanosecondType>();
10757
10758        assert_eq!(v.value(0), 946728000000000000);
10759        assert_eq!(v.value(1), 1608035696000000000);
10760
10761        let b = cast(
10762            &b,
10763            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10764        )
10765        .unwrap();
10766        let v = b.as_primitive::<TimestampNanosecondType>();
10767
10768        assert_eq!(v.value(0), 946728000000000000);
10769        assert_eq!(v.value(1), 1608035696000000000);
10770
10771        let b = cast(
10772            &b,
10773            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10774        )
10775        .unwrap();
10776        let v = b.as_primitive::<TimestampMillisecondType>();
10777
10778        assert_eq!(v.value(0), 946728000000);
10779        assert_eq!(v.value(1), 1608035696000);
10780    }
10781
10782    #[test]
10783    fn test_cast_utf8_to_timestamp() {
10784        fn test_tz(tz: Arc<str>) {
10785            let valid = StringArray::from(vec![
10786                "2023-01-01 04:05:06.789000-08:00",
10787                "2023-01-01 04:05:06.789000-07:00",
10788                "2023-01-01 04:05:06.789 -0800",
10789                "2023-01-01 04:05:06.789 -08:00",
10790                "2023-01-01 040506 +0730",
10791                "2023-01-01 040506 +07:30",
10792                "2023-01-01 04:05:06.789",
10793                "2023-01-01 04:05:06",
10794                "2023-01-01",
10795            ]);
10796
10797            let array = Arc::new(valid) as ArrayRef;
10798            let b = cast_with_options(
10799                &array,
10800                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
10801                &CastOptions {
10802                    safe: false,
10803                    format_options: FormatOptions::default(),
10804                },
10805            )
10806            .unwrap();
10807
10808            let tz = tz.as_ref().parse().unwrap();
10809
10810            let as_tz =
10811                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
10812
10813            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
10814            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
10815
10816            let values = b.as_primitive::<TimestampNanosecondType>().values();
10817            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
10818            let local_results: Vec<_> = values.iter().map(as_local).collect();
10819
10820            // Absolute timestamps should be parsed preserving the same UTC instant
10821            assert_eq!(
10822                &utc_results[..6],
10823                &[
10824                    "2023-01-01 12:05:06.789".to_string(),
10825                    "2023-01-01 11:05:06.789".to_string(),
10826                    "2023-01-01 12:05:06.789".to_string(),
10827                    "2023-01-01 12:05:06.789".to_string(),
10828                    "2022-12-31 20:35:06".to_string(),
10829                    "2022-12-31 20:35:06".to_string(),
10830                ]
10831            );
10832            // Non-absolute timestamps should be parsed preserving the same local instant
10833            assert_eq!(
10834                &local_results[6..],
10835                &[
10836                    "2023-01-01 04:05:06.789".to_string(),
10837                    "2023-01-01 04:05:06".to_string(),
10838                    "2023-01-01 00:00:00".to_string()
10839                ]
10840            )
10841        }
10842
10843        test_tz("+00:00".into());
10844        test_tz("+02:00".into());
10845    }
10846
10847    #[test]
10848    fn test_cast_invalid_utf8() {
10849        let v1: &[u8] = b"\xFF invalid";
10850        let v2: &[u8] = b"\x00 Foo";
10851        let s = BinaryArray::from(vec![v1, v2]);
10852        let options = CastOptions {
10853            safe: true,
10854            format_options: FormatOptions::default(),
10855        };
10856        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
10857        let a = array.as_string::<i32>();
10858        a.to_data().validate_full().unwrap();
10859
10860        assert_eq!(a.null_count(), 1);
10861        assert_eq!(a.len(), 2);
10862        assert!(a.is_null(0));
10863        assert_eq!(a.value(0), "");
10864        assert_eq!(a.value(1), "\x00 Foo");
10865    }
10866
10867    #[test]
10868    fn test_cast_sliced_binary_with_hidden_invalid_utf8() {
10869        // The invalid first value is outside the slice, so it must not fail the cast
10870        let values: Vec<&[u8]> = vec![b"\xFF", b"a", b"b", b"c", b"d", b"e", b"f"];
10871        let arrays: [ArrayRef; 2] = [
10872            Arc::new(BinaryArray::from(values.clone()).slice(1, 6)),
10873            Arc::new(LargeBinaryArray::from(values).slice(1, 6)),
10874        ];
10875        let options = CastOptions {
10876            safe: false,
10877            format_options: FormatOptions::default(),
10878        };
10879        let expected = StringArray::from(vec!["a", "b", "c", "d", "e", "f"]);
10880        for array in &arrays {
10881            for to_type in [DataType::Utf8, DataType::LargeUtf8, DataType::Utf8View] {
10882                let result = cast_with_options(array, &to_type, &options).unwrap();
10883                assert_eq!(result.as_ref(), cast(&expected, &to_type).unwrap().as_ref());
10884            }
10885
10886            // A dictionary with fewer keys than half its values takes a separate
10887            // path to Utf8View
10888            let keys = Int32Array::from(vec![5, 0]);
10889            let dict = DictionaryArray::try_new(keys, Arc::clone(array)).unwrap();
10890            let result = cast_with_options(&dict, &DataType::Utf8View, &options).unwrap();
10891            assert_eq!(result.as_ref(), &StringViewArray::from(vec!["f", "a"]));
10892        }
10893    }
10894
10895    #[test]
10896    fn test_cast_utf8_to_timestamptz() {
10897        let valid = StringArray::from(vec!["2023-01-01"]);
10898
10899        let array = Arc::new(valid) as ArrayRef;
10900        let b = cast(
10901            &array,
10902            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10903        )
10904        .unwrap();
10905
10906        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
10907
10908        assert_eq!(b.data_type(), &expect);
10909        let c = b
10910            .as_any()
10911            .downcast_ref::<TimestampNanosecondArray>()
10912            .unwrap();
10913        assert_eq!(1672531200000000000, c.value(0));
10914    }
10915
10916    #[test]
10917    fn test_cast_out_of_precision_decimal_to_string() {
10918        // Decimal values are not validated against their type's declared
10919        // precision by default. Check that out-of-precision values are rendered
10920        // in full when cast to strings, rather than truncated to the declared
10921        // precision (https://github.com/apache/arrow-rs/issues/10866)
10922        let array = create_decimal128_array(vec![Some(123456789), Some(-123456789)], 7, 3).unwrap();
10923        let b = cast(&array, &DataType::Utf8).unwrap();
10924        let c = b.as_string::<i32>();
10925        assert_eq!("123456.789", c.value(0));
10926        assert_eq!("-123456.789", c.value(1));
10927    }
10928
10929    #[test]
10930    fn test_cast_decimal_to_string() {
10931        assert!(can_cast_types(
10932            &DataType::Decimal32(9, 4),
10933            &DataType::Utf8View
10934        ));
10935        assert!(can_cast_types(
10936            &DataType::Decimal64(16, 4),
10937            &DataType::Utf8View
10938        ));
10939        assert!(can_cast_types(
10940            &DataType::Decimal128(10, 4),
10941            &DataType::Utf8View
10942        ));
10943        assert!(can_cast_types(
10944            &DataType::Decimal256(38, 10),
10945            &DataType::Utf8View
10946        ));
10947
10948        macro_rules! assert_decimal_values {
10949            ($array:expr) => {
10950                let c = $array;
10951                assert_eq!("1123.454", c.value(0));
10952                assert_eq!("2123.456", c.value(1));
10953                assert_eq!("-3123.453", c.value(2));
10954                assert_eq!("-3123.456", c.value(3));
10955                assert_eq!("0.000", c.value(4));
10956                assert_eq!("0.123", c.value(5));
10957                assert!(c.is_null(6));
10958            };
10959        }
10960
10961        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
10962            output_type: DataType,
10963            array: PrimitiveArray<IN>,
10964        ) {
10965            let b = cast(&array, &output_type).unwrap();
10966
10967            assert_eq!(b.data_type(), &output_type);
10968            match b.data_type() {
10969                DataType::Utf8View => {
10970                    let c = b.as_string_view();
10971                    assert_decimal_values!(c);
10972                }
10973                DataType::Utf8 | DataType::LargeUtf8 => {
10974                    let c = b.as_string::<OffsetSize>();
10975                    assert_decimal_values!(c);
10976                }
10977                _ => (),
10978            }
10979        }
10980
10981        let array32: Vec<Option<i32>> = vec![
10982            Some(1123454),
10983            Some(2123456),
10984            Some(-3123453),
10985            Some(-3123456),
10986            Some(0),
10987            Some(123),
10988            None,
10989        ];
10990        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
10991        let array128: Vec<Option<i128>> =
10992            array64.iter().map(|num| num.map(|x| x as i128)).collect();
10993        let array256: Vec<Option<i256>> = array128
10994            .iter()
10995            .map(|num| num.map(i256::from_i128))
10996            .collect();
10997
10998        test_decimal_to_string::<Decimal32Type, i32>(
10999            DataType::Utf8View,
11000            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11001        );
11002        test_decimal_to_string::<Decimal32Type, i32>(
11003            DataType::Utf8,
11004            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11005        );
11006        test_decimal_to_string::<Decimal32Type, i64>(
11007            DataType::LargeUtf8,
11008            create_decimal32_array(array32, 7, 3).unwrap(),
11009        );
11010
11011        test_decimal_to_string::<Decimal64Type, i32>(
11012            DataType::Utf8View,
11013            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11014        );
11015        test_decimal_to_string::<Decimal64Type, i32>(
11016            DataType::Utf8,
11017            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11018        );
11019        test_decimal_to_string::<Decimal64Type, i64>(
11020            DataType::LargeUtf8,
11021            create_decimal64_array(array64, 7, 3).unwrap(),
11022        );
11023
11024        test_decimal_to_string::<Decimal128Type, i32>(
11025            DataType::Utf8View,
11026            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11027        );
11028        test_decimal_to_string::<Decimal128Type, i32>(
11029            DataType::Utf8,
11030            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11031        );
11032        test_decimal_to_string::<Decimal128Type, i64>(
11033            DataType::LargeUtf8,
11034            create_decimal128_array(array128, 7, 3).unwrap(),
11035        );
11036
11037        test_decimal_to_string::<Decimal256Type, i32>(
11038            DataType::Utf8View,
11039            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11040        );
11041        test_decimal_to_string::<Decimal256Type, i32>(
11042            DataType::Utf8,
11043            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11044        );
11045        test_decimal_to_string::<Decimal256Type, i64>(
11046            DataType::LargeUtf8,
11047            create_decimal256_array(array256, 7, 3).unwrap(),
11048        );
11049    }
11050
11051    #[test]
11052    fn test_cast_numeric_to_decimal128_precision_overflow() {
11053        let array = Int64Array::from(vec![1234567]);
11054        let array = Arc::new(array) as ArrayRef;
11055        let casted_array = cast_with_options(
11056            &array,
11057            &DataType::Decimal128(7, 3),
11058            &CastOptions {
11059                safe: true,
11060                format_options: FormatOptions::default(),
11061            },
11062        );
11063        assert!(casted_array.is_ok());
11064        assert!(casted_array.unwrap().is_null(0));
11065
11066        let err = cast_with_options(
11067            &array,
11068            &DataType::Decimal128(7, 3),
11069            &CastOptions {
11070                safe: false,
11071                format_options: FormatOptions::default(),
11072            },
11073        );
11074        assert_eq!(
11075            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11076            err.unwrap_err().to_string()
11077        );
11078    }
11079
11080    #[test]
11081    fn test_cast_numeric_to_decimal256_precision_overflow() {
11082        let array = Int64Array::from(vec![1234567]);
11083        let array = Arc::new(array) as ArrayRef;
11084        let casted_array = cast_with_options(
11085            &array,
11086            &DataType::Decimal256(7, 3),
11087            &CastOptions {
11088                safe: true,
11089                format_options: FormatOptions::default(),
11090            },
11091        );
11092        assert!(casted_array.is_ok());
11093        assert!(casted_array.unwrap().is_null(0));
11094
11095        let err = cast_with_options(
11096            &array,
11097            &DataType::Decimal256(7, 3),
11098            &CastOptions {
11099                safe: false,
11100                format_options: FormatOptions::default(),
11101            },
11102        );
11103        assert_eq!(
11104            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11105            err.unwrap_err().to_string()
11106        );
11107    }
11108
11109    /// helper function to test casting from duration to interval
11110    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11111        array: Vec<i64>,
11112        cast_options: &CastOptions,
11113    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11114        let array = PrimitiveArray::<T>::new(array.into(), None);
11115        let array = Arc::new(array) as ArrayRef;
11116        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11117        let out = cast_with_options(&array, &interval, cast_options)?;
11118        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11119        Ok(out)
11120    }
11121
11122    #[test]
11123    fn test_cast_from_duration_to_interval() {
11124        // from duration second to interval month day nano
11125        let array = vec![1234567];
11126        let casted_array =
11127            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11128                .unwrap();
11129        assert_eq!(
11130            casted_array.data_type(),
11131            &DataType::Interval(IntervalUnit::MonthDayNano)
11132        );
11133        assert_eq!(
11134            casted_array.value(0),
11135            IntervalMonthDayNano::new(0, 0, 1234567000000000)
11136        );
11137
11138        let array = vec![i64::MAX];
11139        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11140            array.clone(),
11141            &CastOptions::default(),
11142        )
11143        .unwrap();
11144        assert!(!casted_array.is_valid(0));
11145
11146        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11147            array,
11148            &CastOptions {
11149                safe: false,
11150                format_options: FormatOptions::default(),
11151            },
11152        );
11153        assert!(casted_array.is_err());
11154
11155        // from duration millisecond to interval month day nano
11156        let array = vec![1234567];
11157        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11158            array,
11159            &CastOptions::default(),
11160        )
11161        .unwrap();
11162        assert_eq!(
11163            casted_array.data_type(),
11164            &DataType::Interval(IntervalUnit::MonthDayNano)
11165        );
11166        assert_eq!(
11167            casted_array.value(0),
11168            IntervalMonthDayNano::new(0, 0, 1234567000000)
11169        );
11170
11171        let array = vec![i64::MAX];
11172        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11173            array.clone(),
11174            &CastOptions::default(),
11175        )
11176        .unwrap();
11177        assert!(!casted_array.is_valid(0));
11178
11179        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11180            array,
11181            &CastOptions {
11182                safe: false,
11183                format_options: FormatOptions::default(),
11184            },
11185        );
11186        assert!(casted_array.is_err());
11187
11188        // from duration microsecond to interval month day nano
11189        let array = vec![1234567];
11190        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11191            array,
11192            &CastOptions::default(),
11193        )
11194        .unwrap();
11195        assert_eq!(
11196            casted_array.data_type(),
11197            &DataType::Interval(IntervalUnit::MonthDayNano)
11198        );
11199        assert_eq!(
11200            casted_array.value(0),
11201            IntervalMonthDayNano::new(0, 0, 1234567000)
11202        );
11203
11204        let array = vec![i64::MAX];
11205        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11206            array.clone(),
11207            &CastOptions::default(),
11208        )
11209        .unwrap();
11210        assert!(!casted_array.is_valid(0));
11211
11212        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11213            array,
11214            &CastOptions {
11215                safe: false,
11216                format_options: FormatOptions::default(),
11217            },
11218        );
11219        assert!(casted_array.is_err());
11220
11221        // from duration nanosecond to interval month day nano
11222        let array = vec![1234567];
11223        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11224            array,
11225            &CastOptions::default(),
11226        )
11227        .unwrap();
11228        assert_eq!(
11229            casted_array.data_type(),
11230            &DataType::Interval(IntervalUnit::MonthDayNano)
11231        );
11232        assert_eq!(
11233            casted_array.value(0),
11234            IntervalMonthDayNano::new(0, 0, 1234567)
11235        );
11236
11237        let array = vec![i64::MAX];
11238        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11239            array,
11240            &CastOptions {
11241                safe: false,
11242                format_options: FormatOptions::default(),
11243            },
11244        )
11245        .unwrap();
11246        assert_eq!(
11247            casted_array.value(0),
11248            IntervalMonthDayNano::new(0, 0, i64::MAX)
11249        );
11250    }
11251
11252    /// helper function to test casting from interval to duration
11253    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
11254        array: &IntervalMonthDayNanoArray,
11255        cast_options: &CastOptions,
11256    ) -> Result<PrimitiveArray<T>, ArrowError> {
11257        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
11258        casted_array
11259            .as_any()
11260            .downcast_ref::<PrimitiveArray<T>>()
11261            .ok_or_else(|| {
11262                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
11263            })
11264            .cloned()
11265    }
11266
11267    #[test]
11268    fn test_cast_from_interval_to_duration() {
11269        let nullable = CastOptions::default();
11270        let fallible = CastOptions {
11271            safe: false,
11272            format_options: FormatOptions::default(),
11273        };
11274        let v = IntervalMonthDayNano::new(0, 0, 1234567);
11275
11276        // from interval month day nano to duration second
11277        let array = vec![v].into();
11278        let casted_array: DurationSecondArray =
11279            cast_from_interval_to_duration(&array, &nullable).unwrap();
11280        assert_eq!(casted_array.value(0), 0);
11281
11282        let array = vec![IntervalMonthDayNano::MAX].into();
11283        let casted_array: DurationSecondArray =
11284            cast_from_interval_to_duration(&array, &nullable).unwrap();
11285        assert!(!casted_array.is_valid(0));
11286
11287        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
11288        assert!(res.is_err());
11289
11290        // from interval month day nano to duration millisecond
11291        let array = vec![v].into();
11292        let casted_array: DurationMillisecondArray =
11293            cast_from_interval_to_duration(&array, &nullable).unwrap();
11294        assert_eq!(casted_array.value(0), 1);
11295
11296        let array = vec![IntervalMonthDayNano::MAX].into();
11297        let casted_array: DurationMillisecondArray =
11298            cast_from_interval_to_duration(&array, &nullable).unwrap();
11299        assert!(!casted_array.is_valid(0));
11300
11301        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
11302        assert!(res.is_err());
11303
11304        // from interval month day nano to duration microsecond
11305        let array = vec![v].into();
11306        let casted_array: DurationMicrosecondArray =
11307            cast_from_interval_to_duration(&array, &nullable).unwrap();
11308        assert_eq!(casted_array.value(0), 1234);
11309
11310        let array = vec![IntervalMonthDayNano::MAX].into();
11311        let casted_array =
11312            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
11313        assert!(!casted_array.is_valid(0));
11314
11315        let casted_array =
11316            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
11317        assert!(casted_array.is_err());
11318
11319        // from interval month day nano to duration nanosecond
11320        let array = vec![v].into();
11321        let casted_array: DurationNanosecondArray =
11322            cast_from_interval_to_duration(&array, &nullable).unwrap();
11323        assert_eq!(casted_array.value(0), 1234567);
11324
11325        let array = vec![IntervalMonthDayNano::MAX].into();
11326        let casted_array: DurationNanosecondArray =
11327            cast_from_interval_to_duration(&array, &nullable).unwrap();
11328        assert!(!casted_array.is_valid(0));
11329
11330        let casted_array =
11331            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
11332        assert!(casted_array.is_err());
11333
11334        let array = vec![
11335            IntervalMonthDayNanoType::make_value(0, 1, 0),
11336            IntervalMonthDayNanoType::make_value(-1, 0, 0),
11337            IntervalMonthDayNanoType::make_value(1, 1, 0),
11338            IntervalMonthDayNanoType::make_value(1, 0, 1),
11339            IntervalMonthDayNanoType::make_value(0, 0, -1),
11340        ]
11341        .into();
11342        let casted_array =
11343            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
11344        assert!(!casted_array.is_valid(0));
11345        assert!(!casted_array.is_valid(1));
11346        assert!(!casted_array.is_valid(2));
11347        assert!(!casted_array.is_valid(3));
11348        assert!(casted_array.is_valid(4));
11349        assert_eq!(casted_array.value(4), -1);
11350    }
11351
11352    /// helper function to test casting from interval year month to interval month day nano
11353    fn cast_from_interval_year_month_to_interval_month_day_nano(
11354        array: Vec<i32>,
11355        cast_options: &CastOptions,
11356    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11357        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
11358        let array = Arc::new(array) as ArrayRef;
11359        let casted_array = cast_with_options(
11360            &array,
11361            &DataType::Interval(IntervalUnit::MonthDayNano),
11362            cast_options,
11363        )?;
11364        casted_array
11365            .as_any()
11366            .downcast_ref::<IntervalMonthDayNanoArray>()
11367            .ok_or_else(|| {
11368                ArrowError::ComputeError(
11369                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
11370                )
11371            })
11372            .cloned()
11373    }
11374
11375    #[test]
11376    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
11377        // from interval year month to interval month day nano
11378        let array = vec![1234567];
11379        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
11380            array,
11381            &CastOptions::default(),
11382        )
11383        .unwrap();
11384        assert_eq!(
11385            casted_array.data_type(),
11386            &DataType::Interval(IntervalUnit::MonthDayNano)
11387        );
11388        assert_eq!(
11389            casted_array.value(0),
11390            IntervalMonthDayNano::new(1234567, 0, 0)
11391        );
11392    }
11393
11394    /// helper function to test casting from interval day time to interval month day nano
11395    fn cast_from_interval_day_time_to_interval_month_day_nano(
11396        array: Vec<IntervalDayTime>,
11397        cast_options: &CastOptions,
11398    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11399        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
11400        let array = Arc::new(array) as ArrayRef;
11401        let casted_array = cast_with_options(
11402            &array,
11403            &DataType::Interval(IntervalUnit::MonthDayNano),
11404            cast_options,
11405        )?;
11406        Ok(casted_array
11407            .as_primitive::<IntervalMonthDayNanoType>()
11408            .clone())
11409    }
11410
11411    #[test]
11412    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
11413        // from interval day time to interval month day nano
11414        let array = vec![IntervalDayTime::new(123, 0)];
11415        let casted_array =
11416            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
11417                .unwrap();
11418        assert_eq!(
11419            casted_array.data_type(),
11420            &DataType::Interval(IntervalUnit::MonthDayNano)
11421        );
11422        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
11423    }
11424
11425    #[test]
11426    fn test_can_cast_interval_to_int64_matches_cast() {
11427        // Assert the two agree for every interval unit, rather than hard coding the answer.
11428        let arrays: Vec<ArrayRef> = vec![
11429            Arc::new(IntervalYearMonthArray::from(vec![12])),
11430            Arc::new(IntervalDayTimeArray::from(vec![IntervalDayTime::new(1, 2)])),
11431            Arc::new(IntervalMonthDayNanoArray::from(vec![
11432                IntervalMonthDayNano::new(1, 2, 3),
11433            ])),
11434        ];
11435        for array in arrays {
11436            let from = array.data_type();
11437            assert_eq!(
11438                can_cast_types(from, &DataType::Int64),
11439                cast(&array, &DataType::Int64).is_ok(),
11440                "can_cast_types disagrees with cast for {from} -> Int64"
11441            );
11442        }
11443    }
11444
11445    #[test]
11446    fn test_cast_union_to_int64_skips_uncastable_interval_child() {
11447        // `resolve_child_array` picks the first child `can_cast_types` accepts, so an
11448        // over-permissive answer made it pick the interval child instead of the `Utf8` one.
11449        let fields = UnionFields::try_new(
11450            [0, 1],
11451            [
11452                Field::new("iv", DataType::Interval(IntervalUnit::YearMonth), true),
11453                Field::new("s", DataType::Utf8, true),
11454            ],
11455        )
11456        .unwrap();
11457        let union = UnionArray::try_new(
11458            fields,
11459            vec![0, 1, 0].into(),
11460            None,
11461            vec![
11462                Arc::new(IntervalYearMonthArray::from(vec![Some(12), None, Some(24)])),
11463                Arc::new(StringArray::from(vec![None, Some("77"), None])),
11464            ],
11465        )
11466        .unwrap();
11467
11468        let casted = cast(&union, &DataType::Int64).unwrap();
11469        let casted = casted.as_primitive::<Int64Type>();
11470        assert!(casted.is_null(0));
11471        assert_eq!(casted.value(1), 77);
11472        assert!(casted.is_null(2));
11473    }
11474
11475    #[test]
11476    fn test_cast_below_unixtimestamp() {
11477        let valid = StringArray::from(vec![
11478            "1900-01-03 23:59:59",
11479            "1969-12-31 00:00:01",
11480            "1989-12-31 00:00:01",
11481        ]);
11482
11483        let array = Arc::new(valid) as ArrayRef;
11484        let casted_array = cast_with_options(
11485            &array,
11486            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11487            &CastOptions {
11488                safe: false,
11489                format_options: FormatOptions::default(),
11490            },
11491        )
11492        .unwrap();
11493
11494        let ts_array = casted_array
11495            .as_primitive::<TimestampNanosecondType>()
11496            .values()
11497            .iter()
11498            .map(|ts| ts / 1_000_000)
11499            .collect::<Vec<_>>();
11500
11501        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
11502        let casted_array = cast(&array, &DataType::Date32).unwrap();
11503        let date_array = casted_array.as_primitive::<Date32Type>();
11504        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
11505        let string_array = casted_array.as_string::<i32>();
11506        assert_eq!("1900-01-03", string_array.value(0));
11507        assert_eq!("1969-12-31", string_array.value(1));
11508        assert_eq!("1989-12-31", string_array.value(2));
11509    }
11510
11511    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
11512        safe: true,
11513        format_options: FormatOptions::new(),
11514    };
11515
11516    #[test]
11517    #[expect(clippy::assertions_on_constants)]
11518    fn test_const_options() {
11519        assert!(CAST_OPTIONS.safe)
11520    }
11521
11522    #[test]
11523    fn test_cast_string_to_timestamp_invalid_tz() {
11524        // content after Z should be ignored
11525        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
11526        let array = StringArray::from(vec![Some(bad_timestamp)]);
11527
11528        let data_types = [
11529            DataType::Timestamp(TimeUnit::Second, None),
11530            DataType::Timestamp(TimeUnit::Millisecond, None),
11531            DataType::Timestamp(TimeUnit::Microsecond, None),
11532            DataType::Timestamp(TimeUnit::Nanosecond, None),
11533        ];
11534
11535        let cast_options = CastOptions {
11536            safe: false,
11537            ..Default::default()
11538        };
11539
11540        for dt in data_types {
11541            // The trailing detail of the message differs depending on whether
11542            // `chrono-tz` is enabled, so only the common prefix is asserted.
11543            let err = cast_with_options(&array, &dt, &cast_options)
11544                .unwrap_err()
11545                .to_string();
11546            assert!(
11547                err.starts_with("Parser error: Invalid timezone \"ZZTOP\":"),
11548                "{err}"
11549            );
11550        }
11551    }
11552    #[test]
11553    fn test_cast_struct_to_struct() {
11554        let struct_type = DataType::Struct(
11555            vec![
11556                Field::new("a", DataType::Boolean, false),
11557                Field::new("b", DataType::Int32, false),
11558            ]
11559            .into(),
11560        );
11561        let to_type = DataType::Struct(
11562            vec![
11563                Field::new("a", DataType::Utf8, false),
11564                Field::new("b", DataType::Utf8, false),
11565            ]
11566            .into(),
11567        );
11568        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11569        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11570        let struct_array = StructArray::from(vec![
11571            (
11572                Arc::new(Field::new("b", DataType::Boolean, false)),
11573                boolean.clone() as ArrayRef,
11574            ),
11575            (
11576                Arc::new(Field::new("c", DataType::Int32, false)),
11577                int.clone() as ArrayRef,
11578            ),
11579        ]);
11580        let casted_array = cast(&struct_array, &to_type).unwrap();
11581        let casted_array = casted_array.as_struct();
11582        assert_eq!(casted_array.data_type(), &to_type);
11583        let casted_boolean_array = casted_array
11584            .column(0)
11585            .as_string::<i32>()
11586            .into_iter()
11587            .flatten()
11588            .collect::<Vec<_>>();
11589        let casted_int_array = casted_array
11590            .column(1)
11591            .as_string::<i32>()
11592            .into_iter()
11593            .flatten()
11594            .collect::<Vec<_>>();
11595        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
11596        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
11597
11598        // test for can't cast
11599        let to_type = DataType::Struct(
11600            vec![
11601                Field::new("a", DataType::Date32, false),
11602                Field::new("b", DataType::Utf8, false),
11603            ]
11604            .into(),
11605        );
11606        assert!(!can_cast_types(&struct_type, &to_type));
11607        let result = cast(&struct_array, &to_type);
11608        assert_eq!(
11609            "Cast error: Casting from Boolean to Date32 not supported",
11610            result.unwrap_err().to_string()
11611        );
11612    }
11613
11614    #[test]
11615    fn test_cast_struct_to_struct_nullability() {
11616        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11617        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
11618        let struct_array = StructArray::from(vec![
11619            (
11620                Arc::new(Field::new("b", DataType::Boolean, false)),
11621                boolean.clone() as ArrayRef,
11622            ),
11623            (
11624                Arc::new(Field::new("c", DataType::Int32, true)),
11625                int.clone() as ArrayRef,
11626            ),
11627        ]);
11628
11629        // okay: nullable to nullable
11630        let to_type = DataType::Struct(
11631            vec![
11632                Field::new("a", DataType::Utf8, false),
11633                Field::new("b", DataType::Utf8, true),
11634            ]
11635            .into(),
11636        );
11637        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
11638
11639        // error: nullable to non-nullable
11640        let to_type = DataType::Struct(
11641            vec![
11642                Field::new("a", DataType::Utf8, false),
11643                Field::new("b", DataType::Utf8, false),
11644            ]
11645            .into(),
11646        );
11647        cast(&struct_array, &to_type)
11648            .expect_err("Cast nullable to non-nullable struct field should fail");
11649
11650        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11651        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
11652        let struct_array = StructArray::from(vec![
11653            (
11654                Arc::new(Field::new("b", DataType::Boolean, false)),
11655                boolean.clone() as ArrayRef,
11656            ),
11657            (
11658                Arc::new(Field::new("c", DataType::Int32, false)),
11659                int.clone() as ArrayRef,
11660            ),
11661        ]);
11662
11663        // okay: non-nullable to non-nullable
11664        let to_type = DataType::Struct(
11665            vec![
11666                Field::new("a", DataType::Utf8, false),
11667                Field::new("b", DataType::Utf8, false),
11668            ]
11669            .into(),
11670        );
11671        cast(&struct_array, &to_type)
11672            .expect("Cast non-nullable to non-nullable struct field should work");
11673
11674        // err: non-nullable to non-nullable but overflowing return null during casting
11675        let to_type = DataType::Struct(
11676            vec![
11677                Field::new("a", DataType::Utf8, false),
11678                Field::new("b", DataType::Int8, false),
11679            ]
11680            .into(),
11681        );
11682        cast(&struct_array, &to_type).expect_err(
11683            "Cast non-nullable to non-nullable struct field returning null should fail",
11684        );
11685    }
11686
11687    #[test]
11688    fn test_cast_struct_to_non_struct() {
11689        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
11690        let struct_array = StructArray::from(vec![(
11691            Arc::new(Field::new("a", DataType::Boolean, false)),
11692            boolean.clone() as ArrayRef,
11693        )]);
11694        let to_type = DataType::Utf8;
11695        let result = cast(&struct_array, &to_type);
11696        assert_eq!(
11697            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
11698            result.unwrap_err().to_string()
11699        );
11700    }
11701
11702    #[test]
11703    fn test_cast_non_struct_to_struct() {
11704        let array = StringArray::from(vec!["a", "b"]);
11705        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
11706        let result = cast(&array, &to_type);
11707        assert_eq!(
11708            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
11709            result.unwrap_err().to_string()
11710        );
11711    }
11712
11713    #[test]
11714    fn test_cast_struct_with_different_field_order() {
11715        // Test slow path: fields are in different order
11716        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11717        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11718        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11719
11720        let struct_array = StructArray::from(vec![
11721            (
11722                Arc::new(Field::new("a", DataType::Boolean, false)),
11723                boolean.clone() as ArrayRef,
11724            ),
11725            (
11726                Arc::new(Field::new("b", DataType::Int32, false)),
11727                int.clone() as ArrayRef,
11728            ),
11729            (
11730                Arc::new(Field::new("c", DataType::Utf8, false)),
11731                string.clone() as ArrayRef,
11732            ),
11733        ]);
11734
11735        // Target has fields in different order: c, a, b instead of a, b, c
11736        let to_type = DataType::Struct(
11737            vec![
11738                Field::new("c", DataType::Utf8, false),
11739                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
11740                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
11741            ]
11742            .into(),
11743        );
11744
11745        let result = cast(&struct_array, &to_type).unwrap();
11746        let result_struct = result.as_struct();
11747
11748        assert_eq!(result_struct.data_type(), &to_type);
11749        assert_eq!(result_struct.num_columns(), 3);
11750
11751        // Verify field "c" (originally position 2, now position 0) remains Utf8
11752        let c_column = result_struct.column(0).as_string::<i32>();
11753        assert_eq!(
11754            c_column.into_iter().flatten().collect::<Vec<_>>(),
11755            vec!["foo", "bar", "baz", "qux"]
11756        );
11757
11758        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
11759        let a_column = result_struct.column(1).as_string::<i32>();
11760        assert_eq!(
11761            a_column.into_iter().flatten().collect::<Vec<_>>(),
11762            vec!["false", "false", "true", "true"]
11763        );
11764
11765        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
11766        let b_column = result_struct.column(2).as_string::<i32>();
11767        assert_eq!(
11768            b_column.into_iter().flatten().collect::<Vec<_>>(),
11769            vec!["42", "28", "19", "31"]
11770        );
11771    }
11772
11773    #[test]
11774    fn test_cast_struct_with_missing_field() {
11775        // Test that casting fails when target has a field not present in source
11776        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
11777        let struct_array = StructArray::from(vec![(
11778            Arc::new(Field::new("a", DataType::Boolean, false)),
11779            boolean.clone() as ArrayRef,
11780        )]);
11781
11782        let to_type = DataType::Struct(
11783            vec![
11784                Field::new("a", DataType::Utf8, false),
11785                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
11786            ]
11787            .into(),
11788        );
11789
11790        let result = cast(&struct_array, &to_type);
11791        assert!(result.is_err());
11792        assert_eq!(
11793            result.unwrap_err().to_string(),
11794            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
11795        );
11796    }
11797
11798    #[test]
11799    fn test_cast_struct_with_subset_of_fields() {
11800        // Test casting to a struct with fewer fields (selecting a subset)
11801        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11802        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11803        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11804
11805        let struct_array = StructArray::from(vec![
11806            (
11807                Arc::new(Field::new("a", DataType::Boolean, false)),
11808                boolean.clone() as ArrayRef,
11809            ),
11810            (
11811                Arc::new(Field::new("b", DataType::Int32, false)),
11812                int.clone() as ArrayRef,
11813            ),
11814            (
11815                Arc::new(Field::new("c", DataType::Utf8, false)),
11816                string.clone() as ArrayRef,
11817            ),
11818        ]);
11819
11820        // Target has only fields "c" and "a", omitting "b"
11821        let to_type = DataType::Struct(
11822            vec![
11823                Field::new("c", DataType::Utf8, false),
11824                Field::new("a", DataType::Utf8, false),
11825            ]
11826            .into(),
11827        );
11828
11829        let result = cast(&struct_array, &to_type).unwrap();
11830        let result_struct = result.as_struct();
11831
11832        assert_eq!(result_struct.data_type(), &to_type);
11833        assert_eq!(result_struct.num_columns(), 2);
11834
11835        // Verify field "c" remains Utf8
11836        let c_column = result_struct.column(0).as_string::<i32>();
11837        assert_eq!(
11838            c_column.into_iter().flatten().collect::<Vec<_>>(),
11839            vec!["foo", "bar", "baz", "qux"]
11840        );
11841
11842        // Verify field "a" was cast from Boolean to Utf8
11843        let a_column = result_struct.column(1).as_string::<i32>();
11844        assert_eq!(
11845            a_column.into_iter().flatten().collect::<Vec<_>>(),
11846            vec!["false", "false", "true", "true"]
11847        );
11848    }
11849
11850    #[test]
11851    fn test_can_cast_struct_rename_field() {
11852        // Test that can_cast_types returns false when target has a field not in source
11853        let from_type = DataType::Struct(
11854            vec![
11855                Field::new("a", DataType::Int32, false),
11856                Field::new("b", DataType::Utf8, false),
11857            ]
11858            .into(),
11859        );
11860
11861        let to_type = DataType::Struct(
11862            vec![
11863                Field::new("a", DataType::Int64, false),
11864                Field::new("c", DataType::Boolean, false), // Field "c" not in source
11865            ]
11866            .into(),
11867        );
11868
11869        assert!(can_cast_types(&from_type, &to_type));
11870    }
11871
11872    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
11873        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
11874        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
11875        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
11876        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
11877    }
11878
11879    #[test]
11880    fn test_decimal_to_decimal_coverage() {
11881        let test_cases = [
11882            // increase precision, increase scale, infallible
11883            DecimalCastTestConfig {
11884                input_prec: 5,
11885                input_scale: 1,
11886                input_repr: 99999, // 9999.9
11887                output_prec: 10,
11888                output_scale: 6,
11889                expected_output_repr: Ok(9999900000), // 9999.900000
11890            },
11891            // increase precision, increase scale, fallible, safe
11892            DecimalCastTestConfig {
11893                input_prec: 5,
11894                input_scale: 1,
11895                input_repr: 99, // 9999.9
11896                output_prec: 7,
11897                output_scale: 6,
11898                expected_output_repr: Ok(9900000), // 9.900000
11899            },
11900            // increase precision, increase scale, fallible, unsafe
11901            DecimalCastTestConfig {
11902                input_prec: 5,
11903                input_scale: 1,
11904                input_repr: 99999, // 9999.9
11905                output_prec: 7,
11906                output_scale: 6,
11907                expected_output_repr: Err("Invalid argument error: 9999.900000 is too large to store in a {} of precision 7. Max is 9.999999".to_string()) // max is 9.999999
11908            },
11909            // increase precision, decrease scale, always infallible
11910            DecimalCastTestConfig {
11911                input_prec: 5,
11912                input_scale: 3,
11913                input_repr: 99999, // 99.999
11914                output_prec: 10,
11915                output_scale: 2,
11916                expected_output_repr: Ok(10000), // 100.00
11917            },
11918            // increase precision, decrease scale, no rounding
11919            DecimalCastTestConfig {
11920                input_prec: 5,
11921                input_scale: 3,
11922                input_repr: 99994, // 99.994
11923                output_prec: 10,
11924                output_scale: 2,
11925                expected_output_repr: Ok(9999), // 99.99
11926            },
11927            // increase precision, don't change scale, always infallible
11928            DecimalCastTestConfig {
11929                input_prec: 5,
11930                input_scale: 3,
11931                input_repr: 99999, // 99.999
11932                output_prec: 10,
11933                output_scale: 3,
11934                expected_output_repr: Ok(99999), // 99.999
11935            },
11936            // decrease precision, increase scale, safe
11937            DecimalCastTestConfig {
11938                input_prec: 10,
11939                input_scale: 5,
11940                input_repr: 999999, // 9.99999
11941                output_prec: 8,
11942                output_scale: 7,
11943                expected_output_repr: Ok(99999900), // 9.9999900
11944            },
11945            // decrease precision, increase scale, unsafe
11946            DecimalCastTestConfig {
11947                input_prec: 10,
11948                input_scale: 5,
11949                input_repr: 9999999, // 99.99999
11950                output_prec: 8,
11951                output_scale: 7,
11952                expected_output_repr: Err("Invalid argument error: 99.9999900 is too large to store in a {} of precision 8. Max is 9.9999999".to_string()) // max is 9.9999999
11953            },
11954            // decrease precision, decrease scale, safe, infallible
11955            DecimalCastTestConfig {
11956                input_prec: 7,
11957                input_scale: 4,
11958                input_repr: 9999999, // 999.9999
11959                output_prec: 6,
11960                output_scale: 2,
11961                expected_output_repr: Ok(100000),
11962            },
11963            // decrease precision, decrease scale, safe, fallible
11964            DecimalCastTestConfig {
11965                input_prec: 10,
11966                input_scale: 5,
11967                input_repr: 12345678, // 123.45678
11968                output_prec: 8,
11969                output_scale: 3,
11970                expected_output_repr: Ok(123457), // 123.457
11971            },
11972            // decrease precision, decrease scale, unsafe
11973            DecimalCastTestConfig {
11974                input_prec: 10,
11975                input_scale: 5,
11976                input_repr: 9999999, // 99.99999
11977                output_prec: 4,
11978                output_scale: 3,
11979                expected_output_repr: Err("Invalid argument error: 100.000 is too large to store in a {} of precision 4. Max is 9.999".to_string()) // max is 9.999
11980            },
11981            // decrease precision, same scale, safe
11982            DecimalCastTestConfig {
11983                input_prec: 10,
11984                input_scale: 5,
11985                input_repr: 999999, // 9.99999
11986                output_prec: 6,
11987                output_scale: 5,
11988                expected_output_repr: Ok(999999), // 9.99999
11989            },
11990            // decrease precision, same scale, unsafe
11991            DecimalCastTestConfig {
11992                input_prec: 10,
11993                input_scale: 5,
11994                input_repr: 9999999, // 99.99999
11995                output_prec: 6,
11996                output_scale: 5,
11997                expected_output_repr: Err("Invalid argument error: 99.99999 is too large to store in a {} of precision 6. Max is 9.99999".to_string()) // max is 9.99999
11998            },
11999            // same precision, increase scale, safe
12000            DecimalCastTestConfig {
12001                input_prec: 7,
12002                input_scale: 4,
12003                input_repr: 12345, // 1.2345
12004                output_prec: 7,
12005                output_scale: 6,
12006                expected_output_repr: Ok(1234500), // 1.234500
12007            },
12008            // same precision, increase scale, unsafe
12009            DecimalCastTestConfig {
12010                input_prec: 7,
12011                input_scale: 4,
12012                input_repr: 123456, // 12.3456
12013                output_prec: 7,
12014                output_scale: 6,
12015                expected_output_repr: Err("Invalid argument error: 12.345600 is too large to store in a {} of precision 7. Max is 9.999999".to_string()) // max is 9.99999
12016            },
12017            // same precision, decrease scale, infallible
12018            DecimalCastTestConfig {
12019                input_prec: 7,
12020                input_scale: 5,
12021                input_repr: 1234567, // 12.34567
12022                output_prec: 7,
12023                output_scale: 4,
12024                expected_output_repr: Ok(123457), // 12.3457
12025            },
12026            // same precision, same scale, infallible
12027            DecimalCastTestConfig {
12028                input_prec: 7,
12029                input_scale: 5,
12030                input_repr: 9999999, // 99.99999
12031                output_prec: 7,
12032                output_scale: 5,
12033                expected_output_repr: Ok(9999999), // 99.99999
12034            },
12035            // precision increase, input scale & output scale = 0, infallible
12036            DecimalCastTestConfig {
12037                input_prec: 7,
12038                input_scale: 0,
12039                input_repr: 1234567, // 1234567
12040                output_prec: 8,
12041                output_scale: 0,
12042                expected_output_repr: Ok(1234567), // 1234567
12043            },
12044            // precision decrease, input scale & output scale = 0, failure
12045            DecimalCastTestConfig {
12046                input_prec: 7,
12047                input_scale: 0,
12048                input_repr: 1234567, // 1234567
12049                output_prec: 6,
12050                output_scale: 0,
12051                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12052            },
12053            // precision decrease, input scale & output scale = 0, success
12054            DecimalCastTestConfig {
12055                input_prec: 7,
12056                input_scale: 0,
12057                input_repr: 123456, // 123456
12058                output_prec: 6,
12059                output_scale: 0,
12060                expected_output_repr: Ok(123456), // 123456
12061            },
12062        ];
12063
12064        for t in test_cases {
12065            run_decimal_cast_test_case_between_multiple_types(t);
12066        }
12067    }
12068
12069    #[test]
12070    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12071        let test_cases = [
12072            DecimalCastTestConfig {
12073                input_prec: 5,
12074                input_scale: 0,
12075                input_repr: 99999,
12076                output_prec: 10,
12077                output_scale: 5,
12078                expected_output_repr: Ok(9999900000),
12079            },
12080            DecimalCastTestConfig {
12081                input_prec: 5,
12082                input_scale: 0,
12083                input_repr: -99999,
12084                output_prec: 10,
12085                output_scale: 5,
12086                expected_output_repr: Ok(-9999900000),
12087            },
12088            DecimalCastTestConfig {
12089                input_prec: 5,
12090                input_scale: 2,
12091                input_repr: 99999,
12092                output_prec: 10,
12093                output_scale: 5,
12094                expected_output_repr: Ok(99999000),
12095            },
12096            DecimalCastTestConfig {
12097                input_prec: 5,
12098                input_scale: -2,
12099                input_repr: -99999,
12100                output_prec: 10,
12101                output_scale: 3,
12102                expected_output_repr: Ok(-9999900000),
12103            },
12104            DecimalCastTestConfig {
12105                input_prec: 5,
12106                input_scale: 3,
12107                input_repr: -12345,
12108                output_prec: 6,
12109                output_scale: 5,
12110                expected_output_repr: Err("Invalid argument error: -12.34500 is too small to store in a {} of precision 6. Min is -9.99999".to_string())
12111            },
12112        ];
12113
12114        for t in test_cases {
12115            run_decimal_cast_test_case_between_multiple_types(t);
12116        }
12117    }
12118
12119    #[test]
12120    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12121        let test_cases = [
12122            DecimalCastTestConfig {
12123                input_prec: 5,
12124                input_scale: 0,
12125                input_repr: 99999,
12126                output_scale: -3,
12127                output_prec: 3,
12128                expected_output_repr: Ok(100),
12129            },
12130            DecimalCastTestConfig {
12131                input_prec: 5,
12132                input_scale: 0,
12133                input_repr: -99999,
12134                output_prec: 1,
12135                output_scale: -5,
12136                expected_output_repr: Ok(-1),
12137            },
12138            DecimalCastTestConfig {
12139                input_prec: 10,
12140                input_scale: 2,
12141                input_repr: 123456789,
12142                output_prec: 5,
12143                output_scale: -2,
12144                expected_output_repr: Ok(12346),
12145            },
12146            DecimalCastTestConfig {
12147                input_prec: 10,
12148                input_scale: 4,
12149                input_repr: -9876543210,
12150                output_prec: 7,
12151                output_scale: 0,
12152                expected_output_repr: Ok(-987654),
12153            },
12154            DecimalCastTestConfig {
12155                input_prec: 7,
12156                input_scale: 4,
12157                input_repr: 9999999,
12158                output_prec: 6,
12159                output_scale: 3,
12160                expected_output_repr:
12161                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12162            },
12163        ];
12164        for t in test_cases {
12165            run_decimal_cast_test_case_between_multiple_types(t);
12166        }
12167    }
12168
12169    #[test]
12170    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12171        let array = vec![Some(123456789)];
12172        let array = create_decimal128_array(array, 24, 2).unwrap();
12173        let input_type = DataType::Decimal128(24, 2);
12174        let output_type = DataType::Decimal128(6, 2);
12175        assert!(can_cast_types(&input_type, &output_type));
12176
12177        let options = CastOptions {
12178            safe: false,
12179            ..Default::default()
12180        };
12181        let result = cast_with_options(&array, &output_type, &options);
12182        assert_eq!(
12183            result.unwrap_err().to_string(),
12184            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12185        );
12186    }
12187
12188    #[test]
12189    fn test_decimal_to_decimal_same_scale() {
12190        let array = vec![Some(520)];
12191        let array = create_decimal128_array(array, 4, 2).unwrap();
12192        let input_type = DataType::Decimal128(4, 2);
12193        let output_type = DataType::Decimal128(3, 2);
12194        assert!(can_cast_types(&input_type, &output_type));
12195
12196        let options = CastOptions {
12197            safe: false,
12198            ..Default::default()
12199        };
12200        let result = cast_with_options(&array, &output_type, &options);
12201        assert_eq!(
12202            result.unwrap().as_primitive::<Decimal128Type>().value(0),
12203            520
12204        );
12205
12206        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
12207        assert_eq!(
12208            &cast(
12209                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12210                &DataType::Decimal128(2, 0)
12211            )
12212            .unwrap(),
12213            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
12214        );
12215    }
12216
12217    #[test]
12218    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
12219        let array = vec![Some(123456789)];
12220        let array = create_decimal128_array(array, 24, 4).unwrap();
12221        let input_type = DataType::Decimal128(24, 4);
12222        let output_type = DataType::Decimal128(6, 2);
12223        assert!(can_cast_types(&input_type, &output_type));
12224
12225        let options = CastOptions {
12226            safe: false,
12227            ..Default::default()
12228        };
12229        let result = cast_with_options(&array, &output_type, &options);
12230        assert_eq!(
12231            result.unwrap_err().to_string(),
12232            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12233        );
12234    }
12235
12236    #[test]
12237    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
12238        let array = vec![Some(123456789)];
12239        let array = create_decimal128_array(array, 24, 2).unwrap();
12240        let input_type = DataType::Decimal128(24, 2);
12241        let output_type = DataType::Decimal128(6, 3);
12242        assert!(can_cast_types(&input_type, &output_type));
12243
12244        let options = CastOptions {
12245            safe: false,
12246            ..Default::default()
12247        };
12248        let result = cast_with_options(&array, &output_type, &options);
12249        assert_eq!(
12250            result.unwrap_err().to_string(),
12251            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
12252        );
12253    }
12254
12255    #[test]
12256    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
12257        let array = vec![Some(123456789)];
12258        let array = create_decimal128_array(array, 24, 2).unwrap();
12259        let input_type = DataType::Decimal128(24, 2);
12260        let output_type = DataType::Decimal256(6, 2);
12261        assert!(can_cast_types(&input_type, &output_type));
12262
12263        let options = CastOptions {
12264            safe: false,
12265            ..Default::default()
12266        };
12267        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
12268        assert_eq!(
12269            result.to_string(),
12270            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
12271        );
12272    }
12273
12274    #[test]
12275    fn test_first_none() {
12276        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12277            None,
12278            Some(vec![Some(1), Some(2)]),
12279        ])) as ArrayRef;
12280        let data_type =
12281            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12282        let opt = CastOptions::default();
12283        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12284
12285        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12286            vec![None, Some(vec![Some(1), Some(2)])],
12287            2,
12288        )) as ArrayRef;
12289        assert_eq!(*fixed_array, *r);
12290    }
12291
12292    #[test]
12293    fn test_first_last_none() {
12294        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12295            None,
12296            Some(vec![Some(1), Some(2)]),
12297            None,
12298        ])) as ArrayRef;
12299        let data_type =
12300            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12301        let opt = CastOptions::default();
12302        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12303
12304        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12305            vec![None, Some(vec![Some(1), Some(2)]), None],
12306            2,
12307        )) as ArrayRef;
12308        assert_eq!(*fixed_array, *r);
12309    }
12310
12311    #[test]
12312    fn test_cast_decimal_error_output() {
12313        let array = Int64Array::from(vec![1]);
12314        let error = cast_with_options(
12315            &array,
12316            &DataType::Decimal32(1, 1),
12317            &CastOptions {
12318                safe: false,
12319                format_options: FormatOptions::default(),
12320            },
12321        )
12322        .unwrap_err();
12323        assert_eq!(
12324            error.to_string(),
12325            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
12326        );
12327
12328        let array = Int64Array::from(vec![-1]);
12329        let error = cast_with_options(
12330            &array,
12331            &DataType::Decimal32(1, 1),
12332            &CastOptions {
12333                safe: false,
12334                format_options: FormatOptions::default(),
12335            },
12336        )
12337        .unwrap_err();
12338        assert_eq!(
12339            error.to_string(),
12340            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
12341        );
12342    }
12343
12344    #[test]
12345    fn test_run_end_encoded_to_primitive() {
12346        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
12347        let run_ends = Int32Array::from(vec![2, 5, 6]);
12348        let values = Int32Array::from(vec![1, 2, 3]);
12349        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12350        let array_ref = Arc::new(run_array) as ArrayRef;
12351        // Cast to Int64
12352        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12353        // Verify the result is a RunArray with Int64 values
12354        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
12355        assert_eq!(
12356            result_run_array.values(),
12357            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
12358        );
12359    }
12360
12361    #[test]
12362    fn test_sliced_run_end_encoded_to_primitive() {
12363        let run_ends = Int32Array::from(vec![2, 5, 6]);
12364        let values = Int32Array::from(vec![1, 2, 3]);
12365        // [1, 1, 2, 2, 2, 3]
12366        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12367        let run_array = run_array.slice(3, 3); // [2, 2, 3]
12368        let array_ref = Arc::new(run_array) as ArrayRef;
12369
12370        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12371        let result_run_array = cast_result.as_primitive::<Int64Type>();
12372        assert_eq!(result_run_array.values(), &[2, 2, 3]);
12373    }
12374
12375    #[test]
12376    fn test_run_end_encoded_to_string() {
12377        let run_ends = Int32Array::from(vec![2, 3, 5]);
12378        let values = Int32Array::from(vec![10, 20, 30]);
12379        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12380        let array_ref = Arc::new(run_array) as ArrayRef;
12381
12382        // Cast to String
12383        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12384
12385        // Verify the result is a RunArray with String values
12386        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12387        // Check that values are correct
12388        assert_eq!(result_array.value(0), "10");
12389        assert_eq!(result_array.value(1), "10");
12390        assert_eq!(result_array.value(2), "20");
12391    }
12392
12393    #[test]
12394    fn test_primitive_to_run_end_encoded() {
12395        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
12396        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
12397        let array_ref = Arc::new(source_array) as ArrayRef;
12398
12399        // Cast to RunEndEncoded<Int32, Int32>
12400        let target_type = DataType::RunEndEncoded(
12401            Arc::new(Field::new(
12402                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12403                DataType::Int32,
12404                false,
12405            )),
12406            Arc::new(Field::new(
12407                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12408                DataType::Int32,
12409                true,
12410            )),
12411        );
12412        let cast_result = cast(&array_ref, &target_type).unwrap();
12413
12414        // Verify the result is a RunArray
12415        let result_run_array = cast_result
12416            .as_any()
12417            .downcast_ref::<RunArray<Int32Type>>()
12418            .unwrap();
12419
12420        // Check run structure: runs should end at positions [2, 5, 6]
12421        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
12422
12423        // Check values: should be [1, 2, 3]
12424        let values_array = result_run_array.values().as_primitive::<Int32Type>();
12425        assert_eq!(values_array.values(), &[1, 2, 3]);
12426    }
12427
12428    #[test]
12429    fn test_primitive_to_run_end_encoded_with_nulls() {
12430        let source_array = Int32Array::from(vec![
12431            Some(1),
12432            Some(1),
12433            None,
12434            None,
12435            Some(2),
12436            Some(2),
12437            Some(3),
12438            Some(3),
12439            None,
12440            None,
12441            Some(4),
12442            Some(4),
12443            Some(5),
12444            Some(5),
12445            None,
12446            None,
12447        ]);
12448        let array_ref = Arc::new(source_array) as ArrayRef;
12449        let target_type = DataType::RunEndEncoded(
12450            Arc::new(Field::new(
12451                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12452                DataType::Int32,
12453                false,
12454            )),
12455            Arc::new(Field::new(
12456                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12457                DataType::Int32,
12458                true,
12459            )),
12460        );
12461        let cast_result = cast(&array_ref, &target_type).unwrap();
12462        let result_run_array = cast_result
12463            .as_any()
12464            .downcast_ref::<RunArray<Int32Type>>()
12465            .unwrap();
12466        assert_eq!(
12467            result_run_array.run_ends().values(),
12468            &[2, 4, 6, 8, 10, 12, 14, 16]
12469        );
12470        assert_eq!(
12471            result_run_array
12472                .values()
12473                .as_primitive::<Int32Type>()
12474                .values(),
12475            &[1, 0, 2, 3, 0, 4, 5, 0]
12476        );
12477        assert_eq!(result_run_array.values().null_count(), 3);
12478    }
12479
12480    #[test]
12481    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
12482        let source_array = Int64Array::from(vec![
12483            Some(1),
12484            Some(1),
12485            None,
12486            None,
12487            None,
12488            None,
12489            None,
12490            None,
12491            None,
12492            None,
12493            Some(4),
12494            Some(20),
12495            Some(500),
12496            Some(500),
12497            None,
12498            None,
12499        ]);
12500        let array_ref = Arc::new(source_array) as ArrayRef;
12501        let target_type = DataType::RunEndEncoded(
12502            Arc::new(Field::new(
12503                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12504                DataType::Int16,
12505                false,
12506            )),
12507            Arc::new(Field::new(
12508                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12509                DataType::Int64,
12510                true,
12511            )),
12512        );
12513        let cast_result = cast(&array_ref, &target_type).unwrap();
12514        let result_run_array = cast_result
12515            .as_any()
12516            .downcast_ref::<RunArray<Int16Type>>()
12517            .unwrap();
12518        assert_eq!(
12519            result_run_array.run_ends().values(),
12520            &[2, 10, 11, 12, 14, 16]
12521        );
12522        assert_eq!(
12523            result_run_array
12524                .values()
12525                .as_primitive::<Int64Type>()
12526                .values(),
12527            &[1, 0, 4, 20, 500, 0]
12528        );
12529        assert_eq!(result_run_array.values().null_count(), 2);
12530    }
12531
12532    #[test]
12533    fn test_string_to_run_end_encoded() {
12534        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
12535        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
12536        let array_ref = Arc::new(source_array) as ArrayRef;
12537
12538        // Cast to RunEndEncoded<Int32, String>
12539        let target_type = DataType::RunEndEncoded(
12540            Arc::new(Field::new(
12541                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12542                DataType::Int32,
12543                false,
12544            )),
12545            Arc::new(Field::new(
12546                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12547                DataType::Utf8,
12548                true,
12549            )),
12550        );
12551        let cast_result = cast(&array_ref, &target_type).unwrap();
12552
12553        // Verify the result is a RunArray
12554        let result_run_array = cast_result
12555            .as_any()
12556            .downcast_ref::<RunArray<Int32Type>>()
12557            .unwrap();
12558
12559        // Check run structure: runs should end at positions [2, 3, 5]
12560        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
12561
12562        // Check values: should be ["a", "b", "c"]
12563        let values_array = result_run_array.values().as_string::<i32>();
12564        assert_eq!(values_array.value(0), "a");
12565        assert_eq!(values_array.value(1), "b");
12566        assert_eq!(values_array.value(2), "c");
12567    }
12568
12569    #[test]
12570    fn test_empty_array_to_run_end_encoded() {
12571        // Create an empty Int32 array
12572        let source_array = Int32Array::from(Vec::<i32>::new());
12573        let array_ref = Arc::new(source_array) as ArrayRef;
12574
12575        // Cast to RunEndEncoded<Int32, Int32>
12576        let target_type = DataType::RunEndEncoded(
12577            Arc::new(Field::new(
12578                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12579                DataType::Int32,
12580                false,
12581            )),
12582            Arc::new(Field::new(
12583                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12584                DataType::Int32,
12585                true,
12586            )),
12587        );
12588        let cast_result = cast(&array_ref, &target_type).unwrap();
12589
12590        // Verify the result is an empty RunArray
12591        let result_run_array = cast_result
12592            .as_any()
12593            .downcast_ref::<RunArray<Int32Type>>()
12594            .unwrap();
12595
12596        // Check that both run_ends and values are empty
12597        assert_eq!(result_run_array.run_ends().len(), 0);
12598        assert_eq!(result_run_array.values().len(), 0);
12599    }
12600
12601    #[test]
12602    fn test_run_end_encoded_with_nulls() {
12603        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
12604        let run_ends = Int32Array::from(vec![2, 3, 5]);
12605        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
12606        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12607        let array_ref = Arc::new(run_array) as ArrayRef;
12608
12609        // Cast to String
12610        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12611
12612        // Verify the result preserves nulls
12613        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12614        assert_eq!(result_run_array.value(0), "1");
12615        assert!(result_run_array.is_null(2));
12616        assert_eq!(result_run_array.value(4), "2");
12617    }
12618
12619    #[test]
12620    fn test_different_index_types() {
12621        // Test with Int16 index type
12622        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
12623        let array_ref = Arc::new(source_array) as ArrayRef;
12624
12625        let target_type = DataType::RunEndEncoded(
12626            Arc::new(Field::new(
12627                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12628                DataType::Int16,
12629                false,
12630            )),
12631            Arc::new(Field::new(
12632                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12633                DataType::Int32,
12634                true,
12635            )),
12636        );
12637        let cast_result = cast(&array_ref, &target_type).unwrap();
12638        assert_eq!(cast_result.data_type(), &target_type);
12639
12640        // Verify the cast worked correctly: values are [1, 2, 3]
12641        // and run-ends are [2, 3, 5]
12642        let run_array = cast_result
12643            .as_any()
12644            .downcast_ref::<RunArray<Int16Type>>()
12645            .unwrap();
12646        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12647        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12648        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12649        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
12650
12651        // Test again with Int64 index type
12652        let target_type = DataType::RunEndEncoded(
12653            Arc::new(Field::new(
12654                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12655                DataType::Int64,
12656                false,
12657            )),
12658            Arc::new(Field::new(
12659                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12660                DataType::Int32,
12661                true,
12662            )),
12663        );
12664        let cast_result = cast(&array_ref, &target_type).unwrap();
12665        assert_eq!(cast_result.data_type(), &target_type);
12666
12667        // Verify the cast worked correctly: values are [1, 2, 3]
12668        // and run-ends are [2, 3, 5]
12669        let run_array = cast_result
12670            .as_any()
12671            .downcast_ref::<RunArray<Int64Type>>()
12672            .unwrap();
12673        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12674        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12675        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12676        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
12677    }
12678
12679    #[test]
12680    fn test_unsupported_cast_to_run_end_encoded() {
12681        // Create a Struct array - complex nested type that might not be supported
12682        let field = Field::new("item", DataType::Int32, false);
12683        let struct_array = StructArray::from(vec![(
12684            Arc::new(field),
12685            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
12686        )]);
12687        let array_ref = Arc::new(struct_array) as ArrayRef;
12688
12689        // This should fail because:
12690        // 1. The target type is not RunEndEncoded
12691        // 2. The target type is not supported for casting from StructArray
12692        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
12693
12694        // Expect this to fail
12695        assert!(cast_result.is_err());
12696    }
12697
12698    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
12699    #[test]
12700    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
12701        // Construct a valid REE array with Int64 run-ends
12702        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12703        let values = StringArray::from(vec!["a", "b", "c"]);
12704
12705        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12706        let array_ref = Arc::new(ree_array) as ArrayRef;
12707
12708        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12709        let target_type = DataType::RunEndEncoded(
12710            Arc::new(Field::new(
12711                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12712                DataType::Int16,
12713                false,
12714            )),
12715            Arc::new(Field::new(
12716                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12717                DataType::Utf8,
12718                true,
12719            )),
12720        );
12721        let cast_options = CastOptions {
12722            safe: false, // This should make it fail instead of returning nulls
12723            format_options: FormatOptions::default(),
12724        };
12725
12726        // This should fail due to run-end overflow
12727        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12728            cast_with_options(&array_ref, &target_type, &cast_options);
12729
12730        let e = result.expect_err("Cast should have failed but succeeded");
12731        assert!(
12732            e.to_string()
12733                .contains("Cast error: Can't cast value 100000 to type Int16")
12734        );
12735    }
12736
12737    #[test]
12738    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
12739        // Construct a valid REE array with Int64 run-ends
12740        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12741        let values = StringArray::from(vec!["a", "b", "c"]);
12742
12743        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12744        let array_ref = Arc::new(ree_array) as ArrayRef;
12745
12746        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12747        let target_type = DataType::RunEndEncoded(
12748            Arc::new(Field::new(
12749                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12750                DataType::Int16,
12751                false,
12752            )),
12753            Arc::new(Field::new(
12754                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12755                DataType::Utf8,
12756                true,
12757            )),
12758        );
12759        let cast_options = CastOptions {
12760            safe: true,
12761            format_options: FormatOptions::default(),
12762        };
12763
12764        // This fails even though safe is true because the run_ends array has null values
12765        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12766            cast_with_options(&array_ref, &target_type, &cast_options);
12767        let e = result.expect_err("Cast should have failed but succeeded");
12768        assert!(
12769            e.to_string()
12770                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
12771        );
12772    }
12773
12774    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
12775    #[test]
12776    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
12777        // Construct a valid REE array with Int16 run-ends
12778        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
12779        let values = StringArray::from(vec!["a", "b", "c"]);
12780
12781        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12782        let array_ref = Arc::new(ree_array) as ArrayRef;
12783
12784        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
12785        let target_type = DataType::RunEndEncoded(
12786            Arc::new(Field::new(
12787                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12788                DataType::Int64,
12789                false,
12790            )),
12791            Arc::new(Field::new(
12792                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12793                DataType::Utf8,
12794                true,
12795            )),
12796        );
12797        let cast_options = CastOptions {
12798            safe: false,
12799            format_options: FormatOptions::default(),
12800        };
12801
12802        // This should succeed due to valid upcast
12803        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12804            cast_with_options(&array_ref, &target_type, &cast_options);
12805
12806        let array_ref = result.expect("Cast should have succeeded but failed");
12807        // Downcast to RunArray<Int64Type>
12808        let run_array = array_ref
12809            .as_any()
12810            .downcast_ref::<RunArray<Int64Type>>()
12811            .unwrap();
12812
12813        // Verify the cast worked correctly
12814        // Assert the values were cast correctly
12815        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
12816        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
12817        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
12818        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12819    }
12820
12821    #[test]
12822    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
12823        // Construct a valid dictionary encoded array
12824        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
12825        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
12826        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
12827
12828        // Attempt to cast to RunEndEncoded<Int64, Utf8>
12829        let target_type = DataType::RunEndEncoded(
12830            Arc::new(Field::new(
12831                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
12832                DataType::Int64,
12833                false,
12834            )),
12835            Arc::new(Field::new(
12836                Field::REE_VALUES_FIELD_DEFAULT_NAME,
12837                DataType::Utf8,
12838                true,
12839            )),
12840        );
12841        let cast_options = CastOptions {
12842            safe: false,
12843            format_options: FormatOptions::default(),
12844        };
12845
12846        // This should succeed
12847        let result = cast_with_options(&array_ref, &target_type, &cast_options)
12848            .expect("Cast should have succeeded but failed");
12849
12850        // Verify the cast worked correctly
12851        // Assert the values were cast correctly
12852        let run_array = result
12853            .as_any()
12854            .downcast_ref::<RunArray<Int64Type>>()
12855            .unwrap();
12856        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
12857        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
12858        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12859
12860        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
12861        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
12862    }
12863
12864    #[test]
12865    fn test_cast_time32_second_to_int64() {
12866        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
12867        let array = Arc::new(array) as Arc<dyn Array>;
12868        let to_type = DataType::Int64;
12869        let cast_options = CastOptions::default();
12870
12871        assert!(can_cast_types(array.data_type(), &to_type));
12872
12873        let result = cast_with_options(&array, &to_type, &cast_options);
12874        assert!(
12875            result.is_ok(),
12876            "Failed to cast Time32(Second) to Int64: {:?}",
12877            result.err()
12878        );
12879
12880        let cast_array = result.unwrap();
12881        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12882
12883        assert_eq!(cast_array.value(0), 1000);
12884        assert_eq!(cast_array.value(1), 2000);
12885        assert_eq!(cast_array.value(2), 3000);
12886    }
12887
12888    #[test]
12889    fn test_cast_time32_millisecond_to_int64() {
12890        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
12891        let array = Arc::new(array) as Arc<dyn Array>;
12892        let to_type = DataType::Int64;
12893        let cast_options = CastOptions::default();
12894
12895        assert!(can_cast_types(array.data_type(), &to_type));
12896
12897        let result = cast_with_options(&array, &to_type, &cast_options);
12898        assert!(
12899            result.is_ok(),
12900            "Failed to cast Time32(Millisecond) to Int64: {:?}",
12901            result.err()
12902        );
12903
12904        let cast_array = result.unwrap();
12905        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12906
12907        assert_eq!(cast_array.value(0), 1000);
12908        assert_eq!(cast_array.value(1), 2000);
12909        assert_eq!(cast_array.value(2), 3000);
12910    }
12911
12912    #[test]
12913    fn test_cast_time32_millisecond_to_time64_nanosecond() {
12914        let array =
12915            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
12916        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
12917        let c = b.as_primitive::<Time64NanosecondType>();
12918        assert_eq!(c.value(0), 1_000_000_000);
12919        assert_eq!(c.value(1), 2_000_000_000);
12920        assert!(c.is_null(2));
12921        assert_eq!(c.value(3), 43_200_000_000_000);
12922    }
12923
12924    #[test]
12925    fn test_cast_time32_millisecond_to_time64_microsecond() {
12926        let array =
12927            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
12928        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
12929        let c = b.as_primitive::<Time64MicrosecondType>();
12930        assert_eq!(c.value(0), 1_000_000);
12931        assert_eq!(c.value(1), 2_000_000);
12932        assert!(c.is_null(2));
12933        assert_eq!(c.value(3), 43_200_000_000);
12934    }
12935
12936    #[test]
12937    fn test_cast_time32_second_to_time64_nanosecond() {
12938        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
12939        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
12940        let c = b.as_primitive::<Time64NanosecondType>();
12941        assert_eq!(c.value(0), 1_000_000_000);
12942        assert_eq!(c.value(1), 60_000_000_000);
12943        assert!(c.is_null(2));
12944        assert_eq!(c.value(3), 43_200_000_000_000);
12945    }
12946
12947    #[test]
12948    fn test_cast_time32_second_to_time64_microsecond() {
12949        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
12950        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
12951        let c = b.as_primitive::<Time64MicrosecondType>();
12952        assert_eq!(c.value(0), 1_000_000);
12953        assert_eq!(c.value(1), 60_000_000);
12954        assert!(c.is_null(2));
12955        assert_eq!(c.value(3), 43_200_000_000);
12956    }
12957
12958    #[test]
12959    fn test_cast_time32_second_to_time32_millisecond_overflow() {
12960        let array = Time32SecondArray::from(vec![i32::MAX]);
12961
12962        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
12963        let c = b.as_primitive::<Time32MillisecondType>();
12964        assert!(c.is_null(0));
12965
12966        let options = CastOptions {
12967            safe: false,
12968            ..Default::default()
12969        };
12970        let err = cast_with_options(&array, &DataType::Time32(TimeUnit::Millisecond), &options)
12971            .unwrap_err();
12972        assert!(err.to_string().contains("Overflow"), "{err}");
12973    }
12974
12975    fn assert_temporal_overflow_is_safe(array: &dyn Array, to_type: &DataType) {
12976        let result = cast(array, to_type).unwrap();
12977        assert_eq!(result.null_count(), array.len());
12978
12979        let options = CastOptions {
12980            safe: false,
12981            ..Default::default()
12982        };
12983        assert!(cast_with_options(array, to_type, &options).is_err());
12984    }
12985
12986    #[test]
12987    fn test_cast_time64_overflow() {
12988        let microseconds = Time64MicrosecondArray::from(vec![i64::MIN, i64::MAX]);
12989        for to_type in [
12990            DataType::Time32(TimeUnit::Second),
12991            DataType::Time32(TimeUnit::Millisecond),
12992            DataType::Time64(TimeUnit::Nanosecond),
12993        ] {
12994            assert_temporal_overflow_is_safe(&microseconds, &to_type);
12995        }
12996
12997        let nanoseconds = Time64NanosecondArray::from(vec![i64::MIN, i64::MAX]);
12998        for to_type in [
12999            DataType::Time32(TimeUnit::Second),
13000            DataType::Time32(TimeUnit::Millisecond),
13001        ] {
13002            assert_temporal_overflow_is_safe(&nanoseconds, &to_type);
13003        }
13004    }
13005
13006    #[test]
13007    fn test_cast_date64_to_timestamp_overflow() {
13008        let array = Date64Array::from(vec![i64::MIN, i64::MAX]);
13009        for to_type in [
13010            DataType::Timestamp(TimeUnit::Microsecond, None),
13011            DataType::Timestamp(TimeUnit::Nanosecond, None),
13012        ] {
13013            assert_temporal_overflow_is_safe(&array, &to_type);
13014        }
13015    }
13016
13017    #[test]
13018    fn test_cast_time64_to_time32_boundaries() {
13019        fn check<FROM, TO>(divisor: i64)
13020        where
13021            FROM: ArrowPrimitiveType<Native = i64>,
13022            TO: ArrowPrimitiveType<Native = i32>,
13023        {
13024            // Division truncates towards zero before the range check. Values
13025            // slightly outside the scaled i32 bounds can still be representable.
13026            let min = <i64 as From<i32>>::from(i32::MIN) * divisor - (divisor - 1);
13027            let max = <i64 as From<i32>>::from(i32::MAX) * divisor + (divisor - 1);
13028            let array = PrimitiveArray::<FROM>::new(
13029                vec![i64::MAX, min - 1, min, -divisor + 1, i64::MAX, max, max + 1].into(),
13030                Some(vec![true, true, true, true, false, true, true].into()),
13031            )
13032            .slice(1, 6);
13033            let expected = PrimitiveArray::<TO>::from_iter([
13034                None,
13035                Some(i32::MIN),
13036                Some(0),
13037                None,
13038                Some(i32::MAX),
13039                None,
13040            ]);
13041            let result = cast(&array, &TO::DATA_TYPE).unwrap();
13042            assert_eq!(result.as_primitive::<TO>(), &expected);
13043
13044            let options = CastOptions {
13045                safe: false,
13046                ..Default::default()
13047            };
13048            assert!(cast_with_options(&array, &TO::DATA_TYPE, &options).is_err());
13049            // The invalid physical value under the null must not cause an error.
13050            let result = cast_with_options(&array.slice(1, 4), &TO::DATA_TYPE, &options).unwrap();
13051            assert_eq!(result.as_primitive::<TO>(), &expected.slice(1, 4));
13052        }
13053        check::<Time64MicrosecondType, Time32SecondType>(MICROSECONDS);
13054        check::<Time64MicrosecondType, Time32MillisecondType>(MICROSECONDS / MILLISECONDS);
13055        check::<Time64NanosecondType, Time32SecondType>(NANOSECONDS);
13056        check::<Time64NanosecondType, Time32MillisecondType>(NANOSECONDS / MILLISECONDS);
13057    }
13058
13059    #[test]
13060    fn test_cast_temporal_scaling_boundaries() {
13061        fn check<FROM, TO>(multiplier: i64)
13062        where
13063            FROM: ArrowPrimitiveType<Native = i64>,
13064            TO: ArrowPrimitiveType<Native = i64>,
13065        {
13066            let min = i64::MIN / multiplier;
13067            let max = i64::MAX / multiplier;
13068            let array = PrimitiveArray::<FROM>::new(
13069                vec![i64::MAX, min - 1, min, -1, i64::MAX, max, max + 1].into(),
13070                Some(vec![true, true, true, true, false, true, true].into()),
13071            )
13072            .slice(1, 6);
13073            let expected = PrimitiveArray::<TO>::from_iter([
13074                None,
13075                Some(min * multiplier),
13076                Some(-multiplier),
13077                None,
13078                Some(max * multiplier),
13079                None,
13080            ]);
13081            let result = cast(&array, &TO::DATA_TYPE).unwrap();
13082            assert_eq!(result.as_primitive::<TO>(), &expected);
13083            let options = CastOptions {
13084                safe: false,
13085                ..Default::default()
13086            };
13087            assert!(cast_with_options(&array, &TO::DATA_TYPE, &options).is_err());
13088            let result = cast_with_options(&array.slice(1, 4), &TO::DATA_TYPE, &options).unwrap();
13089            assert_eq!(result.as_primitive::<TO>(), &expected.slice(1, 4));
13090        }
13091        check::<Time64MicrosecondType, Time64NanosecondType>(NANOSECONDS / MICROSECONDS);
13092        check::<Date64Type, TimestampMicrosecondType>(MICROSECONDS / MILLISECONDS);
13093        check::<Date64Type, TimestampNanosecondType>(NANOSECONDS / MILLISECONDS);
13094    }
13095
13096    #[test]
13097    fn test_cast_string_to_time32_second_to_int64() {
13098        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
13099        // raised in https://github.com/apache/datafusion/issues/19036
13100        let array = StringArray::from(vec!["03:12:44"]);
13101        let array = Arc::new(array) as Arc<dyn Array>;
13102        let cast_options = CastOptions::default();
13103
13104        // 1. Cast String to Time32(Second)
13105        let time32_type = DataType::Time32(TimeUnit::Second);
13106        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
13107
13108        // 2. Cast Time32(Second) to Int64
13109        let int64_type = DataType::Int64;
13110        assert!(can_cast_types(time32_array.data_type(), &int64_type));
13111
13112        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
13113
13114        assert!(
13115            result.is_ok(),
13116            "Failed to cast Time32(Second) to Int64: {:?}",
13117            result.err()
13118        );
13119
13120        let cast_array = result.unwrap();
13121        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13122
13123        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
13124        assert_eq!(cast_array.value(0), 11564);
13125    }
13126    #[test]
13127    fn test_string_dicts_to_binary_view() {
13128        let expected = BinaryViewArray::from_iter(vec![
13129            VIEW_TEST_DATA[1],
13130            VIEW_TEST_DATA[0],
13131            None,
13132            VIEW_TEST_DATA[3],
13133            None,
13134            VIEW_TEST_DATA[1],
13135            VIEW_TEST_DATA[4],
13136        ]);
13137
13138        let values_arrays: [ArrayRef; _] = [
13139            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
13140            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
13141            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
13142        ];
13143        for values in values_arrays {
13144            let keys =
13145                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13146            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13147
13148            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
13149            assert_eq!(casted.as_ref(), &expected);
13150        }
13151    }
13152
13153    #[test]
13154    fn test_binary_dicts_to_string_view() {
13155        let expected = StringViewArray::from_iter(vec![
13156            VIEW_TEST_DATA[1],
13157            VIEW_TEST_DATA[0],
13158            None,
13159            VIEW_TEST_DATA[3],
13160            None,
13161            VIEW_TEST_DATA[1],
13162            VIEW_TEST_DATA[4],
13163        ]);
13164
13165        let values_arrays: [ArrayRef; _] = [
13166            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
13167            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
13168            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
13169        ];
13170        for values in values_arrays {
13171            let keys =
13172                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13173            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13174
13175            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
13176            assert_eq!(casted.as_ref(), &expected);
13177        }
13178    }
13179
13180    #[test]
13181    fn test_cast_between_sliced_run_end_encoded() {
13182        let run_ends = Int16Array::from(vec![2, 5, 8]);
13183        let values = StringArray::from(vec!["a", "b", "c"]);
13184
13185        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13186        let ree_array = ree_array.slice(1, 2);
13187        let array_ref = Arc::new(ree_array) as ArrayRef;
13188
13189        let target_type = DataType::RunEndEncoded(
13190            Arc::new(Field::new(
13191                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
13192                DataType::Int64,
13193                false,
13194            )),
13195            Arc::new(Field::new(
13196                Field::REE_VALUES_FIELD_DEFAULT_NAME,
13197                DataType::Utf8,
13198                true,
13199            )),
13200        );
13201        let cast_options = CastOptions {
13202            safe: false,
13203            format_options: FormatOptions::default(),
13204        };
13205
13206        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
13207        let run_array = result.as_run::<Int64Type>();
13208        let run_array = run_array.downcast::<StringArray>().unwrap();
13209
13210        let expected = vec!["a", "b"];
13211        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
13212
13213        assert_eq!(expected, actual);
13214    }
13215
13216    fn decimal128(unscaled: i128, precision: u8, scale: i8) -> Decimal128Array {
13217        Decimal128Array::from(vec![unscaled])
13218            .with_precision_and_scale(precision, scale)
13219            .unwrap()
13220    }
13221
13222    #[test]
13223    fn test_cast_decimal_to_float_is_correctly_rounded() {
13224        // (unscaled, scale, nearest double). The last four already passed.
13225        for (unscaled, scale, expected) in [
13226            (12345678901234567890i128, 2i8, 1.2345678901234568e17f64),
13227            (10i128.pow(37), 37, 1.0),
13228            (123456789012345678901, 20, 1.2345678901234567),
13229            (1, 37, 1e-37),
13230            (1, 1, 0.1),
13231            (15, 1, 1.5),
13232            (123456, 3, 123.456),
13233            (-123456, 3, -123.456),
13234        ] {
13235            let out = cast(&decimal128(unscaled, 38, scale), &DataType::Float64).unwrap();
13236            assert_eq!(
13237                out.as_primitive::<Float64Type>().value(0),
13238                expected,
13239                "Decimal128({unscaled}, scale={scale}) -> Float64"
13240            );
13241        }
13242
13243        // (unscaled, scale, nearest float). The first three differ from
13244        // `(f32) (f64) value`, so fixing the f64 path alone leaves them wrong.
13245        for (unscaled, scale, expected) in [
13246            (13631072500000000514758830i128, 18i8, 13631073.0f32),
13247            (72073620000000000000000582908005, 24, 72073624.0),
13248            (-3273316900000000000957536840, 20, -32733170.0),
13249            (123456, 3, 123.456),
13250            (-12345678, 3, -12345.678),
13251            (1, 10, 1e-10),
13252        ] {
13253            let out = cast(&decimal128(unscaled, 38, scale), &DataType::Float32).unwrap();
13254            assert_eq!(
13255                out.as_primitive::<Float32Type>().value(0),
13256                expected,
13257                "Decimal128({unscaled}, scale={scale}) -> Float32"
13258            );
13259        }
13260    }
13261
13262    // Both sides of every boundary the conversion switches on.
13263    #[test]
13264    fn test_cast_decimal_to_float_matches_parsing_its_own_text() {
13265        for (unscaled, precision, scale) in [
13266            (9007199254740991i128, 38u8, 6i8), // last integer exact in an f64
13267            (9007199254740992, 38, 6),         // 2^53, the first that is not
13268            (9007199254740993, 38, 6),         // one past that
13269            (16777215, 38, 3),                 // last integer exact in an f32
13270            (16777216, 38, 3),                 // 2^24, the first that is not
13271            (16777217, 38, 3),                 // one past that
13272            (123456789, 38, 10),               // last exact power of ten for an f32
13273            (123456789, 38, 11),               // one past it
13274            (123456789, 38, 22),               // last exact power of ten for an f64
13275            (123456789, 38, 23),               // one past it
13276            (12345, 38, 0),                    // lowest scale the division handles
13277            (12345, 38, -1),                   // one below it: negative scales
13278            (12345, 38, -23),                  //   are all rescaled as text
13279            // only the upper bound of a scale is validated, so this is legal
13280            (i128::MAX, 38, i8::MIN),
13281            (999999999999999, 15, 3), // widest value precision 15 allows
13282            (-999999999999999, 15, 15),
13283            (999999999999999, 16, 3), // one precision past the skipped test
13284            (9999999, 7, 3),          // widest value precision 7 allows
13285            (-9999999, 7, 7),
13286            (9999999, 8, 3), // one precision past it, for `f32`
13287            (-99999999999999999999999999999999999999, 38, 20),
13288            (1, 38, 38),
13289        ] {
13290            let text = Decimal128Type::format_decimal(unscaled, u8::MAX, scale);
13291            let array = decimal128(unscaled, precision, scale);
13292            let label = format!("Decimal128({unscaled}, precision={precision}, scale={scale})");
13293
13294            let as_f64 = cast(&array, &DataType::Float64).unwrap();
13295            assert_eq!(
13296                as_f64.as_primitive::<Float64Type>().value(0),
13297                text.parse::<f64>().unwrap(),
13298                "{label} -> Float64 disagrees with parsing {text:?}"
13299            );
13300
13301            let as_f32 = cast(&array, &DataType::Float32).unwrap();
13302            assert_eq!(
13303                as_f32.as_primitive::<Float32Type>().value(0),
13304                text.parse::<f32>().unwrap(),
13305                "{label} -> Float32 disagrees with parsing {text:?}"
13306            );
13307        }
13308    }
13309
13310    #[test]
13311    fn test_cast_decimal256_to_float_is_correctly_rounded() {
13312        for (unscaled, scale) in [
13313            ("12345678901234567890", 2i8),
13314            ("10000000000000000000000000000000000000", 37),
13315            ("13631072500000000514758830", 18),
13316            ("123456", 3),
13317        ] {
13318            let value = i256::from_string(unscaled).unwrap();
13319            let array = Decimal256Array::from(vec![value])
13320                .with_precision_and_scale(76, scale)
13321                .unwrap();
13322            let text = Decimal256Type::format_decimal(value, u8::MAX, scale);
13323
13324            let as_f64 = cast(&array, &DataType::Float64).unwrap();
13325            assert_eq!(
13326                as_f64.as_primitive::<Float64Type>().value(0),
13327                text.parse::<f64>().unwrap(),
13328                "Decimal256({unscaled}, scale={scale}) -> Float64"
13329            );
13330
13331            let as_f32 = cast(&array, &DataType::Float32).unwrap();
13332            assert_eq!(
13333                as_f32.as_primitive::<Float32Type>().value(0),
13334                text.parse::<f32>().unwrap(),
13335                "Decimal256({unscaled}, scale={scale}) -> Float32"
13336            );
13337        }
13338    }
13339
13340    #[test]
13341    fn test_cast_decimal_to_float_saturates_out_of_range() {
13342        // 1.7e39: past f32::MAX, still far inside f64.
13343        for (unscaled, want_f32) in [(i128::MAX, f32::INFINITY), (i128::MIN, f32::NEG_INFINITY)] {
13344            let array = decimal128(unscaled, 38, -1);
13345
13346            let as_f32 = cast(&array, &DataType::Float32).unwrap();
13347            assert_eq!(as_f32.as_primitive::<Float32Type>().value(0), want_f32);
13348
13349            let text = Decimal128Type::format_decimal(unscaled, u8::MAX, -1);
13350            let as_f64 = cast(&array, &DataType::Float64).unwrap();
13351            assert_eq!(
13352                as_f64.as_primitive::<Float64Type>().value(0),
13353                text.parse::<f64>().unwrap()
13354            );
13355        }
13356
13357        // 1e-76: below the smallest f32 subnormal, representable in an f64.
13358        let array = Decimal256Array::from(vec![i256::from(1)])
13359            .with_precision_and_scale(76, 76)
13360            .unwrap();
13361
13362        let as_f32 = cast(&array, &DataType::Float32).unwrap();
13363        assert_eq!(as_f32.as_primitive::<Float32Type>().value(0), 0.0);
13364
13365        let as_f64 = cast(&array, &DataType::Float64).unwrap();
13366        assert_eq!(as_f64.as_primitive::<Float64Type>().value(0), 1e-76);
13367    }
13368
13369    #[test]
13370    fn test_single_decimal_to_float_lossy_scale_outside_i8() {
13371        // `format_decimal` takes an `i8` scale; narrowing 128 into one wraps it to
13372        // -128 and turns 1e-128 into 1e128. The edges of the `i8` range still round
13373        // once, and anything past them falls back to the division.
13374        let as_float = |x: i128| x as f64;
13375        let lossy =
13376            |scale: i32| single_decimal_to_float_lossy::<Decimal128Type, _>(&as_float, 1, scale);
13377        for scale in [i8::MAX, i8::MIN] {
13378            let text = Decimal128Type::format_decimal(1, u8::MAX, scale);
13379            assert_eq!(
13380                lossy(scale.into()),
13381                text.parse::<f64>().unwrap(),
13382                "scale={scale}"
13383            );
13384        }
13385        for scale in [128, -129] {
13386            assert_eq!(lossy(scale), 1.0 / 10_f64.powi(scale), "scale={scale}");
13387        }
13388    }
13389
13390    // Keeping only `MAX_PRECISION` digits would scale the result by ten.
13391    #[test]
13392    fn test_cast_decimal_to_float_keeps_digits_beyond_declared_precision() {
13393        // 39 digits, one more than `Decimal128Type::MAX_PRECISION`.
13394        let array = decimal128(i128::MAX, 38, 0);
13395
13396        let as_f64 = cast(&array, &DataType::Float64).unwrap();
13397        assert_eq!(
13398            as_f64.as_primitive::<Float64Type>().value(0),
13399            1.7014118346046923e38 // 2^127, the double nearest i128::MAX
13400        );
13401
13402        let as_f32 = cast(&array, &DataType::Float32).unwrap();
13403        assert_eq!(
13404            as_f32.as_primitive::<Float32Type>().value(0),
13405            1.7014118e38 // 2^127, the float nearest i128::MAX
13406        );
13407
13408        // A precision of 15 (7 for `f32`) promises an integer the float holds
13409        // exactly, but values are not validated against the precision they declare.
13410        for (unscaled, precision, scale) in
13411            [(12345678901234567890i128, 15u8, 2i8), (16777217, 7, 1)]
13412        {
13413            let text = Decimal128Type::format_decimal(unscaled, u8::MAX, scale);
13414            let array = decimal128(unscaled, precision, scale);
13415
13416            let as_f64 = cast(&array, &DataType::Float64).unwrap();
13417            assert_eq!(
13418                as_f64.as_primitive::<Float64Type>().value(0),
13419                text.parse::<f64>().unwrap(),
13420                "{text} at precision {precision} -> Float64"
13421            );
13422
13423            let as_f32 = cast(&array, &DataType::Float32).unwrap();
13424            assert_eq!(
13425                as_f32.as_primitive::<Float32Type>().value(0),
13426                text.parse::<f32>().unwrap(),
13427                "{text} at precision {precision} -> Float32"
13428            );
13429        }
13430    }
13431}