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 decimal;
41mod dictionary;
42mod list;
43mod map;
44mod run_array;
45mod string;
46
47use crate::cast::decimal::*;
48use crate::cast::dictionary::*;
49use crate::cast::list::*;
50use crate::cast::map::*;
51use crate::cast::run_array::*;
52use crate::cast::string::*;
53
54use arrow_buffer::IntervalMonthDayNano;
55use arrow_data::ByteView;
56use chrono::{NaiveTime, Offset, TimeZone, Utc};
57use std::cmp::Ordering;
58use std::sync::Arc;
59
60use crate::display::{ArrayFormatter, FormatOptions};
61use crate::parse::{
62    Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
63    string_to_datetime,
64};
65use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
66use arrow_buffer::{ArrowNativeType, OffsetBuffer, i256};
67use arrow_data::ArrayData;
68use arrow_data::transform::MutableArrayData;
69use arrow_schema::*;
70use arrow_select::take::take;
71use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
72
73pub use decimal::{DecimalCast, rescale_decimal};
74
75/// CastOptions provides a way to override the default cast behaviors
76#[derive(Debug, Clone, PartialEq, Eq, Hash)]
77pub struct CastOptions<'a> {
78    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
79    pub safe: bool,
80    /// Formatting options when casting from temporal types to string
81    pub format_options: FormatOptions<'a>,
82}
83
84impl Default for CastOptions<'_> {
85    fn default() -> Self {
86        Self {
87            safe: true,
88            format_options: FormatOptions::default(),
89        }
90    }
91}
92
93/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
94///
95/// See [`cast_with_options`] for more information
96pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
97    use self::DataType::*;
98    use self::IntervalUnit::*;
99    use self::TimeUnit::*;
100    if from_type == to_type {
101        return true;
102    }
103
104    match (from_type, to_type) {
105        (Null, _) => true,
106        // Dictionary/List conditions should be put in front of others
107        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
108            can_cast_types(from_value_type, to_value_type)
109        }
110        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
111        (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
112        (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
113        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
114        (
115            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
116            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
117        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
118        (
119            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
120            Utf8 | LargeUtf8 | Utf8View,
121        ) => can_cast_types(list_from.data_type(), to_type),
122        (
123            FixedSizeList(list_from, _),
124            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
125        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
126        (
127            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
128            FixedSizeList(list_to, _),
129        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
130        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
131            can_cast_types(inner.data_type(), inner_to.data_type())
132        }
133        (_, List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to)) => {
134            can_cast_types(from_type, list_to.data_type())
135        }
136        (_, FixedSizeList(list_to, size)) if *size == 1 => {
137            can_cast_types(from_type, list_to.data_type())
138        }
139        (FixedSizeList(list_from, size), _) if *size == 1 => {
140            can_cast_types(list_from.data_type(), to_type)
141        }
142        (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
143            if ordered_from == ordered_to =>
144        {
145            match (
146                key_field(from_entries),
147                key_field(to_entries),
148                value_field(from_entries),
149                value_field(to_entries),
150            ) {
151                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
152                    can_cast_types(from_key.data_type(), to_key.data_type())
153                        && can_cast_types(from_value.data_type(), to_value.data_type())
154                }
155                _ => false,
156            }
157        }
158        // cast one decimal type to another decimal type
159        (
160            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
161            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
162        ) => true,
163        // unsigned integer to decimal
164        (
165            UInt8 | UInt16 | UInt32 | UInt64,
166            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
167        ) => true,
168        // signed numeric to decimal
169        (
170            Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
171            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
172        ) => true,
173        // decimal to unsigned numeric
174        (
175            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
176            UInt8 | UInt16 | UInt32 | UInt64,
177        ) => true,
178        // decimal to signed numeric
179        (
180            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
181            Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
182        ) => true,
183        // decimal to string
184        (
185            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
186            Utf8View | Utf8 | LargeUtf8,
187        ) => true,
188        // string to decimal
189        (
190            Utf8View | Utf8 | LargeUtf8,
191            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
192        ) => true,
193        (Struct(from_fields), Struct(to_fields)) => {
194            if from_fields.len() != to_fields.len() {
195                return false;
196            }
197
198            // fast path, all field names are in the same order and same number of fields
199            if from_fields
200                .iter()
201                .zip(to_fields.iter())
202                .all(|(f1, f2)| f1.name() == f2.name())
203            {
204                return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
205                    // Assume that nullability between two structs are compatible, if not,
206                    // cast kernel will return error.
207                    can_cast_types(f1.data_type(), f2.data_type())
208                });
209            }
210
211            // slow path, we match the fields by name
212            if to_fields.iter().all(|to_field| {
213                from_fields
214                    .iter()
215                    .find(|from_field| from_field.name() == to_field.name())
216                    .is_some_and(|from_field| {
217                        // Assume that nullability between two structs are compatible, if not,
218                        // cast kernel will return error.
219                        can_cast_types(from_field.data_type(), to_field.data_type())
220                    })
221            }) {
222                return true;
223            }
224
225            // if we couldn't match by name, we try to see if they can be matched by position
226            from_fields
227                .iter()
228                .zip(to_fields.iter())
229                .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
230        }
231        (Struct(_), _) => false,
232        (_, Struct(_)) => false,
233
234        (_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
235        (Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
236
237        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
238            true
239        }
240        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
241            true
242        }
243        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
244        (
245            Utf8 | LargeUtf8 | Utf8View,
246            Binary
247            | LargeBinary
248            | Utf8
249            | LargeUtf8
250            | Date32
251            | Date64
252            | Time32(Second)
253            | Time32(Millisecond)
254            | Time64(Microsecond)
255            | Time64(Nanosecond)
256            | Timestamp(Second, _)
257            | Timestamp(Millisecond, _)
258            | Timestamp(Microsecond, _)
259            | Timestamp(Nanosecond, _)
260            | Interval(_)
261            | BinaryView,
262        ) => true,
263        (Utf8 | LargeUtf8, Utf8View) => true,
264        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
265        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
266        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
267
268        (_, Binary | LargeBinary) => from_type.is_integer(),
269
270        // start numeric casts
271        (
272            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
273            | Float64,
274            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
275            | Float64,
276        ) => true,
277        // end numeric casts
278
279        // temporal casts
280        (Int32, Date32 | Date64 | Time32(_)) => true,
281        (Date32, Int32 | Int64) => true,
282        (Time32(_), Int32 | Int64) => true,
283        (Int64, Date64 | Date32 | Time64(_)) => true,
284        (Date64, Int64 | Int32) => true,
285        (Time64(_), Int64) => true,
286        (Date32 | Date64, Date32 | Date64) => true,
287        // time casts
288        (Time32(_), Time32(_)) => true,
289        (Time32(_), Time64(_)) => true,
290        (Time64(_), Time64(_)) => true,
291        (Time64(_), Time32(to_unit)) => {
292            matches!(to_unit, Second | Millisecond)
293        }
294        (Timestamp(_, _), _) if to_type.is_numeric() => true,
295        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
296        (Date64, Timestamp(_, _)) => true,
297        (Date32, Timestamp(_, _)) => true,
298        (
299            Timestamp(_, _),
300            Timestamp(_, _)
301            | Date32
302            | Date64
303            | Time32(Second)
304            | Time32(Millisecond)
305            | Time64(Microsecond)
306            | Time64(Nanosecond),
307        ) => true,
308        (_, Duration(_)) if from_type.is_numeric() => true,
309        (Duration(_), _) if to_type.is_numeric() => true,
310        (Duration(_), Duration(_)) => true,
311        (Interval(from_type), Int64) => {
312            match from_type {
313                YearMonth => true,
314                DayTime => true,
315                MonthDayNano => false, // Native type is i128
316            }
317        }
318        (Int32, Interval(to_type)) => match to_type {
319            YearMonth => true,
320            DayTime => false,
321            MonthDayNano => false,
322        },
323        (Duration(_), Interval(MonthDayNano)) => true,
324        (Interval(MonthDayNano), Duration(_)) => true,
325        (Interval(YearMonth), Interval(MonthDayNano)) => true,
326        (Interval(DayTime), Interval(MonthDayNano)) => true,
327        (_, _) => false,
328    }
329}
330
331/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
332///
333/// See [`cast_with_options`] for more information
334pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
335    cast_with_options(array, to_type, &CastOptions::default())
336}
337
338fn cast_integer_to_decimal<
339    T: ArrowPrimitiveType,
340    D: DecimalType + ArrowPrimitiveType<Native = M>,
341    M,
342>(
343    array: &PrimitiveArray<T>,
344    precision: u8,
345    scale: i8,
346    base: M,
347    cast_options: &CastOptions,
348) -> Result<ArrayRef, ArrowError>
349where
350    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
351    M: ArrowNativeTypeOp,
352{
353    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
354        ArrowError::CastError(format!(
355            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
356            D::PREFIX,
357            precision,
358            scale,
359        ))
360    })?;
361
362    let array = if scale < 0 {
363        match cast_options.safe {
364            true => array.unary_opt::<_, D>(|v| {
365                v.as_()
366                    .div_checked(scale_factor)
367                    .ok()
368                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
369            }),
370            false => array.try_unary::<_, D, _>(|v| {
371                v.as_()
372                    .div_checked(scale_factor)
373                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
374            })?,
375        }
376    } else {
377        match cast_options.safe {
378            true => array.unary_opt::<_, D>(|v| {
379                v.as_()
380                    .mul_checked(scale_factor)
381                    .ok()
382                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
383            }),
384            false => array.try_unary::<_, D, _>(|v| {
385                v.as_()
386                    .mul_checked(scale_factor)
387                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
388            })?,
389        }
390    };
391
392    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
393}
394
395/// Cast the array from interval year month to month day nano
396fn cast_interval_year_month_to_interval_month_day_nano(
397    array: &dyn Array,
398    _cast_options: &CastOptions,
399) -> Result<ArrayRef, ArrowError> {
400    let array = array.as_primitive::<IntervalYearMonthType>();
401
402    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
403        let months = IntervalYearMonthType::to_months(v);
404        IntervalMonthDayNanoType::make_value(months, 0, 0)
405    })))
406}
407
408/// Cast the array from interval day time to month day nano
409fn cast_interval_day_time_to_interval_month_day_nano(
410    array: &dyn Array,
411    _cast_options: &CastOptions,
412) -> Result<ArrayRef, ArrowError> {
413    let array = array.as_primitive::<IntervalDayTimeType>();
414    let mul = 1_000_000;
415
416    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
417        let (days, ms) = IntervalDayTimeType::to_parts(v);
418        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
419    })))
420}
421
422/// Cast the array from interval to duration
423fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
424    array: &dyn Array,
425    cast_options: &CastOptions,
426) -> Result<ArrayRef, ArrowError> {
427    let array = array.as_primitive::<IntervalMonthDayNanoType>();
428    let scale = match D::DATA_TYPE {
429        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
430        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
431        DataType::Duration(TimeUnit::Microsecond) => 1_000,
432        DataType::Duration(TimeUnit::Nanosecond) => 1,
433        _ => unreachable!(),
434    };
435
436    if cast_options.safe {
437        let iter = array.iter().map(|v| {
438            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
439        });
440        Ok(Arc::new(unsafe {
441            PrimitiveArray::<D>::from_trusted_len_iter(iter)
442        }))
443    } else {
444        let vec = array
445            .iter()
446            .map(|v| {
447                v.map(|v| match v.days == 0 && v.months == 0 {
448                    true => Ok((v.nanoseconds) / scale),
449                    _ => Err(ArrowError::ComputeError(
450                        "Cannot convert interval containing non-zero months or days to duration"
451                            .to_string(),
452                    )),
453                })
454                .transpose()
455            })
456            .collect::<Result<Vec<_>, _>>()?;
457        Ok(Arc::new(unsafe {
458            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
459        }))
460    }
461}
462
463/// Cast the array from duration and interval
464fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
465    array: &dyn Array,
466    cast_options: &CastOptions,
467) -> Result<ArrayRef, ArrowError> {
468    let array = array
469        .as_any()
470        .downcast_ref::<PrimitiveArray<D>>()
471        .ok_or_else(|| {
472            ArrowError::ComputeError(
473                "Internal Error: Cannot cast duration to DurationArray of expected type"
474                    .to_string(),
475            )
476        })?;
477
478    let scale = match array.data_type() {
479        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
480        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
481        DataType::Duration(TimeUnit::Microsecond) => 1_000,
482        DataType::Duration(TimeUnit::Nanosecond) => 1,
483        _ => unreachable!(),
484    };
485
486    if cast_options.safe {
487        let iter = array.iter().map(|v| {
488            v.and_then(|v| {
489                v.checked_mul(scale)
490                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
491            })
492        });
493        Ok(Arc::new(unsafe {
494            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
495        }))
496    } else {
497        let vec = array
498            .iter()
499            .map(|v| {
500                v.map(|v| {
501                    if let Ok(v) = v.mul_checked(scale) {
502                        Ok(IntervalMonthDayNano::new(0, 0, v))
503                    } else {
504                        Err(ArrowError::ComputeError(format!(
505                            "Cannot cast to {:?}. Overflowing on {:?}",
506                            IntervalMonthDayNanoType::DATA_TYPE,
507                            v
508                        )))
509                    }
510                })
511                .transpose()
512            })
513            .collect::<Result<Vec<_>, _>>()?;
514        Ok(Arc::new(unsafe {
515            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
516        }))
517    }
518}
519
520/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
521fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
522    array: &dyn Array,
523) -> Result<ArrayRef, ArrowError> {
524    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
525}
526
527fn make_timestamp_array(
528    array: &PrimitiveArray<Int64Type>,
529    unit: TimeUnit,
530    tz: Option<Arc<str>>,
531) -> ArrayRef {
532    match unit {
533        TimeUnit::Second => Arc::new(
534            array
535                .reinterpret_cast::<TimestampSecondType>()
536                .with_timezone_opt(tz),
537        ),
538        TimeUnit::Millisecond => Arc::new(
539            array
540                .reinterpret_cast::<TimestampMillisecondType>()
541                .with_timezone_opt(tz),
542        ),
543        TimeUnit::Microsecond => Arc::new(
544            array
545                .reinterpret_cast::<TimestampMicrosecondType>()
546                .with_timezone_opt(tz),
547        ),
548        TimeUnit::Nanosecond => Arc::new(
549            array
550                .reinterpret_cast::<TimestampNanosecondType>()
551                .with_timezone_opt(tz),
552        ),
553    }
554}
555
556fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
557    match unit {
558        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
559        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
560        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
561        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
562    }
563}
564
565fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
566    v: i64,
567    tz: Option<Tz>,
568) -> Result<NaiveTime, ArrowError> {
569    let time = match tz {
570        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
571        None => as_datetime::<T>(v).map(|d| d.time()),
572    };
573
574    time.ok_or_else(|| {
575        ArrowError::CastError(format!(
576            "Failed to create naive time with {} {}",
577            std::any::type_name::<T>(),
578            v
579        ))
580    })
581}
582
583fn timestamp_to_date32<T: ArrowTimestampType>(
584    array: &PrimitiveArray<T>,
585) -> Result<ArrayRef, ArrowError> {
586    let err = |x: i64| {
587        ArrowError::CastError(format!(
588            "Cannot convert {} {x} to datetime",
589            std::any::type_name::<T>()
590        ))
591    };
592
593    let array: Date32Array = match array.timezone() {
594        Some(tz) => {
595            let tz: Tz = tz.parse()?;
596            array.try_unary(|x| {
597                as_datetime_with_timezone::<T>(x, tz)
598                    .ok_or_else(|| err(x))
599                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
600            })?
601        }
602        None => array.try_unary(|x| {
603            as_datetime::<T>(x)
604                .ok_or_else(|| err(x))
605                .map(|d| Date32Type::from_naive_date(d.date()))
606        })?,
607    };
608    Ok(Arc::new(array))
609}
610
611/// Try to cast `array` to `to_type` if possible.
612///
613/// Returns a new Array with type `to_type` if possible.
614///
615/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
616///
617/// # Behavior
618/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
619/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
620///   short variants are accepted, other strings return null or error
621/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
622///   in integer casts return null
623/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
624/// * `List` to `List`: the underlying data type is cast
625/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
626///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
627/// * Primitive to `List`: a list array with 1 value per slot is created
628/// * `Date32` and `Date64`: precision lost when going to higher interval
629/// * `Time32 and `Time64`: precision lost when going to higher interval
630/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
631/// * Temporal to/from backing Primitive: zero-copy with data type change
632/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
633///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
634/// * `Decimal` to `Float32/Float64` is lossy and values outside the representable
635///   range become `INFINITY` or `-INFINITY` without error.
636///
637/// Unsupported Casts (check with `can_cast_types` before calling):
638/// * To or from `StructArray`
639/// * `List` to `Primitive`
640/// * `Interval` and `Duration`
641///
642/// # Durations and Intervals
643///
644/// Casting integer types directly to interval types such as
645/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
646/// is ambiguous. For example, the integer  could represent either nanoseconds
647/// or months.
648///
649/// To cast an integer type to an interval type, first convert to a Duration
650/// type, and then cast that to the desired interval type.
651///
652/// For example, to convert an `Int64` representing nanoseconds to an
653/// `IntervalMonthDayNano` you would first convert the `Int64` to a
654/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
655///
656/// # Timestamps and Timezones
657///
658/// Timestamps are stored with an optional timezone in Arrow.
659///
660/// ## Casting timestamps to a timestamp without timezone / UTC
661/// ```
662/// # use arrow_array::Int64Array;
663/// # use arrow_array::types::TimestampSecondType;
664/// # use arrow_cast::{cast, display};
665/// # use arrow_array::cast::AsArray;
666/// # use arrow_schema::{DataType, TimeUnit};
667/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
668/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
669/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
670/// let b = cast(&a, &data_type).unwrap();
671/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
672/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
673/// // use display to show them (note has no trailing Z)
674/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
675/// ```
676///
677/// ## Casting timestamps to a timestamp with timezone
678///
679/// Similarly to the previous example, if you cast numeric values to a timestamp
680/// with timezone, the cast kernel will not change the underlying values
681/// but display and other functions will interpret them as being in the provided timezone.
682///
683/// ```
684/// # use arrow_array::Int64Array;
685/// # use arrow_array::types::TimestampSecondType;
686/// # use arrow_cast::{cast, display};
687/// # use arrow_array::cast::AsArray;
688/// # use arrow_schema::{DataType, TimeUnit};
689/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
690/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
691/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
692/// let b = cast(&a, &data_type).unwrap();
693/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
694/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
695/// // displayed in the target timezone (note the offset -05:00)
696/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
697/// ```
698/// # Casting timestamps without timezone to timestamps with timezone
699///
700/// When casting from a timestamp without timezone to a timestamp with
701/// timezone, the cast kernel interprets the timestamp values as being in
702/// the destination timezone and then adjusts the underlying value to UTC as required
703///
704/// However, note that when casting from a timestamp with timezone BACK to a
705/// timestamp without timezone the cast kernel does not adjust the values.
706///
707/// Thus round trip casting a timestamp without timezone to a timestamp with
708/// timezone and back to a timestamp without timezone results in different
709/// values than the starting values.
710///
711/// ```
712/// # use arrow_array::Int64Array;
713/// # use arrow_array::types::{TimestampSecondType};
714/// # use arrow_cast::{cast, display};
715/// # use arrow_array::cast::AsArray;
716/// # use arrow_schema::{DataType, TimeUnit};
717/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
718/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
719/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
720/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
721/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
722/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
723/// // displayed without a timezone (note lack of offset or Z)
724/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
725///
726/// // Convert timestamps without a timezone to timestamps with a timezone
727/// let c = cast(&b, &data_type_tz).unwrap();
728/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
729/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
730/// // displayed with the target timezone offset (-05:00)
731/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
732///
733/// // Convert from timestamp with timezone back to timestamp without timezone
734/// let d = cast(&c, &data_type).unwrap();
735/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
736/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
737/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
738/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
739/// ```
740pub fn cast_with_options(
741    array: &dyn Array,
742    to_type: &DataType,
743    cast_options: &CastOptions,
744) -> Result<ArrayRef, ArrowError> {
745    use DataType::*;
746    let from_type = array.data_type();
747    // clone array if types are the same
748    if from_type == to_type {
749        return Ok(make_array(array.to_data()));
750    }
751    match (from_type, to_type) {
752        (Null, _) => Ok(new_null_array(to_type, array.len())),
753        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
754            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
755            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
756            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
757            _ => Err(ArrowError::CastError(format!(
758                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
759            ))),
760        },
761        (_, RunEndEncoded(index_type, value_type)) => {
762            let array_ref = make_array(array.to_data());
763            match index_type.data_type() {
764                Int16 => cast_to_run_end_encoded::<Int16Type>(
765                    &array_ref,
766                    value_type.data_type(),
767                    cast_options,
768                ),
769                Int32 => cast_to_run_end_encoded::<Int32Type>(
770                    &array_ref,
771                    value_type.data_type(),
772                    cast_options,
773                ),
774                Int64 => cast_to_run_end_encoded::<Int64Type>(
775                    &array_ref,
776                    value_type.data_type(),
777                    cast_options,
778                ),
779                _ => Err(ArrowError::CastError(format!(
780                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
781                ))),
782            }
783        }
784        (Dictionary(index_type, _), _) => match **index_type {
785            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
786            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
787            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
788            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
789            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
790            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
791            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
792            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
793            _ => Err(ArrowError::CastError(format!(
794                "Casting from dictionary type {from_type} to {to_type} not supported",
795            ))),
796        },
797        (_, Dictionary(index_type, value_type)) => match **index_type {
798            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
799            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
800            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
801            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
802            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
803            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
804            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
805            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
806            _ => Err(ArrowError::CastError(format!(
807                "Casting from type {from_type} to dictionary type {to_type} not supported",
808            ))),
809        },
810        // Casting between lists of same types (cast inner values)
811        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
812        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
813        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
814            if size_from != size_to {
815                return Err(ArrowError::CastError(
816                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
817                ));
818            }
819            let array = array.as_fixed_size_list();
820            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
821            Ok(Arc::new(FixedSizeListArray::try_new(
822                list_to.clone(),
823                *size_from,
824                values,
825                array.nulls().cloned(),
826            )?))
827        }
828        (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
829        (LargeListView(_), LargeListView(to)) => {
830            cast_list_view_values::<i64>(array, to, cast_options)
831        }
832        // Casting between different types of lists
833        // List
834        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
835        (List(_), FixedSizeList(field, size)) => {
836            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
837        }
838        (List(_), ListView(list_to)) => {
839            cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
840        }
841        (List(_), LargeListView(list_to)) => {
842            cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
843        }
844        // LargeList
845        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
846        (LargeList(_), FixedSizeList(field, size)) => {
847            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
848        }
849        (LargeList(_), ListView(list_to)) => {
850            cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
851        }
852        (LargeList(_), LargeListView(list_to)) => {
853            cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
854        }
855        // ListView
856        (ListView(_), List(list_to)) => {
857            cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
858        }
859        (ListView(_), LargeList(list_to)) => {
860            cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
861        }
862        (ListView(_), LargeListView(list_to)) => {
863            cast_list_view::<i32, i64>(array, list_to, cast_options)
864        }
865        (ListView(_), FixedSizeList(field, size)) => {
866            cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
867        }
868        // LargeListView
869        (LargeListView(_), LargeList(list_to)) => {
870            cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
871        }
872        (LargeListView(_), List(list_to)) => {
873            cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
874        }
875        (LargeListView(_), ListView(list_to)) => {
876            cast_list_view::<i64, i32>(array, list_to, cast_options)
877        }
878        (LargeListView(_), FixedSizeList(field, size)) => {
879            cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
880        }
881        // FixedSizeList
882        (FixedSizeList(_, _), List(list_to)) => {
883            cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
884        }
885        (FixedSizeList(_, _), LargeList(list_to)) => {
886            cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
887        }
888        (FixedSizeList(_, _), ListView(list_to)) => {
889            cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
890        }
891        (FixedSizeList(_, _), LargeListView(list_to)) => {
892            cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
893        }
894        // List to/from other types
895        (FixedSizeList(_, size), _) if *size == 1 => {
896            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
897        }
898        // NOTE: we could support FSL to string here too but might be confusing
899        //       since behaviour for size 1 would be different (see arm above)
900        (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
901            Utf8 => value_to_string::<i32>(array, cast_options),
902            LargeUtf8 => value_to_string::<i64>(array, cast_options),
903            Utf8View => value_to_string_view(array, cast_options),
904            _ => Err(ArrowError::CastError(
905                "Cannot cast list to non-list data types".to_string(),
906            )),
907        },
908        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
909        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
910        (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
911        (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
912        (_, FixedSizeList(to, size)) if *size == 1 => {
913            cast_values_to_fixed_size_list(array, to, *size, cast_options)
914        }
915        // Map
916        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
917            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
918        }
919        // Decimal to decimal, same width
920        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
921            cast_decimal_to_decimal_same_type::<Decimal32Type>(
922                array.as_primitive(),
923                *p1,
924                *s1,
925                *p2,
926                *s2,
927                cast_options,
928            )
929        }
930        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
931            cast_decimal_to_decimal_same_type::<Decimal64Type>(
932                array.as_primitive(),
933                *p1,
934                *s1,
935                *p2,
936                *s2,
937                cast_options,
938            )
939        }
940        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
941            cast_decimal_to_decimal_same_type::<Decimal128Type>(
942                array.as_primitive(),
943                *p1,
944                *s1,
945                *p2,
946                *s2,
947                cast_options,
948            )
949        }
950        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
951            cast_decimal_to_decimal_same_type::<Decimal256Type>(
952                array.as_primitive(),
953                *p1,
954                *s1,
955                *p2,
956                *s2,
957                cast_options,
958            )
959        }
960        // Decimal to decimal, different width
961        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
962            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
963                array.as_primitive(),
964                *p1,
965                *s1,
966                *p2,
967                *s2,
968                cast_options,
969            )
970        }
971        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
972            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
973                array.as_primitive(),
974                *p1,
975                *s1,
976                *p2,
977                *s2,
978                cast_options,
979            )
980        }
981        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
982            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
983                array.as_primitive(),
984                *p1,
985                *s1,
986                *p2,
987                *s2,
988                cast_options,
989            )
990        }
991        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
992            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
993                array.as_primitive(),
994                *p1,
995                *s1,
996                *p2,
997                *s2,
998                cast_options,
999            )
1000        }
1001        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1002            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1003                array.as_primitive(),
1004                *p1,
1005                *s1,
1006                *p2,
1007                *s2,
1008                cast_options,
1009            )
1010        }
1011        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1012            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1013                array.as_primitive(),
1014                *p1,
1015                *s1,
1016                *p2,
1017                *s2,
1018                cast_options,
1019            )
1020        }
1021        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1022            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1023                array.as_primitive(),
1024                *p1,
1025                *s1,
1026                *p2,
1027                *s2,
1028                cast_options,
1029            )
1030        }
1031        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1032            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1033                array.as_primitive(),
1034                *p1,
1035                *s1,
1036                *p2,
1037                *s2,
1038                cast_options,
1039            )
1040        }
1041        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1042            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1043                array.as_primitive(),
1044                *p1,
1045                *s1,
1046                *p2,
1047                *s2,
1048                cast_options,
1049            )
1050        }
1051        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1052            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1053                array.as_primitive(),
1054                *p1,
1055                *s1,
1056                *p2,
1057                *s2,
1058                cast_options,
1059            )
1060        }
1061        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1062            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1063                array.as_primitive(),
1064                *p1,
1065                *s1,
1066                *p2,
1067                *s2,
1068                cast_options,
1069            )
1070        }
1071        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1072            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1073                array.as_primitive(),
1074                *p1,
1075                *s1,
1076                *p2,
1077                *s2,
1078                cast_options,
1079            )
1080        }
1081        // Decimal to non-decimal
1082        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1083            cast_from_decimal::<Decimal32Type, _>(
1084                array,
1085                10_i32,
1086                scale,
1087                from_type,
1088                to_type,
1089                |x: i32| x as f64,
1090                cast_options,
1091            )
1092        }
1093        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1094            cast_from_decimal::<Decimal64Type, _>(
1095                array,
1096                10_i64,
1097                scale,
1098                from_type,
1099                to_type,
1100                |x: i64| x as f64,
1101                cast_options,
1102            )
1103        }
1104        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1105            cast_from_decimal::<Decimal128Type, _>(
1106                array,
1107                10_i128,
1108                scale,
1109                from_type,
1110                to_type,
1111                |x: i128| x as f64,
1112                cast_options,
1113            )
1114        }
1115        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1116            cast_from_decimal::<Decimal256Type, _>(
1117                array,
1118                i256::from_i128(10_i128),
1119                scale,
1120                from_type,
1121                to_type,
1122                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1123                cast_options,
1124            )
1125        }
1126        // Non-decimal to decimal
1127        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1128            cast_to_decimal::<Decimal32Type, _>(
1129                array,
1130                10_i32,
1131                precision,
1132                scale,
1133                from_type,
1134                to_type,
1135                cast_options,
1136            )
1137        }
1138        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1139            cast_to_decimal::<Decimal64Type, _>(
1140                array,
1141                10_i64,
1142                precision,
1143                scale,
1144                from_type,
1145                to_type,
1146                cast_options,
1147            )
1148        }
1149        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1150            cast_to_decimal::<Decimal128Type, _>(
1151                array,
1152                10_i128,
1153                precision,
1154                scale,
1155                from_type,
1156                to_type,
1157                cast_options,
1158            )
1159        }
1160        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1161            cast_to_decimal::<Decimal256Type, _>(
1162                array,
1163                i256::from_i128(10_i128),
1164                precision,
1165                scale,
1166                from_type,
1167                to_type,
1168                cast_options,
1169            )
1170        }
1171        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1172            array.as_struct(),
1173            from_fields.clone(),
1174            to_fields.clone(),
1175            cast_options,
1176        ),
1177        (Struct(_), _) => Err(ArrowError::CastError(format!(
1178            "Casting from {from_type} to {to_type} not supported"
1179        ))),
1180        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1181            "Casting from {from_type} to {to_type} not supported"
1182        ))),
1183        (_, Boolean) => match from_type {
1184            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1185            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1186            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1187            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1188            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1189            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1190            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1191            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1192            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1193            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1194            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1195            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1196            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1197            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1198            _ => Err(ArrowError::CastError(format!(
1199                "Casting from {from_type} to {to_type} not supported",
1200            ))),
1201        },
1202        (Boolean, _) => match to_type {
1203            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1204            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1205            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1206            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1207            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1208            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1209            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1210            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1211            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1212            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1213            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1214            Utf8View => value_to_string_view(array, cast_options),
1215            Utf8 => value_to_string::<i32>(array, cast_options),
1216            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1217            _ => Err(ArrowError::CastError(format!(
1218                "Casting from {from_type} to {to_type} not supported",
1219            ))),
1220        },
1221        (Utf8, _) => match to_type {
1222            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1223            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1224            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1225            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1226            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1227            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1228            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1229            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1230            Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1231            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1232            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1233            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1234            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1235            Binary => Ok(Arc::new(BinaryArray::from(
1236                array.as_string::<i32>().clone(),
1237            ))),
1238            LargeBinary => {
1239                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1240                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1241            }
1242            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1243            BinaryView => Ok(Arc::new(
1244                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1245            )),
1246            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1247            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1248            Time32(TimeUnit::Millisecond) => {
1249                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1250            }
1251            Time64(TimeUnit::Microsecond) => {
1252                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1253            }
1254            Time64(TimeUnit::Nanosecond) => {
1255                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1256            }
1257            Timestamp(TimeUnit::Second, to_tz) => {
1258                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1259            }
1260            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1261                i32,
1262                TimestampMillisecondType,
1263            >(array, to_tz, cast_options),
1264            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1265                i32,
1266                TimestampMicrosecondType,
1267            >(array, to_tz, cast_options),
1268            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1269                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1270            }
1271            Interval(IntervalUnit::YearMonth) => {
1272                cast_string_to_year_month_interval::<i32>(array, cast_options)
1273            }
1274            Interval(IntervalUnit::DayTime) => {
1275                cast_string_to_day_time_interval::<i32>(array, cast_options)
1276            }
1277            Interval(IntervalUnit::MonthDayNano) => {
1278                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1279            }
1280            _ => Err(ArrowError::CastError(format!(
1281                "Casting from {from_type} to {to_type} not supported",
1282            ))),
1283        },
1284        (Utf8View, _) => match to_type {
1285            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1286            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1287            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1288            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1289            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1290            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1291            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1292            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1293            Float16 => parse_string_view::<Float16Type>(array, cast_options),
1294            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1295            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1296            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1297            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1298            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1299            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1300            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1301            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1302            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1303            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1304            Time32(TimeUnit::Millisecond) => {
1305                parse_string_view::<Time32MillisecondType>(array, cast_options)
1306            }
1307            Time64(TimeUnit::Microsecond) => {
1308                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1309            }
1310            Time64(TimeUnit::Nanosecond) => {
1311                parse_string_view::<Time64NanosecondType>(array, cast_options)
1312            }
1313            Timestamp(TimeUnit::Second, to_tz) => {
1314                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1315            }
1316            Timestamp(TimeUnit::Millisecond, to_tz) => {
1317                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1318            }
1319            Timestamp(TimeUnit::Microsecond, to_tz) => {
1320                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1321            }
1322            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1323                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1324            }
1325            Interval(IntervalUnit::YearMonth) => {
1326                cast_view_to_year_month_interval(array, cast_options)
1327            }
1328            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1329            Interval(IntervalUnit::MonthDayNano) => {
1330                cast_view_to_month_day_nano_interval(array, cast_options)
1331            }
1332            _ => Err(ArrowError::CastError(format!(
1333                "Casting from {from_type} to {to_type} not supported",
1334            ))),
1335        },
1336        (LargeUtf8, _) => match to_type {
1337            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1338            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1339            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1340            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1341            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1342            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1343            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1344            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1345            Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1346            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1347            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1348            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1349            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1350            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1351            Binary => {
1352                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1353                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1354            }
1355            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1356                array.as_string::<i64>().clone(),
1357            ))),
1358            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1359            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1360                array
1361                    .as_string::<i64>()
1362                    .into_iter()
1363                    .map(|x| x.map(|x| x.as_bytes()))
1364                    .collect::<Vec<_>>(),
1365            ))),
1366            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1367            Time32(TimeUnit::Millisecond) => {
1368                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1369            }
1370            Time64(TimeUnit::Microsecond) => {
1371                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1372            }
1373            Time64(TimeUnit::Nanosecond) => {
1374                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1375            }
1376            Timestamp(TimeUnit::Second, to_tz) => {
1377                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1378            }
1379            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1380                i64,
1381                TimestampMillisecondType,
1382            >(array, to_tz, cast_options),
1383            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1384                i64,
1385                TimestampMicrosecondType,
1386            >(array, to_tz, cast_options),
1387            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1388                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1389            }
1390            Interval(IntervalUnit::YearMonth) => {
1391                cast_string_to_year_month_interval::<i64>(array, cast_options)
1392            }
1393            Interval(IntervalUnit::DayTime) => {
1394                cast_string_to_day_time_interval::<i64>(array, cast_options)
1395            }
1396            Interval(IntervalUnit::MonthDayNano) => {
1397                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1398            }
1399            _ => Err(ArrowError::CastError(format!(
1400                "Casting from {from_type} to {to_type} not supported",
1401            ))),
1402        },
1403        (Binary, _) => match to_type {
1404            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1405            LargeUtf8 => {
1406                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1407                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1408            }
1409            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1410            FixedSizeBinary(size) => {
1411                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1412            }
1413            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1414            Utf8View => Ok(Arc::new(StringViewArray::from(
1415                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1416            ))),
1417            _ => Err(ArrowError::CastError(format!(
1418                "Casting from {from_type} to {to_type} not supported",
1419            ))),
1420        },
1421        (LargeBinary, _) => match to_type {
1422            Utf8 => {
1423                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1424                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1425            }
1426            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1427            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1428            FixedSizeBinary(size) => {
1429                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1430            }
1431            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1432            Utf8View => {
1433                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1434                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1435            }
1436            _ => Err(ArrowError::CastError(format!(
1437                "Casting from {from_type} to {to_type} not supported",
1438            ))),
1439        },
1440        (FixedSizeBinary(size), _) => match to_type {
1441            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1442            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1443            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1444            _ => Err(ArrowError::CastError(format!(
1445                "Casting from {from_type} to {to_type} not supported",
1446            ))),
1447        },
1448        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1449        (BinaryView, LargeBinary) => {
1450            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1451        }
1452        (BinaryView, Utf8) => {
1453            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1454            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1455        }
1456        (BinaryView, LargeUtf8) => {
1457            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1458            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1459        }
1460        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1461        (BinaryView, _) => Err(ArrowError::CastError(format!(
1462            "Casting from {from_type} to {to_type} not supported",
1463        ))),
1464        (from_type, Utf8View) if from_type.is_primitive() => {
1465            value_to_string_view(array, cast_options)
1466        }
1467        (from_type, LargeUtf8) if from_type.is_primitive() => {
1468            value_to_string::<i64>(array, cast_options)
1469        }
1470        (from_type, Utf8) if from_type.is_primitive() => {
1471            value_to_string::<i32>(array, cast_options)
1472        }
1473        (from_type, Binary) if from_type.is_integer() => match from_type {
1474            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1475            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1476            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1477            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1478            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1479            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1480            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1481            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1482            _ => unreachable!(),
1483        },
1484        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1485            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1486            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1487            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1488            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1489            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1490            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1491            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1492            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1493            _ => unreachable!(),
1494        },
1495        // start numeric casts
1496        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1497        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1498        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1499        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1500        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1501        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1502        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1503        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1504        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1505        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1506
1507        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1508        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1509        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1510        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1511        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1512        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1513        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1514        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1515        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1516        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1517
1518        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1519        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1520        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1521        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1522        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1523        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1524        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1525        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1526        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1527        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1528
1529        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1530        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1531        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1532        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1533        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1534        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1535        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1536        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1537        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1538        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1539
1540        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1541        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1542        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1543        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1544        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1545        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1546        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1547        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1548        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1549        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1550
1551        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1552        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1553        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1554        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1555        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1556        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1557        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1558        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1559        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1560        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1561
1562        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1563        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1564        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1565        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1566        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1567        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1568        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1569        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1570        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1571        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1572
1573        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1574        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1575        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1576        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1577        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1578        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1579        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1580        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1581        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1582        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1583
1584        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1585        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1586        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1587        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1588        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1589        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1590        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1591        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1592        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1593        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1594
1595        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1596        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1597        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1598        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1599        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1600        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1601        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1602        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1603        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1604        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1605
1606        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1607        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1608        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1609        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1610        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1611        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1612        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1613        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1614        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1615        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1616        // end numeric casts
1617
1618        // temporal casts
1619        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1620        (Int32, Date64) => cast_with_options(
1621            &cast_with_options(array, &Date32, cast_options)?,
1622            &Date64,
1623            cast_options,
1624        ),
1625        (Int32, Time32(TimeUnit::Second)) => {
1626            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1627        }
1628        (Int32, Time32(TimeUnit::Millisecond)) => {
1629            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1630        }
1631        // No support for microsecond/nanosecond with i32
1632        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1633        (Date32, Int64) => cast_with_options(
1634            &cast_with_options(array, &Int32, cast_options)?,
1635            &Int64,
1636            cast_options,
1637        ),
1638        (Time32(TimeUnit::Second), Int32) => {
1639            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1640        }
1641        (Time32(TimeUnit::Millisecond), Int32) => {
1642            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1643        }
1644        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1645            &cast_with_options(array, &Int32, cast_options)?,
1646            &Int64,
1647            cast_options,
1648        ),
1649        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1650            &cast_with_options(array, &Int32, cast_options)?,
1651            &Int64,
1652            cast_options,
1653        ),
1654        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1655        (Int64, Date32) => cast_with_options(
1656            &cast_with_options(array, &Int32, cast_options)?,
1657            &Date32,
1658            cast_options,
1659        ),
1660        // No support for second/milliseconds with i64
1661        (Int64, Time64(TimeUnit::Microsecond)) => {
1662            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1663        }
1664        (Int64, Time64(TimeUnit::Nanosecond)) => {
1665            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1666        }
1667
1668        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1669        (Date64, Int32) => cast_with_options(
1670            &cast_with_options(array, &Int64, cast_options)?,
1671            &Int32,
1672            cast_options,
1673        ),
1674        (Time64(TimeUnit::Microsecond), Int64) => {
1675            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1676        }
1677        (Time64(TimeUnit::Nanosecond), Int64) => {
1678            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1679        }
1680        (Date32, Date64) => Ok(Arc::new(
1681            array
1682                .as_primitive::<Date32Type>()
1683                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1684        )),
1685        (Date64, Date32) => Ok(Arc::new(
1686            array
1687                .as_primitive::<Date64Type>()
1688                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1689        )),
1690
1691        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1692            array
1693                .as_primitive::<Time32SecondType>()
1694                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1695        )),
1696        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1697            array
1698                .as_primitive::<Time32SecondType>()
1699                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1700        )),
1701        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1702            array
1703                .as_primitive::<Time32SecondType>()
1704                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1705        )),
1706
1707        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1708            array
1709                .as_primitive::<Time32MillisecondType>()
1710                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1711        )),
1712        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1713            array
1714                .as_primitive::<Time32MillisecondType>()
1715                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1716        )),
1717        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1718            array
1719                .as_primitive::<Time32MillisecondType>()
1720                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1721        )),
1722
1723        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1724            array
1725                .as_primitive::<Time64MicrosecondType>()
1726                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1727        )),
1728        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1729            array
1730                .as_primitive::<Time64MicrosecondType>()
1731                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1732        )),
1733        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1734            array
1735                .as_primitive::<Time64MicrosecondType>()
1736                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1737        )),
1738
1739        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1740            array
1741                .as_primitive::<Time64NanosecondType>()
1742                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1743        )),
1744        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1745            array
1746                .as_primitive::<Time64NanosecondType>()
1747                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1748        )),
1749        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1750            array
1751                .as_primitive::<Time64NanosecondType>()
1752                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1753        )),
1754
1755        // Timestamp to integer/floating/decimals
1756        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1757            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1758            cast_with_options(&array, to_type, cast_options)
1759        }
1760        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1761            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1762            cast_with_options(&array, to_type, cast_options)
1763        }
1764        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1765            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1766            cast_with_options(&array, to_type, cast_options)
1767        }
1768        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1769            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1770            cast_with_options(&array, to_type, cast_options)
1771        }
1772
1773        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1774            let array = cast_with_options(array, &Int64, cast_options)?;
1775            Ok(make_timestamp_array(
1776                array.as_primitive(),
1777                *unit,
1778                tz.clone(),
1779            ))
1780        }
1781
1782        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1783            let array = cast_with_options(array, &Int64, cast_options)?;
1784            let time_array = array.as_primitive::<Int64Type>();
1785            let from_size = time_unit_multiple(from_unit);
1786            let to_size = time_unit_multiple(to_unit);
1787            // we either divide or multiply, depending on size of each unit
1788            // units are never the same when the types are the same
1789            let converted = match from_size.cmp(&to_size) {
1790                Ordering::Greater => {
1791                    let divisor = from_size / to_size;
1792                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1793                }
1794                Ordering::Equal => time_array.clone(),
1795                Ordering::Less => {
1796                    let mul = to_size / from_size;
1797                    if cast_options.safe {
1798                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1799                    } else {
1800                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1801                    }
1802                }
1803            };
1804            // Normalize timezone
1805            let adjusted = match (from_tz, to_tz) {
1806                // Only this case needs to be adjusted because we're casting from
1807                // unknown time offset to some time offset, we want the time to be
1808                // unchanged.
1809                //
1810                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1811                (None, Some(to_tz)) => {
1812                    let to_tz: Tz = to_tz.parse()?;
1813                    match to_unit {
1814                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1815                            converted,
1816                            &to_tz,
1817                            cast_options,
1818                        )?,
1819                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1820                            TimestampMillisecondType,
1821                        >(
1822                            converted, &to_tz, cast_options
1823                        )?,
1824                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1825                            TimestampMicrosecondType,
1826                        >(
1827                            converted, &to_tz, cast_options
1828                        )?,
1829                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1830                            TimestampNanosecondType,
1831                        >(
1832                            converted, &to_tz, cast_options
1833                        )?,
1834                    }
1835                }
1836                _ => converted,
1837            };
1838            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1839        }
1840        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1841            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1842        }
1843        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1844            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1845        }
1846        (Timestamp(TimeUnit::Second, _), Date32) => {
1847            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1848        }
1849        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1850            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1851        }
1852        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1853            true => {
1854                // change error to None
1855                array
1856                    .as_primitive::<TimestampSecondType>()
1857                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1858            }
1859            false => array
1860                .as_primitive::<TimestampSecondType>()
1861                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1862        })),
1863        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1864            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1865        }
1866        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1867            array
1868                .as_primitive::<TimestampMicrosecondType>()
1869                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1870        )),
1871        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1872            array
1873                .as_primitive::<TimestampNanosecondType>()
1874                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1875        )),
1876        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1877            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1878            Ok(Arc::new(
1879                array
1880                    .as_primitive::<TimestampSecondType>()
1881                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1882                        Ok(time_to_time64us(as_time_res_with_timezone::<
1883                            TimestampSecondType,
1884                        >(x, tz)?))
1885                    })?,
1886            ))
1887        }
1888        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1889            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1890            Ok(Arc::new(
1891                array
1892                    .as_primitive::<TimestampSecondType>()
1893                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1894                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1895                            TimestampSecondType,
1896                        >(x, tz)?))
1897                    })?,
1898            ))
1899        }
1900        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1901            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1902            Ok(Arc::new(
1903                array
1904                    .as_primitive::<TimestampMillisecondType>()
1905                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1906                        Ok(time_to_time64us(as_time_res_with_timezone::<
1907                            TimestampMillisecondType,
1908                        >(x, tz)?))
1909                    })?,
1910            ))
1911        }
1912        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1913            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1914            Ok(Arc::new(
1915                array
1916                    .as_primitive::<TimestampMillisecondType>()
1917                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1918                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1919                            TimestampMillisecondType,
1920                        >(x, tz)?))
1921                    })?,
1922            ))
1923        }
1924        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1925            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1926            Ok(Arc::new(
1927                array
1928                    .as_primitive::<TimestampMicrosecondType>()
1929                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1930                        Ok(time_to_time64us(as_time_res_with_timezone::<
1931                            TimestampMicrosecondType,
1932                        >(x, tz)?))
1933                    })?,
1934            ))
1935        }
1936        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1937            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1938            Ok(Arc::new(
1939                array
1940                    .as_primitive::<TimestampMicrosecondType>()
1941                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1942                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1943                            TimestampMicrosecondType,
1944                        >(x, tz)?))
1945                    })?,
1946            ))
1947        }
1948        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1949            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1950            Ok(Arc::new(
1951                array
1952                    .as_primitive::<TimestampNanosecondType>()
1953                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1954                        Ok(time_to_time64us(as_time_res_with_timezone::<
1955                            TimestampNanosecondType,
1956                        >(x, tz)?))
1957                    })?,
1958            ))
1959        }
1960        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1961            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1962            Ok(Arc::new(
1963                array
1964                    .as_primitive::<TimestampNanosecondType>()
1965                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1966                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1967                            TimestampNanosecondType,
1968                        >(x, tz)?))
1969                    })?,
1970            ))
1971        }
1972        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1973            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1974            Ok(Arc::new(
1975                array
1976                    .as_primitive::<TimestampSecondType>()
1977                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1978                        Ok(time_to_time32s(as_time_res_with_timezone::<
1979                            TimestampSecondType,
1980                        >(x, tz)?))
1981                    })?,
1982            ))
1983        }
1984        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1985            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1986            Ok(Arc::new(
1987                array
1988                    .as_primitive::<TimestampSecondType>()
1989                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1990                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1991                            TimestampSecondType,
1992                        >(x, tz)?))
1993                    })?,
1994            ))
1995        }
1996        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1997            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1998            Ok(Arc::new(
1999                array
2000                    .as_primitive::<TimestampMillisecondType>()
2001                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2002                        Ok(time_to_time32s(as_time_res_with_timezone::<
2003                            TimestampMillisecondType,
2004                        >(x, tz)?))
2005                    })?,
2006            ))
2007        }
2008        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2009            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2010            Ok(Arc::new(
2011                array
2012                    .as_primitive::<TimestampMillisecondType>()
2013                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2014                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2015                            TimestampMillisecondType,
2016                        >(x, tz)?))
2017                    })?,
2018            ))
2019        }
2020        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2021            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2022            Ok(Arc::new(
2023                array
2024                    .as_primitive::<TimestampMicrosecondType>()
2025                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2026                        Ok(time_to_time32s(as_time_res_with_timezone::<
2027                            TimestampMicrosecondType,
2028                        >(x, tz)?))
2029                    })?,
2030            ))
2031        }
2032        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2033            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2034            Ok(Arc::new(
2035                array
2036                    .as_primitive::<TimestampMicrosecondType>()
2037                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2038                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2039                            TimestampMicrosecondType,
2040                        >(x, tz)?))
2041                    })?,
2042            ))
2043        }
2044        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2045            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2046            Ok(Arc::new(
2047                array
2048                    .as_primitive::<TimestampNanosecondType>()
2049                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2050                        Ok(time_to_time32s(as_time_res_with_timezone::<
2051                            TimestampNanosecondType,
2052                        >(x, tz)?))
2053                    })?,
2054            ))
2055        }
2056        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2057            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2058            Ok(Arc::new(
2059                array
2060                    .as_primitive::<TimestampNanosecondType>()
2061                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2062                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2063                            TimestampNanosecondType,
2064                        >(x, tz)?))
2065                    })?,
2066            ))
2067        }
2068        (Date64, Timestamp(TimeUnit::Second, _)) => {
2069            let array = array
2070                .as_primitive::<Date64Type>()
2071                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2072
2073            cast_with_options(&array, to_type, cast_options)
2074        }
2075        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2076            let array = array
2077                .as_primitive::<Date64Type>()
2078                .reinterpret_cast::<TimestampMillisecondType>();
2079
2080            cast_with_options(&array, to_type, cast_options)
2081        }
2082
2083        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2084            let array = array
2085                .as_primitive::<Date64Type>()
2086                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2087
2088            cast_with_options(&array, to_type, cast_options)
2089        }
2090        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2091            let array = array
2092                .as_primitive::<Date64Type>()
2093                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2094
2095            cast_with_options(&array, to_type, cast_options)
2096        }
2097        (Date32, Timestamp(TimeUnit::Second, _)) => {
2098            let array = array
2099                .as_primitive::<Date32Type>()
2100                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2101
2102            cast_with_options(&array, to_type, cast_options)
2103        }
2104        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2105            let array = array
2106                .as_primitive::<Date32Type>()
2107                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2108
2109            cast_with_options(&array, to_type, cast_options)
2110        }
2111        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2112            let array = array
2113                .as_primitive::<Date32Type>()
2114                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2115
2116            cast_with_options(&array, to_type, cast_options)
2117        }
2118        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2119            let array = array
2120                .as_primitive::<Date32Type>()
2121                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2122
2123            cast_with_options(&array, to_type, cast_options)
2124        }
2125
2126        (_, Duration(unit)) if from_type.is_numeric() => {
2127            let array = cast_with_options(array, &Int64, cast_options)?;
2128            Ok(make_duration_array(array.as_primitive(), *unit))
2129        }
2130        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2131            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2132            cast_with_options(&array, to_type, cast_options)
2133        }
2134        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2135            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2136            cast_with_options(&array, to_type, cast_options)
2137        }
2138        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2139            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2140            cast_with_options(&array, to_type, cast_options)
2141        }
2142        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2143            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2144            cast_with_options(&array, to_type, cast_options)
2145        }
2146
2147        (Duration(from_unit), Duration(to_unit)) => {
2148            let array = cast_with_options(array, &Int64, cast_options)?;
2149            let time_array = array.as_primitive::<Int64Type>();
2150            let from_size = time_unit_multiple(from_unit);
2151            let to_size = time_unit_multiple(to_unit);
2152            // we either divide or multiply, depending on size of each unit
2153            // units are never the same when the types are the same
2154            let converted = match from_size.cmp(&to_size) {
2155                Ordering::Greater => {
2156                    let divisor = from_size / to_size;
2157                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2158                }
2159                Ordering::Equal => time_array.clone(),
2160                Ordering::Less => {
2161                    let mul = to_size / from_size;
2162                    if cast_options.safe {
2163                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2164                    } else {
2165                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2166                    }
2167                }
2168            };
2169            Ok(make_duration_array(&converted, *to_unit))
2170        }
2171
2172        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2173            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2174        }
2175        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2176            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2177        }
2178        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2179            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2180        }
2181        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2182            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2183        }
2184        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2185            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2186        }
2187        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2188            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2189        }
2190        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2191            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2192        }
2193        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2194            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2195        }
2196        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2197            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2198        }
2199        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2200            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2201        }
2202        (Int32, Interval(IntervalUnit::YearMonth)) => {
2203            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2204        }
2205        (_, _) => Err(ArrowError::CastError(format!(
2206            "Casting from {from_type} to {to_type} not supported",
2207        ))),
2208    }
2209}
2210
2211fn cast_struct_to_struct(
2212    array: &StructArray,
2213    from_fields: Fields,
2214    to_fields: Fields,
2215    cast_options: &CastOptions,
2216) -> Result<ArrayRef, ArrowError> {
2217    // Fast path: if field names are in the same order, we can just zip and cast
2218    let fields_match_order = from_fields.len() == to_fields.len()
2219        && from_fields
2220            .iter()
2221            .zip(to_fields.iter())
2222            .all(|(f1, f2)| f1.name() == f2.name());
2223
2224    let fields = if fields_match_order {
2225        // Fast path: cast columns in order if their names match
2226        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2227    } else {
2228        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2229            from_fields
2230                .iter()
2231                .any(|from_field| from_field.name() == to_field.name())
2232        });
2233
2234        if all_fields_match_by_name {
2235            // Slow path: match fields by name and reorder
2236            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2237        } else {
2238            // Fallback: cast field by field in order
2239            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2240        }
2241    };
2242
2243    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2244    Ok(Arc::new(array) as ArrayRef)
2245}
2246
2247fn cast_struct_fields_by_name(
2248    array: &StructArray,
2249    from_fields: Fields,
2250    to_fields: Fields,
2251    cast_options: &CastOptions,
2252) -> Result<Vec<ArrayRef>, ArrowError> {
2253    to_fields
2254        .iter()
2255        .map(|to_field| {
2256            let from_field_idx = from_fields
2257                .iter()
2258                .position(|from_field| from_field.name() == to_field.name())
2259                .unwrap(); // safe because we checked above
2260            let column = array.column(from_field_idx);
2261            cast_with_options(column, to_field.data_type(), cast_options)
2262        })
2263        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2264}
2265
2266fn cast_struct_fields_in_order(
2267    array: &StructArray,
2268    to_fields: Fields,
2269    cast_options: &CastOptions,
2270) -> Result<Vec<ArrayRef>, ArrowError> {
2271    array
2272        .columns()
2273        .iter()
2274        .zip(to_fields.iter())
2275        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2276        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2277}
2278
2279fn cast_from_decimal<D, F>(
2280    array: &dyn Array,
2281    base: D::Native,
2282    scale: &i8,
2283    from_type: &DataType,
2284    to_type: &DataType,
2285    as_float: F,
2286    cast_options: &CastOptions,
2287) -> Result<ArrayRef, ArrowError>
2288where
2289    D: DecimalType + ArrowPrimitiveType,
2290    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2291    F: Fn(D::Native) -> f64,
2292{
2293    use DataType::*;
2294    // cast decimal to other type
2295    match to_type {
2296        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2297        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2298        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2299        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2300        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2301        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2302        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2303        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2304        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2305            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2306        }),
2307        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2308            as_float(x) / 10_f64.powi(*scale as i32)
2309        }),
2310        Utf8View => value_to_string_view(array, cast_options),
2311        Utf8 => value_to_string::<i32>(array, cast_options),
2312        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2313        Null => Ok(new_null_array(to_type, array.len())),
2314        _ => Err(ArrowError::CastError(format!(
2315            "Casting from {from_type} to {to_type} not supported"
2316        ))),
2317    }
2318}
2319
2320fn cast_to_decimal<D, M>(
2321    array: &dyn Array,
2322    base: M,
2323    precision: &u8,
2324    scale: &i8,
2325    from_type: &DataType,
2326    to_type: &DataType,
2327    cast_options: &CastOptions,
2328) -> Result<ArrayRef, ArrowError>
2329where
2330    D: DecimalType + ArrowPrimitiveType<Native = M>,
2331    M: ArrowNativeTypeOp + DecimalCast,
2332    u8: num_traits::AsPrimitive<M>,
2333    u16: num_traits::AsPrimitive<M>,
2334    u32: num_traits::AsPrimitive<M>,
2335    u64: num_traits::AsPrimitive<M>,
2336    i8: num_traits::AsPrimitive<M>,
2337    i16: num_traits::AsPrimitive<M>,
2338    i32: num_traits::AsPrimitive<M>,
2339    i64: num_traits::AsPrimitive<M>,
2340{
2341    use DataType::*;
2342    // cast data to decimal
2343    match from_type {
2344        UInt8 => cast_integer_to_decimal::<_, D, M>(
2345            array.as_primitive::<UInt8Type>(),
2346            *precision,
2347            *scale,
2348            base,
2349            cast_options,
2350        ),
2351        UInt16 => cast_integer_to_decimal::<_, D, _>(
2352            array.as_primitive::<UInt16Type>(),
2353            *precision,
2354            *scale,
2355            base,
2356            cast_options,
2357        ),
2358        UInt32 => cast_integer_to_decimal::<_, D, _>(
2359            array.as_primitive::<UInt32Type>(),
2360            *precision,
2361            *scale,
2362            base,
2363            cast_options,
2364        ),
2365        UInt64 => cast_integer_to_decimal::<_, D, _>(
2366            array.as_primitive::<UInt64Type>(),
2367            *precision,
2368            *scale,
2369            base,
2370            cast_options,
2371        ),
2372        Int8 => cast_integer_to_decimal::<_, D, _>(
2373            array.as_primitive::<Int8Type>(),
2374            *precision,
2375            *scale,
2376            base,
2377            cast_options,
2378        ),
2379        Int16 => cast_integer_to_decimal::<_, D, _>(
2380            array.as_primitive::<Int16Type>(),
2381            *precision,
2382            *scale,
2383            base,
2384            cast_options,
2385        ),
2386        Int32 => cast_integer_to_decimal::<_, D, _>(
2387            array.as_primitive::<Int32Type>(),
2388            *precision,
2389            *scale,
2390            base,
2391            cast_options,
2392        ),
2393        Int64 => cast_integer_to_decimal::<_, D, _>(
2394            array.as_primitive::<Int64Type>(),
2395            *precision,
2396            *scale,
2397            base,
2398            cast_options,
2399        ),
2400        Float32 => cast_floating_point_to_decimal::<_, D>(
2401            array.as_primitive::<Float32Type>(),
2402            *precision,
2403            *scale,
2404            cast_options,
2405        ),
2406        Float64 => cast_floating_point_to_decimal::<_, D>(
2407            array.as_primitive::<Float64Type>(),
2408            *precision,
2409            *scale,
2410            cast_options,
2411        ),
2412        Utf8View | Utf8 => {
2413            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2414        }
2415        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2416        Null => Ok(new_null_array(to_type, array.len())),
2417        _ => Err(ArrowError::CastError(format!(
2418            "Casting from {from_type} to {to_type} not supported"
2419        ))),
2420    }
2421}
2422
2423/// Get the time unit as a multiple of a second
2424const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2425    match unit {
2426        TimeUnit::Second => 1,
2427        TimeUnit::Millisecond => MILLISECONDS,
2428        TimeUnit::Microsecond => MICROSECONDS,
2429        TimeUnit::Nanosecond => NANOSECONDS,
2430    }
2431}
2432
2433/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2434fn cast_numeric_arrays<FROM, TO>(
2435    from: &dyn Array,
2436    cast_options: &CastOptions,
2437) -> Result<ArrayRef, ArrowError>
2438where
2439    FROM: ArrowPrimitiveType,
2440    TO: ArrowPrimitiveType,
2441    FROM::Native: NumCast,
2442    TO::Native: NumCast,
2443{
2444    if cast_options.safe {
2445        // If the value can't be casted to the `TO::Native`, return null
2446        Ok(Arc::new(numeric_cast::<FROM, TO>(
2447            from.as_primitive::<FROM>(),
2448        )))
2449    } else {
2450        // If the value can't be casted to the `TO::Native`, return error
2451        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2452            from.as_primitive::<FROM>(),
2453        )?))
2454    }
2455}
2456
2457// Natural cast between numeric types
2458// If the value of T can't be casted to R, will throw error
2459fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2460where
2461    T: ArrowPrimitiveType,
2462    R: ArrowPrimitiveType,
2463    T::Native: NumCast,
2464    R::Native: NumCast,
2465{
2466    from.try_unary(|value| {
2467        num_traits::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2468            ArrowError::CastError(format!(
2469                "Can't cast value {:?} to type {}",
2470                value,
2471                R::DATA_TYPE
2472            ))
2473        })
2474    })
2475}
2476
2477// Natural cast between numeric types
2478// If the value of T can't be casted to R, it will be converted to null
2479fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2480where
2481    T: ArrowPrimitiveType,
2482    R: ArrowPrimitiveType,
2483    T::Native: NumCast,
2484    R::Native: NumCast,
2485{
2486    from.unary_opt::<_, R>(num_traits::cast::cast::<T::Native, R::Native>)
2487}
2488
2489fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2490    array: &dyn Array,
2491) -> Result<ArrayRef, ArrowError> {
2492    let array = array.as_primitive::<FROM>();
2493    let size = std::mem::size_of::<FROM::Native>();
2494    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2495    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2496        offsets,
2497        array.values().inner().clone(),
2498        array.nulls().cloned(),
2499    )?))
2500}
2501
2502fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2503    array: PrimitiveArray<Int64Type>,
2504    to_tz: &Tz,
2505    cast_options: &CastOptions,
2506) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2507    let adjust = |o| {
2508        let local = as_datetime::<T>(o)?;
2509        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2510        T::make_value(local - offset.fix())
2511    };
2512    let adjusted = if cast_options.safe {
2513        array.unary_opt::<_, Int64Type>(adjust)
2514    } else {
2515        array.try_unary::<_, Int64Type, _>(|o| {
2516            adjust(o).ok_or_else(|| {
2517                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2518            })
2519        })?
2520    };
2521    Ok(adjusted)
2522}
2523
2524/// Cast numeric types to Boolean
2525///
2526/// Any zero value returns `false` while non-zero returns `true`
2527fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2528where
2529    FROM: ArrowPrimitiveType,
2530{
2531    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2532}
2533
2534fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2535where
2536    T: ArrowPrimitiveType + ArrowPrimitiveType,
2537{
2538    let mut b = BooleanBuilder::with_capacity(from.len());
2539
2540    for i in 0..from.len() {
2541        if from.is_null(i) {
2542            b.append_null();
2543        } else if from.value(i) != T::default_value() {
2544            b.append_value(true);
2545        } else {
2546            b.append_value(false);
2547        }
2548    }
2549
2550    Ok(b.finish())
2551}
2552
2553/// Cast Boolean types to numeric
2554///
2555/// `false` returns 0 while `true` returns 1
2556fn cast_bool_to_numeric<TO>(
2557    from: &dyn Array,
2558    cast_options: &CastOptions,
2559) -> Result<ArrayRef, ArrowError>
2560where
2561    TO: ArrowPrimitiveType,
2562    TO::Native: num_traits::cast::NumCast,
2563{
2564    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2565        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2566        cast_options,
2567    )))
2568}
2569
2570fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2571where
2572    T: ArrowPrimitiveType,
2573    T::Native: num_traits::NumCast,
2574{
2575    let iter = (0..from.len()).map(|i| {
2576        if from.is_null(i) {
2577            None
2578        } else if from.value(i) {
2579            // a workaround to cast a primitive to T::Native, infallible
2580            num_traits::cast::cast(1)
2581        } else {
2582            Some(T::default_value())
2583        }
2584    });
2585    // Benefit:
2586    //     20% performance improvement
2587    // Soundness:
2588    //     The iterator is trustedLen because it comes from a Range
2589    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2590}
2591
2592/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2593fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2594    array: &dyn Array,
2595    byte_width: i32,
2596    cast_options: &CastOptions,
2597) -> Result<ArrayRef, ArrowError> {
2598    let array = array.as_binary::<O>();
2599    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2600
2601    for i in 0..array.len() {
2602        if array.is_null(i) {
2603            builder.append_null();
2604        } else {
2605            match builder.append_value(array.value(i)) {
2606                Ok(_) => {}
2607                Err(e) => match cast_options.safe {
2608                    true => builder.append_null(),
2609                    false => return Err(e),
2610                },
2611            }
2612        }
2613    }
2614
2615    Ok(Arc::new(builder.finish()))
2616}
2617
2618/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2619/// If the target one is too large for the source array it will return an Error.
2620fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2621    array: &dyn Array,
2622    byte_width: i32,
2623) -> Result<ArrayRef, ArrowError> {
2624    let array = array
2625        .as_any()
2626        .downcast_ref::<FixedSizeBinaryArray>()
2627        .unwrap();
2628
2629    let offsets: i128 = byte_width as i128 * array.len() as i128;
2630
2631    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2632    if is_binary && offsets > i32::MAX as i128 {
2633        return Err(ArrowError::ComputeError(
2634            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2635        ));
2636    } else if !is_binary && offsets > i64::MAX as i128 {
2637        return Err(ArrowError::ComputeError(
2638            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2639        ));
2640    }
2641
2642    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2643
2644    for i in 0..array.len() {
2645        if array.is_null(i) {
2646            builder.append_null();
2647        } else {
2648            builder.append_value(array.value(i));
2649        }
2650    }
2651
2652    Ok(Arc::new(builder.finish()))
2653}
2654
2655fn cast_fixed_size_binary_to_binary_view(
2656    array: &dyn Array,
2657    _byte_width: i32,
2658) -> Result<ArrayRef, ArrowError> {
2659    let array = array
2660        .as_any()
2661        .downcast_ref::<FixedSizeBinaryArray>()
2662        .unwrap();
2663
2664    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2665    for i in 0..array.len() {
2666        if array.is_null(i) {
2667            builder.append_null();
2668        } else {
2669            builder.append_value(array.value(i));
2670        }
2671    }
2672
2673    Ok(Arc::new(builder.finish()))
2674}
2675
2676/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2677/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2678fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2679where
2680    FROM: ByteArrayType,
2681    TO: ByteArrayType<Native = FROM::Native>,
2682    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2683    TO::Offset: OffsetSizeTrait + NumCast,
2684{
2685    let data = array.to_data();
2686    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2687    let str_values_buf = data.buffers()[1].clone();
2688    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2689
2690    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2691    offsets
2692        .iter()
2693        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2694            let offset =
2695                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2696                    ArrowError::ComputeError(format!(
2697                        "{}{} array too large to cast to {}{} array",
2698                        FROM::Offset::PREFIX,
2699                        FROM::PREFIX,
2700                        TO::Offset::PREFIX,
2701                        TO::PREFIX
2702                    ))
2703                })?;
2704            offset_builder.append(offset);
2705            Ok(())
2706        })?;
2707
2708    let offset_buffer = offset_builder.finish();
2709
2710    let dtype = TO::DATA_TYPE;
2711
2712    let builder = ArrayData::builder(dtype)
2713        .offset(array.offset())
2714        .len(array.len())
2715        .add_buffer(offset_buffer)
2716        .add_buffer(str_values_buf)
2717        .nulls(data.nulls().cloned());
2718
2719    let array_data = unsafe { builder.build_unchecked() };
2720
2721    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2722}
2723
2724/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2725fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2726where
2727    FROM: ByteViewType,
2728    TO: ByteArrayType,
2729    FROM::Native: AsRef<TO::Native>,
2730{
2731    let data = array.to_data();
2732    let view_array = GenericByteViewArray::<FROM>::from(data);
2733
2734    let len = view_array.len();
2735    let bytes = view_array
2736        .views()
2737        .iter()
2738        .map(|v| ByteView::from(*v).length as usize)
2739        .sum::<usize>();
2740
2741    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2742
2743    for val in view_array.iter() {
2744        byte_array_builder.append_option(val);
2745    }
2746
2747    Ok(Arc::new(byte_array_builder.finish()))
2748}
2749
2750#[cfg(test)]
2751mod tests {
2752    use super::*;
2753    use DataType::*;
2754    use arrow_array::{Int64Array, RunArray, StringArray};
2755    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2756    use arrow_buffer::{ScalarBuffer, i256};
2757    use arrow_schema::{DataType, Field};
2758    use chrono::NaiveDate;
2759    use half::f16;
2760    use std::sync::Arc;
2761
2762    #[derive(Clone)]
2763    struct DecimalCastTestConfig {
2764        input_prec: u8,
2765        input_scale: i8,
2766        input_repr: i128,
2767        output_prec: u8,
2768        output_scale: i8,
2769        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2770                                                    // template where the "{}" will be
2771                                                    // replaced with the decimal type name
2772                                                    // (e.g. Decimal128)
2773    }
2774
2775    macro_rules! generate_cast_test_case {
2776        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2777            let output =
2778                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2779
2780            // assert cast type
2781            let input_array_type = $INPUT_ARRAY.data_type();
2782            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2783            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2784            assert_eq!($OUTPUT_TYPE, result.data_type());
2785            assert_eq!(result.as_ref(), &output);
2786
2787            let cast_option = CastOptions {
2788                safe: false,
2789                format_options: FormatOptions::default(),
2790            };
2791            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2792            assert_eq!($OUTPUT_TYPE, result.data_type());
2793            assert_eq!(result.as_ref(), &output);
2794        };
2795    }
2796
2797    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2798    where
2799        I: DecimalType,
2800        O: DecimalType,
2801        I::Native: DecimalCast,
2802        O::Native: DecimalCast,
2803    {
2804        let array = vec![I::Native::from_decimal(t.input_repr)];
2805        let array = array
2806            .into_iter()
2807            .collect::<PrimitiveArray<I>>()
2808            .with_precision_and_scale(t.input_prec, t.input_scale)
2809            .unwrap();
2810        let input_type = array.data_type();
2811        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2812        assert!(can_cast_types(input_type, &output_type));
2813
2814        let options = CastOptions {
2815            safe: false,
2816            ..Default::default()
2817        };
2818        let result = cast_with_options(&array, &output_type, &options);
2819
2820        match t.expected_output_repr {
2821            Ok(v) => {
2822                let expected_array = vec![O::Native::from_decimal(v)];
2823                let expected_array = expected_array
2824                    .into_iter()
2825                    .collect::<PrimitiveArray<O>>()
2826                    .with_precision_and_scale(t.output_prec, t.output_scale)
2827                    .unwrap();
2828                assert_eq!(*result.unwrap(), expected_array);
2829            }
2830            Err(expected_output_message_template) => {
2831                assert!(result.is_err());
2832                let expected_error_message =
2833                    expected_output_message_template.replace("{}", O::PREFIX);
2834                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2835            }
2836        }
2837    }
2838
2839    fn create_decimal32_array(
2840        array: Vec<Option<i32>>,
2841        precision: u8,
2842        scale: i8,
2843    ) -> Result<Decimal32Array, ArrowError> {
2844        array
2845            .into_iter()
2846            .collect::<Decimal32Array>()
2847            .with_precision_and_scale(precision, scale)
2848    }
2849
2850    fn create_decimal64_array(
2851        array: Vec<Option<i64>>,
2852        precision: u8,
2853        scale: i8,
2854    ) -> Result<Decimal64Array, ArrowError> {
2855        array
2856            .into_iter()
2857            .collect::<Decimal64Array>()
2858            .with_precision_and_scale(precision, scale)
2859    }
2860
2861    fn create_decimal128_array(
2862        array: Vec<Option<i128>>,
2863        precision: u8,
2864        scale: i8,
2865    ) -> Result<Decimal128Array, ArrowError> {
2866        array
2867            .into_iter()
2868            .collect::<Decimal128Array>()
2869            .with_precision_and_scale(precision, scale)
2870    }
2871
2872    fn create_decimal256_array(
2873        array: Vec<Option<i256>>,
2874        precision: u8,
2875        scale: i8,
2876    ) -> Result<Decimal256Array, ArrowError> {
2877        array
2878            .into_iter()
2879            .collect::<Decimal256Array>()
2880            .with_precision_and_scale(precision, scale)
2881    }
2882
2883    #[test]
2884    #[cfg(not(feature = "force_validate"))]
2885    #[should_panic(
2886        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2887    )]
2888    fn test_cast_decimal_to_decimal_round_with_error() {
2889        // decimal256 to decimal128 overflow
2890        let array = vec![
2891            Some(i256::from_i128(1123454)),
2892            Some(i256::from_i128(2123456)),
2893            Some(i256::from_i128(-3123453)),
2894            Some(i256::from_i128(-3123456)),
2895            None,
2896            Some(i256::MAX),
2897            Some(i256::MIN),
2898        ];
2899        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2900        let array = Arc::new(input_decimal_array) as ArrayRef;
2901        let input_type = DataType::Decimal256(76, 4);
2902        let output_type = DataType::Decimal128(20, 3);
2903        assert!(can_cast_types(&input_type, &output_type));
2904        generate_cast_test_case!(
2905            &array,
2906            Decimal128Array,
2907            &output_type,
2908            vec![
2909                Some(112345_i128),
2910                Some(212346_i128),
2911                Some(-312345_i128),
2912                Some(-312346_i128),
2913                None,
2914                None,
2915                None,
2916            ]
2917        );
2918    }
2919
2920    #[test]
2921    #[cfg(not(feature = "force_validate"))]
2922    fn test_cast_decimal_to_decimal_round() {
2923        let array = vec![
2924            Some(1123454),
2925            Some(2123456),
2926            Some(-3123453),
2927            Some(-3123456),
2928            None,
2929        ];
2930        let array = create_decimal128_array(array, 20, 4).unwrap();
2931        // decimal128 to decimal128
2932        let input_type = DataType::Decimal128(20, 4);
2933        let output_type = DataType::Decimal128(20, 3);
2934        assert!(can_cast_types(&input_type, &output_type));
2935        generate_cast_test_case!(
2936            &array,
2937            Decimal128Array,
2938            &output_type,
2939            vec![
2940                Some(112345_i128),
2941                Some(212346_i128),
2942                Some(-312345_i128),
2943                Some(-312346_i128),
2944                None
2945            ]
2946        );
2947
2948        // decimal128 to decimal256
2949        let input_type = DataType::Decimal128(20, 4);
2950        let output_type = DataType::Decimal256(20, 3);
2951        assert!(can_cast_types(&input_type, &output_type));
2952        generate_cast_test_case!(
2953            &array,
2954            Decimal256Array,
2955            &output_type,
2956            vec![
2957                Some(i256::from_i128(112345_i128)),
2958                Some(i256::from_i128(212346_i128)),
2959                Some(i256::from_i128(-312345_i128)),
2960                Some(i256::from_i128(-312346_i128)),
2961                None
2962            ]
2963        );
2964
2965        // decimal256
2966        let array = vec![
2967            Some(i256::from_i128(1123454)),
2968            Some(i256::from_i128(2123456)),
2969            Some(i256::from_i128(-3123453)),
2970            Some(i256::from_i128(-3123456)),
2971            None,
2972        ];
2973        let array = create_decimal256_array(array, 20, 4).unwrap();
2974
2975        // decimal256 to decimal256
2976        let input_type = DataType::Decimal256(20, 4);
2977        let output_type = DataType::Decimal256(20, 3);
2978        assert!(can_cast_types(&input_type, &output_type));
2979        generate_cast_test_case!(
2980            &array,
2981            Decimal256Array,
2982            &output_type,
2983            vec![
2984                Some(i256::from_i128(112345_i128)),
2985                Some(i256::from_i128(212346_i128)),
2986                Some(i256::from_i128(-312345_i128)),
2987                Some(i256::from_i128(-312346_i128)),
2988                None
2989            ]
2990        );
2991        // decimal256 to decimal128
2992        let input_type = DataType::Decimal256(20, 4);
2993        let output_type = DataType::Decimal128(20, 3);
2994        assert!(can_cast_types(&input_type, &output_type));
2995        generate_cast_test_case!(
2996            &array,
2997            Decimal128Array,
2998            &output_type,
2999            vec![
3000                Some(112345_i128),
3001                Some(212346_i128),
3002                Some(-312345_i128),
3003                Some(-312346_i128),
3004                None
3005            ]
3006        );
3007    }
3008
3009    #[test]
3010    fn test_cast_decimal32_to_decimal32() {
3011        // test changing precision
3012        let input_type = DataType::Decimal32(9, 3);
3013        let output_type = DataType::Decimal32(9, 4);
3014        assert!(can_cast_types(&input_type, &output_type));
3015        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3016        let array = create_decimal32_array(array, 9, 3).unwrap();
3017        generate_cast_test_case!(
3018            &array,
3019            Decimal32Array,
3020            &output_type,
3021            vec![
3022                Some(11234560_i32),
3023                Some(21234560_i32),
3024                Some(31234560_i32),
3025                None
3026            ]
3027        );
3028        // negative test
3029        let array = vec![Some(123456), None];
3030        let array = create_decimal32_array(array, 9, 0).unwrap();
3031        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3032        assert!(result_safe.is_ok());
3033        let options = CastOptions {
3034            safe: false,
3035            ..Default::default()
3036        };
3037
3038        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3039        assert_eq!(
3040            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3041            result_unsafe.unwrap_err().to_string()
3042        );
3043    }
3044
3045    #[test]
3046    fn test_cast_decimal64_to_decimal64() {
3047        // test changing precision
3048        let input_type = DataType::Decimal64(17, 3);
3049        let output_type = DataType::Decimal64(17, 4);
3050        assert!(can_cast_types(&input_type, &output_type));
3051        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3052        let array = create_decimal64_array(array, 17, 3).unwrap();
3053        generate_cast_test_case!(
3054            &array,
3055            Decimal64Array,
3056            &output_type,
3057            vec![
3058                Some(11234560_i64),
3059                Some(21234560_i64),
3060                Some(31234560_i64),
3061                None
3062            ]
3063        );
3064        // negative test
3065        let array = vec![Some(123456), None];
3066        let array = create_decimal64_array(array, 9, 0).unwrap();
3067        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3068        assert!(result_safe.is_ok());
3069        let options = CastOptions {
3070            safe: false,
3071            ..Default::default()
3072        };
3073
3074        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3075        assert_eq!(
3076            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3077            result_unsafe.unwrap_err().to_string()
3078        );
3079    }
3080
3081    #[test]
3082    fn test_cast_decimal128_to_decimal128() {
3083        // test changing precision
3084        let input_type = DataType::Decimal128(20, 3);
3085        let output_type = DataType::Decimal128(20, 4);
3086        assert!(can_cast_types(&input_type, &output_type));
3087        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3088        let array = create_decimal128_array(array, 20, 3).unwrap();
3089        generate_cast_test_case!(
3090            &array,
3091            Decimal128Array,
3092            &output_type,
3093            vec![
3094                Some(11234560_i128),
3095                Some(21234560_i128),
3096                Some(31234560_i128),
3097                None
3098            ]
3099        );
3100        // negative test
3101        let array = vec![Some(123456), None];
3102        let array = create_decimal128_array(array, 10, 0).unwrap();
3103        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3104        assert!(result_safe.is_ok());
3105        let options = CastOptions {
3106            safe: false,
3107            ..Default::default()
3108        };
3109
3110        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3111        assert_eq!(
3112            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3113            result_unsafe.unwrap_err().to_string()
3114        );
3115    }
3116
3117    #[test]
3118    fn test_cast_decimal32_to_decimal32_dict() {
3119        let p = 9;
3120        let s = 3;
3121        let input_type = DataType::Decimal32(p, s);
3122        let output_type = DataType::Dictionary(
3123            Box::new(DataType::Int32),
3124            Box::new(DataType::Decimal32(p, s)),
3125        );
3126        assert!(can_cast_types(&input_type, &output_type));
3127        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3128        let array = create_decimal32_array(array, p, s).unwrap();
3129        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3130        assert_eq!(cast_array.data_type(), &output_type);
3131    }
3132
3133    #[test]
3134    fn test_cast_decimal64_to_decimal64_dict() {
3135        let p = 15;
3136        let s = 3;
3137        let input_type = DataType::Decimal64(p, s);
3138        let output_type = DataType::Dictionary(
3139            Box::new(DataType::Int32),
3140            Box::new(DataType::Decimal64(p, s)),
3141        );
3142        assert!(can_cast_types(&input_type, &output_type));
3143        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3144        let array = create_decimal64_array(array, p, s).unwrap();
3145        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3146        assert_eq!(cast_array.data_type(), &output_type);
3147    }
3148
3149    #[test]
3150    fn test_cast_decimal128_to_decimal128_dict() {
3151        let p = 20;
3152        let s = 3;
3153        let input_type = DataType::Decimal128(p, s);
3154        let output_type = DataType::Dictionary(
3155            Box::new(DataType::Int32),
3156            Box::new(DataType::Decimal128(p, s)),
3157        );
3158        assert!(can_cast_types(&input_type, &output_type));
3159        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3160        let array = create_decimal128_array(array, p, s).unwrap();
3161        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3162        assert_eq!(cast_array.data_type(), &output_type);
3163    }
3164
3165    #[test]
3166    fn test_cast_decimal256_to_decimal256_dict() {
3167        let p = 20;
3168        let s = 3;
3169        let input_type = DataType::Decimal256(p, s);
3170        let output_type = DataType::Dictionary(
3171            Box::new(DataType::Int32),
3172            Box::new(DataType::Decimal256(p, s)),
3173        );
3174        assert!(can_cast_types(&input_type, &output_type));
3175        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3176        let array = create_decimal128_array(array, p, s).unwrap();
3177        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3178        assert_eq!(cast_array.data_type(), &output_type);
3179    }
3180
3181    #[test]
3182    fn test_cast_decimal32_to_decimal32_overflow() {
3183        let input_type = DataType::Decimal32(9, 3);
3184        let output_type = DataType::Decimal32(9, 9);
3185        assert!(can_cast_types(&input_type, &output_type));
3186
3187        let array = vec![Some(i32::MAX)];
3188        let array = create_decimal32_array(array, 9, 3).unwrap();
3189        let result = cast_with_options(
3190            &array,
3191            &output_type,
3192            &CastOptions {
3193                safe: false,
3194                format_options: FormatOptions::default(),
3195            },
3196        );
3197        assert_eq!(
3198            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3199            result.unwrap_err().to_string()
3200        );
3201    }
3202
3203    #[test]
3204    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3205        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3206        let array = create_decimal32_array(array, 9, 3).unwrap();
3207
3208        // Divide out all digits of precision -- rounding could still produce +/- 1
3209        let output_type = DataType::Decimal32(9, -6);
3210        assert!(can_cast_types(array.data_type(), &output_type));
3211        generate_cast_test_case!(
3212            &array,
3213            Decimal32Array,
3214            &output_type,
3215            vec![Some(-1), Some(0), Some(1), None]
3216        );
3217
3218        // Divide out more digits than we have precision -- all-zero result
3219        let output_type = DataType::Decimal32(9, -7);
3220        assert!(can_cast_types(array.data_type(), &output_type));
3221        generate_cast_test_case!(
3222            &array,
3223            Decimal32Array,
3224            &output_type,
3225            vec![Some(0), Some(0), Some(0), None]
3226        );
3227    }
3228
3229    #[test]
3230    fn test_cast_decimal64_to_decimal64_overflow() {
3231        let input_type = DataType::Decimal64(18, 3);
3232        let output_type = DataType::Decimal64(18, 18);
3233        assert!(can_cast_types(&input_type, &output_type));
3234
3235        let array = vec![Some(i64::MAX)];
3236        let array = create_decimal64_array(array, 18, 3).unwrap();
3237        let result = cast_with_options(
3238            &array,
3239            &output_type,
3240            &CastOptions {
3241                safe: false,
3242                format_options: FormatOptions::default(),
3243            },
3244        );
3245        assert_eq!(
3246            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3247            result.unwrap_err().to_string()
3248        );
3249    }
3250
3251    #[test]
3252    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3253        let array = vec![
3254            Some(-999999999999999999),
3255            Some(0),
3256            Some(999999999999999999),
3257            None,
3258        ];
3259        let array = create_decimal64_array(array, 18, 3).unwrap();
3260
3261        // Divide out all digits of precision -- rounding could still produce +/- 1
3262        let output_type = DataType::Decimal64(18, -15);
3263        assert!(can_cast_types(array.data_type(), &output_type));
3264        generate_cast_test_case!(
3265            &array,
3266            Decimal64Array,
3267            &output_type,
3268            vec![Some(-1), Some(0), Some(1), None]
3269        );
3270
3271        // Divide out more digits than we have precision -- all-zero result
3272        let output_type = DataType::Decimal64(18, -16);
3273        assert!(can_cast_types(array.data_type(), &output_type));
3274        generate_cast_test_case!(
3275            &array,
3276            Decimal64Array,
3277            &output_type,
3278            vec![Some(0), Some(0), Some(0), None]
3279        );
3280    }
3281
3282    #[test]
3283    fn test_cast_floating_to_decimals() {
3284        for output_type in [
3285            DataType::Decimal32(9, 3),
3286            DataType::Decimal64(9, 3),
3287            DataType::Decimal128(9, 3),
3288            DataType::Decimal256(9, 3),
3289        ] {
3290            let input_type = DataType::Float64;
3291            assert!(can_cast_types(&input_type, &output_type));
3292
3293            let array = vec![Some(1.1_f64)];
3294            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3295            let result = cast_with_options(
3296                &array,
3297                &output_type,
3298                &CastOptions {
3299                    safe: false,
3300                    format_options: FormatOptions::default(),
3301                },
3302            );
3303            assert!(
3304                result.is_ok(),
3305                "Failed to cast to {output_type} with: {}",
3306                result.unwrap_err()
3307            );
3308        }
3309    }
3310
3311    #[test]
3312    fn test_cast_decimal128_to_decimal128_overflow() {
3313        let input_type = DataType::Decimal128(38, 3);
3314        let output_type = DataType::Decimal128(38, 38);
3315        assert!(can_cast_types(&input_type, &output_type));
3316
3317        let array = vec![Some(i128::MAX)];
3318        let array = create_decimal128_array(array, 38, 3).unwrap();
3319        let result = cast_with_options(
3320            &array,
3321            &output_type,
3322            &CastOptions {
3323                safe: false,
3324                format_options: FormatOptions::default(),
3325            },
3326        );
3327        assert_eq!(
3328            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3329            result.unwrap_err().to_string()
3330        );
3331    }
3332
3333    #[test]
3334    fn test_cast_decimal128_to_decimal256_overflow() {
3335        let input_type = DataType::Decimal128(38, 3);
3336        let output_type = DataType::Decimal256(76, 76);
3337        assert!(can_cast_types(&input_type, &output_type));
3338
3339        let array = vec![Some(i128::MAX)];
3340        let array = create_decimal128_array(array, 38, 3).unwrap();
3341        let result = cast_with_options(
3342            &array,
3343            &output_type,
3344            &CastOptions {
3345                safe: false,
3346                format_options: FormatOptions::default(),
3347            },
3348        );
3349        assert_eq!(
3350            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3351            result.unwrap_err().to_string()
3352        );
3353    }
3354
3355    #[test]
3356    fn test_cast_decimal32_to_decimal256() {
3357        let input_type = DataType::Decimal32(8, 3);
3358        let output_type = DataType::Decimal256(20, 4);
3359        assert!(can_cast_types(&input_type, &output_type));
3360        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3361        let array = create_decimal32_array(array, 8, 3).unwrap();
3362        generate_cast_test_case!(
3363            &array,
3364            Decimal256Array,
3365            &output_type,
3366            vec![
3367                Some(i256::from_i128(11234560_i128)),
3368                Some(i256::from_i128(21234560_i128)),
3369                Some(i256::from_i128(31234560_i128)),
3370                None
3371            ]
3372        );
3373    }
3374    #[test]
3375    fn test_cast_decimal64_to_decimal256() {
3376        let input_type = DataType::Decimal64(12, 3);
3377        let output_type = DataType::Decimal256(20, 4);
3378        assert!(can_cast_types(&input_type, &output_type));
3379        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3380        let array = create_decimal64_array(array, 12, 3).unwrap();
3381        generate_cast_test_case!(
3382            &array,
3383            Decimal256Array,
3384            &output_type,
3385            vec![
3386                Some(i256::from_i128(11234560_i128)),
3387                Some(i256::from_i128(21234560_i128)),
3388                Some(i256::from_i128(31234560_i128)),
3389                None
3390            ]
3391        );
3392    }
3393    #[test]
3394    fn test_cast_decimal128_to_decimal256() {
3395        let input_type = DataType::Decimal128(20, 3);
3396        let output_type = DataType::Decimal256(20, 4);
3397        assert!(can_cast_types(&input_type, &output_type));
3398        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3399        let array = create_decimal128_array(array, 20, 3).unwrap();
3400        generate_cast_test_case!(
3401            &array,
3402            Decimal256Array,
3403            &output_type,
3404            vec![
3405                Some(i256::from_i128(11234560_i128)),
3406                Some(i256::from_i128(21234560_i128)),
3407                Some(i256::from_i128(31234560_i128)),
3408                None
3409            ]
3410        );
3411    }
3412
3413    #[test]
3414    fn test_cast_decimal256_to_decimal128_overflow() {
3415        let input_type = DataType::Decimal256(76, 5);
3416        let output_type = DataType::Decimal128(38, 7);
3417        assert!(can_cast_types(&input_type, &output_type));
3418        let array = vec![Some(i256::from_i128(i128::MAX))];
3419        let array = create_decimal256_array(array, 76, 5).unwrap();
3420        let result = cast_with_options(
3421            &array,
3422            &output_type,
3423            &CastOptions {
3424                safe: false,
3425                format_options: FormatOptions::default(),
3426            },
3427        );
3428        assert_eq!(
3429            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3430            result.unwrap_err().to_string()
3431        );
3432    }
3433
3434    #[test]
3435    fn test_cast_decimal256_to_decimal256_overflow() {
3436        let input_type = DataType::Decimal256(76, 5);
3437        let output_type = DataType::Decimal256(76, 55);
3438        assert!(can_cast_types(&input_type, &output_type));
3439        let array = vec![Some(i256::from_i128(i128::MAX))];
3440        let array = create_decimal256_array(array, 76, 5).unwrap();
3441        let result = cast_with_options(
3442            &array,
3443            &output_type,
3444            &CastOptions {
3445                safe: false,
3446                format_options: FormatOptions::default(),
3447            },
3448        );
3449        assert_eq!(
3450            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3451            result.unwrap_err().to_string()
3452        );
3453    }
3454
3455    #[test]
3456    fn test_cast_decimal256_to_decimal128() {
3457        let input_type = DataType::Decimal256(20, 3);
3458        let output_type = DataType::Decimal128(20, 4);
3459        assert!(can_cast_types(&input_type, &output_type));
3460        let array = vec![
3461            Some(i256::from_i128(1123456)),
3462            Some(i256::from_i128(2123456)),
3463            Some(i256::from_i128(3123456)),
3464            None,
3465        ];
3466        let array = create_decimal256_array(array, 20, 3).unwrap();
3467        generate_cast_test_case!(
3468            &array,
3469            Decimal128Array,
3470            &output_type,
3471            vec![
3472                Some(11234560_i128),
3473                Some(21234560_i128),
3474                Some(31234560_i128),
3475                None
3476            ]
3477        );
3478    }
3479
3480    #[test]
3481    fn test_cast_decimal256_to_decimal256() {
3482        let input_type = DataType::Decimal256(20, 3);
3483        let output_type = DataType::Decimal256(20, 4);
3484        assert!(can_cast_types(&input_type, &output_type));
3485        let array = vec![
3486            Some(i256::from_i128(1123456)),
3487            Some(i256::from_i128(2123456)),
3488            Some(i256::from_i128(3123456)),
3489            None,
3490        ];
3491        let array = create_decimal256_array(array, 20, 3).unwrap();
3492        generate_cast_test_case!(
3493            &array,
3494            Decimal256Array,
3495            &output_type,
3496            vec![
3497                Some(i256::from_i128(11234560_i128)),
3498                Some(i256::from_i128(21234560_i128)),
3499                Some(i256::from_i128(31234560_i128)),
3500                None
3501            ]
3502        );
3503    }
3504
3505    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3506    where
3507        T: ArrowPrimitiveType + DecimalType,
3508    {
3509        // u8
3510        generate_cast_test_case!(
3511            array,
3512            UInt8Array,
3513            &DataType::UInt8,
3514            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3515        );
3516        // u16
3517        generate_cast_test_case!(
3518            array,
3519            UInt16Array,
3520            &DataType::UInt16,
3521            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3522        );
3523        // u32
3524        generate_cast_test_case!(
3525            array,
3526            UInt32Array,
3527            &DataType::UInt32,
3528            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3529        );
3530        // u64
3531        generate_cast_test_case!(
3532            array,
3533            UInt64Array,
3534            &DataType::UInt64,
3535            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3536        );
3537        // i8
3538        generate_cast_test_case!(
3539            array,
3540            Int8Array,
3541            &DataType::Int8,
3542            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3543        );
3544        // i16
3545        generate_cast_test_case!(
3546            array,
3547            Int16Array,
3548            &DataType::Int16,
3549            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3550        );
3551        // i32
3552        generate_cast_test_case!(
3553            array,
3554            Int32Array,
3555            &DataType::Int32,
3556            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3557        );
3558        // i64
3559        generate_cast_test_case!(
3560            array,
3561            Int64Array,
3562            &DataType::Int64,
3563            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3564        );
3565        // f32
3566        generate_cast_test_case!(
3567            array,
3568            Float32Array,
3569            &DataType::Float32,
3570            vec![
3571                Some(1.25_f32),
3572                Some(2.25_f32),
3573                Some(3.25_f32),
3574                None,
3575                Some(5.25_f32)
3576            ]
3577        );
3578        // f64
3579        generate_cast_test_case!(
3580            array,
3581            Float64Array,
3582            &DataType::Float64,
3583            vec![
3584                Some(1.25_f64),
3585                Some(2.25_f64),
3586                Some(3.25_f64),
3587                None,
3588                Some(5.25_f64)
3589            ]
3590        );
3591    }
3592
3593    #[test]
3594    fn test_cast_decimal32_to_numeric() {
3595        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3596        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3597
3598        generate_decimal_to_numeric_cast_test_case(&array);
3599    }
3600
3601    #[test]
3602    fn test_cast_decimal64_to_numeric() {
3603        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3604        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3605
3606        generate_decimal_to_numeric_cast_test_case(&array);
3607    }
3608
3609    #[test]
3610    fn test_cast_decimal128_to_numeric() {
3611        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3612        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3613
3614        generate_decimal_to_numeric_cast_test_case(&array);
3615
3616        // overflow test: out of range of max u8
3617        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3618        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3619        let casted_array = cast_with_options(
3620            &array,
3621            &DataType::UInt8,
3622            &CastOptions {
3623                safe: false,
3624                format_options: FormatOptions::default(),
3625            },
3626        );
3627        assert_eq!(
3628            "Cast error: value of 513 is out of range UInt8".to_string(),
3629            casted_array.unwrap_err().to_string()
3630        );
3631
3632        let casted_array = cast_with_options(
3633            &array,
3634            &DataType::UInt8,
3635            &CastOptions {
3636                safe: true,
3637                format_options: FormatOptions::default(),
3638            },
3639        );
3640        assert!(casted_array.is_ok());
3641        assert!(casted_array.unwrap().is_null(0));
3642
3643        // overflow test: out of range of max i8
3644        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3645        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3646        let casted_array = cast_with_options(
3647            &array,
3648            &DataType::Int8,
3649            &CastOptions {
3650                safe: false,
3651                format_options: FormatOptions::default(),
3652            },
3653        );
3654        assert_eq!(
3655            "Cast error: value of 244 is out of range Int8".to_string(),
3656            casted_array.unwrap_err().to_string()
3657        );
3658
3659        let casted_array = cast_with_options(
3660            &array,
3661            &DataType::Int8,
3662            &CastOptions {
3663                safe: true,
3664                format_options: FormatOptions::default(),
3665            },
3666        );
3667        assert!(casted_array.is_ok());
3668        assert!(casted_array.unwrap().is_null(0));
3669
3670        // loss the precision: convert decimal to f32、f64
3671        // f32
3672        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3673        let value_array: Vec<Option<i128>> = vec![
3674            Some(125),
3675            Some(225),
3676            Some(325),
3677            None,
3678            Some(525),
3679            Some(112345678),
3680            Some(112345679),
3681        ];
3682        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3683        generate_cast_test_case!(
3684            &array,
3685            Float32Array,
3686            &DataType::Float32,
3687            vec![
3688                Some(1.25_f32),
3689                Some(2.25_f32),
3690                Some(3.25_f32),
3691                None,
3692                Some(5.25_f32),
3693                Some(1_123_456.7_f32),
3694                Some(1_123_456.7_f32)
3695            ]
3696        );
3697
3698        // f64
3699        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3700        let value_array: Vec<Option<i128>> = vec![
3701            Some(125),
3702            Some(225),
3703            Some(325),
3704            None,
3705            Some(525),
3706            Some(112345678901234568),
3707            Some(112345678901234560),
3708        ];
3709        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3710        generate_cast_test_case!(
3711            &array,
3712            Float64Array,
3713            &DataType::Float64,
3714            vec![
3715                Some(1.25_f64),
3716                Some(2.25_f64),
3717                Some(3.25_f64),
3718                None,
3719                Some(5.25_f64),
3720                Some(1_123_456_789_012_345.6_f64),
3721                Some(1_123_456_789_012_345.6_f64),
3722            ]
3723        );
3724    }
3725
3726    #[test]
3727    fn test_cast_decimal256_to_numeric() {
3728        let value_array: Vec<Option<i256>> = vec![
3729            Some(i256::from_i128(125)),
3730            Some(i256::from_i128(225)),
3731            Some(i256::from_i128(325)),
3732            None,
3733            Some(i256::from_i128(525)),
3734        ];
3735        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3736        // u8
3737        generate_cast_test_case!(
3738            &array,
3739            UInt8Array,
3740            &DataType::UInt8,
3741            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3742        );
3743        // u16
3744        generate_cast_test_case!(
3745            &array,
3746            UInt16Array,
3747            &DataType::UInt16,
3748            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3749        );
3750        // u32
3751        generate_cast_test_case!(
3752            &array,
3753            UInt32Array,
3754            &DataType::UInt32,
3755            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3756        );
3757        // u64
3758        generate_cast_test_case!(
3759            &array,
3760            UInt64Array,
3761            &DataType::UInt64,
3762            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3763        );
3764        // i8
3765        generate_cast_test_case!(
3766            &array,
3767            Int8Array,
3768            &DataType::Int8,
3769            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3770        );
3771        // i16
3772        generate_cast_test_case!(
3773            &array,
3774            Int16Array,
3775            &DataType::Int16,
3776            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3777        );
3778        // i32
3779        generate_cast_test_case!(
3780            &array,
3781            Int32Array,
3782            &DataType::Int32,
3783            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3784        );
3785        // i64
3786        generate_cast_test_case!(
3787            &array,
3788            Int64Array,
3789            &DataType::Int64,
3790            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3791        );
3792        // f32
3793        generate_cast_test_case!(
3794            &array,
3795            Float32Array,
3796            &DataType::Float32,
3797            vec![
3798                Some(1.25_f32),
3799                Some(2.25_f32),
3800                Some(3.25_f32),
3801                None,
3802                Some(5.25_f32)
3803            ]
3804        );
3805        // f64
3806        generate_cast_test_case!(
3807            &array,
3808            Float64Array,
3809            &DataType::Float64,
3810            vec![
3811                Some(1.25_f64),
3812                Some(2.25_f64),
3813                Some(3.25_f64),
3814                None,
3815                Some(5.25_f64)
3816            ]
3817        );
3818
3819        // overflow test: out of range of max i8
3820        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3821        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3822        let casted_array = cast_with_options(
3823            &array,
3824            &DataType::Int8,
3825            &CastOptions {
3826                safe: false,
3827                format_options: FormatOptions::default(),
3828            },
3829        );
3830        assert_eq!(
3831            "Cast error: value of 244 is out of range Int8".to_string(),
3832            casted_array.unwrap_err().to_string()
3833        );
3834
3835        let casted_array = cast_with_options(
3836            &array,
3837            &DataType::Int8,
3838            &CastOptions {
3839                safe: true,
3840                format_options: FormatOptions::default(),
3841            },
3842        );
3843        assert!(casted_array.is_ok());
3844        assert!(casted_array.unwrap().is_null(0));
3845
3846        // loss the precision: convert decimal to f32、f64
3847        // f32
3848        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3849        let value_array: Vec<Option<i256>> = vec![
3850            Some(i256::from_i128(125)),
3851            Some(i256::from_i128(225)),
3852            Some(i256::from_i128(325)),
3853            None,
3854            Some(i256::from_i128(525)),
3855            Some(i256::from_i128(112345678)),
3856            Some(i256::from_i128(112345679)),
3857        ];
3858        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3859        generate_cast_test_case!(
3860            &array,
3861            Float32Array,
3862            &DataType::Float32,
3863            vec![
3864                Some(1.25_f32),
3865                Some(2.25_f32),
3866                Some(3.25_f32),
3867                None,
3868                Some(5.25_f32),
3869                Some(1_123_456.7_f32),
3870                Some(1_123_456.7_f32)
3871            ]
3872        );
3873
3874        // f64
3875        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3876        let value_array: Vec<Option<i256>> = vec![
3877            Some(i256::from_i128(125)),
3878            Some(i256::from_i128(225)),
3879            Some(i256::from_i128(325)),
3880            None,
3881            Some(i256::from_i128(525)),
3882            Some(i256::from_i128(112345678901234568)),
3883            Some(i256::from_i128(112345678901234560)),
3884        ];
3885        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3886        generate_cast_test_case!(
3887            &array,
3888            Float64Array,
3889            &DataType::Float64,
3890            vec![
3891                Some(1.25_f64),
3892                Some(2.25_f64),
3893                Some(3.25_f64),
3894                None,
3895                Some(5.25_f64),
3896                Some(1_123_456_789_012_345.6_f64),
3897                Some(1_123_456_789_012_345.6_f64),
3898            ]
3899        );
3900    }
3901
3902    #[test]
3903    fn test_cast_decimal_to_numeric_negative_scale() {
3904        let value_array: Vec<Option<i256>> = vec![
3905            Some(i256::from_i128(125)),
3906            Some(i256::from_i128(225)),
3907            Some(i256::from_i128(325)),
3908            None,
3909            Some(i256::from_i128(525)),
3910        ];
3911        let array = create_decimal256_array(value_array, 38, -1).unwrap();
3912
3913        generate_cast_test_case!(
3914            &array,
3915            Int64Array,
3916            &DataType::Int64,
3917            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
3918        );
3919
3920        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3921        let array = create_decimal32_array(value_array, 8, -2).unwrap();
3922        generate_cast_test_case!(
3923            &array,
3924            Int64Array,
3925            &DataType::Int64,
3926            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
3927        );
3928
3929        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
3930        let array = create_decimal32_array(value_array, 9, -9).unwrap();
3931        generate_cast_test_case!(
3932            &array,
3933            Int64Array,
3934            &DataType::Int64,
3935            vec![Some(2_000_000_000), Some(1_000_000_000), None]
3936        );
3937
3938        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3939        let array = create_decimal64_array(value_array, 18, -3).unwrap();
3940        generate_cast_test_case!(
3941            &array,
3942            Int64Array,
3943            &DataType::Int64,
3944            vec![
3945                Some(125_000),
3946                Some(225_000),
3947                Some(325_000),
3948                None,
3949                Some(525_000)
3950            ]
3951        );
3952
3953        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
3954        let array = create_decimal64_array(value_array, 18, -10).unwrap();
3955        generate_cast_test_case!(
3956            &array,
3957            Int64Array,
3958            &DataType::Int64,
3959            vec![Some(120_000_000_000), Some(340_000_000_000), None]
3960        );
3961
3962        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3963        let array = create_decimal128_array(value_array, 38, -4).unwrap();
3964        generate_cast_test_case!(
3965            &array,
3966            Int64Array,
3967            &DataType::Int64,
3968            vec![
3969                Some(1_250_000),
3970                Some(2_250_000),
3971                Some(3_250_000),
3972                None,
3973                Some(5_250_000)
3974            ]
3975        );
3976
3977        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
3978        let array = create_decimal128_array(value_array, 38, -18).unwrap();
3979        generate_cast_test_case!(
3980            &array,
3981            Int64Array,
3982            &DataType::Int64,
3983            vec![
3984                Some(9_000_000_000_000_000_000),
3985                Some(1_000_000_000_000_000_000),
3986                None
3987            ]
3988        );
3989
3990        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
3991        let casted_array = cast_with_options(
3992            &array,
3993            &DataType::Int64,
3994            &CastOptions {
3995                safe: false,
3996                format_options: FormatOptions::default(),
3997            },
3998        );
3999        assert_eq!(
4000            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4001            casted_array.unwrap_err().to_string()
4002        );
4003
4004        let casted_array = cast_with_options(
4005            &array,
4006            &DataType::Int64,
4007            &CastOptions {
4008                safe: true,
4009                format_options: FormatOptions::default(),
4010            },
4011        );
4012        assert!(casted_array.is_ok());
4013        assert!(casted_array.unwrap().is_null(0));
4014
4015        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4016        let casted_array = cast_with_options(
4017            &array,
4018            &DataType::Int8,
4019            &CastOptions {
4020                safe: false,
4021                format_options: FormatOptions::default(),
4022            },
4023        );
4024        assert_eq!(
4025            "Cast error: value of 130 is out of range Int8".to_string(),
4026            casted_array.unwrap_err().to_string()
4027        );
4028
4029        let casted_array = cast_with_options(
4030            &array,
4031            &DataType::Int8,
4032            &CastOptions {
4033                safe: true,
4034                format_options: FormatOptions::default(),
4035            },
4036        );
4037        assert!(casted_array.is_ok());
4038        assert!(casted_array.unwrap().is_null(0));
4039    }
4040
4041    #[test]
4042    fn test_cast_numeric_to_decimal128() {
4043        let decimal_type = DataType::Decimal128(38, 6);
4044        // u8, u16, u32, u64
4045        let input_datas = vec![
4046            Arc::new(UInt8Array::from(vec![
4047                Some(1),
4048                Some(2),
4049                Some(3),
4050                None,
4051                Some(5),
4052            ])) as ArrayRef, // u8
4053            Arc::new(UInt16Array::from(vec![
4054                Some(1),
4055                Some(2),
4056                Some(3),
4057                None,
4058                Some(5),
4059            ])) as ArrayRef, // u16
4060            Arc::new(UInt32Array::from(vec![
4061                Some(1),
4062                Some(2),
4063                Some(3),
4064                None,
4065                Some(5),
4066            ])) as ArrayRef, // u32
4067            Arc::new(UInt64Array::from(vec![
4068                Some(1),
4069                Some(2),
4070                Some(3),
4071                None,
4072                Some(5),
4073            ])) as ArrayRef, // u64
4074        ];
4075
4076        for array in input_datas {
4077            generate_cast_test_case!(
4078                &array,
4079                Decimal128Array,
4080                &decimal_type,
4081                vec![
4082                    Some(1000000_i128),
4083                    Some(2000000_i128),
4084                    Some(3000000_i128),
4085                    None,
4086                    Some(5000000_i128)
4087                ]
4088            );
4089        }
4090
4091        // i8, i16, i32, i64
4092        let input_datas = vec![
4093            Arc::new(Int8Array::from(vec![
4094                Some(1),
4095                Some(2),
4096                Some(3),
4097                None,
4098                Some(5),
4099            ])) as ArrayRef, // i8
4100            Arc::new(Int16Array::from(vec![
4101                Some(1),
4102                Some(2),
4103                Some(3),
4104                None,
4105                Some(5),
4106            ])) as ArrayRef, // i16
4107            Arc::new(Int32Array::from(vec![
4108                Some(1),
4109                Some(2),
4110                Some(3),
4111                None,
4112                Some(5),
4113            ])) as ArrayRef, // i32
4114            Arc::new(Int64Array::from(vec![
4115                Some(1),
4116                Some(2),
4117                Some(3),
4118                None,
4119                Some(5),
4120            ])) as ArrayRef, // i64
4121        ];
4122        for array in input_datas {
4123            generate_cast_test_case!(
4124                &array,
4125                Decimal128Array,
4126                &decimal_type,
4127                vec![
4128                    Some(1000000_i128),
4129                    Some(2000000_i128),
4130                    Some(3000000_i128),
4131                    None,
4132                    Some(5000000_i128)
4133                ]
4134            );
4135        }
4136
4137        // test u8 to decimal type with overflow the result type
4138        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4139        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4140        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4141        assert!(casted_array.is_ok());
4142        let array = casted_array.unwrap();
4143        let array: &Decimal128Array = array.as_primitive();
4144        assert!(array.is_null(4));
4145
4146        // test i8 to decimal type with overflow the result type
4147        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4148        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4149        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4150        assert!(casted_array.is_ok());
4151        let array = casted_array.unwrap();
4152        let array: &Decimal128Array = array.as_primitive();
4153        assert!(array.is_null(4));
4154
4155        // test f32 to decimal type
4156        let array = Float32Array::from(vec![
4157            Some(1.1),
4158            Some(2.2),
4159            Some(4.4),
4160            None,
4161            Some(1.123_456_4), // round down
4162            Some(1.123_456_7), // round up
4163        ]);
4164        let array = Arc::new(array) as ArrayRef;
4165        generate_cast_test_case!(
4166            &array,
4167            Decimal128Array,
4168            &decimal_type,
4169            vec![
4170                Some(1100000_i128),
4171                Some(2200000_i128),
4172                Some(4400000_i128),
4173                None,
4174                Some(1123456_i128), // round down
4175                Some(1123457_i128), // round up
4176            ]
4177        );
4178
4179        // test f64 to decimal type
4180        let array = Float64Array::from(vec![
4181            Some(1.1),
4182            Some(2.2),
4183            Some(4.4),
4184            None,
4185            Some(1.123_456_489_123_4),     // round up
4186            Some(1.123_456_789_123_4),     // round up
4187            Some(1.123_456_489_012_345_6), // round down
4188            Some(1.123_456_789_012_345_6), // round up
4189        ]);
4190        generate_cast_test_case!(
4191            &array,
4192            Decimal128Array,
4193            &decimal_type,
4194            vec![
4195                Some(1100000_i128),
4196                Some(2200000_i128),
4197                Some(4400000_i128),
4198                None,
4199                Some(1123456_i128), // round down
4200                Some(1123457_i128), // round up
4201                Some(1123456_i128), // round down
4202                Some(1123457_i128), // round up
4203            ]
4204        );
4205    }
4206
4207    #[test]
4208    fn test_cast_numeric_to_decimal256() {
4209        let decimal_type = DataType::Decimal256(76, 6);
4210        // u8, u16, u32, u64
4211        let input_datas = vec![
4212            Arc::new(UInt8Array::from(vec![
4213                Some(1),
4214                Some(2),
4215                Some(3),
4216                None,
4217                Some(5),
4218            ])) as ArrayRef, // u8
4219            Arc::new(UInt16Array::from(vec![
4220                Some(1),
4221                Some(2),
4222                Some(3),
4223                None,
4224                Some(5),
4225            ])) as ArrayRef, // u16
4226            Arc::new(UInt32Array::from(vec![
4227                Some(1),
4228                Some(2),
4229                Some(3),
4230                None,
4231                Some(5),
4232            ])) as ArrayRef, // u32
4233            Arc::new(UInt64Array::from(vec![
4234                Some(1),
4235                Some(2),
4236                Some(3),
4237                None,
4238                Some(5),
4239            ])) as ArrayRef, // u64
4240        ];
4241
4242        for array in input_datas {
4243            generate_cast_test_case!(
4244                &array,
4245                Decimal256Array,
4246                &decimal_type,
4247                vec![
4248                    Some(i256::from_i128(1000000_i128)),
4249                    Some(i256::from_i128(2000000_i128)),
4250                    Some(i256::from_i128(3000000_i128)),
4251                    None,
4252                    Some(i256::from_i128(5000000_i128))
4253                ]
4254            );
4255        }
4256
4257        // i8, i16, i32, i64
4258        let input_datas = vec![
4259            Arc::new(Int8Array::from(vec![
4260                Some(1),
4261                Some(2),
4262                Some(3),
4263                None,
4264                Some(5),
4265            ])) as ArrayRef, // i8
4266            Arc::new(Int16Array::from(vec![
4267                Some(1),
4268                Some(2),
4269                Some(3),
4270                None,
4271                Some(5),
4272            ])) as ArrayRef, // i16
4273            Arc::new(Int32Array::from(vec![
4274                Some(1),
4275                Some(2),
4276                Some(3),
4277                None,
4278                Some(5),
4279            ])) as ArrayRef, // i32
4280            Arc::new(Int64Array::from(vec![
4281                Some(1),
4282                Some(2),
4283                Some(3),
4284                None,
4285                Some(5),
4286            ])) as ArrayRef, // i64
4287        ];
4288        for array in input_datas {
4289            generate_cast_test_case!(
4290                &array,
4291                Decimal256Array,
4292                &decimal_type,
4293                vec![
4294                    Some(i256::from_i128(1000000_i128)),
4295                    Some(i256::from_i128(2000000_i128)),
4296                    Some(i256::from_i128(3000000_i128)),
4297                    None,
4298                    Some(i256::from_i128(5000000_i128))
4299                ]
4300            );
4301        }
4302
4303        // test i8 to decimal type with overflow the result type
4304        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4305        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4306        let array = Arc::new(array) as ArrayRef;
4307        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4308        assert!(casted_array.is_ok());
4309        let array = casted_array.unwrap();
4310        let array: &Decimal256Array = array.as_primitive();
4311        assert!(array.is_null(4));
4312
4313        // test f32 to decimal type
4314        let array = Float32Array::from(vec![
4315            Some(1.1),
4316            Some(2.2),
4317            Some(4.4),
4318            None,
4319            Some(1.123_456_4), // round down
4320            Some(1.123_456_7), // round up
4321        ]);
4322        generate_cast_test_case!(
4323            &array,
4324            Decimal256Array,
4325            &decimal_type,
4326            vec![
4327                Some(i256::from_i128(1100000_i128)),
4328                Some(i256::from_i128(2200000_i128)),
4329                Some(i256::from_i128(4400000_i128)),
4330                None,
4331                Some(i256::from_i128(1123456_i128)), // round down
4332                Some(i256::from_i128(1123457_i128)), // round up
4333            ]
4334        );
4335
4336        // test f64 to decimal type
4337        let array = Float64Array::from(vec![
4338            Some(1.1),
4339            Some(2.2),
4340            Some(4.4),
4341            None,
4342            Some(1.123_456_489_123_4),     // round down
4343            Some(1.123_456_789_123_4),     // round up
4344            Some(1.123_456_489_012_345_6), // round down
4345            Some(1.123_456_789_012_345_6), // round up
4346        ]);
4347        generate_cast_test_case!(
4348            &array,
4349            Decimal256Array,
4350            &decimal_type,
4351            vec![
4352                Some(i256::from_i128(1100000_i128)),
4353                Some(i256::from_i128(2200000_i128)),
4354                Some(i256::from_i128(4400000_i128)),
4355                None,
4356                Some(i256::from_i128(1123456_i128)), // round down
4357                Some(i256::from_i128(1123457_i128)), // round up
4358                Some(i256::from_i128(1123456_i128)), // round down
4359                Some(i256::from_i128(1123457_i128)), // round up
4360            ]
4361        );
4362    }
4363
4364    #[test]
4365    fn test_cast_i32_to_f64() {
4366        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4367        let b = cast(&array, &DataType::Float64).unwrap();
4368        let c = b.as_primitive::<Float64Type>();
4369        assert_eq!(5.0, c.value(0));
4370        assert_eq!(6.0, c.value(1));
4371        assert_eq!(7.0, c.value(2));
4372        assert_eq!(8.0, c.value(3));
4373        assert_eq!(9.0, c.value(4));
4374    }
4375
4376    #[test]
4377    fn test_cast_i32_to_u8() {
4378        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4379        let b = cast(&array, &DataType::UInt8).unwrap();
4380        let c = b.as_primitive::<UInt8Type>();
4381        assert!(!c.is_valid(0));
4382        assert_eq!(6, c.value(1));
4383        assert!(!c.is_valid(2));
4384        assert_eq!(8, c.value(3));
4385        // overflows return None
4386        assert!(!c.is_valid(4));
4387    }
4388
4389    #[test]
4390    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4391    fn test_cast_int32_to_u8_with_error() {
4392        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4393        // overflow with the error
4394        let cast_option = CastOptions {
4395            safe: false,
4396            format_options: FormatOptions::default(),
4397        };
4398        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4399        assert!(result.is_err());
4400        result.unwrap();
4401    }
4402
4403    #[test]
4404    fn test_cast_i32_to_u8_sliced() {
4405        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4406        assert_eq!(0, array.offset());
4407        let array = array.slice(2, 3);
4408        let b = cast(&array, &DataType::UInt8).unwrap();
4409        assert_eq!(3, b.len());
4410        let c = b.as_primitive::<UInt8Type>();
4411        assert!(!c.is_valid(0));
4412        assert_eq!(8, c.value(1));
4413        // overflows return None
4414        assert!(!c.is_valid(2));
4415    }
4416
4417    #[test]
4418    fn test_cast_i32_to_i32() {
4419        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4420        let b = cast(&array, &DataType::Int32).unwrap();
4421        let c = b.as_primitive::<Int32Type>();
4422        assert_eq!(5, c.value(0));
4423        assert_eq!(6, c.value(1));
4424        assert_eq!(7, c.value(2));
4425        assert_eq!(8, c.value(3));
4426        assert_eq!(9, c.value(4));
4427    }
4428
4429    #[test]
4430    fn test_cast_i32_to_list_i32() {
4431        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4432        let b = cast(
4433            &array,
4434            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4435        )
4436        .unwrap();
4437        assert_eq!(5, b.len());
4438        let arr = b.as_list::<i32>();
4439        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4440        assert_eq!(1, arr.value_length(0));
4441        assert_eq!(1, arr.value_length(1));
4442        assert_eq!(1, arr.value_length(2));
4443        assert_eq!(1, arr.value_length(3));
4444        assert_eq!(1, arr.value_length(4));
4445        let c = arr.values().as_primitive::<Int32Type>();
4446        assert_eq!(5, c.value(0));
4447        assert_eq!(6, c.value(1));
4448        assert_eq!(7, c.value(2));
4449        assert_eq!(8, c.value(3));
4450        assert_eq!(9, c.value(4));
4451    }
4452
4453    #[test]
4454    fn test_cast_i32_to_list_i32_nullable() {
4455        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4456        let b = cast(
4457            &array,
4458            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4459        )
4460        .unwrap();
4461        assert_eq!(5, b.len());
4462        assert_eq!(0, b.null_count());
4463        let arr = b.as_list::<i32>();
4464        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4465        assert_eq!(1, arr.value_length(0));
4466        assert_eq!(1, arr.value_length(1));
4467        assert_eq!(1, arr.value_length(2));
4468        assert_eq!(1, arr.value_length(3));
4469        assert_eq!(1, arr.value_length(4));
4470
4471        let c = arr.values().as_primitive::<Int32Type>();
4472        assert_eq!(1, c.null_count());
4473        assert_eq!(5, c.value(0));
4474        assert!(!c.is_valid(1));
4475        assert_eq!(7, c.value(2));
4476        assert_eq!(8, c.value(3));
4477        assert_eq!(9, c.value(4));
4478    }
4479
4480    #[test]
4481    fn test_cast_i32_to_list_f64_nullable_sliced() {
4482        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4483        let array = array.slice(2, 4);
4484        let b = cast(
4485            &array,
4486            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4487        )
4488        .unwrap();
4489        assert_eq!(4, b.len());
4490        assert_eq!(0, b.null_count());
4491        let arr = b.as_list::<i32>();
4492        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4493        assert_eq!(1, arr.value_length(0));
4494        assert_eq!(1, arr.value_length(1));
4495        assert_eq!(1, arr.value_length(2));
4496        assert_eq!(1, arr.value_length(3));
4497        let c = arr.values().as_primitive::<Float64Type>();
4498        assert_eq!(1, c.null_count());
4499        assert_eq!(7.0, c.value(0));
4500        assert_eq!(8.0, c.value(1));
4501        assert!(!c.is_valid(2));
4502        assert_eq!(10.0, c.value(3));
4503    }
4504
4505    #[test]
4506    fn test_cast_int_to_utf8view() {
4507        let inputs = vec![
4508            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4509            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4510            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4511            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4512            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4513            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4514            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4515            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4516        ];
4517        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4518            None,
4519            Some("8"),
4520            Some("9"),
4521            Some("10"),
4522        ]));
4523
4524        for array in inputs {
4525            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4526            let arr = cast(&array, &DataType::Utf8View).unwrap();
4527            assert_eq!(expected.as_ref(), arr.as_ref());
4528        }
4529    }
4530
4531    #[test]
4532    fn test_cast_float_to_utf8view() {
4533        let inputs = vec![
4534            Arc::new(Float16Array::from(vec![
4535                Some(f16::from_f64(1.5)),
4536                Some(f16::from_f64(2.5)),
4537                None,
4538            ])) as ArrayRef,
4539            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4540            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4541        ];
4542
4543        let expected: ArrayRef =
4544            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4545
4546        for array in inputs {
4547            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4548            let arr = cast(&array, &DataType::Utf8View).unwrap();
4549            assert_eq!(expected.as_ref(), arr.as_ref());
4550        }
4551    }
4552
4553    #[test]
4554    fn test_cast_utf8_to_i32() {
4555        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4556        let b = cast(&array, &DataType::Int32).unwrap();
4557        let c = b.as_primitive::<Int32Type>();
4558        assert_eq!(5, c.value(0));
4559        assert_eq!(6, c.value(1));
4560        assert!(!c.is_valid(2));
4561        assert_eq!(8, c.value(3));
4562        assert!(!c.is_valid(4));
4563    }
4564
4565    #[test]
4566    fn test_cast_utf8view_to_i32() {
4567        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4568        let b = cast(&array, &DataType::Int32).unwrap();
4569        let c = b.as_primitive::<Int32Type>();
4570        assert_eq!(5, c.value(0));
4571        assert_eq!(6, c.value(1));
4572        assert!(!c.is_valid(2));
4573        assert_eq!(8, c.value(3));
4574        assert!(!c.is_valid(4));
4575    }
4576
4577    #[test]
4578    fn test_cast_utf8view_to_f32() {
4579        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4580        let b = cast(&array, &DataType::Float32).unwrap();
4581        let c = b.as_primitive::<Float32Type>();
4582        assert_eq!(3.0, c.value(0));
4583        assert_eq!(4.56, c.value(1));
4584        assert!(!c.is_valid(2));
4585        assert_eq!(8.9, c.value(3));
4586    }
4587
4588    #[test]
4589    fn test_cast_string_to_f16() {
4590        let arrays = [
4591            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4592            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4593            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4594        ];
4595        for array in arrays {
4596            let b = cast(&array, &DataType::Float16).unwrap();
4597            let c = b.as_primitive::<Float16Type>();
4598            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4599            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4600            assert!(!c.is_valid(2));
4601            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4602        }
4603    }
4604
4605    #[test]
4606    fn test_cast_utf8view_to_decimal128() {
4607        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4608        let arr = Arc::new(array) as ArrayRef;
4609        generate_cast_test_case!(
4610            &arr,
4611            Decimal128Array,
4612            &DataType::Decimal128(4, 2),
4613            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4614        );
4615    }
4616
4617    #[test]
4618    fn test_cast_with_options_utf8_to_i32() {
4619        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4620        let result = cast_with_options(
4621            &array,
4622            &DataType::Int32,
4623            &CastOptions {
4624                safe: false,
4625                format_options: FormatOptions::default(),
4626            },
4627        );
4628        match result {
4629            Ok(_) => panic!("expected error"),
4630            Err(e) => {
4631                assert!(
4632                    e.to_string()
4633                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4634                    "Error: {e}"
4635                )
4636            }
4637        }
4638    }
4639
4640    #[test]
4641    fn test_cast_utf8_to_bool() {
4642        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4643        let casted = cast(&strings, &DataType::Boolean).unwrap();
4644        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4645        assert_eq!(*as_boolean_array(&casted), expected);
4646    }
4647
4648    #[test]
4649    fn test_cast_utf8view_to_bool() {
4650        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4651        let casted = cast(&strings, &DataType::Boolean).unwrap();
4652        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4653        assert_eq!(*as_boolean_array(&casted), expected);
4654    }
4655
4656    #[test]
4657    fn test_cast_with_options_utf8_to_bool() {
4658        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4659        let casted = cast_with_options(
4660            &strings,
4661            &DataType::Boolean,
4662            &CastOptions {
4663                safe: false,
4664                format_options: FormatOptions::default(),
4665            },
4666        );
4667        match casted {
4668            Ok(_) => panic!("expected error"),
4669            Err(e) => {
4670                assert!(
4671                    e.to_string().contains(
4672                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4673                    )
4674                )
4675            }
4676        }
4677    }
4678
4679    #[test]
4680    fn test_cast_bool_to_i32() {
4681        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4682        let b = cast(&array, &DataType::Int32).unwrap();
4683        let c = b.as_primitive::<Int32Type>();
4684        assert_eq!(1, c.value(0));
4685        assert_eq!(0, c.value(1));
4686        assert!(!c.is_valid(2));
4687    }
4688
4689    #[test]
4690    fn test_cast_bool_to_utf8view() {
4691        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4692        let b = cast(&array, &DataType::Utf8View).unwrap();
4693        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4694        assert_eq!("true", c.value(0));
4695        assert_eq!("false", c.value(1));
4696        assert!(!c.is_valid(2));
4697    }
4698
4699    #[test]
4700    fn test_cast_bool_to_utf8() {
4701        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4702        let b = cast(&array, &DataType::Utf8).unwrap();
4703        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4704        assert_eq!("true", c.value(0));
4705        assert_eq!("false", c.value(1));
4706        assert!(!c.is_valid(2));
4707    }
4708
4709    #[test]
4710    fn test_cast_bool_to_large_utf8() {
4711        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4712        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4713        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4714        assert_eq!("true", c.value(0));
4715        assert_eq!("false", c.value(1));
4716        assert!(!c.is_valid(2));
4717    }
4718
4719    #[test]
4720    fn test_cast_bool_to_f64() {
4721        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4722        let b = cast(&array, &DataType::Float64).unwrap();
4723        let c = b.as_primitive::<Float64Type>();
4724        assert_eq!(1.0, c.value(0));
4725        assert_eq!(0.0, c.value(1));
4726        assert!(!c.is_valid(2));
4727    }
4728
4729    #[test]
4730    fn test_cast_integer_to_timestamp() {
4731        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4732        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4733
4734        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4735        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4736
4737        assert_eq!(&actual, &expected);
4738
4739        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4740        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4741
4742        assert_eq!(&actual, &expected);
4743
4744        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4745        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4746
4747        assert_eq!(&actual, &expected);
4748
4749        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4750        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4751
4752        assert_eq!(&actual, &expected);
4753
4754        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4755        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4756
4757        assert_eq!(&actual, &expected);
4758
4759        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4760        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4761
4762        assert_eq!(&actual, &expected);
4763
4764        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4765        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4766
4767        assert_eq!(&actual, &expected);
4768    }
4769
4770    #[test]
4771    fn test_cast_timestamp_to_integer() {
4772        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4773            .with_timezone("UTC".to_string());
4774        let expected = cast(&array, &DataType::Int64).unwrap();
4775
4776        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4777        assert_eq!(&actual, &expected);
4778
4779        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4780        assert_eq!(&actual, &expected);
4781
4782        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4783        assert_eq!(&actual, &expected);
4784
4785        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4786        assert_eq!(&actual, &expected);
4787
4788        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4789        assert_eq!(&actual, &expected);
4790
4791        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4792        assert_eq!(&actual, &expected);
4793
4794        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4795        assert_eq!(&actual, &expected);
4796    }
4797
4798    #[test]
4799    fn test_cast_floating_to_timestamp() {
4800        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4801        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4802
4803        let array = Float16Array::from(vec![
4804            Some(f16::from_f32(2.0)),
4805            Some(f16::from_f32(10.6)),
4806            None,
4807        ]);
4808        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4809
4810        assert_eq!(&actual, &expected);
4811
4812        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4813        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4814
4815        assert_eq!(&actual, &expected);
4816
4817        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4818        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4819
4820        assert_eq!(&actual, &expected);
4821    }
4822
4823    #[test]
4824    fn test_cast_timestamp_to_floating() {
4825        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4826            .with_timezone("UTC".to_string());
4827        let expected = cast(&array, &DataType::Int64).unwrap();
4828
4829        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4830        assert_eq!(&actual, &expected);
4831
4832        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4833        assert_eq!(&actual, &expected);
4834
4835        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4836        assert_eq!(&actual, &expected);
4837    }
4838
4839    #[test]
4840    fn test_cast_decimal_to_timestamp() {
4841        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4842        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4843
4844        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4845            .with_precision_and_scale(4, 2)
4846            .unwrap();
4847        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4848
4849        assert_eq!(&actual, &expected);
4850
4851        let array = Decimal256Array::from(vec![
4852            Some(i256::from_i128(2000)),
4853            Some(i256::from_i128(10000)),
4854            None,
4855        ])
4856        .with_precision_and_scale(5, 3)
4857        .unwrap();
4858        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4859
4860        assert_eq!(&actual, &expected);
4861    }
4862
4863    #[test]
4864    fn test_cast_timestamp_to_decimal() {
4865        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4866            .with_timezone("UTC".to_string());
4867        let expected = cast(&array, &DataType::Int64).unwrap();
4868
4869        let actual = cast(
4870            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4871            &DataType::Int64,
4872        )
4873        .unwrap();
4874        assert_eq!(&actual, &expected);
4875
4876        let actual = cast(
4877            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4878            &DataType::Int64,
4879        )
4880        .unwrap();
4881        assert_eq!(&actual, &expected);
4882    }
4883
4884    #[test]
4885    fn test_cast_list_i32_to_list_u16() {
4886        let values = vec![
4887            Some(vec![Some(0), Some(0), Some(0)]),
4888            Some(vec![Some(-1), Some(-2), Some(-1)]),
4889            Some(vec![Some(2), Some(100000000)]),
4890        ];
4891        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
4892
4893        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
4894        assert!(can_cast_types(list_array.data_type(), &target_type));
4895        let cast_array = cast(&list_array, &target_type).unwrap();
4896
4897        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4898        //
4899        // 3 negative values should get lost when casting to unsigned,
4900        // 1 value should overflow
4901        assert_eq!(0, cast_array.null_count());
4902
4903        // offsets should be the same
4904        let array = cast_array.as_list::<i32>();
4905        assert_eq!(list_array.value_offsets(), array.value_offsets());
4906
4907        assert_eq!(DataType::UInt16, array.value_type());
4908        assert_eq!(3, array.value_length(0));
4909        assert_eq!(3, array.value_length(1));
4910        assert_eq!(2, array.value_length(2));
4911
4912        // expect 4 nulls: negative numbers and overflow
4913        let u16arr = array.values().as_primitive::<UInt16Type>();
4914        assert_eq!(4, u16arr.null_count());
4915
4916        // expect 4 nulls: negative numbers and overflow
4917        let expected: UInt16Array =
4918            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4919                .into_iter()
4920                .collect();
4921
4922        assert_eq!(u16arr, &expected);
4923    }
4924
4925    #[test]
4926    fn test_cast_list_i32_to_list_timestamp() {
4927        // Construct a value array
4928        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4929
4930        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4931
4932        // Construct a list array from the above two
4933        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4934        let list_data = ArrayData::builder(list_data_type)
4935            .len(3)
4936            .add_buffer(value_offsets)
4937            .add_child_data(value_data)
4938            .build()
4939            .unwrap();
4940        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4941
4942        let actual = cast(
4943            &list_array,
4944            &DataType::List(Arc::new(Field::new_list_field(
4945                DataType::Timestamp(TimeUnit::Microsecond, None),
4946                true,
4947            ))),
4948        )
4949        .unwrap();
4950
4951        let expected = cast(
4952            &cast(
4953                &list_array,
4954                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4955            )
4956            .unwrap(),
4957            &DataType::List(Arc::new(Field::new_list_field(
4958                DataType::Timestamp(TimeUnit::Microsecond, None),
4959                true,
4960            ))),
4961        )
4962        .unwrap();
4963
4964        assert_eq!(&actual, &expected);
4965    }
4966
4967    #[test]
4968    fn test_cast_date32_to_date64() {
4969        let a = Date32Array::from(vec![10000, 17890]);
4970        let array = Arc::new(a) as ArrayRef;
4971        let b = cast(&array, &DataType::Date64).unwrap();
4972        let c = b.as_primitive::<Date64Type>();
4973        assert_eq!(864000000000, c.value(0));
4974        assert_eq!(1545696000000, c.value(1));
4975    }
4976
4977    #[test]
4978    fn test_cast_date64_to_date32() {
4979        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4980        let array = Arc::new(a) as ArrayRef;
4981        let b = cast(&array, &DataType::Date32).unwrap();
4982        let c = b.as_primitive::<Date32Type>();
4983        assert_eq!(10000, c.value(0));
4984        assert_eq!(17890, c.value(1));
4985        assert!(c.is_null(2));
4986    }
4987
4988    #[test]
4989    fn test_cast_string_to_integral_overflow() {
4990        let str = Arc::new(StringArray::from(vec![
4991            Some("123"),
4992            Some("-123"),
4993            Some("86374"),
4994            None,
4995        ])) as ArrayRef;
4996
4997        let options = CastOptions {
4998            safe: true,
4999            format_options: FormatOptions::default(),
5000        };
5001        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5002        let expected =
5003            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5004        assert_eq!(&res, &expected);
5005    }
5006
5007    #[test]
5008    fn test_cast_string_to_timestamp() {
5009        let a0 = Arc::new(StringViewArray::from(vec![
5010            Some("2020-09-08T12:00:00.123456789+00:00"),
5011            Some("Not a valid date"),
5012            None,
5013        ])) as ArrayRef;
5014        let a1 = Arc::new(StringArray::from(vec![
5015            Some("2020-09-08T12:00:00.123456789+00:00"),
5016            Some("Not a valid date"),
5017            None,
5018        ])) as ArrayRef;
5019        let a2 = Arc::new(LargeStringArray::from(vec![
5020            Some("2020-09-08T12:00:00.123456789+00:00"),
5021            Some("Not a valid date"),
5022            None,
5023        ])) as ArrayRef;
5024        for array in &[a0, a1, a2] {
5025            for time_unit in &[
5026                TimeUnit::Second,
5027                TimeUnit::Millisecond,
5028                TimeUnit::Microsecond,
5029                TimeUnit::Nanosecond,
5030            ] {
5031                let to_type = DataType::Timestamp(*time_unit, None);
5032                let b = cast(array, &to_type).unwrap();
5033
5034                match time_unit {
5035                    TimeUnit::Second => {
5036                        let c = b.as_primitive::<TimestampSecondType>();
5037                        assert_eq!(1599566400, c.value(0));
5038                        assert!(c.is_null(1));
5039                        assert!(c.is_null(2));
5040                    }
5041                    TimeUnit::Millisecond => {
5042                        let c = b
5043                            .as_any()
5044                            .downcast_ref::<TimestampMillisecondArray>()
5045                            .unwrap();
5046                        assert_eq!(1599566400123, c.value(0));
5047                        assert!(c.is_null(1));
5048                        assert!(c.is_null(2));
5049                    }
5050                    TimeUnit::Microsecond => {
5051                        let c = b
5052                            .as_any()
5053                            .downcast_ref::<TimestampMicrosecondArray>()
5054                            .unwrap();
5055                        assert_eq!(1599566400123456, c.value(0));
5056                        assert!(c.is_null(1));
5057                        assert!(c.is_null(2));
5058                    }
5059                    TimeUnit::Nanosecond => {
5060                        let c = b
5061                            .as_any()
5062                            .downcast_ref::<TimestampNanosecondArray>()
5063                            .unwrap();
5064                        assert_eq!(1599566400123456789, c.value(0));
5065                        assert!(c.is_null(1));
5066                        assert!(c.is_null(2));
5067                    }
5068                }
5069
5070                let options = CastOptions {
5071                    safe: false,
5072                    format_options: FormatOptions::default(),
5073                };
5074                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5075                assert_eq!(
5076                    err.to_string(),
5077                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5078                );
5079            }
5080        }
5081    }
5082
5083    #[test]
5084    fn test_cast_string_to_timestamp_overflow() {
5085        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5086        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5087        let result = result.as_primitive::<TimestampSecondType>();
5088        assert_eq!(result.values(), &[247112596800]);
5089    }
5090
5091    #[test]
5092    fn test_cast_string_to_date32() {
5093        let a0 = Arc::new(StringViewArray::from(vec![
5094            Some("2018-12-25"),
5095            Some("Not a valid date"),
5096            None,
5097        ])) as ArrayRef;
5098        let a1 = Arc::new(StringArray::from(vec![
5099            Some("2018-12-25"),
5100            Some("Not a valid date"),
5101            None,
5102        ])) as ArrayRef;
5103        let a2 = Arc::new(LargeStringArray::from(vec![
5104            Some("2018-12-25"),
5105            Some("Not a valid date"),
5106            None,
5107        ])) as ArrayRef;
5108        for array in &[a0, a1, a2] {
5109            let to_type = DataType::Date32;
5110            let b = cast(array, &to_type).unwrap();
5111            let c = b.as_primitive::<Date32Type>();
5112            assert_eq!(17890, c.value(0));
5113            assert!(c.is_null(1));
5114            assert!(c.is_null(2));
5115
5116            let options = CastOptions {
5117                safe: false,
5118                format_options: FormatOptions::default(),
5119            };
5120            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5121            assert_eq!(
5122                err.to_string(),
5123                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5124            );
5125        }
5126    }
5127
5128    #[test]
5129    fn test_cast_string_with_large_date_to_date32() {
5130        let array = Arc::new(StringArray::from(vec![
5131            Some("+10999-12-31"),
5132            Some("-0010-02-28"),
5133            Some("0010-02-28"),
5134            Some("0000-01-01"),
5135            Some("-0000-01-01"),
5136            Some("-0001-01-01"),
5137        ])) as ArrayRef;
5138        let to_type = DataType::Date32;
5139        let options = CastOptions {
5140            safe: false,
5141            format_options: FormatOptions::default(),
5142        };
5143        let b = cast_with_options(&array, &to_type, &options).unwrap();
5144        let c = b.as_primitive::<Date32Type>();
5145        assert_eq!(3298139, c.value(0)); // 10999-12-31
5146        assert_eq!(-723122, c.value(1)); // -0010-02-28
5147        assert_eq!(-715817, c.value(2)); // 0010-02-28
5148        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5149        assert_eq!(-719528, c.value(3)); // 0000-01-01
5150        assert_eq!(-719528, c.value(4)); // -0000-01-01
5151        assert_eq!(-719893, c.value(5)); // -0001-01-01
5152    }
5153
5154    #[test]
5155    fn test_cast_invalid_string_with_large_date_to_date32() {
5156        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5157        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5158        let to_type = DataType::Date32;
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 '10999-12-31' to value of Date32 type"
5167        );
5168    }
5169
5170    #[test]
5171    fn test_cast_string_format_yyyymmdd_to_date32() {
5172        let a0 = Arc::new(StringViewArray::from(vec![
5173            Some("2020-12-25"),
5174            Some("20201117"),
5175        ])) as ArrayRef;
5176        let a1 = Arc::new(StringArray::from(vec![
5177            Some("2020-12-25"),
5178            Some("20201117"),
5179        ])) as ArrayRef;
5180        let a2 = Arc::new(LargeStringArray::from(vec![
5181            Some("2020-12-25"),
5182            Some("20201117"),
5183        ])) as ArrayRef;
5184
5185        for array in &[a0, a1, a2] {
5186            let to_type = DataType::Date32;
5187            let options = CastOptions {
5188                safe: false,
5189                format_options: FormatOptions::default(),
5190            };
5191            let result = cast_with_options(&array, &to_type, &options).unwrap();
5192            let c = result.as_primitive::<Date32Type>();
5193            assert_eq!(
5194                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5195                c.value_as_date(0)
5196            );
5197            assert_eq!(
5198                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5199                c.value_as_date(1)
5200            );
5201        }
5202    }
5203
5204    #[test]
5205    fn test_cast_string_to_time32second() {
5206        let a0 = Arc::new(StringViewArray::from(vec![
5207            Some("08:08:35.091323414"),
5208            Some("08:08:60.091323414"), // leap second
5209            Some("08:08:61.091323414"), // not valid
5210            Some("Not a valid time"),
5211            None,
5212        ])) as ArrayRef;
5213        let a1 = Arc::new(StringArray::from(vec![
5214            Some("08:08:35.091323414"),
5215            Some("08:08:60.091323414"), // leap second
5216            Some("08:08:61.091323414"), // not valid
5217            Some("Not a valid time"),
5218            None,
5219        ])) as ArrayRef;
5220        let a2 = Arc::new(LargeStringArray::from(vec![
5221            Some("08:08:35.091323414"),
5222            Some("08:08:60.091323414"), // leap second
5223            Some("08:08:61.091323414"), // not valid
5224            Some("Not a valid time"),
5225            None,
5226        ])) as ArrayRef;
5227        for array in &[a0, a1, a2] {
5228            let to_type = DataType::Time32(TimeUnit::Second);
5229            let b = cast(array, &to_type).unwrap();
5230            let c = b.as_primitive::<Time32SecondType>();
5231            assert_eq!(29315, c.value(0));
5232            assert_eq!(29340, c.value(1));
5233            assert!(c.is_null(2));
5234            assert!(c.is_null(3));
5235            assert!(c.is_null(4));
5236
5237            let options = CastOptions {
5238                safe: false,
5239                format_options: FormatOptions::default(),
5240            };
5241            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5242            assert_eq!(
5243                err.to_string(),
5244                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5245            );
5246        }
5247    }
5248
5249    #[test]
5250    fn test_cast_string_to_time32millisecond() {
5251        let a0 = Arc::new(StringViewArray::from(vec![
5252            Some("08:08:35.091323414"),
5253            Some("08:08:60.091323414"), // leap second
5254            Some("08:08:61.091323414"), // not valid
5255            Some("Not a valid time"),
5256            None,
5257        ])) as ArrayRef;
5258        let a1 = Arc::new(StringArray::from(vec![
5259            Some("08:08:35.091323414"),
5260            Some("08:08:60.091323414"), // leap second
5261            Some("08:08:61.091323414"), // not valid
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("08:08:60.091323414"), // leap second
5268            Some("08:08:61.091323414"), // not valid
5269            Some("Not a valid time"),
5270            None,
5271        ])) as ArrayRef;
5272        for array in &[a0, a1, a2] {
5273            let to_type = DataType::Time32(TimeUnit::Millisecond);
5274            let b = cast(array, &to_type).unwrap();
5275            let c = b.as_primitive::<Time32MillisecondType>();
5276            assert_eq!(29315091, c.value(0));
5277            assert_eq!(29340091, c.value(1));
5278            assert!(c.is_null(2));
5279            assert!(c.is_null(3));
5280            assert!(c.is_null(4));
5281
5282            let options = CastOptions {
5283                safe: false,
5284                format_options: FormatOptions::default(),
5285            };
5286            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5287            assert_eq!(
5288                err.to_string(),
5289                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5290            );
5291        }
5292    }
5293
5294    #[test]
5295    fn test_cast_string_to_time64microsecond() {
5296        let a0 = Arc::new(StringViewArray::from(vec![
5297            Some("08:08:35.091323414"),
5298            Some("Not a valid time"),
5299            None,
5300        ])) as ArrayRef;
5301        let a1 = Arc::new(StringArray::from(vec![
5302            Some("08:08:35.091323414"),
5303            Some("Not a valid time"),
5304            None,
5305        ])) as ArrayRef;
5306        let a2 = Arc::new(LargeStringArray::from(vec![
5307            Some("08:08:35.091323414"),
5308            Some("Not a valid time"),
5309            None,
5310        ])) as ArrayRef;
5311        for array in &[a0, a1, a2] {
5312            let to_type = DataType::Time64(TimeUnit::Microsecond);
5313            let b = cast(array, &to_type).unwrap();
5314            let c = b.as_primitive::<Time64MicrosecondType>();
5315            assert_eq!(29315091323, c.value(0));
5316            assert!(c.is_null(1));
5317            assert!(c.is_null(2));
5318
5319            let options = CastOptions {
5320                safe: false,
5321                format_options: FormatOptions::default(),
5322            };
5323            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5324            assert_eq!(
5325                err.to_string(),
5326                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5327            );
5328        }
5329    }
5330
5331    #[test]
5332    fn test_cast_string_to_time64nanosecond() {
5333        let a0 = Arc::new(StringViewArray::from(vec![
5334            Some("08:08:35.091323414"),
5335            Some("Not a valid time"),
5336            None,
5337        ])) as ArrayRef;
5338        let a1 = Arc::new(StringArray::from(vec![
5339            Some("08:08:35.091323414"),
5340            Some("Not a valid time"),
5341            None,
5342        ])) as ArrayRef;
5343        let a2 = Arc::new(LargeStringArray::from(vec![
5344            Some("08:08:35.091323414"),
5345            Some("Not a valid time"),
5346            None,
5347        ])) as ArrayRef;
5348        for array in &[a0, a1, a2] {
5349            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5350            let b = cast(array, &to_type).unwrap();
5351            let c = b.as_primitive::<Time64NanosecondType>();
5352            assert_eq!(29315091323414, c.value(0));
5353            assert!(c.is_null(1));
5354            assert!(c.is_null(2));
5355
5356            let options = CastOptions {
5357                safe: false,
5358                format_options: FormatOptions::default(),
5359            };
5360            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5361            assert_eq!(
5362                err.to_string(),
5363                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5364            );
5365        }
5366    }
5367
5368    #[test]
5369    fn test_cast_string_to_date64() {
5370        let a0 = Arc::new(StringViewArray::from(vec![
5371            Some("2020-09-08T12:00:00"),
5372            Some("Not a valid date"),
5373            None,
5374        ])) as ArrayRef;
5375        let a1 = Arc::new(StringArray::from(vec![
5376            Some("2020-09-08T12:00:00"),
5377            Some("Not a valid date"),
5378            None,
5379        ])) as ArrayRef;
5380        let a2 = Arc::new(LargeStringArray::from(vec![
5381            Some("2020-09-08T12:00:00"),
5382            Some("Not a valid date"),
5383            None,
5384        ])) as ArrayRef;
5385        for array in &[a0, a1, a2] {
5386            let to_type = DataType::Date64;
5387            let b = cast(array, &to_type).unwrap();
5388            let c = b.as_primitive::<Date64Type>();
5389            assert_eq!(1599566400000, c.value(0));
5390            assert!(c.is_null(1));
5391            assert!(c.is_null(2));
5392
5393            let options = CastOptions {
5394                safe: false,
5395                format_options: FormatOptions::default(),
5396            };
5397            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5398            assert_eq!(
5399                err.to_string(),
5400                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5401            );
5402        }
5403    }
5404
5405    macro_rules! test_safe_string_to_interval {
5406        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5407            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5408
5409            let options = CastOptions {
5410                safe: true,
5411                format_options: FormatOptions::default(),
5412            };
5413
5414            let target_interval_array = cast_with_options(
5415                &source_string_array.clone(),
5416                &DataType::Interval($interval_unit),
5417                &options,
5418            )
5419            .unwrap()
5420            .as_any()
5421            .downcast_ref::<$array_ty>()
5422            .unwrap()
5423            .clone() as $array_ty;
5424
5425            let target_string_array =
5426                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5427                    .unwrap()
5428                    .as_any()
5429                    .downcast_ref::<StringArray>()
5430                    .unwrap()
5431                    .clone();
5432
5433            let expect_string_array = StringArray::from($expect_vec);
5434
5435            assert_eq!(target_string_array, expect_string_array);
5436
5437            let target_large_string_array =
5438                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5439                    .unwrap()
5440                    .as_any()
5441                    .downcast_ref::<LargeStringArray>()
5442                    .unwrap()
5443                    .clone();
5444
5445            let expect_large_string_array = LargeStringArray::from($expect_vec);
5446
5447            assert_eq!(target_large_string_array, expect_large_string_array);
5448        };
5449    }
5450
5451    #[test]
5452    fn test_cast_string_to_interval_year_month() {
5453        test_safe_string_to_interval!(
5454            vec![
5455                Some("1 year 1 month"),
5456                Some("1.5 years 13 month"),
5457                Some("30 days"),
5458                Some("31 days"),
5459                Some("2 months 31 days"),
5460                Some("2 months 31 days 1 second"),
5461                Some("foobar"),
5462            ],
5463            IntervalUnit::YearMonth,
5464            IntervalYearMonthArray,
5465            vec![
5466                Some("1 years 1 mons"),
5467                Some("2 years 7 mons"),
5468                None,
5469                None,
5470                None,
5471                None,
5472                None,
5473            ]
5474        );
5475    }
5476
5477    #[test]
5478    fn test_cast_string_to_interval_day_time() {
5479        test_safe_string_to_interval!(
5480            vec![
5481                Some("1 year 1 month"),
5482                Some("1.5 years 13 month"),
5483                Some("30 days"),
5484                Some("1 day 2 second 3.5 milliseconds"),
5485                Some("foobar"),
5486            ],
5487            IntervalUnit::DayTime,
5488            IntervalDayTimeArray,
5489            vec![
5490                Some("390 days"),
5491                Some("930 days"),
5492                Some("30 days"),
5493                None,
5494                None,
5495            ]
5496        );
5497    }
5498
5499    #[test]
5500    fn test_cast_string_to_interval_month_day_nano() {
5501        test_safe_string_to_interval!(
5502            vec![
5503                Some("1 year 1 month 1 day"),
5504                None,
5505                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5506                Some("3 days"),
5507                Some("8 seconds"),
5508                None,
5509                Some("1 day 29800 milliseconds"),
5510                Some("3 months 1 second"),
5511                Some("6 minutes 120 second"),
5512                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5513                Some("foobar"),
5514            ],
5515            IntervalUnit::MonthDayNano,
5516            IntervalMonthDayNanoArray,
5517            vec![
5518                Some("13 mons 1 days"),
5519                None,
5520                Some("31 mons 35 days 0.001400000 secs"),
5521                Some("3 days"),
5522                Some("8.000000000 secs"),
5523                None,
5524                Some("1 days 29.800000000 secs"),
5525                Some("3 mons 1.000000000 secs"),
5526                Some("8 mins"),
5527                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5528                None,
5529            ]
5530        );
5531    }
5532
5533    macro_rules! test_unsafe_string_to_interval_err {
5534        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5535            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5536            let options = CastOptions {
5537                safe: false,
5538                format_options: FormatOptions::default(),
5539            };
5540            let arrow_err = cast_with_options(
5541                &string_array.clone(),
5542                &DataType::Interval($interval_unit),
5543                &options,
5544            )
5545            .unwrap_err();
5546            assert_eq!($error_msg, arrow_err.to_string());
5547        };
5548    }
5549
5550    #[test]
5551    fn test_cast_string_to_interval_err() {
5552        test_unsafe_string_to_interval_err!(
5553            vec![Some("foobar")],
5554            IntervalUnit::YearMonth,
5555            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5556        );
5557        test_unsafe_string_to_interval_err!(
5558            vec![Some("foobar")],
5559            IntervalUnit::DayTime,
5560            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5561        );
5562        test_unsafe_string_to_interval_err!(
5563            vec![Some("foobar")],
5564            IntervalUnit::MonthDayNano,
5565            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5566        );
5567        test_unsafe_string_to_interval_err!(
5568            vec![Some("2 months 31 days 1 second")],
5569            IntervalUnit::YearMonth,
5570            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5571        );
5572        test_unsafe_string_to_interval_err!(
5573            vec![Some("1 day 1.5 milliseconds")],
5574            IntervalUnit::DayTime,
5575            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5576        );
5577
5578        // overflow
5579        test_unsafe_string_to_interval_err!(
5580            vec![Some(format!(
5581                "{} century {} year {} month",
5582                i64::MAX - 2,
5583                i64::MAX - 2,
5584                i64::MAX - 2
5585            ))],
5586            IntervalUnit::DayTime,
5587            format!(
5588                "Arithmetic overflow: Overflow happened on: {} * 100",
5589                i64::MAX - 2
5590            )
5591        );
5592        test_unsafe_string_to_interval_err!(
5593            vec![Some(format!(
5594                "{} year {} month {} day",
5595                i64::MAX - 2,
5596                i64::MAX - 2,
5597                i64::MAX - 2
5598            ))],
5599            IntervalUnit::MonthDayNano,
5600            format!(
5601                "Arithmetic overflow: Overflow happened on: {} * 12",
5602                i64::MAX - 2
5603            )
5604        );
5605    }
5606
5607    #[test]
5608    fn test_cast_binary_to_fixed_size_binary() {
5609        let bytes_1 = "Hiiii".as_bytes();
5610        let bytes_2 = "Hello".as_bytes();
5611
5612        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5613        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5614        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5615
5616        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5617        let down_cast = array_ref
5618            .as_any()
5619            .downcast_ref::<FixedSizeBinaryArray>()
5620            .unwrap();
5621        assert_eq!(bytes_1, down_cast.value(0));
5622        assert_eq!(bytes_2, down_cast.value(1));
5623        assert!(down_cast.is_null(2));
5624
5625        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5626        let down_cast = array_ref
5627            .as_any()
5628            .downcast_ref::<FixedSizeBinaryArray>()
5629            .unwrap();
5630        assert_eq!(bytes_1, down_cast.value(0));
5631        assert_eq!(bytes_2, down_cast.value(1));
5632        assert!(down_cast.is_null(2));
5633
5634        // test error cases when the length of binary are not same
5635        let bytes_1 = "Hi".as_bytes();
5636        let bytes_2 = "Hello".as_bytes();
5637
5638        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5639        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5640        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5641
5642        let array_ref = cast_with_options(
5643            &a1,
5644            &DataType::FixedSizeBinary(5),
5645            &CastOptions {
5646                safe: false,
5647                format_options: FormatOptions::default(),
5648            },
5649        );
5650        assert!(array_ref.is_err());
5651
5652        let array_ref = cast_with_options(
5653            &a2,
5654            &DataType::FixedSizeBinary(5),
5655            &CastOptions {
5656                safe: false,
5657                format_options: FormatOptions::default(),
5658            },
5659        );
5660        assert!(array_ref.is_err());
5661    }
5662
5663    #[test]
5664    fn test_fixed_size_binary_to_binary() {
5665        let bytes_1 = "Hiiii".as_bytes();
5666        let bytes_2 = "Hello".as_bytes();
5667
5668        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5669        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5670
5671        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5672        let down_cast = array_ref.as_binary::<i32>();
5673        assert_eq!(bytes_1, down_cast.value(0));
5674        assert_eq!(bytes_2, down_cast.value(1));
5675        assert!(down_cast.is_null(2));
5676
5677        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5678        let down_cast = array_ref.as_binary::<i64>();
5679        assert_eq!(bytes_1, down_cast.value(0));
5680        assert_eq!(bytes_2, down_cast.value(1));
5681        assert!(down_cast.is_null(2));
5682
5683        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5684        let down_cast = array_ref.as_binary_view();
5685        assert_eq!(bytes_1, down_cast.value(0));
5686        assert_eq!(bytes_2, down_cast.value(1));
5687        assert!(down_cast.is_null(2));
5688    }
5689
5690    #[test]
5691    fn test_fixed_size_binary_to_dictionary() {
5692        let bytes_1 = "Hiiii".as_bytes();
5693        let bytes_2 = "Hello".as_bytes();
5694
5695        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5696        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5697
5698        let cast_type = DataType::Dictionary(
5699            Box::new(DataType::Int8),
5700            Box::new(DataType::FixedSizeBinary(5)),
5701        );
5702        let cast_array = cast(&a1, &cast_type).unwrap();
5703        assert_eq!(cast_array.data_type(), &cast_type);
5704        assert_eq!(
5705            array_to_strings(&cast_array),
5706            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5707        );
5708        // dictionary should only have two distinct values
5709        let dict_array = cast_array.as_dictionary::<Int8Type>();
5710        assert_eq!(dict_array.values().len(), 2);
5711    }
5712
5713    #[test]
5714    fn test_binary_to_dictionary() {
5715        let mut builder = GenericBinaryBuilder::<i32>::new();
5716        builder.append_value(b"hello");
5717        builder.append_value(b"hiiii");
5718        builder.append_value(b"hiiii"); // duplicate
5719        builder.append_null();
5720        builder.append_value(b"rustt");
5721
5722        let a1 = builder.finish();
5723
5724        let cast_type = DataType::Dictionary(
5725            Box::new(DataType::Int8),
5726            Box::new(DataType::FixedSizeBinary(5)),
5727        );
5728        let cast_array = cast(&a1, &cast_type).unwrap();
5729        assert_eq!(cast_array.data_type(), &cast_type);
5730        assert_eq!(
5731            array_to_strings(&cast_array),
5732            vec![
5733                "68656c6c6f",
5734                "6869696969",
5735                "6869696969",
5736                "null",
5737                "7275737474"
5738            ]
5739        );
5740        // dictionary should only have three distinct values
5741        let dict_array = cast_array.as_dictionary::<Int8Type>();
5742        assert_eq!(dict_array.values().len(), 3);
5743    }
5744
5745    #[test]
5746    fn test_cast_string_array_to_dict_utf8_view() {
5747        let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5748
5749        let cast_type =
5750            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5751        assert!(can_cast_types(array.data_type(), &cast_type));
5752        let cast_array = cast(&array, &cast_type).unwrap();
5753        assert_eq!(cast_array.data_type(), &cast_type);
5754
5755        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5756        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5757        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5758
5759        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5760        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5761        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5762
5763        let keys = dict_array.keys();
5764        assert!(keys.is_null(1));
5765        assert_eq!(keys.value(0), keys.value(3));
5766        assert_ne!(keys.value(0), keys.value(2));
5767    }
5768
5769    #[test]
5770    fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
5771        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
5772
5773        let cast_type =
5774            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5775        assert!(can_cast_types(array.data_type(), &cast_type));
5776        let cast_array = cast(&array, &cast_type).unwrap();
5777        assert_eq!(cast_array.data_type(), &cast_type);
5778
5779        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5780        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5781        assert_eq!(dict_array.values().len(), 2);
5782
5783        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5784        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5785        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
5786
5787        let keys = dict_array.keys();
5788        assert!(keys.is_null(1));
5789        assert_eq!(keys.value(0), keys.value(3));
5790        assert_ne!(keys.value(0), keys.value(2));
5791    }
5792
5793    #[test]
5794    fn test_cast_string_view_array_to_dict_utf8_view() {
5795        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5796
5797        let cast_type =
5798            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5799        assert!(can_cast_types(array.data_type(), &cast_type));
5800        let cast_array = cast(&array, &cast_type).unwrap();
5801        assert_eq!(cast_array.data_type(), &cast_type);
5802
5803        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5804        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5805        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5806
5807        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5808        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5809        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5810
5811        let keys = dict_array.keys();
5812        assert!(keys.is_null(1));
5813        assert_eq!(keys.value(0), keys.value(3));
5814        assert_ne!(keys.value(0), keys.value(2));
5815    }
5816
5817    #[test]
5818    fn test_cast_string_view_slice_to_dict_utf8_view() {
5819        let array = StringViewArray::from(vec![
5820            Some("zero"),
5821            Some("one"),
5822            None,
5823            Some("three"),
5824            Some("one"),
5825        ]);
5826        let view = array.slice(1, 4);
5827
5828        let cast_type =
5829            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5830        assert!(can_cast_types(view.data_type(), &cast_type));
5831        let cast_array = cast(&view, &cast_type).unwrap();
5832        assert_eq!(cast_array.data_type(), &cast_type);
5833
5834        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5835        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5836        assert_eq!(dict_array.values().len(), 2);
5837
5838        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5839        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5840        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5841
5842        let keys = dict_array.keys();
5843        assert!(keys.is_null(1));
5844        assert_eq!(keys.value(0), keys.value(3));
5845        assert_ne!(keys.value(0), keys.value(2));
5846    }
5847
5848    #[test]
5849    fn test_cast_binary_array_to_dict_binary_view() {
5850        let mut builder = GenericBinaryBuilder::<i32>::new();
5851        builder.append_value(b"hello");
5852        builder.append_value(b"hiiii");
5853        builder.append_value(b"hiiii"); // duplicate
5854        builder.append_null();
5855        builder.append_value(b"rustt");
5856
5857        let array = builder.finish();
5858
5859        let cast_type =
5860            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5861        assert!(can_cast_types(array.data_type(), &cast_type));
5862        let cast_array = cast(&array, &cast_type).unwrap();
5863        assert_eq!(cast_array.data_type(), &cast_type);
5864
5865        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5866        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5867        assert_eq!(dict_array.values().len(), 3);
5868
5869        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5870        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5871        assert_eq!(
5872            actual,
5873            vec![
5874                Some(b"hello".as_slice()),
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(3));
5884        assert_eq!(keys.value(1), keys.value(2));
5885        assert_ne!(keys.value(0), keys.value(1));
5886    }
5887
5888    #[test]
5889    fn test_cast_binary_view_array_to_dict_binary_view() {
5890        let view = BinaryViewArray::from_iter([
5891            Some(b"hello".as_slice()),
5892            Some(b"hiiii".as_slice()),
5893            Some(b"hiiii".as_slice()), // duplicate
5894            None,
5895            Some(b"rustt".as_slice()),
5896        ]);
5897
5898        let cast_type =
5899            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5900        assert!(can_cast_types(view.data_type(), &cast_type));
5901        let cast_array = cast(&view, &cast_type).unwrap();
5902        assert_eq!(cast_array.data_type(), &cast_type);
5903
5904        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5905        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5906        assert_eq!(dict_array.values().len(), 3);
5907
5908        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5909        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5910        assert_eq!(
5911            actual,
5912            vec![
5913                Some(b"hello".as_slice()),
5914                Some(b"hiiii".as_slice()),
5915                Some(b"hiiii".as_slice()),
5916                None,
5917                Some(b"rustt".as_slice())
5918            ]
5919        );
5920
5921        let keys = dict_array.keys();
5922        assert!(keys.is_null(3));
5923        assert_eq!(keys.value(1), keys.value(2));
5924        assert_ne!(keys.value(0), keys.value(1));
5925    }
5926
5927    #[test]
5928    fn test_cast_binary_view_slice_to_dict_binary_view() {
5929        let view = BinaryViewArray::from_iter([
5930            Some(b"hello".as_slice()),
5931            Some(b"hiiii".as_slice()),
5932            Some(b"hiiii".as_slice()), // duplicate
5933            None,
5934            Some(b"rustt".as_slice()),
5935        ]);
5936        let sliced = view.slice(1, 4);
5937
5938        let cast_type =
5939            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5940        assert!(can_cast_types(sliced.data_type(), &cast_type));
5941        let cast_array = cast(&sliced, &cast_type).unwrap();
5942        assert_eq!(cast_array.data_type(), &cast_type);
5943
5944        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5945        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5946        assert_eq!(dict_array.values().len(), 2);
5947
5948        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5949        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5950        assert_eq!(
5951            actual,
5952            vec![
5953                Some(b"hiiii".as_slice()),
5954                Some(b"hiiii".as_slice()),
5955                None,
5956                Some(b"rustt".as_slice())
5957            ]
5958        );
5959
5960        let keys = dict_array.keys();
5961        assert!(keys.is_null(2));
5962        assert_eq!(keys.value(0), keys.value(1));
5963        assert_ne!(keys.value(0), keys.value(3));
5964    }
5965
5966    #[test]
5967    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
5968        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
5969
5970        let cast_type =
5971            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
5972        assert!(can_cast_types(array.data_type(), &cast_type));
5973        let err = cast(&array, &cast_type).unwrap_err();
5974        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
5975    }
5976
5977    #[test]
5978    fn test_cast_large_string_array_to_dict_utf8_view() {
5979        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5980
5981        let cast_type =
5982            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5983        assert!(can_cast_types(array.data_type(), &cast_type));
5984        let cast_array = cast(&array, &cast_type).unwrap();
5985        assert_eq!(cast_array.data_type(), &cast_type);
5986
5987        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5988        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5989        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5990
5991        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5992        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5993        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5994
5995        let keys = dict_array.keys();
5996        assert!(keys.is_null(1));
5997        assert_eq!(keys.value(0), keys.value(3));
5998        assert_ne!(keys.value(0), keys.value(2));
5999    }
6000
6001    #[test]
6002    fn test_cast_large_binary_array_to_dict_binary_view() {
6003        let mut builder = GenericBinaryBuilder::<i64>::new();
6004        builder.append_value(b"hello");
6005        builder.append_value(b"world");
6006        builder.append_value(b"hello"); // duplicate
6007        builder.append_null();
6008
6009        let array = builder.finish();
6010
6011        let cast_type =
6012            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6013        assert!(can_cast_types(array.data_type(), &cast_type));
6014        let cast_array = cast(&array, &cast_type).unwrap();
6015        assert_eq!(cast_array.data_type(), &cast_type);
6016
6017        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6018        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6019        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
6020
6021        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6022        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6023        assert_eq!(
6024            actual,
6025            vec![
6026                Some(b"hello".as_slice()),
6027                Some(b"world".as_slice()),
6028                Some(b"hello".as_slice()),
6029                None
6030            ]
6031        );
6032
6033        let keys = dict_array.keys();
6034        assert!(keys.is_null(3));
6035        assert_eq!(keys.value(0), keys.value(2));
6036        assert_ne!(keys.value(0), keys.value(1));
6037    }
6038
6039    #[test]
6040    fn test_cast_empty_string_array_to_dict_utf8_view() {
6041        let array = StringArray::from(Vec::<Option<&str>>::new());
6042
6043        let cast_type =
6044            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6045        assert!(can_cast_types(array.data_type(), &cast_type));
6046        let cast_array = cast(&array, &cast_type).unwrap();
6047        assert_eq!(cast_array.data_type(), &cast_type);
6048        assert_eq!(cast_array.len(), 0);
6049    }
6050
6051    #[test]
6052    fn test_cast_empty_binary_array_to_dict_binary_view() {
6053        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6054
6055        let cast_type =
6056            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6057        assert!(can_cast_types(array.data_type(), &cast_type));
6058        let cast_array = cast(&array, &cast_type).unwrap();
6059        assert_eq!(cast_array.data_type(), &cast_type);
6060        assert_eq!(cast_array.len(), 0);
6061    }
6062
6063    #[test]
6064    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6065        let array = StringArray::from(vec![None::<&str>, None, None]);
6066
6067        let cast_type =
6068            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6069        assert!(can_cast_types(array.data_type(), &cast_type));
6070        let cast_array = cast(&array, &cast_type).unwrap();
6071        assert_eq!(cast_array.data_type(), &cast_type);
6072        assert_eq!(cast_array.null_count(), 3);
6073
6074        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6075        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6076        assert_eq!(dict_array.values().len(), 0);
6077        assert_eq!(dict_array.keys().null_count(), 3);
6078
6079        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6080        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6081        assert_eq!(actual, vec![None, None, None]);
6082    }
6083
6084    #[test]
6085    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6086        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6087
6088        let cast_type =
6089            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6090        assert!(can_cast_types(array.data_type(), &cast_type));
6091        let cast_array = cast(&array, &cast_type).unwrap();
6092        assert_eq!(cast_array.data_type(), &cast_type);
6093        assert_eq!(cast_array.null_count(), 3);
6094
6095        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6096        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6097        assert_eq!(dict_array.values().len(), 0);
6098        assert_eq!(dict_array.keys().null_count(), 3);
6099
6100        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6101        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6102        assert_eq!(actual, vec![None, None, None]);
6103    }
6104
6105    #[test]
6106    fn test_numeric_to_binary() {
6107        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6108
6109        let array_ref = cast(&a, &DataType::Binary).unwrap();
6110        let down_cast = array_ref.as_binary::<i32>();
6111        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6112        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6113        assert!(down_cast.is_null(2));
6114
6115        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6116
6117        let array_ref = cast(&a, &DataType::Binary).unwrap();
6118        let down_cast = array_ref.as_binary::<i32>();
6119        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6120        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6121        assert!(down_cast.is_null(2));
6122    }
6123
6124    #[test]
6125    fn test_numeric_to_large_binary() {
6126        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6127
6128        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6129        let down_cast = array_ref.as_binary::<i64>();
6130        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6131        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6132        assert!(down_cast.is_null(2));
6133
6134        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6135
6136        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6137        let down_cast = array_ref.as_binary::<i64>();
6138        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6139        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6140        assert!(down_cast.is_null(2));
6141    }
6142
6143    #[test]
6144    fn test_cast_date32_to_int32() {
6145        let array = Date32Array::from(vec![10000, 17890]);
6146        let b = cast(&array, &DataType::Int32).unwrap();
6147        let c = b.as_primitive::<Int32Type>();
6148        assert_eq!(10000, c.value(0));
6149        assert_eq!(17890, c.value(1));
6150    }
6151
6152    #[test]
6153    fn test_cast_int32_to_date32() {
6154        let array = Int32Array::from(vec![10000, 17890]);
6155        let b = cast(&array, &DataType::Date32).unwrap();
6156        let c = b.as_primitive::<Date32Type>();
6157        assert_eq!(10000, c.value(0));
6158        assert_eq!(17890, c.value(1));
6159    }
6160
6161    #[test]
6162    fn test_cast_timestamp_to_date32() {
6163        let array =
6164            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6165                .with_timezone("+00:00".to_string());
6166        let b = cast(&array, &DataType::Date32).unwrap();
6167        let c = b.as_primitive::<Date32Type>();
6168        assert_eq!(10000, c.value(0));
6169        assert_eq!(17890, c.value(1));
6170        assert!(c.is_null(2));
6171    }
6172    #[test]
6173    fn test_cast_timestamp_to_date32_zone() {
6174        let strings = StringArray::from_iter([
6175            Some("1970-01-01T00:00:01"),
6176            Some("1970-01-01T23:59:59"),
6177            None,
6178            Some("2020-03-01T02:00:23+00:00"),
6179        ]);
6180        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6181        let timestamps = cast(&strings, &dt).unwrap();
6182        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6183
6184        let c = dates.as_primitive::<Date32Type>();
6185        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6186        assert_eq!(c.value_as_date(0).unwrap(), expected);
6187        assert_eq!(c.value_as_date(1).unwrap(), expected);
6188        assert!(c.is_null(2));
6189        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6190        assert_eq!(c.value_as_date(3).unwrap(), expected);
6191    }
6192    #[test]
6193    fn test_cast_timestamp_to_date64() {
6194        let array =
6195            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6196        let b = cast(&array, &DataType::Date64).unwrap();
6197        let c = b.as_primitive::<Date64Type>();
6198        assert_eq!(864000000005, c.value(0));
6199        assert_eq!(1545696000001, c.value(1));
6200        assert!(c.is_null(2));
6201
6202        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6203        let b = cast(&array, &DataType::Date64).unwrap();
6204        let c = b.as_primitive::<Date64Type>();
6205        assert_eq!(864000000005000, c.value(0));
6206        assert_eq!(1545696000001000, c.value(1));
6207
6208        // test overflow, safe cast
6209        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6210        let b = cast(&array, &DataType::Date64).unwrap();
6211        assert!(b.is_null(0));
6212        // test overflow, unsafe cast
6213        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6214        let options = CastOptions {
6215            safe: false,
6216            format_options: FormatOptions::default(),
6217        };
6218        let b = cast_with_options(&array, &DataType::Date64, &options);
6219        assert!(b.is_err());
6220    }
6221
6222    #[test]
6223    fn test_cast_timestamp_to_time64() {
6224        // test timestamp secs
6225        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6226            .with_timezone("+01:00".to_string());
6227        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6228        let c = b.as_primitive::<Time64MicrosecondType>();
6229        assert_eq!(3605000000, c.value(0));
6230        assert_eq!(3601000000, c.value(1));
6231        assert!(c.is_null(2));
6232        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6233        let c = b.as_primitive::<Time64NanosecondType>();
6234        assert_eq!(3605000000000, c.value(0));
6235        assert_eq!(3601000000000, c.value(1));
6236        assert!(c.is_null(2));
6237
6238        // test timestamp milliseconds
6239        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6240            .with_timezone("+01:00".to_string());
6241        let array = Arc::new(a) as ArrayRef;
6242        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6243        let c = b.as_primitive::<Time64MicrosecondType>();
6244        assert_eq!(3605000000, c.value(0));
6245        assert_eq!(3601000000, c.value(1));
6246        assert!(c.is_null(2));
6247        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6248        let c = b.as_primitive::<Time64NanosecondType>();
6249        assert_eq!(3605000000000, c.value(0));
6250        assert_eq!(3601000000000, c.value(1));
6251        assert!(c.is_null(2));
6252
6253        // test timestamp microseconds
6254        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6255            .with_timezone("+01:00".to_string());
6256        let array = Arc::new(a) as ArrayRef;
6257        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6258        let c = b.as_primitive::<Time64MicrosecondType>();
6259        assert_eq!(3605000000, c.value(0));
6260        assert_eq!(3601000000, c.value(1));
6261        assert!(c.is_null(2));
6262        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6263        let c = b.as_primitive::<Time64NanosecondType>();
6264        assert_eq!(3605000000000, c.value(0));
6265        assert_eq!(3601000000000, c.value(1));
6266        assert!(c.is_null(2));
6267
6268        // test timestamp nanoseconds
6269        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6270            .with_timezone("+01:00".to_string());
6271        let array = Arc::new(a) as ArrayRef;
6272        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6273        let c = b.as_primitive::<Time64MicrosecondType>();
6274        assert_eq!(3605000000, c.value(0));
6275        assert_eq!(3601000000, c.value(1));
6276        assert!(c.is_null(2));
6277        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6278        let c = b.as_primitive::<Time64NanosecondType>();
6279        assert_eq!(3605000000000, c.value(0));
6280        assert_eq!(3601000000000, c.value(1));
6281        assert!(c.is_null(2));
6282
6283        // test overflow
6284        let a =
6285            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6286        let array = Arc::new(a) as ArrayRef;
6287        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6288        assert!(b.is_err());
6289        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6290        assert!(b.is_err());
6291        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6292        assert!(b.is_err());
6293    }
6294
6295    #[test]
6296    fn test_cast_timestamp_to_time32() {
6297        // test timestamp secs
6298        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6299            .with_timezone("+01:00".to_string());
6300        let array = Arc::new(a) as ArrayRef;
6301        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6302        let c = b.as_primitive::<Time32SecondType>();
6303        assert_eq!(3605, c.value(0));
6304        assert_eq!(3601, c.value(1));
6305        assert!(c.is_null(2));
6306        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6307        let c = b.as_primitive::<Time32MillisecondType>();
6308        assert_eq!(3605000, c.value(0));
6309        assert_eq!(3601000, c.value(1));
6310        assert!(c.is_null(2));
6311
6312        // test timestamp milliseconds
6313        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6314            .with_timezone("+01:00".to_string());
6315        let array = Arc::new(a) as ArrayRef;
6316        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6317        let c = b.as_primitive::<Time32SecondType>();
6318        assert_eq!(3605, c.value(0));
6319        assert_eq!(3601, c.value(1));
6320        assert!(c.is_null(2));
6321        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6322        let c = b.as_primitive::<Time32MillisecondType>();
6323        assert_eq!(3605000, c.value(0));
6324        assert_eq!(3601000, c.value(1));
6325        assert!(c.is_null(2));
6326
6327        // test timestamp microseconds
6328        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6329            .with_timezone("+01:00".to_string());
6330        let array = Arc::new(a) as ArrayRef;
6331        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6332        let c = b.as_primitive::<Time32SecondType>();
6333        assert_eq!(3605, c.value(0));
6334        assert_eq!(3601, c.value(1));
6335        assert!(c.is_null(2));
6336        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6337        let c = b.as_primitive::<Time32MillisecondType>();
6338        assert_eq!(3605000, c.value(0));
6339        assert_eq!(3601000, c.value(1));
6340        assert!(c.is_null(2));
6341
6342        // test timestamp nanoseconds
6343        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6344            .with_timezone("+01:00".to_string());
6345        let array = Arc::new(a) as ArrayRef;
6346        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6347        let c = b.as_primitive::<Time32SecondType>();
6348        assert_eq!(3605, c.value(0));
6349        assert_eq!(3601, c.value(1));
6350        assert!(c.is_null(2));
6351        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6352        let c = b.as_primitive::<Time32MillisecondType>();
6353        assert_eq!(3605000, c.value(0));
6354        assert_eq!(3601000, c.value(1));
6355        assert!(c.is_null(2));
6356
6357        // test overflow
6358        let a =
6359            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6360        let array = Arc::new(a) as ArrayRef;
6361        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6362        assert!(b.is_err());
6363        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6364        assert!(b.is_err());
6365    }
6366
6367    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6368    #[test]
6369    fn test_cast_timestamp_with_timezone_1() {
6370        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6371            Some("2000-01-01T00:00:00.123456789"),
6372            Some("2010-01-01T00:00:00.123456789"),
6373            None,
6374        ]));
6375        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6376        let timestamp_array = cast(&string_array, &to_type).unwrap();
6377
6378        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6379        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6380
6381        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6382        let result = string_array.as_string::<i32>();
6383        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6384        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6385        assert!(result.is_null(2));
6386    }
6387
6388    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6389    #[test]
6390    fn test_cast_timestamp_with_timezone_2() {
6391        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6392            Some("2000-01-01T07:00:00.123456789"),
6393            Some("2010-01-01T07:00:00.123456789"),
6394            None,
6395        ]));
6396        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6397        let timestamp_array = cast(&string_array, &to_type).unwrap();
6398
6399        // Check intermediate representation is correct
6400        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6401        let result = string_array.as_string::<i32>();
6402        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6403        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6404        assert!(result.is_null(2));
6405
6406        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6407        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6408
6409        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6410        let result = string_array.as_string::<i32>();
6411        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6412        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6413        assert!(result.is_null(2));
6414    }
6415
6416    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6417    #[test]
6418    fn test_cast_timestamp_with_timezone_3() {
6419        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6420            Some("2000-01-01T07:00:00.123456789"),
6421            Some("2010-01-01T07:00:00.123456789"),
6422            None,
6423        ]));
6424        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6425        let timestamp_array = cast(&string_array, &to_type).unwrap();
6426
6427        // Check intermediate representation is correct
6428        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6429        let result = string_array.as_string::<i32>();
6430        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6431        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6432        assert!(result.is_null(2));
6433
6434        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6435        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6436
6437        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6438        let result = string_array.as_string::<i32>();
6439        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6440        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6441        assert!(result.is_null(2));
6442    }
6443
6444    #[test]
6445    fn test_cast_date64_to_timestamp() {
6446        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6447        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6448        let c = b.as_primitive::<TimestampSecondType>();
6449        assert_eq!(864000000, c.value(0));
6450        assert_eq!(1545696000, c.value(1));
6451        assert!(c.is_null(2));
6452    }
6453
6454    #[test]
6455    fn test_cast_date64_to_timestamp_ms() {
6456        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6457        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6458        let c = b
6459            .as_any()
6460            .downcast_ref::<TimestampMillisecondArray>()
6461            .unwrap();
6462        assert_eq!(864000000005, c.value(0));
6463        assert_eq!(1545696000001, c.value(1));
6464        assert!(c.is_null(2));
6465    }
6466
6467    #[test]
6468    fn test_cast_date64_to_timestamp_us() {
6469        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6470        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6471        let c = b
6472            .as_any()
6473            .downcast_ref::<TimestampMicrosecondArray>()
6474            .unwrap();
6475        assert_eq!(864000000005000, c.value(0));
6476        assert_eq!(1545696000001000, c.value(1));
6477        assert!(c.is_null(2));
6478    }
6479
6480    #[test]
6481    fn test_cast_date64_to_timestamp_ns() {
6482        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6483        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6484        let c = b
6485            .as_any()
6486            .downcast_ref::<TimestampNanosecondArray>()
6487            .unwrap();
6488        assert_eq!(864000000005000000, c.value(0));
6489        assert_eq!(1545696000001000000, c.value(1));
6490        assert!(c.is_null(2));
6491    }
6492
6493    #[test]
6494    fn test_cast_timestamp_to_i64() {
6495        let array =
6496            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6497                .with_timezone("UTC".to_string());
6498        let b = cast(&array, &DataType::Int64).unwrap();
6499        let c = b.as_primitive::<Int64Type>();
6500        assert_eq!(&DataType::Int64, c.data_type());
6501        assert_eq!(864000000005, c.value(0));
6502        assert_eq!(1545696000001, c.value(1));
6503        assert!(c.is_null(2));
6504    }
6505
6506    macro_rules! assert_cast {
6507        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6508            assert!(can_cast_types($array.data_type(), &$datatype));
6509            let out = cast(&$array, &$datatype).unwrap();
6510            let actual = out
6511                .as_any()
6512                .downcast_ref::<$output_array_type>()
6513                .unwrap()
6514                .into_iter()
6515                .collect::<Vec<_>>();
6516            assert_eq!(actual, $expected);
6517        }};
6518        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6519            assert!(can_cast_types($array.data_type(), &$datatype));
6520            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6521            let actual = out
6522                .as_any()
6523                .downcast_ref::<$output_array_type>()
6524                .unwrap()
6525                .into_iter()
6526                .collect::<Vec<_>>();
6527            assert_eq!(actual, $expected);
6528        }};
6529    }
6530
6531    #[test]
6532    fn test_cast_date32_to_string() {
6533        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6534        let expected = vec![
6535            Some("1970-01-01"),
6536            Some("1997-05-19"),
6537            Some("2005-09-10"),
6538            Some("2018-12-25"),
6539            None,
6540        ];
6541
6542        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6543        assert_cast!(array, DataType::Utf8, StringArray, expected);
6544        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6545    }
6546
6547    #[test]
6548    fn test_cast_date64_to_string() {
6549        let array = Date64Array::from(vec![
6550            Some(0),
6551            Some(10000 * 86400000),
6552            Some(13036 * 86400000),
6553            Some(17890 * 86400000),
6554            None,
6555        ]);
6556        let expected = vec![
6557            Some("1970-01-01T00:00:00"),
6558            Some("1997-05-19T00:00:00"),
6559            Some("2005-09-10T00:00:00"),
6560            Some("2018-12-25T00:00:00"),
6561            None,
6562        ];
6563
6564        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6565        assert_cast!(array, DataType::Utf8, StringArray, expected);
6566        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6567    }
6568
6569    #[test]
6570    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6571        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6572        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6573        let array = Arc::new(a) as ArrayRef;
6574
6575        let b = cast(
6576            &array,
6577            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6578        )
6579        .unwrap();
6580        let c = b.as_primitive::<TimestampSecondType>();
6581        let string_array = cast(&c, &DataType::Utf8).unwrap();
6582        let result = string_array.as_string::<i32>();
6583        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6584
6585        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6586        let c = b.as_primitive::<TimestampSecondType>();
6587        let string_array = cast(&c, &DataType::Utf8).unwrap();
6588        let result = string_array.as_string::<i32>();
6589        assert_eq!("2021-01-01T00:00:00", result.value(0));
6590    }
6591
6592    #[test]
6593    fn test_cast_date32_to_timestamp_with_timezone() {
6594        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6595        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6596        let array = Arc::new(a) as ArrayRef;
6597        let b = cast(
6598            &array,
6599            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6600        )
6601        .unwrap();
6602        let c = b.as_primitive::<TimestampSecondType>();
6603        assert_eq!(1609438500, c.value(0));
6604        assert_eq!(1640974500, c.value(1));
6605        assert!(c.is_null(2));
6606
6607        let string_array = cast(&c, &DataType::Utf8).unwrap();
6608        let result = string_array.as_string::<i32>();
6609        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6610        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6611    }
6612
6613    #[test]
6614    fn test_cast_date32_to_timestamp_with_timezone_ms() {
6615        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6616        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6617        let array = Arc::new(a) as ArrayRef;
6618        let b = cast(
6619            &array,
6620            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6621        )
6622        .unwrap();
6623        let c = b.as_primitive::<TimestampMillisecondType>();
6624        assert_eq!(1609438500000, c.value(0));
6625        assert_eq!(1640974500000, c.value(1));
6626        assert!(c.is_null(2));
6627
6628        let string_array = cast(&c, &DataType::Utf8).unwrap();
6629        let result = string_array.as_string::<i32>();
6630        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6631        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6632    }
6633
6634    #[test]
6635    fn test_cast_date32_to_timestamp_with_timezone_us() {
6636        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6637        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6638        let array = Arc::new(a) as ArrayRef;
6639        let b = cast(
6640            &array,
6641            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6642        )
6643        .unwrap();
6644        let c = b.as_primitive::<TimestampMicrosecondType>();
6645        assert_eq!(1609438500000000, c.value(0));
6646        assert_eq!(1640974500000000, c.value(1));
6647        assert!(c.is_null(2));
6648
6649        let string_array = cast(&c, &DataType::Utf8).unwrap();
6650        let result = string_array.as_string::<i32>();
6651        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6652        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6653    }
6654
6655    #[test]
6656    fn test_cast_date32_to_timestamp_with_timezone_ns() {
6657        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6658        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6659        let array = Arc::new(a) as ArrayRef;
6660        let b = cast(
6661            &array,
6662            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6663        )
6664        .unwrap();
6665        let c = b.as_primitive::<TimestampNanosecondType>();
6666        assert_eq!(1609438500000000000, c.value(0));
6667        assert_eq!(1640974500000000000, c.value(1));
6668        assert!(c.is_null(2));
6669
6670        let string_array = cast(&c, &DataType::Utf8).unwrap();
6671        let result = string_array.as_string::<i32>();
6672        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6673        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6674    }
6675
6676    #[test]
6677    fn test_cast_date64_to_timestamp_with_timezone() {
6678        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6679        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6680        let b = cast(
6681            &array,
6682            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6683        )
6684        .unwrap();
6685
6686        let c = b.as_primitive::<TimestampSecondType>();
6687        assert_eq!(863979300, c.value(0));
6688        assert_eq!(1545675300, c.value(1));
6689        assert!(c.is_null(2));
6690
6691        let string_array = cast(&c, &DataType::Utf8).unwrap();
6692        let result = string_array.as_string::<i32>();
6693        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
6694        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6695    }
6696
6697    #[test]
6698    fn test_cast_date64_to_timestamp_with_timezone_ms() {
6699        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6700        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6701        let b = cast(
6702            &array,
6703            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6704        )
6705        .unwrap();
6706
6707        let c = b.as_primitive::<TimestampMillisecondType>();
6708        assert_eq!(863979300005, c.value(0));
6709        assert_eq!(1545675300001, c.value(1));
6710        assert!(c.is_null(2));
6711
6712        let string_array = cast(&c, &DataType::Utf8).unwrap();
6713        let result = string_array.as_string::<i32>();
6714        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6715        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6716    }
6717
6718    #[test]
6719    fn test_cast_date64_to_timestamp_with_timezone_us() {
6720        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6721        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6722        let b = cast(
6723            &array,
6724            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6725        )
6726        .unwrap();
6727
6728        let c = b.as_primitive::<TimestampMicrosecondType>();
6729        assert_eq!(863979300005000, c.value(0));
6730        assert_eq!(1545675300001000, c.value(1));
6731        assert!(c.is_null(2));
6732
6733        let string_array = cast(&c, &DataType::Utf8).unwrap();
6734        let result = string_array.as_string::<i32>();
6735        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6736        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6737    }
6738
6739    #[test]
6740    fn test_cast_date64_to_timestamp_with_timezone_ns() {
6741        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6742        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6743        let b = cast(
6744            &array,
6745            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6746        )
6747        .unwrap();
6748
6749        let c = b.as_primitive::<TimestampNanosecondType>();
6750        assert_eq!(863979300005000000, c.value(0));
6751        assert_eq!(1545675300001000000, c.value(1));
6752        assert!(c.is_null(2));
6753
6754        let string_array = cast(&c, &DataType::Utf8).unwrap();
6755        let result = string_array.as_string::<i32>();
6756        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6757        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6758    }
6759
6760    #[test]
6761    fn test_cast_timestamp_to_strings() {
6762        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6763        let array =
6764            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6765        let expected = vec![
6766            Some("1997-05-19T00:00:03.005"),
6767            Some("2018-12-25T00:00:02.001"),
6768            None,
6769        ];
6770
6771        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6772        assert_cast!(array, DataType::Utf8, StringArray, expected);
6773        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6774    }
6775
6776    #[test]
6777    fn test_cast_timestamp_to_strings_opt() {
6778        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6779        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6780        let cast_options = CastOptions {
6781            safe: true,
6782            format_options: FormatOptions::default()
6783                .with_timestamp_format(Some(ts_format))
6784                .with_timestamp_tz_format(Some(ts_format)),
6785        };
6786
6787        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6788        let array_without_tz =
6789            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6790        let expected = vec![
6791            Some("1997-05-19 00:00:03.005000"),
6792            Some("2018-12-25 00:00:02.001000"),
6793            None,
6794        ];
6795        assert_cast!(
6796            array_without_tz,
6797            DataType::Utf8View,
6798            StringViewArray,
6799            cast_options,
6800            expected
6801        );
6802        assert_cast!(
6803            array_without_tz,
6804            DataType::Utf8,
6805            StringArray,
6806            cast_options,
6807            expected
6808        );
6809        assert_cast!(
6810            array_without_tz,
6811            DataType::LargeUtf8,
6812            LargeStringArray,
6813            cast_options,
6814            expected
6815        );
6816
6817        let array_with_tz =
6818            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6819                .with_timezone(tz.to_string());
6820        let expected = vec![
6821            Some("1997-05-19 05:45:03.005000"),
6822            Some("2018-12-25 05:45:02.001000"),
6823            None,
6824        ];
6825        assert_cast!(
6826            array_with_tz,
6827            DataType::Utf8View,
6828            StringViewArray,
6829            cast_options,
6830            expected
6831        );
6832        assert_cast!(
6833            array_with_tz,
6834            DataType::Utf8,
6835            StringArray,
6836            cast_options,
6837            expected
6838        );
6839        assert_cast!(
6840            array_with_tz,
6841            DataType::LargeUtf8,
6842            LargeStringArray,
6843            cast_options,
6844            expected
6845        );
6846    }
6847
6848    #[test]
6849    fn test_cast_between_timestamps() {
6850        let array =
6851            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6852        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6853        let c = b.as_primitive::<TimestampSecondType>();
6854        assert_eq!(864000003, c.value(0));
6855        assert_eq!(1545696002, c.value(1));
6856        assert!(c.is_null(2));
6857    }
6858
6859    #[test]
6860    fn test_cast_duration_to_i64() {
6861        let base = vec![5, 6, 7, 8, 100000000];
6862
6863        let duration_arrays = vec![
6864            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6865            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6866            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6867            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6868        ];
6869
6870        for arr in duration_arrays {
6871            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6872            let result = cast(&arr, &DataType::Int64).unwrap();
6873            let result = result.as_primitive::<Int64Type>();
6874            assert_eq!(base.as_slice(), result.values());
6875        }
6876    }
6877
6878    #[test]
6879    fn test_cast_between_durations_and_numerics() {
6880        fn test_cast_between_durations<FromType, ToType>()
6881        where
6882            FromType: ArrowPrimitiveType<Native = i64>,
6883            ToType: ArrowPrimitiveType<Native = i64>,
6884            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6885        {
6886            let from_unit = match FromType::DATA_TYPE {
6887                DataType::Duration(unit) => unit,
6888                _ => panic!("Expected a duration type"),
6889            };
6890            let to_unit = match ToType::DATA_TYPE {
6891                DataType::Duration(unit) => unit,
6892                _ => panic!("Expected a duration type"),
6893            };
6894            let from_size = time_unit_multiple(&from_unit);
6895            let to_size = time_unit_multiple(&to_unit);
6896
6897            let (v1_before, v2_before) = (8640003005, 1696002001);
6898            let (v1_after, v2_after) = if from_size >= to_size {
6899                (
6900                    v1_before / (from_size / to_size),
6901                    v2_before / (from_size / to_size),
6902                )
6903            } else {
6904                (
6905                    v1_before * (to_size / from_size),
6906                    v2_before * (to_size / from_size),
6907                )
6908            };
6909
6910            let array =
6911                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6912            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6913            let c = b.as_primitive::<ToType>();
6914            assert_eq!(v1_after, c.value(0));
6915            assert_eq!(v2_after, c.value(1));
6916            assert!(c.is_null(2));
6917        }
6918
6919        // between each individual duration type
6920        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6921        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6922        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6923        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6924        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6925        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6926        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6927        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6928        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6929        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6930        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6931        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6932
6933        // cast failed
6934        let array = DurationSecondArray::from(vec![
6935            Some(i64::MAX),
6936            Some(8640203410378005),
6937            Some(10241096),
6938            None,
6939        ]);
6940        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6941        let c = b.as_primitive::<DurationNanosecondType>();
6942        assert!(c.is_null(0));
6943        assert!(c.is_null(1));
6944        assert_eq!(10241096000000000, c.value(2));
6945        assert!(c.is_null(3));
6946
6947        // durations to numerics
6948        let array = DurationSecondArray::from(vec![
6949            Some(i64::MAX),
6950            Some(8640203410378005),
6951            Some(10241096),
6952            None,
6953        ]);
6954        let b = cast(&array, &DataType::Int64).unwrap();
6955        let c = b.as_primitive::<Int64Type>();
6956        assert_eq!(i64::MAX, c.value(0));
6957        assert_eq!(8640203410378005, c.value(1));
6958        assert_eq!(10241096, c.value(2));
6959        assert!(c.is_null(3));
6960
6961        let b = cast(&array, &DataType::Int32).unwrap();
6962        let c = b.as_primitive::<Int32Type>();
6963        assert_eq!(0, c.value(0));
6964        assert_eq!(0, c.value(1));
6965        assert_eq!(10241096, c.value(2));
6966        assert!(c.is_null(3));
6967
6968        // numerics to durations
6969        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6970        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6971        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6972        assert_eq!(i32::MAX as i64, c.value(0));
6973        assert_eq!(802034103, c.value(1));
6974        assert_eq!(10241096, c.value(2));
6975        assert!(c.is_null(3));
6976    }
6977
6978    #[test]
6979    fn test_cast_to_strings() {
6980        let a = Int32Array::from(vec![1, 2, 3]);
6981        let out = cast(&a, &DataType::Utf8).unwrap();
6982        let out = out
6983            .as_any()
6984            .downcast_ref::<StringArray>()
6985            .unwrap()
6986            .into_iter()
6987            .collect::<Vec<_>>();
6988        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6989        let out = cast(&a, &DataType::LargeUtf8).unwrap();
6990        let out = out
6991            .as_any()
6992            .downcast_ref::<LargeStringArray>()
6993            .unwrap()
6994            .into_iter()
6995            .collect::<Vec<_>>();
6996        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6997    }
6998
6999    #[test]
7000    fn test_str_to_str_casts() {
7001        for data in [
7002            vec![Some("foo"), Some("bar"), Some("ham")],
7003            vec![Some("foo"), None, Some("bar")],
7004        ] {
7005            let a = LargeStringArray::from(data.clone());
7006            let to = cast(&a, &DataType::Utf8).unwrap();
7007            let expect = a
7008                .as_any()
7009                .downcast_ref::<LargeStringArray>()
7010                .unwrap()
7011                .into_iter()
7012                .collect::<Vec<_>>();
7013            let out = to
7014                .as_any()
7015                .downcast_ref::<StringArray>()
7016                .unwrap()
7017                .into_iter()
7018                .collect::<Vec<_>>();
7019            assert_eq!(expect, out);
7020
7021            let a = StringArray::from(data);
7022            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7023            let expect = a
7024                .as_any()
7025                .downcast_ref::<StringArray>()
7026                .unwrap()
7027                .into_iter()
7028                .collect::<Vec<_>>();
7029            let out = to
7030                .as_any()
7031                .downcast_ref::<LargeStringArray>()
7032                .unwrap()
7033                .into_iter()
7034                .collect::<Vec<_>>();
7035            assert_eq!(expect, out);
7036        }
7037    }
7038
7039    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7040        Some("hello"),
7041        Some("repeated"),
7042        None,
7043        Some("large payload over 12 bytes"),
7044        Some("repeated"),
7045    ];
7046
7047    #[test]
7048    fn test_string_view_to_binary_view() {
7049        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7050
7051        assert!(can_cast_types(
7052            string_view_array.data_type(),
7053            &DataType::BinaryView
7054        ));
7055
7056        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7057        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7058
7059        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7060        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7061    }
7062
7063    #[test]
7064    fn test_binary_view_to_string_view() {
7065        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7066
7067        assert!(can_cast_types(
7068            binary_view_array.data_type(),
7069            &DataType::Utf8View
7070        ));
7071
7072        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7073        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7074
7075        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7076        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7077    }
7078
7079    #[test]
7080    fn test_binary_view_to_string_view_with_invalid_utf8() {
7081        let binary_view_array = BinaryViewArray::from_iter(vec![
7082            Some("valid".as_bytes()),
7083            Some(&[0xff]),
7084            Some("utf8".as_bytes()),
7085            None,
7086        ]);
7087
7088        let strict_options = CastOptions {
7089            safe: false,
7090            ..Default::default()
7091        };
7092
7093        assert!(
7094            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7095        );
7096
7097        let safe_options = CastOptions {
7098            safe: true,
7099            ..Default::default()
7100        };
7101
7102        let string_view_array =
7103            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7104        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7105
7106        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7107
7108        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7109    }
7110
7111    #[test]
7112    fn test_string_to_view() {
7113        _test_string_to_view::<i32>();
7114        _test_string_to_view::<i64>();
7115    }
7116
7117    fn _test_string_to_view<O>()
7118    where
7119        O: OffsetSizeTrait,
7120    {
7121        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7122
7123        assert!(can_cast_types(
7124            string_array.data_type(),
7125            &DataType::Utf8View
7126        ));
7127
7128        assert!(can_cast_types(
7129            string_array.data_type(),
7130            &DataType::BinaryView
7131        ));
7132
7133        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7134        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7135
7136        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7137        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7138
7139        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7140        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7141
7142        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7143        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7144    }
7145
7146    #[test]
7147    fn test_bianry_to_view() {
7148        _test_binary_to_view::<i32>();
7149        _test_binary_to_view::<i64>();
7150    }
7151
7152    fn _test_binary_to_view<O>()
7153    where
7154        O: OffsetSizeTrait,
7155    {
7156        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7157
7158        assert!(can_cast_types(
7159            binary_array.data_type(),
7160            &DataType::Utf8View
7161        ));
7162
7163        assert!(can_cast_types(
7164            binary_array.data_type(),
7165            &DataType::BinaryView
7166        ));
7167
7168        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7169        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7170
7171        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7172        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7173
7174        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7175        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7176
7177        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7178        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7179    }
7180
7181    #[test]
7182    fn test_dict_to_view() {
7183        let values = StringArray::from_iter(VIEW_TEST_DATA);
7184        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7185        let string_dict_array =
7186            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7187        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7188
7189        let string_view_array = {
7190            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7191            for v in typed_dict.into_iter() {
7192                builder.append_option(v);
7193            }
7194            builder.finish()
7195        };
7196        let expected_string_array_type = string_view_array.data_type();
7197        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7198        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7199        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7200
7201        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7202        let binary_dict_array =
7203            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7204        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7205
7206        let binary_view_array = {
7207            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7208            for v in typed_binary_dict.into_iter() {
7209                builder.append_option(v);
7210            }
7211            builder.finish()
7212        };
7213        let expected_binary_array_type = binary_view_array.data_type();
7214        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7215        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7216        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7217    }
7218
7219    #[test]
7220    fn test_view_to_dict() {
7221        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7222        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7223        let casted_type = string_dict_array.data_type();
7224        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7225        assert_eq!(casted_dict_array.data_type(), casted_type);
7226        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7227
7228        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7229        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7230        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7231        let binary_dict_array =
7232            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7233        let casted_type = binary_dict_array.data_type();
7234        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7235        assert_eq!(casted_binary_array.data_type(), casted_type);
7236        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7237    }
7238
7239    #[test]
7240    fn test_view_to_string() {
7241        _test_view_to_string::<i32>();
7242        _test_view_to_string::<i64>();
7243    }
7244
7245    fn _test_view_to_string<O>()
7246    where
7247        O: OffsetSizeTrait,
7248    {
7249        let string_view_array = {
7250            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7251            for s in VIEW_TEST_DATA.iter() {
7252                builder.append_option(*s);
7253            }
7254            builder.finish()
7255        };
7256
7257        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7258
7259        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7260        let expected_type = expected_string_array.data_type();
7261
7262        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7263        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7264
7265        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7266        assert_eq!(string_view_casted_array.data_type(), expected_type);
7267        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7268
7269        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7270        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7271        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7272    }
7273
7274    #[test]
7275    fn test_view_to_binary() {
7276        _test_view_to_binary::<i32>();
7277        _test_view_to_binary::<i64>();
7278    }
7279
7280    fn _test_view_to_binary<O>()
7281    where
7282        O: OffsetSizeTrait,
7283    {
7284        let view_array = {
7285            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7286            for s in VIEW_TEST_DATA.iter() {
7287                builder.append_option(*s);
7288            }
7289            builder.finish()
7290        };
7291
7292        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7293        let expected_type = expected_binary_array.data_type();
7294
7295        assert!(can_cast_types(view_array.data_type(), expected_type));
7296
7297        let binary_array = cast(&view_array, expected_type).unwrap();
7298        assert_eq!(binary_array.data_type(), expected_type);
7299
7300        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7301    }
7302
7303    #[test]
7304    fn test_cast_from_f64() {
7305        let f64_values: Vec<f64> = vec![
7306            i64::MIN as f64,
7307            i32::MIN as f64,
7308            i16::MIN as f64,
7309            i8::MIN as f64,
7310            0_f64,
7311            u8::MAX as f64,
7312            u16::MAX as f64,
7313            u32::MAX as f64,
7314            u64::MAX as f64,
7315        ];
7316        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7317
7318        let f64_expected = vec![
7319            -9223372036854776000.0,
7320            -2147483648.0,
7321            -32768.0,
7322            -128.0,
7323            0.0,
7324            255.0,
7325            65535.0,
7326            4294967295.0,
7327            18446744073709552000.0,
7328        ];
7329        assert_eq!(
7330            f64_expected,
7331            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7332                .iter()
7333                .map(|i| i.parse::<f64>().unwrap())
7334                .collect::<Vec<f64>>()
7335        );
7336
7337        let f32_expected = vec![
7338            -9223372000000000000.0,
7339            -2147483600.0,
7340            -32768.0,
7341            -128.0,
7342            0.0,
7343            255.0,
7344            65535.0,
7345            4294967300.0,
7346            18446744000000000000.0,
7347        ];
7348        assert_eq!(
7349            f32_expected,
7350            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7351                .iter()
7352                .map(|i| i.parse::<f32>().unwrap())
7353                .collect::<Vec<f32>>()
7354        );
7355
7356        let f16_expected = vec![
7357            f16::from_f64(-9223372000000000000.0),
7358            f16::from_f64(-2147483600.0),
7359            f16::from_f64(-32768.0),
7360            f16::from_f64(-128.0),
7361            f16::from_f64(0.0),
7362            f16::from_f64(255.0),
7363            f16::from_f64(65535.0),
7364            f16::from_f64(4294967300.0),
7365            f16::from_f64(18446744000000000000.0),
7366        ];
7367        assert_eq!(
7368            f16_expected,
7369            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7370                .iter()
7371                .map(|i| i.parse::<f16>().unwrap())
7372                .collect::<Vec<f16>>()
7373        );
7374
7375        let i64_expected = vec![
7376            "-9223372036854775808",
7377            "-2147483648",
7378            "-32768",
7379            "-128",
7380            "0",
7381            "255",
7382            "65535",
7383            "4294967295",
7384            "null",
7385        ];
7386        assert_eq!(
7387            i64_expected,
7388            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7389        );
7390
7391        let i32_expected = vec![
7392            "null",
7393            "-2147483648",
7394            "-32768",
7395            "-128",
7396            "0",
7397            "255",
7398            "65535",
7399            "null",
7400            "null",
7401        ];
7402        assert_eq!(
7403            i32_expected,
7404            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7405        );
7406
7407        let i16_expected = vec![
7408            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7409        ];
7410        assert_eq!(
7411            i16_expected,
7412            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7413        );
7414
7415        let i8_expected = vec![
7416            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7417        ];
7418        assert_eq!(
7419            i8_expected,
7420            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7421        );
7422
7423        let u64_expected = vec![
7424            "null",
7425            "null",
7426            "null",
7427            "null",
7428            "0",
7429            "255",
7430            "65535",
7431            "4294967295",
7432            "null",
7433        ];
7434        assert_eq!(
7435            u64_expected,
7436            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7437        );
7438
7439        let u32_expected = vec![
7440            "null",
7441            "null",
7442            "null",
7443            "null",
7444            "0",
7445            "255",
7446            "65535",
7447            "4294967295",
7448            "null",
7449        ];
7450        assert_eq!(
7451            u32_expected,
7452            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7453        );
7454
7455        let u16_expected = vec![
7456            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7457        ];
7458        assert_eq!(
7459            u16_expected,
7460            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7461        );
7462
7463        let u8_expected = vec![
7464            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7465        ];
7466        assert_eq!(
7467            u8_expected,
7468            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7469        );
7470    }
7471
7472    #[test]
7473    fn test_cast_from_f32() {
7474        let f32_values: Vec<f32> = vec![
7475            i32::MIN as f32,
7476            i32::MIN as f32,
7477            i16::MIN as f32,
7478            i8::MIN as f32,
7479            0_f32,
7480            u8::MAX as f32,
7481            u16::MAX as f32,
7482            u32::MAX as f32,
7483            u32::MAX as f32,
7484        ];
7485        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7486
7487        let f64_expected = vec![
7488            "-2147483648.0",
7489            "-2147483648.0",
7490            "-32768.0",
7491            "-128.0",
7492            "0.0",
7493            "255.0",
7494            "65535.0",
7495            "4294967296.0",
7496            "4294967296.0",
7497        ];
7498        assert_eq!(
7499            f64_expected,
7500            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7501        );
7502
7503        let f32_expected = vec![
7504            "-2147483600.0",
7505            "-2147483600.0",
7506            "-32768.0",
7507            "-128.0",
7508            "0.0",
7509            "255.0",
7510            "65535.0",
7511            "4294967300.0",
7512            "4294967300.0",
7513        ];
7514        assert_eq!(
7515            f32_expected,
7516            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7517        );
7518
7519        let f16_expected = vec![
7520            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7521        ];
7522        assert_eq!(
7523            f16_expected,
7524            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7525        );
7526
7527        let i64_expected = vec![
7528            "-2147483648",
7529            "-2147483648",
7530            "-32768",
7531            "-128",
7532            "0",
7533            "255",
7534            "65535",
7535            "4294967296",
7536            "4294967296",
7537        ];
7538        assert_eq!(
7539            i64_expected,
7540            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7541        );
7542
7543        let i32_expected = vec![
7544            "-2147483648",
7545            "-2147483648",
7546            "-32768",
7547            "-128",
7548            "0",
7549            "255",
7550            "65535",
7551            "null",
7552            "null",
7553        ];
7554        assert_eq!(
7555            i32_expected,
7556            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7557        );
7558
7559        let i16_expected = vec![
7560            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7561        ];
7562        assert_eq!(
7563            i16_expected,
7564            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7565        );
7566
7567        let i8_expected = vec![
7568            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7569        ];
7570        assert_eq!(
7571            i8_expected,
7572            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7573        );
7574
7575        let u64_expected = vec![
7576            "null",
7577            "null",
7578            "null",
7579            "null",
7580            "0",
7581            "255",
7582            "65535",
7583            "4294967296",
7584            "4294967296",
7585        ];
7586        assert_eq!(
7587            u64_expected,
7588            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7589        );
7590
7591        let u32_expected = vec![
7592            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7593        ];
7594        assert_eq!(
7595            u32_expected,
7596            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7597        );
7598
7599        let u16_expected = vec![
7600            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7601        ];
7602        assert_eq!(
7603            u16_expected,
7604            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7605        );
7606
7607        let u8_expected = vec![
7608            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7609        ];
7610        assert_eq!(
7611            u8_expected,
7612            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7613        );
7614    }
7615
7616    #[test]
7617    fn test_cast_from_uint64() {
7618        let u64_values: Vec<u64> = vec![
7619            0,
7620            u8::MAX as u64,
7621            u16::MAX as u64,
7622            u32::MAX as u64,
7623            u64::MAX,
7624        ];
7625        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7626
7627        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7628        assert_eq!(
7629            f64_expected,
7630            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7631                .iter()
7632                .map(|i| i.parse::<f64>().unwrap())
7633                .collect::<Vec<f64>>()
7634        );
7635
7636        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7637        assert_eq!(
7638            f32_expected,
7639            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7640                .iter()
7641                .map(|i| i.parse::<f32>().unwrap())
7642                .collect::<Vec<f32>>()
7643        );
7644
7645        let f16_expected = vec![
7646            f16::from_f64(0.0),
7647            f16::from_f64(255.0),
7648            f16::from_f64(65535.0),
7649            f16::from_f64(4294967300.0),
7650            f16::from_f64(18446744000000000000.0),
7651        ];
7652        assert_eq!(
7653            f16_expected,
7654            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7655                .iter()
7656                .map(|i| i.parse::<f16>().unwrap())
7657                .collect::<Vec<f16>>()
7658        );
7659
7660        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7661        assert_eq!(
7662            i64_expected,
7663            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7664        );
7665
7666        let i32_expected = vec!["0", "255", "65535", "null", "null"];
7667        assert_eq!(
7668            i32_expected,
7669            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7670        );
7671
7672        let i16_expected = vec!["0", "255", "null", "null", "null"];
7673        assert_eq!(
7674            i16_expected,
7675            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7676        );
7677
7678        let i8_expected = vec!["0", "null", "null", "null", "null"];
7679        assert_eq!(
7680            i8_expected,
7681            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7682        );
7683
7684        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7685        assert_eq!(
7686            u64_expected,
7687            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7688        );
7689
7690        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7691        assert_eq!(
7692            u32_expected,
7693            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7694        );
7695
7696        let u16_expected = vec!["0", "255", "65535", "null", "null"];
7697        assert_eq!(
7698            u16_expected,
7699            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7700        );
7701
7702        let u8_expected = vec!["0", "255", "null", "null", "null"];
7703        assert_eq!(
7704            u8_expected,
7705            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7706        );
7707    }
7708
7709    #[test]
7710    fn test_cast_from_uint32() {
7711        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7712        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7713
7714        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7715        assert_eq!(
7716            f64_expected,
7717            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7718        );
7719
7720        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7721        assert_eq!(
7722            f32_expected,
7723            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7724        );
7725
7726        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7727        assert_eq!(
7728            f16_expected,
7729            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7730        );
7731
7732        let i64_expected = vec!["0", "255", "65535", "4294967295"];
7733        assert_eq!(
7734            i64_expected,
7735            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7736        );
7737
7738        let i32_expected = vec!["0", "255", "65535", "null"];
7739        assert_eq!(
7740            i32_expected,
7741            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7742        );
7743
7744        let i16_expected = vec!["0", "255", "null", "null"];
7745        assert_eq!(
7746            i16_expected,
7747            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7748        );
7749
7750        let i8_expected = vec!["0", "null", "null", "null"];
7751        assert_eq!(
7752            i8_expected,
7753            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7754        );
7755
7756        let u64_expected = vec!["0", "255", "65535", "4294967295"];
7757        assert_eq!(
7758            u64_expected,
7759            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7760        );
7761
7762        let u32_expected = vec!["0", "255", "65535", "4294967295"];
7763        assert_eq!(
7764            u32_expected,
7765            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7766        );
7767
7768        let u16_expected = vec!["0", "255", "65535", "null"];
7769        assert_eq!(
7770            u16_expected,
7771            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7772        );
7773
7774        let u8_expected = vec!["0", "255", "null", "null"];
7775        assert_eq!(
7776            u8_expected,
7777            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7778        );
7779    }
7780
7781    #[test]
7782    fn test_cast_from_uint16() {
7783        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7784        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7785
7786        let f64_expected = vec!["0.0", "255.0", "65535.0"];
7787        assert_eq!(
7788            f64_expected,
7789            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7790        );
7791
7792        let f32_expected = vec!["0.0", "255.0", "65535.0"];
7793        assert_eq!(
7794            f32_expected,
7795            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7796        );
7797
7798        let f16_expected = vec!["0.0", "255.0", "inf"];
7799        assert_eq!(
7800            f16_expected,
7801            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7802        );
7803
7804        let i64_expected = vec!["0", "255", "65535"];
7805        assert_eq!(
7806            i64_expected,
7807            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7808        );
7809
7810        let i32_expected = vec!["0", "255", "65535"];
7811        assert_eq!(
7812            i32_expected,
7813            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7814        );
7815
7816        let i16_expected = vec!["0", "255", "null"];
7817        assert_eq!(
7818            i16_expected,
7819            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7820        );
7821
7822        let i8_expected = vec!["0", "null", "null"];
7823        assert_eq!(
7824            i8_expected,
7825            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7826        );
7827
7828        let u64_expected = vec!["0", "255", "65535"];
7829        assert_eq!(
7830            u64_expected,
7831            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7832        );
7833
7834        let u32_expected = vec!["0", "255", "65535"];
7835        assert_eq!(
7836            u32_expected,
7837            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7838        );
7839
7840        let u16_expected = vec!["0", "255", "65535"];
7841        assert_eq!(
7842            u16_expected,
7843            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7844        );
7845
7846        let u8_expected = vec!["0", "255", "null"];
7847        assert_eq!(
7848            u8_expected,
7849            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7850        );
7851    }
7852
7853    #[test]
7854    fn test_cast_from_uint8() {
7855        let u8_values: Vec<u8> = vec![0, u8::MAX];
7856        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7857
7858        let f64_expected = vec!["0.0", "255.0"];
7859        assert_eq!(
7860            f64_expected,
7861            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7862        );
7863
7864        let f32_expected = vec!["0.0", "255.0"];
7865        assert_eq!(
7866            f32_expected,
7867            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7868        );
7869
7870        let f16_expected = vec!["0.0", "255.0"];
7871        assert_eq!(
7872            f16_expected,
7873            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7874        );
7875
7876        let i64_expected = vec!["0", "255"];
7877        assert_eq!(
7878            i64_expected,
7879            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7880        );
7881
7882        let i32_expected = vec!["0", "255"];
7883        assert_eq!(
7884            i32_expected,
7885            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7886        );
7887
7888        let i16_expected = vec!["0", "255"];
7889        assert_eq!(
7890            i16_expected,
7891            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7892        );
7893
7894        let i8_expected = vec!["0", "null"];
7895        assert_eq!(
7896            i8_expected,
7897            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7898        );
7899
7900        let u64_expected = vec!["0", "255"];
7901        assert_eq!(
7902            u64_expected,
7903            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7904        );
7905
7906        let u32_expected = vec!["0", "255"];
7907        assert_eq!(
7908            u32_expected,
7909            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7910        );
7911
7912        let u16_expected = vec!["0", "255"];
7913        assert_eq!(
7914            u16_expected,
7915            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7916        );
7917
7918        let u8_expected = vec!["0", "255"];
7919        assert_eq!(
7920            u8_expected,
7921            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7922        );
7923    }
7924
7925    #[test]
7926    fn test_cast_from_int64() {
7927        let i64_values: Vec<i64> = vec![
7928            i64::MIN,
7929            i32::MIN as i64,
7930            i16::MIN as i64,
7931            i8::MIN as i64,
7932            0,
7933            i8::MAX as i64,
7934            i16::MAX as i64,
7935            i32::MAX as i64,
7936            i64::MAX,
7937        ];
7938        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7939
7940        let f64_expected = vec![
7941            -9223372036854776000.0,
7942            -2147483648.0,
7943            -32768.0,
7944            -128.0,
7945            0.0,
7946            127.0,
7947            32767.0,
7948            2147483647.0,
7949            9223372036854776000.0,
7950        ];
7951        assert_eq!(
7952            f64_expected,
7953            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7954                .iter()
7955                .map(|i| i.parse::<f64>().unwrap())
7956                .collect::<Vec<f64>>()
7957        );
7958
7959        let f32_expected = vec![
7960            -9223372000000000000.0,
7961            -2147483600.0,
7962            -32768.0,
7963            -128.0,
7964            0.0,
7965            127.0,
7966            32767.0,
7967            2147483600.0,
7968            9223372000000000000.0,
7969        ];
7970        assert_eq!(
7971            f32_expected,
7972            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7973                .iter()
7974                .map(|i| i.parse::<f32>().unwrap())
7975                .collect::<Vec<f32>>()
7976        );
7977
7978        let f16_expected = vec![
7979            f16::from_f64(-9223372000000000000.0),
7980            f16::from_f64(-2147483600.0),
7981            f16::from_f64(-32768.0),
7982            f16::from_f64(-128.0),
7983            f16::from_f64(0.0),
7984            f16::from_f64(127.0),
7985            f16::from_f64(32767.0),
7986            f16::from_f64(2147483600.0),
7987            f16::from_f64(9223372000000000000.0),
7988        ];
7989        assert_eq!(
7990            f16_expected,
7991            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7992                .iter()
7993                .map(|i| i.parse::<f16>().unwrap())
7994                .collect::<Vec<f16>>()
7995        );
7996
7997        let i64_expected = vec![
7998            "-9223372036854775808",
7999            "-2147483648",
8000            "-32768",
8001            "-128",
8002            "0",
8003            "127",
8004            "32767",
8005            "2147483647",
8006            "9223372036854775807",
8007        ];
8008        assert_eq!(
8009            i64_expected,
8010            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8011        );
8012
8013        let i32_expected = vec![
8014            "null",
8015            "-2147483648",
8016            "-32768",
8017            "-128",
8018            "0",
8019            "127",
8020            "32767",
8021            "2147483647",
8022            "null",
8023        ];
8024        assert_eq!(
8025            i32_expected,
8026            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8027        );
8028
8029        assert_eq!(
8030            i32_expected,
8031            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8032        );
8033
8034        let i16_expected = vec![
8035            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8036        ];
8037        assert_eq!(
8038            i16_expected,
8039            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8040        );
8041
8042        let i8_expected = vec![
8043            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8044        ];
8045        assert_eq!(
8046            i8_expected,
8047            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8048        );
8049
8050        let u64_expected = vec![
8051            "null",
8052            "null",
8053            "null",
8054            "null",
8055            "0",
8056            "127",
8057            "32767",
8058            "2147483647",
8059            "9223372036854775807",
8060        ];
8061        assert_eq!(
8062            u64_expected,
8063            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8064        );
8065
8066        let u32_expected = vec![
8067            "null",
8068            "null",
8069            "null",
8070            "null",
8071            "0",
8072            "127",
8073            "32767",
8074            "2147483647",
8075            "null",
8076        ];
8077        assert_eq!(
8078            u32_expected,
8079            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8080        );
8081
8082        let u16_expected = vec![
8083            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8084        ];
8085        assert_eq!(
8086            u16_expected,
8087            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8088        );
8089
8090        let u8_expected = vec![
8091            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8092        ];
8093        assert_eq!(
8094            u8_expected,
8095            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8096        );
8097    }
8098
8099    #[test]
8100    fn test_cast_from_int32() {
8101        let i32_values: Vec<i32> = vec![
8102            i32::MIN,
8103            i16::MIN as i32,
8104            i8::MIN as i32,
8105            0,
8106            i8::MAX as i32,
8107            i16::MAX as i32,
8108            i32::MAX,
8109        ];
8110        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8111
8112        let f64_expected = vec![
8113            "-2147483648.0",
8114            "-32768.0",
8115            "-128.0",
8116            "0.0",
8117            "127.0",
8118            "32767.0",
8119            "2147483647.0",
8120        ];
8121        assert_eq!(
8122            f64_expected,
8123            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8124        );
8125
8126        let f32_expected = vec![
8127            "-2147483600.0",
8128            "-32768.0",
8129            "-128.0",
8130            "0.0",
8131            "127.0",
8132            "32767.0",
8133            "2147483600.0",
8134        ];
8135        assert_eq!(
8136            f32_expected,
8137            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8138        );
8139
8140        let f16_expected = vec![
8141            f16::from_f64(-2147483600.0),
8142            f16::from_f64(-32768.0),
8143            f16::from_f64(-128.0),
8144            f16::from_f64(0.0),
8145            f16::from_f64(127.0),
8146            f16::from_f64(32767.0),
8147            f16::from_f64(2147483600.0),
8148        ];
8149        assert_eq!(
8150            f16_expected,
8151            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8152                .iter()
8153                .map(|i| i.parse::<f16>().unwrap())
8154                .collect::<Vec<f16>>()
8155        );
8156
8157        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8158        assert_eq!(
8159            i16_expected,
8160            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8161        );
8162
8163        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8164        assert_eq!(
8165            i8_expected,
8166            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8167        );
8168
8169        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8170        assert_eq!(
8171            u64_expected,
8172            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8173        );
8174
8175        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8176        assert_eq!(
8177            u32_expected,
8178            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8179        );
8180
8181        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8182        assert_eq!(
8183            u16_expected,
8184            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8185        );
8186
8187        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8188        assert_eq!(
8189            u8_expected,
8190            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8191        );
8192
8193        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8194        let i64_expected = vec![
8195            "-185542587187200000",
8196            "-2831155200000",
8197            "-11059200000",
8198            "0",
8199            "10972800000",
8200            "2831068800000",
8201            "185542587100800000",
8202        ];
8203        assert_eq!(
8204            i64_expected,
8205            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8206        );
8207    }
8208
8209    #[test]
8210    fn test_cast_from_int16() {
8211        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8212        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8213
8214        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8215        assert_eq!(
8216            f64_expected,
8217            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8218        );
8219
8220        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8221        assert_eq!(
8222            f32_expected,
8223            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8224        );
8225
8226        let f16_expected = vec![
8227            f16::from_f64(-32768.0),
8228            f16::from_f64(-128.0),
8229            f16::from_f64(0.0),
8230            f16::from_f64(127.0),
8231            f16::from_f64(32767.0),
8232        ];
8233        assert_eq!(
8234            f16_expected,
8235            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8236                .iter()
8237                .map(|i| i.parse::<f16>().unwrap())
8238                .collect::<Vec<f16>>()
8239        );
8240
8241        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8242        assert_eq!(
8243            i64_expected,
8244            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8245        );
8246
8247        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8248        assert_eq!(
8249            i32_expected,
8250            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8251        );
8252
8253        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8254        assert_eq!(
8255            i16_expected,
8256            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8257        );
8258
8259        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8260        assert_eq!(
8261            i8_expected,
8262            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8263        );
8264
8265        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8266        assert_eq!(
8267            u64_expected,
8268            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8269        );
8270
8271        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8272        assert_eq!(
8273            u32_expected,
8274            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8275        );
8276
8277        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8278        assert_eq!(
8279            u16_expected,
8280            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8281        );
8282
8283        let u8_expected = vec!["null", "null", "0", "127", "null"];
8284        assert_eq!(
8285            u8_expected,
8286            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8287        );
8288    }
8289
8290    #[test]
8291    fn test_cast_from_date32() {
8292        let i32_values: Vec<i32> = vec![
8293            i32::MIN,
8294            i16::MIN as i32,
8295            i8::MIN as i32,
8296            0,
8297            i8::MAX as i32,
8298            i16::MAX as i32,
8299            i32::MAX,
8300        ];
8301        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8302
8303        let i64_expected = vec![
8304            "-2147483648",
8305            "-32768",
8306            "-128",
8307            "0",
8308            "127",
8309            "32767",
8310            "2147483647",
8311        ];
8312        assert_eq!(
8313            i64_expected,
8314            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8315        );
8316    }
8317
8318    #[test]
8319    fn test_cast_from_int8() {
8320        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8321        let i8_array = Int8Array::from(i8_values);
8322
8323        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8324        assert_eq!(
8325            f64_expected,
8326            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8327        );
8328
8329        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8330        assert_eq!(
8331            f32_expected,
8332            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8333        );
8334
8335        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8336        assert_eq!(
8337            f16_expected,
8338            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8339        );
8340
8341        let i64_expected = vec!["-128", "0", "127"];
8342        assert_eq!(
8343            i64_expected,
8344            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8345        );
8346
8347        let i32_expected = vec!["-128", "0", "127"];
8348        assert_eq!(
8349            i32_expected,
8350            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8351        );
8352
8353        let i16_expected = vec!["-128", "0", "127"];
8354        assert_eq!(
8355            i16_expected,
8356            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8357        );
8358
8359        let i8_expected = vec!["-128", "0", "127"];
8360        assert_eq!(
8361            i8_expected,
8362            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8363        );
8364
8365        let u64_expected = vec!["null", "0", "127"];
8366        assert_eq!(
8367            u64_expected,
8368            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8369        );
8370
8371        let u32_expected = vec!["null", "0", "127"];
8372        assert_eq!(
8373            u32_expected,
8374            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8375        );
8376
8377        let u16_expected = vec!["null", "0", "127"];
8378        assert_eq!(
8379            u16_expected,
8380            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8381        );
8382
8383        let u8_expected = vec!["null", "0", "127"];
8384        assert_eq!(
8385            u8_expected,
8386            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8387        );
8388    }
8389
8390    /// Convert `array` into a vector of strings by casting to data type dt
8391    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8392    where
8393        T: ArrowPrimitiveType,
8394    {
8395        let c = cast(array, dt).unwrap();
8396        let a = c.as_primitive::<T>();
8397        let mut v: Vec<String> = vec![];
8398        for i in 0..array.len() {
8399            if a.is_null(i) {
8400                v.push("null".to_string())
8401            } else {
8402                v.push(format!("{:?}", a.value(i)));
8403            }
8404        }
8405        v
8406    }
8407
8408    #[test]
8409    fn test_cast_utf8_dict() {
8410        // FROM a dictionary with of Utf8 values
8411        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8412        builder.append("one").unwrap();
8413        builder.append_null();
8414        builder.append("three").unwrap();
8415        let array: ArrayRef = Arc::new(builder.finish());
8416
8417        let expected = vec!["one", "null", "three"];
8418
8419        // Test casting TO StringArray
8420        let cast_type = Utf8;
8421        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8422        assert_eq!(cast_array.data_type(), &cast_type);
8423        assert_eq!(array_to_strings(&cast_array), expected);
8424
8425        // Test casting TO Dictionary (with different index sizes)
8426
8427        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8428        let cast_array = cast(&array, &cast_type).expect("cast failed");
8429        assert_eq!(cast_array.data_type(), &cast_type);
8430        assert_eq!(array_to_strings(&cast_array), expected);
8431
8432        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8433        let cast_array = cast(&array, &cast_type).expect("cast failed");
8434        assert_eq!(cast_array.data_type(), &cast_type);
8435        assert_eq!(array_to_strings(&cast_array), expected);
8436
8437        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8438        let cast_array = cast(&array, &cast_type).expect("cast failed");
8439        assert_eq!(cast_array.data_type(), &cast_type);
8440        assert_eq!(array_to_strings(&cast_array), expected);
8441
8442        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8443        let cast_array = cast(&array, &cast_type).expect("cast failed");
8444        assert_eq!(cast_array.data_type(), &cast_type);
8445        assert_eq!(array_to_strings(&cast_array), expected);
8446
8447        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8448        let cast_array = cast(&array, &cast_type).expect("cast failed");
8449        assert_eq!(cast_array.data_type(), &cast_type);
8450        assert_eq!(array_to_strings(&cast_array), expected);
8451
8452        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8453        let cast_array = cast(&array, &cast_type).expect("cast failed");
8454        assert_eq!(cast_array.data_type(), &cast_type);
8455        assert_eq!(array_to_strings(&cast_array), expected);
8456
8457        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8458        let cast_array = cast(&array, &cast_type).expect("cast failed");
8459        assert_eq!(cast_array.data_type(), &cast_type);
8460        assert_eq!(array_to_strings(&cast_array), expected);
8461    }
8462
8463    #[test]
8464    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8465        // test converting from an array that has indexes of a type
8466        // that are out of bounds for a particular other kind of
8467        // index.
8468
8469        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8470
8471        // add 200 distinct values (which can be stored by a
8472        // dictionary indexed by int32, but not a dictionary indexed
8473        // with int8)
8474        for i in 0..200 {
8475            builder.append(i).unwrap();
8476        }
8477        let array: ArrayRef = Arc::new(builder.finish());
8478
8479        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8480        let res = cast(&array, &cast_type);
8481        assert!(res.is_err());
8482        let actual_error = format!("{res:?}");
8483        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8484        assert!(
8485            actual_error.contains(expected_error),
8486            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8487        );
8488    }
8489
8490    #[test]
8491    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8492        // Same test as test_cast_dict_to_dict_bad_index_value but use
8493        // string values (and encode the expected behavior here);
8494
8495        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8496
8497        // add 200 distinct values (which can be stored by a
8498        // dictionary indexed by int32, but not a dictionary indexed
8499        // with int8)
8500        for i in 0..200 {
8501            let val = format!("val{i}");
8502            builder.append(&val).unwrap();
8503        }
8504        let array = builder.finish();
8505
8506        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8507        let res = cast(&array, &cast_type);
8508        assert!(res.is_err());
8509        let actual_error = format!("{res:?}");
8510        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8511        assert!(
8512            actual_error.contains(expected_error),
8513            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8514        );
8515    }
8516
8517    #[test]
8518    fn test_cast_primitive_dict() {
8519        // FROM a dictionary with of INT32 values
8520        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8521        builder.append(1).unwrap();
8522        builder.append_null();
8523        builder.append(3).unwrap();
8524        let array: ArrayRef = Arc::new(builder.finish());
8525
8526        let expected = vec!["1", "null", "3"];
8527
8528        // Test casting TO PrimitiveArray, different dictionary type
8529        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8530        assert_eq!(array_to_strings(&cast_array), expected);
8531        assert_eq!(cast_array.data_type(), &Utf8);
8532
8533        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8534        assert_eq!(array_to_strings(&cast_array), expected);
8535        assert_eq!(cast_array.data_type(), &Int64);
8536    }
8537
8538    #[test]
8539    fn test_cast_primitive_array_to_dict() {
8540        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8541        builder.append_value(1);
8542        builder.append_null();
8543        builder.append_value(3);
8544        let array: ArrayRef = Arc::new(builder.finish());
8545
8546        let expected = vec!["1", "null", "3"];
8547
8548        // Cast to a dictionary (same value type, Int32)
8549        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8550        let cast_array = cast(&array, &cast_type).expect("cast failed");
8551        assert_eq!(cast_array.data_type(), &cast_type);
8552        assert_eq!(array_to_strings(&cast_array), expected);
8553
8554        // Cast to a dictionary (different value type, Int8)
8555        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8556        let cast_array = cast(&array, &cast_type).expect("cast failed");
8557        assert_eq!(cast_array.data_type(), &cast_type);
8558        assert_eq!(array_to_strings(&cast_array), expected);
8559    }
8560
8561    #[test]
8562    fn test_cast_time_array_to_dict() {
8563        use DataType::*;
8564
8565        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8566
8567        let expected = vec!["1972-09-27", "null", "1975-06-24"];
8568
8569        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8570        let cast_array = cast(&array, &cast_type).expect("cast failed");
8571        assert_eq!(cast_array.data_type(), &cast_type);
8572        assert_eq!(array_to_strings(&cast_array), expected);
8573    }
8574
8575    #[test]
8576    fn test_cast_timestamp_array_to_dict() {
8577        use DataType::*;
8578
8579        let array = Arc::new(
8580            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8581        ) as ArrayRef;
8582
8583        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8584
8585        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8586        let cast_array = cast(&array, &cast_type).expect("cast failed");
8587        assert_eq!(cast_array.data_type(), &cast_type);
8588        assert_eq!(array_to_strings(&cast_array), expected);
8589    }
8590
8591    #[test]
8592    fn test_cast_string_array_to_dict() {
8593        use DataType::*;
8594
8595        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8596
8597        let expected = vec!["one", "null", "three"];
8598
8599        // Cast to a dictionary (same value type, Utf8)
8600        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8601        let cast_array = cast(&array, &cast_type).expect("cast failed");
8602        assert_eq!(cast_array.data_type(), &cast_type);
8603        assert_eq!(array_to_strings(&cast_array), expected);
8604    }
8605
8606    #[test]
8607    fn test_cast_null_array_to_from_decimal_array() {
8608        let data_type = DataType::Decimal128(12, 4);
8609        let array = new_null_array(&DataType::Null, 4);
8610        assert_eq!(array.data_type(), &DataType::Null);
8611        let cast_array = cast(&array, &data_type).expect("cast failed");
8612        assert_eq!(cast_array.data_type(), &data_type);
8613        for i in 0..4 {
8614            assert!(cast_array.is_null(i));
8615        }
8616
8617        let array = new_null_array(&data_type, 4);
8618        assert_eq!(array.data_type(), &data_type);
8619        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8620        assert_eq!(cast_array.data_type(), &DataType::Null);
8621        assert_eq!(cast_array.len(), 4);
8622        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8623    }
8624
8625    #[test]
8626    fn test_cast_null_array_from_and_to_primitive_array() {
8627        macro_rules! typed_test {
8628            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8629                {
8630                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
8631                    let expected = $ARR_TYPE::from(vec![None; 6]);
8632                    let cast_type = DataType::$DATATYPE;
8633                    let cast_array = cast(&array, &cast_type).expect("cast failed");
8634                    let cast_array = cast_array.as_primitive::<$TYPE>();
8635                    assert_eq!(cast_array.data_type(), &cast_type);
8636                    assert_eq!(cast_array, &expected);
8637                }
8638            }};
8639        }
8640
8641        typed_test!(Int16Array, Int16, Int16Type);
8642        typed_test!(Int32Array, Int32, Int32Type);
8643        typed_test!(Int64Array, Int64, Int64Type);
8644
8645        typed_test!(UInt16Array, UInt16, UInt16Type);
8646        typed_test!(UInt32Array, UInt32, UInt32Type);
8647        typed_test!(UInt64Array, UInt64, UInt64Type);
8648
8649        typed_test!(Float16Array, Float16, Float16Type);
8650        typed_test!(Float32Array, Float32, Float32Type);
8651        typed_test!(Float64Array, Float64, Float64Type);
8652
8653        typed_test!(Date32Array, Date32, Date32Type);
8654        typed_test!(Date64Array, Date64, Date64Type);
8655    }
8656
8657    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
8658        // Cast from null to data_type
8659        let array = new_null_array(&DataType::Null, 4);
8660        assert_eq!(array.data_type(), &DataType::Null);
8661        let cast_array = cast(&array, data_type).expect("cast failed");
8662        assert_eq!(cast_array.data_type(), data_type);
8663        for i in 0..4 {
8664            if is_complex {
8665                assert!(cast_array.logical_nulls().unwrap().is_null(i));
8666            } else {
8667                assert!(cast_array.is_null(i));
8668            }
8669        }
8670    }
8671
8672    fn cast_from_null_to_other(data_type: &DataType) {
8673        cast_from_null_to_other_base(data_type, false);
8674    }
8675
8676    fn cast_from_null_to_other_complex(data_type: &DataType) {
8677        cast_from_null_to_other_base(data_type, true);
8678    }
8679
8680    #[test]
8681    fn test_cast_null_from_and_to_variable_sized() {
8682        cast_from_null_to_other(&DataType::Utf8);
8683        cast_from_null_to_other(&DataType::LargeUtf8);
8684        cast_from_null_to_other(&DataType::Binary);
8685        cast_from_null_to_other(&DataType::LargeBinary);
8686    }
8687
8688    #[test]
8689    fn test_cast_null_from_and_to_nested_type() {
8690        // Cast null from and to map
8691        let data_type = DataType::Map(
8692            Arc::new(Field::new_struct(
8693                "entry",
8694                vec![
8695                    Field::new("key", DataType::Utf8, false),
8696                    Field::new("value", DataType::Int32, true),
8697                ],
8698                false,
8699            )),
8700            false,
8701        );
8702        cast_from_null_to_other(&data_type);
8703
8704        // Cast null from and to list
8705        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8706        cast_from_null_to_other(&data_type);
8707        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8708        cast_from_null_to_other(&data_type);
8709        let data_type =
8710            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8711        cast_from_null_to_other(&data_type);
8712
8713        // Cast null from and to dictionary
8714        let values = vec![None, None, None, None] as Vec<Option<&str>>;
8715        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8716        let array = Arc::new(array) as ArrayRef;
8717        let data_type = array.data_type().to_owned();
8718        cast_from_null_to_other(&data_type);
8719
8720        // Cast null from and to struct
8721        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8722        cast_from_null_to_other(&data_type);
8723
8724        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
8725        cast_from_null_to_other(&target_type);
8726
8727        let target_type =
8728            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
8729        cast_from_null_to_other(&target_type);
8730
8731        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
8732        let target_type = DataType::Union(fields, UnionMode::Sparse);
8733        cast_from_null_to_other_complex(&target_type);
8734
8735        let target_type = DataType::RunEndEncoded(
8736            Arc::new(Field::new("item", DataType::Int32, true)),
8737            Arc::new(Field::new("item", DataType::Int32, true)),
8738        );
8739        cast_from_null_to_other_complex(&target_type);
8740    }
8741
8742    /// Print the `DictionaryArray` `array` as a vector of strings
8743    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8744        let options = FormatOptions::new().with_null("null");
8745        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8746        (0..array.len())
8747            .map(|i| formatter.value(i).to_string())
8748            .collect()
8749    }
8750
8751    #[test]
8752    fn test_cast_utf8_to_date32() {
8753        use chrono::NaiveDate;
8754        let from_ymd = chrono::NaiveDate::from_ymd_opt;
8755        let since = chrono::NaiveDate::signed_duration_since;
8756
8757        let a = StringArray::from(vec![
8758            "2000-01-01",          // valid date with leading 0s
8759            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
8760            "2000-2-2",            // valid date without leading 0s
8761            "2000-00-00",          // invalid month and day
8762            "2000",                // just a year is invalid
8763        ]);
8764        let array = Arc::new(a) as ArrayRef;
8765        let b = cast(&array, &DataType::Date32).unwrap();
8766        let c = b.as_primitive::<Date32Type>();
8767
8768        // test valid inputs
8769        let date_value = since(
8770            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8771            from_ymd(1970, 1, 1).unwrap(),
8772        )
8773        .num_days() as i32;
8774        assert!(c.is_valid(0)); // "2000-01-01"
8775        assert_eq!(date_value, c.value(0));
8776
8777        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
8778        assert_eq!(date_value, c.value(1));
8779
8780        let date_value = since(
8781            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8782            from_ymd(1970, 1, 1).unwrap(),
8783        )
8784        .num_days() as i32;
8785        assert!(c.is_valid(2)); // "2000-2-2"
8786        assert_eq!(date_value, c.value(2));
8787
8788        // test invalid inputs
8789        assert!(!c.is_valid(3)); // "2000-00-00"
8790        assert!(!c.is_valid(4)); // "2000"
8791    }
8792
8793    #[test]
8794    fn test_cast_utf8_to_date64() {
8795        let a = StringArray::from(vec![
8796            "2000-01-01T12:00:00", // date + time valid
8797            "2020-12-15T12:34:56", // date + time valid
8798            "2020-2-2T12:34:56",   // valid date time without leading 0s
8799            "2000-00-00T12:00:00", // invalid month and day
8800            "2000-01-01 12:00:00", // missing the 'T'
8801            "2000-01-01",          // just a date is invalid
8802        ]);
8803        let array = Arc::new(a) as ArrayRef;
8804        let b = cast(&array, &DataType::Date64).unwrap();
8805        let c = b.as_primitive::<Date64Type>();
8806
8807        // test valid inputs
8808        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
8809        assert_eq!(946728000000, c.value(0));
8810        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
8811        assert_eq!(1608035696000, c.value(1));
8812        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
8813
8814        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
8815        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
8816        assert_eq!(946728000000, c.value(4));
8817        assert!(c.is_valid(5)); // "2000-01-01"
8818        assert_eq!(946684800000, c.value(5));
8819    }
8820
8821    #[test]
8822    fn test_can_cast_fsl_to_fsl() {
8823        let from_array = Arc::new(
8824            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8825                [Some([Some(1.0), Some(2.0)]), None],
8826                2,
8827            ),
8828        ) as ArrayRef;
8829        let to_array = Arc::new(
8830            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8831                [
8832                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8833                    None,
8834                ],
8835                2,
8836            ),
8837        ) as ArrayRef;
8838
8839        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8840        let actual = cast(&from_array, to_array.data_type()).unwrap();
8841        assert_eq!(actual.data_type(), to_array.data_type());
8842
8843        let invalid_target =
8844            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8845        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8846
8847        let invalid_size =
8848            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8849        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8850    }
8851
8852    #[test]
8853    fn test_can_cast_types_fixed_size_list_to_list() {
8854        // DataType::List
8855        let array1 = make_fixed_size_list_array();
8856        assert!(can_cast_types(
8857            array1.data_type(),
8858            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8859        ));
8860
8861        // DataType::LargeList
8862        let array2 = make_fixed_size_list_array_for_large_list();
8863        assert!(can_cast_types(
8864            array2.data_type(),
8865            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8866        ));
8867    }
8868
8869    #[test]
8870    fn test_cast_fixed_size_list_to_list() {
8871        // Important cases:
8872        // 1. With/without nulls
8873        // 2. List/LargeList/ListView/LargeListView
8874        // 3. With and without inner casts
8875
8876        let cases = [
8877            // fixed_size_list<i32, 2> => list<i32>
8878            (
8879                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8880                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8881                    2,
8882                )) as ArrayRef,
8883                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8884                    Some([Some(1), Some(1)]),
8885                    Some([Some(2), Some(2)]),
8886                ])) as ArrayRef,
8887            ),
8888            // fixed_size_list<i32, 2> => list<i32> (nullable)
8889            (
8890                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8891                    [None, Some([Some(2), Some(2)])],
8892                    2,
8893                )) as ArrayRef,
8894                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8895                    None,
8896                    Some([Some(2), Some(2)]),
8897                ])) as ArrayRef,
8898            ),
8899            // fixed_size_list<i32, 2> => large_list<i64>
8900            (
8901                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8902                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8903                    2,
8904                )) as ArrayRef,
8905                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8906                    Some([Some(1), Some(1)]),
8907                    Some([Some(2), Some(2)]),
8908                ])) as ArrayRef,
8909            ),
8910            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
8911            (
8912                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8913                    [None, Some([Some(2), Some(2)])],
8914                    2,
8915                )) as ArrayRef,
8916                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8917                    None,
8918                    Some([Some(2), Some(2)]),
8919                ])) as ArrayRef,
8920            ),
8921            // fixed_size_list<i32, 2> => list_view<i32>
8922            (
8923                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8924                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8925                    2,
8926                )) as ArrayRef,
8927                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
8928                    Some([Some(1), Some(1)]),
8929                    Some([Some(2), Some(2)]),
8930                ])) as ArrayRef,
8931            ),
8932            // fixed_size_list<i32, 2> => list_view<i32> (nullable)
8933            (
8934                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8935                    [None, Some([Some(2), Some(2)])],
8936                    2,
8937                )) as ArrayRef,
8938                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
8939                    None,
8940                    Some([Some(2), Some(2)]),
8941                ])) as ArrayRef,
8942            ),
8943            // fixed_size_list<i32, 2> => large_list_view<i64>
8944            (
8945                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8946                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8947                    2,
8948                )) as ArrayRef,
8949                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
8950                    [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
8951                )) as ArrayRef,
8952            ),
8953            // fixed_size_list<i32, 2> => large_list_view<i64> (nullable)
8954            (
8955                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8956                    [None, Some([Some(2), Some(2)])],
8957                    2,
8958                )) as ArrayRef,
8959                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
8960                    [None, Some([Some(2), Some(2)])],
8961                )) as ArrayRef,
8962            ),
8963        ];
8964
8965        for (array, expected) in cases {
8966            let array = Arc::new(array) as ArrayRef;
8967
8968            assert!(
8969                can_cast_types(array.data_type(), expected.data_type()),
8970                "can_cast_types claims we cannot cast {:?} to {:?}",
8971                array.data_type(),
8972                expected.data_type()
8973            );
8974
8975            let list_array = cast(&array, expected.data_type())
8976                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8977            assert_eq!(
8978                list_array.as_ref(),
8979                &expected,
8980                "Incorrect result from casting {array:?} to {expected:?}",
8981            );
8982        }
8983    }
8984
8985    #[test]
8986    fn test_cast_utf8_to_list() {
8987        // DataType::List
8988        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8989        let field = Arc::new(Field::new("", DataType::Int32, false));
8990        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8991        let actual = list_array.as_list_opt::<i32>().unwrap();
8992        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8993        assert_eq!(&expect.value(0), &actual.value(0));
8994
8995        // DataType::LargeList
8996        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8997        let actual = list_array.as_list_opt::<i64>().unwrap();
8998        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8999        assert_eq!(&expect.value(0), &actual.value(0));
9000
9001        // DataType::FixedSizeList
9002        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9003        let actual = list_array.as_fixed_size_list_opt().unwrap();
9004        let expect =
9005            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9006        assert_eq!(&expect.value(0), &actual.value(0));
9007    }
9008
9009    #[test]
9010    fn test_cast_single_element_fixed_size_list() {
9011        // FixedSizeList<T>[1] => T
9012        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9013            [(Some([Some(5)]))],
9014            1,
9015        )) as ArrayRef;
9016        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9017        let actual: &Int32Array = casted_array.as_primitive();
9018        let expected = Int32Array::from(vec![Some(5)]);
9019        assert_eq!(&expected, actual);
9020
9021        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
9022        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9023            [(Some([Some(5)]))],
9024            1,
9025        )) as ArrayRef;
9026        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9027        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9028        let expected = Arc::new(FixedSizeListArray::new(
9029            to_field.clone(),
9030            1,
9031            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9032            None,
9033        )) as ArrayRef;
9034        assert_eq!(*expected, *actual);
9035
9036        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
9037        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9038            [(Some([Some(5)]))],
9039            1,
9040        )) as ArrayRef;
9041        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9042        let to_field = Arc::new(Field::new(
9043            "dummy",
9044            DataType::FixedSizeList(to_field_inner.clone(), 1),
9045            false,
9046        ));
9047        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9048        let expected = Arc::new(FixedSizeListArray::new(
9049            to_field.clone(),
9050            1,
9051            Arc::new(FixedSizeListArray::new(
9052                to_field_inner.clone(),
9053                1,
9054                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9055                None,
9056            )) as ArrayRef,
9057            None,
9058        )) as ArrayRef;
9059        assert_eq!(*expected, *actual);
9060
9061        // T => FixedSizeList<T>[1] (non-nullable)
9062        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9063        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9064        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9065        let actual = casted_array.as_fixed_size_list();
9066        let expected = Arc::new(FixedSizeListArray::new(
9067            field.clone(),
9068            1,
9069            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9070            None,
9071        )) as ArrayRef;
9072        assert_eq!(expected.as_ref(), actual);
9073
9074        // T => FixedSizeList<T>[1] (nullable)
9075        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9076        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9077        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9078        let actual = casted_array.as_fixed_size_list();
9079        let expected = Arc::new(FixedSizeListArray::new(
9080            field.clone(),
9081            1,
9082            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9083            None,
9084        )) as ArrayRef;
9085        assert_eq!(expected.as_ref(), actual);
9086    }
9087
9088    #[test]
9089    fn test_cast_list_containers() {
9090        // large-list to list
9091        let array = make_large_list_array();
9092        let list_array = cast(
9093            &array,
9094            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9095        )
9096        .unwrap();
9097        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9098        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9099
9100        assert_eq!(&expected.value(0), &actual.value(0));
9101        assert_eq!(&expected.value(1), &actual.value(1));
9102        assert_eq!(&expected.value(2), &actual.value(2));
9103
9104        // list to large-list
9105        let array = make_list_array();
9106        let large_list_array = cast(
9107            &array,
9108            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9109        )
9110        .unwrap();
9111        let actual = large_list_array
9112            .as_any()
9113            .downcast_ref::<LargeListArray>()
9114            .unwrap();
9115        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9116
9117        assert_eq!(&expected.value(0), &actual.value(0));
9118        assert_eq!(&expected.value(1), &actual.value(1));
9119        assert_eq!(&expected.value(2), &actual.value(2));
9120    }
9121
9122    #[test]
9123    fn test_cast_list_view() {
9124        // cast between list view and list view
9125        let array = make_list_view_array();
9126        let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9127        assert!(can_cast_types(array.data_type(), &to));
9128        let actual = cast(&array, &to).unwrap();
9129        let actual = actual.as_list_view::<i32>();
9130
9131        assert_eq!(
9132            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9133            actual.value(0).as_ref()
9134        );
9135        assert_eq!(
9136            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9137            actual.value(1).as_ref()
9138        );
9139        assert_eq!(
9140            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9141            actual.value(2).as_ref()
9142        );
9143
9144        // cast between large list view and large list view
9145        let array = make_large_list_view_array();
9146        let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9147        assert!(can_cast_types(array.data_type(), &to));
9148        let actual = cast(&array, &to).unwrap();
9149        let actual = actual.as_list_view::<i64>();
9150
9151        assert_eq!(
9152            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9153            actual.value(0).as_ref()
9154        );
9155        assert_eq!(
9156            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9157            actual.value(1).as_ref()
9158        );
9159        assert_eq!(
9160            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9161            actual.value(2).as_ref()
9162        );
9163    }
9164
9165    #[test]
9166    fn test_non_list_to_list_view() {
9167        let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9168        let expected_primitive =
9169            Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9170
9171        // [[0], [NULL], [2]]
9172        let expected = ListViewArray::new(
9173            Field::new_list_field(DataType::Float32, true).into(),
9174            vec![0, 1, 2].into(),
9175            vec![1, 1, 1].into(),
9176            expected_primitive.clone(),
9177            None,
9178        );
9179        assert!(can_cast_types(input.data_type(), expected.data_type()));
9180        let actual = cast(&input, expected.data_type()).unwrap();
9181        assert_eq!(actual.as_ref(), &expected);
9182
9183        // [[0], [NULL], [2]]
9184        let expected = LargeListViewArray::new(
9185            Field::new_list_field(DataType::Float32, true).into(),
9186            vec![0, 1, 2].into(),
9187            vec![1, 1, 1].into(),
9188            expected_primitive.clone(),
9189            None,
9190        );
9191        assert!(can_cast_types(input.data_type(), expected.data_type()));
9192        let actual = cast(&input, expected.data_type()).unwrap();
9193        assert_eq!(actual.as_ref(), &expected);
9194    }
9195
9196    #[test]
9197    fn test_cast_list_to_fsl() {
9198        // There four noteworthy cases we should handle:
9199        // 1. No nulls
9200        // 2. Nulls that are always empty
9201        // 3. Nulls that have varying lengths
9202        // 4. Nulls that are correctly sized (same as target list size)
9203
9204        // Non-null case
9205        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9206        let values = vec![
9207            Some(vec![Some(1), Some(2), Some(3)]),
9208            Some(vec![Some(4), Some(5), Some(6)]),
9209        ];
9210        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9211            values.clone(),
9212        )) as ArrayRef;
9213        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9214            values, 3,
9215        )) as ArrayRef;
9216        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9217        assert_eq!(expected.as_ref(), actual.as_ref());
9218
9219        // Null cases
9220        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9221        let cases = [
9222            (
9223                // Zero-length nulls
9224                vec![1, 2, 3, 4, 5, 6],
9225                vec![3, 0, 3, 0],
9226            ),
9227            (
9228                // Varying-length nulls
9229                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9230                vec![3, 2, 3, 1],
9231            ),
9232            (
9233                // Correctly-sized nulls
9234                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9235                vec![3, 3, 3, 3],
9236            ),
9237            (
9238                // Mixed nulls
9239                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9240                vec![3, 0, 3, 3],
9241            ),
9242        ];
9243        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9244
9245        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9246            vec![
9247                Some(vec![Some(1), Some(2), Some(3)]),
9248                None,
9249                Some(vec![Some(4), Some(5), Some(6)]),
9250                None,
9251            ],
9252            3,
9253        )) as ArrayRef;
9254
9255        for (values, lengths) in cases.iter() {
9256            let array = Arc::new(ListArray::new(
9257                field.clone(),
9258                OffsetBuffer::from_lengths(lengths.clone()),
9259                Arc::new(Int32Array::from(values.clone())),
9260                Some(null_buffer.clone()),
9261            )) as ArrayRef;
9262            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9263            assert_eq!(expected.as_ref(), actual.as_ref());
9264        }
9265    }
9266
9267    #[test]
9268    fn test_cast_list_view_to_fsl() {
9269        // There four noteworthy cases we should handle:
9270        // 1. No nulls
9271        // 2. Nulls that are always empty
9272        // 3. Nulls that have varying lengths
9273        // 4. Nulls that are correctly sized (same as target list size)
9274
9275        // Non-null case
9276        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9277        let values = vec![
9278            Some(vec![Some(1), Some(2), Some(3)]),
9279            Some(vec![Some(4), Some(5), Some(6)]),
9280        ];
9281        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9282            values.clone(),
9283        )) as ArrayRef;
9284        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9285            values, 3,
9286        )) as ArrayRef;
9287        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9288        assert_eq!(expected.as_ref(), actual.as_ref());
9289
9290        // Null cases
9291        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9292        let cases = [
9293            (
9294                // Zero-length nulls
9295                vec![1, 2, 3, 4, 5, 6],
9296                vec![0, 0, 3, 0],
9297                vec![3, 0, 3, 0],
9298            ),
9299            (
9300                // Varying-length nulls
9301                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9302                vec![0, 1, 5, 0],
9303                vec![3, 2, 3, 1],
9304            ),
9305            (
9306                // Correctly-sized nulls
9307                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9308                vec![0, 3, 6, 9],
9309                vec![3, 3, 3, 3],
9310            ),
9311            (
9312                // Mixed nulls
9313                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9314                vec![0, 0, 3, 6],
9315                vec![3, 0, 3, 3],
9316            ),
9317        ];
9318        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9319
9320        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9321            vec![
9322                Some(vec![Some(1), Some(2), Some(3)]),
9323                None,
9324                Some(vec![Some(4), Some(5), Some(6)]),
9325                None,
9326            ],
9327            3,
9328        )) as ArrayRef;
9329
9330        for (values, offsets, lengths) in cases.iter() {
9331            let array = Arc::new(ListViewArray::new(
9332                field.clone(),
9333                offsets.clone().into(),
9334                lengths.clone().into(),
9335                Arc::new(Int32Array::from(values.clone())),
9336                Some(null_buffer.clone()),
9337            )) as ArrayRef;
9338            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9339            assert_eq!(expected.as_ref(), actual.as_ref());
9340        }
9341    }
9342
9343    #[test]
9344    fn test_cast_list_to_fsl_safety() {
9345        let values = vec![
9346            Some(vec![Some(1), Some(2), Some(3)]),
9347            Some(vec![Some(4), Some(5)]),
9348            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9349            Some(vec![Some(3), Some(4), Some(5)]),
9350        ];
9351        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9352            values.clone(),
9353        )) as ArrayRef;
9354
9355        let res = cast_with_options(
9356            array.as_ref(),
9357            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9358            &CastOptions {
9359                safe: false,
9360                ..Default::default()
9361            },
9362        );
9363        assert!(res.is_err());
9364        assert!(
9365            format!("{res:?}")
9366                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9367        );
9368
9369        // When safe=true (default), the cast will fill nulls for lists that are
9370        // too short and truncate lists that are too long.
9371        let res = cast(
9372            array.as_ref(),
9373            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9374        )
9375        .unwrap();
9376        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9377            vec![
9378                Some(vec![Some(1), Some(2), Some(3)]),
9379                None, // Too short -> replaced with null
9380                None, // Too long -> replaced with null
9381                Some(vec![Some(3), Some(4), Some(5)]),
9382            ],
9383            3,
9384        )) as ArrayRef;
9385        assert_eq!(expected.as_ref(), res.as_ref());
9386
9387        // The safe option is false and the source array contains a null list.
9388        // issue: https://github.com/apache/arrow-rs/issues/5642
9389        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9390            Some(vec![Some(1), Some(2), Some(3)]),
9391            None,
9392        ])) as ArrayRef;
9393        let res = cast_with_options(
9394            array.as_ref(),
9395            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9396            &CastOptions {
9397                safe: false,
9398                ..Default::default()
9399            },
9400        )
9401        .unwrap();
9402        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9403            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9404            3,
9405        )) as ArrayRef;
9406        assert_eq!(expected.as_ref(), res.as_ref());
9407    }
9408
9409    #[test]
9410    fn test_cast_list_view_to_fsl_safety() {
9411        let values = vec![
9412            Some(vec![Some(1), Some(2), Some(3)]),
9413            Some(vec![Some(4), Some(5)]),
9414            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9415            Some(vec![Some(3), Some(4), Some(5)]),
9416        ];
9417        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9418            values.clone(),
9419        )) as ArrayRef;
9420
9421        let res = cast_with_options(
9422            array.as_ref(),
9423            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9424            &CastOptions {
9425                safe: false,
9426                ..Default::default()
9427            },
9428        );
9429        assert!(res.is_err());
9430        assert!(
9431            format!("{res:?}")
9432                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9433        );
9434
9435        // When safe=true (default), the cast will fill nulls for lists that are
9436        // too short and truncate lists that are too long.
9437        let res = cast(
9438            array.as_ref(),
9439            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9440        )
9441        .unwrap();
9442        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9443            vec![
9444                Some(vec![Some(1), Some(2), Some(3)]),
9445                None, // Too short -> replaced with null
9446                None, // Too long -> replaced with null
9447                Some(vec![Some(3), Some(4), Some(5)]),
9448            ],
9449            3,
9450        )) as ArrayRef;
9451        assert_eq!(expected.as_ref(), res.as_ref());
9452
9453        // The safe option is false and the source array contains a null list.
9454        // issue: https://github.com/apache/arrow-rs/issues/5642
9455        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
9456            Some(vec![Some(1), Some(2), Some(3)]),
9457            None,
9458        ])) as ArrayRef;
9459        let res = cast_with_options(
9460            array.as_ref(),
9461            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9462            &CastOptions {
9463                safe: false,
9464                ..Default::default()
9465            },
9466        )
9467        .unwrap();
9468        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9469            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9470            3,
9471        )) as ArrayRef;
9472        assert_eq!(expected.as_ref(), res.as_ref());
9473    }
9474
9475    #[test]
9476    fn test_cast_large_list_to_fsl() {
9477        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
9478        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9479            values.clone(),
9480            2,
9481        )) as ArrayRef;
9482        let target_type =
9483            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
9484
9485        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9486            values.clone(),
9487        )) as ArrayRef;
9488        let actual = cast(array.as_ref(), &target_type).unwrap();
9489        assert_eq!(expected.as_ref(), actual.as_ref());
9490
9491        let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
9492            values.clone(),
9493        )) as ArrayRef;
9494        let actual = cast(array.as_ref(), &target_type).unwrap();
9495        assert_eq!(expected.as_ref(), actual.as_ref());
9496    }
9497
9498    #[test]
9499    fn test_cast_list_to_fsl_subcast() {
9500        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9501            vec![
9502                Some(vec![Some(1), Some(2)]),
9503                Some(vec![Some(3), Some(i32::MAX)]),
9504            ],
9505        )) as ArrayRef;
9506        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
9507            vec![
9508                Some(vec![Some(1), Some(2)]),
9509                Some(vec![Some(3), Some(i32::MAX as i64)]),
9510            ],
9511            2,
9512        )) as ArrayRef;
9513        let actual = cast(
9514            array.as_ref(),
9515            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
9516        )
9517        .unwrap();
9518        assert_eq!(expected.as_ref(), actual.as_ref());
9519
9520        let res = cast_with_options(
9521            array.as_ref(),
9522            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
9523            &CastOptions {
9524                safe: false,
9525                ..Default::default()
9526            },
9527        );
9528        assert!(res.is_err());
9529        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
9530    }
9531
9532    #[test]
9533    fn test_cast_list_to_fsl_empty() {
9534        let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
9535        let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
9536        let expected = new_empty_array(&target_type);
9537
9538        // list
9539        let array = new_empty_array(&DataType::List(inner_field.clone()));
9540        assert!(can_cast_types(array.data_type(), &target_type));
9541        let actual = cast(array.as_ref(), &target_type).unwrap();
9542        assert_eq!(expected.as_ref(), actual.as_ref());
9543
9544        // largelist
9545        let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
9546        assert!(can_cast_types(array.data_type(), &target_type));
9547        let actual = cast(array.as_ref(), &target_type).unwrap();
9548        assert_eq!(expected.as_ref(), actual.as_ref());
9549
9550        // listview
9551        let array = new_empty_array(&DataType::ListView(inner_field.clone()));
9552        assert!(can_cast_types(array.data_type(), &target_type));
9553        let actual = cast(array.as_ref(), &target_type).unwrap();
9554        assert_eq!(expected.as_ref(), actual.as_ref());
9555
9556        // largelistview
9557        let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
9558        assert!(can_cast_types(array.data_type(), &target_type));
9559        let actual = cast(array.as_ref(), &target_type).unwrap();
9560        assert_eq!(expected.as_ref(), actual.as_ref());
9561    }
9562
9563    fn make_list_array() -> ArrayRef {
9564        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9565        Arc::new(ListArray::new(
9566            Field::new_list_field(DataType::Int32, true).into(),
9567            OffsetBuffer::from_lengths(vec![3, 3, 2]),
9568            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9569            None,
9570        ))
9571    }
9572
9573    fn make_large_list_array() -> ArrayRef {
9574        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9575        Arc::new(LargeListArray::new(
9576            Field::new_list_field(DataType::Int32, true).into(),
9577            OffsetBuffer::from_lengths(vec![3, 3, 2]),
9578            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9579            None,
9580        ))
9581    }
9582
9583    fn make_list_view_array() -> ArrayRef {
9584        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9585        Arc::new(ListViewArray::new(
9586            Field::new_list_field(DataType::Int32, true).into(),
9587            vec![0, 3, 6].into(),
9588            vec![3, 3, 2].into(),
9589            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9590            None,
9591        ))
9592    }
9593
9594    fn make_large_list_view_array() -> ArrayRef {
9595        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9596        Arc::new(LargeListViewArray::new(
9597            Field::new_list_field(DataType::Int32, true).into(),
9598            vec![0, 3, 6].into(),
9599            vec![3, 3, 2].into(),
9600            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9601            None,
9602        ))
9603    }
9604
9605    fn make_fixed_size_list_array() -> ArrayRef {
9606        // [[0, 1, 2, 3], [4, 5, 6, 7]]
9607        Arc::new(FixedSizeListArray::new(
9608            Field::new_list_field(DataType::Int32, true).into(),
9609            4,
9610            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9611            None,
9612        ))
9613    }
9614
9615    fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
9616        // [[0, 1, 2, 3], [4, 5, 6, 7]]
9617        Arc::new(FixedSizeListArray::new(
9618            Field::new_list_field(DataType::Int64, true).into(),
9619            4,
9620            Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9621            None,
9622        ))
9623    }
9624
9625    #[test]
9626    fn test_cast_map_dont_allow_change_of_order() {
9627        let string_builder = StringBuilder::new();
9628        let value_builder = StringBuilder::new();
9629        let mut builder = MapBuilder::new(
9630            Some(MapFieldNames {
9631                entry: "entries".to_string(),
9632                key: "key".to_string(),
9633                value: "value".to_string(),
9634            }),
9635            string_builder,
9636            value_builder,
9637        );
9638
9639        builder.keys().append_value("0");
9640        builder.values().append_value("test_val_1");
9641        builder.append(true).unwrap();
9642        builder.keys().append_value("1");
9643        builder.values().append_value("test_val_2");
9644        builder.append(true).unwrap();
9645
9646        // map builder returns unsorted map by default
9647        let array = builder.finish();
9648
9649        let new_ordered = true;
9650        let new_type = DataType::Map(
9651            Arc::new(Field::new(
9652                "entries",
9653                DataType::Struct(
9654                    vec![
9655                        Field::new("key", DataType::Utf8, false),
9656                        Field::new("value", DataType::Utf8, false),
9657                    ]
9658                    .into(),
9659                ),
9660                false,
9661            )),
9662            new_ordered,
9663        );
9664
9665        let new_array_result = cast(&array, &new_type.clone());
9666        assert!(!can_cast_types(array.data_type(), &new_type));
9667        let Err(ArrowError::CastError(t)) = new_array_result else {
9668            panic!();
9669        };
9670        assert_eq!(
9671            t,
9672            r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Utf8), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Utf8), sorted) not supported"#
9673        );
9674    }
9675
9676    #[test]
9677    fn test_cast_map_dont_allow_when_container_cant_cast() {
9678        let string_builder = StringBuilder::new();
9679        let value_builder = IntervalDayTimeArray::builder(2);
9680        let mut builder = MapBuilder::new(
9681            Some(MapFieldNames {
9682                entry: "entries".to_string(),
9683                key: "key".to_string(),
9684                value: "value".to_string(),
9685            }),
9686            string_builder,
9687            value_builder,
9688        );
9689
9690        builder.keys().append_value("0");
9691        builder.values().append_value(IntervalDayTime::new(1, 1));
9692        builder.append(true).unwrap();
9693        builder.keys().append_value("1");
9694        builder.values().append_value(IntervalDayTime::new(2, 2));
9695        builder.append(true).unwrap();
9696
9697        // map builder returns unsorted map by default
9698        let array = builder.finish();
9699
9700        let new_ordered = true;
9701        let new_type = DataType::Map(
9702            Arc::new(Field::new(
9703                "entries",
9704                DataType::Struct(
9705                    vec![
9706                        Field::new("key", DataType::Utf8, false),
9707                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
9708                    ]
9709                    .into(),
9710                ),
9711                false,
9712            )),
9713            new_ordered,
9714        );
9715
9716        let new_array_result = cast(&array, &new_type.clone());
9717        assert!(!can_cast_types(array.data_type(), &new_type));
9718        let Err(ArrowError::CastError(t)) = new_array_result else {
9719            panic!();
9720        };
9721        assert_eq!(
9722            t,
9723            r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Interval(DayTime)), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Duration(s)), sorted) not supported"#
9724        );
9725    }
9726
9727    #[test]
9728    fn test_cast_map_field_names() {
9729        let string_builder = StringBuilder::new();
9730        let value_builder = StringBuilder::new();
9731        let mut builder = MapBuilder::new(
9732            Some(MapFieldNames {
9733                entry: "entries".to_string(),
9734                key: "key".to_string(),
9735                value: "value".to_string(),
9736            }),
9737            string_builder,
9738            value_builder,
9739        );
9740
9741        builder.keys().append_value("0");
9742        builder.values().append_value("test_val_1");
9743        builder.append(true).unwrap();
9744        builder.keys().append_value("1");
9745        builder.values().append_value("test_val_2");
9746        builder.append(true).unwrap();
9747        builder.append(false).unwrap();
9748
9749        let array = builder.finish();
9750
9751        let new_type = DataType::Map(
9752            Arc::new(Field::new(
9753                "entries_new",
9754                DataType::Struct(
9755                    vec![
9756                        Field::new("key_new", DataType::Utf8, false),
9757                        Field::new("value_values", DataType::Utf8, false),
9758                    ]
9759                    .into(),
9760                ),
9761                false,
9762            )),
9763            false,
9764        );
9765
9766        assert_ne!(new_type, array.data_type().clone());
9767
9768        let new_array = cast(&array, &new_type.clone()).unwrap();
9769        assert_eq!(new_type, new_array.data_type().clone());
9770        let map_array = new_array.as_map();
9771
9772        assert_ne!(new_type, array.data_type().clone());
9773        assert_eq!(new_type, map_array.data_type().clone());
9774
9775        let key_string = map_array
9776            .keys()
9777            .as_any()
9778            .downcast_ref::<StringArray>()
9779            .unwrap()
9780            .into_iter()
9781            .flatten()
9782            .collect::<Vec<_>>();
9783        assert_eq!(&key_string, &vec!["0", "1"]);
9784
9785        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9786        let values_string = values_string_array
9787            .as_any()
9788            .downcast_ref::<StringArray>()
9789            .unwrap()
9790            .into_iter()
9791            .flatten()
9792            .collect::<Vec<_>>();
9793        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
9794
9795        assert_eq!(
9796            map_array.nulls(),
9797            Some(&NullBuffer::from(vec![true, true, false]))
9798        );
9799    }
9800
9801    #[test]
9802    fn test_cast_map_contained_values() {
9803        let string_builder = StringBuilder::new();
9804        let value_builder = Int8Builder::new();
9805        let mut builder = MapBuilder::new(
9806            Some(MapFieldNames {
9807                entry: "entries".to_string(),
9808                key: "key".to_string(),
9809                value: "value".to_string(),
9810            }),
9811            string_builder,
9812            value_builder,
9813        );
9814
9815        builder.keys().append_value("0");
9816        builder.values().append_value(44);
9817        builder.append(true).unwrap();
9818        builder.keys().append_value("1");
9819        builder.values().append_value(22);
9820        builder.append(true).unwrap();
9821
9822        let array = builder.finish();
9823
9824        let new_type = DataType::Map(
9825            Arc::new(Field::new(
9826                "entries",
9827                DataType::Struct(
9828                    vec![
9829                        Field::new("key", DataType::Utf8, false),
9830                        Field::new("value", DataType::Utf8, false),
9831                    ]
9832                    .into(),
9833                ),
9834                false,
9835            )),
9836            false,
9837        );
9838
9839        let new_array = cast(&array, &new_type.clone()).unwrap();
9840        assert_eq!(new_type, new_array.data_type().clone());
9841        let map_array = new_array.as_map();
9842
9843        assert_ne!(new_type, array.data_type().clone());
9844        assert_eq!(new_type, map_array.data_type().clone());
9845
9846        let key_string = map_array
9847            .keys()
9848            .as_any()
9849            .downcast_ref::<StringArray>()
9850            .unwrap()
9851            .into_iter()
9852            .flatten()
9853            .collect::<Vec<_>>();
9854        assert_eq!(&key_string, &vec!["0", "1"]);
9855
9856        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9857        let values_string = values_string_array
9858            .as_any()
9859            .downcast_ref::<StringArray>()
9860            .unwrap()
9861            .into_iter()
9862            .flatten()
9863            .collect::<Vec<_>>();
9864        assert_eq!(&values_string, &vec!["44", "22"]);
9865    }
9866
9867    #[test]
9868    fn test_utf8_cast_offsets() {
9869        // test if offset of the array is taken into account during cast
9870        let str_array = StringArray::from(vec!["a", "b", "c"]);
9871        let str_array = str_array.slice(1, 2);
9872
9873        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9874
9875        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9876        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9877        assert_eq!(strs, &["b", "c"])
9878    }
9879
9880    #[test]
9881    fn test_list_cast_offsets() {
9882        // test if offset of the array is taken into account during cast
9883        let array1 = make_list_array().slice(1, 2);
9884        let array2 = make_list_array();
9885
9886        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9887        let out1 = cast(&array1, &dt).unwrap();
9888        let out2 = cast(&array2, &dt).unwrap();
9889
9890        assert_eq!(&out1, &out2.slice(1, 2))
9891    }
9892
9893    #[test]
9894    fn test_list_to_string() {
9895        fn assert_cast(array: &ArrayRef, expected: &[&str]) {
9896            assert!(can_cast_types(array.data_type(), &DataType::Utf8));
9897            let out = cast(array, &DataType::Utf8).unwrap();
9898            let out = out
9899                .as_string::<i32>()
9900                .into_iter()
9901                .flatten()
9902                .collect::<Vec<_>>();
9903            assert_eq!(out, expected);
9904
9905            assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
9906            let out = cast(array, &DataType::LargeUtf8).unwrap();
9907            let out = out
9908                .as_string::<i64>()
9909                .into_iter()
9910                .flatten()
9911                .collect::<Vec<_>>();
9912            assert_eq!(out, expected);
9913
9914            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
9915            let out = cast(array, &DataType::Utf8View).unwrap();
9916            let out = out
9917                .as_string_view()
9918                .into_iter()
9919                .flatten()
9920                .collect::<Vec<_>>();
9921            assert_eq!(out, expected);
9922        }
9923
9924        let array = Arc::new(ListArray::new(
9925            Field::new_list_field(DataType::Utf8, true).into(),
9926            OffsetBuffer::from_lengths(vec![3, 3, 2]),
9927            Arc::new(StringArray::from(vec![
9928                "a", "b", "c", "d", "e", "f", "g", "h",
9929            ])),
9930            None,
9931        )) as ArrayRef;
9932
9933        assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
9934
9935        let array = make_list_array();
9936        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9937
9938        let array = make_large_list_array();
9939        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9940
9941        let array = make_list_view_array();
9942        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9943
9944        let array = make_large_list_view_array();
9945        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9946    }
9947
9948    #[test]
9949    fn test_cast_f64_to_decimal128() {
9950        // to reproduce https://github.com/apache/arrow-rs/issues/2997
9951
9952        let decimal_type = DataType::Decimal128(18, 2);
9953        let array = Float64Array::from(vec![
9954            Some(0.0699999999),
9955            Some(0.0659999999),
9956            Some(0.0650000000),
9957            Some(0.0649999999),
9958        ]);
9959        let array = Arc::new(array) as ArrayRef;
9960        generate_cast_test_case!(
9961            &array,
9962            Decimal128Array,
9963            &decimal_type,
9964            vec![
9965                Some(7_i128), // round up
9966                Some(7_i128), // round up
9967                Some(7_i128), // round up
9968                Some(6_i128), // round down
9969            ]
9970        );
9971
9972        let decimal_type = DataType::Decimal128(18, 3);
9973        let array = Float64Array::from(vec![
9974            Some(0.0699999999),
9975            Some(0.0659999999),
9976            Some(0.0650000000),
9977            Some(0.0649999999),
9978        ]);
9979        let array = Arc::new(array) as ArrayRef;
9980        generate_cast_test_case!(
9981            &array,
9982            Decimal128Array,
9983            &decimal_type,
9984            vec![
9985                Some(70_i128), // round up
9986                Some(66_i128), // round up
9987                Some(65_i128), // round down
9988                Some(65_i128), // round up
9989            ]
9990        );
9991    }
9992
9993    #[test]
9994    fn test_cast_numeric_to_decimal128_overflow() {
9995        let array = Int64Array::from(vec![i64::MAX]);
9996        let array = Arc::new(array) as ArrayRef;
9997        let casted_array = cast_with_options(
9998            &array,
9999            &DataType::Decimal128(38, 30),
10000            &CastOptions {
10001                safe: true,
10002                format_options: FormatOptions::default(),
10003            },
10004        );
10005        assert!(casted_array.is_ok());
10006        assert!(casted_array.unwrap().is_null(0));
10007
10008        let casted_array = cast_with_options(
10009            &array,
10010            &DataType::Decimal128(38, 30),
10011            &CastOptions {
10012                safe: false,
10013                format_options: FormatOptions::default(),
10014            },
10015        );
10016        assert!(casted_array.is_err());
10017    }
10018
10019    #[test]
10020    fn test_cast_numeric_to_decimal256_overflow() {
10021        let array = Int64Array::from(vec![i64::MAX]);
10022        let array = Arc::new(array) as ArrayRef;
10023        let casted_array = cast_with_options(
10024            &array,
10025            &DataType::Decimal256(76, 76),
10026            &CastOptions {
10027                safe: true,
10028                format_options: FormatOptions::default(),
10029            },
10030        );
10031        assert!(casted_array.is_ok());
10032        assert!(casted_array.unwrap().is_null(0));
10033
10034        let casted_array = cast_with_options(
10035            &array,
10036            &DataType::Decimal256(76, 76),
10037            &CastOptions {
10038                safe: false,
10039                format_options: FormatOptions::default(),
10040            },
10041        );
10042        assert!(casted_array.is_err());
10043    }
10044
10045    #[test]
10046    fn test_cast_floating_point_to_decimal128_precision_overflow() {
10047        let array = Float64Array::from(vec![1.1]);
10048        let array = Arc::new(array) as ArrayRef;
10049        let casted_array = cast_with_options(
10050            &array,
10051            &DataType::Decimal128(2, 2),
10052            &CastOptions {
10053                safe: true,
10054                format_options: FormatOptions::default(),
10055            },
10056        );
10057        assert!(casted_array.is_ok());
10058        assert!(casted_array.unwrap().is_null(0));
10059
10060        let casted_array = cast_with_options(
10061            &array,
10062            &DataType::Decimal128(2, 2),
10063            &CastOptions {
10064                safe: false,
10065                format_options: FormatOptions::default(),
10066            },
10067        );
10068        let err = casted_array.unwrap_err().to_string();
10069        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10070        assert!(
10071            err.contains(expected_error),
10072            "did not find expected error '{expected_error}' in actual error '{err}'"
10073        );
10074    }
10075
10076    #[test]
10077    fn test_cast_floating_point_to_decimal256_precision_overflow() {
10078        let array = Float64Array::from(vec![1.1]);
10079        let array = Arc::new(array) as ArrayRef;
10080        let casted_array = cast_with_options(
10081            &array,
10082            &DataType::Decimal256(2, 2),
10083            &CastOptions {
10084                safe: true,
10085                format_options: FormatOptions::default(),
10086            },
10087        );
10088        assert!(casted_array.is_ok());
10089        assert!(casted_array.unwrap().is_null(0));
10090
10091        let casted_array = cast_with_options(
10092            &array,
10093            &DataType::Decimal256(2, 2),
10094            &CastOptions {
10095                safe: false,
10096                format_options: FormatOptions::default(),
10097            },
10098        );
10099        let err = casted_array.unwrap_err().to_string();
10100        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10101        assert_eq!(err, expected_error);
10102    }
10103
10104    #[test]
10105    fn test_cast_floating_point_to_decimal128_overflow() {
10106        let array = Float64Array::from(vec![f64::MAX]);
10107        let array = Arc::new(array) as ArrayRef;
10108        let casted_array = cast_with_options(
10109            &array,
10110            &DataType::Decimal128(38, 30),
10111            &CastOptions {
10112                safe: true,
10113                format_options: FormatOptions::default(),
10114            },
10115        );
10116        assert!(casted_array.is_ok());
10117        assert!(casted_array.unwrap().is_null(0));
10118
10119        let casted_array = cast_with_options(
10120            &array,
10121            &DataType::Decimal128(38, 30),
10122            &CastOptions {
10123                safe: false,
10124                format_options: FormatOptions::default(),
10125            },
10126        );
10127        let err = casted_array.unwrap_err().to_string();
10128        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10129        assert!(
10130            err.contains(expected_error),
10131            "did not find expected error '{expected_error}' in actual error '{err}'"
10132        );
10133    }
10134
10135    #[test]
10136    fn test_cast_floating_point_to_decimal256_overflow() {
10137        let array = Float64Array::from(vec![f64::MAX]);
10138        let array = Arc::new(array) as ArrayRef;
10139        let casted_array = cast_with_options(
10140            &array,
10141            &DataType::Decimal256(76, 50),
10142            &CastOptions {
10143                safe: true,
10144                format_options: FormatOptions::default(),
10145            },
10146        );
10147        assert!(casted_array.is_ok());
10148        assert!(casted_array.unwrap().is_null(0));
10149
10150        let casted_array = cast_with_options(
10151            &array,
10152            &DataType::Decimal256(76, 50),
10153            &CastOptions {
10154                safe: false,
10155                format_options: FormatOptions::default(),
10156            },
10157        );
10158        let err = casted_array.unwrap_err().to_string();
10159        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10160        assert!(
10161            err.contains(expected_error),
10162            "did not find expected error '{expected_error}' in actual error '{err}'"
10163        );
10164    }
10165    #[test]
10166    fn test_cast_decimal256_to_f64_no_overflow() {
10167        // Test casting i256::MAX: should produce a large finite positive value
10168        let array = vec![Some(i256::MAX)];
10169        let array = create_decimal256_array(array, 76, 2).unwrap();
10170        let array = Arc::new(array) as ArrayRef;
10171
10172        let result = cast(&array, &DataType::Float64).unwrap();
10173        let result = result.as_primitive::<Float64Type>();
10174        assert!(result.value(0).is_finite());
10175        assert!(result.value(0) > 0.0); // Positive result
10176
10177        // Test casting i256::MIN: should produce a large finite negative value
10178        let array = vec![Some(i256::MIN)];
10179        let array = create_decimal256_array(array, 76, 2).unwrap();
10180        let array = Arc::new(array) as ArrayRef;
10181
10182        let result = cast(&array, &DataType::Float64).unwrap();
10183        let result = result.as_primitive::<Float64Type>();
10184        assert!(result.value(0).is_finite());
10185        assert!(result.value(0) < 0.0); // Negative result
10186    }
10187
10188    #[test]
10189    fn test_cast_decimal128_to_decimal128_negative_scale() {
10190        let input_type = DataType::Decimal128(20, 0);
10191        let output_type = DataType::Decimal128(20, -1);
10192        assert!(can_cast_types(&input_type, &output_type));
10193        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10194        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10195        let array = Arc::new(input_decimal_array) as ArrayRef;
10196        generate_cast_test_case!(
10197            &array,
10198            Decimal128Array,
10199            &output_type,
10200            vec![
10201                Some(112345_i128),
10202                Some(212346_i128),
10203                Some(312346_i128),
10204                None
10205            ]
10206        );
10207
10208        let casted_array = cast(&array, &output_type).unwrap();
10209        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10210
10211        assert_eq!("1123450", decimal_arr.value_as_string(0));
10212        assert_eq!("2123460", decimal_arr.value_as_string(1));
10213        assert_eq!("3123460", decimal_arr.value_as_string(2));
10214    }
10215
10216    #[test]
10217    fn decimal128_min_max_to_f64() {
10218        // Ensure Decimal128 i128::MIN/MAX round-trip cast
10219        let min128 = i128::MIN;
10220        let max128 = i128::MAX;
10221        assert_eq!(min128 as f64, min128 as f64);
10222        assert_eq!(max128 as f64, max128 as f64);
10223    }
10224
10225    #[test]
10226    fn test_cast_numeric_to_decimal128_negative() {
10227        let decimal_type = DataType::Decimal128(38, -1);
10228        let array = Arc::new(Int32Array::from(vec![
10229            Some(1123456),
10230            Some(2123456),
10231            Some(3123456),
10232        ])) as ArrayRef;
10233
10234        let casted_array = cast(&array, &decimal_type).unwrap();
10235        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10236
10237        assert_eq!("1123450", decimal_arr.value_as_string(0));
10238        assert_eq!("2123450", decimal_arr.value_as_string(1));
10239        assert_eq!("3123450", decimal_arr.value_as_string(2));
10240
10241        let array = Arc::new(Float32Array::from(vec![
10242            Some(1123.456),
10243            Some(2123.456),
10244            Some(3123.456),
10245        ])) as ArrayRef;
10246
10247        let casted_array = cast(&array, &decimal_type).unwrap();
10248        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10249
10250        assert_eq!("1120", decimal_arr.value_as_string(0));
10251        assert_eq!("2120", decimal_arr.value_as_string(1));
10252        assert_eq!("3120", decimal_arr.value_as_string(2));
10253    }
10254
10255    #[test]
10256    fn test_cast_decimal128_to_decimal128_negative() {
10257        let input_type = DataType::Decimal128(10, -1);
10258        let output_type = DataType::Decimal128(10, -2);
10259        assert!(can_cast_types(&input_type, &output_type));
10260        let array = vec![Some(123)];
10261        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10262        let array = Arc::new(input_decimal_array) as ArrayRef;
10263        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10264
10265        let casted_array = cast(&array, &output_type).unwrap();
10266        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10267
10268        assert_eq!("1200", decimal_arr.value_as_string(0));
10269
10270        let array = vec![Some(125)];
10271        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10272        let array = Arc::new(input_decimal_array) as ArrayRef;
10273        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10274
10275        let casted_array = cast(&array, &output_type).unwrap();
10276        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10277
10278        assert_eq!("1300", decimal_arr.value_as_string(0));
10279    }
10280
10281    #[test]
10282    fn test_cast_decimal128_to_decimal256_negative() {
10283        let input_type = DataType::Decimal128(10, 3);
10284        let output_type = DataType::Decimal256(10, 5);
10285        assert!(can_cast_types(&input_type, &output_type));
10286        let array = vec![Some(123456), Some(-123456)];
10287        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10288        let array = Arc::new(input_decimal_array) as ArrayRef;
10289
10290        let hundred = i256::from_i128(100);
10291        generate_cast_test_case!(
10292            &array,
10293            Decimal256Array,
10294            &output_type,
10295            vec![
10296                Some(i256::from_i128(123456).mul_wrapping(hundred)),
10297                Some(i256::from_i128(-123456).mul_wrapping(hundred))
10298            ]
10299        );
10300    }
10301
10302    #[test]
10303    fn test_parse_string_to_decimal() {
10304        assert_eq!(
10305            Decimal128Type::format_decimal(
10306                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10307                38,
10308                2,
10309            ),
10310            "123.45"
10311        );
10312        assert_eq!(
10313            Decimal128Type::format_decimal(
10314                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10315                38,
10316                2,
10317            ),
10318            "12345.00"
10319        );
10320        assert_eq!(
10321            Decimal128Type::format_decimal(
10322                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10323                38,
10324                2,
10325            ),
10326            "0.12"
10327        );
10328        assert_eq!(
10329            Decimal128Type::format_decimal(
10330                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10331                38,
10332                2,
10333            ),
10334            "0.12"
10335        );
10336        assert_eq!(
10337            Decimal128Type::format_decimal(
10338                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10339                38,
10340                2,
10341            ),
10342            "0.13"
10343        );
10344        assert_eq!(
10345            Decimal128Type::format_decimal(
10346                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10347                38,
10348                2,
10349            ),
10350            "0.13"
10351        );
10352
10353        assert_eq!(
10354            Decimal256Type::format_decimal(
10355                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
10356                38,
10357                3,
10358            ),
10359            "123.450"
10360        );
10361        assert_eq!(
10362            Decimal256Type::format_decimal(
10363                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
10364                38,
10365                3,
10366            ),
10367            "12345.000"
10368        );
10369        assert_eq!(
10370            Decimal256Type::format_decimal(
10371                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
10372                38,
10373                3,
10374            ),
10375            "0.123"
10376        );
10377        assert_eq!(
10378            Decimal256Type::format_decimal(
10379                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
10380                38,
10381                3,
10382            ),
10383            "0.123"
10384        );
10385        assert_eq!(
10386            Decimal256Type::format_decimal(
10387                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
10388                38,
10389                3,
10390            ),
10391            "0.127"
10392        );
10393    }
10394
10395    fn test_cast_string_to_decimal(array: ArrayRef) {
10396        // Decimal128
10397        let output_type = DataType::Decimal128(38, 2);
10398        assert!(can_cast_types(array.data_type(), &output_type));
10399
10400        let casted_array = cast(&array, &output_type).unwrap();
10401        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10402
10403        assert_eq!("123.45", decimal_arr.value_as_string(0));
10404        assert_eq!("1.23", decimal_arr.value_as_string(1));
10405        assert_eq!("0.12", decimal_arr.value_as_string(2));
10406        assert_eq!("0.13", decimal_arr.value_as_string(3));
10407        assert_eq!("1.26", decimal_arr.value_as_string(4));
10408        assert_eq!("12345.00", decimal_arr.value_as_string(5));
10409        assert_eq!("12345.00", decimal_arr.value_as_string(6));
10410        assert_eq!("0.12", decimal_arr.value_as_string(7));
10411        assert_eq!("12.23", decimal_arr.value_as_string(8));
10412        assert!(decimal_arr.is_null(9));
10413        assert_eq!("0.00", decimal_arr.value_as_string(10));
10414        assert_eq!("0.00", decimal_arr.value_as_string(11));
10415        assert!(decimal_arr.is_null(12));
10416        assert_eq!("-1.23", decimal_arr.value_as_string(13));
10417        assert_eq!("-1.24", decimal_arr.value_as_string(14));
10418        assert_eq!("0.00", decimal_arr.value_as_string(15));
10419        assert_eq!("-123.00", decimal_arr.value_as_string(16));
10420        assert_eq!("-123.23", decimal_arr.value_as_string(17));
10421        assert_eq!("-0.12", decimal_arr.value_as_string(18));
10422        assert_eq!("1.23", decimal_arr.value_as_string(19));
10423        assert_eq!("1.24", decimal_arr.value_as_string(20));
10424        assert_eq!("0.00", decimal_arr.value_as_string(21));
10425        assert_eq!("123.00", decimal_arr.value_as_string(22));
10426        assert_eq!("123.23", decimal_arr.value_as_string(23));
10427        assert_eq!("0.12", decimal_arr.value_as_string(24));
10428        assert!(decimal_arr.is_null(25));
10429        assert!(decimal_arr.is_null(26));
10430        assert!(decimal_arr.is_null(27));
10431        assert_eq!("0.00", decimal_arr.value_as_string(28));
10432        assert_eq!("0.00", decimal_arr.value_as_string(29));
10433        assert_eq!("12345.00", decimal_arr.value_as_string(30));
10434        assert_eq!(decimal_arr.len(), 31);
10435
10436        // Decimal256
10437        let output_type = DataType::Decimal256(76, 3);
10438        assert!(can_cast_types(array.data_type(), &output_type));
10439
10440        let casted_array = cast(&array, &output_type).unwrap();
10441        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10442
10443        assert_eq!("123.450", decimal_arr.value_as_string(0));
10444        assert_eq!("1.235", decimal_arr.value_as_string(1));
10445        assert_eq!("0.123", decimal_arr.value_as_string(2));
10446        assert_eq!("0.127", decimal_arr.value_as_string(3));
10447        assert_eq!("1.263", decimal_arr.value_as_string(4));
10448        assert_eq!("12345.000", decimal_arr.value_as_string(5));
10449        assert_eq!("12345.000", decimal_arr.value_as_string(6));
10450        assert_eq!("0.123", decimal_arr.value_as_string(7));
10451        assert_eq!("12.234", decimal_arr.value_as_string(8));
10452        assert!(decimal_arr.is_null(9));
10453        assert_eq!("0.000", decimal_arr.value_as_string(10));
10454        assert_eq!("0.000", decimal_arr.value_as_string(11));
10455        assert!(decimal_arr.is_null(12));
10456        assert_eq!("-1.235", decimal_arr.value_as_string(13));
10457        assert_eq!("-1.236", decimal_arr.value_as_string(14));
10458        assert_eq!("0.000", decimal_arr.value_as_string(15));
10459        assert_eq!("-123.000", decimal_arr.value_as_string(16));
10460        assert_eq!("-123.234", decimal_arr.value_as_string(17));
10461        assert_eq!("-0.123", decimal_arr.value_as_string(18));
10462        assert_eq!("1.235", decimal_arr.value_as_string(19));
10463        assert_eq!("1.236", decimal_arr.value_as_string(20));
10464        assert_eq!("0.000", decimal_arr.value_as_string(21));
10465        assert_eq!("123.000", decimal_arr.value_as_string(22));
10466        assert_eq!("123.234", decimal_arr.value_as_string(23));
10467        assert_eq!("0.123", decimal_arr.value_as_string(24));
10468        assert!(decimal_arr.is_null(25));
10469        assert!(decimal_arr.is_null(26));
10470        assert!(decimal_arr.is_null(27));
10471        assert_eq!("0.000", decimal_arr.value_as_string(28));
10472        assert_eq!("0.000", decimal_arr.value_as_string(29));
10473        assert_eq!("12345.000", decimal_arr.value_as_string(30));
10474        assert_eq!(decimal_arr.len(), 31);
10475    }
10476
10477    #[test]
10478    fn test_cast_utf8_to_decimal() {
10479        let str_array = StringArray::from(vec![
10480            Some("123.45"),
10481            Some("1.2345"),
10482            Some("0.12345"),
10483            Some("0.1267"),
10484            Some("1.263"),
10485            Some("12345.0"),
10486            Some("12345"),
10487            Some("000.123"),
10488            Some("12.234000"),
10489            None,
10490            Some(""),
10491            Some(" "),
10492            None,
10493            Some("-1.23499999"),
10494            Some("-1.23599999"),
10495            Some("-0.00001"),
10496            Some("-123"),
10497            Some("-123.234000"),
10498            Some("-000.123"),
10499            Some("+1.23499999"),
10500            Some("+1.23599999"),
10501            Some("+0.00001"),
10502            Some("+123"),
10503            Some("+123.234000"),
10504            Some("+000.123"),
10505            Some("1.-23499999"),
10506            Some("-1.-23499999"),
10507            Some("--1.23499999"),
10508            Some("0"),
10509            Some("000.000"),
10510            Some("0000000000000000012345.000"),
10511        ]);
10512        let array = Arc::new(str_array) as ArrayRef;
10513
10514        test_cast_string_to_decimal(array);
10515
10516        let test_cases = [
10517            (None, None),
10518            // (Some(""), None),
10519            // (Some("   "), None),
10520            (Some("0"), Some("0")),
10521            (Some("000.000"), Some("0")),
10522            (Some("12345"), Some("12345")),
10523            (Some("000000000000000000000000000012345"), Some("12345")),
10524            (Some("-123"), Some("-123")),
10525            (Some("+123"), Some("123")),
10526        ];
10527        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10528        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10529
10530        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
10531        test_cast_string_to_decimal_scale_zero(array, &expected);
10532    }
10533
10534    #[test]
10535    fn test_cast_large_utf8_to_decimal() {
10536        let str_array = LargeStringArray::from(vec![
10537            Some("123.45"),
10538            Some("1.2345"),
10539            Some("0.12345"),
10540            Some("0.1267"),
10541            Some("1.263"),
10542            Some("12345.0"),
10543            Some("12345"),
10544            Some("000.123"),
10545            Some("12.234000"),
10546            None,
10547            Some(""),
10548            Some(" "),
10549            None,
10550            Some("-1.23499999"),
10551            Some("-1.23599999"),
10552            Some("-0.00001"),
10553            Some("-123"),
10554            Some("-123.234000"),
10555            Some("-000.123"),
10556            Some("+1.23499999"),
10557            Some("+1.23599999"),
10558            Some("+0.00001"),
10559            Some("+123"),
10560            Some("+123.234000"),
10561            Some("+000.123"),
10562            Some("1.-23499999"),
10563            Some("-1.-23499999"),
10564            Some("--1.23499999"),
10565            Some("0"),
10566            Some("000.000"),
10567            Some("0000000000000000012345.000"),
10568        ]);
10569        let array = Arc::new(str_array) as ArrayRef;
10570
10571        test_cast_string_to_decimal(array);
10572
10573        let test_cases = [
10574            (None, None),
10575            (Some(""), None),
10576            (Some("   "), None),
10577            (Some("0"), Some("0")),
10578            (Some("000.000"), Some("0")),
10579            (Some("12345"), Some("12345")),
10580            (Some("000000000000000000000000000012345"), Some("12345")),
10581            (Some("-123"), Some("-123")),
10582            (Some("+123"), Some("123")),
10583        ];
10584        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10585        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10586
10587        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
10588        test_cast_string_to_decimal_scale_zero(array, &expected);
10589    }
10590
10591    fn test_cast_string_to_decimal_scale_zero(
10592        array: ArrayRef,
10593        expected_as_string: &[Option<&str>],
10594    ) {
10595        // Decimal128
10596        let output_type = DataType::Decimal128(38, 0);
10597        assert!(can_cast_types(array.data_type(), &output_type));
10598        let casted_array = cast(&array, &output_type).unwrap();
10599        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10600        assert_decimal_array_contents(decimal_arr, expected_as_string);
10601
10602        // Decimal256
10603        let output_type = DataType::Decimal256(76, 0);
10604        assert!(can_cast_types(array.data_type(), &output_type));
10605        let casted_array = cast(&array, &output_type).unwrap();
10606        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10607        assert_decimal_array_contents(decimal_arr, expected_as_string);
10608    }
10609
10610    fn assert_decimal_array_contents<T>(
10611        array: &PrimitiveArray<T>,
10612        expected_as_string: &[Option<&str>],
10613    ) where
10614        T: DecimalType + ArrowPrimitiveType,
10615    {
10616        assert_eq!(array.len(), expected_as_string.len());
10617        for (i, expected) in expected_as_string.iter().enumerate() {
10618            let actual = if array.is_null(i) {
10619                None
10620            } else {
10621                Some(array.value_as_string(i))
10622            };
10623            let actual = actual.as_ref().map(|s| s.as_ref());
10624            assert_eq!(*expected, actual, "Expected at position {i}");
10625        }
10626    }
10627
10628    #[test]
10629    fn test_cast_invalid_utf8_to_decimal() {
10630        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
10631        let array = Arc::new(str_array) as ArrayRef;
10632
10633        // Safe cast
10634        let output_type = DataType::Decimal128(38, 2);
10635        let casted_array = cast(&array, &output_type).unwrap();
10636        assert!(casted_array.is_null(0));
10637        assert!(casted_array.is_null(1));
10638
10639        let output_type = DataType::Decimal256(76, 2);
10640        let casted_array = cast(&array, &output_type).unwrap();
10641        assert!(casted_array.is_null(0));
10642        assert!(casted_array.is_null(1));
10643
10644        // Non-safe cast
10645        let output_type = DataType::Decimal128(38, 2);
10646        let str_array = StringArray::from(vec!["4.4.5"]);
10647        let array = Arc::new(str_array) as ArrayRef;
10648        let option = CastOptions {
10649            safe: false,
10650            format_options: FormatOptions::default(),
10651        };
10652        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10653        assert!(
10654            casted_err
10655                .to_string()
10656                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
10657        );
10658
10659        let str_array = StringArray::from(vec![". 0.123"]);
10660        let array = Arc::new(str_array) as ArrayRef;
10661        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10662        assert!(
10663            casted_err
10664                .to_string()
10665                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
10666        );
10667    }
10668
10669    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
10670        let output_type = DataType::Decimal128(38, 2);
10671        let casted_array = cast(&overflow_array, &output_type).unwrap();
10672        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10673
10674        assert!(decimal_arr.is_null(0));
10675        assert!(decimal_arr.is_null(1));
10676        assert!(decimal_arr.is_null(2));
10677        assert_eq!(
10678            "999999999999999999999999999999999999.99",
10679            decimal_arr.value_as_string(3)
10680        );
10681        assert_eq!(
10682            "100000000000000000000000000000000000.00",
10683            decimal_arr.value_as_string(4)
10684        );
10685    }
10686
10687    #[test]
10688    fn test_cast_string_to_decimal128_precision_overflow() {
10689        let array = StringArray::from(vec!["1000".to_string()]);
10690        let array = Arc::new(array) as ArrayRef;
10691        let casted_array = cast_with_options(
10692            &array,
10693            &DataType::Decimal128(10, 8),
10694            &CastOptions {
10695                safe: true,
10696                format_options: FormatOptions::default(),
10697            },
10698        );
10699        assert!(casted_array.is_ok());
10700        assert!(casted_array.unwrap().is_null(0));
10701
10702        let err = cast_with_options(
10703            &array,
10704            &DataType::Decimal128(10, 8),
10705            &CastOptions {
10706                safe: false,
10707                format_options: FormatOptions::default(),
10708            },
10709        );
10710        assert_eq!(
10711            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
10712            err.unwrap_err().to_string()
10713        );
10714    }
10715
10716    #[test]
10717    fn test_cast_utf8_to_decimal128_overflow() {
10718        let overflow_str_array = StringArray::from(vec![
10719            i128::MAX.to_string(),
10720            i128::MIN.to_string(),
10721            "99999999999999999999999999999999999999".to_string(),
10722            "999999999999999999999999999999999999.99".to_string(),
10723            "99999999999999999999999999999999999.999".to_string(),
10724        ]);
10725        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10726
10727        test_cast_string_to_decimal128_overflow(overflow_array);
10728    }
10729
10730    #[test]
10731    fn test_cast_large_utf8_to_decimal128_overflow() {
10732        let overflow_str_array = LargeStringArray::from(vec![
10733            i128::MAX.to_string(),
10734            i128::MIN.to_string(),
10735            "99999999999999999999999999999999999999".to_string(),
10736            "999999999999999999999999999999999999.99".to_string(),
10737            "99999999999999999999999999999999999.999".to_string(),
10738        ]);
10739        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10740
10741        test_cast_string_to_decimal128_overflow(overflow_array);
10742    }
10743
10744    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
10745        let output_type = DataType::Decimal256(76, 2);
10746        let casted_array = cast(&overflow_array, &output_type).unwrap();
10747        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10748
10749        assert_eq!(
10750            "170141183460469231731687303715884105727.00",
10751            decimal_arr.value_as_string(0)
10752        );
10753        assert_eq!(
10754            "-170141183460469231731687303715884105728.00",
10755            decimal_arr.value_as_string(1)
10756        );
10757        assert_eq!(
10758            "99999999999999999999999999999999999999.00",
10759            decimal_arr.value_as_string(2)
10760        );
10761        assert_eq!(
10762            "999999999999999999999999999999999999.99",
10763            decimal_arr.value_as_string(3)
10764        );
10765        assert_eq!(
10766            "100000000000000000000000000000000000.00",
10767            decimal_arr.value_as_string(4)
10768        );
10769        assert!(decimal_arr.is_null(5));
10770        assert!(decimal_arr.is_null(6));
10771    }
10772
10773    #[test]
10774    fn test_cast_string_to_decimal256_precision_overflow() {
10775        let array = StringArray::from(vec!["1000".to_string()]);
10776        let array = Arc::new(array) as ArrayRef;
10777        let casted_array = cast_with_options(
10778            &array,
10779            &DataType::Decimal256(10, 8),
10780            &CastOptions {
10781                safe: true,
10782                format_options: FormatOptions::default(),
10783            },
10784        );
10785        assert!(casted_array.is_ok());
10786        assert!(casted_array.unwrap().is_null(0));
10787
10788        let err = cast_with_options(
10789            &array,
10790            &DataType::Decimal256(10, 8),
10791            &CastOptions {
10792                safe: false,
10793                format_options: FormatOptions::default(),
10794            },
10795        );
10796        assert_eq!(
10797            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
10798            err.unwrap_err().to_string()
10799        );
10800    }
10801
10802    #[test]
10803    fn test_cast_utf8_to_decimal256_overflow() {
10804        let overflow_str_array = StringArray::from(vec![
10805            i128::MAX.to_string(),
10806            i128::MIN.to_string(),
10807            "99999999999999999999999999999999999999".to_string(),
10808            "999999999999999999999999999999999999.99".to_string(),
10809            "99999999999999999999999999999999999.999".to_string(),
10810            i256::MAX.to_string(),
10811            i256::MIN.to_string(),
10812        ]);
10813        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10814
10815        test_cast_string_to_decimal256_overflow(overflow_array);
10816    }
10817
10818    #[test]
10819    fn test_cast_large_utf8_to_decimal256_overflow() {
10820        let overflow_str_array = LargeStringArray::from(vec![
10821            i128::MAX.to_string(),
10822            i128::MIN.to_string(),
10823            "99999999999999999999999999999999999999".to_string(),
10824            "999999999999999999999999999999999999.99".to_string(),
10825            "99999999999999999999999999999999999.999".to_string(),
10826            i256::MAX.to_string(),
10827            i256::MIN.to_string(),
10828        ]);
10829        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10830
10831        test_cast_string_to_decimal256_overflow(overflow_array);
10832    }
10833
10834    #[test]
10835    fn test_cast_outside_supported_range_for_nanoseconds() {
10836        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";
10837
10838        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
10839
10840        let cast_options = CastOptions {
10841            safe: false,
10842            format_options: FormatOptions::default(),
10843        };
10844
10845        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
10846            &array,
10847            &None::<Arc<str>>,
10848            &cast_options,
10849        );
10850
10851        let err = result.unwrap_err();
10852        assert_eq!(
10853            err.to_string(),
10854            format!(
10855                "Cast error: Overflow converting {} to Nanosecond. {}",
10856                array.value(0),
10857                EXPECTED_ERROR_MESSAGE
10858            )
10859        );
10860    }
10861
10862    #[test]
10863    fn test_cast_date32_to_timestamp() {
10864        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10865        let array = Arc::new(a) as ArrayRef;
10866        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10867        let c = b.as_primitive::<TimestampSecondType>();
10868        assert_eq!(1609459200, c.value(0));
10869        assert_eq!(1640995200, c.value(1));
10870        assert!(c.is_null(2));
10871    }
10872
10873    #[test]
10874    fn test_cast_date32_to_timestamp_ms() {
10875        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10876        let array = Arc::new(a) as ArrayRef;
10877        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10878        let c = b
10879            .as_any()
10880            .downcast_ref::<TimestampMillisecondArray>()
10881            .unwrap();
10882        assert_eq!(1609459200000, c.value(0));
10883        assert_eq!(1640995200000, c.value(1));
10884        assert!(c.is_null(2));
10885    }
10886
10887    #[test]
10888    fn test_cast_date32_to_timestamp_us() {
10889        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10890        let array = Arc::new(a) as ArrayRef;
10891        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10892        let c = b
10893            .as_any()
10894            .downcast_ref::<TimestampMicrosecondArray>()
10895            .unwrap();
10896        assert_eq!(1609459200000000, c.value(0));
10897        assert_eq!(1640995200000000, c.value(1));
10898        assert!(c.is_null(2));
10899    }
10900
10901    #[test]
10902    fn test_cast_date32_to_timestamp_ns() {
10903        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10904        let array = Arc::new(a) as ArrayRef;
10905        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10906        let c = b
10907            .as_any()
10908            .downcast_ref::<TimestampNanosecondArray>()
10909            .unwrap();
10910        assert_eq!(1609459200000000000, c.value(0));
10911        assert_eq!(1640995200000000000, c.value(1));
10912        assert!(c.is_null(2));
10913    }
10914
10915    #[test]
10916    fn test_timezone_cast() {
10917        let a = StringArray::from(vec![
10918            "2000-01-01T12:00:00", // date + time valid
10919            "2020-12-15T12:34:56", // date + time valid
10920        ]);
10921        let array = Arc::new(a) as ArrayRef;
10922        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10923        let v = b.as_primitive::<TimestampNanosecondType>();
10924
10925        assert_eq!(v.value(0), 946728000000000000);
10926        assert_eq!(v.value(1), 1608035696000000000);
10927
10928        let b = cast(
10929            &b,
10930            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10931        )
10932        .unwrap();
10933        let v = b.as_primitive::<TimestampNanosecondType>();
10934
10935        assert_eq!(v.value(0), 946728000000000000);
10936        assert_eq!(v.value(1), 1608035696000000000);
10937
10938        let b = cast(
10939            &b,
10940            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10941        )
10942        .unwrap();
10943        let v = b.as_primitive::<TimestampMillisecondType>();
10944
10945        assert_eq!(v.value(0), 946728000000);
10946        assert_eq!(v.value(1), 1608035696000);
10947    }
10948
10949    #[test]
10950    fn test_cast_utf8_to_timestamp() {
10951        fn test_tz(tz: Arc<str>) {
10952            let valid = StringArray::from(vec![
10953                "2023-01-01 04:05:06.789000-08:00",
10954                "2023-01-01 04:05:06.789000-07:00",
10955                "2023-01-01 04:05:06.789 -0800",
10956                "2023-01-01 04:05:06.789 -08:00",
10957                "2023-01-01 040506 +0730",
10958                "2023-01-01 040506 +07:30",
10959                "2023-01-01 04:05:06.789",
10960                "2023-01-01 04:05:06",
10961                "2023-01-01",
10962            ]);
10963
10964            let array = Arc::new(valid) as ArrayRef;
10965            let b = cast_with_options(
10966                &array,
10967                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
10968                &CastOptions {
10969                    safe: false,
10970                    format_options: FormatOptions::default(),
10971                },
10972            )
10973            .unwrap();
10974
10975            let tz = tz.as_ref().parse().unwrap();
10976
10977            let as_tz =
10978                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
10979
10980            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
10981            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
10982
10983            let values = b.as_primitive::<TimestampNanosecondType>().values();
10984            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
10985            let local_results: Vec<_> = values.iter().map(as_local).collect();
10986
10987            // Absolute timestamps should be parsed preserving the same UTC instant
10988            assert_eq!(
10989                &utc_results[..6],
10990                &[
10991                    "2023-01-01 12:05:06.789".to_string(),
10992                    "2023-01-01 11:05:06.789".to_string(),
10993                    "2023-01-01 12:05:06.789".to_string(),
10994                    "2023-01-01 12:05:06.789".to_string(),
10995                    "2022-12-31 20:35:06".to_string(),
10996                    "2022-12-31 20:35:06".to_string(),
10997                ]
10998            );
10999            // Non-absolute timestamps should be parsed preserving the same local instant
11000            assert_eq!(
11001                &local_results[6..],
11002                &[
11003                    "2023-01-01 04:05:06.789".to_string(),
11004                    "2023-01-01 04:05:06".to_string(),
11005                    "2023-01-01 00:00:00".to_string()
11006                ]
11007            )
11008        }
11009
11010        test_tz("+00:00".into());
11011        test_tz("+02:00".into());
11012    }
11013
11014    #[test]
11015    fn test_cast_invalid_utf8() {
11016        let v1: &[u8] = b"\xFF invalid";
11017        let v2: &[u8] = b"\x00 Foo";
11018        let s = BinaryArray::from(vec![v1, v2]);
11019        let options = CastOptions {
11020            safe: true,
11021            format_options: FormatOptions::default(),
11022        };
11023        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11024        let a = array.as_string::<i32>();
11025        a.to_data().validate_full().unwrap();
11026
11027        assert_eq!(a.null_count(), 1);
11028        assert_eq!(a.len(), 2);
11029        assert!(a.is_null(0));
11030        assert_eq!(a.value(0), "");
11031        assert_eq!(a.value(1), "\x00 Foo");
11032    }
11033
11034    #[test]
11035    fn test_cast_utf8_to_timestamptz() {
11036        let valid = StringArray::from(vec!["2023-01-01"]);
11037
11038        let array = Arc::new(valid) as ArrayRef;
11039        let b = cast(
11040            &array,
11041            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11042        )
11043        .unwrap();
11044
11045        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11046
11047        assert_eq!(b.data_type(), &expect);
11048        let c = b
11049            .as_any()
11050            .downcast_ref::<TimestampNanosecondArray>()
11051            .unwrap();
11052        assert_eq!(1672531200000000000, c.value(0));
11053    }
11054
11055    #[test]
11056    fn test_cast_decimal_to_string() {
11057        assert!(can_cast_types(
11058            &DataType::Decimal32(9, 4),
11059            &DataType::Utf8View
11060        ));
11061        assert!(can_cast_types(
11062            &DataType::Decimal64(16, 4),
11063            &DataType::Utf8View
11064        ));
11065        assert!(can_cast_types(
11066            &DataType::Decimal128(10, 4),
11067            &DataType::Utf8View
11068        ));
11069        assert!(can_cast_types(
11070            &DataType::Decimal256(38, 10),
11071            &DataType::Utf8View
11072        ));
11073
11074        macro_rules! assert_decimal_values {
11075            ($array:expr) => {
11076                let c = $array;
11077                assert_eq!("1123.454", c.value(0));
11078                assert_eq!("2123.456", c.value(1));
11079                assert_eq!("-3123.453", c.value(2));
11080                assert_eq!("-3123.456", c.value(3));
11081                assert_eq!("0.000", c.value(4));
11082                assert_eq!("0.123", c.value(5));
11083                assert_eq!("1234.567", c.value(6));
11084                assert_eq!("-1234.567", c.value(7));
11085                assert!(c.is_null(8));
11086            };
11087        }
11088
11089        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11090            output_type: DataType,
11091            array: PrimitiveArray<IN>,
11092        ) {
11093            let b = cast(&array, &output_type).unwrap();
11094
11095            assert_eq!(b.data_type(), &output_type);
11096            match b.data_type() {
11097                DataType::Utf8View => {
11098                    let c = b.as_string_view();
11099                    assert_decimal_values!(c);
11100                }
11101                DataType::Utf8 | DataType::LargeUtf8 => {
11102                    let c = b.as_string::<OffsetSize>();
11103                    assert_decimal_values!(c);
11104                }
11105                _ => (),
11106            }
11107        }
11108
11109        let array32: Vec<Option<i32>> = vec![
11110            Some(1123454),
11111            Some(2123456),
11112            Some(-3123453),
11113            Some(-3123456),
11114            Some(0),
11115            Some(123),
11116            Some(123456789),
11117            Some(-123456789),
11118            None,
11119        ];
11120        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11121        let array128: Vec<Option<i128>> =
11122            array64.iter().map(|num| num.map(|x| x as i128)).collect();
11123        let array256: Vec<Option<i256>> = array128
11124            .iter()
11125            .map(|num| num.map(i256::from_i128))
11126            .collect();
11127
11128        test_decimal_to_string::<Decimal32Type, i32>(
11129            DataType::Utf8View,
11130            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11131        );
11132        test_decimal_to_string::<Decimal32Type, i32>(
11133            DataType::Utf8,
11134            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11135        );
11136        test_decimal_to_string::<Decimal32Type, i64>(
11137            DataType::LargeUtf8,
11138            create_decimal32_array(array32, 7, 3).unwrap(),
11139        );
11140
11141        test_decimal_to_string::<Decimal64Type, i32>(
11142            DataType::Utf8View,
11143            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11144        );
11145        test_decimal_to_string::<Decimal64Type, i32>(
11146            DataType::Utf8,
11147            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11148        );
11149        test_decimal_to_string::<Decimal64Type, i64>(
11150            DataType::LargeUtf8,
11151            create_decimal64_array(array64, 7, 3).unwrap(),
11152        );
11153
11154        test_decimal_to_string::<Decimal128Type, i32>(
11155            DataType::Utf8View,
11156            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11157        );
11158        test_decimal_to_string::<Decimal128Type, i32>(
11159            DataType::Utf8,
11160            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11161        );
11162        test_decimal_to_string::<Decimal128Type, i64>(
11163            DataType::LargeUtf8,
11164            create_decimal128_array(array128, 7, 3).unwrap(),
11165        );
11166
11167        test_decimal_to_string::<Decimal256Type, i32>(
11168            DataType::Utf8View,
11169            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11170        );
11171        test_decimal_to_string::<Decimal256Type, i32>(
11172            DataType::Utf8,
11173            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11174        );
11175        test_decimal_to_string::<Decimal256Type, i64>(
11176            DataType::LargeUtf8,
11177            create_decimal256_array(array256, 7, 3).unwrap(),
11178        );
11179    }
11180
11181    #[test]
11182    fn test_cast_numeric_to_decimal128_precision_overflow() {
11183        let array = Int64Array::from(vec![1234567]);
11184        let array = Arc::new(array) as ArrayRef;
11185        let casted_array = cast_with_options(
11186            &array,
11187            &DataType::Decimal128(7, 3),
11188            &CastOptions {
11189                safe: true,
11190                format_options: FormatOptions::default(),
11191            },
11192        );
11193        assert!(casted_array.is_ok());
11194        assert!(casted_array.unwrap().is_null(0));
11195
11196        let err = cast_with_options(
11197            &array,
11198            &DataType::Decimal128(7, 3),
11199            &CastOptions {
11200                safe: false,
11201                format_options: FormatOptions::default(),
11202            },
11203        );
11204        assert_eq!(
11205            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11206            err.unwrap_err().to_string()
11207        );
11208    }
11209
11210    #[test]
11211    fn test_cast_numeric_to_decimal256_precision_overflow() {
11212        let array = Int64Array::from(vec![1234567]);
11213        let array = Arc::new(array) as ArrayRef;
11214        let casted_array = cast_with_options(
11215            &array,
11216            &DataType::Decimal256(7, 3),
11217            &CastOptions {
11218                safe: true,
11219                format_options: FormatOptions::default(),
11220            },
11221        );
11222        assert!(casted_array.is_ok());
11223        assert!(casted_array.unwrap().is_null(0));
11224
11225        let err = cast_with_options(
11226            &array,
11227            &DataType::Decimal256(7, 3),
11228            &CastOptions {
11229                safe: false,
11230                format_options: FormatOptions::default(),
11231            },
11232        );
11233        assert_eq!(
11234            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11235            err.unwrap_err().to_string()
11236        );
11237    }
11238
11239    /// helper function to test casting from duration to interval
11240    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11241        array: Vec<i64>,
11242        cast_options: &CastOptions,
11243    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11244        let array = PrimitiveArray::<T>::new(array.into(), None);
11245        let array = Arc::new(array) as ArrayRef;
11246        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11247        let out = cast_with_options(&array, &interval, cast_options)?;
11248        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11249        Ok(out)
11250    }
11251
11252    #[test]
11253    fn test_cast_from_duration_to_interval() {
11254        // from duration second to interval month day nano
11255        let array = vec![1234567];
11256        let casted_array =
11257            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11258                .unwrap();
11259        assert_eq!(
11260            casted_array.data_type(),
11261            &DataType::Interval(IntervalUnit::MonthDayNano)
11262        );
11263        assert_eq!(
11264            casted_array.value(0),
11265            IntervalMonthDayNano::new(0, 0, 1234567000000000)
11266        );
11267
11268        let array = vec![i64::MAX];
11269        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11270            array.clone(),
11271            &CastOptions::default(),
11272        )
11273        .unwrap();
11274        assert!(!casted_array.is_valid(0));
11275
11276        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11277            array,
11278            &CastOptions {
11279                safe: false,
11280                format_options: FormatOptions::default(),
11281            },
11282        );
11283        assert!(casted_array.is_err());
11284
11285        // from duration millisecond to interval month day nano
11286        let array = vec![1234567];
11287        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11288            array,
11289            &CastOptions::default(),
11290        )
11291        .unwrap();
11292        assert_eq!(
11293            casted_array.data_type(),
11294            &DataType::Interval(IntervalUnit::MonthDayNano)
11295        );
11296        assert_eq!(
11297            casted_array.value(0),
11298            IntervalMonthDayNano::new(0, 0, 1234567000000)
11299        );
11300
11301        let array = vec![i64::MAX];
11302        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11303            array.clone(),
11304            &CastOptions::default(),
11305        )
11306        .unwrap();
11307        assert!(!casted_array.is_valid(0));
11308
11309        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11310            array,
11311            &CastOptions {
11312                safe: false,
11313                format_options: FormatOptions::default(),
11314            },
11315        );
11316        assert!(casted_array.is_err());
11317
11318        // from duration microsecond to interval month day nano
11319        let array = vec![1234567];
11320        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11321            array,
11322            &CastOptions::default(),
11323        )
11324        .unwrap();
11325        assert_eq!(
11326            casted_array.data_type(),
11327            &DataType::Interval(IntervalUnit::MonthDayNano)
11328        );
11329        assert_eq!(
11330            casted_array.value(0),
11331            IntervalMonthDayNano::new(0, 0, 1234567000)
11332        );
11333
11334        let array = vec![i64::MAX];
11335        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11336            array.clone(),
11337            &CastOptions::default(),
11338        )
11339        .unwrap();
11340        assert!(!casted_array.is_valid(0));
11341
11342        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11343            array,
11344            &CastOptions {
11345                safe: false,
11346                format_options: FormatOptions::default(),
11347            },
11348        );
11349        assert!(casted_array.is_err());
11350
11351        // from duration nanosecond to interval month day nano
11352        let array = vec![1234567];
11353        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11354            array,
11355            &CastOptions::default(),
11356        )
11357        .unwrap();
11358        assert_eq!(
11359            casted_array.data_type(),
11360            &DataType::Interval(IntervalUnit::MonthDayNano)
11361        );
11362        assert_eq!(
11363            casted_array.value(0),
11364            IntervalMonthDayNano::new(0, 0, 1234567)
11365        );
11366
11367        let array = vec![i64::MAX];
11368        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11369            array,
11370            &CastOptions {
11371                safe: false,
11372                format_options: FormatOptions::default(),
11373            },
11374        )
11375        .unwrap();
11376        assert_eq!(
11377            casted_array.value(0),
11378            IntervalMonthDayNano::new(0, 0, i64::MAX)
11379        );
11380    }
11381
11382    /// helper function to test casting from interval to duration
11383    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
11384        array: &IntervalMonthDayNanoArray,
11385        cast_options: &CastOptions,
11386    ) -> Result<PrimitiveArray<T>, ArrowError> {
11387        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
11388        casted_array
11389            .as_any()
11390            .downcast_ref::<PrimitiveArray<T>>()
11391            .ok_or_else(|| {
11392                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
11393            })
11394            .cloned()
11395    }
11396
11397    #[test]
11398    fn test_cast_from_interval_to_duration() {
11399        let nullable = CastOptions::default();
11400        let fallible = CastOptions {
11401            safe: false,
11402            format_options: FormatOptions::default(),
11403        };
11404        let v = IntervalMonthDayNano::new(0, 0, 1234567);
11405
11406        // from interval month day nano to duration second
11407        let array = vec![v].into();
11408        let casted_array: DurationSecondArray =
11409            cast_from_interval_to_duration(&array, &nullable).unwrap();
11410        assert_eq!(casted_array.value(0), 0);
11411
11412        let array = vec![IntervalMonthDayNano::MAX].into();
11413        let casted_array: DurationSecondArray =
11414            cast_from_interval_to_duration(&array, &nullable).unwrap();
11415        assert!(!casted_array.is_valid(0));
11416
11417        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
11418        assert!(res.is_err());
11419
11420        // from interval month day nano to duration millisecond
11421        let array = vec![v].into();
11422        let casted_array: DurationMillisecondArray =
11423            cast_from_interval_to_duration(&array, &nullable).unwrap();
11424        assert_eq!(casted_array.value(0), 1);
11425
11426        let array = vec![IntervalMonthDayNano::MAX].into();
11427        let casted_array: DurationMillisecondArray =
11428            cast_from_interval_to_duration(&array, &nullable).unwrap();
11429        assert!(!casted_array.is_valid(0));
11430
11431        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
11432        assert!(res.is_err());
11433
11434        // from interval month day nano to duration microsecond
11435        let array = vec![v].into();
11436        let casted_array: DurationMicrosecondArray =
11437            cast_from_interval_to_duration(&array, &nullable).unwrap();
11438        assert_eq!(casted_array.value(0), 1234);
11439
11440        let array = vec![IntervalMonthDayNano::MAX].into();
11441        let casted_array =
11442            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
11443        assert!(!casted_array.is_valid(0));
11444
11445        let casted_array =
11446            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
11447        assert!(casted_array.is_err());
11448
11449        // from interval month day nano to duration nanosecond
11450        let array = vec![v].into();
11451        let casted_array: DurationNanosecondArray =
11452            cast_from_interval_to_duration(&array, &nullable).unwrap();
11453        assert_eq!(casted_array.value(0), 1234567);
11454
11455        let array = vec![IntervalMonthDayNano::MAX].into();
11456        let casted_array: DurationNanosecondArray =
11457            cast_from_interval_to_duration(&array, &nullable).unwrap();
11458        assert!(!casted_array.is_valid(0));
11459
11460        let casted_array =
11461            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
11462        assert!(casted_array.is_err());
11463
11464        let array = vec![
11465            IntervalMonthDayNanoType::make_value(0, 1, 0),
11466            IntervalMonthDayNanoType::make_value(-1, 0, 0),
11467            IntervalMonthDayNanoType::make_value(1, 1, 0),
11468            IntervalMonthDayNanoType::make_value(1, 0, 1),
11469            IntervalMonthDayNanoType::make_value(0, 0, -1),
11470        ]
11471        .into();
11472        let casted_array =
11473            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
11474        assert!(!casted_array.is_valid(0));
11475        assert!(!casted_array.is_valid(1));
11476        assert!(!casted_array.is_valid(2));
11477        assert!(!casted_array.is_valid(3));
11478        assert!(casted_array.is_valid(4));
11479        assert_eq!(casted_array.value(4), -1);
11480    }
11481
11482    /// helper function to test casting from interval year month to interval month day nano
11483    fn cast_from_interval_year_month_to_interval_month_day_nano(
11484        array: Vec<i32>,
11485        cast_options: &CastOptions,
11486    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11487        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
11488        let array = Arc::new(array) as ArrayRef;
11489        let casted_array = cast_with_options(
11490            &array,
11491            &DataType::Interval(IntervalUnit::MonthDayNano),
11492            cast_options,
11493        )?;
11494        casted_array
11495            .as_any()
11496            .downcast_ref::<IntervalMonthDayNanoArray>()
11497            .ok_or_else(|| {
11498                ArrowError::ComputeError(
11499                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
11500                )
11501            })
11502            .cloned()
11503    }
11504
11505    #[test]
11506    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
11507        // from interval year month to interval month day nano
11508        let array = vec![1234567];
11509        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
11510            array,
11511            &CastOptions::default(),
11512        )
11513        .unwrap();
11514        assert_eq!(
11515            casted_array.data_type(),
11516            &DataType::Interval(IntervalUnit::MonthDayNano)
11517        );
11518        assert_eq!(
11519            casted_array.value(0),
11520            IntervalMonthDayNano::new(1234567, 0, 0)
11521        );
11522    }
11523
11524    /// helper function to test casting from interval day time to interval month day nano
11525    fn cast_from_interval_day_time_to_interval_month_day_nano(
11526        array: Vec<IntervalDayTime>,
11527        cast_options: &CastOptions,
11528    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11529        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
11530        let array = Arc::new(array) as ArrayRef;
11531        let casted_array = cast_with_options(
11532            &array,
11533            &DataType::Interval(IntervalUnit::MonthDayNano),
11534            cast_options,
11535        )?;
11536        Ok(casted_array
11537            .as_primitive::<IntervalMonthDayNanoType>()
11538            .clone())
11539    }
11540
11541    #[test]
11542    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
11543        // from interval day time to interval month day nano
11544        let array = vec![IntervalDayTime::new(123, 0)];
11545        let casted_array =
11546            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
11547                .unwrap();
11548        assert_eq!(
11549            casted_array.data_type(),
11550            &DataType::Interval(IntervalUnit::MonthDayNano)
11551        );
11552        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
11553    }
11554
11555    #[test]
11556    fn test_cast_below_unixtimestamp() {
11557        let valid = StringArray::from(vec![
11558            "1900-01-03 23:59:59",
11559            "1969-12-31 00:00:01",
11560            "1989-12-31 00:00:01",
11561        ]);
11562
11563        let array = Arc::new(valid) as ArrayRef;
11564        let casted_array = cast_with_options(
11565            &array,
11566            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11567            &CastOptions {
11568                safe: false,
11569                format_options: FormatOptions::default(),
11570            },
11571        )
11572        .unwrap();
11573
11574        let ts_array = casted_array
11575            .as_primitive::<TimestampNanosecondType>()
11576            .values()
11577            .iter()
11578            .map(|ts| ts / 1_000_000)
11579            .collect::<Vec<_>>();
11580
11581        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
11582        let casted_array = cast(&array, &DataType::Date32).unwrap();
11583        let date_array = casted_array.as_primitive::<Date32Type>();
11584        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
11585        let string_array = casted_array.as_string::<i32>();
11586        assert_eq!("1900-01-03", string_array.value(0));
11587        assert_eq!("1969-12-31", string_array.value(1));
11588        assert_eq!("1989-12-31", string_array.value(2));
11589    }
11590
11591    #[test]
11592    fn test_nested_list() {
11593        let mut list = ListBuilder::new(Int32Builder::new());
11594        list.append_value([Some(1), Some(2), Some(3)]);
11595        list.append_value([Some(4), None, Some(6)]);
11596        let list = list.finish();
11597
11598        let to_field = Field::new("nested", list.data_type().clone(), false);
11599        let to = DataType::List(Arc::new(to_field));
11600        let out = cast(&list, &to).unwrap();
11601        let opts = FormatOptions::default().with_null("null");
11602        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
11603
11604        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
11605        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
11606    }
11607
11608    #[test]
11609    fn test_nested_list_cast() {
11610        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
11611        builder.append_value([Some([Some(1), Some(2), None]), None]);
11612        builder.append_value([None, Some([]), None]);
11613        builder.append_null();
11614        builder.append_value([Some([Some(2), Some(3)])]);
11615        let start = builder.finish();
11616
11617        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
11618        builder.append_value([Some([Some(1), Some(2), None]), None]);
11619        builder.append_value([None, Some([]), None]);
11620        builder.append_null();
11621        builder.append_value([Some([Some(2), Some(3)])]);
11622        let expected = builder.finish();
11623
11624        let actual = cast(&start, expected.data_type()).unwrap();
11625        assert_eq!(actual.as_ref(), &expected);
11626    }
11627
11628    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
11629        safe: true,
11630        format_options: FormatOptions::new(),
11631    };
11632
11633    #[test]
11634    #[allow(clippy::assertions_on_constants)]
11635    fn test_const_options() {
11636        assert!(CAST_OPTIONS.safe)
11637    }
11638
11639    #[test]
11640    fn test_list_format_options() {
11641        let options = CastOptions {
11642            safe: false,
11643            format_options: FormatOptions::default().with_null("null"),
11644        };
11645        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
11646            Some(vec![Some(0), Some(1), Some(2)]),
11647            Some(vec![Some(0), None, Some(2)]),
11648        ]);
11649        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
11650        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
11651        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
11652    }
11653    #[test]
11654    fn test_cast_string_to_timestamp_invalid_tz() {
11655        // content after Z should be ignored
11656        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
11657        let array = StringArray::from(vec![Some(bad_timestamp)]);
11658
11659        let data_types = [
11660            DataType::Timestamp(TimeUnit::Second, None),
11661            DataType::Timestamp(TimeUnit::Millisecond, None),
11662            DataType::Timestamp(TimeUnit::Microsecond, None),
11663            DataType::Timestamp(TimeUnit::Nanosecond, None),
11664        ];
11665
11666        let cast_options = CastOptions {
11667            safe: false,
11668            ..Default::default()
11669        };
11670
11671        for dt in data_types {
11672            assert_eq!(
11673                cast_with_options(&array, &dt, &cast_options)
11674                    .unwrap_err()
11675                    .to_string(),
11676                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
11677            );
11678        }
11679    }
11680    #[test]
11681    fn test_cast_struct_to_struct() {
11682        let struct_type = DataType::Struct(
11683            vec![
11684                Field::new("a", DataType::Boolean, false),
11685                Field::new("b", DataType::Int32, false),
11686            ]
11687            .into(),
11688        );
11689        let to_type = DataType::Struct(
11690            vec![
11691                Field::new("a", DataType::Utf8, false),
11692                Field::new("b", DataType::Utf8, false),
11693            ]
11694            .into(),
11695        );
11696        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11697        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11698        let struct_array = StructArray::from(vec![
11699            (
11700                Arc::new(Field::new("b", DataType::Boolean, false)),
11701                boolean.clone() as ArrayRef,
11702            ),
11703            (
11704                Arc::new(Field::new("c", DataType::Int32, false)),
11705                int.clone() as ArrayRef,
11706            ),
11707        ]);
11708        let casted_array = cast(&struct_array, &to_type).unwrap();
11709        let casted_array = casted_array.as_struct();
11710        assert_eq!(casted_array.data_type(), &to_type);
11711        let casted_boolean_array = casted_array
11712            .column(0)
11713            .as_string::<i32>()
11714            .into_iter()
11715            .flatten()
11716            .collect::<Vec<_>>();
11717        let casted_int_array = casted_array
11718            .column(1)
11719            .as_string::<i32>()
11720            .into_iter()
11721            .flatten()
11722            .collect::<Vec<_>>();
11723        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
11724        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
11725
11726        // test for can't cast
11727        let to_type = DataType::Struct(
11728            vec![
11729                Field::new("a", DataType::Date32, false),
11730                Field::new("b", DataType::Utf8, false),
11731            ]
11732            .into(),
11733        );
11734        assert!(!can_cast_types(&struct_type, &to_type));
11735        let result = cast(&struct_array, &to_type);
11736        assert_eq!(
11737            "Cast error: Casting from Boolean to Date32 not supported",
11738            result.unwrap_err().to_string()
11739        );
11740    }
11741
11742    #[test]
11743    fn test_cast_struct_to_struct_nullability() {
11744        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11745        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
11746        let struct_array = StructArray::from(vec![
11747            (
11748                Arc::new(Field::new("b", DataType::Boolean, false)),
11749                boolean.clone() as ArrayRef,
11750            ),
11751            (
11752                Arc::new(Field::new("c", DataType::Int32, true)),
11753                int.clone() as ArrayRef,
11754            ),
11755        ]);
11756
11757        // okay: nullable to nullable
11758        let to_type = DataType::Struct(
11759            vec![
11760                Field::new("a", DataType::Utf8, false),
11761                Field::new("b", DataType::Utf8, true),
11762            ]
11763            .into(),
11764        );
11765        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
11766
11767        // error: nullable to non-nullable
11768        let to_type = DataType::Struct(
11769            vec![
11770                Field::new("a", DataType::Utf8, false),
11771                Field::new("b", DataType::Utf8, false),
11772            ]
11773            .into(),
11774        );
11775        cast(&struct_array, &to_type)
11776            .expect_err("Cast nullable to non-nullable struct field should fail");
11777
11778        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11779        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
11780        let struct_array = StructArray::from(vec![
11781            (
11782                Arc::new(Field::new("b", DataType::Boolean, false)),
11783                boolean.clone() as ArrayRef,
11784            ),
11785            (
11786                Arc::new(Field::new("c", DataType::Int32, false)),
11787                int.clone() as ArrayRef,
11788            ),
11789        ]);
11790
11791        // okay: non-nullable to non-nullable
11792        let to_type = DataType::Struct(
11793            vec![
11794                Field::new("a", DataType::Utf8, false),
11795                Field::new("b", DataType::Utf8, false),
11796            ]
11797            .into(),
11798        );
11799        cast(&struct_array, &to_type)
11800            .expect("Cast non-nullable to non-nullable struct field should work");
11801
11802        // err: non-nullable to non-nullable but overflowing return null during casting
11803        let to_type = DataType::Struct(
11804            vec![
11805                Field::new("a", DataType::Utf8, false),
11806                Field::new("b", DataType::Int8, false),
11807            ]
11808            .into(),
11809        );
11810        cast(&struct_array, &to_type).expect_err(
11811            "Cast non-nullable to non-nullable struct field returning null should fail",
11812        );
11813    }
11814
11815    #[test]
11816    fn test_cast_struct_to_non_struct() {
11817        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
11818        let struct_array = StructArray::from(vec![(
11819            Arc::new(Field::new("a", DataType::Boolean, false)),
11820            boolean.clone() as ArrayRef,
11821        )]);
11822        let to_type = DataType::Utf8;
11823        let result = cast(&struct_array, &to_type);
11824        assert_eq!(
11825            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
11826            result.unwrap_err().to_string()
11827        );
11828    }
11829
11830    #[test]
11831    fn test_cast_non_struct_to_struct() {
11832        let array = StringArray::from(vec!["a", "b"]);
11833        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
11834        let result = cast(&array, &to_type);
11835        assert_eq!(
11836            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
11837            result.unwrap_err().to_string()
11838        );
11839    }
11840
11841    #[test]
11842    fn test_cast_struct_with_different_field_order() {
11843        // Test slow path: fields are in different order
11844        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11845        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11846        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11847
11848        let struct_array = StructArray::from(vec![
11849            (
11850                Arc::new(Field::new("a", DataType::Boolean, false)),
11851                boolean.clone() as ArrayRef,
11852            ),
11853            (
11854                Arc::new(Field::new("b", DataType::Int32, false)),
11855                int.clone() as ArrayRef,
11856            ),
11857            (
11858                Arc::new(Field::new("c", DataType::Utf8, false)),
11859                string.clone() as ArrayRef,
11860            ),
11861        ]);
11862
11863        // Target has fields in different order: c, a, b instead of a, b, c
11864        let to_type = DataType::Struct(
11865            vec![
11866                Field::new("c", DataType::Utf8, false),
11867                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
11868                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
11869            ]
11870            .into(),
11871        );
11872
11873        let result = cast(&struct_array, &to_type).unwrap();
11874        let result_struct = result.as_struct();
11875
11876        assert_eq!(result_struct.data_type(), &to_type);
11877        assert_eq!(result_struct.num_columns(), 3);
11878
11879        // Verify field "c" (originally position 2, now position 0) remains Utf8
11880        let c_column = result_struct.column(0).as_string::<i32>();
11881        assert_eq!(
11882            c_column.into_iter().flatten().collect::<Vec<_>>(),
11883            vec!["foo", "bar", "baz", "qux"]
11884        );
11885
11886        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
11887        let a_column = result_struct.column(1).as_string::<i32>();
11888        assert_eq!(
11889            a_column.into_iter().flatten().collect::<Vec<_>>(),
11890            vec!["false", "false", "true", "true"]
11891        );
11892
11893        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
11894        let b_column = result_struct.column(2).as_string::<i32>();
11895        assert_eq!(
11896            b_column.into_iter().flatten().collect::<Vec<_>>(),
11897            vec!["42", "28", "19", "31"]
11898        );
11899    }
11900
11901    #[test]
11902    fn test_cast_struct_with_missing_field() {
11903        // Test that casting fails when target has a field not present in source
11904        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
11905        let struct_array = StructArray::from(vec![(
11906            Arc::new(Field::new("a", DataType::Boolean, false)),
11907            boolean.clone() as ArrayRef,
11908        )]);
11909
11910        let to_type = DataType::Struct(
11911            vec![
11912                Field::new("a", DataType::Utf8, false),
11913                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
11914            ]
11915            .into(),
11916        );
11917
11918        let result = cast(&struct_array, &to_type);
11919        assert!(result.is_err());
11920        assert_eq!(
11921            result.unwrap_err().to_string(),
11922            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
11923        );
11924    }
11925
11926    #[test]
11927    fn test_cast_struct_with_subset_of_fields() {
11928        // Test casting to a struct with fewer fields (selecting a subset)
11929        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11930        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11931        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11932
11933        let struct_array = StructArray::from(vec![
11934            (
11935                Arc::new(Field::new("a", DataType::Boolean, false)),
11936                boolean.clone() as ArrayRef,
11937            ),
11938            (
11939                Arc::new(Field::new("b", DataType::Int32, false)),
11940                int.clone() as ArrayRef,
11941            ),
11942            (
11943                Arc::new(Field::new("c", DataType::Utf8, false)),
11944                string.clone() as ArrayRef,
11945            ),
11946        ]);
11947
11948        // Target has only fields "c" and "a", omitting "b"
11949        let to_type = DataType::Struct(
11950            vec![
11951                Field::new("c", DataType::Utf8, false),
11952                Field::new("a", DataType::Utf8, false),
11953            ]
11954            .into(),
11955        );
11956
11957        let result = cast(&struct_array, &to_type).unwrap();
11958        let result_struct = result.as_struct();
11959
11960        assert_eq!(result_struct.data_type(), &to_type);
11961        assert_eq!(result_struct.num_columns(), 2);
11962
11963        // Verify field "c" remains Utf8
11964        let c_column = result_struct.column(0).as_string::<i32>();
11965        assert_eq!(
11966            c_column.into_iter().flatten().collect::<Vec<_>>(),
11967            vec!["foo", "bar", "baz", "qux"]
11968        );
11969
11970        // Verify field "a" was cast from Boolean to Utf8
11971        let a_column = result_struct.column(1).as_string::<i32>();
11972        assert_eq!(
11973            a_column.into_iter().flatten().collect::<Vec<_>>(),
11974            vec!["false", "false", "true", "true"]
11975        );
11976    }
11977
11978    #[test]
11979    fn test_can_cast_struct_rename_field() {
11980        // Test that can_cast_types returns false when target has a field not in source
11981        let from_type = DataType::Struct(
11982            vec![
11983                Field::new("a", DataType::Int32, false),
11984                Field::new("b", DataType::Utf8, false),
11985            ]
11986            .into(),
11987        );
11988
11989        let to_type = DataType::Struct(
11990            vec![
11991                Field::new("a", DataType::Int64, false),
11992                Field::new("c", DataType::Boolean, false), // Field "c" not in source
11993            ]
11994            .into(),
11995        );
11996
11997        assert!(can_cast_types(&from_type, &to_type));
11998    }
11999
12000    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12001        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12002        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12003        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12004        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12005    }
12006
12007    #[test]
12008    fn test_decimal_to_decimal_coverage() {
12009        let test_cases = [
12010            // increase precision, increase scale, infallible
12011            DecimalCastTestConfig {
12012                input_prec: 5,
12013                input_scale: 1,
12014                input_repr: 99999, // 9999.9
12015                output_prec: 10,
12016                output_scale: 6,
12017                expected_output_repr: Ok(9999900000), // 9999.900000
12018            },
12019            // increase precision, increase scale, fallible, safe
12020            DecimalCastTestConfig {
12021                input_prec: 5,
12022                input_scale: 1,
12023                input_repr: 99, // 9999.9
12024                output_prec: 7,
12025                output_scale: 6,
12026                expected_output_repr: Ok(9900000), // 9.900000
12027            },
12028            // increase precision, increase scale, fallible, unsafe
12029            DecimalCastTestConfig {
12030                input_prec: 5,
12031                input_scale: 1,
12032                input_repr: 99999, // 9999.9
12033                output_prec: 7,
12034                output_scale: 6,
12035                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
12036            },
12037            // increase precision, decrease scale, always infallible
12038            DecimalCastTestConfig {
12039                input_prec: 5,
12040                input_scale: 3,
12041                input_repr: 99999, // 99.999
12042                output_prec: 10,
12043                output_scale: 2,
12044                expected_output_repr: Ok(10000), // 100.00
12045            },
12046            // increase precision, decrease scale, no rouding
12047            DecimalCastTestConfig {
12048                input_prec: 5,
12049                input_scale: 3,
12050                input_repr: 99994, // 99.994
12051                output_prec: 10,
12052                output_scale: 2,
12053                expected_output_repr: Ok(9999), // 99.99
12054            },
12055            // increase precision, don't change scale, always infallible
12056            DecimalCastTestConfig {
12057                input_prec: 5,
12058                input_scale: 3,
12059                input_repr: 99999, // 99.999
12060                output_prec: 10,
12061                output_scale: 3,
12062                expected_output_repr: Ok(99999), // 99.999
12063            },
12064            // decrease precision, increase scale, safe
12065            DecimalCastTestConfig {
12066                input_prec: 10,
12067                input_scale: 5,
12068                input_repr: 999999, // 9.99999
12069                output_prec: 8,
12070                output_scale: 7,
12071                expected_output_repr: Ok(99999900), // 9.9999900
12072            },
12073            // decrease precision, increase scale, unsafe
12074            DecimalCastTestConfig {
12075                input_prec: 10,
12076                input_scale: 5,
12077                input_repr: 9999999, // 99.99999
12078                output_prec: 8,
12079                output_scale: 7,
12080                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
12081            },
12082            // decrease precision, decrease scale, safe, infallible
12083            DecimalCastTestConfig {
12084                input_prec: 7,
12085                input_scale: 4,
12086                input_repr: 9999999, // 999.9999
12087                output_prec: 6,
12088                output_scale: 2,
12089                expected_output_repr: Ok(100000),
12090            },
12091            // decrease precision, decrease scale, safe, fallible
12092            DecimalCastTestConfig {
12093                input_prec: 10,
12094                input_scale: 5,
12095                input_repr: 12345678, // 123.45678
12096                output_prec: 8,
12097                output_scale: 3,
12098                expected_output_repr: Ok(123457), // 123.457
12099            },
12100            // decrease precision, decrease scale, unsafe
12101            DecimalCastTestConfig {
12102                input_prec: 10,
12103                input_scale: 5,
12104                input_repr: 9999999, // 99.99999
12105                output_prec: 4,
12106                output_scale: 3,
12107                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
12108            },
12109            // decrease precision, same scale, safe
12110            DecimalCastTestConfig {
12111                input_prec: 10,
12112                input_scale: 5,
12113                input_repr: 999999, // 9.99999
12114                output_prec: 6,
12115                output_scale: 5,
12116                expected_output_repr: Ok(999999), // 9.99999
12117            },
12118            // decrease precision, same scale, unsafe
12119            DecimalCastTestConfig {
12120                input_prec: 10,
12121                input_scale: 5,
12122                input_repr: 9999999, // 99.99999
12123                output_prec: 6,
12124                output_scale: 5,
12125                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
12126            },
12127            // same precision, increase scale, safe
12128            DecimalCastTestConfig {
12129                input_prec: 7,
12130                input_scale: 4,
12131                input_repr: 12345, // 1.2345
12132                output_prec: 7,
12133                output_scale: 6,
12134                expected_output_repr: Ok(1234500), // 1.234500
12135            },
12136            // same precision, increase scale, unsafe
12137            DecimalCastTestConfig {
12138                input_prec: 7,
12139                input_scale: 4,
12140                input_repr: 123456, // 12.3456
12141                output_prec: 7,
12142                output_scale: 6,
12143                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
12144            },
12145            // same precision, decrease scale, infallible
12146            DecimalCastTestConfig {
12147                input_prec: 7,
12148                input_scale: 5,
12149                input_repr: 1234567, // 12.34567
12150                output_prec: 7,
12151                output_scale: 4,
12152                expected_output_repr: Ok(123457), // 12.3457
12153            },
12154            // same precision, same scale, infallible
12155            DecimalCastTestConfig {
12156                input_prec: 7,
12157                input_scale: 5,
12158                input_repr: 9999999, // 99.99999
12159                output_prec: 7,
12160                output_scale: 5,
12161                expected_output_repr: Ok(9999999), // 99.99999
12162            },
12163            // precision increase, input scale & output scale = 0, infallible
12164            DecimalCastTestConfig {
12165                input_prec: 7,
12166                input_scale: 0,
12167                input_repr: 1234567, // 1234567
12168                output_prec: 8,
12169                output_scale: 0,
12170                expected_output_repr: Ok(1234567), // 1234567
12171            },
12172            // precision decrease, input scale & output scale = 0, failure
12173            DecimalCastTestConfig {
12174                input_prec: 7,
12175                input_scale: 0,
12176                input_repr: 1234567, // 1234567
12177                output_prec: 6,
12178                output_scale: 0,
12179                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12180            },
12181            // precision decrease, input scale & output scale = 0, success
12182            DecimalCastTestConfig {
12183                input_prec: 7,
12184                input_scale: 0,
12185                input_repr: 123456, // 123456
12186                output_prec: 6,
12187                output_scale: 0,
12188                expected_output_repr: Ok(123456), // 123456
12189            },
12190        ];
12191
12192        for t in test_cases {
12193            run_decimal_cast_test_case_between_multiple_types(t);
12194        }
12195    }
12196
12197    #[test]
12198    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12199        let test_cases = [
12200            DecimalCastTestConfig {
12201                input_prec: 5,
12202                input_scale: 0,
12203                input_repr: 99999,
12204                output_prec: 10,
12205                output_scale: 5,
12206                expected_output_repr: Ok(9999900000),
12207            },
12208            DecimalCastTestConfig {
12209                input_prec: 5,
12210                input_scale: 0,
12211                input_repr: -99999,
12212                output_prec: 10,
12213                output_scale: 5,
12214                expected_output_repr: Ok(-9999900000),
12215            },
12216            DecimalCastTestConfig {
12217                input_prec: 5,
12218                input_scale: 2,
12219                input_repr: 99999,
12220                output_prec: 10,
12221                output_scale: 5,
12222                expected_output_repr: Ok(99999000),
12223            },
12224            DecimalCastTestConfig {
12225                input_prec: 5,
12226                input_scale: -2,
12227                input_repr: -99999,
12228                output_prec: 10,
12229                output_scale: 3,
12230                expected_output_repr: Ok(-9999900000),
12231            },
12232            DecimalCastTestConfig {
12233                input_prec: 5,
12234                input_scale: 3,
12235                input_repr: -12345,
12236                output_prec: 6,
12237                output_scale: 5,
12238                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())
12239            },
12240        ];
12241
12242        for t in test_cases {
12243            run_decimal_cast_test_case_between_multiple_types(t);
12244        }
12245    }
12246
12247    #[test]
12248    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12249        let test_cases = [
12250            DecimalCastTestConfig {
12251                input_prec: 5,
12252                input_scale: 0,
12253                input_repr: 99999,
12254                output_scale: -3,
12255                output_prec: 3,
12256                expected_output_repr: Ok(100),
12257            },
12258            DecimalCastTestConfig {
12259                input_prec: 5,
12260                input_scale: 0,
12261                input_repr: -99999,
12262                output_prec: 1,
12263                output_scale: -5,
12264                expected_output_repr: Ok(-1),
12265            },
12266            DecimalCastTestConfig {
12267                input_prec: 10,
12268                input_scale: 2,
12269                input_repr: 123456789,
12270                output_prec: 5,
12271                output_scale: -2,
12272                expected_output_repr: Ok(12346),
12273            },
12274            DecimalCastTestConfig {
12275                input_prec: 10,
12276                input_scale: 4,
12277                input_repr: -9876543210,
12278                output_prec: 7,
12279                output_scale: 0,
12280                expected_output_repr: Ok(-987654),
12281            },
12282            DecimalCastTestConfig {
12283                input_prec: 7,
12284                input_scale: 4,
12285                input_repr: 9999999,
12286                output_prec: 6,
12287                output_scale: 3,
12288                expected_output_repr:
12289                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12290            },
12291        ];
12292        for t in test_cases {
12293            run_decimal_cast_test_case_between_multiple_types(t);
12294        }
12295    }
12296
12297    #[test]
12298    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12299        let array = vec![Some(123456789)];
12300        let array = create_decimal128_array(array, 24, 2).unwrap();
12301        let input_type = DataType::Decimal128(24, 2);
12302        let output_type = DataType::Decimal128(6, 2);
12303        assert!(can_cast_types(&input_type, &output_type));
12304
12305        let options = CastOptions {
12306            safe: false,
12307            ..Default::default()
12308        };
12309        let result = cast_with_options(&array, &output_type, &options);
12310        assert_eq!(
12311            result.unwrap_err().to_string(),
12312            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12313        );
12314    }
12315
12316    #[test]
12317    fn test_decimal_to_decimal_same_scale() {
12318        let array = vec![Some(520)];
12319        let array = create_decimal128_array(array, 4, 2).unwrap();
12320        let input_type = DataType::Decimal128(4, 2);
12321        let output_type = DataType::Decimal128(3, 2);
12322        assert!(can_cast_types(&input_type, &output_type));
12323
12324        let options = CastOptions {
12325            safe: false,
12326            ..Default::default()
12327        };
12328        let result = cast_with_options(&array, &output_type, &options);
12329        assert_eq!(
12330            result.unwrap().as_primitive::<Decimal128Type>().value(0),
12331            520
12332        );
12333
12334        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
12335        assert_eq!(
12336            &cast(
12337                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12338                &DataType::Decimal128(2, 0)
12339            )
12340            .unwrap(),
12341            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
12342        );
12343    }
12344
12345    #[test]
12346    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
12347        let array = vec![Some(123456789)];
12348        let array = create_decimal128_array(array, 24, 4).unwrap();
12349        let input_type = DataType::Decimal128(24, 4);
12350        let output_type = DataType::Decimal128(6, 2);
12351        assert!(can_cast_types(&input_type, &output_type));
12352
12353        let options = CastOptions {
12354            safe: false,
12355            ..Default::default()
12356        };
12357        let result = cast_with_options(&array, &output_type, &options);
12358        assert_eq!(
12359            result.unwrap_err().to_string(),
12360            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12361        );
12362    }
12363
12364    #[test]
12365    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
12366        let array = vec![Some(123456789)];
12367        let array = create_decimal128_array(array, 24, 2).unwrap();
12368        let input_type = DataType::Decimal128(24, 2);
12369        let output_type = DataType::Decimal128(6, 3);
12370        assert!(can_cast_types(&input_type, &output_type));
12371
12372        let options = CastOptions {
12373            safe: false,
12374            ..Default::default()
12375        };
12376        let result = cast_with_options(&array, &output_type, &options);
12377        assert_eq!(
12378            result.unwrap_err().to_string(),
12379            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
12380        );
12381    }
12382
12383    #[test]
12384    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
12385        let array = vec![Some(123456789)];
12386        let array = create_decimal128_array(array, 24, 2).unwrap();
12387        let input_type = DataType::Decimal128(24, 2);
12388        let output_type = DataType::Decimal256(6, 2);
12389        assert!(can_cast_types(&input_type, &output_type));
12390
12391        let options = CastOptions {
12392            safe: false,
12393            ..Default::default()
12394        };
12395        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
12396        assert_eq!(
12397            result.to_string(),
12398            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
12399        );
12400    }
12401
12402    #[test]
12403    fn test_first_none() {
12404        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12405            None,
12406            Some(vec![Some(1), Some(2)]),
12407        ])) as ArrayRef;
12408        let data_type =
12409            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12410        let opt = CastOptions::default();
12411        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12412
12413        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12414            vec![None, Some(vec![Some(1), Some(2)])],
12415            2,
12416        )) as ArrayRef;
12417        assert_eq!(*fixed_array, *r);
12418    }
12419
12420    #[test]
12421    fn test_first_last_none() {
12422        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12423            None,
12424            Some(vec![Some(1), Some(2)]),
12425            None,
12426        ])) as ArrayRef;
12427        let data_type =
12428            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12429        let opt = CastOptions::default();
12430        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12431
12432        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12433            vec![None, Some(vec![Some(1), Some(2)]), None],
12434            2,
12435        )) as ArrayRef;
12436        assert_eq!(*fixed_array, *r);
12437    }
12438
12439    #[test]
12440    fn test_cast_decimal_error_output() {
12441        let array = Int64Array::from(vec![1]);
12442        let error = cast_with_options(
12443            &array,
12444            &DataType::Decimal32(1, 1),
12445            &CastOptions {
12446                safe: false,
12447                format_options: FormatOptions::default(),
12448            },
12449        )
12450        .unwrap_err();
12451        assert_eq!(
12452            error.to_string(),
12453            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
12454        );
12455
12456        let array = Int64Array::from(vec![-1]);
12457        let error = cast_with_options(
12458            &array,
12459            &DataType::Decimal32(1, 1),
12460            &CastOptions {
12461                safe: false,
12462                format_options: FormatOptions::default(),
12463            },
12464        )
12465        .unwrap_err();
12466        assert_eq!(
12467            error.to_string(),
12468            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
12469        );
12470    }
12471
12472    #[test]
12473    fn test_run_end_encoded_to_primitive() {
12474        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
12475        let run_ends = Int32Array::from(vec![2, 5, 6]);
12476        let values = Int32Array::from(vec![1, 2, 3]);
12477        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12478        let array_ref = Arc::new(run_array) as ArrayRef;
12479        // Cast to Int64
12480        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12481        // Verify the result is a RunArray with Int64 values
12482        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
12483        assert_eq!(
12484            result_run_array.values(),
12485            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
12486        );
12487    }
12488
12489    #[test]
12490    fn test_sliced_run_end_encoded_to_primitive() {
12491        let run_ends = Int32Array::from(vec![2, 5, 6]);
12492        let values = Int32Array::from(vec![1, 2, 3]);
12493        // [1, 1, 2, 2, 2, 3]
12494        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12495        let run_array = run_array.slice(3, 3); // [2, 2, 3]
12496        let array_ref = Arc::new(run_array) as ArrayRef;
12497
12498        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12499        let result_run_array = cast_result.as_primitive::<Int64Type>();
12500        assert_eq!(result_run_array.values(), &[2, 2, 3]);
12501    }
12502
12503    #[test]
12504    fn test_run_end_encoded_to_string() {
12505        let run_ends = Int32Array::from(vec![2, 3, 5]);
12506        let values = Int32Array::from(vec![10, 20, 30]);
12507        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12508        let array_ref = Arc::new(run_array) as ArrayRef;
12509
12510        // Cast to String
12511        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12512
12513        // Verify the result is a RunArray with String values
12514        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12515        // Check that values are correct
12516        assert_eq!(result_array.value(0), "10");
12517        assert_eq!(result_array.value(1), "10");
12518        assert_eq!(result_array.value(2), "20");
12519    }
12520
12521    #[test]
12522    fn test_primitive_to_run_end_encoded() {
12523        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
12524        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
12525        let array_ref = Arc::new(source_array) as ArrayRef;
12526
12527        // Cast to RunEndEncoded<Int32, Int32>
12528        let target_type = DataType::RunEndEncoded(
12529            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12530            Arc::new(Field::new("values", DataType::Int32, true)),
12531        );
12532        let cast_result = cast(&array_ref, &target_type).unwrap();
12533
12534        // Verify the result is a RunArray
12535        let result_run_array = cast_result
12536            .as_any()
12537            .downcast_ref::<RunArray<Int32Type>>()
12538            .unwrap();
12539
12540        // Check run structure: runs should end at positions [2, 5, 6]
12541        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
12542
12543        // Check values: should be [1, 2, 3]
12544        let values_array = result_run_array.values().as_primitive::<Int32Type>();
12545        assert_eq!(values_array.values(), &[1, 2, 3]);
12546    }
12547
12548    #[test]
12549    fn test_primitive_to_run_end_encoded_with_nulls() {
12550        let source_array = Int32Array::from(vec![
12551            Some(1),
12552            Some(1),
12553            None,
12554            None,
12555            Some(2),
12556            Some(2),
12557            Some(3),
12558            Some(3),
12559            None,
12560            None,
12561            Some(4),
12562            Some(4),
12563            Some(5),
12564            Some(5),
12565            None,
12566            None,
12567        ]);
12568        let array_ref = Arc::new(source_array) as ArrayRef;
12569        let target_type = DataType::RunEndEncoded(
12570            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12571            Arc::new(Field::new("values", DataType::Int32, true)),
12572        );
12573        let cast_result = cast(&array_ref, &target_type).unwrap();
12574        let result_run_array = cast_result
12575            .as_any()
12576            .downcast_ref::<RunArray<Int32Type>>()
12577            .unwrap();
12578        assert_eq!(
12579            result_run_array.run_ends().values(),
12580            &[2, 4, 6, 8, 10, 12, 14, 16]
12581        );
12582        assert_eq!(
12583            result_run_array
12584                .values()
12585                .as_primitive::<Int32Type>()
12586                .values(),
12587            &[1, 0, 2, 3, 0, 4, 5, 0]
12588        );
12589        assert_eq!(result_run_array.values().null_count(), 3);
12590    }
12591
12592    #[test]
12593    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
12594        let source_array = Int64Array::from(vec![
12595            Some(1),
12596            Some(1),
12597            None,
12598            None,
12599            None,
12600            None,
12601            None,
12602            None,
12603            None,
12604            None,
12605            Some(4),
12606            Some(20),
12607            Some(500),
12608            Some(500),
12609            None,
12610            None,
12611        ]);
12612        let array_ref = Arc::new(source_array) as ArrayRef;
12613        let target_type = DataType::RunEndEncoded(
12614            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12615            Arc::new(Field::new("values", DataType::Int64, true)),
12616        );
12617        let cast_result = cast(&array_ref, &target_type).unwrap();
12618        let result_run_array = cast_result
12619            .as_any()
12620            .downcast_ref::<RunArray<Int16Type>>()
12621            .unwrap();
12622        assert_eq!(
12623            result_run_array.run_ends().values(),
12624            &[2, 10, 11, 12, 14, 16]
12625        );
12626        assert_eq!(
12627            result_run_array
12628                .values()
12629                .as_primitive::<Int64Type>()
12630                .values(),
12631            &[1, 0, 4, 20, 500, 0]
12632        );
12633        assert_eq!(result_run_array.values().null_count(), 2);
12634    }
12635
12636    #[test]
12637    fn test_string_to_run_end_encoded() {
12638        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
12639        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
12640        let array_ref = Arc::new(source_array) as ArrayRef;
12641
12642        // Cast to RunEndEncoded<Int32, String>
12643        let target_type = DataType::RunEndEncoded(
12644            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12645            Arc::new(Field::new("values", DataType::Utf8, true)),
12646        );
12647        let cast_result = cast(&array_ref, &target_type).unwrap();
12648
12649        // Verify the result is a RunArray
12650        let result_run_array = cast_result
12651            .as_any()
12652            .downcast_ref::<RunArray<Int32Type>>()
12653            .unwrap();
12654
12655        // Check run structure: runs should end at positions [2, 3, 5]
12656        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
12657
12658        // Check values: should be ["a", "b", "c"]
12659        let values_array = result_run_array.values().as_string::<i32>();
12660        assert_eq!(values_array.value(0), "a");
12661        assert_eq!(values_array.value(1), "b");
12662        assert_eq!(values_array.value(2), "c");
12663    }
12664
12665    #[test]
12666    fn test_empty_array_to_run_end_encoded() {
12667        // Create an empty Int32 array
12668        let source_array = Int32Array::from(Vec::<i32>::new());
12669        let array_ref = Arc::new(source_array) as ArrayRef;
12670
12671        // Cast to RunEndEncoded<Int32, Int32>
12672        let target_type = DataType::RunEndEncoded(
12673            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12674            Arc::new(Field::new("values", DataType::Int32, true)),
12675        );
12676        let cast_result = cast(&array_ref, &target_type).unwrap();
12677
12678        // Verify the result is an empty RunArray
12679        let result_run_array = cast_result
12680            .as_any()
12681            .downcast_ref::<RunArray<Int32Type>>()
12682            .unwrap();
12683
12684        // Check that both run_ends and values are empty
12685        assert_eq!(result_run_array.run_ends().len(), 0);
12686        assert_eq!(result_run_array.values().len(), 0);
12687    }
12688
12689    #[test]
12690    fn test_run_end_encoded_with_nulls() {
12691        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
12692        let run_ends = Int32Array::from(vec![2, 3, 5]);
12693        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
12694        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12695        let array_ref = Arc::new(run_array) as ArrayRef;
12696
12697        // Cast to String
12698        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12699
12700        // Verify the result preserves nulls
12701        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12702        assert_eq!(result_run_array.value(0), "1");
12703        assert!(result_run_array.is_null(2));
12704        assert_eq!(result_run_array.value(4), "2");
12705    }
12706
12707    #[test]
12708    fn test_different_index_types() {
12709        // Test with Int16 index type
12710        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
12711        let array_ref = Arc::new(source_array) as ArrayRef;
12712
12713        let target_type = DataType::RunEndEncoded(
12714            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12715            Arc::new(Field::new("values", DataType::Int32, true)),
12716        );
12717        let cast_result = cast(&array_ref, &target_type).unwrap();
12718        assert_eq!(cast_result.data_type(), &target_type);
12719
12720        // Verify the cast worked correctly: values are [1, 2, 3]
12721        // and run-ends are [2, 3, 5]
12722        let run_array = cast_result
12723            .as_any()
12724            .downcast_ref::<RunArray<Int16Type>>()
12725            .unwrap();
12726        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12727        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12728        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12729        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
12730
12731        // Test again with Int64 index type
12732        let target_type = DataType::RunEndEncoded(
12733            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12734            Arc::new(Field::new("values", DataType::Int32, true)),
12735        );
12736        let cast_result = cast(&array_ref, &target_type).unwrap();
12737        assert_eq!(cast_result.data_type(), &target_type);
12738
12739        // Verify the cast worked correctly: values are [1, 2, 3]
12740        // and run-ends are [2, 3, 5]
12741        let run_array = cast_result
12742            .as_any()
12743            .downcast_ref::<RunArray<Int64Type>>()
12744            .unwrap();
12745        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12746        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12747        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12748        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
12749    }
12750
12751    #[test]
12752    fn test_unsupported_cast_to_run_end_encoded() {
12753        // Create a Struct array - complex nested type that might not be supported
12754        let field = Field::new("item", DataType::Int32, false);
12755        let struct_array = StructArray::from(vec![(
12756            Arc::new(field),
12757            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
12758        )]);
12759        let array_ref = Arc::new(struct_array) as ArrayRef;
12760
12761        // This should fail because:
12762        // 1. The target type is not RunEndEncoded
12763        // 2. The target type is not supported for casting from StructArray
12764        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
12765
12766        // Expect this to fail
12767        assert!(cast_result.is_err());
12768    }
12769
12770    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
12771    #[test]
12772    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
12773        // Construct a valid REE array with Int64 run-ends
12774        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12775        let values = StringArray::from(vec!["a", "b", "c"]);
12776
12777        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12778        let array_ref = Arc::new(ree_array) as ArrayRef;
12779
12780        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12781        let target_type = DataType::RunEndEncoded(
12782            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12783            Arc::new(Field::new("values", DataType::Utf8, true)),
12784        );
12785        let cast_options = CastOptions {
12786            safe: false, // This should make it fail instead of returning nulls
12787            format_options: FormatOptions::default(),
12788        };
12789
12790        // This should fail due to run-end overflow
12791        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12792            cast_with_options(&array_ref, &target_type, &cast_options);
12793
12794        let e = result.expect_err("Cast should have failed but succeeded");
12795        assert!(
12796            e.to_string()
12797                .contains("Cast error: Can't cast value 100000 to type Int16")
12798        );
12799    }
12800
12801    #[test]
12802    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
12803        // Construct a valid REE array with Int64 run-ends
12804        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12805        let values = StringArray::from(vec!["a", "b", "c"]);
12806
12807        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12808        let array_ref = Arc::new(ree_array) as ArrayRef;
12809
12810        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12811        let target_type = DataType::RunEndEncoded(
12812            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12813            Arc::new(Field::new("values", DataType::Utf8, true)),
12814        );
12815        let cast_options = CastOptions {
12816            safe: true,
12817            format_options: FormatOptions::default(),
12818        };
12819
12820        // This fails even though safe is true because the run_ends array has null values
12821        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12822            cast_with_options(&array_ref, &target_type, &cast_options);
12823        let e = result.expect_err("Cast should have failed but succeeded");
12824        assert!(
12825            e.to_string()
12826                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
12827        );
12828    }
12829
12830    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
12831    #[test]
12832    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
12833        // Construct a valid REE array with Int16 run-ends
12834        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
12835        let values = StringArray::from(vec!["a", "b", "c"]);
12836
12837        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12838        let array_ref = Arc::new(ree_array) as ArrayRef;
12839
12840        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
12841        let target_type = DataType::RunEndEncoded(
12842            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12843            Arc::new(Field::new("values", DataType::Utf8, true)),
12844        );
12845        let cast_options = CastOptions {
12846            safe: false,
12847            format_options: FormatOptions::default(),
12848        };
12849
12850        // This should succeed due to valid upcast
12851        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12852            cast_with_options(&array_ref, &target_type, &cast_options);
12853
12854        let array_ref = result.expect("Cast should have succeeded but failed");
12855        // Downcast to RunArray<Int64Type>
12856        let run_array = array_ref
12857            .as_any()
12858            .downcast_ref::<RunArray<Int64Type>>()
12859            .unwrap();
12860
12861        // Verify the cast worked correctly
12862        // Assert the values were cast correctly
12863        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
12864        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
12865        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
12866        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12867    }
12868
12869    #[test]
12870    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
12871        // Construct a valid dictionary encoded array
12872        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
12873        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
12874        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
12875
12876        // Attempt to cast to RunEndEncoded<Int64, Utf8>
12877        let target_type = DataType::RunEndEncoded(
12878            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12879            Arc::new(Field::new("values", DataType::Utf8, true)),
12880        );
12881        let cast_options = CastOptions {
12882            safe: false,
12883            format_options: FormatOptions::default(),
12884        };
12885
12886        // This should succeed
12887        let result = cast_with_options(&array_ref, &target_type, &cast_options)
12888            .expect("Cast should have succeeded but failed");
12889
12890        // Verify the cast worked correctly
12891        // Assert the values were cast correctly
12892        let run_array = result
12893            .as_any()
12894            .downcast_ref::<RunArray<Int64Type>>()
12895            .unwrap();
12896        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
12897        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
12898        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12899
12900        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
12901        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
12902    }
12903
12904    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
12905        vec![
12906            Some(vec![Some(1), Some(2), Some(3)]),
12907            Some(vec![Some(4), Some(5), Some(6)]),
12908            None,
12909            Some(vec![Some(7), Some(8), Some(9)]),
12910            Some(vec![None, Some(10)]),
12911        ]
12912    }
12913
12914    #[test]
12915    fn test_cast_list_view_to_list() {
12916        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12917        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12918        assert!(can_cast_types(list_view.data_type(), &target_type));
12919        let cast_result = cast(&list_view, &target_type).unwrap();
12920        let got_list = cast_result.as_list::<i32>();
12921        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12922        assert_eq!(got_list, &expected_list);
12923    }
12924
12925    #[test]
12926    fn test_cast_list_view_to_large_list() {
12927        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12928        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
12929        assert!(can_cast_types(list_view.data_type(), &target_type));
12930        let cast_result = cast(&list_view, &target_type).unwrap();
12931        let got_list = cast_result.as_list::<i64>();
12932        let expected_list =
12933            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12934        assert_eq!(got_list, &expected_list);
12935    }
12936
12937    #[test]
12938    fn test_cast_list_to_list_view() {
12939        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12940        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12941        assert!(can_cast_types(list.data_type(), &target_type));
12942        let cast_result = cast(&list, &target_type).unwrap();
12943
12944        let got_list_view = cast_result.as_list_view::<i32>();
12945        let expected_list_view =
12946            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12947        assert_eq!(got_list_view, &expected_list_view);
12948
12949        // inner types get cast
12950        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12951            Some(vec![Some(1), Some(2)]),
12952            None,
12953            Some(vec![None, Some(3)]),
12954        ]);
12955        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
12956        assert!(can_cast_types(list.data_type(), &target_type));
12957        let cast_result = cast(&list, &target_type).unwrap();
12958
12959        let got_list_view = cast_result.as_list_view::<i32>();
12960        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
12961            Some(vec![Some(1.0), Some(2.0)]),
12962            None,
12963            Some(vec![None, Some(3.0)]),
12964        ]);
12965        assert_eq!(got_list_view, &expected_list_view);
12966    }
12967
12968    #[test]
12969    fn test_cast_list_to_large_list_view() {
12970        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12971            Some(vec![Some(1), Some(2)]),
12972            None,
12973            Some(vec![None, Some(3)]),
12974        ]);
12975        let target_type =
12976            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
12977        assert!(can_cast_types(list.data_type(), &target_type));
12978        let cast_result = cast(&list, &target_type).unwrap();
12979
12980        let got_list_view = cast_result.as_list_view::<i64>();
12981        let expected_list_view =
12982            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
12983                Some(vec![Some(1.0), Some(2.0)]),
12984                None,
12985                Some(vec![None, Some(3.0)]),
12986            ]);
12987        assert_eq!(got_list_view, &expected_list_view);
12988    }
12989
12990    #[test]
12991    fn test_cast_large_list_view_to_large_list() {
12992        let list_view =
12993            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12994        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
12995        assert!(can_cast_types(list_view.data_type(), &target_type));
12996        let cast_result = cast(&list_view, &target_type).unwrap();
12997        let got_list = cast_result.as_list::<i64>();
12998
12999        let expected_list =
13000            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13001        assert_eq!(got_list, &expected_list);
13002    }
13003
13004    #[test]
13005    fn test_cast_large_list_view_to_list() {
13006        let list_view =
13007            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13008        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13009        assert!(can_cast_types(list_view.data_type(), &target_type));
13010        let cast_result = cast(&list_view, &target_type).unwrap();
13011        let got_list = cast_result.as_list::<i32>();
13012
13013        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13014        assert_eq!(got_list, &expected_list);
13015    }
13016
13017    #[test]
13018    fn test_cast_large_list_to_large_list_view() {
13019        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13020        let target_type =
13021            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13022        assert!(can_cast_types(list.data_type(), &target_type));
13023        let cast_result = cast(&list, &target_type).unwrap();
13024
13025        let got_list_view = cast_result.as_list_view::<i64>();
13026        let expected_list_view =
13027            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13028        assert_eq!(got_list_view, &expected_list_view);
13029
13030        // inner types get cast
13031        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13032            Some(vec![Some(1), Some(2)]),
13033            None,
13034            Some(vec![None, Some(3)]),
13035        ]);
13036        let target_type =
13037            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13038        assert!(can_cast_types(list.data_type(), &target_type));
13039        let cast_result = cast(&list, &target_type).unwrap();
13040
13041        let got_list_view = cast_result.as_list_view::<i64>();
13042        let expected_list_view =
13043            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13044                Some(vec![Some(1.0), Some(2.0)]),
13045                None,
13046                Some(vec![None, Some(3.0)]),
13047            ]);
13048        assert_eq!(got_list_view, &expected_list_view);
13049    }
13050
13051    #[test]
13052    fn test_cast_large_list_to_list_view() {
13053        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13054            Some(vec![Some(1), Some(2)]),
13055            None,
13056            Some(vec![None, Some(3)]),
13057        ]);
13058        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13059        assert!(can_cast_types(list.data_type(), &target_type));
13060        let cast_result = cast(&list, &target_type).unwrap();
13061
13062        let got_list_view = cast_result.as_list_view::<i32>();
13063        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13064            Some(vec![Some(1.0), Some(2.0)]),
13065            None,
13066            Some(vec![None, Some(3.0)]),
13067        ]);
13068        assert_eq!(got_list_view, &expected_list_view);
13069    }
13070
13071    #[test]
13072    fn test_cast_list_view_to_list_out_of_order() {
13073        let list_view = ListViewArray::new(
13074            Arc::new(Field::new("item", DataType::Int32, true)),
13075            ScalarBuffer::from(vec![0, 6, 3]),
13076            ScalarBuffer::from(vec![3, 3, 3]),
13077            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13078            None,
13079        );
13080        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13081        assert!(can_cast_types(list_view.data_type(), &target_type));
13082        let cast_result = cast(&list_view, &target_type).unwrap();
13083        let got_list = cast_result.as_list::<i32>();
13084        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13085            Some(vec![Some(1), Some(2), Some(3)]),
13086            Some(vec![Some(7), Some(8), Some(9)]),
13087            Some(vec![Some(4), Some(5), Some(6)]),
13088        ]);
13089        assert_eq!(got_list, &expected_list);
13090    }
13091
13092    #[test]
13093    fn test_cast_list_view_to_list_overlapping() {
13094        let list_view = ListViewArray::new(
13095            Arc::new(Field::new("item", DataType::Int32, true)),
13096            ScalarBuffer::from(vec![0, 0]),
13097            ScalarBuffer::from(vec![1, 2]),
13098            Arc::new(Int32Array::from(vec![1, 2])),
13099            None,
13100        );
13101        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13102        assert!(can_cast_types(list_view.data_type(), &target_type));
13103        let cast_result = cast(&list_view, &target_type).unwrap();
13104        let got_list = cast_result.as_list::<i32>();
13105        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13106            Some(vec![Some(1)]),
13107            Some(vec![Some(1), Some(2)]),
13108        ]);
13109        assert_eq!(got_list, &expected_list);
13110    }
13111
13112    #[test]
13113    fn test_cast_list_view_to_list_empty() {
13114        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
13115        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13116        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13117        assert!(can_cast_types(list_view.data_type(), &target_type));
13118        let cast_result = cast(&list_view, &target_type).unwrap();
13119        let got_list = cast_result.as_list::<i32>();
13120        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
13121        assert_eq!(got_list, &expected_list);
13122    }
13123
13124    #[test]
13125    fn test_cast_list_view_to_list_different_inner_type() {
13126        let values = int32_list_values();
13127        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13128        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
13129        assert!(can_cast_types(list_view.data_type(), &target_type));
13130        let cast_result = cast(&list_view, &target_type).unwrap();
13131        let got_list = cast_result.as_list::<i32>();
13132
13133        let expected_list =
13134            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
13135                list.map(|list| {
13136                    list.into_iter()
13137                        .map(|v| v.map(|v| v as i64))
13138                        .collect::<Vec<_>>()
13139                })
13140            }));
13141        assert_eq!(got_list, &expected_list);
13142    }
13143
13144    #[test]
13145    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
13146        let list_view = ListViewArray::new(
13147            Arc::new(Field::new("item", DataType::Int32, true)),
13148            ScalarBuffer::from(vec![0, 6, 3]),
13149            ScalarBuffer::from(vec![3, 3, 3]),
13150            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13151            Some(NullBuffer::from(vec![false, true, false])),
13152        );
13153        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13154        assert!(can_cast_types(list_view.data_type(), &target_type));
13155        let cast_result = cast(&list_view, &target_type).unwrap();
13156        let got_list = cast_result.as_list::<i32>();
13157        let expected_list = ListArray::new(
13158            Arc::new(Field::new("item", DataType::Int32, true)),
13159            OffsetBuffer::from_lengths([3, 3, 3]),
13160            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
13161            Some(NullBuffer::from(vec![false, true, false])),
13162        );
13163        assert_eq!(got_list, &expected_list);
13164    }
13165
13166    #[test]
13167    fn test_cast_list_view_to_large_list_view() {
13168        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13169        let target_type =
13170            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13171        assert!(can_cast_types(list_view.data_type(), &target_type));
13172        let cast_result = cast(&list_view, &target_type).unwrap();
13173        let got = cast_result.as_list_view::<i64>();
13174
13175        let expected =
13176            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13177        assert_eq!(got, &expected);
13178    }
13179
13180    #[test]
13181    fn test_cast_large_list_view_to_list_view() {
13182        let list_view =
13183            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13184        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13185        assert!(can_cast_types(list_view.data_type(), &target_type));
13186        let cast_result = cast(&list_view, &target_type).unwrap();
13187        let got = cast_result.as_list_view::<i32>();
13188
13189        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13190        assert_eq!(got, &expected);
13191    }
13192
13193    #[test]
13194    fn test_cast_time32_second_to_int64() {
13195        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
13196        let array = Arc::new(array) as Arc<dyn Array>;
13197        let to_type = DataType::Int64;
13198        let cast_options = CastOptions::default();
13199
13200        assert!(can_cast_types(array.data_type(), &to_type));
13201
13202        let result = cast_with_options(&array, &to_type, &cast_options);
13203        assert!(
13204            result.is_ok(),
13205            "Failed to cast Time32(Second) to Int64: {:?}",
13206            result.err()
13207        );
13208
13209        let cast_array = result.unwrap();
13210        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13211
13212        assert_eq!(cast_array.value(0), 1000);
13213        assert_eq!(cast_array.value(1), 2000);
13214        assert_eq!(cast_array.value(2), 3000);
13215    }
13216
13217    #[test]
13218    fn test_cast_time32_millisecond_to_int64() {
13219        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
13220        let array = Arc::new(array) as Arc<dyn Array>;
13221        let to_type = DataType::Int64;
13222        let cast_options = CastOptions::default();
13223
13224        assert!(can_cast_types(array.data_type(), &to_type));
13225
13226        let result = cast_with_options(&array, &to_type, &cast_options);
13227        assert!(
13228            result.is_ok(),
13229            "Failed to cast Time32(Millisecond) to Int64: {:?}",
13230            result.err()
13231        );
13232
13233        let cast_array = result.unwrap();
13234        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13235
13236        assert_eq!(cast_array.value(0), 1000);
13237        assert_eq!(cast_array.value(1), 2000);
13238        assert_eq!(cast_array.value(2), 3000);
13239    }
13240
13241    #[test]
13242    fn test_cast_string_to_time32_second_to_int64() {
13243        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
13244        // raised in https://github.com/apache/datafusion/issues/19036
13245        let array = StringArray::from(vec!["03:12:44"]);
13246        let array = Arc::new(array) as Arc<dyn Array>;
13247        let cast_options = CastOptions::default();
13248
13249        // 1. Cast String to Time32(Second)
13250        let time32_type = DataType::Time32(TimeUnit::Second);
13251        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
13252
13253        // 2. Cast Time32(Second) to Int64
13254        let int64_type = DataType::Int64;
13255        assert!(can_cast_types(time32_array.data_type(), &int64_type));
13256
13257        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
13258
13259        assert!(
13260            result.is_ok(),
13261            "Failed to cast Time32(Second) to Int64: {:?}",
13262            result.err()
13263        );
13264
13265        let cast_array = result.unwrap();
13266        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13267
13268        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
13269        assert_eq!(cast_array.value(0), 11564);
13270    }
13271    #[test]
13272    fn test_string_dicts_to_binary_view() {
13273        let expected = BinaryViewArray::from_iter(vec![
13274            VIEW_TEST_DATA[1],
13275            VIEW_TEST_DATA[0],
13276            None,
13277            VIEW_TEST_DATA[3],
13278            None,
13279            VIEW_TEST_DATA[1],
13280            VIEW_TEST_DATA[4],
13281        ]);
13282
13283        let values_arrays: [ArrayRef; _] = [
13284            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
13285            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
13286            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
13287        ];
13288        for values in values_arrays {
13289            let keys =
13290                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13291            let string_dict_array =
13292                DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
13293
13294            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
13295            assert_eq!(casted.as_ref(), &expected);
13296        }
13297    }
13298
13299    #[test]
13300    fn test_binary_dicts_to_string_view() {
13301        let expected = StringViewArray::from_iter(vec![
13302            VIEW_TEST_DATA[1],
13303            VIEW_TEST_DATA[0],
13304            None,
13305            VIEW_TEST_DATA[3],
13306            None,
13307            VIEW_TEST_DATA[1],
13308            VIEW_TEST_DATA[4],
13309        ]);
13310
13311        let values_arrays: [ArrayRef; _] = [
13312            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
13313            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
13314            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
13315        ];
13316        for values in values_arrays {
13317            let keys =
13318                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13319            let string_dict_array =
13320                DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
13321
13322            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
13323            assert_eq!(casted.as_ref(), &expected);
13324        }
13325    }
13326
13327    #[test]
13328    fn test_cast_between_sliced_run_end_encoded() {
13329        let run_ends = Int16Array::from(vec![2, 5, 8]);
13330        let values = StringArray::from(vec!["a", "b", "c"]);
13331
13332        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13333        let ree_array = ree_array.slice(1, 2);
13334        let array_ref = Arc::new(ree_array) as ArrayRef;
13335
13336        let target_type = DataType::RunEndEncoded(
13337            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13338            Arc::new(Field::new("values", DataType::Utf8, true)),
13339        );
13340        let cast_options = CastOptions {
13341            safe: false,
13342            format_options: FormatOptions::default(),
13343        };
13344
13345        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
13346        let run_array = result.as_run::<Int64Type>();
13347        let run_array = run_array.downcast::<StringArray>().unwrap();
13348
13349        let expected = vec!["a", "b"];
13350        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
13351
13352        assert_eq!(expected, actual);
13353    }
13354}