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 list_view;
44mod map;
45mod run_array;
46mod string;
47
48use crate::cast::decimal::*;
49use crate::cast::dictionary::*;
50use crate::cast::list::*;
51use crate::cast::map::*;
52use crate::cast::run_array::*;
53use crate::cast::string::*;
54
55use arrow_buffer::IntervalMonthDayNano;
56use arrow_data::ByteView;
57use chrono::{NaiveTime, Offset, TimeZone, Utc};
58use std::cmp::Ordering;
59use std::sync::Arc;
60
61use crate::display::{ArrayFormatter, FormatOptions};
62use crate::parse::{
63    Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
64    string_to_datetime,
65};
66use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
67use arrow_buffer::{ArrowNativeType, OffsetBuffer, i256};
68use arrow_data::ArrayData;
69use arrow_data::transform::MutableArrayData;
70use arrow_schema::*;
71use arrow_select::take::take;
72use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
73
74use crate::cast::list_view::{cast_list_to_list_view, cast_list_view, cast_list_view_to_list};
75pub use decimal::{DecimalCast, rescale_decimal};
76
77/// CastOptions provides a way to override the default cast behaviors
78#[derive(Debug, Clone, PartialEq, Eq, Hash)]
79pub struct CastOptions<'a> {
80    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
81    pub safe: bool,
82    /// Formatting options when casting from temporal types to string
83    pub format_options: FormatOptions<'a>,
84}
85
86impl Default for CastOptions<'_> {
87    fn default() -> Self {
88        Self {
89            safe: true,
90            format_options: FormatOptions::default(),
91        }
92    }
93}
94
95/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
96///
97/// See [`cast_with_options`] for more information
98pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
99    use self::DataType::*;
100    use self::IntervalUnit::*;
101    use self::TimeUnit::*;
102    if from_type == to_type {
103        return true;
104    }
105
106    match (from_type, to_type) {
107        (Null, _) => true,
108        // Dictionary/List conditions should be put in front of others
109        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
110            can_cast_types(from_value_type, to_value_type)
111        }
112        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
113        (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
114        (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
115        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
116        (List(list_from) | LargeList(list_from), List(list_to) | LargeList(list_to)) => {
117            can_cast_types(list_from.data_type(), list_to.data_type())
118        }
119        (List(list_from) | LargeList(list_from), Utf8 | LargeUtf8) => {
120            can_cast_types(list_from.data_type(), to_type)
121        }
122        (List(list_from) | LargeList(list_from), FixedSizeList(list_to, _)) => {
123            can_cast_types(list_from.data_type(), list_to.data_type())
124        }
125        (List(list_from) | LargeList(list_from), ListView(list_to) | LargeListView(list_to)) => {
126            can_cast_types(list_from.data_type(), list_to.data_type())
127        }
128        (List(_), _) => false,
129        (ListView(list_from) | LargeListView(list_from), List(list_to) | LargeList(list_to)) => {
130            can_cast_types(list_from.data_type(), list_to.data_type())
131        }
132        (ListView(list_from), LargeListView(list_to)) => {
133            can_cast_types(list_from.data_type(), list_to.data_type())
134        }
135        (LargeListView(list_from), ListView(list_to)) => {
136            can_cast_types(list_from.data_type(), list_to.data_type())
137        }
138        (FixedSizeList(list_from, _), List(list_to))
139        | (FixedSizeList(list_from, _), LargeList(list_to)) => {
140            can_cast_types(list_from.data_type(), list_to.data_type())
141        }
142        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
143            can_cast_types(inner.data_type(), inner_to.data_type())
144        }
145        (_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
146        (_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
147        (_, FixedSizeList(list_to, size)) if *size == 1 => {
148            can_cast_types(from_type, list_to.data_type())
149        }
150        (FixedSizeList(list_from, size), _) if *size == 1 => {
151            can_cast_types(list_from.data_type(), to_type)
152        }
153        (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
154            if ordered_from == ordered_to =>
155        {
156            match (
157                key_field(from_entries),
158                key_field(to_entries),
159                value_field(from_entries),
160                value_field(to_entries),
161            ) {
162                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
163                    can_cast_types(from_key.data_type(), to_key.data_type())
164                        && can_cast_types(from_value.data_type(), to_value.data_type())
165                }
166                _ => false,
167            }
168        }
169        // cast one decimal type to another decimal type
170        (
171            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
172            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
173        ) => true,
174        // unsigned integer to decimal
175        (
176            UInt8 | UInt16 | UInt32 | UInt64,
177            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
178        ) => true,
179        // signed numeric to decimal
180        (
181            Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
182            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
183        ) => true,
184        // decimal to unsigned numeric
185        (
186            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
187            UInt8 | UInt16 | UInt32 | UInt64,
188        ) => true,
189        // decimal to signed numeric
190        (
191            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
192            Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
193        ) => true,
194        // decimal to string
195        (
196            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
197            Utf8View | Utf8 | LargeUtf8,
198        ) => true,
199        // string to decimal
200        (
201            Utf8View | Utf8 | LargeUtf8,
202            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
203        ) => true,
204        (Struct(from_fields), Struct(to_fields)) => {
205            if from_fields.len() != to_fields.len() {
206                return false;
207            }
208
209            // fast path, all field names are in the same order and same number of fields
210            if from_fields
211                .iter()
212                .zip(to_fields.iter())
213                .all(|(f1, f2)| f1.name() == f2.name())
214            {
215                return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
216                    // Assume that nullability between two structs are compatible, if not,
217                    // cast kernel will return error.
218                    can_cast_types(f1.data_type(), f2.data_type())
219                });
220            }
221
222            // slow path, we match the fields by name
223            if to_fields.iter().all(|to_field| {
224                from_fields
225                    .iter()
226                    .find(|from_field| from_field.name() == to_field.name())
227                    .is_some_and(|from_field| {
228                        // Assume that nullability between two structs are compatible, if not,
229                        // cast kernel will return error.
230                        can_cast_types(from_field.data_type(), to_field.data_type())
231                    })
232            }) {
233                return true;
234            }
235
236            // if we couldn't match by name, we try to see if they can be matched by position
237            from_fields
238                .iter()
239                .zip(to_fields.iter())
240                .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
241        }
242        (Struct(_), _) => false,
243        (_, Struct(_)) => false,
244
245        (_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
246        (Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
247
248        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
249            true
250        }
251        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
252            true
253        }
254        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
255        (
256            Utf8 | LargeUtf8 | Utf8View,
257            Binary
258            | LargeBinary
259            | Utf8
260            | LargeUtf8
261            | Date32
262            | Date64
263            | Time32(Second)
264            | Time32(Millisecond)
265            | Time64(Microsecond)
266            | Time64(Nanosecond)
267            | Timestamp(Second, _)
268            | Timestamp(Millisecond, _)
269            | Timestamp(Microsecond, _)
270            | Timestamp(Nanosecond, _)
271            | Interval(_)
272            | BinaryView,
273        ) => true,
274        (Utf8 | LargeUtf8, Utf8View) => true,
275        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
276        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
277        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
278
279        (_, Binary | LargeBinary) => from_type.is_integer(),
280
281        // start numeric casts
282        (
283            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
284            | Float64,
285            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
286            | Float64,
287        ) => true,
288        // end numeric casts
289
290        // temporal casts
291        (Int32, Date32 | Date64 | Time32(_)) => true,
292        (Date32, Int32 | Int64) => true,
293        (Time32(_), Int32 | Int64) => true,
294        (Int64, Date64 | Date32 | Time64(_)) => true,
295        (Date64, Int64 | Int32) => true,
296        (Time64(_), Int64) => true,
297        (Date32 | Date64, Date32 | Date64) => true,
298        // time casts
299        (Time32(_), Time32(_)) => true,
300        (Time32(_), Time64(_)) => true,
301        (Time64(_), Time64(_)) => true,
302        (Time64(_), Time32(to_unit)) => {
303            matches!(to_unit, Second | Millisecond)
304        }
305        (Timestamp(_, _), _) if to_type.is_numeric() => true,
306        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
307        (Date64, Timestamp(_, _)) => true,
308        (Date32, Timestamp(_, _)) => true,
309        (
310            Timestamp(_, _),
311            Timestamp(_, _)
312            | Date32
313            | Date64
314            | Time32(Second)
315            | Time32(Millisecond)
316            | Time64(Microsecond)
317            | Time64(Nanosecond),
318        ) => true,
319        (_, Duration(_)) if from_type.is_numeric() => true,
320        (Duration(_), _) if to_type.is_numeric() => true,
321        (Duration(_), Duration(_)) => true,
322        (Interval(from_type), Int64) => {
323            match from_type {
324                YearMonth => true,
325                DayTime => true,
326                MonthDayNano => false, // Native type is i128
327            }
328        }
329        (Int32, Interval(to_type)) => match to_type {
330            YearMonth => true,
331            DayTime => false,
332            MonthDayNano => false,
333        },
334        (Duration(_), Interval(MonthDayNano)) => true,
335        (Interval(MonthDayNano), Duration(_)) => true,
336        (Interval(YearMonth), Interval(MonthDayNano)) => true,
337        (Interval(DayTime), Interval(MonthDayNano)) => true,
338        (_, _) => false,
339    }
340}
341
342/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
343///
344/// See [`cast_with_options`] for more information
345pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
346    cast_with_options(array, to_type, &CastOptions::default())
347}
348
349fn cast_integer_to_decimal<
350    T: ArrowPrimitiveType,
351    D: DecimalType + ArrowPrimitiveType<Native = M>,
352    M,
353>(
354    array: &PrimitiveArray<T>,
355    precision: u8,
356    scale: i8,
357    base: M,
358    cast_options: &CastOptions,
359) -> Result<ArrayRef, ArrowError>
360where
361    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
362    M: ArrowNativeTypeOp,
363{
364    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
365        ArrowError::CastError(format!(
366            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
367            D::PREFIX,
368            precision,
369            scale,
370        ))
371    })?;
372
373    let array = if scale < 0 {
374        match cast_options.safe {
375            true => array.unary_opt::<_, D>(|v| {
376                v.as_()
377                    .div_checked(scale_factor)
378                    .ok()
379                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
380            }),
381            false => array.try_unary::<_, D, _>(|v| {
382                v.as_()
383                    .div_checked(scale_factor)
384                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
385            })?,
386        }
387    } else {
388        match cast_options.safe {
389            true => array.unary_opt::<_, D>(|v| {
390                v.as_()
391                    .mul_checked(scale_factor)
392                    .ok()
393                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
394            }),
395            false => array.try_unary::<_, D, _>(|v| {
396                v.as_()
397                    .mul_checked(scale_factor)
398                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
399            })?,
400        }
401    };
402
403    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
404}
405
406/// Cast the array from interval year month to month day nano
407fn cast_interval_year_month_to_interval_month_day_nano(
408    array: &dyn Array,
409    _cast_options: &CastOptions,
410) -> Result<ArrayRef, ArrowError> {
411    let array = array.as_primitive::<IntervalYearMonthType>();
412
413    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
414        let months = IntervalYearMonthType::to_months(v);
415        IntervalMonthDayNanoType::make_value(months, 0, 0)
416    })))
417}
418
419/// Cast the array from interval day time to month day nano
420fn cast_interval_day_time_to_interval_month_day_nano(
421    array: &dyn Array,
422    _cast_options: &CastOptions,
423) -> Result<ArrayRef, ArrowError> {
424    let array = array.as_primitive::<IntervalDayTimeType>();
425    let mul = 1_000_000;
426
427    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
428        let (days, ms) = IntervalDayTimeType::to_parts(v);
429        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
430    })))
431}
432
433/// Cast the array from interval to duration
434fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
435    array: &dyn Array,
436    cast_options: &CastOptions,
437) -> Result<ArrayRef, ArrowError> {
438    let array = array.as_primitive::<IntervalMonthDayNanoType>();
439    let scale = match D::DATA_TYPE {
440        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
441        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
442        DataType::Duration(TimeUnit::Microsecond) => 1_000,
443        DataType::Duration(TimeUnit::Nanosecond) => 1,
444        _ => unreachable!(),
445    };
446
447    if cast_options.safe {
448        let iter = array.iter().map(|v| {
449            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
450        });
451        Ok(Arc::new(unsafe {
452            PrimitiveArray::<D>::from_trusted_len_iter(iter)
453        }))
454    } else {
455        let vec = array
456            .iter()
457            .map(|v| {
458                v.map(|v| match v.days == 0 && v.months == 0 {
459                    true => Ok((v.nanoseconds) / scale),
460                    _ => Err(ArrowError::ComputeError(
461                        "Cannot convert interval containing non-zero months or days to duration"
462                            .to_string(),
463                    )),
464                })
465                .transpose()
466            })
467            .collect::<Result<Vec<_>, _>>()?;
468        Ok(Arc::new(unsafe {
469            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
470        }))
471    }
472}
473
474/// Cast the array from duration and interval
475fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
476    array: &dyn Array,
477    cast_options: &CastOptions,
478) -> Result<ArrayRef, ArrowError> {
479    let array = array
480        .as_any()
481        .downcast_ref::<PrimitiveArray<D>>()
482        .ok_or_else(|| {
483            ArrowError::ComputeError(
484                "Internal Error: Cannot cast duration to DurationArray of expected type"
485                    .to_string(),
486            )
487        })?;
488
489    let scale = match array.data_type() {
490        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
491        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
492        DataType::Duration(TimeUnit::Microsecond) => 1_000,
493        DataType::Duration(TimeUnit::Nanosecond) => 1,
494        _ => unreachable!(),
495    };
496
497    if cast_options.safe {
498        let iter = array.iter().map(|v| {
499            v.and_then(|v| {
500                v.checked_mul(scale)
501                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
502            })
503        });
504        Ok(Arc::new(unsafe {
505            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
506        }))
507    } else {
508        let vec = array
509            .iter()
510            .map(|v| {
511                v.map(|v| {
512                    if let Ok(v) = v.mul_checked(scale) {
513                        Ok(IntervalMonthDayNano::new(0, 0, v))
514                    } else {
515                        Err(ArrowError::ComputeError(format!(
516                            "Cannot cast to {:?}. Overflowing on {:?}",
517                            IntervalMonthDayNanoType::DATA_TYPE,
518                            v
519                        )))
520                    }
521                })
522                .transpose()
523            })
524            .collect::<Result<Vec<_>, _>>()?;
525        Ok(Arc::new(unsafe {
526            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
527        }))
528    }
529}
530
531/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
532fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
533    array: &dyn Array,
534) -> Result<ArrayRef, ArrowError> {
535    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
536}
537
538fn make_timestamp_array(
539    array: &PrimitiveArray<Int64Type>,
540    unit: TimeUnit,
541    tz: Option<Arc<str>>,
542) -> ArrayRef {
543    match unit {
544        TimeUnit::Second => Arc::new(
545            array
546                .reinterpret_cast::<TimestampSecondType>()
547                .with_timezone_opt(tz),
548        ),
549        TimeUnit::Millisecond => Arc::new(
550            array
551                .reinterpret_cast::<TimestampMillisecondType>()
552                .with_timezone_opt(tz),
553        ),
554        TimeUnit::Microsecond => Arc::new(
555            array
556                .reinterpret_cast::<TimestampMicrosecondType>()
557                .with_timezone_opt(tz),
558        ),
559        TimeUnit::Nanosecond => Arc::new(
560            array
561                .reinterpret_cast::<TimestampNanosecondType>()
562                .with_timezone_opt(tz),
563        ),
564    }
565}
566
567fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
568    match unit {
569        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
570        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
571        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
572        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
573    }
574}
575
576fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
577    v: i64,
578    tz: Option<Tz>,
579) -> Result<NaiveTime, ArrowError> {
580    let time = match tz {
581        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
582        None => as_datetime::<T>(v).map(|d| d.time()),
583    };
584
585    time.ok_or_else(|| {
586        ArrowError::CastError(format!(
587            "Failed to create naive time with {} {}",
588            std::any::type_name::<T>(),
589            v
590        ))
591    })
592}
593
594fn timestamp_to_date32<T: ArrowTimestampType>(
595    array: &PrimitiveArray<T>,
596) -> Result<ArrayRef, ArrowError> {
597    let err = |x: i64| {
598        ArrowError::CastError(format!(
599            "Cannot convert {} {x} to datetime",
600            std::any::type_name::<T>()
601        ))
602    };
603
604    let array: Date32Array = match array.timezone() {
605        Some(tz) => {
606            let tz: Tz = tz.parse()?;
607            array.try_unary(|x| {
608                as_datetime_with_timezone::<T>(x, tz)
609                    .ok_or_else(|| err(x))
610                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
611            })?
612        }
613        None => array.try_unary(|x| {
614            as_datetime::<T>(x)
615                .ok_or_else(|| err(x))
616                .map(|d| Date32Type::from_naive_date(d.date()))
617        })?,
618    };
619    Ok(Arc::new(array))
620}
621
622/// Try to cast `array` to `to_type` if possible.
623///
624/// Returns a new Array with type `to_type` if possible.
625///
626/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
627///
628/// # Behavior
629/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
630/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
631///   short variants are accepted, other strings return null or error
632/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
633///   in integer casts return null
634/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
635/// * `List` to `List`: the underlying data type is cast
636/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
637///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
638/// * Primitive to `List`: a list array with 1 value per slot is created
639/// * `Date32` and `Date64`: precision lost when going to higher interval
640/// * `Time32 and `Time64`: precision lost when going to higher interval
641/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
642/// * Temporal to/from backing Primitive: zero-copy with data type change
643/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
644///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
645/// * `Decimal` to `Float32/Float64` is lossy and values outside the representable
646///   range become `INFINITY` or `-INFINITY` without error.
647///
648/// Unsupported Casts (check with `can_cast_types` before calling):
649/// * To or from `StructArray`
650/// * `List` to `Primitive`
651/// * `Interval` and `Duration`
652///
653/// # Durations and Intervals
654///
655/// Casting integer types directly to interval types such as
656/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
657/// is ambiguous. For example, the integer  could represent either nanoseconds
658/// or months.
659///
660/// To cast an integer type to an interval type, first convert to a Duration
661/// type, and then cast that to the desired interval type.
662///
663/// For example, to convert an `Int64` representing nanoseconds to an
664/// `IntervalMonthDayNano` you would first convert the `Int64` to a
665/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
666///
667/// # Timestamps and Timezones
668///
669/// Timestamps are stored with an optional timezone in Arrow.
670///
671/// ## Casting timestamps to a timestamp without timezone / UTC
672/// ```
673/// # use arrow_array::Int64Array;
674/// # use arrow_array::types::TimestampSecondType;
675/// # use arrow_cast::{cast, display};
676/// # use arrow_array::cast::AsArray;
677/// # use arrow_schema::{DataType, TimeUnit};
678/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
679/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
680/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
681/// let b = cast(&a, &data_type).unwrap();
682/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
683/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
684/// // use display to show them (note has no trailing Z)
685/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
686/// ```
687///
688/// ## Casting timestamps to a timestamp with timezone
689///
690/// Similarly to the previous example, if you cast numeric values to a timestamp
691/// with timezone, the cast kernel will not change the underlying values
692/// but display and other functions will interpret them as being in the provided timezone.
693///
694/// ```
695/// # use arrow_array::Int64Array;
696/// # use arrow_array::types::TimestampSecondType;
697/// # use arrow_cast::{cast, display};
698/// # use arrow_array::cast::AsArray;
699/// # use arrow_schema::{DataType, TimeUnit};
700/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
701/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
702/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
703/// let b = cast(&a, &data_type).unwrap();
704/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
705/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
706/// // displayed in the target timezone (note the offset -05:00)
707/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
708/// ```
709/// # Casting timestamps without timezone to timestamps with timezone
710///
711/// When casting from a timestamp without timezone to a timestamp with
712/// timezone, the cast kernel interprets the timestamp values as being in
713/// the destination timezone and then adjusts the underlying value to UTC as required
714///
715/// However, note that when casting from a timestamp with timezone BACK to a
716/// timestamp without timezone the cast kernel does not adjust the values.
717///
718/// Thus round trip casting a timestamp without timezone to a timestamp with
719/// timezone and back to a timestamp without timezone results in different
720/// values than the starting values.
721///
722/// ```
723/// # use arrow_array::Int64Array;
724/// # use arrow_array::types::{TimestampSecondType};
725/// # use arrow_cast::{cast, display};
726/// # use arrow_array::cast::AsArray;
727/// # use arrow_schema::{DataType, TimeUnit};
728/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
729/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
730/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
731/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
732/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
733/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
734/// // displayed without a timezone (note lack of offset or Z)
735/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
736///
737/// // Convert timestamps without a timezone to timestamps with a timezone
738/// let c = cast(&b, &data_type_tz).unwrap();
739/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
740/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
741/// // displayed with the target timezone offset (-05:00)
742/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
743///
744/// // Convert from timestamp with timezone back to timestamp without timezone
745/// let d = cast(&c, &data_type).unwrap();
746/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
747/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
748/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
749/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
750/// ```
751pub fn cast_with_options(
752    array: &dyn Array,
753    to_type: &DataType,
754    cast_options: &CastOptions,
755) -> Result<ArrayRef, ArrowError> {
756    use DataType::*;
757    let from_type = array.data_type();
758    // clone array if types are the same
759    if from_type == to_type {
760        return Ok(make_array(array.to_data()));
761    }
762    match (from_type, to_type) {
763        (Null, _) => Ok(new_null_array(to_type, array.len())),
764        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
765            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
766            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
767            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
768            _ => Err(ArrowError::CastError(format!(
769                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
770            ))),
771        },
772        (_, RunEndEncoded(index_type, value_type)) => {
773            let array_ref = make_array(array.to_data());
774            match index_type.data_type() {
775                Int16 => cast_to_run_end_encoded::<Int16Type>(
776                    &array_ref,
777                    value_type.data_type(),
778                    cast_options,
779                ),
780                Int32 => cast_to_run_end_encoded::<Int32Type>(
781                    &array_ref,
782                    value_type.data_type(),
783                    cast_options,
784                ),
785                Int64 => cast_to_run_end_encoded::<Int64Type>(
786                    &array_ref,
787                    value_type.data_type(),
788                    cast_options,
789                ),
790                _ => Err(ArrowError::CastError(format!(
791                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
792                ))),
793            }
794        }
795        (Dictionary(index_type, _), _) => match **index_type {
796            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
797            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
798            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
799            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
800            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
801            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
802            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
803            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
804            _ => Err(ArrowError::CastError(format!(
805                "Casting from dictionary type {from_type} to {to_type} not supported",
806            ))),
807        },
808        (_, Dictionary(index_type, value_type)) => match **index_type {
809            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
810            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
811            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
812            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
813            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
814            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
815            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
816            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
817            _ => Err(ArrowError::CastError(format!(
818                "Casting from type {from_type} to dictionary type {to_type} not supported",
819            ))),
820        },
821        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
822        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
823        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
824        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
825        (List(_), FixedSizeList(field, size)) => {
826            let array = array.as_list::<i32>();
827            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
828        }
829        (LargeList(_), FixedSizeList(field, size)) => {
830            let array = array.as_list::<i64>();
831            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
832        }
833        (ListView(_), List(list_to)) => cast_list_view_to_list::<i32>(array, list_to, cast_options),
834        (LargeListView(_), LargeList(list_to)) => {
835            cast_list_view_to_list::<i64>(array, list_to, cast_options)
836        }
837        (ListView(_), LargeListView(list_to)) => {
838            cast_list_view::<i32, i64>(array, list_to, cast_options)
839        }
840        (LargeListView(_), ListView(list_to)) => {
841            cast_list_view::<i64, i32>(array, list_to, cast_options)
842        }
843        (List(_), ListView(_)) => cast_list_to_list_view::<i32>(array),
844        (LargeList(_), LargeListView(_)) => cast_list_to_list_view::<i64>(array),
845        (List(_) | LargeList(_), _) => match to_type {
846            Utf8 => value_to_string::<i32>(array, cast_options),
847            LargeUtf8 => value_to_string::<i64>(array, cast_options),
848            _ => Err(ArrowError::CastError(
849                "Cannot cast list to non-list data types".to_string(),
850            )),
851        },
852        (FixedSizeList(list_from, size), List(list_to)) => {
853            if list_to.data_type() != list_from.data_type() {
854                // To transform inner type, can first cast to FSL with new inner type.
855                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
856                let array = cast_with_options(array, &fsl_to, cast_options)?;
857                cast_fixed_size_list_to_list::<i32>(array.as_ref())
858            } else {
859                cast_fixed_size_list_to_list::<i32>(array)
860            }
861        }
862        (FixedSizeList(list_from, size), LargeList(list_to)) => {
863            if list_to.data_type() != list_from.data_type() {
864                // To transform inner type, can first cast to FSL with new inner type.
865                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
866                let array = cast_with_options(array, &fsl_to, cast_options)?;
867                cast_fixed_size_list_to_list::<i64>(array.as_ref())
868            } else {
869                cast_fixed_size_list_to_list::<i64>(array)
870            }
871        }
872        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
873            if size_from != size_to {
874                return Err(ArrowError::CastError(
875                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
876                ));
877            }
878            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
879            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
880            Ok(Arc::new(FixedSizeListArray::try_new(
881                list_to.clone(),
882                *size_from,
883                values,
884                array.nulls().cloned(),
885            )?))
886        }
887        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
888        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
889        (_, FixedSizeList(to, size)) if *size == 1 => {
890            cast_values_to_fixed_size_list(array, to, *size, cast_options)
891        }
892        (FixedSizeList(_, size), _) if *size == 1 => {
893            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
894        }
895        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
896            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
897        }
898        // Decimal to decimal, same width
899        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
900            cast_decimal_to_decimal_same_type::<Decimal32Type>(
901                array.as_primitive(),
902                *p1,
903                *s1,
904                *p2,
905                *s2,
906                cast_options,
907            )
908        }
909        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
910            cast_decimal_to_decimal_same_type::<Decimal64Type>(
911                array.as_primitive(),
912                *p1,
913                *s1,
914                *p2,
915                *s2,
916                cast_options,
917            )
918        }
919        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
920            cast_decimal_to_decimal_same_type::<Decimal128Type>(
921                array.as_primitive(),
922                *p1,
923                *s1,
924                *p2,
925                *s2,
926                cast_options,
927            )
928        }
929        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
930            cast_decimal_to_decimal_same_type::<Decimal256Type>(
931                array.as_primitive(),
932                *p1,
933                *s1,
934                *p2,
935                *s2,
936                cast_options,
937            )
938        }
939        // Decimal to decimal, different width
940        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
941            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
942                array.as_primitive(),
943                *p1,
944                *s1,
945                *p2,
946                *s2,
947                cast_options,
948            )
949        }
950        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
951            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
952                array.as_primitive(),
953                *p1,
954                *s1,
955                *p2,
956                *s2,
957                cast_options,
958            )
959        }
960        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
961            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
962                array.as_primitive(),
963                *p1,
964                *s1,
965                *p2,
966                *s2,
967                cast_options,
968            )
969        }
970        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
971            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
972                array.as_primitive(),
973                *p1,
974                *s1,
975                *p2,
976                *s2,
977                cast_options,
978            )
979        }
980        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
981            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
982                array.as_primitive(),
983                *p1,
984                *s1,
985                *p2,
986                *s2,
987                cast_options,
988            )
989        }
990        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
991            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
992                array.as_primitive(),
993                *p1,
994                *s1,
995                *p2,
996                *s2,
997                cast_options,
998            )
999        }
1000        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1001            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1002                array.as_primitive(),
1003                *p1,
1004                *s1,
1005                *p2,
1006                *s2,
1007                cast_options,
1008            )
1009        }
1010        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1011            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1012                array.as_primitive(),
1013                *p1,
1014                *s1,
1015                *p2,
1016                *s2,
1017                cast_options,
1018            )
1019        }
1020        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1021            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1022                array.as_primitive(),
1023                *p1,
1024                *s1,
1025                *p2,
1026                *s2,
1027                cast_options,
1028            )
1029        }
1030        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1031            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1032                array.as_primitive(),
1033                *p1,
1034                *s1,
1035                *p2,
1036                *s2,
1037                cast_options,
1038            )
1039        }
1040        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1041            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1042                array.as_primitive(),
1043                *p1,
1044                *s1,
1045                *p2,
1046                *s2,
1047                cast_options,
1048            )
1049        }
1050        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1051            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1052                array.as_primitive(),
1053                *p1,
1054                *s1,
1055                *p2,
1056                *s2,
1057                cast_options,
1058            )
1059        }
1060        // Decimal to non-decimal
1061        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1062            cast_from_decimal::<Decimal32Type, _>(
1063                array,
1064                10_i32,
1065                scale,
1066                from_type,
1067                to_type,
1068                |x: i32| x as f64,
1069                cast_options,
1070            )
1071        }
1072        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1073            cast_from_decimal::<Decimal64Type, _>(
1074                array,
1075                10_i64,
1076                scale,
1077                from_type,
1078                to_type,
1079                |x: i64| x as f64,
1080                cast_options,
1081            )
1082        }
1083        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1084            cast_from_decimal::<Decimal128Type, _>(
1085                array,
1086                10_i128,
1087                scale,
1088                from_type,
1089                to_type,
1090                |x: i128| x as f64,
1091                cast_options,
1092            )
1093        }
1094        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1095            cast_from_decimal::<Decimal256Type, _>(
1096                array,
1097                i256::from_i128(10_i128),
1098                scale,
1099                from_type,
1100                to_type,
1101                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1102                cast_options,
1103            )
1104        }
1105        // Non-decimal to decimal
1106        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1107            cast_to_decimal::<Decimal32Type, _>(
1108                array,
1109                10_i32,
1110                precision,
1111                scale,
1112                from_type,
1113                to_type,
1114                cast_options,
1115            )
1116        }
1117        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1118            cast_to_decimal::<Decimal64Type, _>(
1119                array,
1120                10_i64,
1121                precision,
1122                scale,
1123                from_type,
1124                to_type,
1125                cast_options,
1126            )
1127        }
1128        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1129            cast_to_decimal::<Decimal128Type, _>(
1130                array,
1131                10_i128,
1132                precision,
1133                scale,
1134                from_type,
1135                to_type,
1136                cast_options,
1137            )
1138        }
1139        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1140            cast_to_decimal::<Decimal256Type, _>(
1141                array,
1142                i256::from_i128(10_i128),
1143                precision,
1144                scale,
1145                from_type,
1146                to_type,
1147                cast_options,
1148            )
1149        }
1150        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1151            array.as_struct(),
1152            from_fields.clone(),
1153            to_fields.clone(),
1154            cast_options,
1155        ),
1156        (Struct(_), _) => Err(ArrowError::CastError(format!(
1157            "Casting from {from_type} to {to_type} not supported"
1158        ))),
1159        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1160            "Casting from {from_type} to {to_type} not supported"
1161        ))),
1162        (_, Boolean) => match from_type {
1163            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1164            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1165            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1166            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1167            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1168            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1169            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1170            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1171            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1172            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1173            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1174            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1175            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1176            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1177            _ => Err(ArrowError::CastError(format!(
1178                "Casting from {from_type} to {to_type} not supported",
1179            ))),
1180        },
1181        (Boolean, _) => match to_type {
1182            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1183            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1184            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1185            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1186            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1187            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1188            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1189            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1190            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1191            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1192            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1193            Utf8View => value_to_string_view(array, cast_options),
1194            Utf8 => value_to_string::<i32>(array, cast_options),
1195            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1196            _ => Err(ArrowError::CastError(format!(
1197                "Casting from {from_type} to {to_type} not supported",
1198            ))),
1199        },
1200        (Utf8, _) => match to_type {
1201            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1202            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1203            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1204            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1205            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1206            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1207            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1208            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1209            Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1210            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1211            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1212            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1213            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1214            Binary => Ok(Arc::new(BinaryArray::from(
1215                array.as_string::<i32>().clone(),
1216            ))),
1217            LargeBinary => {
1218                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1219                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1220            }
1221            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1222            BinaryView => Ok(Arc::new(
1223                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1224            )),
1225            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1226            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1227            Time32(TimeUnit::Millisecond) => {
1228                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1229            }
1230            Time64(TimeUnit::Microsecond) => {
1231                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1232            }
1233            Time64(TimeUnit::Nanosecond) => {
1234                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1235            }
1236            Timestamp(TimeUnit::Second, to_tz) => {
1237                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1238            }
1239            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1240                i32,
1241                TimestampMillisecondType,
1242            >(array, to_tz, cast_options),
1243            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1244                i32,
1245                TimestampMicrosecondType,
1246            >(array, to_tz, cast_options),
1247            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1248                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1249            }
1250            Interval(IntervalUnit::YearMonth) => {
1251                cast_string_to_year_month_interval::<i32>(array, cast_options)
1252            }
1253            Interval(IntervalUnit::DayTime) => {
1254                cast_string_to_day_time_interval::<i32>(array, cast_options)
1255            }
1256            Interval(IntervalUnit::MonthDayNano) => {
1257                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1258            }
1259            _ => Err(ArrowError::CastError(format!(
1260                "Casting from {from_type} to {to_type} not supported",
1261            ))),
1262        },
1263        (Utf8View, _) => match to_type {
1264            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1265            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1266            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1267            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1268            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1269            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1270            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1271            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1272            Float16 => parse_string_view::<Float16Type>(array, cast_options),
1273            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1274            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1275            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1276            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1277            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1278            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1279            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1280            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1281            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1282            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1283            Time32(TimeUnit::Millisecond) => {
1284                parse_string_view::<Time32MillisecondType>(array, cast_options)
1285            }
1286            Time64(TimeUnit::Microsecond) => {
1287                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1288            }
1289            Time64(TimeUnit::Nanosecond) => {
1290                parse_string_view::<Time64NanosecondType>(array, cast_options)
1291            }
1292            Timestamp(TimeUnit::Second, to_tz) => {
1293                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1294            }
1295            Timestamp(TimeUnit::Millisecond, to_tz) => {
1296                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1297            }
1298            Timestamp(TimeUnit::Microsecond, to_tz) => {
1299                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1300            }
1301            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1302                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1303            }
1304            Interval(IntervalUnit::YearMonth) => {
1305                cast_view_to_year_month_interval(array, cast_options)
1306            }
1307            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1308            Interval(IntervalUnit::MonthDayNano) => {
1309                cast_view_to_month_day_nano_interval(array, cast_options)
1310            }
1311            _ => Err(ArrowError::CastError(format!(
1312                "Casting from {from_type} to {to_type} not supported",
1313            ))),
1314        },
1315        (LargeUtf8, _) => match to_type {
1316            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1317            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1318            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1319            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1320            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1321            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1322            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1323            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1324            Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1325            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1326            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1327            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1328            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1329            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1330            Binary => {
1331                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1332                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1333            }
1334            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1335                array.as_string::<i64>().clone(),
1336            ))),
1337            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1338            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1339                array
1340                    .as_string::<i64>()
1341                    .into_iter()
1342                    .map(|x| x.map(|x| x.as_bytes()))
1343                    .collect::<Vec<_>>(),
1344            ))),
1345            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1346            Time32(TimeUnit::Millisecond) => {
1347                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1348            }
1349            Time64(TimeUnit::Microsecond) => {
1350                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1351            }
1352            Time64(TimeUnit::Nanosecond) => {
1353                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1354            }
1355            Timestamp(TimeUnit::Second, to_tz) => {
1356                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1357            }
1358            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1359                i64,
1360                TimestampMillisecondType,
1361            >(array, to_tz, cast_options),
1362            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1363                i64,
1364                TimestampMicrosecondType,
1365            >(array, to_tz, cast_options),
1366            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1367                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1368            }
1369            Interval(IntervalUnit::YearMonth) => {
1370                cast_string_to_year_month_interval::<i64>(array, cast_options)
1371            }
1372            Interval(IntervalUnit::DayTime) => {
1373                cast_string_to_day_time_interval::<i64>(array, cast_options)
1374            }
1375            Interval(IntervalUnit::MonthDayNano) => {
1376                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1377            }
1378            _ => Err(ArrowError::CastError(format!(
1379                "Casting from {from_type} to {to_type} not supported",
1380            ))),
1381        },
1382        (Binary, _) => match to_type {
1383            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1384            LargeUtf8 => {
1385                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1386                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1387            }
1388            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1389            FixedSizeBinary(size) => {
1390                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1391            }
1392            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1393            Utf8View => Ok(Arc::new(StringViewArray::from(
1394                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1395            ))),
1396            _ => Err(ArrowError::CastError(format!(
1397                "Casting from {from_type} to {to_type} not supported",
1398            ))),
1399        },
1400        (LargeBinary, _) => match to_type {
1401            Utf8 => {
1402                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1403                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1404            }
1405            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1406            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1407            FixedSizeBinary(size) => {
1408                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1409            }
1410            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1411            Utf8View => {
1412                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1413                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1414            }
1415            _ => Err(ArrowError::CastError(format!(
1416                "Casting from {from_type} to {to_type} not supported",
1417            ))),
1418        },
1419        (FixedSizeBinary(size), _) => match to_type {
1420            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1421            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1422            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1423            _ => Err(ArrowError::CastError(format!(
1424                "Casting from {from_type} to {to_type} not supported",
1425            ))),
1426        },
1427        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1428        (BinaryView, LargeBinary) => {
1429            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1430        }
1431        (BinaryView, Utf8) => {
1432            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1433            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1434        }
1435        (BinaryView, LargeUtf8) => {
1436            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1437            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1438        }
1439        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1440        (BinaryView, _) => Err(ArrowError::CastError(format!(
1441            "Casting from {from_type} to {to_type} not supported",
1442        ))),
1443        (from_type, Utf8View) if from_type.is_primitive() => {
1444            value_to_string_view(array, cast_options)
1445        }
1446        (from_type, LargeUtf8) if from_type.is_primitive() => {
1447            value_to_string::<i64>(array, cast_options)
1448        }
1449        (from_type, Utf8) if from_type.is_primitive() => {
1450            value_to_string::<i32>(array, cast_options)
1451        }
1452        (from_type, Binary) if from_type.is_integer() => match from_type {
1453            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1454            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1455            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1456            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1457            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1458            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1459            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1460            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1461            _ => unreachable!(),
1462        },
1463        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1464            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1465            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1466            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1467            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1468            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1469            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1470            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1471            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1472            _ => unreachable!(),
1473        },
1474        // start numeric casts
1475        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1476        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1477        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1478        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1479        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1480        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1481        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1482        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1483        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1484        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1485
1486        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1487        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1488        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1489        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1490        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1491        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1492        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1493        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1494        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1495        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1496
1497        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1498        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1499        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1500        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1501        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1502        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1503        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1504        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1505        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1506        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1507
1508        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1509        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1510        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1511        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1512        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1513        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1514        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1515        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1516        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1517        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1518
1519        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1520        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1521        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1522        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1523        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1524        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1525        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1526        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1527        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1528        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1529
1530        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1531        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1532        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1533        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1534        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1535        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1536        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1537        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1538        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1539        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1540
1541        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1542        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1543        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1544        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1545        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1546        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1547        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1548        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1549        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1550        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1551
1552        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1553        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1554        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1555        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1556        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1557        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1558        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1559        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1560        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1561        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1562
1563        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1564        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1565        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1566        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1567        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1568        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1569        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1570        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1571        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1572        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1573
1574        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1575        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1576        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1577        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1578        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1579        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1580        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1581        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1582        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1583        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1584
1585        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1586        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1587        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1588        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1589        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1590        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1591        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1592        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1593        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1594        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1595        // end numeric casts
1596
1597        // temporal casts
1598        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1599        (Int32, Date64) => cast_with_options(
1600            &cast_with_options(array, &Date32, cast_options)?,
1601            &Date64,
1602            cast_options,
1603        ),
1604        (Int32, Time32(TimeUnit::Second)) => {
1605            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1606        }
1607        (Int32, Time32(TimeUnit::Millisecond)) => {
1608            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1609        }
1610        // No support for microsecond/nanosecond with i32
1611        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1612        (Date32, Int64) => cast_with_options(
1613            &cast_with_options(array, &Int32, cast_options)?,
1614            &Int64,
1615            cast_options,
1616        ),
1617        (Time32(TimeUnit::Second), Int32) => {
1618            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1619        }
1620        (Time32(TimeUnit::Millisecond), Int32) => {
1621            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1622        }
1623        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1624            &cast_with_options(array, &Int32, cast_options)?,
1625            &Int64,
1626            cast_options,
1627        ),
1628        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1629            &cast_with_options(array, &Int32, cast_options)?,
1630            &Int64,
1631            cast_options,
1632        ),
1633        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1634        (Int64, Date32) => cast_with_options(
1635            &cast_with_options(array, &Int32, cast_options)?,
1636            &Date32,
1637            cast_options,
1638        ),
1639        // No support for second/milliseconds with i64
1640        (Int64, Time64(TimeUnit::Microsecond)) => {
1641            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1642        }
1643        (Int64, Time64(TimeUnit::Nanosecond)) => {
1644            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1645        }
1646
1647        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1648        (Date64, Int32) => cast_with_options(
1649            &cast_with_options(array, &Int64, cast_options)?,
1650            &Int32,
1651            cast_options,
1652        ),
1653        (Time64(TimeUnit::Microsecond), Int64) => {
1654            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1655        }
1656        (Time64(TimeUnit::Nanosecond), Int64) => {
1657            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1658        }
1659        (Date32, Date64) => Ok(Arc::new(
1660            array
1661                .as_primitive::<Date32Type>()
1662                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1663        )),
1664        (Date64, Date32) => Ok(Arc::new(
1665            array
1666                .as_primitive::<Date64Type>()
1667                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1668        )),
1669
1670        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1671            array
1672                .as_primitive::<Time32SecondType>()
1673                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1674        )),
1675        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1676            array
1677                .as_primitive::<Time32SecondType>()
1678                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1679        )),
1680        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1681            array
1682                .as_primitive::<Time32SecondType>()
1683                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1684        )),
1685
1686        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1687            array
1688                .as_primitive::<Time32MillisecondType>()
1689                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1690        )),
1691        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1692            array
1693                .as_primitive::<Time32MillisecondType>()
1694                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1695        )),
1696        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1697            array
1698                .as_primitive::<Time32MillisecondType>()
1699                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1700        )),
1701
1702        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1703            array
1704                .as_primitive::<Time64MicrosecondType>()
1705                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1706        )),
1707        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1708            array
1709                .as_primitive::<Time64MicrosecondType>()
1710                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1711        )),
1712        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1713            array
1714                .as_primitive::<Time64MicrosecondType>()
1715                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1716        )),
1717
1718        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1719            array
1720                .as_primitive::<Time64NanosecondType>()
1721                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1722        )),
1723        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1724            array
1725                .as_primitive::<Time64NanosecondType>()
1726                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1727        )),
1728        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1729            array
1730                .as_primitive::<Time64NanosecondType>()
1731                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1732        )),
1733
1734        // Timestamp to integer/floating/decimals
1735        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1736            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1737            cast_with_options(&array, to_type, cast_options)
1738        }
1739        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1740            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1741            cast_with_options(&array, to_type, cast_options)
1742        }
1743        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1744            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1745            cast_with_options(&array, to_type, cast_options)
1746        }
1747        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1748            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1749            cast_with_options(&array, to_type, cast_options)
1750        }
1751
1752        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1753            let array = cast_with_options(array, &Int64, cast_options)?;
1754            Ok(make_timestamp_array(
1755                array.as_primitive(),
1756                *unit,
1757                tz.clone(),
1758            ))
1759        }
1760
1761        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1762            let array = cast_with_options(array, &Int64, cast_options)?;
1763            let time_array = array.as_primitive::<Int64Type>();
1764            let from_size = time_unit_multiple(from_unit);
1765            let to_size = time_unit_multiple(to_unit);
1766            // we either divide or multiply, depending on size of each unit
1767            // units are never the same when the types are the same
1768            let converted = match from_size.cmp(&to_size) {
1769                Ordering::Greater => {
1770                    let divisor = from_size / to_size;
1771                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1772                }
1773                Ordering::Equal => time_array.clone(),
1774                Ordering::Less => {
1775                    let mul = to_size / from_size;
1776                    if cast_options.safe {
1777                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1778                    } else {
1779                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1780                    }
1781                }
1782            };
1783            // Normalize timezone
1784            let adjusted = match (from_tz, to_tz) {
1785                // Only this case needs to be adjusted because we're casting from
1786                // unknown time offset to some time offset, we want the time to be
1787                // unchanged.
1788                //
1789                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1790                (None, Some(to_tz)) => {
1791                    let to_tz: Tz = to_tz.parse()?;
1792                    match to_unit {
1793                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1794                            converted,
1795                            &to_tz,
1796                            cast_options,
1797                        )?,
1798                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1799                            TimestampMillisecondType,
1800                        >(
1801                            converted, &to_tz, cast_options
1802                        )?,
1803                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1804                            TimestampMicrosecondType,
1805                        >(
1806                            converted, &to_tz, cast_options
1807                        )?,
1808                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1809                            TimestampNanosecondType,
1810                        >(
1811                            converted, &to_tz, cast_options
1812                        )?,
1813                    }
1814                }
1815                _ => converted,
1816            };
1817            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1818        }
1819        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1820            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1821        }
1822        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1823            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1824        }
1825        (Timestamp(TimeUnit::Second, _), Date32) => {
1826            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1827        }
1828        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1829            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1830        }
1831        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1832            true => {
1833                // change error to None
1834                array
1835                    .as_primitive::<TimestampSecondType>()
1836                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1837            }
1838            false => array
1839                .as_primitive::<TimestampSecondType>()
1840                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1841        })),
1842        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1843            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1844        }
1845        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1846            array
1847                .as_primitive::<TimestampMicrosecondType>()
1848                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1849        )),
1850        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1851            array
1852                .as_primitive::<TimestampNanosecondType>()
1853                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1854        )),
1855        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1856            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1857            Ok(Arc::new(
1858                array
1859                    .as_primitive::<TimestampSecondType>()
1860                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1861                        Ok(time_to_time64us(as_time_res_with_timezone::<
1862                            TimestampSecondType,
1863                        >(x, tz)?))
1864                    })?,
1865            ))
1866        }
1867        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1868            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1869            Ok(Arc::new(
1870                array
1871                    .as_primitive::<TimestampSecondType>()
1872                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1873                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1874                            TimestampSecondType,
1875                        >(x, tz)?))
1876                    })?,
1877            ))
1878        }
1879        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1880            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1881            Ok(Arc::new(
1882                array
1883                    .as_primitive::<TimestampMillisecondType>()
1884                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1885                        Ok(time_to_time64us(as_time_res_with_timezone::<
1886                            TimestampMillisecondType,
1887                        >(x, tz)?))
1888                    })?,
1889            ))
1890        }
1891        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1892            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1893            Ok(Arc::new(
1894                array
1895                    .as_primitive::<TimestampMillisecondType>()
1896                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1897                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1898                            TimestampMillisecondType,
1899                        >(x, tz)?))
1900                    })?,
1901            ))
1902        }
1903        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1904            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1905            Ok(Arc::new(
1906                array
1907                    .as_primitive::<TimestampMicrosecondType>()
1908                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1909                        Ok(time_to_time64us(as_time_res_with_timezone::<
1910                            TimestampMicrosecondType,
1911                        >(x, tz)?))
1912                    })?,
1913            ))
1914        }
1915        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1916            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1917            Ok(Arc::new(
1918                array
1919                    .as_primitive::<TimestampMicrosecondType>()
1920                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1921                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1922                            TimestampMicrosecondType,
1923                        >(x, tz)?))
1924                    })?,
1925            ))
1926        }
1927        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1928            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1929            Ok(Arc::new(
1930                array
1931                    .as_primitive::<TimestampNanosecondType>()
1932                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1933                        Ok(time_to_time64us(as_time_res_with_timezone::<
1934                            TimestampNanosecondType,
1935                        >(x, tz)?))
1936                    })?,
1937            ))
1938        }
1939        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1940            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1941            Ok(Arc::new(
1942                array
1943                    .as_primitive::<TimestampNanosecondType>()
1944                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1945                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1946                            TimestampNanosecondType,
1947                        >(x, tz)?))
1948                    })?,
1949            ))
1950        }
1951        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1952            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1953            Ok(Arc::new(
1954                array
1955                    .as_primitive::<TimestampSecondType>()
1956                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1957                        Ok(time_to_time32s(as_time_res_with_timezone::<
1958                            TimestampSecondType,
1959                        >(x, tz)?))
1960                    })?,
1961            ))
1962        }
1963        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1964            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1965            Ok(Arc::new(
1966                array
1967                    .as_primitive::<TimestampSecondType>()
1968                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1969                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1970                            TimestampSecondType,
1971                        >(x, tz)?))
1972                    })?,
1973            ))
1974        }
1975        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1976            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1977            Ok(Arc::new(
1978                array
1979                    .as_primitive::<TimestampMillisecondType>()
1980                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1981                        Ok(time_to_time32s(as_time_res_with_timezone::<
1982                            TimestampMillisecondType,
1983                        >(x, tz)?))
1984                    })?,
1985            ))
1986        }
1987        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
1988            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1989            Ok(Arc::new(
1990                array
1991                    .as_primitive::<TimestampMillisecondType>()
1992                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1993                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1994                            TimestampMillisecondType,
1995                        >(x, tz)?))
1996                    })?,
1997            ))
1998        }
1999        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2000            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2001            Ok(Arc::new(
2002                array
2003                    .as_primitive::<TimestampMicrosecondType>()
2004                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2005                        Ok(time_to_time32s(as_time_res_with_timezone::<
2006                            TimestampMicrosecondType,
2007                        >(x, tz)?))
2008                    })?,
2009            ))
2010        }
2011        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2012            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2013            Ok(Arc::new(
2014                array
2015                    .as_primitive::<TimestampMicrosecondType>()
2016                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2017                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2018                            TimestampMicrosecondType,
2019                        >(x, tz)?))
2020                    })?,
2021            ))
2022        }
2023        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2024            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2025            Ok(Arc::new(
2026                array
2027                    .as_primitive::<TimestampNanosecondType>()
2028                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2029                        Ok(time_to_time32s(as_time_res_with_timezone::<
2030                            TimestampNanosecondType,
2031                        >(x, tz)?))
2032                    })?,
2033            ))
2034        }
2035        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2036            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2037            Ok(Arc::new(
2038                array
2039                    .as_primitive::<TimestampNanosecondType>()
2040                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2041                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2042                            TimestampNanosecondType,
2043                        >(x, tz)?))
2044                    })?,
2045            ))
2046        }
2047        (Date64, Timestamp(TimeUnit::Second, _)) => {
2048            let array = array
2049                .as_primitive::<Date64Type>()
2050                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2051
2052            cast_with_options(&array, to_type, cast_options)
2053        }
2054        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2055            let array = array
2056                .as_primitive::<Date64Type>()
2057                .reinterpret_cast::<TimestampMillisecondType>();
2058
2059            cast_with_options(&array, to_type, cast_options)
2060        }
2061
2062        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2063            let array = array
2064                .as_primitive::<Date64Type>()
2065                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2066
2067            cast_with_options(&array, to_type, cast_options)
2068        }
2069        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2070            let array = array
2071                .as_primitive::<Date64Type>()
2072                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2073
2074            cast_with_options(&array, to_type, cast_options)
2075        }
2076        (Date32, Timestamp(TimeUnit::Second, _)) => {
2077            let array = array
2078                .as_primitive::<Date32Type>()
2079                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2080
2081            cast_with_options(&array, to_type, cast_options)
2082        }
2083        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2084            let array = array
2085                .as_primitive::<Date32Type>()
2086                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2087
2088            cast_with_options(&array, to_type, cast_options)
2089        }
2090        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2091            let array = array
2092                .as_primitive::<Date32Type>()
2093                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2094
2095            cast_with_options(&array, to_type, cast_options)
2096        }
2097        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2098            let array = array
2099                .as_primitive::<Date32Type>()
2100                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2101
2102            cast_with_options(&array, to_type, cast_options)
2103        }
2104
2105        (_, Duration(unit)) if from_type.is_numeric() => {
2106            let array = cast_with_options(array, &Int64, cast_options)?;
2107            Ok(make_duration_array(array.as_primitive(), *unit))
2108        }
2109        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2110            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2111            cast_with_options(&array, to_type, cast_options)
2112        }
2113        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2114            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2115            cast_with_options(&array, to_type, cast_options)
2116        }
2117        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2118            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2119            cast_with_options(&array, to_type, cast_options)
2120        }
2121        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2122            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2123            cast_with_options(&array, to_type, cast_options)
2124        }
2125
2126        (Duration(from_unit), Duration(to_unit)) => {
2127            let array = cast_with_options(array, &Int64, cast_options)?;
2128            let time_array = array.as_primitive::<Int64Type>();
2129            let from_size = time_unit_multiple(from_unit);
2130            let to_size = time_unit_multiple(to_unit);
2131            // we either divide or multiply, depending on size of each unit
2132            // units are never the same when the types are the same
2133            let converted = match from_size.cmp(&to_size) {
2134                Ordering::Greater => {
2135                    let divisor = from_size / to_size;
2136                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2137                }
2138                Ordering::Equal => time_array.clone(),
2139                Ordering::Less => {
2140                    let mul = to_size / from_size;
2141                    if cast_options.safe {
2142                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2143                    } else {
2144                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2145                    }
2146                }
2147            };
2148            Ok(make_duration_array(&converted, *to_unit))
2149        }
2150
2151        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2152            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2153        }
2154        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2155            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2156        }
2157        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2158            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2159        }
2160        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2161            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2162        }
2163        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2164            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2165        }
2166        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2167            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2168        }
2169        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2170            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2171        }
2172        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2173            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2174        }
2175        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2176            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2177        }
2178        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2179            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2180        }
2181        (Int32, Interval(IntervalUnit::YearMonth)) => {
2182            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2183        }
2184        (_, _) => Err(ArrowError::CastError(format!(
2185            "Casting from {from_type} to {to_type} not supported",
2186        ))),
2187    }
2188}
2189
2190fn cast_struct_to_struct(
2191    array: &StructArray,
2192    from_fields: Fields,
2193    to_fields: Fields,
2194    cast_options: &CastOptions,
2195) -> Result<ArrayRef, ArrowError> {
2196    // Fast path: if field names are in the same order, we can just zip and cast
2197    let fields_match_order = from_fields.len() == to_fields.len()
2198        && from_fields
2199            .iter()
2200            .zip(to_fields.iter())
2201            .all(|(f1, f2)| f1.name() == f2.name());
2202
2203    let fields = if fields_match_order {
2204        // Fast path: cast columns in order if their names match
2205        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2206    } else {
2207        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2208            from_fields
2209                .iter()
2210                .any(|from_field| from_field.name() == to_field.name())
2211        });
2212
2213        if all_fields_match_by_name {
2214            // Slow path: match fields by name and reorder
2215            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2216        } else {
2217            // Fallback: cast field by field in order
2218            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2219        }
2220    };
2221
2222    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2223    Ok(Arc::new(array) as ArrayRef)
2224}
2225
2226fn cast_struct_fields_by_name(
2227    array: &StructArray,
2228    from_fields: Fields,
2229    to_fields: Fields,
2230    cast_options: &CastOptions,
2231) -> Result<Vec<ArrayRef>, ArrowError> {
2232    to_fields
2233        .iter()
2234        .map(|to_field| {
2235            let from_field_idx = from_fields
2236                .iter()
2237                .position(|from_field| from_field.name() == to_field.name())
2238                .unwrap(); // safe because we checked above
2239            let column = array.column(from_field_idx);
2240            cast_with_options(column, to_field.data_type(), cast_options)
2241        })
2242        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2243}
2244
2245fn cast_struct_fields_in_order(
2246    array: &StructArray,
2247    to_fields: Fields,
2248    cast_options: &CastOptions,
2249) -> Result<Vec<ArrayRef>, ArrowError> {
2250    array
2251        .columns()
2252        .iter()
2253        .zip(to_fields.iter())
2254        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2255        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2256}
2257
2258fn cast_from_decimal<D, F>(
2259    array: &dyn Array,
2260    base: D::Native,
2261    scale: &i8,
2262    from_type: &DataType,
2263    to_type: &DataType,
2264    as_float: F,
2265    cast_options: &CastOptions,
2266) -> Result<ArrayRef, ArrowError>
2267where
2268    D: DecimalType + ArrowPrimitiveType,
2269    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2270    F: Fn(D::Native) -> f64,
2271{
2272    use DataType::*;
2273    // cast decimal to other type
2274    match to_type {
2275        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2276        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2277        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2278        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2279        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2280        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2281        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2282        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2283        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2284            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2285        }),
2286        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2287            as_float(x) / 10_f64.powi(*scale as i32)
2288        }),
2289        Utf8View => value_to_string_view(array, cast_options),
2290        Utf8 => value_to_string::<i32>(array, cast_options),
2291        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2292        Null => Ok(new_null_array(to_type, array.len())),
2293        _ => Err(ArrowError::CastError(format!(
2294            "Casting from {from_type} to {to_type} not supported"
2295        ))),
2296    }
2297}
2298
2299fn cast_to_decimal<D, M>(
2300    array: &dyn Array,
2301    base: M,
2302    precision: &u8,
2303    scale: &i8,
2304    from_type: &DataType,
2305    to_type: &DataType,
2306    cast_options: &CastOptions,
2307) -> Result<ArrayRef, ArrowError>
2308where
2309    D: DecimalType + ArrowPrimitiveType<Native = M>,
2310    M: ArrowNativeTypeOp + DecimalCast,
2311    u8: num_traits::AsPrimitive<M>,
2312    u16: num_traits::AsPrimitive<M>,
2313    u32: num_traits::AsPrimitive<M>,
2314    u64: num_traits::AsPrimitive<M>,
2315    i8: num_traits::AsPrimitive<M>,
2316    i16: num_traits::AsPrimitive<M>,
2317    i32: num_traits::AsPrimitive<M>,
2318    i64: num_traits::AsPrimitive<M>,
2319{
2320    use DataType::*;
2321    // cast data to decimal
2322    match from_type {
2323        UInt8 => cast_integer_to_decimal::<_, D, M>(
2324            array.as_primitive::<UInt8Type>(),
2325            *precision,
2326            *scale,
2327            base,
2328            cast_options,
2329        ),
2330        UInt16 => cast_integer_to_decimal::<_, D, _>(
2331            array.as_primitive::<UInt16Type>(),
2332            *precision,
2333            *scale,
2334            base,
2335            cast_options,
2336        ),
2337        UInt32 => cast_integer_to_decimal::<_, D, _>(
2338            array.as_primitive::<UInt32Type>(),
2339            *precision,
2340            *scale,
2341            base,
2342            cast_options,
2343        ),
2344        UInt64 => cast_integer_to_decimal::<_, D, _>(
2345            array.as_primitive::<UInt64Type>(),
2346            *precision,
2347            *scale,
2348            base,
2349            cast_options,
2350        ),
2351        Int8 => cast_integer_to_decimal::<_, D, _>(
2352            array.as_primitive::<Int8Type>(),
2353            *precision,
2354            *scale,
2355            base,
2356            cast_options,
2357        ),
2358        Int16 => cast_integer_to_decimal::<_, D, _>(
2359            array.as_primitive::<Int16Type>(),
2360            *precision,
2361            *scale,
2362            base,
2363            cast_options,
2364        ),
2365        Int32 => cast_integer_to_decimal::<_, D, _>(
2366            array.as_primitive::<Int32Type>(),
2367            *precision,
2368            *scale,
2369            base,
2370            cast_options,
2371        ),
2372        Int64 => cast_integer_to_decimal::<_, D, _>(
2373            array.as_primitive::<Int64Type>(),
2374            *precision,
2375            *scale,
2376            base,
2377            cast_options,
2378        ),
2379        Float32 => cast_floating_point_to_decimal::<_, D>(
2380            array.as_primitive::<Float32Type>(),
2381            *precision,
2382            *scale,
2383            cast_options,
2384        ),
2385        Float64 => cast_floating_point_to_decimal::<_, D>(
2386            array.as_primitive::<Float64Type>(),
2387            *precision,
2388            *scale,
2389            cast_options,
2390        ),
2391        Utf8View | Utf8 => {
2392            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2393        }
2394        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2395        Null => Ok(new_null_array(to_type, array.len())),
2396        _ => Err(ArrowError::CastError(format!(
2397            "Casting from {from_type} to {to_type} not supported"
2398        ))),
2399    }
2400}
2401
2402/// Get the time unit as a multiple of a second
2403const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2404    match unit {
2405        TimeUnit::Second => 1,
2406        TimeUnit::Millisecond => MILLISECONDS,
2407        TimeUnit::Microsecond => MICROSECONDS,
2408        TimeUnit::Nanosecond => NANOSECONDS,
2409    }
2410}
2411
2412/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2413fn cast_numeric_arrays<FROM, TO>(
2414    from: &dyn Array,
2415    cast_options: &CastOptions,
2416) -> Result<ArrayRef, ArrowError>
2417where
2418    FROM: ArrowPrimitiveType,
2419    TO: ArrowPrimitiveType,
2420    FROM::Native: NumCast,
2421    TO::Native: NumCast,
2422{
2423    if cast_options.safe {
2424        // If the value can't be casted to the `TO::Native`, return null
2425        Ok(Arc::new(numeric_cast::<FROM, TO>(
2426            from.as_primitive::<FROM>(),
2427        )))
2428    } else {
2429        // If the value can't be casted to the `TO::Native`, return error
2430        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2431            from.as_primitive::<FROM>(),
2432        )?))
2433    }
2434}
2435
2436// Natural cast between numeric types
2437// If the value of T can't be casted to R, will throw error
2438fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2439where
2440    T: ArrowPrimitiveType,
2441    R: ArrowPrimitiveType,
2442    T::Native: NumCast,
2443    R::Native: NumCast,
2444{
2445    from.try_unary(|value| {
2446        num_traits::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2447            ArrowError::CastError(format!(
2448                "Can't cast value {:?} to type {}",
2449                value,
2450                R::DATA_TYPE
2451            ))
2452        })
2453    })
2454}
2455
2456// Natural cast between numeric types
2457// If the value of T can't be casted to R, it will be converted to null
2458fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2459where
2460    T: ArrowPrimitiveType,
2461    R: ArrowPrimitiveType,
2462    T::Native: NumCast,
2463    R::Native: NumCast,
2464{
2465    from.unary_opt::<_, R>(num_traits::cast::cast::<T::Native, R::Native>)
2466}
2467
2468fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2469    array: &dyn Array,
2470) -> Result<ArrayRef, ArrowError> {
2471    let array = array.as_primitive::<FROM>();
2472    let size = std::mem::size_of::<FROM::Native>();
2473    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2474    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2475        offsets,
2476        array.values().inner().clone(),
2477        array.nulls().cloned(),
2478    )?))
2479}
2480
2481fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2482    array: PrimitiveArray<Int64Type>,
2483    to_tz: &Tz,
2484    cast_options: &CastOptions,
2485) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2486    let adjust = |o| {
2487        let local = as_datetime::<T>(o)?;
2488        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2489        T::make_value(local - offset.fix())
2490    };
2491    let adjusted = if cast_options.safe {
2492        array.unary_opt::<_, Int64Type>(adjust)
2493    } else {
2494        array.try_unary::<_, Int64Type, _>(|o| {
2495            adjust(o).ok_or_else(|| {
2496                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2497            })
2498        })?
2499    };
2500    Ok(adjusted)
2501}
2502
2503/// Cast numeric types to Boolean
2504///
2505/// Any zero value returns `false` while non-zero returns `true`
2506fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2507where
2508    FROM: ArrowPrimitiveType,
2509{
2510    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2511}
2512
2513fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2514where
2515    T: ArrowPrimitiveType + ArrowPrimitiveType,
2516{
2517    let mut b = BooleanBuilder::with_capacity(from.len());
2518
2519    for i in 0..from.len() {
2520        if from.is_null(i) {
2521            b.append_null();
2522        } else if from.value(i) != T::default_value() {
2523            b.append_value(true);
2524        } else {
2525            b.append_value(false);
2526        }
2527    }
2528
2529    Ok(b.finish())
2530}
2531
2532/// Cast Boolean types to numeric
2533///
2534/// `false` returns 0 while `true` returns 1
2535fn cast_bool_to_numeric<TO>(
2536    from: &dyn Array,
2537    cast_options: &CastOptions,
2538) -> Result<ArrayRef, ArrowError>
2539where
2540    TO: ArrowPrimitiveType,
2541    TO::Native: num_traits::cast::NumCast,
2542{
2543    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2544        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2545        cast_options,
2546    )))
2547}
2548
2549fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2550where
2551    T: ArrowPrimitiveType,
2552    T::Native: num_traits::NumCast,
2553{
2554    let iter = (0..from.len()).map(|i| {
2555        if from.is_null(i) {
2556            None
2557        } else if from.value(i) {
2558            // a workaround to cast a primitive to T::Native, infallible
2559            num_traits::cast::cast(1)
2560        } else {
2561            Some(T::default_value())
2562        }
2563    });
2564    // Benefit:
2565    //     20% performance improvement
2566    // Soundness:
2567    //     The iterator is trustedLen because it comes from a Range
2568    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2569}
2570
2571/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2572fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2573    array: &dyn Array,
2574    byte_width: i32,
2575    cast_options: &CastOptions,
2576) -> Result<ArrayRef, ArrowError> {
2577    let array = array.as_binary::<O>();
2578    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2579
2580    for i in 0..array.len() {
2581        if array.is_null(i) {
2582            builder.append_null();
2583        } else {
2584            match builder.append_value(array.value(i)) {
2585                Ok(_) => {}
2586                Err(e) => match cast_options.safe {
2587                    true => builder.append_null(),
2588                    false => return Err(e),
2589                },
2590            }
2591        }
2592    }
2593
2594    Ok(Arc::new(builder.finish()))
2595}
2596
2597/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2598/// If the target one is too large for the source array it will return an Error.
2599fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2600    array: &dyn Array,
2601    byte_width: i32,
2602) -> Result<ArrayRef, ArrowError> {
2603    let array = array
2604        .as_any()
2605        .downcast_ref::<FixedSizeBinaryArray>()
2606        .unwrap();
2607
2608    let offsets: i128 = byte_width as i128 * array.len() as i128;
2609
2610    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2611    if is_binary && offsets > i32::MAX as i128 {
2612        return Err(ArrowError::ComputeError(
2613            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2614        ));
2615    } else if !is_binary && offsets > i64::MAX as i128 {
2616        return Err(ArrowError::ComputeError(
2617            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2618        ));
2619    }
2620
2621    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2622
2623    for i in 0..array.len() {
2624        if array.is_null(i) {
2625            builder.append_null();
2626        } else {
2627            builder.append_value(array.value(i));
2628        }
2629    }
2630
2631    Ok(Arc::new(builder.finish()))
2632}
2633
2634fn cast_fixed_size_binary_to_binary_view(
2635    array: &dyn Array,
2636    _byte_width: i32,
2637) -> Result<ArrayRef, ArrowError> {
2638    let array = array
2639        .as_any()
2640        .downcast_ref::<FixedSizeBinaryArray>()
2641        .unwrap();
2642
2643    let mut builder = BinaryViewBuilder::with_capacity(array.len());
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
2655/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2656/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2657fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2658where
2659    FROM: ByteArrayType,
2660    TO: ByteArrayType<Native = FROM::Native>,
2661    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2662    TO::Offset: OffsetSizeTrait + NumCast,
2663{
2664    let data = array.to_data();
2665    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2666    let str_values_buf = data.buffers()[1].clone();
2667    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2668
2669    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2670    offsets
2671        .iter()
2672        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2673            let offset =
2674                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2675                    ArrowError::ComputeError(format!(
2676                        "{}{} array too large to cast to {}{} array",
2677                        FROM::Offset::PREFIX,
2678                        FROM::PREFIX,
2679                        TO::Offset::PREFIX,
2680                        TO::PREFIX
2681                    ))
2682                })?;
2683            offset_builder.append(offset);
2684            Ok(())
2685        })?;
2686
2687    let offset_buffer = offset_builder.finish();
2688
2689    let dtype = TO::DATA_TYPE;
2690
2691    let builder = ArrayData::builder(dtype)
2692        .offset(array.offset())
2693        .len(array.len())
2694        .add_buffer(offset_buffer)
2695        .add_buffer(str_values_buf)
2696        .nulls(data.nulls().cloned());
2697
2698    let array_data = unsafe { builder.build_unchecked() };
2699
2700    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2701}
2702
2703/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2704fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2705where
2706    FROM: ByteViewType,
2707    TO: ByteArrayType,
2708    FROM::Native: AsRef<TO::Native>,
2709{
2710    let data = array.to_data();
2711    let view_array = GenericByteViewArray::<FROM>::from(data);
2712
2713    let len = view_array.len();
2714    let bytes = view_array
2715        .views()
2716        .iter()
2717        .map(|v| ByteView::from(*v).length as usize)
2718        .sum::<usize>();
2719
2720    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2721
2722    for val in view_array.iter() {
2723        byte_array_builder.append_option(val);
2724    }
2725
2726    Ok(Arc::new(byte_array_builder.finish()))
2727}
2728
2729#[cfg(test)]
2730mod tests {
2731    use super::*;
2732    use DataType::*;
2733    use arrow_array::{Int64Array, RunArray, StringArray};
2734    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2735    use arrow_buffer::{ScalarBuffer, i256};
2736    use arrow_schema::{DataType, Field};
2737    use chrono::NaiveDate;
2738    use half::f16;
2739    use std::sync::Arc;
2740
2741    #[derive(Clone)]
2742    struct DecimalCastTestConfig {
2743        input_prec: u8,
2744        input_scale: i8,
2745        input_repr: i128,
2746        output_prec: u8,
2747        output_scale: i8,
2748        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2749                                                    // template where the "{}" will be
2750                                                    // replaced with the decimal type name
2751                                                    // (e.g. Decimal128)
2752    }
2753
2754    macro_rules! generate_cast_test_case {
2755        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2756            let output =
2757                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2758
2759            // assert cast type
2760            let input_array_type = $INPUT_ARRAY.data_type();
2761            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2762            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2763            assert_eq!($OUTPUT_TYPE, result.data_type());
2764            assert_eq!(result.as_ref(), &output);
2765
2766            let cast_option = CastOptions {
2767                safe: false,
2768                format_options: FormatOptions::default(),
2769            };
2770            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2771            assert_eq!($OUTPUT_TYPE, result.data_type());
2772            assert_eq!(result.as_ref(), &output);
2773        };
2774    }
2775
2776    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2777    where
2778        I: DecimalType,
2779        O: DecimalType,
2780        I::Native: DecimalCast,
2781        O::Native: DecimalCast,
2782    {
2783        let array = vec![I::Native::from_decimal(t.input_repr)];
2784        let array = array
2785            .into_iter()
2786            .collect::<PrimitiveArray<I>>()
2787            .with_precision_and_scale(t.input_prec, t.input_scale)
2788            .unwrap();
2789        let input_type = array.data_type();
2790        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2791        assert!(can_cast_types(input_type, &output_type));
2792
2793        let options = CastOptions {
2794            safe: false,
2795            ..Default::default()
2796        };
2797        let result = cast_with_options(&array, &output_type, &options);
2798
2799        match t.expected_output_repr {
2800            Ok(v) => {
2801                let expected_array = vec![O::Native::from_decimal(v)];
2802                let expected_array = expected_array
2803                    .into_iter()
2804                    .collect::<PrimitiveArray<O>>()
2805                    .with_precision_and_scale(t.output_prec, t.output_scale)
2806                    .unwrap();
2807                assert_eq!(*result.unwrap(), expected_array);
2808            }
2809            Err(expected_output_message_template) => {
2810                assert!(result.is_err());
2811                let expected_error_message =
2812                    expected_output_message_template.replace("{}", O::PREFIX);
2813                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2814            }
2815        }
2816    }
2817
2818    fn create_decimal32_array(
2819        array: Vec<Option<i32>>,
2820        precision: u8,
2821        scale: i8,
2822    ) -> Result<Decimal32Array, ArrowError> {
2823        array
2824            .into_iter()
2825            .collect::<Decimal32Array>()
2826            .with_precision_and_scale(precision, scale)
2827    }
2828
2829    fn create_decimal64_array(
2830        array: Vec<Option<i64>>,
2831        precision: u8,
2832        scale: i8,
2833    ) -> Result<Decimal64Array, ArrowError> {
2834        array
2835            .into_iter()
2836            .collect::<Decimal64Array>()
2837            .with_precision_and_scale(precision, scale)
2838    }
2839
2840    fn create_decimal128_array(
2841        array: Vec<Option<i128>>,
2842        precision: u8,
2843        scale: i8,
2844    ) -> Result<Decimal128Array, ArrowError> {
2845        array
2846            .into_iter()
2847            .collect::<Decimal128Array>()
2848            .with_precision_and_scale(precision, scale)
2849    }
2850
2851    fn create_decimal256_array(
2852        array: Vec<Option<i256>>,
2853        precision: u8,
2854        scale: i8,
2855    ) -> Result<Decimal256Array, ArrowError> {
2856        array
2857            .into_iter()
2858            .collect::<Decimal256Array>()
2859            .with_precision_and_scale(precision, scale)
2860    }
2861
2862    #[test]
2863    #[cfg(not(feature = "force_validate"))]
2864    #[should_panic(
2865        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2866    )]
2867    fn test_cast_decimal_to_decimal_round_with_error() {
2868        // decimal256 to decimal128 overflow
2869        let array = vec![
2870            Some(i256::from_i128(1123454)),
2871            Some(i256::from_i128(2123456)),
2872            Some(i256::from_i128(-3123453)),
2873            Some(i256::from_i128(-3123456)),
2874            None,
2875            Some(i256::MAX),
2876            Some(i256::MIN),
2877        ];
2878        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2879        let array = Arc::new(input_decimal_array) as ArrayRef;
2880        let input_type = DataType::Decimal256(76, 4);
2881        let output_type = DataType::Decimal128(20, 3);
2882        assert!(can_cast_types(&input_type, &output_type));
2883        generate_cast_test_case!(
2884            &array,
2885            Decimal128Array,
2886            &output_type,
2887            vec![
2888                Some(112345_i128),
2889                Some(212346_i128),
2890                Some(-312345_i128),
2891                Some(-312346_i128),
2892                None,
2893                None,
2894                None,
2895            ]
2896        );
2897    }
2898
2899    #[test]
2900    #[cfg(not(feature = "force_validate"))]
2901    fn test_cast_decimal_to_decimal_round() {
2902        let array = vec![
2903            Some(1123454),
2904            Some(2123456),
2905            Some(-3123453),
2906            Some(-3123456),
2907            None,
2908        ];
2909        let array = create_decimal128_array(array, 20, 4).unwrap();
2910        // decimal128 to decimal128
2911        let input_type = DataType::Decimal128(20, 4);
2912        let output_type = DataType::Decimal128(20, 3);
2913        assert!(can_cast_types(&input_type, &output_type));
2914        generate_cast_test_case!(
2915            &array,
2916            Decimal128Array,
2917            &output_type,
2918            vec![
2919                Some(112345_i128),
2920                Some(212346_i128),
2921                Some(-312345_i128),
2922                Some(-312346_i128),
2923                None
2924            ]
2925        );
2926
2927        // decimal128 to decimal256
2928        let input_type = DataType::Decimal128(20, 4);
2929        let output_type = DataType::Decimal256(20, 3);
2930        assert!(can_cast_types(&input_type, &output_type));
2931        generate_cast_test_case!(
2932            &array,
2933            Decimal256Array,
2934            &output_type,
2935            vec![
2936                Some(i256::from_i128(112345_i128)),
2937                Some(i256::from_i128(212346_i128)),
2938                Some(i256::from_i128(-312345_i128)),
2939                Some(i256::from_i128(-312346_i128)),
2940                None
2941            ]
2942        );
2943
2944        // decimal256
2945        let array = vec![
2946            Some(i256::from_i128(1123454)),
2947            Some(i256::from_i128(2123456)),
2948            Some(i256::from_i128(-3123453)),
2949            Some(i256::from_i128(-3123456)),
2950            None,
2951        ];
2952        let array = create_decimal256_array(array, 20, 4).unwrap();
2953
2954        // decimal256 to decimal256
2955        let input_type = DataType::Decimal256(20, 4);
2956        let output_type = DataType::Decimal256(20, 3);
2957        assert!(can_cast_types(&input_type, &output_type));
2958        generate_cast_test_case!(
2959            &array,
2960            Decimal256Array,
2961            &output_type,
2962            vec![
2963                Some(i256::from_i128(112345_i128)),
2964                Some(i256::from_i128(212346_i128)),
2965                Some(i256::from_i128(-312345_i128)),
2966                Some(i256::from_i128(-312346_i128)),
2967                None
2968            ]
2969        );
2970        // decimal256 to decimal128
2971        let input_type = DataType::Decimal256(20, 4);
2972        let output_type = DataType::Decimal128(20, 3);
2973        assert!(can_cast_types(&input_type, &output_type));
2974        generate_cast_test_case!(
2975            &array,
2976            Decimal128Array,
2977            &output_type,
2978            vec![
2979                Some(112345_i128),
2980                Some(212346_i128),
2981                Some(-312345_i128),
2982                Some(-312346_i128),
2983                None
2984            ]
2985        );
2986    }
2987
2988    #[test]
2989    fn test_cast_decimal32_to_decimal32() {
2990        // test changing precision
2991        let input_type = DataType::Decimal32(9, 3);
2992        let output_type = DataType::Decimal32(9, 4);
2993        assert!(can_cast_types(&input_type, &output_type));
2994        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2995        let array = create_decimal32_array(array, 9, 3).unwrap();
2996        generate_cast_test_case!(
2997            &array,
2998            Decimal32Array,
2999            &output_type,
3000            vec![
3001                Some(11234560_i32),
3002                Some(21234560_i32),
3003                Some(31234560_i32),
3004                None
3005            ]
3006        );
3007        // negative test
3008        let array = vec![Some(123456), None];
3009        let array = create_decimal32_array(array, 9, 0).unwrap();
3010        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3011        assert!(result_safe.is_ok());
3012        let options = CastOptions {
3013            safe: false,
3014            ..Default::default()
3015        };
3016
3017        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3018        assert_eq!(
3019            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3020            result_unsafe.unwrap_err().to_string()
3021        );
3022    }
3023
3024    #[test]
3025    fn test_cast_decimal64_to_decimal64() {
3026        // test changing precision
3027        let input_type = DataType::Decimal64(17, 3);
3028        let output_type = DataType::Decimal64(17, 4);
3029        assert!(can_cast_types(&input_type, &output_type));
3030        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3031        let array = create_decimal64_array(array, 17, 3).unwrap();
3032        generate_cast_test_case!(
3033            &array,
3034            Decimal64Array,
3035            &output_type,
3036            vec![
3037                Some(11234560_i64),
3038                Some(21234560_i64),
3039                Some(31234560_i64),
3040                None
3041            ]
3042        );
3043        // negative test
3044        let array = vec![Some(123456), None];
3045        let array = create_decimal64_array(array, 9, 0).unwrap();
3046        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3047        assert!(result_safe.is_ok());
3048        let options = CastOptions {
3049            safe: false,
3050            ..Default::default()
3051        };
3052
3053        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3054        assert_eq!(
3055            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3056            result_unsafe.unwrap_err().to_string()
3057        );
3058    }
3059
3060    #[test]
3061    fn test_cast_decimal128_to_decimal128() {
3062        // test changing precision
3063        let input_type = DataType::Decimal128(20, 3);
3064        let output_type = DataType::Decimal128(20, 4);
3065        assert!(can_cast_types(&input_type, &output_type));
3066        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3067        let array = create_decimal128_array(array, 20, 3).unwrap();
3068        generate_cast_test_case!(
3069            &array,
3070            Decimal128Array,
3071            &output_type,
3072            vec![
3073                Some(11234560_i128),
3074                Some(21234560_i128),
3075                Some(31234560_i128),
3076                None
3077            ]
3078        );
3079        // negative test
3080        let array = vec![Some(123456), None];
3081        let array = create_decimal128_array(array, 10, 0).unwrap();
3082        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3083        assert!(result_safe.is_ok());
3084        let options = CastOptions {
3085            safe: false,
3086            ..Default::default()
3087        };
3088
3089        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3090        assert_eq!(
3091            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3092            result_unsafe.unwrap_err().to_string()
3093        );
3094    }
3095
3096    #[test]
3097    fn test_cast_decimal32_to_decimal32_dict() {
3098        let p = 9;
3099        let s = 3;
3100        let input_type = DataType::Decimal32(p, s);
3101        let output_type = DataType::Dictionary(
3102            Box::new(DataType::Int32),
3103            Box::new(DataType::Decimal32(p, s)),
3104        );
3105        assert!(can_cast_types(&input_type, &output_type));
3106        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3107        let array = create_decimal32_array(array, p, s).unwrap();
3108        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3109        assert_eq!(cast_array.data_type(), &output_type);
3110    }
3111
3112    #[test]
3113    fn test_cast_decimal64_to_decimal64_dict() {
3114        let p = 15;
3115        let s = 3;
3116        let input_type = DataType::Decimal64(p, s);
3117        let output_type = DataType::Dictionary(
3118            Box::new(DataType::Int32),
3119            Box::new(DataType::Decimal64(p, s)),
3120        );
3121        assert!(can_cast_types(&input_type, &output_type));
3122        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3123        let array = create_decimal64_array(array, p, s).unwrap();
3124        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3125        assert_eq!(cast_array.data_type(), &output_type);
3126    }
3127
3128    #[test]
3129    fn test_cast_decimal128_to_decimal128_dict() {
3130        let p = 20;
3131        let s = 3;
3132        let input_type = DataType::Decimal128(p, s);
3133        let output_type = DataType::Dictionary(
3134            Box::new(DataType::Int32),
3135            Box::new(DataType::Decimal128(p, s)),
3136        );
3137        assert!(can_cast_types(&input_type, &output_type));
3138        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3139        let array = create_decimal128_array(array, p, s).unwrap();
3140        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3141        assert_eq!(cast_array.data_type(), &output_type);
3142    }
3143
3144    #[test]
3145    fn test_cast_decimal256_to_decimal256_dict() {
3146        let p = 20;
3147        let s = 3;
3148        let input_type = DataType::Decimal256(p, s);
3149        let output_type = DataType::Dictionary(
3150            Box::new(DataType::Int32),
3151            Box::new(DataType::Decimal256(p, s)),
3152        );
3153        assert!(can_cast_types(&input_type, &output_type));
3154        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3155        let array = create_decimal128_array(array, p, s).unwrap();
3156        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3157        assert_eq!(cast_array.data_type(), &output_type);
3158    }
3159
3160    #[test]
3161    fn test_cast_decimal32_to_decimal32_overflow() {
3162        let input_type = DataType::Decimal32(9, 3);
3163        let output_type = DataType::Decimal32(9, 9);
3164        assert!(can_cast_types(&input_type, &output_type));
3165
3166        let array = vec![Some(i32::MAX)];
3167        let array = create_decimal32_array(array, 9, 3).unwrap();
3168        let result = cast_with_options(
3169            &array,
3170            &output_type,
3171            &CastOptions {
3172                safe: false,
3173                format_options: FormatOptions::default(),
3174            },
3175        );
3176        assert_eq!(
3177            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3178            result.unwrap_err().to_string()
3179        );
3180    }
3181
3182    #[test]
3183    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3184        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3185        let array = create_decimal32_array(array, 9, 3).unwrap();
3186
3187        // Divide out all digits of precision -- rounding could still produce +/- 1
3188        let output_type = DataType::Decimal32(9, -6);
3189        assert!(can_cast_types(array.data_type(), &output_type));
3190        generate_cast_test_case!(
3191            &array,
3192            Decimal32Array,
3193            &output_type,
3194            vec![Some(-1), Some(0), Some(1), None]
3195        );
3196
3197        // Divide out more digits than we have precision -- all-zero result
3198        let output_type = DataType::Decimal32(9, -7);
3199        assert!(can_cast_types(array.data_type(), &output_type));
3200        generate_cast_test_case!(
3201            &array,
3202            Decimal32Array,
3203            &output_type,
3204            vec![Some(0), Some(0), Some(0), None]
3205        );
3206    }
3207
3208    #[test]
3209    fn test_cast_decimal64_to_decimal64_overflow() {
3210        let input_type = DataType::Decimal64(18, 3);
3211        let output_type = DataType::Decimal64(18, 18);
3212        assert!(can_cast_types(&input_type, &output_type));
3213
3214        let array = vec![Some(i64::MAX)];
3215        let array = create_decimal64_array(array, 18, 3).unwrap();
3216        let result = cast_with_options(
3217            &array,
3218            &output_type,
3219            &CastOptions {
3220                safe: false,
3221                format_options: FormatOptions::default(),
3222            },
3223        );
3224        assert_eq!(
3225            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3226            result.unwrap_err().to_string()
3227        );
3228    }
3229
3230    #[test]
3231    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3232        let array = vec![
3233            Some(-999999999999999999),
3234            Some(0),
3235            Some(999999999999999999),
3236            None,
3237        ];
3238        let array = create_decimal64_array(array, 18, 3).unwrap();
3239
3240        // Divide out all digits of precision -- rounding could still produce +/- 1
3241        let output_type = DataType::Decimal64(18, -15);
3242        assert!(can_cast_types(array.data_type(), &output_type));
3243        generate_cast_test_case!(
3244            &array,
3245            Decimal64Array,
3246            &output_type,
3247            vec![Some(-1), Some(0), Some(1), None]
3248        );
3249
3250        // Divide out more digits than we have precision -- all-zero result
3251        let output_type = DataType::Decimal64(18, -16);
3252        assert!(can_cast_types(array.data_type(), &output_type));
3253        generate_cast_test_case!(
3254            &array,
3255            Decimal64Array,
3256            &output_type,
3257            vec![Some(0), Some(0), Some(0), None]
3258        );
3259    }
3260
3261    #[test]
3262    fn test_cast_floating_to_decimals() {
3263        for output_type in [
3264            DataType::Decimal32(9, 3),
3265            DataType::Decimal64(9, 3),
3266            DataType::Decimal128(9, 3),
3267            DataType::Decimal256(9, 3),
3268        ] {
3269            let input_type = DataType::Float64;
3270            assert!(can_cast_types(&input_type, &output_type));
3271
3272            let array = vec![Some(1.1_f64)];
3273            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3274            let result = cast_with_options(
3275                &array,
3276                &output_type,
3277                &CastOptions {
3278                    safe: false,
3279                    format_options: FormatOptions::default(),
3280                },
3281            );
3282            assert!(
3283                result.is_ok(),
3284                "Failed to cast to {output_type} with: {}",
3285                result.unwrap_err()
3286            );
3287        }
3288    }
3289
3290    #[test]
3291    fn test_cast_decimal128_to_decimal128_overflow() {
3292        let input_type = DataType::Decimal128(38, 3);
3293        let output_type = DataType::Decimal128(38, 38);
3294        assert!(can_cast_types(&input_type, &output_type));
3295
3296        let array = vec![Some(i128::MAX)];
3297        let array = create_decimal128_array(array, 38, 3).unwrap();
3298        let result = cast_with_options(
3299            &array,
3300            &output_type,
3301            &CastOptions {
3302                safe: false,
3303                format_options: FormatOptions::default(),
3304            },
3305        );
3306        assert_eq!(
3307            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3308            result.unwrap_err().to_string()
3309        );
3310    }
3311
3312    #[test]
3313    fn test_cast_decimal128_to_decimal256_overflow() {
3314        let input_type = DataType::Decimal128(38, 3);
3315        let output_type = DataType::Decimal256(76, 76);
3316        assert!(can_cast_types(&input_type, &output_type));
3317
3318        let array = vec![Some(i128::MAX)];
3319        let array = create_decimal128_array(array, 38, 3).unwrap();
3320        let result = cast_with_options(
3321            &array,
3322            &output_type,
3323            &CastOptions {
3324                safe: false,
3325                format_options: FormatOptions::default(),
3326            },
3327        );
3328        assert_eq!(
3329            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3330            result.unwrap_err().to_string()
3331        );
3332    }
3333
3334    #[test]
3335    fn test_cast_decimal32_to_decimal256() {
3336        let input_type = DataType::Decimal32(8, 3);
3337        let output_type = DataType::Decimal256(20, 4);
3338        assert!(can_cast_types(&input_type, &output_type));
3339        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3340        let array = create_decimal32_array(array, 8, 3).unwrap();
3341        generate_cast_test_case!(
3342            &array,
3343            Decimal256Array,
3344            &output_type,
3345            vec![
3346                Some(i256::from_i128(11234560_i128)),
3347                Some(i256::from_i128(21234560_i128)),
3348                Some(i256::from_i128(31234560_i128)),
3349                None
3350            ]
3351        );
3352    }
3353    #[test]
3354    fn test_cast_decimal64_to_decimal256() {
3355        let input_type = DataType::Decimal64(12, 3);
3356        let output_type = DataType::Decimal256(20, 4);
3357        assert!(can_cast_types(&input_type, &output_type));
3358        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3359        let array = create_decimal64_array(array, 12, 3).unwrap();
3360        generate_cast_test_case!(
3361            &array,
3362            Decimal256Array,
3363            &output_type,
3364            vec![
3365                Some(i256::from_i128(11234560_i128)),
3366                Some(i256::from_i128(21234560_i128)),
3367                Some(i256::from_i128(31234560_i128)),
3368                None
3369            ]
3370        );
3371    }
3372    #[test]
3373    fn test_cast_decimal128_to_decimal256() {
3374        let input_type = DataType::Decimal128(20, 3);
3375        let output_type = DataType::Decimal256(20, 4);
3376        assert!(can_cast_types(&input_type, &output_type));
3377        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3378        let array = create_decimal128_array(array, 20, 3).unwrap();
3379        generate_cast_test_case!(
3380            &array,
3381            Decimal256Array,
3382            &output_type,
3383            vec![
3384                Some(i256::from_i128(11234560_i128)),
3385                Some(i256::from_i128(21234560_i128)),
3386                Some(i256::from_i128(31234560_i128)),
3387                None
3388            ]
3389        );
3390    }
3391
3392    #[test]
3393    fn test_cast_decimal256_to_decimal128_overflow() {
3394        let input_type = DataType::Decimal256(76, 5);
3395        let output_type = DataType::Decimal128(38, 7);
3396        assert!(can_cast_types(&input_type, &output_type));
3397        let array = vec![Some(i256::from_i128(i128::MAX))];
3398        let array = create_decimal256_array(array, 76, 5).unwrap();
3399        let result = cast_with_options(
3400            &array,
3401            &output_type,
3402            &CastOptions {
3403                safe: false,
3404                format_options: FormatOptions::default(),
3405            },
3406        );
3407        assert_eq!(
3408            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3409            result.unwrap_err().to_string()
3410        );
3411    }
3412
3413    #[test]
3414    fn test_cast_decimal256_to_decimal256_overflow() {
3415        let input_type = DataType::Decimal256(76, 5);
3416        let output_type = DataType::Decimal256(76, 55);
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 Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3430            result.unwrap_err().to_string()
3431        );
3432    }
3433
3434    #[test]
3435    fn test_cast_decimal256_to_decimal128() {
3436        let input_type = DataType::Decimal256(20, 3);
3437        let output_type = DataType::Decimal128(20, 4);
3438        assert!(can_cast_types(&input_type, &output_type));
3439        let array = vec![
3440            Some(i256::from_i128(1123456)),
3441            Some(i256::from_i128(2123456)),
3442            Some(i256::from_i128(3123456)),
3443            None,
3444        ];
3445        let array = create_decimal256_array(array, 20, 3).unwrap();
3446        generate_cast_test_case!(
3447            &array,
3448            Decimal128Array,
3449            &output_type,
3450            vec![
3451                Some(11234560_i128),
3452                Some(21234560_i128),
3453                Some(31234560_i128),
3454                None
3455            ]
3456        );
3457    }
3458
3459    #[test]
3460    fn test_cast_decimal256_to_decimal256() {
3461        let input_type = DataType::Decimal256(20, 3);
3462        let output_type = DataType::Decimal256(20, 4);
3463        assert!(can_cast_types(&input_type, &output_type));
3464        let array = vec![
3465            Some(i256::from_i128(1123456)),
3466            Some(i256::from_i128(2123456)),
3467            Some(i256::from_i128(3123456)),
3468            None,
3469        ];
3470        let array = create_decimal256_array(array, 20, 3).unwrap();
3471        generate_cast_test_case!(
3472            &array,
3473            Decimal256Array,
3474            &output_type,
3475            vec![
3476                Some(i256::from_i128(11234560_i128)),
3477                Some(i256::from_i128(21234560_i128)),
3478                Some(i256::from_i128(31234560_i128)),
3479                None
3480            ]
3481        );
3482    }
3483
3484    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3485    where
3486        T: ArrowPrimitiveType + DecimalType,
3487    {
3488        // u8
3489        generate_cast_test_case!(
3490            array,
3491            UInt8Array,
3492            &DataType::UInt8,
3493            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3494        );
3495        // u16
3496        generate_cast_test_case!(
3497            array,
3498            UInt16Array,
3499            &DataType::UInt16,
3500            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3501        );
3502        // u32
3503        generate_cast_test_case!(
3504            array,
3505            UInt32Array,
3506            &DataType::UInt32,
3507            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3508        );
3509        // u64
3510        generate_cast_test_case!(
3511            array,
3512            UInt64Array,
3513            &DataType::UInt64,
3514            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3515        );
3516        // i8
3517        generate_cast_test_case!(
3518            array,
3519            Int8Array,
3520            &DataType::Int8,
3521            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3522        );
3523        // i16
3524        generate_cast_test_case!(
3525            array,
3526            Int16Array,
3527            &DataType::Int16,
3528            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3529        );
3530        // i32
3531        generate_cast_test_case!(
3532            array,
3533            Int32Array,
3534            &DataType::Int32,
3535            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3536        );
3537        // i64
3538        generate_cast_test_case!(
3539            array,
3540            Int64Array,
3541            &DataType::Int64,
3542            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3543        );
3544        // f32
3545        generate_cast_test_case!(
3546            array,
3547            Float32Array,
3548            &DataType::Float32,
3549            vec![
3550                Some(1.25_f32),
3551                Some(2.25_f32),
3552                Some(3.25_f32),
3553                None,
3554                Some(5.25_f32)
3555            ]
3556        );
3557        // f64
3558        generate_cast_test_case!(
3559            array,
3560            Float64Array,
3561            &DataType::Float64,
3562            vec![
3563                Some(1.25_f64),
3564                Some(2.25_f64),
3565                Some(3.25_f64),
3566                None,
3567                Some(5.25_f64)
3568            ]
3569        );
3570    }
3571
3572    #[test]
3573    fn test_cast_decimal32_to_numeric() {
3574        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3575        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3576
3577        generate_decimal_to_numeric_cast_test_case(&array);
3578    }
3579
3580    #[test]
3581    fn test_cast_decimal64_to_numeric() {
3582        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3583        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3584
3585        generate_decimal_to_numeric_cast_test_case(&array);
3586    }
3587
3588    #[test]
3589    fn test_cast_decimal128_to_numeric() {
3590        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3591        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3592
3593        generate_decimal_to_numeric_cast_test_case(&array);
3594
3595        // overflow test: out of range of max u8
3596        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3597        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3598        let casted_array = cast_with_options(
3599            &array,
3600            &DataType::UInt8,
3601            &CastOptions {
3602                safe: false,
3603                format_options: FormatOptions::default(),
3604            },
3605        );
3606        assert_eq!(
3607            "Cast error: value of 513 is out of range UInt8".to_string(),
3608            casted_array.unwrap_err().to_string()
3609        );
3610
3611        let casted_array = cast_with_options(
3612            &array,
3613            &DataType::UInt8,
3614            &CastOptions {
3615                safe: true,
3616                format_options: FormatOptions::default(),
3617            },
3618        );
3619        assert!(casted_array.is_ok());
3620        assert!(casted_array.unwrap().is_null(0));
3621
3622        // overflow test: out of range of max i8
3623        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3624        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3625        let casted_array = cast_with_options(
3626            &array,
3627            &DataType::Int8,
3628            &CastOptions {
3629                safe: false,
3630                format_options: FormatOptions::default(),
3631            },
3632        );
3633        assert_eq!(
3634            "Cast error: value of 244 is out of range Int8".to_string(),
3635            casted_array.unwrap_err().to_string()
3636        );
3637
3638        let casted_array = cast_with_options(
3639            &array,
3640            &DataType::Int8,
3641            &CastOptions {
3642                safe: true,
3643                format_options: FormatOptions::default(),
3644            },
3645        );
3646        assert!(casted_array.is_ok());
3647        assert!(casted_array.unwrap().is_null(0));
3648
3649        // loss the precision: convert decimal to f32、f64
3650        // f32
3651        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3652        let value_array: Vec<Option<i128>> = vec![
3653            Some(125),
3654            Some(225),
3655            Some(325),
3656            None,
3657            Some(525),
3658            Some(112345678),
3659            Some(112345679),
3660        ];
3661        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3662        generate_cast_test_case!(
3663            &array,
3664            Float32Array,
3665            &DataType::Float32,
3666            vec![
3667                Some(1.25_f32),
3668                Some(2.25_f32),
3669                Some(3.25_f32),
3670                None,
3671                Some(5.25_f32),
3672                Some(1_123_456.7_f32),
3673                Some(1_123_456.7_f32)
3674            ]
3675        );
3676
3677        // f64
3678        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3679        let value_array: Vec<Option<i128>> = vec![
3680            Some(125),
3681            Some(225),
3682            Some(325),
3683            None,
3684            Some(525),
3685            Some(112345678901234568),
3686            Some(112345678901234560),
3687        ];
3688        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3689        generate_cast_test_case!(
3690            &array,
3691            Float64Array,
3692            &DataType::Float64,
3693            vec![
3694                Some(1.25_f64),
3695                Some(2.25_f64),
3696                Some(3.25_f64),
3697                None,
3698                Some(5.25_f64),
3699                Some(1_123_456_789_012_345.6_f64),
3700                Some(1_123_456_789_012_345.6_f64),
3701            ]
3702        );
3703    }
3704
3705    #[test]
3706    fn test_cast_decimal256_to_numeric() {
3707        let value_array: Vec<Option<i256>> = vec![
3708            Some(i256::from_i128(125)),
3709            Some(i256::from_i128(225)),
3710            Some(i256::from_i128(325)),
3711            None,
3712            Some(i256::from_i128(525)),
3713        ];
3714        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3715        // u8
3716        generate_cast_test_case!(
3717            &array,
3718            UInt8Array,
3719            &DataType::UInt8,
3720            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3721        );
3722        // u16
3723        generate_cast_test_case!(
3724            &array,
3725            UInt16Array,
3726            &DataType::UInt16,
3727            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3728        );
3729        // u32
3730        generate_cast_test_case!(
3731            &array,
3732            UInt32Array,
3733            &DataType::UInt32,
3734            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3735        );
3736        // u64
3737        generate_cast_test_case!(
3738            &array,
3739            UInt64Array,
3740            &DataType::UInt64,
3741            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3742        );
3743        // i8
3744        generate_cast_test_case!(
3745            &array,
3746            Int8Array,
3747            &DataType::Int8,
3748            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3749        );
3750        // i16
3751        generate_cast_test_case!(
3752            &array,
3753            Int16Array,
3754            &DataType::Int16,
3755            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3756        );
3757        // i32
3758        generate_cast_test_case!(
3759            &array,
3760            Int32Array,
3761            &DataType::Int32,
3762            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3763        );
3764        // i64
3765        generate_cast_test_case!(
3766            &array,
3767            Int64Array,
3768            &DataType::Int64,
3769            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3770        );
3771        // f32
3772        generate_cast_test_case!(
3773            &array,
3774            Float32Array,
3775            &DataType::Float32,
3776            vec![
3777                Some(1.25_f32),
3778                Some(2.25_f32),
3779                Some(3.25_f32),
3780                None,
3781                Some(5.25_f32)
3782            ]
3783        );
3784        // f64
3785        generate_cast_test_case!(
3786            &array,
3787            Float64Array,
3788            &DataType::Float64,
3789            vec![
3790                Some(1.25_f64),
3791                Some(2.25_f64),
3792                Some(3.25_f64),
3793                None,
3794                Some(5.25_f64)
3795            ]
3796        );
3797
3798        // overflow test: out of range of max i8
3799        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3800        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3801        let casted_array = cast_with_options(
3802            &array,
3803            &DataType::Int8,
3804            &CastOptions {
3805                safe: false,
3806                format_options: FormatOptions::default(),
3807            },
3808        );
3809        assert_eq!(
3810            "Cast error: value of 244 is out of range Int8".to_string(),
3811            casted_array.unwrap_err().to_string()
3812        );
3813
3814        let casted_array = cast_with_options(
3815            &array,
3816            &DataType::Int8,
3817            &CastOptions {
3818                safe: true,
3819                format_options: FormatOptions::default(),
3820            },
3821        );
3822        assert!(casted_array.is_ok());
3823        assert!(casted_array.unwrap().is_null(0));
3824
3825        // loss the precision: convert decimal to f32、f64
3826        // f32
3827        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3828        let value_array: Vec<Option<i256>> = vec![
3829            Some(i256::from_i128(125)),
3830            Some(i256::from_i128(225)),
3831            Some(i256::from_i128(325)),
3832            None,
3833            Some(i256::from_i128(525)),
3834            Some(i256::from_i128(112345678)),
3835            Some(i256::from_i128(112345679)),
3836        ];
3837        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3838        generate_cast_test_case!(
3839            &array,
3840            Float32Array,
3841            &DataType::Float32,
3842            vec![
3843                Some(1.25_f32),
3844                Some(2.25_f32),
3845                Some(3.25_f32),
3846                None,
3847                Some(5.25_f32),
3848                Some(1_123_456.7_f32),
3849                Some(1_123_456.7_f32)
3850            ]
3851        );
3852
3853        // f64
3854        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3855        let value_array: Vec<Option<i256>> = vec![
3856            Some(i256::from_i128(125)),
3857            Some(i256::from_i128(225)),
3858            Some(i256::from_i128(325)),
3859            None,
3860            Some(i256::from_i128(525)),
3861            Some(i256::from_i128(112345678901234568)),
3862            Some(i256::from_i128(112345678901234560)),
3863        ];
3864        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3865        generate_cast_test_case!(
3866            &array,
3867            Float64Array,
3868            &DataType::Float64,
3869            vec![
3870                Some(1.25_f64),
3871                Some(2.25_f64),
3872                Some(3.25_f64),
3873                None,
3874                Some(5.25_f64),
3875                Some(1_123_456_789_012_345.6_f64),
3876                Some(1_123_456_789_012_345.6_f64),
3877            ]
3878        );
3879    }
3880
3881    #[test]
3882    fn test_cast_decimal_to_numeric_negative_scale() {
3883        let value_array: Vec<Option<i256>> = vec![
3884            Some(i256::from_i128(125)),
3885            Some(i256::from_i128(225)),
3886            Some(i256::from_i128(325)),
3887            None,
3888            Some(i256::from_i128(525)),
3889        ];
3890        let array = create_decimal256_array(value_array, 38, -1).unwrap();
3891
3892        generate_cast_test_case!(
3893            &array,
3894            Int64Array,
3895            &DataType::Int64,
3896            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
3897        );
3898
3899        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3900        let array = create_decimal32_array(value_array, 8, -2).unwrap();
3901        generate_cast_test_case!(
3902            &array,
3903            Int64Array,
3904            &DataType::Int64,
3905            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
3906        );
3907
3908        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
3909        let array = create_decimal32_array(value_array, 9, -9).unwrap();
3910        generate_cast_test_case!(
3911            &array,
3912            Int64Array,
3913            &DataType::Int64,
3914            vec![Some(2_000_000_000), Some(1_000_000_000), None]
3915        );
3916
3917        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3918        let array = create_decimal64_array(value_array, 18, -3).unwrap();
3919        generate_cast_test_case!(
3920            &array,
3921            Int64Array,
3922            &DataType::Int64,
3923            vec![
3924                Some(125_000),
3925                Some(225_000),
3926                Some(325_000),
3927                None,
3928                Some(525_000)
3929            ]
3930        );
3931
3932        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
3933        let array = create_decimal64_array(value_array, 18, -10).unwrap();
3934        generate_cast_test_case!(
3935            &array,
3936            Int64Array,
3937            &DataType::Int64,
3938            vec![Some(120_000_000_000), Some(340_000_000_000), None]
3939        );
3940
3941        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3942        let array = create_decimal128_array(value_array, 38, -4).unwrap();
3943        generate_cast_test_case!(
3944            &array,
3945            Int64Array,
3946            &DataType::Int64,
3947            vec![
3948                Some(1_250_000),
3949                Some(2_250_000),
3950                Some(3_250_000),
3951                None,
3952                Some(5_250_000)
3953            ]
3954        );
3955
3956        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
3957        let array = create_decimal128_array(value_array, 38, -18).unwrap();
3958        generate_cast_test_case!(
3959            &array,
3960            Int64Array,
3961            &DataType::Int64,
3962            vec![
3963                Some(9_000_000_000_000_000_000),
3964                Some(1_000_000_000_000_000_000),
3965                None
3966            ]
3967        );
3968
3969        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
3970        let casted_array = cast_with_options(
3971            &array,
3972            &DataType::Int64,
3973            &CastOptions {
3974                safe: false,
3975                format_options: FormatOptions::default(),
3976            },
3977        );
3978        assert_eq!(
3979            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
3980            casted_array.unwrap_err().to_string()
3981        );
3982
3983        let casted_array = cast_with_options(
3984            &array,
3985            &DataType::Int64,
3986            &CastOptions {
3987                safe: true,
3988                format_options: FormatOptions::default(),
3989            },
3990        );
3991        assert!(casted_array.is_ok());
3992        assert!(casted_array.unwrap().is_null(0));
3993
3994        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
3995        let casted_array = cast_with_options(
3996            &array,
3997            &DataType::Int8,
3998            &CastOptions {
3999                safe: false,
4000                format_options: FormatOptions::default(),
4001            },
4002        );
4003        assert_eq!(
4004            "Cast error: value of 130 is out of range Int8".to_string(),
4005            casted_array.unwrap_err().to_string()
4006        );
4007
4008        let casted_array = cast_with_options(
4009            &array,
4010            &DataType::Int8,
4011            &CastOptions {
4012                safe: true,
4013                format_options: FormatOptions::default(),
4014            },
4015        );
4016        assert!(casted_array.is_ok());
4017        assert!(casted_array.unwrap().is_null(0));
4018    }
4019
4020    #[test]
4021    fn test_cast_numeric_to_decimal128() {
4022        let decimal_type = DataType::Decimal128(38, 6);
4023        // u8, u16, u32, u64
4024        let input_datas = vec![
4025            Arc::new(UInt8Array::from(vec![
4026                Some(1),
4027                Some(2),
4028                Some(3),
4029                None,
4030                Some(5),
4031            ])) as ArrayRef, // u8
4032            Arc::new(UInt16Array::from(vec![
4033                Some(1),
4034                Some(2),
4035                Some(3),
4036                None,
4037                Some(5),
4038            ])) as ArrayRef, // u16
4039            Arc::new(UInt32Array::from(vec![
4040                Some(1),
4041                Some(2),
4042                Some(3),
4043                None,
4044                Some(5),
4045            ])) as ArrayRef, // u32
4046            Arc::new(UInt64Array::from(vec![
4047                Some(1),
4048                Some(2),
4049                Some(3),
4050                None,
4051                Some(5),
4052            ])) as ArrayRef, // u64
4053        ];
4054
4055        for array in input_datas {
4056            generate_cast_test_case!(
4057                &array,
4058                Decimal128Array,
4059                &decimal_type,
4060                vec![
4061                    Some(1000000_i128),
4062                    Some(2000000_i128),
4063                    Some(3000000_i128),
4064                    None,
4065                    Some(5000000_i128)
4066                ]
4067            );
4068        }
4069
4070        // i8, i16, i32, i64
4071        let input_datas = vec![
4072            Arc::new(Int8Array::from(vec![
4073                Some(1),
4074                Some(2),
4075                Some(3),
4076                None,
4077                Some(5),
4078            ])) as ArrayRef, // i8
4079            Arc::new(Int16Array::from(vec![
4080                Some(1),
4081                Some(2),
4082                Some(3),
4083                None,
4084                Some(5),
4085            ])) as ArrayRef, // i16
4086            Arc::new(Int32Array::from(vec![
4087                Some(1),
4088                Some(2),
4089                Some(3),
4090                None,
4091                Some(5),
4092            ])) as ArrayRef, // i32
4093            Arc::new(Int64Array::from(vec![
4094                Some(1),
4095                Some(2),
4096                Some(3),
4097                None,
4098                Some(5),
4099            ])) as ArrayRef, // i64
4100        ];
4101        for array in input_datas {
4102            generate_cast_test_case!(
4103                &array,
4104                Decimal128Array,
4105                &decimal_type,
4106                vec![
4107                    Some(1000000_i128),
4108                    Some(2000000_i128),
4109                    Some(3000000_i128),
4110                    None,
4111                    Some(5000000_i128)
4112                ]
4113            );
4114        }
4115
4116        // test u8 to decimal type with overflow the result type
4117        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4118        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4119        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4120        assert!(casted_array.is_ok());
4121        let array = casted_array.unwrap();
4122        let array: &Decimal128Array = array.as_primitive();
4123        assert!(array.is_null(4));
4124
4125        // test i8 to decimal type with overflow the result type
4126        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4127        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4128        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4129        assert!(casted_array.is_ok());
4130        let array = casted_array.unwrap();
4131        let array: &Decimal128Array = array.as_primitive();
4132        assert!(array.is_null(4));
4133
4134        // test f32 to decimal type
4135        let array = Float32Array::from(vec![
4136            Some(1.1),
4137            Some(2.2),
4138            Some(4.4),
4139            None,
4140            Some(1.123_456_4), // round down
4141            Some(1.123_456_7), // round up
4142        ]);
4143        let array = Arc::new(array) as ArrayRef;
4144        generate_cast_test_case!(
4145            &array,
4146            Decimal128Array,
4147            &decimal_type,
4148            vec![
4149                Some(1100000_i128),
4150                Some(2200000_i128),
4151                Some(4400000_i128),
4152                None,
4153                Some(1123456_i128), // round down
4154                Some(1123457_i128), // round up
4155            ]
4156        );
4157
4158        // test f64 to decimal type
4159        let array = Float64Array::from(vec![
4160            Some(1.1),
4161            Some(2.2),
4162            Some(4.4),
4163            None,
4164            Some(1.123_456_489_123_4),     // round up
4165            Some(1.123_456_789_123_4),     // round up
4166            Some(1.123_456_489_012_345_6), // round down
4167            Some(1.123_456_789_012_345_6), // round up
4168        ]);
4169        generate_cast_test_case!(
4170            &array,
4171            Decimal128Array,
4172            &decimal_type,
4173            vec![
4174                Some(1100000_i128),
4175                Some(2200000_i128),
4176                Some(4400000_i128),
4177                None,
4178                Some(1123456_i128), // round down
4179                Some(1123457_i128), // round up
4180                Some(1123456_i128), // round down
4181                Some(1123457_i128), // round up
4182            ]
4183        );
4184    }
4185
4186    #[test]
4187    fn test_cast_numeric_to_decimal256() {
4188        let decimal_type = DataType::Decimal256(76, 6);
4189        // u8, u16, u32, u64
4190        let input_datas = vec![
4191            Arc::new(UInt8Array::from(vec![
4192                Some(1),
4193                Some(2),
4194                Some(3),
4195                None,
4196                Some(5),
4197            ])) as ArrayRef, // u8
4198            Arc::new(UInt16Array::from(vec![
4199                Some(1),
4200                Some(2),
4201                Some(3),
4202                None,
4203                Some(5),
4204            ])) as ArrayRef, // u16
4205            Arc::new(UInt32Array::from(vec![
4206                Some(1),
4207                Some(2),
4208                Some(3),
4209                None,
4210                Some(5),
4211            ])) as ArrayRef, // u32
4212            Arc::new(UInt64Array::from(vec![
4213                Some(1),
4214                Some(2),
4215                Some(3),
4216                None,
4217                Some(5),
4218            ])) as ArrayRef, // u64
4219        ];
4220
4221        for array in input_datas {
4222            generate_cast_test_case!(
4223                &array,
4224                Decimal256Array,
4225                &decimal_type,
4226                vec![
4227                    Some(i256::from_i128(1000000_i128)),
4228                    Some(i256::from_i128(2000000_i128)),
4229                    Some(i256::from_i128(3000000_i128)),
4230                    None,
4231                    Some(i256::from_i128(5000000_i128))
4232                ]
4233            );
4234        }
4235
4236        // i8, i16, i32, i64
4237        let input_datas = vec![
4238            Arc::new(Int8Array::from(vec![
4239                Some(1),
4240                Some(2),
4241                Some(3),
4242                None,
4243                Some(5),
4244            ])) as ArrayRef, // i8
4245            Arc::new(Int16Array::from(vec![
4246                Some(1),
4247                Some(2),
4248                Some(3),
4249                None,
4250                Some(5),
4251            ])) as ArrayRef, // i16
4252            Arc::new(Int32Array::from(vec![
4253                Some(1),
4254                Some(2),
4255                Some(3),
4256                None,
4257                Some(5),
4258            ])) as ArrayRef, // i32
4259            Arc::new(Int64Array::from(vec![
4260                Some(1),
4261                Some(2),
4262                Some(3),
4263                None,
4264                Some(5),
4265            ])) as ArrayRef, // i64
4266        ];
4267        for array in input_datas {
4268            generate_cast_test_case!(
4269                &array,
4270                Decimal256Array,
4271                &decimal_type,
4272                vec![
4273                    Some(i256::from_i128(1000000_i128)),
4274                    Some(i256::from_i128(2000000_i128)),
4275                    Some(i256::from_i128(3000000_i128)),
4276                    None,
4277                    Some(i256::from_i128(5000000_i128))
4278                ]
4279            );
4280        }
4281
4282        // test i8 to decimal type with overflow the result type
4283        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4284        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4285        let array = Arc::new(array) as ArrayRef;
4286        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4287        assert!(casted_array.is_ok());
4288        let array = casted_array.unwrap();
4289        let array: &Decimal256Array = array.as_primitive();
4290        assert!(array.is_null(4));
4291
4292        // test f32 to decimal type
4293        let array = Float32Array::from(vec![
4294            Some(1.1),
4295            Some(2.2),
4296            Some(4.4),
4297            None,
4298            Some(1.123_456_4), // round down
4299            Some(1.123_456_7), // round up
4300        ]);
4301        generate_cast_test_case!(
4302            &array,
4303            Decimal256Array,
4304            &decimal_type,
4305            vec![
4306                Some(i256::from_i128(1100000_i128)),
4307                Some(i256::from_i128(2200000_i128)),
4308                Some(i256::from_i128(4400000_i128)),
4309                None,
4310                Some(i256::from_i128(1123456_i128)), // round down
4311                Some(i256::from_i128(1123457_i128)), // round up
4312            ]
4313        );
4314
4315        // test f64 to decimal type
4316        let array = Float64Array::from(vec![
4317            Some(1.1),
4318            Some(2.2),
4319            Some(4.4),
4320            None,
4321            Some(1.123_456_489_123_4),     // round down
4322            Some(1.123_456_789_123_4),     // round up
4323            Some(1.123_456_489_012_345_6), // round down
4324            Some(1.123_456_789_012_345_6), // round up
4325        ]);
4326        generate_cast_test_case!(
4327            &array,
4328            Decimal256Array,
4329            &decimal_type,
4330            vec![
4331                Some(i256::from_i128(1100000_i128)),
4332                Some(i256::from_i128(2200000_i128)),
4333                Some(i256::from_i128(4400000_i128)),
4334                None,
4335                Some(i256::from_i128(1123456_i128)), // round down
4336                Some(i256::from_i128(1123457_i128)), // round up
4337                Some(i256::from_i128(1123456_i128)), // round down
4338                Some(i256::from_i128(1123457_i128)), // round up
4339            ]
4340        );
4341    }
4342
4343    #[test]
4344    fn test_cast_i32_to_f64() {
4345        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4346        let b = cast(&array, &DataType::Float64).unwrap();
4347        let c = b.as_primitive::<Float64Type>();
4348        assert_eq!(5.0, c.value(0));
4349        assert_eq!(6.0, c.value(1));
4350        assert_eq!(7.0, c.value(2));
4351        assert_eq!(8.0, c.value(3));
4352        assert_eq!(9.0, c.value(4));
4353    }
4354
4355    #[test]
4356    fn test_cast_i32_to_u8() {
4357        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4358        let b = cast(&array, &DataType::UInt8).unwrap();
4359        let c = b.as_primitive::<UInt8Type>();
4360        assert!(!c.is_valid(0));
4361        assert_eq!(6, c.value(1));
4362        assert!(!c.is_valid(2));
4363        assert_eq!(8, c.value(3));
4364        // overflows return None
4365        assert!(!c.is_valid(4));
4366    }
4367
4368    #[test]
4369    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4370    fn test_cast_int32_to_u8_with_error() {
4371        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4372        // overflow with the error
4373        let cast_option = CastOptions {
4374            safe: false,
4375            format_options: FormatOptions::default(),
4376        };
4377        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4378        assert!(result.is_err());
4379        result.unwrap();
4380    }
4381
4382    #[test]
4383    fn test_cast_i32_to_u8_sliced() {
4384        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4385        assert_eq!(0, array.offset());
4386        let array = array.slice(2, 3);
4387        let b = cast(&array, &DataType::UInt8).unwrap();
4388        assert_eq!(3, b.len());
4389        let c = b.as_primitive::<UInt8Type>();
4390        assert!(!c.is_valid(0));
4391        assert_eq!(8, c.value(1));
4392        // overflows return None
4393        assert!(!c.is_valid(2));
4394    }
4395
4396    #[test]
4397    fn test_cast_i32_to_i32() {
4398        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4399        let b = cast(&array, &DataType::Int32).unwrap();
4400        let c = b.as_primitive::<Int32Type>();
4401        assert_eq!(5, c.value(0));
4402        assert_eq!(6, c.value(1));
4403        assert_eq!(7, c.value(2));
4404        assert_eq!(8, c.value(3));
4405        assert_eq!(9, c.value(4));
4406    }
4407
4408    #[test]
4409    fn test_cast_i32_to_list_i32() {
4410        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4411        let b = cast(
4412            &array,
4413            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4414        )
4415        .unwrap();
4416        assert_eq!(5, b.len());
4417        let arr = b.as_list::<i32>();
4418        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4419        assert_eq!(1, arr.value_length(0));
4420        assert_eq!(1, arr.value_length(1));
4421        assert_eq!(1, arr.value_length(2));
4422        assert_eq!(1, arr.value_length(3));
4423        assert_eq!(1, arr.value_length(4));
4424        let c = arr.values().as_primitive::<Int32Type>();
4425        assert_eq!(5, c.value(0));
4426        assert_eq!(6, c.value(1));
4427        assert_eq!(7, c.value(2));
4428        assert_eq!(8, c.value(3));
4429        assert_eq!(9, c.value(4));
4430    }
4431
4432    #[test]
4433    fn test_cast_i32_to_list_i32_nullable() {
4434        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4435        let b = cast(
4436            &array,
4437            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4438        )
4439        .unwrap();
4440        assert_eq!(5, b.len());
4441        assert_eq!(0, b.null_count());
4442        let arr = b.as_list::<i32>();
4443        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4444        assert_eq!(1, arr.value_length(0));
4445        assert_eq!(1, arr.value_length(1));
4446        assert_eq!(1, arr.value_length(2));
4447        assert_eq!(1, arr.value_length(3));
4448        assert_eq!(1, arr.value_length(4));
4449
4450        let c = arr.values().as_primitive::<Int32Type>();
4451        assert_eq!(1, c.null_count());
4452        assert_eq!(5, c.value(0));
4453        assert!(!c.is_valid(1));
4454        assert_eq!(7, c.value(2));
4455        assert_eq!(8, c.value(3));
4456        assert_eq!(9, c.value(4));
4457    }
4458
4459    #[test]
4460    fn test_cast_i32_to_list_f64_nullable_sliced() {
4461        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4462        let array = array.slice(2, 4);
4463        let b = cast(
4464            &array,
4465            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4466        )
4467        .unwrap();
4468        assert_eq!(4, b.len());
4469        assert_eq!(0, b.null_count());
4470        let arr = b.as_list::<i32>();
4471        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4472        assert_eq!(1, arr.value_length(0));
4473        assert_eq!(1, arr.value_length(1));
4474        assert_eq!(1, arr.value_length(2));
4475        assert_eq!(1, arr.value_length(3));
4476        let c = arr.values().as_primitive::<Float64Type>();
4477        assert_eq!(1, c.null_count());
4478        assert_eq!(7.0, c.value(0));
4479        assert_eq!(8.0, c.value(1));
4480        assert!(!c.is_valid(2));
4481        assert_eq!(10.0, c.value(3));
4482    }
4483
4484    #[test]
4485    fn test_cast_int_to_utf8view() {
4486        let inputs = vec![
4487            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4488            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4489            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4490            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4491            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4492            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4493            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4494            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4495        ];
4496        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4497            None,
4498            Some("8"),
4499            Some("9"),
4500            Some("10"),
4501        ]));
4502
4503        for array in inputs {
4504            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4505            let arr = cast(&array, &DataType::Utf8View).unwrap();
4506            assert_eq!(expected.as_ref(), arr.as_ref());
4507        }
4508    }
4509
4510    #[test]
4511    fn test_cast_float_to_utf8view() {
4512        let inputs = vec![
4513            Arc::new(Float16Array::from(vec![
4514                Some(f16::from_f64(1.5)),
4515                Some(f16::from_f64(2.5)),
4516                None,
4517            ])) as ArrayRef,
4518            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4519            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4520        ];
4521
4522        let expected: ArrayRef =
4523            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4524
4525        for array in inputs {
4526            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4527            let arr = cast(&array, &DataType::Utf8View).unwrap();
4528            assert_eq!(expected.as_ref(), arr.as_ref());
4529        }
4530    }
4531
4532    #[test]
4533    fn test_cast_utf8_to_i32() {
4534        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4535        let b = cast(&array, &DataType::Int32).unwrap();
4536        let c = b.as_primitive::<Int32Type>();
4537        assert_eq!(5, c.value(0));
4538        assert_eq!(6, c.value(1));
4539        assert!(!c.is_valid(2));
4540        assert_eq!(8, c.value(3));
4541        assert!(!c.is_valid(4));
4542    }
4543
4544    #[test]
4545    fn test_cast_utf8view_to_i32() {
4546        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4547        let b = cast(&array, &DataType::Int32).unwrap();
4548        let c = b.as_primitive::<Int32Type>();
4549        assert_eq!(5, c.value(0));
4550        assert_eq!(6, c.value(1));
4551        assert!(!c.is_valid(2));
4552        assert_eq!(8, c.value(3));
4553        assert!(!c.is_valid(4));
4554    }
4555
4556    #[test]
4557    fn test_cast_utf8view_to_f32() {
4558        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4559        let b = cast(&array, &DataType::Float32).unwrap();
4560        let c = b.as_primitive::<Float32Type>();
4561        assert_eq!(3.0, c.value(0));
4562        assert_eq!(4.56, c.value(1));
4563        assert!(!c.is_valid(2));
4564        assert_eq!(8.9, c.value(3));
4565    }
4566
4567    #[test]
4568    fn test_cast_string_to_f16() {
4569        let arrays = [
4570            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4571            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4572            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4573        ];
4574        for array in arrays {
4575            let b = cast(&array, &DataType::Float16).unwrap();
4576            let c = b.as_primitive::<Float16Type>();
4577            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4578            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4579            assert!(!c.is_valid(2));
4580            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4581        }
4582    }
4583
4584    #[test]
4585    fn test_cast_utf8view_to_decimal128() {
4586        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4587        let arr = Arc::new(array) as ArrayRef;
4588        generate_cast_test_case!(
4589            &arr,
4590            Decimal128Array,
4591            &DataType::Decimal128(4, 2),
4592            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4593        );
4594    }
4595
4596    #[test]
4597    fn test_cast_with_options_utf8_to_i32() {
4598        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4599        let result = cast_with_options(
4600            &array,
4601            &DataType::Int32,
4602            &CastOptions {
4603                safe: false,
4604                format_options: FormatOptions::default(),
4605            },
4606        );
4607        match result {
4608            Ok(_) => panic!("expected error"),
4609            Err(e) => {
4610                assert!(
4611                    e.to_string()
4612                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4613                    "Error: {e}"
4614                )
4615            }
4616        }
4617    }
4618
4619    #[test]
4620    fn test_cast_utf8_to_bool() {
4621        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4622        let casted = cast(&strings, &DataType::Boolean).unwrap();
4623        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4624        assert_eq!(*as_boolean_array(&casted), expected);
4625    }
4626
4627    #[test]
4628    fn test_cast_utf8view_to_bool() {
4629        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4630        let casted = cast(&strings, &DataType::Boolean).unwrap();
4631        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4632        assert_eq!(*as_boolean_array(&casted), expected);
4633    }
4634
4635    #[test]
4636    fn test_cast_with_options_utf8_to_bool() {
4637        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4638        let casted = cast_with_options(
4639            &strings,
4640            &DataType::Boolean,
4641            &CastOptions {
4642                safe: false,
4643                format_options: FormatOptions::default(),
4644            },
4645        );
4646        match casted {
4647            Ok(_) => panic!("expected error"),
4648            Err(e) => {
4649                assert!(
4650                    e.to_string().contains(
4651                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4652                    )
4653                )
4654            }
4655        }
4656    }
4657
4658    #[test]
4659    fn test_cast_bool_to_i32() {
4660        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4661        let b = cast(&array, &DataType::Int32).unwrap();
4662        let c = b.as_primitive::<Int32Type>();
4663        assert_eq!(1, c.value(0));
4664        assert_eq!(0, c.value(1));
4665        assert!(!c.is_valid(2));
4666    }
4667
4668    #[test]
4669    fn test_cast_bool_to_utf8view() {
4670        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4671        let b = cast(&array, &DataType::Utf8View).unwrap();
4672        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4673        assert_eq!("true", c.value(0));
4674        assert_eq!("false", c.value(1));
4675        assert!(!c.is_valid(2));
4676    }
4677
4678    #[test]
4679    fn test_cast_bool_to_utf8() {
4680        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4681        let b = cast(&array, &DataType::Utf8).unwrap();
4682        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4683        assert_eq!("true", c.value(0));
4684        assert_eq!("false", c.value(1));
4685        assert!(!c.is_valid(2));
4686    }
4687
4688    #[test]
4689    fn test_cast_bool_to_large_utf8() {
4690        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4691        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4692        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4693        assert_eq!("true", c.value(0));
4694        assert_eq!("false", c.value(1));
4695        assert!(!c.is_valid(2));
4696    }
4697
4698    #[test]
4699    fn test_cast_bool_to_f64() {
4700        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4701        let b = cast(&array, &DataType::Float64).unwrap();
4702        let c = b.as_primitive::<Float64Type>();
4703        assert_eq!(1.0, c.value(0));
4704        assert_eq!(0.0, c.value(1));
4705        assert!(!c.is_valid(2));
4706    }
4707
4708    #[test]
4709    fn test_cast_integer_to_timestamp() {
4710        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4711        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4712
4713        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4714        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4715
4716        assert_eq!(&actual, &expected);
4717
4718        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4719        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4720
4721        assert_eq!(&actual, &expected);
4722
4723        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4724        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4725
4726        assert_eq!(&actual, &expected);
4727
4728        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4729        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4730
4731        assert_eq!(&actual, &expected);
4732
4733        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4734        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4735
4736        assert_eq!(&actual, &expected);
4737
4738        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4739        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4740
4741        assert_eq!(&actual, &expected);
4742
4743        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4744        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4745
4746        assert_eq!(&actual, &expected);
4747    }
4748
4749    #[test]
4750    fn test_cast_timestamp_to_integer() {
4751        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4752            .with_timezone("UTC".to_string());
4753        let expected = cast(&array, &DataType::Int64).unwrap();
4754
4755        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4756        assert_eq!(&actual, &expected);
4757
4758        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4759        assert_eq!(&actual, &expected);
4760
4761        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4762        assert_eq!(&actual, &expected);
4763
4764        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4765        assert_eq!(&actual, &expected);
4766
4767        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4768        assert_eq!(&actual, &expected);
4769
4770        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4771        assert_eq!(&actual, &expected);
4772
4773        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4774        assert_eq!(&actual, &expected);
4775    }
4776
4777    #[test]
4778    fn test_cast_floating_to_timestamp() {
4779        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4780        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4781
4782        let array = Float16Array::from(vec![
4783            Some(f16::from_f32(2.0)),
4784            Some(f16::from_f32(10.6)),
4785            None,
4786        ]);
4787        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4788
4789        assert_eq!(&actual, &expected);
4790
4791        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4792        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4793
4794        assert_eq!(&actual, &expected);
4795
4796        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4797        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4798
4799        assert_eq!(&actual, &expected);
4800    }
4801
4802    #[test]
4803    fn test_cast_timestamp_to_floating() {
4804        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4805            .with_timezone("UTC".to_string());
4806        let expected = cast(&array, &DataType::Int64).unwrap();
4807
4808        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4809        assert_eq!(&actual, &expected);
4810
4811        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4812        assert_eq!(&actual, &expected);
4813
4814        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4815        assert_eq!(&actual, &expected);
4816    }
4817
4818    #[test]
4819    fn test_cast_decimal_to_timestamp() {
4820        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4821        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4822
4823        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4824            .with_precision_and_scale(4, 2)
4825            .unwrap();
4826        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4827
4828        assert_eq!(&actual, &expected);
4829
4830        let array = Decimal256Array::from(vec![
4831            Some(i256::from_i128(2000)),
4832            Some(i256::from_i128(10000)),
4833            None,
4834        ])
4835        .with_precision_and_scale(5, 3)
4836        .unwrap();
4837        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4838
4839        assert_eq!(&actual, &expected);
4840    }
4841
4842    #[test]
4843    fn test_cast_timestamp_to_decimal() {
4844        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4845            .with_timezone("UTC".to_string());
4846        let expected = cast(&array, &DataType::Int64).unwrap();
4847
4848        let actual = cast(
4849            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4850            &DataType::Int64,
4851        )
4852        .unwrap();
4853        assert_eq!(&actual, &expected);
4854
4855        let actual = cast(
4856            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4857            &DataType::Int64,
4858        )
4859        .unwrap();
4860        assert_eq!(&actual, &expected);
4861    }
4862
4863    #[test]
4864    fn test_cast_list_i32_to_list_u16() {
4865        let values = vec![
4866            Some(vec![Some(0), Some(0), Some(0)]),
4867            Some(vec![Some(-1), Some(-2), Some(-1)]),
4868            Some(vec![Some(2), Some(100000000)]),
4869        ];
4870        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
4871
4872        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
4873        assert!(can_cast_types(list_array.data_type(), &target_type));
4874        let cast_array = cast(&list_array, &target_type).unwrap();
4875
4876        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4877        //
4878        // 3 negative values should get lost when casting to unsigned,
4879        // 1 value should overflow
4880        assert_eq!(0, cast_array.null_count());
4881
4882        // offsets should be the same
4883        let array = cast_array.as_list::<i32>();
4884        assert_eq!(list_array.value_offsets(), array.value_offsets());
4885
4886        assert_eq!(DataType::UInt16, array.value_type());
4887        assert_eq!(3, array.value_length(0));
4888        assert_eq!(3, array.value_length(1));
4889        assert_eq!(2, array.value_length(2));
4890
4891        // expect 4 nulls: negative numbers and overflow
4892        let u16arr = array.values().as_primitive::<UInt16Type>();
4893        assert_eq!(4, u16arr.null_count());
4894
4895        // expect 4 nulls: negative numbers and overflow
4896        let expected: UInt16Array =
4897            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4898                .into_iter()
4899                .collect();
4900
4901        assert_eq!(u16arr, &expected);
4902    }
4903
4904    #[test]
4905    fn test_cast_list_i32_to_list_timestamp() {
4906        // Construct a value array
4907        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4908
4909        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4910
4911        // Construct a list array from the above two
4912        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4913        let list_data = ArrayData::builder(list_data_type)
4914            .len(3)
4915            .add_buffer(value_offsets)
4916            .add_child_data(value_data)
4917            .build()
4918            .unwrap();
4919        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4920
4921        let actual = cast(
4922            &list_array,
4923            &DataType::List(Arc::new(Field::new_list_field(
4924                DataType::Timestamp(TimeUnit::Microsecond, None),
4925                true,
4926            ))),
4927        )
4928        .unwrap();
4929
4930        let expected = cast(
4931            &cast(
4932                &list_array,
4933                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4934            )
4935            .unwrap(),
4936            &DataType::List(Arc::new(Field::new_list_field(
4937                DataType::Timestamp(TimeUnit::Microsecond, None),
4938                true,
4939            ))),
4940        )
4941        .unwrap();
4942
4943        assert_eq!(&actual, &expected);
4944    }
4945
4946    #[test]
4947    fn test_cast_date32_to_date64() {
4948        let a = Date32Array::from(vec![10000, 17890]);
4949        let array = Arc::new(a) as ArrayRef;
4950        let b = cast(&array, &DataType::Date64).unwrap();
4951        let c = b.as_primitive::<Date64Type>();
4952        assert_eq!(864000000000, c.value(0));
4953        assert_eq!(1545696000000, c.value(1));
4954    }
4955
4956    #[test]
4957    fn test_cast_date64_to_date32() {
4958        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4959        let array = Arc::new(a) as ArrayRef;
4960        let b = cast(&array, &DataType::Date32).unwrap();
4961        let c = b.as_primitive::<Date32Type>();
4962        assert_eq!(10000, c.value(0));
4963        assert_eq!(17890, c.value(1));
4964        assert!(c.is_null(2));
4965    }
4966
4967    #[test]
4968    fn test_cast_string_to_integral_overflow() {
4969        let str = Arc::new(StringArray::from(vec![
4970            Some("123"),
4971            Some("-123"),
4972            Some("86374"),
4973            None,
4974        ])) as ArrayRef;
4975
4976        let options = CastOptions {
4977            safe: true,
4978            format_options: FormatOptions::default(),
4979        };
4980        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4981        let expected =
4982            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4983        assert_eq!(&res, &expected);
4984    }
4985
4986    #[test]
4987    fn test_cast_string_to_timestamp() {
4988        let a0 = Arc::new(StringViewArray::from(vec![
4989            Some("2020-09-08T12:00:00.123456789+00:00"),
4990            Some("Not a valid date"),
4991            None,
4992        ])) as ArrayRef;
4993        let a1 = Arc::new(StringArray::from(vec![
4994            Some("2020-09-08T12:00:00.123456789+00:00"),
4995            Some("Not a valid date"),
4996            None,
4997        ])) as ArrayRef;
4998        let a2 = Arc::new(LargeStringArray::from(vec![
4999            Some("2020-09-08T12:00:00.123456789+00:00"),
5000            Some("Not a valid date"),
5001            None,
5002        ])) as ArrayRef;
5003        for array in &[a0, a1, a2] {
5004            for time_unit in &[
5005                TimeUnit::Second,
5006                TimeUnit::Millisecond,
5007                TimeUnit::Microsecond,
5008                TimeUnit::Nanosecond,
5009            ] {
5010                let to_type = DataType::Timestamp(*time_unit, None);
5011                let b = cast(array, &to_type).unwrap();
5012
5013                match time_unit {
5014                    TimeUnit::Second => {
5015                        let c = b.as_primitive::<TimestampSecondType>();
5016                        assert_eq!(1599566400, c.value(0));
5017                        assert!(c.is_null(1));
5018                        assert!(c.is_null(2));
5019                    }
5020                    TimeUnit::Millisecond => {
5021                        let c = b
5022                            .as_any()
5023                            .downcast_ref::<TimestampMillisecondArray>()
5024                            .unwrap();
5025                        assert_eq!(1599566400123, c.value(0));
5026                        assert!(c.is_null(1));
5027                        assert!(c.is_null(2));
5028                    }
5029                    TimeUnit::Microsecond => {
5030                        let c = b
5031                            .as_any()
5032                            .downcast_ref::<TimestampMicrosecondArray>()
5033                            .unwrap();
5034                        assert_eq!(1599566400123456, c.value(0));
5035                        assert!(c.is_null(1));
5036                        assert!(c.is_null(2));
5037                    }
5038                    TimeUnit::Nanosecond => {
5039                        let c = b
5040                            .as_any()
5041                            .downcast_ref::<TimestampNanosecondArray>()
5042                            .unwrap();
5043                        assert_eq!(1599566400123456789, c.value(0));
5044                        assert!(c.is_null(1));
5045                        assert!(c.is_null(2));
5046                    }
5047                }
5048
5049                let options = CastOptions {
5050                    safe: false,
5051                    format_options: FormatOptions::default(),
5052                };
5053                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5054                assert_eq!(
5055                    err.to_string(),
5056                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5057                );
5058            }
5059        }
5060    }
5061
5062    #[test]
5063    fn test_cast_string_to_timestamp_overflow() {
5064        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5065        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5066        let result = result.as_primitive::<TimestampSecondType>();
5067        assert_eq!(result.values(), &[247112596800]);
5068    }
5069
5070    #[test]
5071    fn test_cast_string_to_date32() {
5072        let a0 = Arc::new(StringViewArray::from(vec![
5073            Some("2018-12-25"),
5074            Some("Not a valid date"),
5075            None,
5076        ])) as ArrayRef;
5077        let a1 = Arc::new(StringArray::from(vec![
5078            Some("2018-12-25"),
5079            Some("Not a valid date"),
5080            None,
5081        ])) as ArrayRef;
5082        let a2 = Arc::new(LargeStringArray::from(vec![
5083            Some("2018-12-25"),
5084            Some("Not a valid date"),
5085            None,
5086        ])) as ArrayRef;
5087        for array in &[a0, a1, a2] {
5088            let to_type = DataType::Date32;
5089            let b = cast(array, &to_type).unwrap();
5090            let c = b.as_primitive::<Date32Type>();
5091            assert_eq!(17890, c.value(0));
5092            assert!(c.is_null(1));
5093            assert!(c.is_null(2));
5094
5095            let options = CastOptions {
5096                safe: false,
5097                format_options: FormatOptions::default(),
5098            };
5099            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5100            assert_eq!(
5101                err.to_string(),
5102                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5103            );
5104        }
5105    }
5106
5107    #[test]
5108    fn test_cast_string_with_large_date_to_date32() {
5109        let array = Arc::new(StringArray::from(vec![
5110            Some("+10999-12-31"),
5111            Some("-0010-02-28"),
5112            Some("0010-02-28"),
5113            Some("0000-01-01"),
5114            Some("-0000-01-01"),
5115            Some("-0001-01-01"),
5116        ])) as ArrayRef;
5117        let to_type = DataType::Date32;
5118        let options = CastOptions {
5119            safe: false,
5120            format_options: FormatOptions::default(),
5121        };
5122        let b = cast_with_options(&array, &to_type, &options).unwrap();
5123        let c = b.as_primitive::<Date32Type>();
5124        assert_eq!(3298139, c.value(0)); // 10999-12-31
5125        assert_eq!(-723122, c.value(1)); // -0010-02-28
5126        assert_eq!(-715817, c.value(2)); // 0010-02-28
5127        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5128        assert_eq!(-719528, c.value(3)); // 0000-01-01
5129        assert_eq!(-719528, c.value(4)); // -0000-01-01
5130        assert_eq!(-719893, c.value(5)); // -0001-01-01
5131    }
5132
5133    #[test]
5134    fn test_cast_invalid_string_with_large_date_to_date32() {
5135        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5136        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5137        let to_type = DataType::Date32;
5138        let options = CastOptions {
5139            safe: false,
5140            format_options: FormatOptions::default(),
5141        };
5142        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5143        assert_eq!(
5144            err.to_string(),
5145            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5146        );
5147    }
5148
5149    #[test]
5150    fn test_cast_string_format_yyyymmdd_to_date32() {
5151        let a0 = Arc::new(StringViewArray::from(vec![
5152            Some("2020-12-25"),
5153            Some("20201117"),
5154        ])) as ArrayRef;
5155        let a1 = Arc::new(StringArray::from(vec![
5156            Some("2020-12-25"),
5157            Some("20201117"),
5158        ])) as ArrayRef;
5159        let a2 = Arc::new(LargeStringArray::from(vec![
5160            Some("2020-12-25"),
5161            Some("20201117"),
5162        ])) as ArrayRef;
5163
5164        for array in &[a0, a1, a2] {
5165            let to_type = DataType::Date32;
5166            let options = CastOptions {
5167                safe: false,
5168                format_options: FormatOptions::default(),
5169            };
5170            let result = cast_with_options(&array, &to_type, &options).unwrap();
5171            let c = result.as_primitive::<Date32Type>();
5172            assert_eq!(
5173                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5174                c.value_as_date(0)
5175            );
5176            assert_eq!(
5177                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5178                c.value_as_date(1)
5179            );
5180        }
5181    }
5182
5183    #[test]
5184    fn test_cast_string_to_time32second() {
5185        let a0 = Arc::new(StringViewArray::from(vec![
5186            Some("08:08:35.091323414"),
5187            Some("08:08:60.091323414"), // leap second
5188            Some("08:08:61.091323414"), // not valid
5189            Some("Not a valid time"),
5190            None,
5191        ])) as ArrayRef;
5192        let a1 = Arc::new(StringArray::from(vec![
5193            Some("08:08:35.091323414"),
5194            Some("08:08:60.091323414"), // leap second
5195            Some("08:08:61.091323414"), // not valid
5196            Some("Not a valid time"),
5197            None,
5198        ])) as ArrayRef;
5199        let a2 = Arc::new(LargeStringArray::from(vec![
5200            Some("08:08:35.091323414"),
5201            Some("08:08:60.091323414"), // leap second
5202            Some("08:08:61.091323414"), // not valid
5203            Some("Not a valid time"),
5204            None,
5205        ])) as ArrayRef;
5206        for array in &[a0, a1, a2] {
5207            let to_type = DataType::Time32(TimeUnit::Second);
5208            let b = cast(array, &to_type).unwrap();
5209            let c = b.as_primitive::<Time32SecondType>();
5210            assert_eq!(29315, c.value(0));
5211            assert_eq!(29340, c.value(1));
5212            assert!(c.is_null(2));
5213            assert!(c.is_null(3));
5214            assert!(c.is_null(4));
5215
5216            let options = CastOptions {
5217                safe: false,
5218                format_options: FormatOptions::default(),
5219            };
5220            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5221            assert_eq!(
5222                err.to_string(),
5223                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5224            );
5225        }
5226    }
5227
5228    #[test]
5229    fn test_cast_string_to_time32millisecond() {
5230        let a0 = Arc::new(StringViewArray::from(vec![
5231            Some("08:08:35.091323414"),
5232            Some("08:08:60.091323414"), // leap second
5233            Some("08:08:61.091323414"), // not valid
5234            Some("Not a valid time"),
5235            None,
5236        ])) as ArrayRef;
5237        let a1 = Arc::new(StringArray::from(vec![
5238            Some("08:08:35.091323414"),
5239            Some("08:08:60.091323414"), // leap second
5240            Some("08:08:61.091323414"), // not valid
5241            Some("Not a valid time"),
5242            None,
5243        ])) as ArrayRef;
5244        let a2 = Arc::new(LargeStringArray::from(vec![
5245            Some("08:08:35.091323414"),
5246            Some("08:08:60.091323414"), // leap second
5247            Some("08:08:61.091323414"), // not valid
5248            Some("Not a valid time"),
5249            None,
5250        ])) as ArrayRef;
5251        for array in &[a0, a1, a2] {
5252            let to_type = DataType::Time32(TimeUnit::Millisecond);
5253            let b = cast(array, &to_type).unwrap();
5254            let c = b.as_primitive::<Time32MillisecondType>();
5255            assert_eq!(29315091, c.value(0));
5256            assert_eq!(29340091, c.value(1));
5257            assert!(c.is_null(2));
5258            assert!(c.is_null(3));
5259            assert!(c.is_null(4));
5260
5261            let options = CastOptions {
5262                safe: false,
5263                format_options: FormatOptions::default(),
5264            };
5265            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5266            assert_eq!(
5267                err.to_string(),
5268                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5269            );
5270        }
5271    }
5272
5273    #[test]
5274    fn test_cast_string_to_time64microsecond() {
5275        let a0 = Arc::new(StringViewArray::from(vec![
5276            Some("08:08:35.091323414"),
5277            Some("Not a valid time"),
5278            None,
5279        ])) as ArrayRef;
5280        let a1 = Arc::new(StringArray::from(vec![
5281            Some("08:08:35.091323414"),
5282            Some("Not a valid time"),
5283            None,
5284        ])) as ArrayRef;
5285        let a2 = Arc::new(LargeStringArray::from(vec![
5286            Some("08:08:35.091323414"),
5287            Some("Not a valid time"),
5288            None,
5289        ])) as ArrayRef;
5290        for array in &[a0, a1, a2] {
5291            let to_type = DataType::Time64(TimeUnit::Microsecond);
5292            let b = cast(array, &to_type).unwrap();
5293            let c = b.as_primitive::<Time64MicrosecondType>();
5294            assert_eq!(29315091323, c.value(0));
5295            assert!(c.is_null(1));
5296            assert!(c.is_null(2));
5297
5298            let options = CastOptions {
5299                safe: false,
5300                format_options: FormatOptions::default(),
5301            };
5302            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5303            assert_eq!(
5304                err.to_string(),
5305                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5306            );
5307        }
5308    }
5309
5310    #[test]
5311    fn test_cast_string_to_time64nanosecond() {
5312        let a0 = Arc::new(StringViewArray::from(vec![
5313            Some("08:08:35.091323414"),
5314            Some("Not a valid time"),
5315            None,
5316        ])) as ArrayRef;
5317        let a1 = Arc::new(StringArray::from(vec![
5318            Some("08:08:35.091323414"),
5319            Some("Not a valid time"),
5320            None,
5321        ])) as ArrayRef;
5322        let a2 = Arc::new(LargeStringArray::from(vec![
5323            Some("08:08:35.091323414"),
5324            Some("Not a valid time"),
5325            None,
5326        ])) as ArrayRef;
5327        for array in &[a0, a1, a2] {
5328            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5329            let b = cast(array, &to_type).unwrap();
5330            let c = b.as_primitive::<Time64NanosecondType>();
5331            assert_eq!(29315091323414, c.value(0));
5332            assert!(c.is_null(1));
5333            assert!(c.is_null(2));
5334
5335            let options = CastOptions {
5336                safe: false,
5337                format_options: FormatOptions::default(),
5338            };
5339            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5340            assert_eq!(
5341                err.to_string(),
5342                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5343            );
5344        }
5345    }
5346
5347    #[test]
5348    fn test_cast_string_to_date64() {
5349        let a0 = Arc::new(StringViewArray::from(vec![
5350            Some("2020-09-08T12:00:00"),
5351            Some("Not a valid date"),
5352            None,
5353        ])) as ArrayRef;
5354        let a1 = Arc::new(StringArray::from(vec![
5355            Some("2020-09-08T12:00:00"),
5356            Some("Not a valid date"),
5357            None,
5358        ])) as ArrayRef;
5359        let a2 = Arc::new(LargeStringArray::from(vec![
5360            Some("2020-09-08T12:00:00"),
5361            Some("Not a valid date"),
5362            None,
5363        ])) as ArrayRef;
5364        for array in &[a0, a1, a2] {
5365            let to_type = DataType::Date64;
5366            let b = cast(array, &to_type).unwrap();
5367            let c = b.as_primitive::<Date64Type>();
5368            assert_eq!(1599566400000, c.value(0));
5369            assert!(c.is_null(1));
5370            assert!(c.is_null(2));
5371
5372            let options = CastOptions {
5373                safe: false,
5374                format_options: FormatOptions::default(),
5375            };
5376            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5377            assert_eq!(
5378                err.to_string(),
5379                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5380            );
5381        }
5382    }
5383
5384    macro_rules! test_safe_string_to_interval {
5385        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5386            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5387
5388            let options = CastOptions {
5389                safe: true,
5390                format_options: FormatOptions::default(),
5391            };
5392
5393            let target_interval_array = cast_with_options(
5394                &source_string_array.clone(),
5395                &DataType::Interval($interval_unit),
5396                &options,
5397            )
5398            .unwrap()
5399            .as_any()
5400            .downcast_ref::<$array_ty>()
5401            .unwrap()
5402            .clone() as $array_ty;
5403
5404            let target_string_array =
5405                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5406                    .unwrap()
5407                    .as_any()
5408                    .downcast_ref::<StringArray>()
5409                    .unwrap()
5410                    .clone();
5411
5412            let expect_string_array = StringArray::from($expect_vec);
5413
5414            assert_eq!(target_string_array, expect_string_array);
5415
5416            let target_large_string_array =
5417                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5418                    .unwrap()
5419                    .as_any()
5420                    .downcast_ref::<LargeStringArray>()
5421                    .unwrap()
5422                    .clone();
5423
5424            let expect_large_string_array = LargeStringArray::from($expect_vec);
5425
5426            assert_eq!(target_large_string_array, expect_large_string_array);
5427        };
5428    }
5429
5430    #[test]
5431    fn test_cast_string_to_interval_year_month() {
5432        test_safe_string_to_interval!(
5433            vec![
5434                Some("1 year 1 month"),
5435                Some("1.5 years 13 month"),
5436                Some("30 days"),
5437                Some("31 days"),
5438                Some("2 months 31 days"),
5439                Some("2 months 31 days 1 second"),
5440                Some("foobar"),
5441            ],
5442            IntervalUnit::YearMonth,
5443            IntervalYearMonthArray,
5444            vec![
5445                Some("1 years 1 mons"),
5446                Some("2 years 7 mons"),
5447                None,
5448                None,
5449                None,
5450                None,
5451                None,
5452            ]
5453        );
5454    }
5455
5456    #[test]
5457    fn test_cast_string_to_interval_day_time() {
5458        test_safe_string_to_interval!(
5459            vec![
5460                Some("1 year 1 month"),
5461                Some("1.5 years 13 month"),
5462                Some("30 days"),
5463                Some("1 day 2 second 3.5 milliseconds"),
5464                Some("foobar"),
5465            ],
5466            IntervalUnit::DayTime,
5467            IntervalDayTimeArray,
5468            vec![
5469                Some("390 days"),
5470                Some("930 days"),
5471                Some("30 days"),
5472                None,
5473                None,
5474            ]
5475        );
5476    }
5477
5478    #[test]
5479    fn test_cast_string_to_interval_month_day_nano() {
5480        test_safe_string_to_interval!(
5481            vec![
5482                Some("1 year 1 month 1 day"),
5483                None,
5484                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5485                Some("3 days"),
5486                Some("8 seconds"),
5487                None,
5488                Some("1 day 29800 milliseconds"),
5489                Some("3 months 1 second"),
5490                Some("6 minutes 120 second"),
5491                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5492                Some("foobar"),
5493            ],
5494            IntervalUnit::MonthDayNano,
5495            IntervalMonthDayNanoArray,
5496            vec![
5497                Some("13 mons 1 days"),
5498                None,
5499                Some("31 mons 35 days 0.001400000 secs"),
5500                Some("3 days"),
5501                Some("8.000000000 secs"),
5502                None,
5503                Some("1 days 29.800000000 secs"),
5504                Some("3 mons 1.000000000 secs"),
5505                Some("8 mins"),
5506                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5507                None,
5508            ]
5509        );
5510    }
5511
5512    macro_rules! test_unsafe_string_to_interval_err {
5513        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5514            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5515            let options = CastOptions {
5516                safe: false,
5517                format_options: FormatOptions::default(),
5518            };
5519            let arrow_err = cast_with_options(
5520                &string_array.clone(),
5521                &DataType::Interval($interval_unit),
5522                &options,
5523            )
5524            .unwrap_err();
5525            assert_eq!($error_msg, arrow_err.to_string());
5526        };
5527    }
5528
5529    #[test]
5530    fn test_cast_string_to_interval_err() {
5531        test_unsafe_string_to_interval_err!(
5532            vec![Some("foobar")],
5533            IntervalUnit::YearMonth,
5534            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5535        );
5536        test_unsafe_string_to_interval_err!(
5537            vec![Some("foobar")],
5538            IntervalUnit::DayTime,
5539            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5540        );
5541        test_unsafe_string_to_interval_err!(
5542            vec![Some("foobar")],
5543            IntervalUnit::MonthDayNano,
5544            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5545        );
5546        test_unsafe_string_to_interval_err!(
5547            vec![Some("2 months 31 days 1 second")],
5548            IntervalUnit::YearMonth,
5549            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5550        );
5551        test_unsafe_string_to_interval_err!(
5552            vec![Some("1 day 1.5 milliseconds")],
5553            IntervalUnit::DayTime,
5554            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5555        );
5556
5557        // overflow
5558        test_unsafe_string_to_interval_err!(
5559            vec![Some(format!(
5560                "{} century {} year {} month",
5561                i64::MAX - 2,
5562                i64::MAX - 2,
5563                i64::MAX - 2
5564            ))],
5565            IntervalUnit::DayTime,
5566            format!(
5567                "Arithmetic overflow: Overflow happened on: {} * 100",
5568                i64::MAX - 2
5569            )
5570        );
5571        test_unsafe_string_to_interval_err!(
5572            vec![Some(format!(
5573                "{} year {} month {} day",
5574                i64::MAX - 2,
5575                i64::MAX - 2,
5576                i64::MAX - 2
5577            ))],
5578            IntervalUnit::MonthDayNano,
5579            format!(
5580                "Arithmetic overflow: Overflow happened on: {} * 12",
5581                i64::MAX - 2
5582            )
5583        );
5584    }
5585
5586    #[test]
5587    fn test_cast_binary_to_fixed_size_binary() {
5588        let bytes_1 = "Hiiii".as_bytes();
5589        let bytes_2 = "Hello".as_bytes();
5590
5591        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5592        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5593        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5594
5595        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5596        let down_cast = array_ref
5597            .as_any()
5598            .downcast_ref::<FixedSizeBinaryArray>()
5599            .unwrap();
5600        assert_eq!(bytes_1, down_cast.value(0));
5601        assert_eq!(bytes_2, down_cast.value(1));
5602        assert!(down_cast.is_null(2));
5603
5604        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5605        let down_cast = array_ref
5606            .as_any()
5607            .downcast_ref::<FixedSizeBinaryArray>()
5608            .unwrap();
5609        assert_eq!(bytes_1, down_cast.value(0));
5610        assert_eq!(bytes_2, down_cast.value(1));
5611        assert!(down_cast.is_null(2));
5612
5613        // test error cases when the length of binary are not same
5614        let bytes_1 = "Hi".as_bytes();
5615        let bytes_2 = "Hello".as_bytes();
5616
5617        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5618        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5619        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5620
5621        let array_ref = cast_with_options(
5622            &a1,
5623            &DataType::FixedSizeBinary(5),
5624            &CastOptions {
5625                safe: false,
5626                format_options: FormatOptions::default(),
5627            },
5628        );
5629        assert!(array_ref.is_err());
5630
5631        let array_ref = cast_with_options(
5632            &a2,
5633            &DataType::FixedSizeBinary(5),
5634            &CastOptions {
5635                safe: false,
5636                format_options: FormatOptions::default(),
5637            },
5638        );
5639        assert!(array_ref.is_err());
5640    }
5641
5642    #[test]
5643    fn test_fixed_size_binary_to_binary() {
5644        let bytes_1 = "Hiiii".as_bytes();
5645        let bytes_2 = "Hello".as_bytes();
5646
5647        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5648        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5649
5650        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5651        let down_cast = array_ref.as_binary::<i32>();
5652        assert_eq!(bytes_1, down_cast.value(0));
5653        assert_eq!(bytes_2, down_cast.value(1));
5654        assert!(down_cast.is_null(2));
5655
5656        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5657        let down_cast = array_ref.as_binary::<i64>();
5658        assert_eq!(bytes_1, down_cast.value(0));
5659        assert_eq!(bytes_2, down_cast.value(1));
5660        assert!(down_cast.is_null(2));
5661
5662        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5663        let down_cast = array_ref.as_binary_view();
5664        assert_eq!(bytes_1, down_cast.value(0));
5665        assert_eq!(bytes_2, down_cast.value(1));
5666        assert!(down_cast.is_null(2));
5667    }
5668
5669    #[test]
5670    fn test_fixed_size_binary_to_dictionary() {
5671        let bytes_1 = "Hiiii".as_bytes();
5672        let bytes_2 = "Hello".as_bytes();
5673
5674        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5675        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5676
5677        let cast_type = DataType::Dictionary(
5678            Box::new(DataType::Int8),
5679            Box::new(DataType::FixedSizeBinary(5)),
5680        );
5681        let cast_array = cast(&a1, &cast_type).unwrap();
5682        assert_eq!(cast_array.data_type(), &cast_type);
5683        assert_eq!(
5684            array_to_strings(&cast_array),
5685            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5686        );
5687        // dictionary should only have two distinct values
5688        let dict_array = cast_array.as_dictionary::<Int8Type>();
5689        assert_eq!(dict_array.values().len(), 2);
5690    }
5691
5692    #[test]
5693    fn test_binary_to_dictionary() {
5694        let mut builder = GenericBinaryBuilder::<i32>::new();
5695        builder.append_value(b"hello");
5696        builder.append_value(b"hiiii");
5697        builder.append_value(b"hiiii"); // duplicate
5698        builder.append_null();
5699        builder.append_value(b"rustt");
5700
5701        let a1 = builder.finish();
5702
5703        let cast_type = DataType::Dictionary(
5704            Box::new(DataType::Int8),
5705            Box::new(DataType::FixedSizeBinary(5)),
5706        );
5707        let cast_array = cast(&a1, &cast_type).unwrap();
5708        assert_eq!(cast_array.data_type(), &cast_type);
5709        assert_eq!(
5710            array_to_strings(&cast_array),
5711            vec![
5712                "68656c6c6f",
5713                "6869696969",
5714                "6869696969",
5715                "null",
5716                "7275737474"
5717            ]
5718        );
5719        // dictionary should only have three distinct values
5720        let dict_array = cast_array.as_dictionary::<Int8Type>();
5721        assert_eq!(dict_array.values().len(), 3);
5722    }
5723
5724    #[test]
5725    fn test_cast_string_array_to_dict_utf8_view() {
5726        let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5727
5728        let cast_type =
5729            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5730        assert!(can_cast_types(array.data_type(), &cast_type));
5731        let cast_array = cast(&array, &cast_type).unwrap();
5732        assert_eq!(cast_array.data_type(), &cast_type);
5733
5734        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5735        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5736        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5737
5738        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5739        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5740        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5741
5742        let keys = dict_array.keys();
5743        assert!(keys.is_null(1));
5744        assert_eq!(keys.value(0), keys.value(3));
5745        assert_ne!(keys.value(0), keys.value(2));
5746    }
5747
5748    #[test]
5749    fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
5750        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
5751
5752        let cast_type =
5753            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5754        assert!(can_cast_types(array.data_type(), &cast_type));
5755        let cast_array = cast(&array, &cast_type).unwrap();
5756        assert_eq!(cast_array.data_type(), &cast_type);
5757
5758        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5759        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5760        assert_eq!(dict_array.values().len(), 2);
5761
5762        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5763        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5764        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
5765
5766        let keys = dict_array.keys();
5767        assert!(keys.is_null(1));
5768        assert_eq!(keys.value(0), keys.value(3));
5769        assert_ne!(keys.value(0), keys.value(2));
5770    }
5771
5772    #[test]
5773    fn test_cast_string_view_array_to_dict_utf8_view() {
5774        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5775
5776        let cast_type =
5777            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5778        assert!(can_cast_types(array.data_type(), &cast_type));
5779        let cast_array = cast(&array, &cast_type).unwrap();
5780        assert_eq!(cast_array.data_type(), &cast_type);
5781
5782        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5783        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5784        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5785
5786        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5787        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5788        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5789
5790        let keys = dict_array.keys();
5791        assert!(keys.is_null(1));
5792        assert_eq!(keys.value(0), keys.value(3));
5793        assert_ne!(keys.value(0), keys.value(2));
5794    }
5795
5796    #[test]
5797    fn test_cast_string_view_slice_to_dict_utf8_view() {
5798        let array = StringViewArray::from(vec![
5799            Some("zero"),
5800            Some("one"),
5801            None,
5802            Some("three"),
5803            Some("one"),
5804        ]);
5805        let view = array.slice(1, 4);
5806
5807        let cast_type =
5808            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5809        assert!(can_cast_types(view.data_type(), &cast_type));
5810        let cast_array = cast(&view, &cast_type).unwrap();
5811        assert_eq!(cast_array.data_type(), &cast_type);
5812
5813        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5814        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5815        assert_eq!(dict_array.values().len(), 2);
5816
5817        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5818        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5819        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5820
5821        let keys = dict_array.keys();
5822        assert!(keys.is_null(1));
5823        assert_eq!(keys.value(0), keys.value(3));
5824        assert_ne!(keys.value(0), keys.value(2));
5825    }
5826
5827    #[test]
5828    fn test_cast_binary_array_to_dict_binary_view() {
5829        let mut builder = GenericBinaryBuilder::<i32>::new();
5830        builder.append_value(b"hello");
5831        builder.append_value(b"hiiii");
5832        builder.append_value(b"hiiii"); // duplicate
5833        builder.append_null();
5834        builder.append_value(b"rustt");
5835
5836        let array = builder.finish();
5837
5838        let cast_type =
5839            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5840        assert!(can_cast_types(array.data_type(), &cast_type));
5841        let cast_array = cast(&array, &cast_type).unwrap();
5842        assert_eq!(cast_array.data_type(), &cast_type);
5843
5844        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5845        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5846        assert_eq!(dict_array.values().len(), 3);
5847
5848        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5849        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5850        assert_eq!(
5851            actual,
5852            vec![
5853                Some(b"hello".as_slice()),
5854                Some(b"hiiii".as_slice()),
5855                Some(b"hiiii".as_slice()),
5856                None,
5857                Some(b"rustt".as_slice())
5858            ]
5859        );
5860
5861        let keys = dict_array.keys();
5862        assert!(keys.is_null(3));
5863        assert_eq!(keys.value(1), keys.value(2));
5864        assert_ne!(keys.value(0), keys.value(1));
5865    }
5866
5867    #[test]
5868    fn test_cast_binary_view_array_to_dict_binary_view() {
5869        let view = BinaryViewArray::from_iter([
5870            Some(b"hello".as_slice()),
5871            Some(b"hiiii".as_slice()),
5872            Some(b"hiiii".as_slice()), // duplicate
5873            None,
5874            Some(b"rustt".as_slice()),
5875        ]);
5876
5877        let cast_type =
5878            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5879        assert!(can_cast_types(view.data_type(), &cast_type));
5880        let cast_array = cast(&view, &cast_type).unwrap();
5881        assert_eq!(cast_array.data_type(), &cast_type);
5882
5883        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5884        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5885        assert_eq!(dict_array.values().len(), 3);
5886
5887        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5888        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5889        assert_eq!(
5890            actual,
5891            vec![
5892                Some(b"hello".as_slice()),
5893                Some(b"hiiii".as_slice()),
5894                Some(b"hiiii".as_slice()),
5895                None,
5896                Some(b"rustt".as_slice())
5897            ]
5898        );
5899
5900        let keys = dict_array.keys();
5901        assert!(keys.is_null(3));
5902        assert_eq!(keys.value(1), keys.value(2));
5903        assert_ne!(keys.value(0), keys.value(1));
5904    }
5905
5906    #[test]
5907    fn test_cast_binary_view_slice_to_dict_binary_view() {
5908        let view = BinaryViewArray::from_iter([
5909            Some(b"hello".as_slice()),
5910            Some(b"hiiii".as_slice()),
5911            Some(b"hiiii".as_slice()), // duplicate
5912            None,
5913            Some(b"rustt".as_slice()),
5914        ]);
5915        let sliced = view.slice(1, 4);
5916
5917        let cast_type =
5918            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5919        assert!(can_cast_types(sliced.data_type(), &cast_type));
5920        let cast_array = cast(&sliced, &cast_type).unwrap();
5921        assert_eq!(cast_array.data_type(), &cast_type);
5922
5923        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5924        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5925        assert_eq!(dict_array.values().len(), 2);
5926
5927        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5928        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5929        assert_eq!(
5930            actual,
5931            vec![
5932                Some(b"hiiii".as_slice()),
5933                Some(b"hiiii".as_slice()),
5934                None,
5935                Some(b"rustt".as_slice())
5936            ]
5937        );
5938
5939        let keys = dict_array.keys();
5940        assert!(keys.is_null(2));
5941        assert_eq!(keys.value(0), keys.value(1));
5942        assert_ne!(keys.value(0), keys.value(3));
5943    }
5944
5945    #[test]
5946    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
5947        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
5948
5949        let cast_type =
5950            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
5951        assert!(can_cast_types(array.data_type(), &cast_type));
5952        let err = cast(&array, &cast_type).unwrap_err();
5953        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
5954    }
5955
5956    #[test]
5957    fn test_cast_large_string_array_to_dict_utf8_view() {
5958        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5959
5960        let cast_type =
5961            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5962        assert!(can_cast_types(array.data_type(), &cast_type));
5963        let cast_array = cast(&array, &cast_type).unwrap();
5964        assert_eq!(cast_array.data_type(), &cast_type);
5965
5966        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5967        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5968        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5969
5970        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5971        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5972        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5973
5974        let keys = dict_array.keys();
5975        assert!(keys.is_null(1));
5976        assert_eq!(keys.value(0), keys.value(3));
5977        assert_ne!(keys.value(0), keys.value(2));
5978    }
5979
5980    #[test]
5981    fn test_cast_large_binary_array_to_dict_binary_view() {
5982        let mut builder = GenericBinaryBuilder::<i64>::new();
5983        builder.append_value(b"hello");
5984        builder.append_value(b"world");
5985        builder.append_value(b"hello"); // duplicate
5986        builder.append_null();
5987
5988        let array = builder.finish();
5989
5990        let cast_type =
5991            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5992        assert!(can_cast_types(array.data_type(), &cast_type));
5993        let cast_array = cast(&array, &cast_type).unwrap();
5994        assert_eq!(cast_array.data_type(), &cast_type);
5995
5996        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5997        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5998        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
5999
6000        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6001        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6002        assert_eq!(
6003            actual,
6004            vec![
6005                Some(b"hello".as_slice()),
6006                Some(b"world".as_slice()),
6007                Some(b"hello".as_slice()),
6008                None
6009            ]
6010        );
6011
6012        let keys = dict_array.keys();
6013        assert!(keys.is_null(3));
6014        assert_eq!(keys.value(0), keys.value(2));
6015        assert_ne!(keys.value(0), keys.value(1));
6016    }
6017
6018    #[test]
6019    fn test_cast_empty_string_array_to_dict_utf8_view() {
6020        let array = StringArray::from(Vec::<Option<&str>>::new());
6021
6022        let cast_type =
6023            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6024        assert!(can_cast_types(array.data_type(), &cast_type));
6025        let cast_array = cast(&array, &cast_type).unwrap();
6026        assert_eq!(cast_array.data_type(), &cast_type);
6027        assert_eq!(cast_array.len(), 0);
6028    }
6029
6030    #[test]
6031    fn test_cast_empty_binary_array_to_dict_binary_view() {
6032        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6033
6034        let cast_type =
6035            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6036        assert!(can_cast_types(array.data_type(), &cast_type));
6037        let cast_array = cast(&array, &cast_type).unwrap();
6038        assert_eq!(cast_array.data_type(), &cast_type);
6039        assert_eq!(cast_array.len(), 0);
6040    }
6041
6042    #[test]
6043    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6044        let array = StringArray::from(vec![None::<&str>, None, None]);
6045
6046        let cast_type =
6047            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6048        assert!(can_cast_types(array.data_type(), &cast_type));
6049        let cast_array = cast(&array, &cast_type).unwrap();
6050        assert_eq!(cast_array.data_type(), &cast_type);
6051        assert_eq!(cast_array.null_count(), 3);
6052
6053        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6054        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6055        assert_eq!(dict_array.values().len(), 0);
6056        assert_eq!(dict_array.keys().null_count(), 3);
6057
6058        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6059        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6060        assert_eq!(actual, vec![None, None, None]);
6061    }
6062
6063    #[test]
6064    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6065        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6066
6067        let cast_type =
6068            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
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::BinaryView);
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::<BinaryViewArray>().unwrap();
6080        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6081        assert_eq!(actual, vec![None, None, None]);
6082    }
6083
6084    #[test]
6085    fn test_numeric_to_binary() {
6086        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6087
6088        let array_ref = cast(&a, &DataType::Binary).unwrap();
6089        let down_cast = array_ref.as_binary::<i32>();
6090        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6091        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6092        assert!(down_cast.is_null(2));
6093
6094        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6095
6096        let array_ref = cast(&a, &DataType::Binary).unwrap();
6097        let down_cast = array_ref.as_binary::<i32>();
6098        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6099        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6100        assert!(down_cast.is_null(2));
6101    }
6102
6103    #[test]
6104    fn test_numeric_to_large_binary() {
6105        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6106
6107        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6108        let down_cast = array_ref.as_binary::<i64>();
6109        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6110        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6111        assert!(down_cast.is_null(2));
6112
6113        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6114
6115        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6116        let down_cast = array_ref.as_binary::<i64>();
6117        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6118        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6119        assert!(down_cast.is_null(2));
6120    }
6121
6122    #[test]
6123    fn test_cast_date32_to_int32() {
6124        let array = Date32Array::from(vec![10000, 17890]);
6125        let b = cast(&array, &DataType::Int32).unwrap();
6126        let c = b.as_primitive::<Int32Type>();
6127        assert_eq!(10000, c.value(0));
6128        assert_eq!(17890, c.value(1));
6129    }
6130
6131    #[test]
6132    fn test_cast_int32_to_date32() {
6133        let array = Int32Array::from(vec![10000, 17890]);
6134        let b = cast(&array, &DataType::Date32).unwrap();
6135        let c = b.as_primitive::<Date32Type>();
6136        assert_eq!(10000, c.value(0));
6137        assert_eq!(17890, c.value(1));
6138    }
6139
6140    #[test]
6141    fn test_cast_timestamp_to_date32() {
6142        let array =
6143            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6144                .with_timezone("+00:00".to_string());
6145        let b = cast(&array, &DataType::Date32).unwrap();
6146        let c = b.as_primitive::<Date32Type>();
6147        assert_eq!(10000, c.value(0));
6148        assert_eq!(17890, c.value(1));
6149        assert!(c.is_null(2));
6150    }
6151    #[test]
6152    fn test_cast_timestamp_to_date32_zone() {
6153        let strings = StringArray::from_iter([
6154            Some("1970-01-01T00:00:01"),
6155            Some("1970-01-01T23:59:59"),
6156            None,
6157            Some("2020-03-01T02:00:23+00:00"),
6158        ]);
6159        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6160        let timestamps = cast(&strings, &dt).unwrap();
6161        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6162
6163        let c = dates.as_primitive::<Date32Type>();
6164        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6165        assert_eq!(c.value_as_date(0).unwrap(), expected);
6166        assert_eq!(c.value_as_date(1).unwrap(), expected);
6167        assert!(c.is_null(2));
6168        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6169        assert_eq!(c.value_as_date(3).unwrap(), expected);
6170    }
6171    #[test]
6172    fn test_cast_timestamp_to_date64() {
6173        let array =
6174            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6175        let b = cast(&array, &DataType::Date64).unwrap();
6176        let c = b.as_primitive::<Date64Type>();
6177        assert_eq!(864000000005, c.value(0));
6178        assert_eq!(1545696000001, c.value(1));
6179        assert!(c.is_null(2));
6180
6181        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6182        let b = cast(&array, &DataType::Date64).unwrap();
6183        let c = b.as_primitive::<Date64Type>();
6184        assert_eq!(864000000005000, c.value(0));
6185        assert_eq!(1545696000001000, c.value(1));
6186
6187        // test overflow, safe cast
6188        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6189        let b = cast(&array, &DataType::Date64).unwrap();
6190        assert!(b.is_null(0));
6191        // test overflow, unsafe cast
6192        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6193        let options = CastOptions {
6194            safe: false,
6195            format_options: FormatOptions::default(),
6196        };
6197        let b = cast_with_options(&array, &DataType::Date64, &options);
6198        assert!(b.is_err());
6199    }
6200
6201    #[test]
6202    fn test_cast_timestamp_to_time64() {
6203        // test timestamp secs
6204        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6205            .with_timezone("+01:00".to_string());
6206        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6207        let c = b.as_primitive::<Time64MicrosecondType>();
6208        assert_eq!(3605000000, c.value(0));
6209        assert_eq!(3601000000, c.value(1));
6210        assert!(c.is_null(2));
6211        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6212        let c = b.as_primitive::<Time64NanosecondType>();
6213        assert_eq!(3605000000000, c.value(0));
6214        assert_eq!(3601000000000, c.value(1));
6215        assert!(c.is_null(2));
6216
6217        // test timestamp milliseconds
6218        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6219            .with_timezone("+01:00".to_string());
6220        let array = Arc::new(a) as ArrayRef;
6221        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6222        let c = b.as_primitive::<Time64MicrosecondType>();
6223        assert_eq!(3605000000, c.value(0));
6224        assert_eq!(3601000000, c.value(1));
6225        assert!(c.is_null(2));
6226        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6227        let c = b.as_primitive::<Time64NanosecondType>();
6228        assert_eq!(3605000000000, c.value(0));
6229        assert_eq!(3601000000000, c.value(1));
6230        assert!(c.is_null(2));
6231
6232        // test timestamp microseconds
6233        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6234            .with_timezone("+01:00".to_string());
6235        let array = Arc::new(a) as ArrayRef;
6236        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6237        let c = b.as_primitive::<Time64MicrosecondType>();
6238        assert_eq!(3605000000, c.value(0));
6239        assert_eq!(3601000000, c.value(1));
6240        assert!(c.is_null(2));
6241        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6242        let c = b.as_primitive::<Time64NanosecondType>();
6243        assert_eq!(3605000000000, c.value(0));
6244        assert_eq!(3601000000000, c.value(1));
6245        assert!(c.is_null(2));
6246
6247        // test timestamp nanoseconds
6248        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6249            .with_timezone("+01:00".to_string());
6250        let array = Arc::new(a) as ArrayRef;
6251        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6252        let c = b.as_primitive::<Time64MicrosecondType>();
6253        assert_eq!(3605000000, c.value(0));
6254        assert_eq!(3601000000, c.value(1));
6255        assert!(c.is_null(2));
6256        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6257        let c = b.as_primitive::<Time64NanosecondType>();
6258        assert_eq!(3605000000000, c.value(0));
6259        assert_eq!(3601000000000, c.value(1));
6260        assert!(c.is_null(2));
6261
6262        // test overflow
6263        let a =
6264            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6265        let array = Arc::new(a) as ArrayRef;
6266        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6267        assert!(b.is_err());
6268        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6269        assert!(b.is_err());
6270        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6271        assert!(b.is_err());
6272    }
6273
6274    #[test]
6275    fn test_cast_timestamp_to_time32() {
6276        // test timestamp secs
6277        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6278            .with_timezone("+01:00".to_string());
6279        let array = Arc::new(a) as ArrayRef;
6280        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6281        let c = b.as_primitive::<Time32SecondType>();
6282        assert_eq!(3605, c.value(0));
6283        assert_eq!(3601, c.value(1));
6284        assert!(c.is_null(2));
6285        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6286        let c = b.as_primitive::<Time32MillisecondType>();
6287        assert_eq!(3605000, c.value(0));
6288        assert_eq!(3601000, c.value(1));
6289        assert!(c.is_null(2));
6290
6291        // test timestamp milliseconds
6292        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6293            .with_timezone("+01:00".to_string());
6294        let array = Arc::new(a) as ArrayRef;
6295        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6296        let c = b.as_primitive::<Time32SecondType>();
6297        assert_eq!(3605, c.value(0));
6298        assert_eq!(3601, c.value(1));
6299        assert!(c.is_null(2));
6300        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6301        let c = b.as_primitive::<Time32MillisecondType>();
6302        assert_eq!(3605000, c.value(0));
6303        assert_eq!(3601000, c.value(1));
6304        assert!(c.is_null(2));
6305
6306        // test timestamp microseconds
6307        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6308            .with_timezone("+01:00".to_string());
6309        let array = Arc::new(a) as ArrayRef;
6310        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6311        let c = b.as_primitive::<Time32SecondType>();
6312        assert_eq!(3605, c.value(0));
6313        assert_eq!(3601, c.value(1));
6314        assert!(c.is_null(2));
6315        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6316        let c = b.as_primitive::<Time32MillisecondType>();
6317        assert_eq!(3605000, c.value(0));
6318        assert_eq!(3601000, c.value(1));
6319        assert!(c.is_null(2));
6320
6321        // test timestamp nanoseconds
6322        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6323            .with_timezone("+01:00".to_string());
6324        let array = Arc::new(a) as ArrayRef;
6325        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6326        let c = b.as_primitive::<Time32SecondType>();
6327        assert_eq!(3605, c.value(0));
6328        assert_eq!(3601, c.value(1));
6329        assert!(c.is_null(2));
6330        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6331        let c = b.as_primitive::<Time32MillisecondType>();
6332        assert_eq!(3605000, c.value(0));
6333        assert_eq!(3601000, c.value(1));
6334        assert!(c.is_null(2));
6335
6336        // test overflow
6337        let a =
6338            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6339        let array = Arc::new(a) as ArrayRef;
6340        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6341        assert!(b.is_err());
6342        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6343        assert!(b.is_err());
6344    }
6345
6346    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6347    #[test]
6348    fn test_cast_timestamp_with_timezone_1() {
6349        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6350            Some("2000-01-01T00:00:00.123456789"),
6351            Some("2010-01-01T00:00:00.123456789"),
6352            None,
6353        ]));
6354        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6355        let timestamp_array = cast(&string_array, &to_type).unwrap();
6356
6357        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6358        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6359
6360        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6361        let result = string_array.as_string::<i32>();
6362        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6363        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6364        assert!(result.is_null(2));
6365    }
6366
6367    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6368    #[test]
6369    fn test_cast_timestamp_with_timezone_2() {
6370        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6371            Some("2000-01-01T07:00:00.123456789"),
6372            Some("2010-01-01T07:00:00.123456789"),
6373            None,
6374        ]));
6375        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6376        let timestamp_array = cast(&string_array, &to_type).unwrap();
6377
6378        // Check intermediate representation is correct
6379        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6380        let result = string_array.as_string::<i32>();
6381        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6382        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6383        assert!(result.is_null(2));
6384
6385        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6386        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6387
6388        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6389        let result = string_array.as_string::<i32>();
6390        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6391        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6392        assert!(result.is_null(2));
6393    }
6394
6395    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6396    #[test]
6397    fn test_cast_timestamp_with_timezone_3() {
6398        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6399            Some("2000-01-01T07:00:00.123456789"),
6400            Some("2010-01-01T07:00:00.123456789"),
6401            None,
6402        ]));
6403        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6404        let timestamp_array = cast(&string_array, &to_type).unwrap();
6405
6406        // Check intermediate representation is correct
6407        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6408        let result = string_array.as_string::<i32>();
6409        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6410        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6411        assert!(result.is_null(2));
6412
6413        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6414        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6415
6416        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6417        let result = string_array.as_string::<i32>();
6418        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6419        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6420        assert!(result.is_null(2));
6421    }
6422
6423    #[test]
6424    fn test_cast_date64_to_timestamp() {
6425        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6426        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6427        let c = b.as_primitive::<TimestampSecondType>();
6428        assert_eq!(864000000, c.value(0));
6429        assert_eq!(1545696000, c.value(1));
6430        assert!(c.is_null(2));
6431    }
6432
6433    #[test]
6434    fn test_cast_date64_to_timestamp_ms() {
6435        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6436        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6437        let c = b
6438            .as_any()
6439            .downcast_ref::<TimestampMillisecondArray>()
6440            .unwrap();
6441        assert_eq!(864000000005, c.value(0));
6442        assert_eq!(1545696000001, c.value(1));
6443        assert!(c.is_null(2));
6444    }
6445
6446    #[test]
6447    fn test_cast_date64_to_timestamp_us() {
6448        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6449        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6450        let c = b
6451            .as_any()
6452            .downcast_ref::<TimestampMicrosecondArray>()
6453            .unwrap();
6454        assert_eq!(864000000005000, c.value(0));
6455        assert_eq!(1545696000001000, c.value(1));
6456        assert!(c.is_null(2));
6457    }
6458
6459    #[test]
6460    fn test_cast_date64_to_timestamp_ns() {
6461        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6462        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6463        let c = b
6464            .as_any()
6465            .downcast_ref::<TimestampNanosecondArray>()
6466            .unwrap();
6467        assert_eq!(864000000005000000, c.value(0));
6468        assert_eq!(1545696000001000000, c.value(1));
6469        assert!(c.is_null(2));
6470    }
6471
6472    #[test]
6473    fn test_cast_timestamp_to_i64() {
6474        let array =
6475            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6476                .with_timezone("UTC".to_string());
6477        let b = cast(&array, &DataType::Int64).unwrap();
6478        let c = b.as_primitive::<Int64Type>();
6479        assert_eq!(&DataType::Int64, c.data_type());
6480        assert_eq!(864000000005, c.value(0));
6481        assert_eq!(1545696000001, c.value(1));
6482        assert!(c.is_null(2));
6483    }
6484
6485    macro_rules! assert_cast {
6486        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6487            assert!(can_cast_types($array.data_type(), &$datatype));
6488            let out = cast(&$array, &$datatype).unwrap();
6489            let actual = out
6490                .as_any()
6491                .downcast_ref::<$output_array_type>()
6492                .unwrap()
6493                .into_iter()
6494                .collect::<Vec<_>>();
6495            assert_eq!(actual, $expected);
6496        }};
6497        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6498            assert!(can_cast_types($array.data_type(), &$datatype));
6499            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6500            let actual = out
6501                .as_any()
6502                .downcast_ref::<$output_array_type>()
6503                .unwrap()
6504                .into_iter()
6505                .collect::<Vec<_>>();
6506            assert_eq!(actual, $expected);
6507        }};
6508    }
6509
6510    #[test]
6511    fn test_cast_date32_to_string() {
6512        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6513        let expected = vec![
6514            Some("1970-01-01"),
6515            Some("1997-05-19"),
6516            Some("2005-09-10"),
6517            Some("2018-12-25"),
6518            None,
6519        ];
6520
6521        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6522        assert_cast!(array, DataType::Utf8, StringArray, expected);
6523        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6524    }
6525
6526    #[test]
6527    fn test_cast_date64_to_string() {
6528        let array = Date64Array::from(vec![
6529            Some(0),
6530            Some(10000 * 86400000),
6531            Some(13036 * 86400000),
6532            Some(17890 * 86400000),
6533            None,
6534        ]);
6535        let expected = vec![
6536            Some("1970-01-01T00:00:00"),
6537            Some("1997-05-19T00:00:00"),
6538            Some("2005-09-10T00:00:00"),
6539            Some("2018-12-25T00:00:00"),
6540            None,
6541        ];
6542
6543        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6544        assert_cast!(array, DataType::Utf8, StringArray, expected);
6545        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6546    }
6547
6548    #[test]
6549    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6550        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6551        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6552        let array = Arc::new(a) as ArrayRef;
6553
6554        let b = cast(
6555            &array,
6556            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6557        )
6558        .unwrap();
6559        let c = b.as_primitive::<TimestampSecondType>();
6560        let string_array = cast(&c, &DataType::Utf8).unwrap();
6561        let result = string_array.as_string::<i32>();
6562        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6563
6564        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6565        let c = b.as_primitive::<TimestampSecondType>();
6566        let string_array = cast(&c, &DataType::Utf8).unwrap();
6567        let result = string_array.as_string::<i32>();
6568        assert_eq!("2021-01-01T00:00:00", result.value(0));
6569    }
6570
6571    #[test]
6572    fn test_cast_date32_to_timestamp_with_timezone() {
6573        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6574        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6575        let array = Arc::new(a) as ArrayRef;
6576        let b = cast(
6577            &array,
6578            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6579        )
6580        .unwrap();
6581        let c = b.as_primitive::<TimestampSecondType>();
6582        assert_eq!(1609438500, c.value(0));
6583        assert_eq!(1640974500, c.value(1));
6584        assert!(c.is_null(2));
6585
6586        let string_array = cast(&c, &DataType::Utf8).unwrap();
6587        let result = string_array.as_string::<i32>();
6588        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6589        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6590    }
6591
6592    #[test]
6593    fn test_cast_date32_to_timestamp_with_timezone_ms() {
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::Millisecond, Some(tz.into())),
6600        )
6601        .unwrap();
6602        let c = b.as_primitive::<TimestampMillisecondType>();
6603        assert_eq!(1609438500000, c.value(0));
6604        assert_eq!(1640974500000, 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_us() {
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::Microsecond, Some(tz.into())),
6621        )
6622        .unwrap();
6623        let c = b.as_primitive::<TimestampMicrosecondType>();
6624        assert_eq!(1609438500000000, c.value(0));
6625        assert_eq!(1640974500000000, 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_ns() {
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::Nanosecond, Some(tz.into())),
6642        )
6643        .unwrap();
6644        let c = b.as_primitive::<TimestampNanosecondType>();
6645        assert_eq!(1609438500000000000, c.value(0));
6646        assert_eq!(1640974500000000000, 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_date64_to_timestamp_with_timezone() {
6657        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6658        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6659        let b = cast(
6660            &array,
6661            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6662        )
6663        .unwrap();
6664
6665        let c = b.as_primitive::<TimestampSecondType>();
6666        assert_eq!(863979300, c.value(0));
6667        assert_eq!(1545675300, 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!("1997-05-19T00:00:00+05:45", result.value(0));
6673        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6674    }
6675
6676    #[test]
6677    fn test_cast_date64_to_timestamp_with_timezone_ms() {
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::Millisecond, Some(tz.into())),
6683        )
6684        .unwrap();
6685
6686        let c = b.as_primitive::<TimestampMillisecondType>();
6687        assert_eq!(863979300005, c.value(0));
6688        assert_eq!(1545675300001, 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.005+05:45", result.value(0));
6694        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6695    }
6696
6697    #[test]
6698    fn test_cast_date64_to_timestamp_with_timezone_us() {
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::Microsecond, Some(tz.into())),
6704        )
6705        .unwrap();
6706
6707        let c = b.as_primitive::<TimestampMicrosecondType>();
6708        assert_eq!(863979300005000, c.value(0));
6709        assert_eq!(1545675300001000, 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_ns() {
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::Nanosecond, Some(tz.into())),
6725        )
6726        .unwrap();
6727
6728        let c = b.as_primitive::<TimestampNanosecondType>();
6729        assert_eq!(863979300005000000, c.value(0));
6730        assert_eq!(1545675300001000000, 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_timestamp_to_strings() {
6741        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6742        let array =
6743            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6744        let expected = vec![
6745            Some("1997-05-19T00:00:03.005"),
6746            Some("2018-12-25T00:00:02.001"),
6747            None,
6748        ];
6749
6750        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6751        assert_cast!(array, DataType::Utf8, StringArray, expected);
6752        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6753    }
6754
6755    #[test]
6756    fn test_cast_timestamp_to_strings_opt() {
6757        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6758        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6759        let cast_options = CastOptions {
6760            safe: true,
6761            format_options: FormatOptions::default()
6762                .with_timestamp_format(Some(ts_format))
6763                .with_timestamp_tz_format(Some(ts_format)),
6764        };
6765
6766        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6767        let array_without_tz =
6768            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6769        let expected = vec![
6770            Some("1997-05-19 00:00:03.005000"),
6771            Some("2018-12-25 00:00:02.001000"),
6772            None,
6773        ];
6774        assert_cast!(
6775            array_without_tz,
6776            DataType::Utf8View,
6777            StringViewArray,
6778            cast_options,
6779            expected
6780        );
6781        assert_cast!(
6782            array_without_tz,
6783            DataType::Utf8,
6784            StringArray,
6785            cast_options,
6786            expected
6787        );
6788        assert_cast!(
6789            array_without_tz,
6790            DataType::LargeUtf8,
6791            LargeStringArray,
6792            cast_options,
6793            expected
6794        );
6795
6796        let array_with_tz =
6797            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6798                .with_timezone(tz.to_string());
6799        let expected = vec![
6800            Some("1997-05-19 05:45:03.005000"),
6801            Some("2018-12-25 05:45:02.001000"),
6802            None,
6803        ];
6804        assert_cast!(
6805            array_with_tz,
6806            DataType::Utf8View,
6807            StringViewArray,
6808            cast_options,
6809            expected
6810        );
6811        assert_cast!(
6812            array_with_tz,
6813            DataType::Utf8,
6814            StringArray,
6815            cast_options,
6816            expected
6817        );
6818        assert_cast!(
6819            array_with_tz,
6820            DataType::LargeUtf8,
6821            LargeStringArray,
6822            cast_options,
6823            expected
6824        );
6825    }
6826
6827    #[test]
6828    fn test_cast_between_timestamps() {
6829        let array =
6830            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6831        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6832        let c = b.as_primitive::<TimestampSecondType>();
6833        assert_eq!(864000003, c.value(0));
6834        assert_eq!(1545696002, c.value(1));
6835        assert!(c.is_null(2));
6836    }
6837
6838    #[test]
6839    fn test_cast_duration_to_i64() {
6840        let base = vec![5, 6, 7, 8, 100000000];
6841
6842        let duration_arrays = vec![
6843            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6844            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6845            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6846            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6847        ];
6848
6849        for arr in duration_arrays {
6850            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6851            let result = cast(&arr, &DataType::Int64).unwrap();
6852            let result = result.as_primitive::<Int64Type>();
6853            assert_eq!(base.as_slice(), result.values());
6854        }
6855    }
6856
6857    #[test]
6858    fn test_cast_between_durations_and_numerics() {
6859        fn test_cast_between_durations<FromType, ToType>()
6860        where
6861            FromType: ArrowPrimitiveType<Native = i64>,
6862            ToType: ArrowPrimitiveType<Native = i64>,
6863            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6864        {
6865            let from_unit = match FromType::DATA_TYPE {
6866                DataType::Duration(unit) => unit,
6867                _ => panic!("Expected a duration type"),
6868            };
6869            let to_unit = match ToType::DATA_TYPE {
6870                DataType::Duration(unit) => unit,
6871                _ => panic!("Expected a duration type"),
6872            };
6873            let from_size = time_unit_multiple(&from_unit);
6874            let to_size = time_unit_multiple(&to_unit);
6875
6876            let (v1_before, v2_before) = (8640003005, 1696002001);
6877            let (v1_after, v2_after) = if from_size >= to_size {
6878                (
6879                    v1_before / (from_size / to_size),
6880                    v2_before / (from_size / to_size),
6881                )
6882            } else {
6883                (
6884                    v1_before * (to_size / from_size),
6885                    v2_before * (to_size / from_size),
6886                )
6887            };
6888
6889            let array =
6890                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6891            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6892            let c = b.as_primitive::<ToType>();
6893            assert_eq!(v1_after, c.value(0));
6894            assert_eq!(v2_after, c.value(1));
6895            assert!(c.is_null(2));
6896        }
6897
6898        // between each individual duration type
6899        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6900        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6901        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6902        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6903        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6904        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6905        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6906        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6907        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6908        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6909        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6910        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6911
6912        // cast failed
6913        let array = DurationSecondArray::from(vec![
6914            Some(i64::MAX),
6915            Some(8640203410378005),
6916            Some(10241096),
6917            None,
6918        ]);
6919        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6920        let c = b.as_primitive::<DurationNanosecondType>();
6921        assert!(c.is_null(0));
6922        assert!(c.is_null(1));
6923        assert_eq!(10241096000000000, c.value(2));
6924        assert!(c.is_null(3));
6925
6926        // durations to numerics
6927        let array = DurationSecondArray::from(vec![
6928            Some(i64::MAX),
6929            Some(8640203410378005),
6930            Some(10241096),
6931            None,
6932        ]);
6933        let b = cast(&array, &DataType::Int64).unwrap();
6934        let c = b.as_primitive::<Int64Type>();
6935        assert_eq!(i64::MAX, c.value(0));
6936        assert_eq!(8640203410378005, c.value(1));
6937        assert_eq!(10241096, c.value(2));
6938        assert!(c.is_null(3));
6939
6940        let b = cast(&array, &DataType::Int32).unwrap();
6941        let c = b.as_primitive::<Int32Type>();
6942        assert_eq!(0, c.value(0));
6943        assert_eq!(0, c.value(1));
6944        assert_eq!(10241096, c.value(2));
6945        assert!(c.is_null(3));
6946
6947        // numerics to durations
6948        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6949        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6950        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6951        assert_eq!(i32::MAX as i64, c.value(0));
6952        assert_eq!(802034103, c.value(1));
6953        assert_eq!(10241096, c.value(2));
6954        assert!(c.is_null(3));
6955    }
6956
6957    #[test]
6958    fn test_cast_to_strings() {
6959        let a = Int32Array::from(vec![1, 2, 3]);
6960        let out = cast(&a, &DataType::Utf8).unwrap();
6961        let out = out
6962            .as_any()
6963            .downcast_ref::<StringArray>()
6964            .unwrap()
6965            .into_iter()
6966            .collect::<Vec<_>>();
6967        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6968        let out = cast(&a, &DataType::LargeUtf8).unwrap();
6969        let out = out
6970            .as_any()
6971            .downcast_ref::<LargeStringArray>()
6972            .unwrap()
6973            .into_iter()
6974            .collect::<Vec<_>>();
6975        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6976    }
6977
6978    #[test]
6979    fn test_str_to_str_casts() {
6980        for data in [
6981            vec![Some("foo"), Some("bar"), Some("ham")],
6982            vec![Some("foo"), None, Some("bar")],
6983        ] {
6984            let a = LargeStringArray::from(data.clone());
6985            let to = cast(&a, &DataType::Utf8).unwrap();
6986            let expect = a
6987                .as_any()
6988                .downcast_ref::<LargeStringArray>()
6989                .unwrap()
6990                .into_iter()
6991                .collect::<Vec<_>>();
6992            let out = to
6993                .as_any()
6994                .downcast_ref::<StringArray>()
6995                .unwrap()
6996                .into_iter()
6997                .collect::<Vec<_>>();
6998            assert_eq!(expect, out);
6999
7000            let a = StringArray::from(data);
7001            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7002            let expect = a
7003                .as_any()
7004                .downcast_ref::<StringArray>()
7005                .unwrap()
7006                .into_iter()
7007                .collect::<Vec<_>>();
7008            let out = to
7009                .as_any()
7010                .downcast_ref::<LargeStringArray>()
7011                .unwrap()
7012                .into_iter()
7013                .collect::<Vec<_>>();
7014            assert_eq!(expect, out);
7015        }
7016    }
7017
7018    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7019        Some("hello"),
7020        Some("repeated"),
7021        None,
7022        Some("large payload over 12 bytes"),
7023        Some("repeated"),
7024    ];
7025
7026    #[test]
7027    fn test_string_view_to_binary_view() {
7028        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7029
7030        assert!(can_cast_types(
7031            string_view_array.data_type(),
7032            &DataType::BinaryView
7033        ));
7034
7035        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7036        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7037
7038        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7039        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7040    }
7041
7042    #[test]
7043    fn test_binary_view_to_string_view() {
7044        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7045
7046        assert!(can_cast_types(
7047            binary_view_array.data_type(),
7048            &DataType::Utf8View
7049        ));
7050
7051        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7052        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7053
7054        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7055        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7056    }
7057
7058    #[test]
7059    fn test_binary_view_to_string_view_with_invalid_utf8() {
7060        let binary_view_array = BinaryViewArray::from_iter(vec![
7061            Some("valid".as_bytes()),
7062            Some(&[0xff]),
7063            Some("utf8".as_bytes()),
7064            None,
7065        ]);
7066
7067        let strict_options = CastOptions {
7068            safe: false,
7069            ..Default::default()
7070        };
7071
7072        assert!(
7073            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7074        );
7075
7076        let safe_options = CastOptions {
7077            safe: true,
7078            ..Default::default()
7079        };
7080
7081        let string_view_array =
7082            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7083        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7084
7085        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7086
7087        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7088    }
7089
7090    #[test]
7091    fn test_string_to_view() {
7092        _test_string_to_view::<i32>();
7093        _test_string_to_view::<i64>();
7094    }
7095
7096    fn _test_string_to_view<O>()
7097    where
7098        O: OffsetSizeTrait,
7099    {
7100        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7101
7102        assert!(can_cast_types(
7103            string_array.data_type(),
7104            &DataType::Utf8View
7105        ));
7106
7107        assert!(can_cast_types(
7108            string_array.data_type(),
7109            &DataType::BinaryView
7110        ));
7111
7112        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7113        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7114
7115        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7116        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7117
7118        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7119        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7120
7121        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7122        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7123    }
7124
7125    #[test]
7126    fn test_bianry_to_view() {
7127        _test_binary_to_view::<i32>();
7128        _test_binary_to_view::<i64>();
7129    }
7130
7131    fn _test_binary_to_view<O>()
7132    where
7133        O: OffsetSizeTrait,
7134    {
7135        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7136
7137        assert!(can_cast_types(
7138            binary_array.data_type(),
7139            &DataType::Utf8View
7140        ));
7141
7142        assert!(can_cast_types(
7143            binary_array.data_type(),
7144            &DataType::BinaryView
7145        ));
7146
7147        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7148        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7149
7150        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7151        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7152
7153        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7154        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7155
7156        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7157        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7158    }
7159
7160    #[test]
7161    fn test_dict_to_view() {
7162        let values = StringArray::from_iter(VIEW_TEST_DATA);
7163        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7164        let string_dict_array =
7165            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7166        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7167
7168        let string_view_array = {
7169            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7170            for v in typed_dict.into_iter() {
7171                builder.append_option(v);
7172            }
7173            builder.finish()
7174        };
7175        let expected_string_array_type = string_view_array.data_type();
7176        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7177        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7178        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7179
7180        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7181        let binary_dict_array =
7182            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7183        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7184
7185        let binary_view_array = {
7186            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7187            for v in typed_binary_dict.into_iter() {
7188                builder.append_option(v);
7189            }
7190            builder.finish()
7191        };
7192        let expected_binary_array_type = binary_view_array.data_type();
7193        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7194        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7195        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7196    }
7197
7198    #[test]
7199    fn test_view_to_dict() {
7200        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7201        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7202        let casted_type = string_dict_array.data_type();
7203        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7204        assert_eq!(casted_dict_array.data_type(), casted_type);
7205        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7206
7207        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7208        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7209        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7210        let binary_dict_array =
7211            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7212        let casted_type = binary_dict_array.data_type();
7213        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7214        assert_eq!(casted_binary_array.data_type(), casted_type);
7215        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7216    }
7217
7218    #[test]
7219    fn test_view_to_string() {
7220        _test_view_to_string::<i32>();
7221        _test_view_to_string::<i64>();
7222    }
7223
7224    fn _test_view_to_string<O>()
7225    where
7226        O: OffsetSizeTrait,
7227    {
7228        let string_view_array = {
7229            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7230            for s in VIEW_TEST_DATA.iter() {
7231                builder.append_option(*s);
7232            }
7233            builder.finish()
7234        };
7235
7236        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7237
7238        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7239        let expected_type = expected_string_array.data_type();
7240
7241        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7242        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7243
7244        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7245        assert_eq!(string_view_casted_array.data_type(), expected_type);
7246        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7247
7248        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7249        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7250        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7251    }
7252
7253    #[test]
7254    fn test_view_to_binary() {
7255        _test_view_to_binary::<i32>();
7256        _test_view_to_binary::<i64>();
7257    }
7258
7259    fn _test_view_to_binary<O>()
7260    where
7261        O: OffsetSizeTrait,
7262    {
7263        let view_array = {
7264            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7265            for s in VIEW_TEST_DATA.iter() {
7266                builder.append_option(*s);
7267            }
7268            builder.finish()
7269        };
7270
7271        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7272        let expected_type = expected_binary_array.data_type();
7273
7274        assert!(can_cast_types(view_array.data_type(), expected_type));
7275
7276        let binary_array = cast(&view_array, expected_type).unwrap();
7277        assert_eq!(binary_array.data_type(), expected_type);
7278
7279        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7280    }
7281
7282    #[test]
7283    fn test_cast_from_f64() {
7284        let f64_values: Vec<f64> = vec![
7285            i64::MIN as f64,
7286            i32::MIN as f64,
7287            i16::MIN as f64,
7288            i8::MIN as f64,
7289            0_f64,
7290            u8::MAX as f64,
7291            u16::MAX as f64,
7292            u32::MAX as f64,
7293            u64::MAX as f64,
7294        ];
7295        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7296
7297        let f64_expected = vec![
7298            -9223372036854776000.0,
7299            -2147483648.0,
7300            -32768.0,
7301            -128.0,
7302            0.0,
7303            255.0,
7304            65535.0,
7305            4294967295.0,
7306            18446744073709552000.0,
7307        ];
7308        assert_eq!(
7309            f64_expected,
7310            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7311                .iter()
7312                .map(|i| i.parse::<f64>().unwrap())
7313                .collect::<Vec<f64>>()
7314        );
7315
7316        let f32_expected = vec![
7317            -9223372000000000000.0,
7318            -2147483600.0,
7319            -32768.0,
7320            -128.0,
7321            0.0,
7322            255.0,
7323            65535.0,
7324            4294967300.0,
7325            18446744000000000000.0,
7326        ];
7327        assert_eq!(
7328            f32_expected,
7329            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7330                .iter()
7331                .map(|i| i.parse::<f32>().unwrap())
7332                .collect::<Vec<f32>>()
7333        );
7334
7335        let f16_expected = vec![
7336            f16::from_f64(-9223372000000000000.0),
7337            f16::from_f64(-2147483600.0),
7338            f16::from_f64(-32768.0),
7339            f16::from_f64(-128.0),
7340            f16::from_f64(0.0),
7341            f16::from_f64(255.0),
7342            f16::from_f64(65535.0),
7343            f16::from_f64(4294967300.0),
7344            f16::from_f64(18446744000000000000.0),
7345        ];
7346        assert_eq!(
7347            f16_expected,
7348            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7349                .iter()
7350                .map(|i| i.parse::<f16>().unwrap())
7351                .collect::<Vec<f16>>()
7352        );
7353
7354        let i64_expected = vec![
7355            "-9223372036854775808",
7356            "-2147483648",
7357            "-32768",
7358            "-128",
7359            "0",
7360            "255",
7361            "65535",
7362            "4294967295",
7363            "null",
7364        ];
7365        assert_eq!(
7366            i64_expected,
7367            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7368        );
7369
7370        let i32_expected = vec![
7371            "null",
7372            "-2147483648",
7373            "-32768",
7374            "-128",
7375            "0",
7376            "255",
7377            "65535",
7378            "null",
7379            "null",
7380        ];
7381        assert_eq!(
7382            i32_expected,
7383            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7384        );
7385
7386        let i16_expected = vec![
7387            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7388        ];
7389        assert_eq!(
7390            i16_expected,
7391            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7392        );
7393
7394        let i8_expected = vec![
7395            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7396        ];
7397        assert_eq!(
7398            i8_expected,
7399            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7400        );
7401
7402        let u64_expected = vec![
7403            "null",
7404            "null",
7405            "null",
7406            "null",
7407            "0",
7408            "255",
7409            "65535",
7410            "4294967295",
7411            "null",
7412        ];
7413        assert_eq!(
7414            u64_expected,
7415            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7416        );
7417
7418        let u32_expected = vec![
7419            "null",
7420            "null",
7421            "null",
7422            "null",
7423            "0",
7424            "255",
7425            "65535",
7426            "4294967295",
7427            "null",
7428        ];
7429        assert_eq!(
7430            u32_expected,
7431            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7432        );
7433
7434        let u16_expected = vec![
7435            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7436        ];
7437        assert_eq!(
7438            u16_expected,
7439            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7440        );
7441
7442        let u8_expected = vec![
7443            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7444        ];
7445        assert_eq!(
7446            u8_expected,
7447            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7448        );
7449    }
7450
7451    #[test]
7452    fn test_cast_from_f32() {
7453        let f32_values: Vec<f32> = vec![
7454            i32::MIN as f32,
7455            i32::MIN as f32,
7456            i16::MIN as f32,
7457            i8::MIN as f32,
7458            0_f32,
7459            u8::MAX as f32,
7460            u16::MAX as f32,
7461            u32::MAX as f32,
7462            u32::MAX as f32,
7463        ];
7464        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7465
7466        let f64_expected = vec![
7467            "-2147483648.0",
7468            "-2147483648.0",
7469            "-32768.0",
7470            "-128.0",
7471            "0.0",
7472            "255.0",
7473            "65535.0",
7474            "4294967296.0",
7475            "4294967296.0",
7476        ];
7477        assert_eq!(
7478            f64_expected,
7479            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7480        );
7481
7482        let f32_expected = vec![
7483            "-2147483600.0",
7484            "-2147483600.0",
7485            "-32768.0",
7486            "-128.0",
7487            "0.0",
7488            "255.0",
7489            "65535.0",
7490            "4294967300.0",
7491            "4294967300.0",
7492        ];
7493        assert_eq!(
7494            f32_expected,
7495            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7496        );
7497
7498        let f16_expected = vec![
7499            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7500        ];
7501        assert_eq!(
7502            f16_expected,
7503            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7504        );
7505
7506        let i64_expected = vec![
7507            "-2147483648",
7508            "-2147483648",
7509            "-32768",
7510            "-128",
7511            "0",
7512            "255",
7513            "65535",
7514            "4294967296",
7515            "4294967296",
7516        ];
7517        assert_eq!(
7518            i64_expected,
7519            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7520        );
7521
7522        let i32_expected = vec![
7523            "-2147483648",
7524            "-2147483648",
7525            "-32768",
7526            "-128",
7527            "0",
7528            "255",
7529            "65535",
7530            "null",
7531            "null",
7532        ];
7533        assert_eq!(
7534            i32_expected,
7535            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7536        );
7537
7538        let i16_expected = vec![
7539            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7540        ];
7541        assert_eq!(
7542            i16_expected,
7543            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7544        );
7545
7546        let i8_expected = vec![
7547            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7548        ];
7549        assert_eq!(
7550            i8_expected,
7551            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7552        );
7553
7554        let u64_expected = vec![
7555            "null",
7556            "null",
7557            "null",
7558            "null",
7559            "0",
7560            "255",
7561            "65535",
7562            "4294967296",
7563            "4294967296",
7564        ];
7565        assert_eq!(
7566            u64_expected,
7567            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7568        );
7569
7570        let u32_expected = vec![
7571            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7572        ];
7573        assert_eq!(
7574            u32_expected,
7575            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7576        );
7577
7578        let u16_expected = vec![
7579            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7580        ];
7581        assert_eq!(
7582            u16_expected,
7583            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7584        );
7585
7586        let u8_expected = vec![
7587            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7588        ];
7589        assert_eq!(
7590            u8_expected,
7591            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7592        );
7593    }
7594
7595    #[test]
7596    fn test_cast_from_uint64() {
7597        let u64_values: Vec<u64> = vec![
7598            0,
7599            u8::MAX as u64,
7600            u16::MAX as u64,
7601            u32::MAX as u64,
7602            u64::MAX,
7603        ];
7604        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7605
7606        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7607        assert_eq!(
7608            f64_expected,
7609            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7610                .iter()
7611                .map(|i| i.parse::<f64>().unwrap())
7612                .collect::<Vec<f64>>()
7613        );
7614
7615        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7616        assert_eq!(
7617            f32_expected,
7618            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7619                .iter()
7620                .map(|i| i.parse::<f32>().unwrap())
7621                .collect::<Vec<f32>>()
7622        );
7623
7624        let f16_expected = vec![
7625            f16::from_f64(0.0),
7626            f16::from_f64(255.0),
7627            f16::from_f64(65535.0),
7628            f16::from_f64(4294967300.0),
7629            f16::from_f64(18446744000000000000.0),
7630        ];
7631        assert_eq!(
7632            f16_expected,
7633            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7634                .iter()
7635                .map(|i| i.parse::<f16>().unwrap())
7636                .collect::<Vec<f16>>()
7637        );
7638
7639        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7640        assert_eq!(
7641            i64_expected,
7642            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7643        );
7644
7645        let i32_expected = vec!["0", "255", "65535", "null", "null"];
7646        assert_eq!(
7647            i32_expected,
7648            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7649        );
7650
7651        let i16_expected = vec!["0", "255", "null", "null", "null"];
7652        assert_eq!(
7653            i16_expected,
7654            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7655        );
7656
7657        let i8_expected = vec!["0", "null", "null", "null", "null"];
7658        assert_eq!(
7659            i8_expected,
7660            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7661        );
7662
7663        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7664        assert_eq!(
7665            u64_expected,
7666            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7667        );
7668
7669        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7670        assert_eq!(
7671            u32_expected,
7672            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7673        );
7674
7675        let u16_expected = vec!["0", "255", "65535", "null", "null"];
7676        assert_eq!(
7677            u16_expected,
7678            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7679        );
7680
7681        let u8_expected = vec!["0", "255", "null", "null", "null"];
7682        assert_eq!(
7683            u8_expected,
7684            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7685        );
7686    }
7687
7688    #[test]
7689    fn test_cast_from_uint32() {
7690        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7691        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7692
7693        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7694        assert_eq!(
7695            f64_expected,
7696            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7697        );
7698
7699        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7700        assert_eq!(
7701            f32_expected,
7702            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7703        );
7704
7705        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7706        assert_eq!(
7707            f16_expected,
7708            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7709        );
7710
7711        let i64_expected = vec!["0", "255", "65535", "4294967295"];
7712        assert_eq!(
7713            i64_expected,
7714            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7715        );
7716
7717        let i32_expected = vec!["0", "255", "65535", "null"];
7718        assert_eq!(
7719            i32_expected,
7720            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7721        );
7722
7723        let i16_expected = vec!["0", "255", "null", "null"];
7724        assert_eq!(
7725            i16_expected,
7726            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7727        );
7728
7729        let i8_expected = vec!["0", "null", "null", "null"];
7730        assert_eq!(
7731            i8_expected,
7732            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7733        );
7734
7735        let u64_expected = vec!["0", "255", "65535", "4294967295"];
7736        assert_eq!(
7737            u64_expected,
7738            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7739        );
7740
7741        let u32_expected = vec!["0", "255", "65535", "4294967295"];
7742        assert_eq!(
7743            u32_expected,
7744            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7745        );
7746
7747        let u16_expected = vec!["0", "255", "65535", "null"];
7748        assert_eq!(
7749            u16_expected,
7750            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7751        );
7752
7753        let u8_expected = vec!["0", "255", "null", "null"];
7754        assert_eq!(
7755            u8_expected,
7756            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7757        );
7758    }
7759
7760    #[test]
7761    fn test_cast_from_uint16() {
7762        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7763        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7764
7765        let f64_expected = vec!["0.0", "255.0", "65535.0"];
7766        assert_eq!(
7767            f64_expected,
7768            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7769        );
7770
7771        let f32_expected = vec!["0.0", "255.0", "65535.0"];
7772        assert_eq!(
7773            f32_expected,
7774            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7775        );
7776
7777        let f16_expected = vec!["0.0", "255.0", "inf"];
7778        assert_eq!(
7779            f16_expected,
7780            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7781        );
7782
7783        let i64_expected = vec!["0", "255", "65535"];
7784        assert_eq!(
7785            i64_expected,
7786            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7787        );
7788
7789        let i32_expected = vec!["0", "255", "65535"];
7790        assert_eq!(
7791            i32_expected,
7792            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7793        );
7794
7795        let i16_expected = vec!["0", "255", "null"];
7796        assert_eq!(
7797            i16_expected,
7798            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7799        );
7800
7801        let i8_expected = vec!["0", "null", "null"];
7802        assert_eq!(
7803            i8_expected,
7804            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7805        );
7806
7807        let u64_expected = vec!["0", "255", "65535"];
7808        assert_eq!(
7809            u64_expected,
7810            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7811        );
7812
7813        let u32_expected = vec!["0", "255", "65535"];
7814        assert_eq!(
7815            u32_expected,
7816            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7817        );
7818
7819        let u16_expected = vec!["0", "255", "65535"];
7820        assert_eq!(
7821            u16_expected,
7822            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7823        );
7824
7825        let u8_expected = vec!["0", "255", "null"];
7826        assert_eq!(
7827            u8_expected,
7828            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7829        );
7830    }
7831
7832    #[test]
7833    fn test_cast_from_uint8() {
7834        let u8_values: Vec<u8> = vec![0, u8::MAX];
7835        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7836
7837        let f64_expected = vec!["0.0", "255.0"];
7838        assert_eq!(
7839            f64_expected,
7840            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7841        );
7842
7843        let f32_expected = vec!["0.0", "255.0"];
7844        assert_eq!(
7845            f32_expected,
7846            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7847        );
7848
7849        let f16_expected = vec!["0.0", "255.0"];
7850        assert_eq!(
7851            f16_expected,
7852            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7853        );
7854
7855        let i64_expected = vec!["0", "255"];
7856        assert_eq!(
7857            i64_expected,
7858            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7859        );
7860
7861        let i32_expected = vec!["0", "255"];
7862        assert_eq!(
7863            i32_expected,
7864            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7865        );
7866
7867        let i16_expected = vec!["0", "255"];
7868        assert_eq!(
7869            i16_expected,
7870            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7871        );
7872
7873        let i8_expected = vec!["0", "null"];
7874        assert_eq!(
7875            i8_expected,
7876            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7877        );
7878
7879        let u64_expected = vec!["0", "255"];
7880        assert_eq!(
7881            u64_expected,
7882            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7883        );
7884
7885        let u32_expected = vec!["0", "255"];
7886        assert_eq!(
7887            u32_expected,
7888            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7889        );
7890
7891        let u16_expected = vec!["0", "255"];
7892        assert_eq!(
7893            u16_expected,
7894            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7895        );
7896
7897        let u8_expected = vec!["0", "255"];
7898        assert_eq!(
7899            u8_expected,
7900            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7901        );
7902    }
7903
7904    #[test]
7905    fn test_cast_from_int64() {
7906        let i64_values: Vec<i64> = vec![
7907            i64::MIN,
7908            i32::MIN as i64,
7909            i16::MIN as i64,
7910            i8::MIN as i64,
7911            0,
7912            i8::MAX as i64,
7913            i16::MAX as i64,
7914            i32::MAX as i64,
7915            i64::MAX,
7916        ];
7917        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7918
7919        let f64_expected = vec![
7920            -9223372036854776000.0,
7921            -2147483648.0,
7922            -32768.0,
7923            -128.0,
7924            0.0,
7925            127.0,
7926            32767.0,
7927            2147483647.0,
7928            9223372036854776000.0,
7929        ];
7930        assert_eq!(
7931            f64_expected,
7932            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7933                .iter()
7934                .map(|i| i.parse::<f64>().unwrap())
7935                .collect::<Vec<f64>>()
7936        );
7937
7938        let f32_expected = vec![
7939            -9223372000000000000.0,
7940            -2147483600.0,
7941            -32768.0,
7942            -128.0,
7943            0.0,
7944            127.0,
7945            32767.0,
7946            2147483600.0,
7947            9223372000000000000.0,
7948        ];
7949        assert_eq!(
7950            f32_expected,
7951            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7952                .iter()
7953                .map(|i| i.parse::<f32>().unwrap())
7954                .collect::<Vec<f32>>()
7955        );
7956
7957        let f16_expected = vec![
7958            f16::from_f64(-9223372000000000000.0),
7959            f16::from_f64(-2147483600.0),
7960            f16::from_f64(-32768.0),
7961            f16::from_f64(-128.0),
7962            f16::from_f64(0.0),
7963            f16::from_f64(127.0),
7964            f16::from_f64(32767.0),
7965            f16::from_f64(2147483600.0),
7966            f16::from_f64(9223372000000000000.0),
7967        ];
7968        assert_eq!(
7969            f16_expected,
7970            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7971                .iter()
7972                .map(|i| i.parse::<f16>().unwrap())
7973                .collect::<Vec<f16>>()
7974        );
7975
7976        let i64_expected = vec![
7977            "-9223372036854775808",
7978            "-2147483648",
7979            "-32768",
7980            "-128",
7981            "0",
7982            "127",
7983            "32767",
7984            "2147483647",
7985            "9223372036854775807",
7986        ];
7987        assert_eq!(
7988            i64_expected,
7989            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
7990        );
7991
7992        let i32_expected = vec![
7993            "null",
7994            "-2147483648",
7995            "-32768",
7996            "-128",
7997            "0",
7998            "127",
7999            "32767",
8000            "2147483647",
8001            "null",
8002        ];
8003        assert_eq!(
8004            i32_expected,
8005            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8006        );
8007
8008        assert_eq!(
8009            i32_expected,
8010            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8011        );
8012
8013        let i16_expected = vec![
8014            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8015        ];
8016        assert_eq!(
8017            i16_expected,
8018            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8019        );
8020
8021        let i8_expected = vec![
8022            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8023        ];
8024        assert_eq!(
8025            i8_expected,
8026            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8027        );
8028
8029        let u64_expected = vec![
8030            "null",
8031            "null",
8032            "null",
8033            "null",
8034            "0",
8035            "127",
8036            "32767",
8037            "2147483647",
8038            "9223372036854775807",
8039        ];
8040        assert_eq!(
8041            u64_expected,
8042            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8043        );
8044
8045        let u32_expected = vec![
8046            "null",
8047            "null",
8048            "null",
8049            "null",
8050            "0",
8051            "127",
8052            "32767",
8053            "2147483647",
8054            "null",
8055        ];
8056        assert_eq!(
8057            u32_expected,
8058            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8059        );
8060
8061        let u16_expected = vec![
8062            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8063        ];
8064        assert_eq!(
8065            u16_expected,
8066            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8067        );
8068
8069        let u8_expected = vec![
8070            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8071        ];
8072        assert_eq!(
8073            u8_expected,
8074            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8075        );
8076    }
8077
8078    #[test]
8079    fn test_cast_from_int32() {
8080        let i32_values: Vec<i32> = vec![
8081            i32::MIN,
8082            i16::MIN as i32,
8083            i8::MIN as i32,
8084            0,
8085            i8::MAX as i32,
8086            i16::MAX as i32,
8087            i32::MAX,
8088        ];
8089        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8090
8091        let f64_expected = vec![
8092            "-2147483648.0",
8093            "-32768.0",
8094            "-128.0",
8095            "0.0",
8096            "127.0",
8097            "32767.0",
8098            "2147483647.0",
8099        ];
8100        assert_eq!(
8101            f64_expected,
8102            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8103        );
8104
8105        let f32_expected = vec![
8106            "-2147483600.0",
8107            "-32768.0",
8108            "-128.0",
8109            "0.0",
8110            "127.0",
8111            "32767.0",
8112            "2147483600.0",
8113        ];
8114        assert_eq!(
8115            f32_expected,
8116            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8117        );
8118
8119        let f16_expected = vec![
8120            f16::from_f64(-2147483600.0),
8121            f16::from_f64(-32768.0),
8122            f16::from_f64(-128.0),
8123            f16::from_f64(0.0),
8124            f16::from_f64(127.0),
8125            f16::from_f64(32767.0),
8126            f16::from_f64(2147483600.0),
8127        ];
8128        assert_eq!(
8129            f16_expected,
8130            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8131                .iter()
8132                .map(|i| i.parse::<f16>().unwrap())
8133                .collect::<Vec<f16>>()
8134        );
8135
8136        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8137        assert_eq!(
8138            i16_expected,
8139            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8140        );
8141
8142        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8143        assert_eq!(
8144            i8_expected,
8145            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8146        );
8147
8148        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8149        assert_eq!(
8150            u64_expected,
8151            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8152        );
8153
8154        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8155        assert_eq!(
8156            u32_expected,
8157            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8158        );
8159
8160        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8161        assert_eq!(
8162            u16_expected,
8163            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8164        );
8165
8166        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8167        assert_eq!(
8168            u8_expected,
8169            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8170        );
8171
8172        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8173        let i64_expected = vec![
8174            "-185542587187200000",
8175            "-2831155200000",
8176            "-11059200000",
8177            "0",
8178            "10972800000",
8179            "2831068800000",
8180            "185542587100800000",
8181        ];
8182        assert_eq!(
8183            i64_expected,
8184            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8185        );
8186    }
8187
8188    #[test]
8189    fn test_cast_from_int16() {
8190        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8191        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8192
8193        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8194        assert_eq!(
8195            f64_expected,
8196            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8197        );
8198
8199        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8200        assert_eq!(
8201            f32_expected,
8202            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8203        );
8204
8205        let f16_expected = vec![
8206            f16::from_f64(-32768.0),
8207            f16::from_f64(-128.0),
8208            f16::from_f64(0.0),
8209            f16::from_f64(127.0),
8210            f16::from_f64(32767.0),
8211        ];
8212        assert_eq!(
8213            f16_expected,
8214            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8215                .iter()
8216                .map(|i| i.parse::<f16>().unwrap())
8217                .collect::<Vec<f16>>()
8218        );
8219
8220        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8221        assert_eq!(
8222            i64_expected,
8223            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8224        );
8225
8226        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8227        assert_eq!(
8228            i32_expected,
8229            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8230        );
8231
8232        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8233        assert_eq!(
8234            i16_expected,
8235            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8236        );
8237
8238        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8239        assert_eq!(
8240            i8_expected,
8241            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8242        );
8243
8244        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8245        assert_eq!(
8246            u64_expected,
8247            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8248        );
8249
8250        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8251        assert_eq!(
8252            u32_expected,
8253            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8254        );
8255
8256        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8257        assert_eq!(
8258            u16_expected,
8259            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8260        );
8261
8262        let u8_expected = vec!["null", "null", "0", "127", "null"];
8263        assert_eq!(
8264            u8_expected,
8265            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8266        );
8267    }
8268
8269    #[test]
8270    fn test_cast_from_date32() {
8271        let i32_values: Vec<i32> = vec![
8272            i32::MIN,
8273            i16::MIN as i32,
8274            i8::MIN as i32,
8275            0,
8276            i8::MAX as i32,
8277            i16::MAX as i32,
8278            i32::MAX,
8279        ];
8280        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8281
8282        let i64_expected = vec![
8283            "-2147483648",
8284            "-32768",
8285            "-128",
8286            "0",
8287            "127",
8288            "32767",
8289            "2147483647",
8290        ];
8291        assert_eq!(
8292            i64_expected,
8293            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8294        );
8295    }
8296
8297    #[test]
8298    fn test_cast_from_int8() {
8299        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8300        let i8_array = Int8Array::from(i8_values);
8301
8302        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8303        assert_eq!(
8304            f64_expected,
8305            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8306        );
8307
8308        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8309        assert_eq!(
8310            f32_expected,
8311            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8312        );
8313
8314        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8315        assert_eq!(
8316            f16_expected,
8317            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8318        );
8319
8320        let i64_expected = vec!["-128", "0", "127"];
8321        assert_eq!(
8322            i64_expected,
8323            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8324        );
8325
8326        let i32_expected = vec!["-128", "0", "127"];
8327        assert_eq!(
8328            i32_expected,
8329            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8330        );
8331
8332        let i16_expected = vec!["-128", "0", "127"];
8333        assert_eq!(
8334            i16_expected,
8335            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8336        );
8337
8338        let i8_expected = vec!["-128", "0", "127"];
8339        assert_eq!(
8340            i8_expected,
8341            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8342        );
8343
8344        let u64_expected = vec!["null", "0", "127"];
8345        assert_eq!(
8346            u64_expected,
8347            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8348        );
8349
8350        let u32_expected = vec!["null", "0", "127"];
8351        assert_eq!(
8352            u32_expected,
8353            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8354        );
8355
8356        let u16_expected = vec!["null", "0", "127"];
8357        assert_eq!(
8358            u16_expected,
8359            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8360        );
8361
8362        let u8_expected = vec!["null", "0", "127"];
8363        assert_eq!(
8364            u8_expected,
8365            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8366        );
8367    }
8368
8369    /// Convert `array` into a vector of strings by casting to data type dt
8370    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8371    where
8372        T: ArrowPrimitiveType,
8373    {
8374        let c = cast(array, dt).unwrap();
8375        let a = c.as_primitive::<T>();
8376        let mut v: Vec<String> = vec![];
8377        for i in 0..array.len() {
8378            if a.is_null(i) {
8379                v.push("null".to_string())
8380            } else {
8381                v.push(format!("{:?}", a.value(i)));
8382            }
8383        }
8384        v
8385    }
8386
8387    #[test]
8388    fn test_cast_utf8_dict() {
8389        // FROM a dictionary with of Utf8 values
8390        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8391        builder.append("one").unwrap();
8392        builder.append_null();
8393        builder.append("three").unwrap();
8394        let array: ArrayRef = Arc::new(builder.finish());
8395
8396        let expected = vec!["one", "null", "three"];
8397
8398        // Test casting TO StringArray
8399        let cast_type = Utf8;
8400        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8401        assert_eq!(cast_array.data_type(), &cast_type);
8402        assert_eq!(array_to_strings(&cast_array), expected);
8403
8404        // Test casting TO Dictionary (with different index sizes)
8405
8406        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8407        let cast_array = cast(&array, &cast_type).expect("cast failed");
8408        assert_eq!(cast_array.data_type(), &cast_type);
8409        assert_eq!(array_to_strings(&cast_array), expected);
8410
8411        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8412        let cast_array = cast(&array, &cast_type).expect("cast failed");
8413        assert_eq!(cast_array.data_type(), &cast_type);
8414        assert_eq!(array_to_strings(&cast_array), expected);
8415
8416        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8417        let cast_array = cast(&array, &cast_type).expect("cast failed");
8418        assert_eq!(cast_array.data_type(), &cast_type);
8419        assert_eq!(array_to_strings(&cast_array), expected);
8420
8421        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8422        let cast_array = cast(&array, &cast_type).expect("cast failed");
8423        assert_eq!(cast_array.data_type(), &cast_type);
8424        assert_eq!(array_to_strings(&cast_array), expected);
8425
8426        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8427        let cast_array = cast(&array, &cast_type).expect("cast failed");
8428        assert_eq!(cast_array.data_type(), &cast_type);
8429        assert_eq!(array_to_strings(&cast_array), expected);
8430
8431        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8432        let cast_array = cast(&array, &cast_type).expect("cast failed");
8433        assert_eq!(cast_array.data_type(), &cast_type);
8434        assert_eq!(array_to_strings(&cast_array), expected);
8435
8436        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8437        let cast_array = cast(&array, &cast_type).expect("cast failed");
8438        assert_eq!(cast_array.data_type(), &cast_type);
8439        assert_eq!(array_to_strings(&cast_array), expected);
8440    }
8441
8442    #[test]
8443    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8444        // test converting from an array that has indexes of a type
8445        // that are out of bounds for a particular other kind of
8446        // index.
8447
8448        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8449
8450        // add 200 distinct values (which can be stored by a
8451        // dictionary indexed by int32, but not a dictionary indexed
8452        // with int8)
8453        for i in 0..200 {
8454            builder.append(i).unwrap();
8455        }
8456        let array: ArrayRef = Arc::new(builder.finish());
8457
8458        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8459        let res = cast(&array, &cast_type);
8460        assert!(res.is_err());
8461        let actual_error = format!("{res:?}");
8462        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8463        assert!(
8464            actual_error.contains(expected_error),
8465            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8466        );
8467    }
8468
8469    #[test]
8470    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8471        // Same test as test_cast_dict_to_dict_bad_index_value but use
8472        // string values (and encode the expected behavior here);
8473
8474        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8475
8476        // add 200 distinct values (which can be stored by a
8477        // dictionary indexed by int32, but not a dictionary indexed
8478        // with int8)
8479        for i in 0..200 {
8480            let val = format!("val{i}");
8481            builder.append(&val).unwrap();
8482        }
8483        let array = builder.finish();
8484
8485        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8486        let res = cast(&array, &cast_type);
8487        assert!(res.is_err());
8488        let actual_error = format!("{res:?}");
8489        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8490        assert!(
8491            actual_error.contains(expected_error),
8492            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8493        );
8494    }
8495
8496    #[test]
8497    fn test_cast_primitive_dict() {
8498        // FROM a dictionary with of INT32 values
8499        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8500        builder.append(1).unwrap();
8501        builder.append_null();
8502        builder.append(3).unwrap();
8503        let array: ArrayRef = Arc::new(builder.finish());
8504
8505        let expected = vec!["1", "null", "3"];
8506
8507        // Test casting TO PrimitiveArray, different dictionary type
8508        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8509        assert_eq!(array_to_strings(&cast_array), expected);
8510        assert_eq!(cast_array.data_type(), &Utf8);
8511
8512        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8513        assert_eq!(array_to_strings(&cast_array), expected);
8514        assert_eq!(cast_array.data_type(), &Int64);
8515    }
8516
8517    #[test]
8518    fn test_cast_primitive_array_to_dict() {
8519        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8520        builder.append_value(1);
8521        builder.append_null();
8522        builder.append_value(3);
8523        let array: ArrayRef = Arc::new(builder.finish());
8524
8525        let expected = vec!["1", "null", "3"];
8526
8527        // Cast to a dictionary (same value type, Int32)
8528        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8529        let cast_array = cast(&array, &cast_type).expect("cast failed");
8530        assert_eq!(cast_array.data_type(), &cast_type);
8531        assert_eq!(array_to_strings(&cast_array), expected);
8532
8533        // Cast to a dictionary (different value type, Int8)
8534        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8535        let cast_array = cast(&array, &cast_type).expect("cast failed");
8536        assert_eq!(cast_array.data_type(), &cast_type);
8537        assert_eq!(array_to_strings(&cast_array), expected);
8538    }
8539
8540    #[test]
8541    fn test_cast_time_array_to_dict() {
8542        use DataType::*;
8543
8544        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8545
8546        let expected = vec!["1972-09-27", "null", "1975-06-24"];
8547
8548        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8549        let cast_array = cast(&array, &cast_type).expect("cast failed");
8550        assert_eq!(cast_array.data_type(), &cast_type);
8551        assert_eq!(array_to_strings(&cast_array), expected);
8552    }
8553
8554    #[test]
8555    fn test_cast_timestamp_array_to_dict() {
8556        use DataType::*;
8557
8558        let array = Arc::new(
8559            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8560        ) as ArrayRef;
8561
8562        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8563
8564        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8565        let cast_array = cast(&array, &cast_type).expect("cast failed");
8566        assert_eq!(cast_array.data_type(), &cast_type);
8567        assert_eq!(array_to_strings(&cast_array), expected);
8568    }
8569
8570    #[test]
8571    fn test_cast_string_array_to_dict() {
8572        use DataType::*;
8573
8574        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8575
8576        let expected = vec!["one", "null", "three"];
8577
8578        // Cast to a dictionary (same value type, Utf8)
8579        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8580        let cast_array = cast(&array, &cast_type).expect("cast failed");
8581        assert_eq!(cast_array.data_type(), &cast_type);
8582        assert_eq!(array_to_strings(&cast_array), expected);
8583    }
8584
8585    #[test]
8586    fn test_cast_null_array_to_from_decimal_array() {
8587        let data_type = DataType::Decimal128(12, 4);
8588        let array = new_null_array(&DataType::Null, 4);
8589        assert_eq!(array.data_type(), &DataType::Null);
8590        let cast_array = cast(&array, &data_type).expect("cast failed");
8591        assert_eq!(cast_array.data_type(), &data_type);
8592        for i in 0..4 {
8593            assert!(cast_array.is_null(i));
8594        }
8595
8596        let array = new_null_array(&data_type, 4);
8597        assert_eq!(array.data_type(), &data_type);
8598        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8599        assert_eq!(cast_array.data_type(), &DataType::Null);
8600        assert_eq!(cast_array.len(), 4);
8601        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8602    }
8603
8604    #[test]
8605    fn test_cast_null_array_from_and_to_primitive_array() {
8606        macro_rules! typed_test {
8607            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8608                {
8609                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
8610                    let expected = $ARR_TYPE::from(vec![None; 6]);
8611                    let cast_type = DataType::$DATATYPE;
8612                    let cast_array = cast(&array, &cast_type).expect("cast failed");
8613                    let cast_array = cast_array.as_primitive::<$TYPE>();
8614                    assert_eq!(cast_array.data_type(), &cast_type);
8615                    assert_eq!(cast_array, &expected);
8616                }
8617            }};
8618        }
8619
8620        typed_test!(Int16Array, Int16, Int16Type);
8621        typed_test!(Int32Array, Int32, Int32Type);
8622        typed_test!(Int64Array, Int64, Int64Type);
8623
8624        typed_test!(UInt16Array, UInt16, UInt16Type);
8625        typed_test!(UInt32Array, UInt32, UInt32Type);
8626        typed_test!(UInt64Array, UInt64, UInt64Type);
8627
8628        typed_test!(Float16Array, Float16, Float16Type);
8629        typed_test!(Float32Array, Float32, Float32Type);
8630        typed_test!(Float64Array, Float64, Float64Type);
8631
8632        typed_test!(Date32Array, Date32, Date32Type);
8633        typed_test!(Date64Array, Date64, Date64Type);
8634    }
8635
8636    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
8637        // Cast from null to data_type
8638        let array = new_null_array(&DataType::Null, 4);
8639        assert_eq!(array.data_type(), &DataType::Null);
8640        let cast_array = cast(&array, data_type).expect("cast failed");
8641        assert_eq!(cast_array.data_type(), data_type);
8642        for i in 0..4 {
8643            if is_complex {
8644                assert!(cast_array.logical_nulls().unwrap().is_null(i));
8645            } else {
8646                assert!(cast_array.is_null(i));
8647            }
8648        }
8649    }
8650
8651    fn cast_from_null_to_other(data_type: &DataType) {
8652        cast_from_null_to_other_base(data_type, false);
8653    }
8654
8655    fn cast_from_null_to_other_complex(data_type: &DataType) {
8656        cast_from_null_to_other_base(data_type, true);
8657    }
8658
8659    #[test]
8660    fn test_cast_null_from_and_to_variable_sized() {
8661        cast_from_null_to_other(&DataType::Utf8);
8662        cast_from_null_to_other(&DataType::LargeUtf8);
8663        cast_from_null_to_other(&DataType::Binary);
8664        cast_from_null_to_other(&DataType::LargeBinary);
8665    }
8666
8667    #[test]
8668    fn test_cast_null_from_and_to_nested_type() {
8669        // Cast null from and to map
8670        let data_type = DataType::Map(
8671            Arc::new(Field::new_struct(
8672                "entry",
8673                vec![
8674                    Field::new("key", DataType::Utf8, false),
8675                    Field::new("value", DataType::Int32, true),
8676                ],
8677                false,
8678            )),
8679            false,
8680        );
8681        cast_from_null_to_other(&data_type);
8682
8683        // Cast null from and to list
8684        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8685        cast_from_null_to_other(&data_type);
8686        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8687        cast_from_null_to_other(&data_type);
8688        let data_type =
8689            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8690        cast_from_null_to_other(&data_type);
8691
8692        // Cast null from and to dictionary
8693        let values = vec![None, None, None, None] as Vec<Option<&str>>;
8694        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8695        let array = Arc::new(array) as ArrayRef;
8696        let data_type = array.data_type().to_owned();
8697        cast_from_null_to_other(&data_type);
8698
8699        // Cast null from and to struct
8700        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8701        cast_from_null_to_other(&data_type);
8702
8703        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
8704        cast_from_null_to_other(&target_type);
8705
8706        let target_type =
8707            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
8708        cast_from_null_to_other(&target_type);
8709
8710        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
8711        let target_type = DataType::Union(fields, UnionMode::Sparse);
8712        cast_from_null_to_other_complex(&target_type);
8713
8714        let target_type = DataType::RunEndEncoded(
8715            Arc::new(Field::new("item", DataType::Int32, true)),
8716            Arc::new(Field::new("item", DataType::Int32, true)),
8717        );
8718        cast_from_null_to_other_complex(&target_type);
8719    }
8720
8721    /// Print the `DictionaryArray` `array` as a vector of strings
8722    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8723        let options = FormatOptions::new().with_null("null");
8724        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8725        (0..array.len())
8726            .map(|i| formatter.value(i).to_string())
8727            .collect()
8728    }
8729
8730    #[test]
8731    fn test_cast_utf8_to_date32() {
8732        use chrono::NaiveDate;
8733        let from_ymd = chrono::NaiveDate::from_ymd_opt;
8734        let since = chrono::NaiveDate::signed_duration_since;
8735
8736        let a = StringArray::from(vec![
8737            "2000-01-01",          // valid date with leading 0s
8738            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
8739            "2000-2-2",            // valid date without leading 0s
8740            "2000-00-00",          // invalid month and day
8741            "2000",                // just a year is invalid
8742        ]);
8743        let array = Arc::new(a) as ArrayRef;
8744        let b = cast(&array, &DataType::Date32).unwrap();
8745        let c = b.as_primitive::<Date32Type>();
8746
8747        // test valid inputs
8748        let date_value = since(
8749            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8750            from_ymd(1970, 1, 1).unwrap(),
8751        )
8752        .num_days() as i32;
8753        assert!(c.is_valid(0)); // "2000-01-01"
8754        assert_eq!(date_value, c.value(0));
8755
8756        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
8757        assert_eq!(date_value, c.value(1));
8758
8759        let date_value = since(
8760            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8761            from_ymd(1970, 1, 1).unwrap(),
8762        )
8763        .num_days() as i32;
8764        assert!(c.is_valid(2)); // "2000-2-2"
8765        assert_eq!(date_value, c.value(2));
8766
8767        // test invalid inputs
8768        assert!(!c.is_valid(3)); // "2000-00-00"
8769        assert!(!c.is_valid(4)); // "2000"
8770    }
8771
8772    #[test]
8773    fn test_cast_utf8_to_date64() {
8774        let a = StringArray::from(vec![
8775            "2000-01-01T12:00:00", // date + time valid
8776            "2020-12-15T12:34:56", // date + time valid
8777            "2020-2-2T12:34:56",   // valid date time without leading 0s
8778            "2000-00-00T12:00:00", // invalid month and day
8779            "2000-01-01 12:00:00", // missing the 'T'
8780            "2000-01-01",          // just a date is invalid
8781        ]);
8782        let array = Arc::new(a) as ArrayRef;
8783        let b = cast(&array, &DataType::Date64).unwrap();
8784        let c = b.as_primitive::<Date64Type>();
8785
8786        // test valid inputs
8787        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
8788        assert_eq!(946728000000, c.value(0));
8789        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
8790        assert_eq!(1608035696000, c.value(1));
8791        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
8792
8793        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
8794        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
8795        assert_eq!(946728000000, c.value(4));
8796        assert!(c.is_valid(5)); // "2000-01-01"
8797        assert_eq!(946684800000, c.value(5));
8798    }
8799
8800    #[test]
8801    fn test_can_cast_fsl_to_fsl() {
8802        let from_array = Arc::new(
8803            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8804                [Some([Some(1.0), Some(2.0)]), None],
8805                2,
8806            ),
8807        ) as ArrayRef;
8808        let to_array = Arc::new(
8809            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8810                [
8811                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8812                    None,
8813                ],
8814                2,
8815            ),
8816        ) as ArrayRef;
8817
8818        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8819        let actual = cast(&from_array, to_array.data_type()).unwrap();
8820        assert_eq!(actual.data_type(), to_array.data_type());
8821
8822        let invalid_target =
8823            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8824        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8825
8826        let invalid_size =
8827            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8828        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8829    }
8830
8831    #[test]
8832    fn test_can_cast_types_fixed_size_list_to_list() {
8833        // DataType::List
8834        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
8835        assert!(can_cast_types(
8836            array1.data_type(),
8837            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8838        ));
8839
8840        // DataType::LargeList
8841        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
8842        assert!(can_cast_types(
8843            array2.data_type(),
8844            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8845        ));
8846    }
8847
8848    #[test]
8849    fn test_cast_fixed_size_list_to_list() {
8850        // Important cases:
8851        // 1. With/without nulls
8852        // 2. LargeList and List
8853        // 3. With and without inner casts
8854
8855        let cases = [
8856            // fixed_size_list<i32, 2> => list<i32>
8857            (
8858                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8859                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8860                    2,
8861                )) as ArrayRef,
8862                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8863                    Some([Some(1), Some(1)]),
8864                    Some([Some(2), Some(2)]),
8865                ])) as ArrayRef,
8866            ),
8867            // fixed_size_list<i32, 2> => list<i32> (nullable)
8868            (
8869                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8870                    [None, Some([Some(2), Some(2)])],
8871                    2,
8872                )) as ArrayRef,
8873                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8874                    None,
8875                    Some([Some(2), Some(2)]),
8876                ])) as ArrayRef,
8877            ),
8878            // fixed_size_list<i32, 2> => large_list<i64>
8879            (
8880                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8881                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8882                    2,
8883                )) as ArrayRef,
8884                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8885                    Some([Some(1), Some(1)]),
8886                    Some([Some(2), Some(2)]),
8887                ])) as ArrayRef,
8888            ),
8889            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
8890            (
8891                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8892                    [None, Some([Some(2), Some(2)])],
8893                    2,
8894                )) as ArrayRef,
8895                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8896                    None,
8897                    Some([Some(2), Some(2)]),
8898                ])) as ArrayRef,
8899            ),
8900        ];
8901
8902        for (array, expected) in cases {
8903            let array = Arc::new(array) as ArrayRef;
8904
8905            assert!(
8906                can_cast_types(array.data_type(), expected.data_type()),
8907                "can_cast_types claims we cannot cast {:?} to {:?}",
8908                array.data_type(),
8909                expected.data_type()
8910            );
8911
8912            let list_array = cast(&array, expected.data_type())
8913                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8914            assert_eq!(
8915                list_array.as_ref(),
8916                &expected,
8917                "Incorrect result from casting {array:?} to {expected:?}",
8918            );
8919        }
8920    }
8921
8922    #[test]
8923    fn test_cast_utf8_to_list() {
8924        // DataType::List
8925        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8926        let field = Arc::new(Field::new("", DataType::Int32, false));
8927        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8928        let actual = list_array.as_list_opt::<i32>().unwrap();
8929        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8930        assert_eq!(&expect.value(0), &actual.value(0));
8931
8932        // DataType::LargeList
8933        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8934        let actual = list_array.as_list_opt::<i64>().unwrap();
8935        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8936        assert_eq!(&expect.value(0), &actual.value(0));
8937
8938        // DataType::FixedSizeList
8939        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8940        let actual = list_array.as_fixed_size_list_opt().unwrap();
8941        let expect =
8942            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
8943        assert_eq!(&expect.value(0), &actual.value(0));
8944    }
8945
8946    #[test]
8947    fn test_cast_single_element_fixed_size_list() {
8948        // FixedSizeList<T>[1] => T
8949        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8950            [(Some([Some(5)]))],
8951            1,
8952        )) as ArrayRef;
8953        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
8954        let actual: &Int32Array = casted_array.as_primitive();
8955        let expected = Int32Array::from(vec![Some(5)]);
8956        assert_eq!(&expected, actual);
8957
8958        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
8959        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8960            [(Some([Some(5)]))],
8961            1,
8962        )) as ArrayRef;
8963        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
8964        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8965        let expected = Arc::new(FixedSizeListArray::new(
8966            to_field.clone(),
8967            1,
8968            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8969            None,
8970        )) as ArrayRef;
8971        assert_eq!(*expected, *actual);
8972
8973        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
8974        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8975            [(Some([Some(5)]))],
8976            1,
8977        )) as ArrayRef;
8978        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
8979        let to_field = Arc::new(Field::new(
8980            "dummy",
8981            DataType::FixedSizeList(to_field_inner.clone(), 1),
8982            false,
8983        ));
8984        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8985        let expected = Arc::new(FixedSizeListArray::new(
8986            to_field.clone(),
8987            1,
8988            Arc::new(FixedSizeListArray::new(
8989                to_field_inner.clone(),
8990                1,
8991                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8992                None,
8993            )) as ArrayRef,
8994            None,
8995        )) as ArrayRef;
8996        assert_eq!(*expected, *actual);
8997
8998        // T => FixedSizeList<T>[1] (non-nullable)
8999        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9000        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9001        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9002        let actual = casted_array.as_fixed_size_list();
9003        let expected = Arc::new(FixedSizeListArray::new(
9004            field.clone(),
9005            1,
9006            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9007            None,
9008        )) as ArrayRef;
9009        assert_eq!(expected.as_ref(), actual);
9010
9011        // T => FixedSizeList<T>[1] (nullable)
9012        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9013        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9014        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9015        let actual = casted_array.as_fixed_size_list();
9016        let expected = Arc::new(FixedSizeListArray::new(
9017            field.clone(),
9018            1,
9019            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9020            None,
9021        )) as ArrayRef;
9022        assert_eq!(expected.as_ref(), actual);
9023    }
9024
9025    #[test]
9026    fn test_cast_list_containers() {
9027        // large-list to list
9028        let array = Arc::new(make_large_list_array()) as ArrayRef;
9029        let list_array = cast(
9030            &array,
9031            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9032        )
9033        .unwrap();
9034        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9035        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9036
9037        assert_eq!(&expected.value(0), &actual.value(0));
9038        assert_eq!(&expected.value(1), &actual.value(1));
9039        assert_eq!(&expected.value(2), &actual.value(2));
9040
9041        // list to large-list
9042        let array = Arc::new(make_list_array()) as ArrayRef;
9043        let large_list_array = cast(
9044            &array,
9045            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9046        )
9047        .unwrap();
9048        let actual = large_list_array
9049            .as_any()
9050            .downcast_ref::<LargeListArray>()
9051            .unwrap();
9052        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9053
9054        assert_eq!(&expected.value(0), &actual.value(0));
9055        assert_eq!(&expected.value(1), &actual.value(1));
9056        assert_eq!(&expected.value(2), &actual.value(2));
9057    }
9058
9059    #[test]
9060    fn test_cast_list_to_fsl() {
9061        // There four noteworthy cases we should handle:
9062        // 1. No nulls
9063        // 2. Nulls that are always empty
9064        // 3. Nulls that have varying lengths
9065        // 4. Nulls that are correctly sized (same as target list size)
9066
9067        // Non-null case
9068        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9069        let values = vec![
9070            Some(vec![Some(1), Some(2), Some(3)]),
9071            Some(vec![Some(4), Some(5), Some(6)]),
9072        ];
9073        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9074            values.clone(),
9075        )) as ArrayRef;
9076        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9077            values, 3,
9078        )) as ArrayRef;
9079        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9080        assert_eq!(expected.as_ref(), actual.as_ref());
9081
9082        // Null cases
9083        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9084        let cases = [
9085            (
9086                // Zero-length nulls
9087                vec![1, 2, 3, 4, 5, 6],
9088                vec![3, 0, 3, 0],
9089            ),
9090            (
9091                // Varying-length nulls
9092                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9093                vec![3, 2, 3, 1],
9094            ),
9095            (
9096                // Correctly-sized nulls
9097                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9098                vec![3, 3, 3, 3],
9099            ),
9100            (
9101                // Mixed nulls
9102                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9103                vec![3, 0, 3, 3],
9104            ),
9105        ];
9106        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9107
9108        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9109            vec![
9110                Some(vec![Some(1), Some(2), Some(3)]),
9111                None,
9112                Some(vec![Some(4), Some(5), Some(6)]),
9113                None,
9114            ],
9115            3,
9116        )) as ArrayRef;
9117
9118        for (values, lengths) in cases.iter() {
9119            let array = Arc::new(ListArray::new(
9120                field.clone(),
9121                OffsetBuffer::from_lengths(lengths.clone()),
9122                Arc::new(Int32Array::from(values.clone())),
9123                Some(null_buffer.clone()),
9124            )) as ArrayRef;
9125            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9126            assert_eq!(expected.as_ref(), actual.as_ref());
9127        }
9128    }
9129
9130    #[test]
9131    fn test_cast_list_to_fsl_safety() {
9132        let values = vec![
9133            Some(vec![Some(1), Some(2), Some(3)]),
9134            Some(vec![Some(4), Some(5)]),
9135            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9136            Some(vec![Some(3), Some(4), Some(5)]),
9137        ];
9138        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9139            values.clone(),
9140        )) as ArrayRef;
9141
9142        let res = cast_with_options(
9143            array.as_ref(),
9144            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9145            &CastOptions {
9146                safe: false,
9147                ..Default::default()
9148            },
9149        );
9150        assert!(res.is_err());
9151        assert!(
9152            format!("{res:?}")
9153                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9154        );
9155
9156        // When safe=true (default), the cast will fill nulls for lists that are
9157        // too short and truncate lists that are too long.
9158        let res = cast(
9159            array.as_ref(),
9160            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9161        )
9162        .unwrap();
9163        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9164            vec![
9165                Some(vec![Some(1), Some(2), Some(3)]),
9166                None, // Too short -> replaced with null
9167                None, // Too long -> replaced with null
9168                Some(vec![Some(3), Some(4), Some(5)]),
9169            ],
9170            3,
9171        )) as ArrayRef;
9172        assert_eq!(expected.as_ref(), res.as_ref());
9173
9174        // The safe option is false and the source array contains a null list.
9175        // issue: https://github.com/apache/arrow-rs/issues/5642
9176        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9177            Some(vec![Some(1), Some(2), Some(3)]),
9178            None,
9179        ])) as ArrayRef;
9180        let res = cast_with_options(
9181            array.as_ref(),
9182            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9183            &CastOptions {
9184                safe: false,
9185                ..Default::default()
9186            },
9187        )
9188        .unwrap();
9189        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9190            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9191            3,
9192        )) as ArrayRef;
9193        assert_eq!(expected.as_ref(), res.as_ref());
9194    }
9195
9196    #[test]
9197    fn test_cast_large_list_to_fsl() {
9198        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
9199        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9200            values.clone(),
9201        )) as ArrayRef;
9202        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9203            values, 2,
9204        )) as ArrayRef;
9205        let actual = cast(
9206            array.as_ref(),
9207            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
9208        )
9209        .unwrap();
9210        assert_eq!(expected.as_ref(), actual.as_ref());
9211    }
9212
9213    #[test]
9214    fn test_cast_list_to_fsl_subcast() {
9215        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9216            vec![
9217                Some(vec![Some(1), Some(2)]),
9218                Some(vec![Some(3), Some(i32::MAX)]),
9219            ],
9220        )) as ArrayRef;
9221        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
9222            vec![
9223                Some(vec![Some(1), Some(2)]),
9224                Some(vec![Some(3), Some(i32::MAX as i64)]),
9225            ],
9226            2,
9227        )) as ArrayRef;
9228        let actual = cast(
9229            array.as_ref(),
9230            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
9231        )
9232        .unwrap();
9233        assert_eq!(expected.as_ref(), actual.as_ref());
9234
9235        let res = cast_with_options(
9236            array.as_ref(),
9237            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
9238            &CastOptions {
9239                safe: false,
9240                ..Default::default()
9241            },
9242        );
9243        assert!(res.is_err());
9244        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
9245    }
9246
9247    #[test]
9248    fn test_cast_list_to_fsl_empty() {
9249        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9250        let array = new_empty_array(&DataType::List(field.clone()));
9251
9252        let target_type = DataType::FixedSizeList(field.clone(), 3);
9253        let expected = new_empty_array(&target_type);
9254
9255        let actual = cast(array.as_ref(), &target_type).unwrap();
9256        assert_eq!(expected.as_ref(), actual.as_ref());
9257    }
9258
9259    fn make_list_array() -> ListArray {
9260        // Construct a value array
9261        let value_data = ArrayData::builder(DataType::Int32)
9262            .len(8)
9263            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
9264            .build()
9265            .unwrap();
9266
9267        // Construct a buffer for value offsets, for the nested array:
9268        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
9269        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
9270
9271        // Construct a list array from the above two
9272        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
9273        let list_data = ArrayData::builder(list_data_type)
9274            .len(3)
9275            .add_buffer(value_offsets)
9276            .add_child_data(value_data)
9277            .build()
9278            .unwrap();
9279        ListArray::from(list_data)
9280    }
9281
9282    fn make_large_list_array() -> LargeListArray {
9283        // Construct a value array
9284        let value_data = ArrayData::builder(DataType::Int32)
9285            .len(8)
9286            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
9287            .build()
9288            .unwrap();
9289
9290        // Construct a buffer for value offsets, for the nested array:
9291        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
9292        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
9293
9294        // Construct a list array from the above two
9295        let list_data_type =
9296            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9297        let list_data = ArrayData::builder(list_data_type)
9298            .len(3)
9299            .add_buffer(value_offsets)
9300            .add_child_data(value_data)
9301            .build()
9302            .unwrap();
9303        LargeListArray::from(list_data)
9304    }
9305
9306    fn make_fixed_size_list_array() -> FixedSizeListArray {
9307        // Construct a value array
9308        let value_data = ArrayData::builder(DataType::Int32)
9309            .len(8)
9310            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
9311            .build()
9312            .unwrap();
9313
9314        let list_data_type =
9315            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
9316        let list_data = ArrayData::builder(list_data_type)
9317            .len(2)
9318            .add_child_data(value_data)
9319            .build()
9320            .unwrap();
9321        FixedSizeListArray::from(list_data)
9322    }
9323
9324    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
9325        // Construct a value array
9326        let value_data = ArrayData::builder(DataType::Int64)
9327            .len(8)
9328            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
9329            .build()
9330            .unwrap();
9331
9332        let list_data_type =
9333            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
9334        let list_data = ArrayData::builder(list_data_type)
9335            .len(2)
9336            .add_child_data(value_data)
9337            .build()
9338            .unwrap();
9339        FixedSizeListArray::from(list_data)
9340    }
9341
9342    #[test]
9343    fn test_cast_map_dont_allow_change_of_order() {
9344        let string_builder = StringBuilder::new();
9345        let value_builder = StringBuilder::new();
9346        let mut builder = MapBuilder::new(
9347            Some(MapFieldNames {
9348                entry: "entries".to_string(),
9349                key: "key".to_string(),
9350                value: "value".to_string(),
9351            }),
9352            string_builder,
9353            value_builder,
9354        );
9355
9356        builder.keys().append_value("0");
9357        builder.values().append_value("test_val_1");
9358        builder.append(true).unwrap();
9359        builder.keys().append_value("1");
9360        builder.values().append_value("test_val_2");
9361        builder.append(true).unwrap();
9362
9363        // map builder returns unsorted map by default
9364        let array = builder.finish();
9365
9366        let new_ordered = true;
9367        let new_type = DataType::Map(
9368            Arc::new(Field::new(
9369                "entries",
9370                DataType::Struct(
9371                    vec![
9372                        Field::new("key", DataType::Utf8, false),
9373                        Field::new("value", DataType::Utf8, false),
9374                    ]
9375                    .into(),
9376                ),
9377                false,
9378            )),
9379            new_ordered,
9380        );
9381
9382        let new_array_result = cast(&array, &new_type.clone());
9383        assert!(!can_cast_types(array.data_type(), &new_type));
9384        let Err(ArrowError::CastError(t)) = new_array_result else {
9385            panic!();
9386        };
9387        assert_eq!(
9388            t,
9389            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"#
9390        );
9391    }
9392
9393    #[test]
9394    fn test_cast_map_dont_allow_when_container_cant_cast() {
9395        let string_builder = StringBuilder::new();
9396        let value_builder = IntervalDayTimeArray::builder(2);
9397        let mut builder = MapBuilder::new(
9398            Some(MapFieldNames {
9399                entry: "entries".to_string(),
9400                key: "key".to_string(),
9401                value: "value".to_string(),
9402            }),
9403            string_builder,
9404            value_builder,
9405        );
9406
9407        builder.keys().append_value("0");
9408        builder.values().append_value(IntervalDayTime::new(1, 1));
9409        builder.append(true).unwrap();
9410        builder.keys().append_value("1");
9411        builder.values().append_value(IntervalDayTime::new(2, 2));
9412        builder.append(true).unwrap();
9413
9414        // map builder returns unsorted map by default
9415        let array = builder.finish();
9416
9417        let new_ordered = true;
9418        let new_type = DataType::Map(
9419            Arc::new(Field::new(
9420                "entries",
9421                DataType::Struct(
9422                    vec![
9423                        Field::new("key", DataType::Utf8, false),
9424                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
9425                    ]
9426                    .into(),
9427                ),
9428                false,
9429            )),
9430            new_ordered,
9431        );
9432
9433        let new_array_result = cast(&array, &new_type.clone());
9434        assert!(!can_cast_types(array.data_type(), &new_type));
9435        let Err(ArrowError::CastError(t)) = new_array_result else {
9436            panic!();
9437        };
9438        assert_eq!(
9439            t,
9440            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"#
9441        );
9442    }
9443
9444    #[test]
9445    fn test_cast_map_field_names() {
9446        let string_builder = StringBuilder::new();
9447        let value_builder = StringBuilder::new();
9448        let mut builder = MapBuilder::new(
9449            Some(MapFieldNames {
9450                entry: "entries".to_string(),
9451                key: "key".to_string(),
9452                value: "value".to_string(),
9453            }),
9454            string_builder,
9455            value_builder,
9456        );
9457
9458        builder.keys().append_value("0");
9459        builder.values().append_value("test_val_1");
9460        builder.append(true).unwrap();
9461        builder.keys().append_value("1");
9462        builder.values().append_value("test_val_2");
9463        builder.append(true).unwrap();
9464        builder.append(false).unwrap();
9465
9466        let array = builder.finish();
9467
9468        let new_type = DataType::Map(
9469            Arc::new(Field::new(
9470                "entries_new",
9471                DataType::Struct(
9472                    vec![
9473                        Field::new("key_new", DataType::Utf8, false),
9474                        Field::new("value_values", DataType::Utf8, false),
9475                    ]
9476                    .into(),
9477                ),
9478                false,
9479            )),
9480            false,
9481        );
9482
9483        assert_ne!(new_type, array.data_type().clone());
9484
9485        let new_array = cast(&array, &new_type.clone()).unwrap();
9486        assert_eq!(new_type, new_array.data_type().clone());
9487        let map_array = new_array.as_map();
9488
9489        assert_ne!(new_type, array.data_type().clone());
9490        assert_eq!(new_type, map_array.data_type().clone());
9491
9492        let key_string = map_array
9493            .keys()
9494            .as_any()
9495            .downcast_ref::<StringArray>()
9496            .unwrap()
9497            .into_iter()
9498            .flatten()
9499            .collect::<Vec<_>>();
9500        assert_eq!(&key_string, &vec!["0", "1"]);
9501
9502        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9503        let values_string = values_string_array
9504            .as_any()
9505            .downcast_ref::<StringArray>()
9506            .unwrap()
9507            .into_iter()
9508            .flatten()
9509            .collect::<Vec<_>>();
9510        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
9511
9512        assert_eq!(
9513            map_array.nulls(),
9514            Some(&NullBuffer::from(vec![true, true, false]))
9515        );
9516    }
9517
9518    #[test]
9519    fn test_cast_map_contained_values() {
9520        let string_builder = StringBuilder::new();
9521        let value_builder = Int8Builder::new();
9522        let mut builder = MapBuilder::new(
9523            Some(MapFieldNames {
9524                entry: "entries".to_string(),
9525                key: "key".to_string(),
9526                value: "value".to_string(),
9527            }),
9528            string_builder,
9529            value_builder,
9530        );
9531
9532        builder.keys().append_value("0");
9533        builder.values().append_value(44);
9534        builder.append(true).unwrap();
9535        builder.keys().append_value("1");
9536        builder.values().append_value(22);
9537        builder.append(true).unwrap();
9538
9539        let array = builder.finish();
9540
9541        let new_type = DataType::Map(
9542            Arc::new(Field::new(
9543                "entries",
9544                DataType::Struct(
9545                    vec![
9546                        Field::new("key", DataType::Utf8, false),
9547                        Field::new("value", DataType::Utf8, false),
9548                    ]
9549                    .into(),
9550                ),
9551                false,
9552            )),
9553            false,
9554        );
9555
9556        let new_array = cast(&array, &new_type.clone()).unwrap();
9557        assert_eq!(new_type, new_array.data_type().clone());
9558        let map_array = new_array.as_map();
9559
9560        assert_ne!(new_type, array.data_type().clone());
9561        assert_eq!(new_type, map_array.data_type().clone());
9562
9563        let key_string = map_array
9564            .keys()
9565            .as_any()
9566            .downcast_ref::<StringArray>()
9567            .unwrap()
9568            .into_iter()
9569            .flatten()
9570            .collect::<Vec<_>>();
9571        assert_eq!(&key_string, &vec!["0", "1"]);
9572
9573        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9574        let values_string = values_string_array
9575            .as_any()
9576            .downcast_ref::<StringArray>()
9577            .unwrap()
9578            .into_iter()
9579            .flatten()
9580            .collect::<Vec<_>>();
9581        assert_eq!(&values_string, &vec!["44", "22"]);
9582    }
9583
9584    #[test]
9585    fn test_utf8_cast_offsets() {
9586        // test if offset of the array is taken into account during cast
9587        let str_array = StringArray::from(vec!["a", "b", "c"]);
9588        let str_array = str_array.slice(1, 2);
9589
9590        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9591
9592        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9593        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9594        assert_eq!(strs, &["b", "c"])
9595    }
9596
9597    #[test]
9598    fn test_list_cast_offsets() {
9599        // test if offset of the array is taken into account during cast
9600        let array1 = make_list_array().slice(1, 2);
9601        let array2 = Arc::new(make_list_array()) as ArrayRef;
9602
9603        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9604        let out1 = cast(&array1, &dt).unwrap();
9605        let out2 = cast(&array2, &dt).unwrap();
9606
9607        assert_eq!(&out1, &out2.slice(1, 2))
9608    }
9609
9610    #[test]
9611    fn test_list_to_string() {
9612        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
9613        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
9614        let value_data = str_array.into_data();
9615
9616        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
9617        let list_data = ArrayData::builder(list_data_type)
9618            .len(3)
9619            .add_buffer(value_offsets)
9620            .add_child_data(value_data)
9621            .build()
9622            .unwrap();
9623        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
9624
9625        let out = cast(&array, &DataType::Utf8).unwrap();
9626        let out = out
9627            .as_any()
9628            .downcast_ref::<StringArray>()
9629            .unwrap()
9630            .into_iter()
9631            .flatten()
9632            .collect::<Vec<_>>();
9633        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9634
9635        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9636        let out = out
9637            .as_any()
9638            .downcast_ref::<LargeStringArray>()
9639            .unwrap()
9640            .into_iter()
9641            .flatten()
9642            .collect::<Vec<_>>();
9643        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9644
9645        let array = Arc::new(make_list_array()) as ArrayRef;
9646        let out = cast(&array, &DataType::Utf8).unwrap();
9647        let out = out
9648            .as_any()
9649            .downcast_ref::<StringArray>()
9650            .unwrap()
9651            .into_iter()
9652            .flatten()
9653            .collect::<Vec<_>>();
9654        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9655
9656        let array = Arc::new(make_large_list_array()) as ArrayRef;
9657        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9658        let out = out
9659            .as_any()
9660            .downcast_ref::<LargeStringArray>()
9661            .unwrap()
9662            .into_iter()
9663            .flatten()
9664            .collect::<Vec<_>>();
9665        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9666    }
9667
9668    #[test]
9669    fn test_cast_f64_to_decimal128() {
9670        // to reproduce https://github.com/apache/arrow-rs/issues/2997
9671
9672        let decimal_type = DataType::Decimal128(18, 2);
9673        let array = Float64Array::from(vec![
9674            Some(0.0699999999),
9675            Some(0.0659999999),
9676            Some(0.0650000000),
9677            Some(0.0649999999),
9678        ]);
9679        let array = Arc::new(array) as ArrayRef;
9680        generate_cast_test_case!(
9681            &array,
9682            Decimal128Array,
9683            &decimal_type,
9684            vec![
9685                Some(7_i128), // round up
9686                Some(7_i128), // round up
9687                Some(7_i128), // round up
9688                Some(6_i128), // round down
9689            ]
9690        );
9691
9692        let decimal_type = DataType::Decimal128(18, 3);
9693        let array = Float64Array::from(vec![
9694            Some(0.0699999999),
9695            Some(0.0659999999),
9696            Some(0.0650000000),
9697            Some(0.0649999999),
9698        ]);
9699        let array = Arc::new(array) as ArrayRef;
9700        generate_cast_test_case!(
9701            &array,
9702            Decimal128Array,
9703            &decimal_type,
9704            vec![
9705                Some(70_i128), // round up
9706                Some(66_i128), // round up
9707                Some(65_i128), // round down
9708                Some(65_i128), // round up
9709            ]
9710        );
9711    }
9712
9713    #[test]
9714    fn test_cast_numeric_to_decimal128_overflow() {
9715        let array = Int64Array::from(vec![i64::MAX]);
9716        let array = Arc::new(array) as ArrayRef;
9717        let casted_array = cast_with_options(
9718            &array,
9719            &DataType::Decimal128(38, 30),
9720            &CastOptions {
9721                safe: true,
9722                format_options: FormatOptions::default(),
9723            },
9724        );
9725        assert!(casted_array.is_ok());
9726        assert!(casted_array.unwrap().is_null(0));
9727
9728        let casted_array = cast_with_options(
9729            &array,
9730            &DataType::Decimal128(38, 30),
9731            &CastOptions {
9732                safe: false,
9733                format_options: FormatOptions::default(),
9734            },
9735        );
9736        assert!(casted_array.is_err());
9737    }
9738
9739    #[test]
9740    fn test_cast_numeric_to_decimal256_overflow() {
9741        let array = Int64Array::from(vec![i64::MAX]);
9742        let array = Arc::new(array) as ArrayRef;
9743        let casted_array = cast_with_options(
9744            &array,
9745            &DataType::Decimal256(76, 76),
9746            &CastOptions {
9747                safe: true,
9748                format_options: FormatOptions::default(),
9749            },
9750        );
9751        assert!(casted_array.is_ok());
9752        assert!(casted_array.unwrap().is_null(0));
9753
9754        let casted_array = cast_with_options(
9755            &array,
9756            &DataType::Decimal256(76, 76),
9757            &CastOptions {
9758                safe: false,
9759                format_options: FormatOptions::default(),
9760            },
9761        );
9762        assert!(casted_array.is_err());
9763    }
9764
9765    #[test]
9766    fn test_cast_floating_point_to_decimal128_precision_overflow() {
9767        let array = Float64Array::from(vec![1.1]);
9768        let array = Arc::new(array) as ArrayRef;
9769        let casted_array = cast_with_options(
9770            &array,
9771            &DataType::Decimal128(2, 2),
9772            &CastOptions {
9773                safe: true,
9774                format_options: FormatOptions::default(),
9775            },
9776        );
9777        assert!(casted_array.is_ok());
9778        assert!(casted_array.unwrap().is_null(0));
9779
9780        let casted_array = cast_with_options(
9781            &array,
9782            &DataType::Decimal128(2, 2),
9783            &CastOptions {
9784                safe: false,
9785                format_options: FormatOptions::default(),
9786            },
9787        );
9788        let err = casted_array.unwrap_err().to_string();
9789        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
9790        assert!(
9791            err.contains(expected_error),
9792            "did not find expected error '{expected_error}' in actual error '{err}'"
9793        );
9794    }
9795
9796    #[test]
9797    fn test_cast_floating_point_to_decimal256_precision_overflow() {
9798        let array = Float64Array::from(vec![1.1]);
9799        let array = Arc::new(array) as ArrayRef;
9800        let casted_array = cast_with_options(
9801            &array,
9802            &DataType::Decimal256(2, 2),
9803            &CastOptions {
9804                safe: true,
9805                format_options: FormatOptions::default(),
9806            },
9807        );
9808        assert!(casted_array.is_ok());
9809        assert!(casted_array.unwrap().is_null(0));
9810
9811        let casted_array = cast_with_options(
9812            &array,
9813            &DataType::Decimal256(2, 2),
9814            &CastOptions {
9815                safe: false,
9816                format_options: FormatOptions::default(),
9817            },
9818        );
9819        let err = casted_array.unwrap_err().to_string();
9820        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
9821        assert_eq!(err, expected_error);
9822    }
9823
9824    #[test]
9825    fn test_cast_floating_point_to_decimal128_overflow() {
9826        let array = Float64Array::from(vec![f64::MAX]);
9827        let array = Arc::new(array) as ArrayRef;
9828        let casted_array = cast_with_options(
9829            &array,
9830            &DataType::Decimal128(38, 30),
9831            &CastOptions {
9832                safe: true,
9833                format_options: FormatOptions::default(),
9834            },
9835        );
9836        assert!(casted_array.is_ok());
9837        assert!(casted_array.unwrap().is_null(0));
9838
9839        let casted_array = cast_with_options(
9840            &array,
9841            &DataType::Decimal128(38, 30),
9842            &CastOptions {
9843                safe: false,
9844                format_options: FormatOptions::default(),
9845            },
9846        );
9847        let err = casted_array.unwrap_err().to_string();
9848        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
9849        assert!(
9850            err.contains(expected_error),
9851            "did not find expected error '{expected_error}' in actual error '{err}'"
9852        );
9853    }
9854
9855    #[test]
9856    fn test_cast_floating_point_to_decimal256_overflow() {
9857        let array = Float64Array::from(vec![f64::MAX]);
9858        let array = Arc::new(array) as ArrayRef;
9859        let casted_array = cast_with_options(
9860            &array,
9861            &DataType::Decimal256(76, 50),
9862            &CastOptions {
9863                safe: true,
9864                format_options: FormatOptions::default(),
9865            },
9866        );
9867        assert!(casted_array.is_ok());
9868        assert!(casted_array.unwrap().is_null(0));
9869
9870        let casted_array = cast_with_options(
9871            &array,
9872            &DataType::Decimal256(76, 50),
9873            &CastOptions {
9874                safe: false,
9875                format_options: FormatOptions::default(),
9876            },
9877        );
9878        let err = casted_array.unwrap_err().to_string();
9879        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
9880        assert!(
9881            err.contains(expected_error),
9882            "did not find expected error '{expected_error}' in actual error '{err}'"
9883        );
9884    }
9885    #[test]
9886    fn test_cast_decimal256_to_f64_no_overflow() {
9887        // Test casting i256::MAX: should produce a large finite positive value
9888        let array = vec![Some(i256::MAX)];
9889        let array = create_decimal256_array(array, 76, 2).unwrap();
9890        let array = Arc::new(array) as ArrayRef;
9891
9892        let result = cast(&array, &DataType::Float64).unwrap();
9893        let result = result.as_primitive::<Float64Type>();
9894        assert!(result.value(0).is_finite());
9895        assert!(result.value(0) > 0.0); // Positive result
9896
9897        // Test casting i256::MIN: should produce a large finite negative value
9898        let array = vec![Some(i256::MIN)];
9899        let array = create_decimal256_array(array, 76, 2).unwrap();
9900        let array = Arc::new(array) as ArrayRef;
9901
9902        let result = cast(&array, &DataType::Float64).unwrap();
9903        let result = result.as_primitive::<Float64Type>();
9904        assert!(result.value(0).is_finite());
9905        assert!(result.value(0) < 0.0); // Negative result
9906    }
9907
9908    #[test]
9909    fn test_cast_decimal128_to_decimal128_negative_scale() {
9910        let input_type = DataType::Decimal128(20, 0);
9911        let output_type = DataType::Decimal128(20, -1);
9912        assert!(can_cast_types(&input_type, &output_type));
9913        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
9914        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
9915        let array = Arc::new(input_decimal_array) as ArrayRef;
9916        generate_cast_test_case!(
9917            &array,
9918            Decimal128Array,
9919            &output_type,
9920            vec![
9921                Some(112345_i128),
9922                Some(212346_i128),
9923                Some(312346_i128),
9924                None
9925            ]
9926        );
9927
9928        let casted_array = cast(&array, &output_type).unwrap();
9929        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9930
9931        assert_eq!("1123450", decimal_arr.value_as_string(0));
9932        assert_eq!("2123460", decimal_arr.value_as_string(1));
9933        assert_eq!("3123460", decimal_arr.value_as_string(2));
9934    }
9935
9936    #[test]
9937    fn decimal128_min_max_to_f64() {
9938        // Ensure Decimal128 i128::MIN/MAX round-trip cast
9939        let min128 = i128::MIN;
9940        let max128 = i128::MAX;
9941        assert_eq!(min128 as f64, min128 as f64);
9942        assert_eq!(max128 as f64, max128 as f64);
9943    }
9944
9945    #[test]
9946    fn test_cast_numeric_to_decimal128_negative() {
9947        let decimal_type = DataType::Decimal128(38, -1);
9948        let array = Arc::new(Int32Array::from(vec![
9949            Some(1123456),
9950            Some(2123456),
9951            Some(3123456),
9952        ])) as ArrayRef;
9953
9954        let casted_array = cast(&array, &decimal_type).unwrap();
9955        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9956
9957        assert_eq!("1123450", decimal_arr.value_as_string(0));
9958        assert_eq!("2123450", decimal_arr.value_as_string(1));
9959        assert_eq!("3123450", decimal_arr.value_as_string(2));
9960
9961        let array = Arc::new(Float32Array::from(vec![
9962            Some(1123.456),
9963            Some(2123.456),
9964            Some(3123.456),
9965        ])) as ArrayRef;
9966
9967        let casted_array = cast(&array, &decimal_type).unwrap();
9968        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9969
9970        assert_eq!("1120", decimal_arr.value_as_string(0));
9971        assert_eq!("2120", decimal_arr.value_as_string(1));
9972        assert_eq!("3120", decimal_arr.value_as_string(2));
9973    }
9974
9975    #[test]
9976    fn test_cast_decimal128_to_decimal128_negative() {
9977        let input_type = DataType::Decimal128(10, -1);
9978        let output_type = DataType::Decimal128(10, -2);
9979        assert!(can_cast_types(&input_type, &output_type));
9980        let array = vec![Some(123)];
9981        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9982        let array = Arc::new(input_decimal_array) as ArrayRef;
9983        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
9984
9985        let casted_array = cast(&array, &output_type).unwrap();
9986        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9987
9988        assert_eq!("1200", decimal_arr.value_as_string(0));
9989
9990        let array = vec![Some(125)];
9991        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9992        let array = Arc::new(input_decimal_array) as ArrayRef;
9993        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
9994
9995        let casted_array = cast(&array, &output_type).unwrap();
9996        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9997
9998        assert_eq!("1300", decimal_arr.value_as_string(0));
9999    }
10000
10001    #[test]
10002    fn test_cast_decimal128_to_decimal256_negative() {
10003        let input_type = DataType::Decimal128(10, 3);
10004        let output_type = DataType::Decimal256(10, 5);
10005        assert!(can_cast_types(&input_type, &output_type));
10006        let array = vec![Some(123456), Some(-123456)];
10007        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10008        let array = Arc::new(input_decimal_array) as ArrayRef;
10009
10010        let hundred = i256::from_i128(100);
10011        generate_cast_test_case!(
10012            &array,
10013            Decimal256Array,
10014            &output_type,
10015            vec![
10016                Some(i256::from_i128(123456).mul_wrapping(hundred)),
10017                Some(i256::from_i128(-123456).mul_wrapping(hundred))
10018            ]
10019        );
10020    }
10021
10022    #[test]
10023    fn test_parse_string_to_decimal() {
10024        assert_eq!(
10025            Decimal128Type::format_decimal(
10026                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10027                38,
10028                2,
10029            ),
10030            "123.45"
10031        );
10032        assert_eq!(
10033            Decimal128Type::format_decimal(
10034                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10035                38,
10036                2,
10037            ),
10038            "12345.00"
10039        );
10040        assert_eq!(
10041            Decimal128Type::format_decimal(
10042                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10043                38,
10044                2,
10045            ),
10046            "0.12"
10047        );
10048        assert_eq!(
10049            Decimal128Type::format_decimal(
10050                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10051                38,
10052                2,
10053            ),
10054            "0.12"
10055        );
10056        assert_eq!(
10057            Decimal128Type::format_decimal(
10058                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10059                38,
10060                2,
10061            ),
10062            "0.13"
10063        );
10064        assert_eq!(
10065            Decimal128Type::format_decimal(
10066                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10067                38,
10068                2,
10069            ),
10070            "0.13"
10071        );
10072
10073        assert_eq!(
10074            Decimal256Type::format_decimal(
10075                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
10076                38,
10077                3,
10078            ),
10079            "123.450"
10080        );
10081        assert_eq!(
10082            Decimal256Type::format_decimal(
10083                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
10084                38,
10085                3,
10086            ),
10087            "12345.000"
10088        );
10089        assert_eq!(
10090            Decimal256Type::format_decimal(
10091                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
10092                38,
10093                3,
10094            ),
10095            "0.123"
10096        );
10097        assert_eq!(
10098            Decimal256Type::format_decimal(
10099                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
10100                38,
10101                3,
10102            ),
10103            "0.123"
10104        );
10105        assert_eq!(
10106            Decimal256Type::format_decimal(
10107                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
10108                38,
10109                3,
10110            ),
10111            "0.127"
10112        );
10113    }
10114
10115    fn test_cast_string_to_decimal(array: ArrayRef) {
10116        // Decimal128
10117        let output_type = DataType::Decimal128(38, 2);
10118        assert!(can_cast_types(array.data_type(), &output_type));
10119
10120        let casted_array = cast(&array, &output_type).unwrap();
10121        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10122
10123        assert_eq!("123.45", decimal_arr.value_as_string(0));
10124        assert_eq!("1.23", decimal_arr.value_as_string(1));
10125        assert_eq!("0.12", decimal_arr.value_as_string(2));
10126        assert_eq!("0.13", decimal_arr.value_as_string(3));
10127        assert_eq!("1.26", decimal_arr.value_as_string(4));
10128        assert_eq!("12345.00", decimal_arr.value_as_string(5));
10129        assert_eq!("12345.00", decimal_arr.value_as_string(6));
10130        assert_eq!("0.12", decimal_arr.value_as_string(7));
10131        assert_eq!("12.23", decimal_arr.value_as_string(8));
10132        assert!(decimal_arr.is_null(9));
10133        assert_eq!("0.00", decimal_arr.value_as_string(10));
10134        assert_eq!("0.00", decimal_arr.value_as_string(11));
10135        assert!(decimal_arr.is_null(12));
10136        assert_eq!("-1.23", decimal_arr.value_as_string(13));
10137        assert_eq!("-1.24", decimal_arr.value_as_string(14));
10138        assert_eq!("0.00", decimal_arr.value_as_string(15));
10139        assert_eq!("-123.00", decimal_arr.value_as_string(16));
10140        assert_eq!("-123.23", decimal_arr.value_as_string(17));
10141        assert_eq!("-0.12", decimal_arr.value_as_string(18));
10142        assert_eq!("1.23", decimal_arr.value_as_string(19));
10143        assert_eq!("1.24", decimal_arr.value_as_string(20));
10144        assert_eq!("0.00", decimal_arr.value_as_string(21));
10145        assert_eq!("123.00", decimal_arr.value_as_string(22));
10146        assert_eq!("123.23", decimal_arr.value_as_string(23));
10147        assert_eq!("0.12", decimal_arr.value_as_string(24));
10148        assert!(decimal_arr.is_null(25));
10149        assert!(decimal_arr.is_null(26));
10150        assert!(decimal_arr.is_null(27));
10151        assert_eq!("0.00", decimal_arr.value_as_string(28));
10152        assert_eq!("0.00", decimal_arr.value_as_string(29));
10153        assert_eq!("12345.00", decimal_arr.value_as_string(30));
10154        assert_eq!(decimal_arr.len(), 31);
10155
10156        // Decimal256
10157        let output_type = DataType::Decimal256(76, 3);
10158        assert!(can_cast_types(array.data_type(), &output_type));
10159
10160        let casted_array = cast(&array, &output_type).unwrap();
10161        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10162
10163        assert_eq!("123.450", decimal_arr.value_as_string(0));
10164        assert_eq!("1.235", decimal_arr.value_as_string(1));
10165        assert_eq!("0.123", decimal_arr.value_as_string(2));
10166        assert_eq!("0.127", decimal_arr.value_as_string(3));
10167        assert_eq!("1.263", decimal_arr.value_as_string(4));
10168        assert_eq!("12345.000", decimal_arr.value_as_string(5));
10169        assert_eq!("12345.000", decimal_arr.value_as_string(6));
10170        assert_eq!("0.123", decimal_arr.value_as_string(7));
10171        assert_eq!("12.234", decimal_arr.value_as_string(8));
10172        assert!(decimal_arr.is_null(9));
10173        assert_eq!("0.000", decimal_arr.value_as_string(10));
10174        assert_eq!("0.000", decimal_arr.value_as_string(11));
10175        assert!(decimal_arr.is_null(12));
10176        assert_eq!("-1.235", decimal_arr.value_as_string(13));
10177        assert_eq!("-1.236", decimal_arr.value_as_string(14));
10178        assert_eq!("0.000", decimal_arr.value_as_string(15));
10179        assert_eq!("-123.000", decimal_arr.value_as_string(16));
10180        assert_eq!("-123.234", decimal_arr.value_as_string(17));
10181        assert_eq!("-0.123", decimal_arr.value_as_string(18));
10182        assert_eq!("1.235", decimal_arr.value_as_string(19));
10183        assert_eq!("1.236", decimal_arr.value_as_string(20));
10184        assert_eq!("0.000", decimal_arr.value_as_string(21));
10185        assert_eq!("123.000", decimal_arr.value_as_string(22));
10186        assert_eq!("123.234", decimal_arr.value_as_string(23));
10187        assert_eq!("0.123", decimal_arr.value_as_string(24));
10188        assert!(decimal_arr.is_null(25));
10189        assert!(decimal_arr.is_null(26));
10190        assert!(decimal_arr.is_null(27));
10191        assert_eq!("0.000", decimal_arr.value_as_string(28));
10192        assert_eq!("0.000", decimal_arr.value_as_string(29));
10193        assert_eq!("12345.000", decimal_arr.value_as_string(30));
10194        assert_eq!(decimal_arr.len(), 31);
10195    }
10196
10197    #[test]
10198    fn test_cast_utf8_to_decimal() {
10199        let str_array = StringArray::from(vec![
10200            Some("123.45"),
10201            Some("1.2345"),
10202            Some("0.12345"),
10203            Some("0.1267"),
10204            Some("1.263"),
10205            Some("12345.0"),
10206            Some("12345"),
10207            Some("000.123"),
10208            Some("12.234000"),
10209            None,
10210            Some(""),
10211            Some(" "),
10212            None,
10213            Some("-1.23499999"),
10214            Some("-1.23599999"),
10215            Some("-0.00001"),
10216            Some("-123"),
10217            Some("-123.234000"),
10218            Some("-000.123"),
10219            Some("+1.23499999"),
10220            Some("+1.23599999"),
10221            Some("+0.00001"),
10222            Some("+123"),
10223            Some("+123.234000"),
10224            Some("+000.123"),
10225            Some("1.-23499999"),
10226            Some("-1.-23499999"),
10227            Some("--1.23499999"),
10228            Some("0"),
10229            Some("000.000"),
10230            Some("0000000000000000012345.000"),
10231        ]);
10232        let array = Arc::new(str_array) as ArrayRef;
10233
10234        test_cast_string_to_decimal(array);
10235
10236        let test_cases = [
10237            (None, None),
10238            // (Some(""), None),
10239            // (Some("   "), None),
10240            (Some("0"), Some("0")),
10241            (Some("000.000"), Some("0")),
10242            (Some("12345"), Some("12345")),
10243            (Some("000000000000000000000000000012345"), Some("12345")),
10244            (Some("-123"), Some("-123")),
10245            (Some("+123"), Some("123")),
10246        ];
10247        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10248        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10249
10250        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
10251        test_cast_string_to_decimal_scale_zero(array, &expected);
10252    }
10253
10254    #[test]
10255    fn test_cast_large_utf8_to_decimal() {
10256        let str_array = LargeStringArray::from(vec![
10257            Some("123.45"),
10258            Some("1.2345"),
10259            Some("0.12345"),
10260            Some("0.1267"),
10261            Some("1.263"),
10262            Some("12345.0"),
10263            Some("12345"),
10264            Some("000.123"),
10265            Some("12.234000"),
10266            None,
10267            Some(""),
10268            Some(" "),
10269            None,
10270            Some("-1.23499999"),
10271            Some("-1.23599999"),
10272            Some("-0.00001"),
10273            Some("-123"),
10274            Some("-123.234000"),
10275            Some("-000.123"),
10276            Some("+1.23499999"),
10277            Some("+1.23599999"),
10278            Some("+0.00001"),
10279            Some("+123"),
10280            Some("+123.234000"),
10281            Some("+000.123"),
10282            Some("1.-23499999"),
10283            Some("-1.-23499999"),
10284            Some("--1.23499999"),
10285            Some("0"),
10286            Some("000.000"),
10287            Some("0000000000000000012345.000"),
10288        ]);
10289        let array = Arc::new(str_array) as ArrayRef;
10290
10291        test_cast_string_to_decimal(array);
10292
10293        let test_cases = [
10294            (None, None),
10295            (Some(""), None),
10296            (Some("   "), None),
10297            (Some("0"), Some("0")),
10298            (Some("000.000"), Some("0")),
10299            (Some("12345"), Some("12345")),
10300            (Some("000000000000000000000000000012345"), Some("12345")),
10301            (Some("-123"), Some("-123")),
10302            (Some("+123"), Some("123")),
10303        ];
10304        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10305        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10306
10307        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
10308        test_cast_string_to_decimal_scale_zero(array, &expected);
10309    }
10310
10311    fn test_cast_string_to_decimal_scale_zero(
10312        array: ArrayRef,
10313        expected_as_string: &[Option<&str>],
10314    ) {
10315        // Decimal128
10316        let output_type = DataType::Decimal128(38, 0);
10317        assert!(can_cast_types(array.data_type(), &output_type));
10318        let casted_array = cast(&array, &output_type).unwrap();
10319        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10320        assert_decimal_array_contents(decimal_arr, expected_as_string);
10321
10322        // Decimal256
10323        let output_type = DataType::Decimal256(76, 0);
10324        assert!(can_cast_types(array.data_type(), &output_type));
10325        let casted_array = cast(&array, &output_type).unwrap();
10326        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10327        assert_decimal_array_contents(decimal_arr, expected_as_string);
10328    }
10329
10330    fn assert_decimal_array_contents<T>(
10331        array: &PrimitiveArray<T>,
10332        expected_as_string: &[Option<&str>],
10333    ) where
10334        T: DecimalType + ArrowPrimitiveType,
10335    {
10336        assert_eq!(array.len(), expected_as_string.len());
10337        for (i, expected) in expected_as_string.iter().enumerate() {
10338            let actual = if array.is_null(i) {
10339                None
10340            } else {
10341                Some(array.value_as_string(i))
10342            };
10343            let actual = actual.as_ref().map(|s| s.as_ref());
10344            assert_eq!(*expected, actual, "Expected at position {i}");
10345        }
10346    }
10347
10348    #[test]
10349    fn test_cast_invalid_utf8_to_decimal() {
10350        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
10351        let array = Arc::new(str_array) as ArrayRef;
10352
10353        // Safe cast
10354        let output_type = DataType::Decimal128(38, 2);
10355        let casted_array = cast(&array, &output_type).unwrap();
10356        assert!(casted_array.is_null(0));
10357        assert!(casted_array.is_null(1));
10358
10359        let output_type = DataType::Decimal256(76, 2);
10360        let casted_array = cast(&array, &output_type).unwrap();
10361        assert!(casted_array.is_null(0));
10362        assert!(casted_array.is_null(1));
10363
10364        // Non-safe cast
10365        let output_type = DataType::Decimal128(38, 2);
10366        let str_array = StringArray::from(vec!["4.4.5"]);
10367        let array = Arc::new(str_array) as ArrayRef;
10368        let option = CastOptions {
10369            safe: false,
10370            format_options: FormatOptions::default(),
10371        };
10372        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10373        assert!(
10374            casted_err
10375                .to_string()
10376                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
10377        );
10378
10379        let str_array = StringArray::from(vec![". 0.123"]);
10380        let array = Arc::new(str_array) as ArrayRef;
10381        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10382        assert!(
10383            casted_err
10384                .to_string()
10385                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
10386        );
10387    }
10388
10389    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
10390        let output_type = DataType::Decimal128(38, 2);
10391        let casted_array = cast(&overflow_array, &output_type).unwrap();
10392        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10393
10394        assert!(decimal_arr.is_null(0));
10395        assert!(decimal_arr.is_null(1));
10396        assert!(decimal_arr.is_null(2));
10397        assert_eq!(
10398            "999999999999999999999999999999999999.99",
10399            decimal_arr.value_as_string(3)
10400        );
10401        assert_eq!(
10402            "100000000000000000000000000000000000.00",
10403            decimal_arr.value_as_string(4)
10404        );
10405    }
10406
10407    #[test]
10408    fn test_cast_string_to_decimal128_precision_overflow() {
10409        let array = StringArray::from(vec!["1000".to_string()]);
10410        let array = Arc::new(array) as ArrayRef;
10411        let casted_array = cast_with_options(
10412            &array,
10413            &DataType::Decimal128(10, 8),
10414            &CastOptions {
10415                safe: true,
10416                format_options: FormatOptions::default(),
10417            },
10418        );
10419        assert!(casted_array.is_ok());
10420        assert!(casted_array.unwrap().is_null(0));
10421
10422        let err = cast_with_options(
10423            &array,
10424            &DataType::Decimal128(10, 8),
10425            &CastOptions {
10426                safe: false,
10427                format_options: FormatOptions::default(),
10428            },
10429        );
10430        assert_eq!(
10431            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
10432            err.unwrap_err().to_string()
10433        );
10434    }
10435
10436    #[test]
10437    fn test_cast_utf8_to_decimal128_overflow() {
10438        let overflow_str_array = StringArray::from(vec![
10439            i128::MAX.to_string(),
10440            i128::MIN.to_string(),
10441            "99999999999999999999999999999999999999".to_string(),
10442            "999999999999999999999999999999999999.99".to_string(),
10443            "99999999999999999999999999999999999.999".to_string(),
10444        ]);
10445        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10446
10447        test_cast_string_to_decimal128_overflow(overflow_array);
10448    }
10449
10450    #[test]
10451    fn test_cast_large_utf8_to_decimal128_overflow() {
10452        let overflow_str_array = LargeStringArray::from(vec![
10453            i128::MAX.to_string(),
10454            i128::MIN.to_string(),
10455            "99999999999999999999999999999999999999".to_string(),
10456            "999999999999999999999999999999999999.99".to_string(),
10457            "99999999999999999999999999999999999.999".to_string(),
10458        ]);
10459        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10460
10461        test_cast_string_to_decimal128_overflow(overflow_array);
10462    }
10463
10464    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
10465        let output_type = DataType::Decimal256(76, 2);
10466        let casted_array = cast(&overflow_array, &output_type).unwrap();
10467        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10468
10469        assert_eq!(
10470            "170141183460469231731687303715884105727.00",
10471            decimal_arr.value_as_string(0)
10472        );
10473        assert_eq!(
10474            "-170141183460469231731687303715884105728.00",
10475            decimal_arr.value_as_string(1)
10476        );
10477        assert_eq!(
10478            "99999999999999999999999999999999999999.00",
10479            decimal_arr.value_as_string(2)
10480        );
10481        assert_eq!(
10482            "999999999999999999999999999999999999.99",
10483            decimal_arr.value_as_string(3)
10484        );
10485        assert_eq!(
10486            "100000000000000000000000000000000000.00",
10487            decimal_arr.value_as_string(4)
10488        );
10489        assert!(decimal_arr.is_null(5));
10490        assert!(decimal_arr.is_null(6));
10491    }
10492
10493    #[test]
10494    fn test_cast_string_to_decimal256_precision_overflow() {
10495        let array = StringArray::from(vec!["1000".to_string()]);
10496        let array = Arc::new(array) as ArrayRef;
10497        let casted_array = cast_with_options(
10498            &array,
10499            &DataType::Decimal256(10, 8),
10500            &CastOptions {
10501                safe: true,
10502                format_options: FormatOptions::default(),
10503            },
10504        );
10505        assert!(casted_array.is_ok());
10506        assert!(casted_array.unwrap().is_null(0));
10507
10508        let err = cast_with_options(
10509            &array,
10510            &DataType::Decimal256(10, 8),
10511            &CastOptions {
10512                safe: false,
10513                format_options: FormatOptions::default(),
10514            },
10515        );
10516        assert_eq!(
10517            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
10518            err.unwrap_err().to_string()
10519        );
10520    }
10521
10522    #[test]
10523    fn test_cast_utf8_to_decimal256_overflow() {
10524        let overflow_str_array = StringArray::from(vec![
10525            i128::MAX.to_string(),
10526            i128::MIN.to_string(),
10527            "99999999999999999999999999999999999999".to_string(),
10528            "999999999999999999999999999999999999.99".to_string(),
10529            "99999999999999999999999999999999999.999".to_string(),
10530            i256::MAX.to_string(),
10531            i256::MIN.to_string(),
10532        ]);
10533        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10534
10535        test_cast_string_to_decimal256_overflow(overflow_array);
10536    }
10537
10538    #[test]
10539    fn test_cast_large_utf8_to_decimal256_overflow() {
10540        let overflow_str_array = LargeStringArray::from(vec![
10541            i128::MAX.to_string(),
10542            i128::MIN.to_string(),
10543            "99999999999999999999999999999999999999".to_string(),
10544            "999999999999999999999999999999999999.99".to_string(),
10545            "99999999999999999999999999999999999.999".to_string(),
10546            i256::MAX.to_string(),
10547            i256::MIN.to_string(),
10548        ]);
10549        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10550
10551        test_cast_string_to_decimal256_overflow(overflow_array);
10552    }
10553
10554    #[test]
10555    fn test_cast_outside_supported_range_for_nanoseconds() {
10556        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";
10557
10558        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
10559
10560        let cast_options = CastOptions {
10561            safe: false,
10562            format_options: FormatOptions::default(),
10563        };
10564
10565        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
10566            &array,
10567            &None::<Arc<str>>,
10568            &cast_options,
10569        );
10570
10571        let err = result.unwrap_err();
10572        assert_eq!(
10573            err.to_string(),
10574            format!(
10575                "Cast error: Overflow converting {} to Nanosecond. {}",
10576                array.value(0),
10577                EXPECTED_ERROR_MESSAGE
10578            )
10579        );
10580    }
10581
10582    #[test]
10583    fn test_cast_date32_to_timestamp() {
10584        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10585        let array = Arc::new(a) as ArrayRef;
10586        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10587        let c = b.as_primitive::<TimestampSecondType>();
10588        assert_eq!(1609459200, c.value(0));
10589        assert_eq!(1640995200, c.value(1));
10590        assert!(c.is_null(2));
10591    }
10592
10593    #[test]
10594    fn test_cast_date32_to_timestamp_ms() {
10595        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10596        let array = Arc::new(a) as ArrayRef;
10597        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10598        let c = b
10599            .as_any()
10600            .downcast_ref::<TimestampMillisecondArray>()
10601            .unwrap();
10602        assert_eq!(1609459200000, c.value(0));
10603        assert_eq!(1640995200000, c.value(1));
10604        assert!(c.is_null(2));
10605    }
10606
10607    #[test]
10608    fn test_cast_date32_to_timestamp_us() {
10609        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10610        let array = Arc::new(a) as ArrayRef;
10611        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10612        let c = b
10613            .as_any()
10614            .downcast_ref::<TimestampMicrosecondArray>()
10615            .unwrap();
10616        assert_eq!(1609459200000000, c.value(0));
10617        assert_eq!(1640995200000000, c.value(1));
10618        assert!(c.is_null(2));
10619    }
10620
10621    #[test]
10622    fn test_cast_date32_to_timestamp_ns() {
10623        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10624        let array = Arc::new(a) as ArrayRef;
10625        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10626        let c = b
10627            .as_any()
10628            .downcast_ref::<TimestampNanosecondArray>()
10629            .unwrap();
10630        assert_eq!(1609459200000000000, c.value(0));
10631        assert_eq!(1640995200000000000, c.value(1));
10632        assert!(c.is_null(2));
10633    }
10634
10635    #[test]
10636    fn test_timezone_cast() {
10637        let a = StringArray::from(vec![
10638            "2000-01-01T12:00:00", // date + time valid
10639            "2020-12-15T12:34:56", // date + time valid
10640        ]);
10641        let array = Arc::new(a) as ArrayRef;
10642        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10643        let v = b.as_primitive::<TimestampNanosecondType>();
10644
10645        assert_eq!(v.value(0), 946728000000000000);
10646        assert_eq!(v.value(1), 1608035696000000000);
10647
10648        let b = cast(
10649            &b,
10650            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10651        )
10652        .unwrap();
10653        let v = b.as_primitive::<TimestampNanosecondType>();
10654
10655        assert_eq!(v.value(0), 946728000000000000);
10656        assert_eq!(v.value(1), 1608035696000000000);
10657
10658        let b = cast(
10659            &b,
10660            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10661        )
10662        .unwrap();
10663        let v = b.as_primitive::<TimestampMillisecondType>();
10664
10665        assert_eq!(v.value(0), 946728000000);
10666        assert_eq!(v.value(1), 1608035696000);
10667    }
10668
10669    #[test]
10670    fn test_cast_utf8_to_timestamp() {
10671        fn test_tz(tz: Arc<str>) {
10672            let valid = StringArray::from(vec![
10673                "2023-01-01 04:05:06.789000-08:00",
10674                "2023-01-01 04:05:06.789000-07:00",
10675                "2023-01-01 04:05:06.789 -0800",
10676                "2023-01-01 04:05:06.789 -08:00",
10677                "2023-01-01 040506 +0730",
10678                "2023-01-01 040506 +07:30",
10679                "2023-01-01 04:05:06.789",
10680                "2023-01-01 04:05:06",
10681                "2023-01-01",
10682            ]);
10683
10684            let array = Arc::new(valid) as ArrayRef;
10685            let b = cast_with_options(
10686                &array,
10687                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
10688                &CastOptions {
10689                    safe: false,
10690                    format_options: FormatOptions::default(),
10691                },
10692            )
10693            .unwrap();
10694
10695            let tz = tz.as_ref().parse().unwrap();
10696
10697            let as_tz =
10698                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
10699
10700            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
10701            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
10702
10703            let values = b.as_primitive::<TimestampNanosecondType>().values();
10704            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
10705            let local_results: Vec<_> = values.iter().map(as_local).collect();
10706
10707            // Absolute timestamps should be parsed preserving the same UTC instant
10708            assert_eq!(
10709                &utc_results[..6],
10710                &[
10711                    "2023-01-01 12:05:06.789".to_string(),
10712                    "2023-01-01 11:05:06.789".to_string(),
10713                    "2023-01-01 12:05:06.789".to_string(),
10714                    "2023-01-01 12:05:06.789".to_string(),
10715                    "2022-12-31 20:35:06".to_string(),
10716                    "2022-12-31 20:35:06".to_string(),
10717                ]
10718            );
10719            // Non-absolute timestamps should be parsed preserving the same local instant
10720            assert_eq!(
10721                &local_results[6..],
10722                &[
10723                    "2023-01-01 04:05:06.789".to_string(),
10724                    "2023-01-01 04:05:06".to_string(),
10725                    "2023-01-01 00:00:00".to_string()
10726                ]
10727            )
10728        }
10729
10730        test_tz("+00:00".into());
10731        test_tz("+02:00".into());
10732    }
10733
10734    #[test]
10735    fn test_cast_invalid_utf8() {
10736        let v1: &[u8] = b"\xFF invalid";
10737        let v2: &[u8] = b"\x00 Foo";
10738        let s = BinaryArray::from(vec![v1, v2]);
10739        let options = CastOptions {
10740            safe: true,
10741            format_options: FormatOptions::default(),
10742        };
10743        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
10744        let a = array.as_string::<i32>();
10745        a.to_data().validate_full().unwrap();
10746
10747        assert_eq!(a.null_count(), 1);
10748        assert_eq!(a.len(), 2);
10749        assert!(a.is_null(0));
10750        assert_eq!(a.value(0), "");
10751        assert_eq!(a.value(1), "\x00 Foo");
10752    }
10753
10754    #[test]
10755    fn test_cast_utf8_to_timestamptz() {
10756        let valid = StringArray::from(vec!["2023-01-01"]);
10757
10758        let array = Arc::new(valid) as ArrayRef;
10759        let b = cast(
10760            &array,
10761            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10762        )
10763        .unwrap();
10764
10765        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
10766
10767        assert_eq!(b.data_type(), &expect);
10768        let c = b
10769            .as_any()
10770            .downcast_ref::<TimestampNanosecondArray>()
10771            .unwrap();
10772        assert_eq!(1672531200000000000, c.value(0));
10773    }
10774
10775    #[test]
10776    fn test_cast_decimal_to_string() {
10777        assert!(can_cast_types(
10778            &DataType::Decimal32(9, 4),
10779            &DataType::Utf8View
10780        ));
10781        assert!(can_cast_types(
10782            &DataType::Decimal64(16, 4),
10783            &DataType::Utf8View
10784        ));
10785        assert!(can_cast_types(
10786            &DataType::Decimal128(10, 4),
10787            &DataType::Utf8View
10788        ));
10789        assert!(can_cast_types(
10790            &DataType::Decimal256(38, 10),
10791            &DataType::Utf8View
10792        ));
10793
10794        macro_rules! assert_decimal_values {
10795            ($array:expr) => {
10796                let c = $array;
10797                assert_eq!("1123.454", c.value(0));
10798                assert_eq!("2123.456", c.value(1));
10799                assert_eq!("-3123.453", c.value(2));
10800                assert_eq!("-3123.456", c.value(3));
10801                assert_eq!("0.000", c.value(4));
10802                assert_eq!("0.123", c.value(5));
10803                assert_eq!("1234.567", c.value(6));
10804                assert_eq!("-1234.567", c.value(7));
10805                assert!(c.is_null(8));
10806            };
10807        }
10808
10809        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
10810            output_type: DataType,
10811            array: PrimitiveArray<IN>,
10812        ) {
10813            let b = cast(&array, &output_type).unwrap();
10814
10815            assert_eq!(b.data_type(), &output_type);
10816            match b.data_type() {
10817                DataType::Utf8View => {
10818                    let c = b.as_string_view();
10819                    assert_decimal_values!(c);
10820                }
10821                DataType::Utf8 | DataType::LargeUtf8 => {
10822                    let c = b.as_string::<OffsetSize>();
10823                    assert_decimal_values!(c);
10824                }
10825                _ => (),
10826            }
10827        }
10828
10829        let array32: Vec<Option<i32>> = vec![
10830            Some(1123454),
10831            Some(2123456),
10832            Some(-3123453),
10833            Some(-3123456),
10834            Some(0),
10835            Some(123),
10836            Some(123456789),
10837            Some(-123456789),
10838            None,
10839        ];
10840        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
10841        let array128: Vec<Option<i128>> =
10842            array64.iter().map(|num| num.map(|x| x as i128)).collect();
10843        let array256: Vec<Option<i256>> = array128
10844            .iter()
10845            .map(|num| num.map(i256::from_i128))
10846            .collect();
10847
10848        test_decimal_to_string::<Decimal32Type, i32>(
10849            DataType::Utf8View,
10850            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10851        );
10852        test_decimal_to_string::<Decimal32Type, i32>(
10853            DataType::Utf8,
10854            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10855        );
10856        test_decimal_to_string::<Decimal32Type, i64>(
10857            DataType::LargeUtf8,
10858            create_decimal32_array(array32, 7, 3).unwrap(),
10859        );
10860
10861        test_decimal_to_string::<Decimal64Type, i32>(
10862            DataType::Utf8View,
10863            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10864        );
10865        test_decimal_to_string::<Decimal64Type, i32>(
10866            DataType::Utf8,
10867            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10868        );
10869        test_decimal_to_string::<Decimal64Type, i64>(
10870            DataType::LargeUtf8,
10871            create_decimal64_array(array64, 7, 3).unwrap(),
10872        );
10873
10874        test_decimal_to_string::<Decimal128Type, i32>(
10875            DataType::Utf8View,
10876            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10877        );
10878        test_decimal_to_string::<Decimal128Type, i32>(
10879            DataType::Utf8,
10880            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10881        );
10882        test_decimal_to_string::<Decimal128Type, i64>(
10883            DataType::LargeUtf8,
10884            create_decimal128_array(array128, 7, 3).unwrap(),
10885        );
10886
10887        test_decimal_to_string::<Decimal256Type, i32>(
10888            DataType::Utf8View,
10889            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10890        );
10891        test_decimal_to_string::<Decimal256Type, i32>(
10892            DataType::Utf8,
10893            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10894        );
10895        test_decimal_to_string::<Decimal256Type, i64>(
10896            DataType::LargeUtf8,
10897            create_decimal256_array(array256, 7, 3).unwrap(),
10898        );
10899    }
10900
10901    #[test]
10902    fn test_cast_numeric_to_decimal128_precision_overflow() {
10903        let array = Int64Array::from(vec![1234567]);
10904        let array = Arc::new(array) as ArrayRef;
10905        let casted_array = cast_with_options(
10906            &array,
10907            &DataType::Decimal128(7, 3),
10908            &CastOptions {
10909                safe: true,
10910                format_options: FormatOptions::default(),
10911            },
10912        );
10913        assert!(casted_array.is_ok());
10914        assert!(casted_array.unwrap().is_null(0));
10915
10916        let err = cast_with_options(
10917            &array,
10918            &DataType::Decimal128(7, 3),
10919            &CastOptions {
10920                safe: false,
10921                format_options: FormatOptions::default(),
10922            },
10923        );
10924        assert_eq!(
10925            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
10926            err.unwrap_err().to_string()
10927        );
10928    }
10929
10930    #[test]
10931    fn test_cast_numeric_to_decimal256_precision_overflow() {
10932        let array = Int64Array::from(vec![1234567]);
10933        let array = Arc::new(array) as ArrayRef;
10934        let casted_array = cast_with_options(
10935            &array,
10936            &DataType::Decimal256(7, 3),
10937            &CastOptions {
10938                safe: true,
10939                format_options: FormatOptions::default(),
10940            },
10941        );
10942        assert!(casted_array.is_ok());
10943        assert!(casted_array.unwrap().is_null(0));
10944
10945        let err = cast_with_options(
10946            &array,
10947            &DataType::Decimal256(7, 3),
10948            &CastOptions {
10949                safe: false,
10950                format_options: FormatOptions::default(),
10951            },
10952        );
10953        assert_eq!(
10954            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
10955            err.unwrap_err().to_string()
10956        );
10957    }
10958
10959    /// helper function to test casting from duration to interval
10960    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
10961        array: Vec<i64>,
10962        cast_options: &CastOptions,
10963    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10964        let array = PrimitiveArray::<T>::new(array.into(), None);
10965        let array = Arc::new(array) as ArrayRef;
10966        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
10967        let out = cast_with_options(&array, &interval, cast_options)?;
10968        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
10969        Ok(out)
10970    }
10971
10972    #[test]
10973    fn test_cast_from_duration_to_interval() {
10974        // from duration second to interval month day nano
10975        let array = vec![1234567];
10976        let casted_array =
10977            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
10978                .unwrap();
10979        assert_eq!(
10980            casted_array.data_type(),
10981            &DataType::Interval(IntervalUnit::MonthDayNano)
10982        );
10983        assert_eq!(
10984            casted_array.value(0),
10985            IntervalMonthDayNano::new(0, 0, 1234567000000000)
10986        );
10987
10988        let array = vec![i64::MAX];
10989        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10990            array.clone(),
10991            &CastOptions::default(),
10992        )
10993        .unwrap();
10994        assert!(!casted_array.is_valid(0));
10995
10996        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10997            array,
10998            &CastOptions {
10999                safe: false,
11000                format_options: FormatOptions::default(),
11001            },
11002        );
11003        assert!(casted_array.is_err());
11004
11005        // from duration millisecond to interval month day nano
11006        let array = vec![1234567];
11007        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11008            array,
11009            &CastOptions::default(),
11010        )
11011        .unwrap();
11012        assert_eq!(
11013            casted_array.data_type(),
11014            &DataType::Interval(IntervalUnit::MonthDayNano)
11015        );
11016        assert_eq!(
11017            casted_array.value(0),
11018            IntervalMonthDayNano::new(0, 0, 1234567000000)
11019        );
11020
11021        let array = vec![i64::MAX];
11022        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11023            array.clone(),
11024            &CastOptions::default(),
11025        )
11026        .unwrap();
11027        assert!(!casted_array.is_valid(0));
11028
11029        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11030            array,
11031            &CastOptions {
11032                safe: false,
11033                format_options: FormatOptions::default(),
11034            },
11035        );
11036        assert!(casted_array.is_err());
11037
11038        // from duration microsecond to interval month day nano
11039        let array = vec![1234567];
11040        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11041            array,
11042            &CastOptions::default(),
11043        )
11044        .unwrap();
11045        assert_eq!(
11046            casted_array.data_type(),
11047            &DataType::Interval(IntervalUnit::MonthDayNano)
11048        );
11049        assert_eq!(
11050            casted_array.value(0),
11051            IntervalMonthDayNano::new(0, 0, 1234567000)
11052        );
11053
11054        let array = vec![i64::MAX];
11055        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11056            array.clone(),
11057            &CastOptions::default(),
11058        )
11059        .unwrap();
11060        assert!(!casted_array.is_valid(0));
11061
11062        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11063            array,
11064            &CastOptions {
11065                safe: false,
11066                format_options: FormatOptions::default(),
11067            },
11068        );
11069        assert!(casted_array.is_err());
11070
11071        // from duration nanosecond to interval month day nano
11072        let array = vec![1234567];
11073        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11074            array,
11075            &CastOptions::default(),
11076        )
11077        .unwrap();
11078        assert_eq!(
11079            casted_array.data_type(),
11080            &DataType::Interval(IntervalUnit::MonthDayNano)
11081        );
11082        assert_eq!(
11083            casted_array.value(0),
11084            IntervalMonthDayNano::new(0, 0, 1234567)
11085        );
11086
11087        let array = vec![i64::MAX];
11088        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11089            array,
11090            &CastOptions {
11091                safe: false,
11092                format_options: FormatOptions::default(),
11093            },
11094        )
11095        .unwrap();
11096        assert_eq!(
11097            casted_array.value(0),
11098            IntervalMonthDayNano::new(0, 0, i64::MAX)
11099        );
11100    }
11101
11102    /// helper function to test casting from interval to duration
11103    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
11104        array: &IntervalMonthDayNanoArray,
11105        cast_options: &CastOptions,
11106    ) -> Result<PrimitiveArray<T>, ArrowError> {
11107        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
11108        casted_array
11109            .as_any()
11110            .downcast_ref::<PrimitiveArray<T>>()
11111            .ok_or_else(|| {
11112                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
11113            })
11114            .cloned()
11115    }
11116
11117    #[test]
11118    fn test_cast_from_interval_to_duration() {
11119        let nullable = CastOptions::default();
11120        let fallible = CastOptions {
11121            safe: false,
11122            format_options: FormatOptions::default(),
11123        };
11124        let v = IntervalMonthDayNano::new(0, 0, 1234567);
11125
11126        // from interval month day nano to duration second
11127        let array = vec![v].into();
11128        let casted_array: DurationSecondArray =
11129            cast_from_interval_to_duration(&array, &nullable).unwrap();
11130        assert_eq!(casted_array.value(0), 0);
11131
11132        let array = vec![IntervalMonthDayNano::MAX].into();
11133        let casted_array: DurationSecondArray =
11134            cast_from_interval_to_duration(&array, &nullable).unwrap();
11135        assert!(!casted_array.is_valid(0));
11136
11137        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
11138        assert!(res.is_err());
11139
11140        // from interval month day nano to duration millisecond
11141        let array = vec![v].into();
11142        let casted_array: DurationMillisecondArray =
11143            cast_from_interval_to_duration(&array, &nullable).unwrap();
11144        assert_eq!(casted_array.value(0), 1);
11145
11146        let array = vec![IntervalMonthDayNano::MAX].into();
11147        let casted_array: DurationMillisecondArray =
11148            cast_from_interval_to_duration(&array, &nullable).unwrap();
11149        assert!(!casted_array.is_valid(0));
11150
11151        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
11152        assert!(res.is_err());
11153
11154        // from interval month day nano to duration microsecond
11155        let array = vec![v].into();
11156        let casted_array: DurationMicrosecondArray =
11157            cast_from_interval_to_duration(&array, &nullable).unwrap();
11158        assert_eq!(casted_array.value(0), 1234);
11159
11160        let array = vec![IntervalMonthDayNano::MAX].into();
11161        let casted_array =
11162            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
11163        assert!(!casted_array.is_valid(0));
11164
11165        let casted_array =
11166            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
11167        assert!(casted_array.is_err());
11168
11169        // from interval month day nano to duration nanosecond
11170        let array = vec![v].into();
11171        let casted_array: DurationNanosecondArray =
11172            cast_from_interval_to_duration(&array, &nullable).unwrap();
11173        assert_eq!(casted_array.value(0), 1234567);
11174
11175        let array = vec![IntervalMonthDayNano::MAX].into();
11176        let casted_array: DurationNanosecondArray =
11177            cast_from_interval_to_duration(&array, &nullable).unwrap();
11178        assert!(!casted_array.is_valid(0));
11179
11180        let casted_array =
11181            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
11182        assert!(casted_array.is_err());
11183
11184        let array = vec![
11185            IntervalMonthDayNanoType::make_value(0, 1, 0),
11186            IntervalMonthDayNanoType::make_value(-1, 0, 0),
11187            IntervalMonthDayNanoType::make_value(1, 1, 0),
11188            IntervalMonthDayNanoType::make_value(1, 0, 1),
11189            IntervalMonthDayNanoType::make_value(0, 0, -1),
11190        ]
11191        .into();
11192        let casted_array =
11193            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
11194        assert!(!casted_array.is_valid(0));
11195        assert!(!casted_array.is_valid(1));
11196        assert!(!casted_array.is_valid(2));
11197        assert!(!casted_array.is_valid(3));
11198        assert!(casted_array.is_valid(4));
11199        assert_eq!(casted_array.value(4), -1);
11200    }
11201
11202    /// helper function to test casting from interval year month to interval month day nano
11203    fn cast_from_interval_year_month_to_interval_month_day_nano(
11204        array: Vec<i32>,
11205        cast_options: &CastOptions,
11206    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11207        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
11208        let array = Arc::new(array) as ArrayRef;
11209        let casted_array = cast_with_options(
11210            &array,
11211            &DataType::Interval(IntervalUnit::MonthDayNano),
11212            cast_options,
11213        )?;
11214        casted_array
11215            .as_any()
11216            .downcast_ref::<IntervalMonthDayNanoArray>()
11217            .ok_or_else(|| {
11218                ArrowError::ComputeError(
11219                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
11220                )
11221            })
11222            .cloned()
11223    }
11224
11225    #[test]
11226    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
11227        // from interval year month to interval month day nano
11228        let array = vec![1234567];
11229        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
11230            array,
11231            &CastOptions::default(),
11232        )
11233        .unwrap();
11234        assert_eq!(
11235            casted_array.data_type(),
11236            &DataType::Interval(IntervalUnit::MonthDayNano)
11237        );
11238        assert_eq!(
11239            casted_array.value(0),
11240            IntervalMonthDayNano::new(1234567, 0, 0)
11241        );
11242    }
11243
11244    /// helper function to test casting from interval day time to interval month day nano
11245    fn cast_from_interval_day_time_to_interval_month_day_nano(
11246        array: Vec<IntervalDayTime>,
11247        cast_options: &CastOptions,
11248    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11249        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
11250        let array = Arc::new(array) as ArrayRef;
11251        let casted_array = cast_with_options(
11252            &array,
11253            &DataType::Interval(IntervalUnit::MonthDayNano),
11254            cast_options,
11255        )?;
11256        Ok(casted_array
11257            .as_primitive::<IntervalMonthDayNanoType>()
11258            .clone())
11259    }
11260
11261    #[test]
11262    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
11263        // from interval day time to interval month day nano
11264        let array = vec![IntervalDayTime::new(123, 0)];
11265        let casted_array =
11266            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
11267                .unwrap();
11268        assert_eq!(
11269            casted_array.data_type(),
11270            &DataType::Interval(IntervalUnit::MonthDayNano)
11271        );
11272        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
11273    }
11274
11275    #[test]
11276    fn test_cast_below_unixtimestamp() {
11277        let valid = StringArray::from(vec![
11278            "1900-01-03 23:59:59",
11279            "1969-12-31 00:00:01",
11280            "1989-12-31 00:00:01",
11281        ]);
11282
11283        let array = Arc::new(valid) as ArrayRef;
11284        let casted_array = cast_with_options(
11285            &array,
11286            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11287            &CastOptions {
11288                safe: false,
11289                format_options: FormatOptions::default(),
11290            },
11291        )
11292        .unwrap();
11293
11294        let ts_array = casted_array
11295            .as_primitive::<TimestampNanosecondType>()
11296            .values()
11297            .iter()
11298            .map(|ts| ts / 1_000_000)
11299            .collect::<Vec<_>>();
11300
11301        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
11302        let casted_array = cast(&array, &DataType::Date32).unwrap();
11303        let date_array = casted_array.as_primitive::<Date32Type>();
11304        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
11305        let string_array = casted_array.as_string::<i32>();
11306        assert_eq!("1900-01-03", string_array.value(0));
11307        assert_eq!("1969-12-31", string_array.value(1));
11308        assert_eq!("1989-12-31", string_array.value(2));
11309    }
11310
11311    #[test]
11312    fn test_nested_list() {
11313        let mut list = ListBuilder::new(Int32Builder::new());
11314        list.append_value([Some(1), Some(2), Some(3)]);
11315        list.append_value([Some(4), None, Some(6)]);
11316        let list = list.finish();
11317
11318        let to_field = Field::new("nested", list.data_type().clone(), false);
11319        let to = DataType::List(Arc::new(to_field));
11320        let out = cast(&list, &to).unwrap();
11321        let opts = FormatOptions::default().with_null("null");
11322        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
11323
11324        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
11325        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
11326    }
11327
11328    #[test]
11329    fn test_nested_list_cast() {
11330        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
11331        builder.append_value([Some([Some(1), Some(2), None]), None]);
11332        builder.append_value([None, Some([]), None]);
11333        builder.append_null();
11334        builder.append_value([Some([Some(2), Some(3)])]);
11335        let start = builder.finish();
11336
11337        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
11338        builder.append_value([Some([Some(1), Some(2), None]), None]);
11339        builder.append_value([None, Some([]), None]);
11340        builder.append_null();
11341        builder.append_value([Some([Some(2), Some(3)])]);
11342        let expected = builder.finish();
11343
11344        let actual = cast(&start, expected.data_type()).unwrap();
11345        assert_eq!(actual.as_ref(), &expected);
11346    }
11347
11348    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
11349        safe: true,
11350        format_options: FormatOptions::new(),
11351    };
11352
11353    #[test]
11354    #[allow(clippy::assertions_on_constants)]
11355    fn test_const_options() {
11356        assert!(CAST_OPTIONS.safe)
11357    }
11358
11359    #[test]
11360    fn test_list_format_options() {
11361        let options = CastOptions {
11362            safe: false,
11363            format_options: FormatOptions::default().with_null("null"),
11364        };
11365        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
11366            Some(vec![Some(0), Some(1), Some(2)]),
11367            Some(vec![Some(0), None, Some(2)]),
11368        ]);
11369        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
11370        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
11371        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
11372    }
11373    #[test]
11374    fn test_cast_string_to_timestamp_invalid_tz() {
11375        // content after Z should be ignored
11376        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
11377        let array = StringArray::from(vec![Some(bad_timestamp)]);
11378
11379        let data_types = [
11380            DataType::Timestamp(TimeUnit::Second, None),
11381            DataType::Timestamp(TimeUnit::Millisecond, None),
11382            DataType::Timestamp(TimeUnit::Microsecond, None),
11383            DataType::Timestamp(TimeUnit::Nanosecond, None),
11384        ];
11385
11386        let cast_options = CastOptions {
11387            safe: false,
11388            ..Default::default()
11389        };
11390
11391        for dt in data_types {
11392            assert_eq!(
11393                cast_with_options(&array, &dt, &cast_options)
11394                    .unwrap_err()
11395                    .to_string(),
11396                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
11397            );
11398        }
11399    }
11400    #[test]
11401    fn test_cast_struct_to_struct() {
11402        let struct_type = DataType::Struct(
11403            vec![
11404                Field::new("a", DataType::Boolean, false),
11405                Field::new("b", DataType::Int32, false),
11406            ]
11407            .into(),
11408        );
11409        let to_type = DataType::Struct(
11410            vec![
11411                Field::new("a", DataType::Utf8, false),
11412                Field::new("b", DataType::Utf8, false),
11413            ]
11414            .into(),
11415        );
11416        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11417        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11418        let struct_array = StructArray::from(vec![
11419            (
11420                Arc::new(Field::new("b", DataType::Boolean, false)),
11421                boolean.clone() as ArrayRef,
11422            ),
11423            (
11424                Arc::new(Field::new("c", DataType::Int32, false)),
11425                int.clone() as ArrayRef,
11426            ),
11427        ]);
11428        let casted_array = cast(&struct_array, &to_type).unwrap();
11429        let casted_array = casted_array.as_struct();
11430        assert_eq!(casted_array.data_type(), &to_type);
11431        let casted_boolean_array = casted_array
11432            .column(0)
11433            .as_string::<i32>()
11434            .into_iter()
11435            .flatten()
11436            .collect::<Vec<_>>();
11437        let casted_int_array = casted_array
11438            .column(1)
11439            .as_string::<i32>()
11440            .into_iter()
11441            .flatten()
11442            .collect::<Vec<_>>();
11443        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
11444        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
11445
11446        // test for can't cast
11447        let to_type = DataType::Struct(
11448            vec![
11449                Field::new("a", DataType::Date32, false),
11450                Field::new("b", DataType::Utf8, false),
11451            ]
11452            .into(),
11453        );
11454        assert!(!can_cast_types(&struct_type, &to_type));
11455        let result = cast(&struct_array, &to_type);
11456        assert_eq!(
11457            "Cast error: Casting from Boolean to Date32 not supported",
11458            result.unwrap_err().to_string()
11459        );
11460    }
11461
11462    #[test]
11463    fn test_cast_struct_to_struct_nullability() {
11464        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11465        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
11466        let struct_array = StructArray::from(vec![
11467            (
11468                Arc::new(Field::new("b", DataType::Boolean, false)),
11469                boolean.clone() as ArrayRef,
11470            ),
11471            (
11472                Arc::new(Field::new("c", DataType::Int32, true)),
11473                int.clone() as ArrayRef,
11474            ),
11475        ]);
11476
11477        // okay: nullable to nullable
11478        let to_type = DataType::Struct(
11479            vec![
11480                Field::new("a", DataType::Utf8, false),
11481                Field::new("b", DataType::Utf8, true),
11482            ]
11483            .into(),
11484        );
11485        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
11486
11487        // error: nullable to non-nullable
11488        let to_type = DataType::Struct(
11489            vec![
11490                Field::new("a", DataType::Utf8, false),
11491                Field::new("b", DataType::Utf8, false),
11492            ]
11493            .into(),
11494        );
11495        cast(&struct_array, &to_type)
11496            .expect_err("Cast nullable to non-nullable struct field should fail");
11497
11498        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11499        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
11500        let struct_array = StructArray::from(vec![
11501            (
11502                Arc::new(Field::new("b", DataType::Boolean, false)),
11503                boolean.clone() as ArrayRef,
11504            ),
11505            (
11506                Arc::new(Field::new("c", DataType::Int32, false)),
11507                int.clone() as ArrayRef,
11508            ),
11509        ]);
11510
11511        // okay: non-nullable to non-nullable
11512        let to_type = DataType::Struct(
11513            vec![
11514                Field::new("a", DataType::Utf8, false),
11515                Field::new("b", DataType::Utf8, false),
11516            ]
11517            .into(),
11518        );
11519        cast(&struct_array, &to_type)
11520            .expect("Cast non-nullable to non-nullable struct field should work");
11521
11522        // err: non-nullable to non-nullable but overflowing return null during casting
11523        let to_type = DataType::Struct(
11524            vec![
11525                Field::new("a", DataType::Utf8, false),
11526                Field::new("b", DataType::Int8, false),
11527            ]
11528            .into(),
11529        );
11530        cast(&struct_array, &to_type).expect_err(
11531            "Cast non-nullable to non-nullable struct field returning null should fail",
11532        );
11533    }
11534
11535    #[test]
11536    fn test_cast_struct_to_non_struct() {
11537        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
11538        let struct_array = StructArray::from(vec![(
11539            Arc::new(Field::new("a", DataType::Boolean, false)),
11540            boolean.clone() as ArrayRef,
11541        )]);
11542        let to_type = DataType::Utf8;
11543        let result = cast(&struct_array, &to_type);
11544        assert_eq!(
11545            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
11546            result.unwrap_err().to_string()
11547        );
11548    }
11549
11550    #[test]
11551    fn test_cast_non_struct_to_struct() {
11552        let array = StringArray::from(vec!["a", "b"]);
11553        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
11554        let result = cast(&array, &to_type);
11555        assert_eq!(
11556            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
11557            result.unwrap_err().to_string()
11558        );
11559    }
11560
11561    #[test]
11562    fn test_cast_struct_with_different_field_order() {
11563        // Test slow path: fields are in different order
11564        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11565        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11566        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11567
11568        let struct_array = StructArray::from(vec![
11569            (
11570                Arc::new(Field::new("a", DataType::Boolean, false)),
11571                boolean.clone() as ArrayRef,
11572            ),
11573            (
11574                Arc::new(Field::new("b", DataType::Int32, false)),
11575                int.clone() as ArrayRef,
11576            ),
11577            (
11578                Arc::new(Field::new("c", DataType::Utf8, false)),
11579                string.clone() as ArrayRef,
11580            ),
11581        ]);
11582
11583        // Target has fields in different order: c, a, b instead of a, b, c
11584        let to_type = DataType::Struct(
11585            vec![
11586                Field::new("c", DataType::Utf8, false),
11587                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
11588                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
11589            ]
11590            .into(),
11591        );
11592
11593        let result = cast(&struct_array, &to_type).unwrap();
11594        let result_struct = result.as_struct();
11595
11596        assert_eq!(result_struct.data_type(), &to_type);
11597        assert_eq!(result_struct.num_columns(), 3);
11598
11599        // Verify field "c" (originally position 2, now position 0) remains Utf8
11600        let c_column = result_struct.column(0).as_string::<i32>();
11601        assert_eq!(
11602            c_column.into_iter().flatten().collect::<Vec<_>>(),
11603            vec!["foo", "bar", "baz", "qux"]
11604        );
11605
11606        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
11607        let a_column = result_struct.column(1).as_string::<i32>();
11608        assert_eq!(
11609            a_column.into_iter().flatten().collect::<Vec<_>>(),
11610            vec!["false", "false", "true", "true"]
11611        );
11612
11613        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
11614        let b_column = result_struct.column(2).as_string::<i32>();
11615        assert_eq!(
11616            b_column.into_iter().flatten().collect::<Vec<_>>(),
11617            vec!["42", "28", "19", "31"]
11618        );
11619    }
11620
11621    #[test]
11622    fn test_cast_struct_with_missing_field() {
11623        // Test that casting fails when target has a field not present in source
11624        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
11625        let struct_array = StructArray::from(vec![(
11626            Arc::new(Field::new("a", DataType::Boolean, false)),
11627            boolean.clone() as ArrayRef,
11628        )]);
11629
11630        let to_type = DataType::Struct(
11631            vec![
11632                Field::new("a", DataType::Utf8, false),
11633                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
11634            ]
11635            .into(),
11636        );
11637
11638        let result = cast(&struct_array, &to_type);
11639        assert!(result.is_err());
11640        assert_eq!(
11641            result.unwrap_err().to_string(),
11642            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
11643        );
11644    }
11645
11646    #[test]
11647    fn test_cast_struct_with_subset_of_fields() {
11648        // Test casting to a struct with fewer fields (selecting a subset)
11649        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11650        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11651        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11652
11653        let struct_array = StructArray::from(vec![
11654            (
11655                Arc::new(Field::new("a", DataType::Boolean, false)),
11656                boolean.clone() as ArrayRef,
11657            ),
11658            (
11659                Arc::new(Field::new("b", DataType::Int32, false)),
11660                int.clone() as ArrayRef,
11661            ),
11662            (
11663                Arc::new(Field::new("c", DataType::Utf8, false)),
11664                string.clone() as ArrayRef,
11665            ),
11666        ]);
11667
11668        // Target has only fields "c" and "a", omitting "b"
11669        let to_type = DataType::Struct(
11670            vec![
11671                Field::new("c", DataType::Utf8, false),
11672                Field::new("a", DataType::Utf8, false),
11673            ]
11674            .into(),
11675        );
11676
11677        let result = cast(&struct_array, &to_type).unwrap();
11678        let result_struct = result.as_struct();
11679
11680        assert_eq!(result_struct.data_type(), &to_type);
11681        assert_eq!(result_struct.num_columns(), 2);
11682
11683        // Verify field "c" remains Utf8
11684        let c_column = result_struct.column(0).as_string::<i32>();
11685        assert_eq!(
11686            c_column.into_iter().flatten().collect::<Vec<_>>(),
11687            vec!["foo", "bar", "baz", "qux"]
11688        );
11689
11690        // Verify field "a" was cast from Boolean to Utf8
11691        let a_column = result_struct.column(1).as_string::<i32>();
11692        assert_eq!(
11693            a_column.into_iter().flatten().collect::<Vec<_>>(),
11694            vec!["false", "false", "true", "true"]
11695        );
11696    }
11697
11698    #[test]
11699    fn test_can_cast_struct_rename_field() {
11700        // Test that can_cast_types returns false when target has a field not in source
11701        let from_type = DataType::Struct(
11702            vec![
11703                Field::new("a", DataType::Int32, false),
11704                Field::new("b", DataType::Utf8, false),
11705            ]
11706            .into(),
11707        );
11708
11709        let to_type = DataType::Struct(
11710            vec![
11711                Field::new("a", DataType::Int64, false),
11712                Field::new("c", DataType::Boolean, false), // Field "c" not in source
11713            ]
11714            .into(),
11715        );
11716
11717        assert!(can_cast_types(&from_type, &to_type));
11718    }
11719
11720    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
11721        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
11722        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
11723        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
11724        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
11725    }
11726
11727    #[test]
11728    fn test_decimal_to_decimal_coverage() {
11729        let test_cases = [
11730            // increase precision, increase scale, infallible
11731            DecimalCastTestConfig {
11732                input_prec: 5,
11733                input_scale: 1,
11734                input_repr: 99999, // 9999.9
11735                output_prec: 10,
11736                output_scale: 6,
11737                expected_output_repr: Ok(9999900000), // 9999.900000
11738            },
11739            // increase precision, increase scale, fallible, safe
11740            DecimalCastTestConfig {
11741                input_prec: 5,
11742                input_scale: 1,
11743                input_repr: 99, // 9999.9
11744                output_prec: 7,
11745                output_scale: 6,
11746                expected_output_repr: Ok(9900000), // 9.900000
11747            },
11748            // increase precision, increase scale, fallible, unsafe
11749            DecimalCastTestConfig {
11750                input_prec: 5,
11751                input_scale: 1,
11752                input_repr: 99999, // 9999.9
11753                output_prec: 7,
11754                output_scale: 6,
11755                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
11756            },
11757            // increase precision, decrease scale, always infallible
11758            DecimalCastTestConfig {
11759                input_prec: 5,
11760                input_scale: 3,
11761                input_repr: 99999, // 99.999
11762                output_prec: 10,
11763                output_scale: 2,
11764                expected_output_repr: Ok(10000), // 100.00
11765            },
11766            // increase precision, decrease scale, no rouding
11767            DecimalCastTestConfig {
11768                input_prec: 5,
11769                input_scale: 3,
11770                input_repr: 99994, // 99.994
11771                output_prec: 10,
11772                output_scale: 2,
11773                expected_output_repr: Ok(9999), // 99.99
11774            },
11775            // increase precision, don't change scale, always infallible
11776            DecimalCastTestConfig {
11777                input_prec: 5,
11778                input_scale: 3,
11779                input_repr: 99999, // 99.999
11780                output_prec: 10,
11781                output_scale: 3,
11782                expected_output_repr: Ok(99999), // 99.999
11783            },
11784            // decrease precision, increase scale, safe
11785            DecimalCastTestConfig {
11786                input_prec: 10,
11787                input_scale: 5,
11788                input_repr: 999999, // 9.99999
11789                output_prec: 8,
11790                output_scale: 7,
11791                expected_output_repr: Ok(99999900), // 9.9999900
11792            },
11793            // decrease precision, increase scale, unsafe
11794            DecimalCastTestConfig {
11795                input_prec: 10,
11796                input_scale: 5,
11797                input_repr: 9999999, // 99.99999
11798                output_prec: 8,
11799                output_scale: 7,
11800                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
11801            },
11802            // decrease precision, decrease scale, safe, infallible
11803            DecimalCastTestConfig {
11804                input_prec: 7,
11805                input_scale: 4,
11806                input_repr: 9999999, // 999.9999
11807                output_prec: 6,
11808                output_scale: 2,
11809                expected_output_repr: Ok(100000),
11810            },
11811            // decrease precision, decrease scale, safe, fallible
11812            DecimalCastTestConfig {
11813                input_prec: 10,
11814                input_scale: 5,
11815                input_repr: 12345678, // 123.45678
11816                output_prec: 8,
11817                output_scale: 3,
11818                expected_output_repr: Ok(123457), // 123.457
11819            },
11820            // decrease precision, decrease scale, unsafe
11821            DecimalCastTestConfig {
11822                input_prec: 10,
11823                input_scale: 5,
11824                input_repr: 9999999, // 99.99999
11825                output_prec: 4,
11826                output_scale: 3,
11827                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
11828            },
11829            // decrease precision, same scale, safe
11830            DecimalCastTestConfig {
11831                input_prec: 10,
11832                input_scale: 5,
11833                input_repr: 999999, // 9.99999
11834                output_prec: 6,
11835                output_scale: 5,
11836                expected_output_repr: Ok(999999), // 9.99999
11837            },
11838            // decrease precision, same scale, unsafe
11839            DecimalCastTestConfig {
11840                input_prec: 10,
11841                input_scale: 5,
11842                input_repr: 9999999, // 99.99999
11843                output_prec: 6,
11844                output_scale: 5,
11845                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
11846            },
11847            // same precision, increase scale, safe
11848            DecimalCastTestConfig {
11849                input_prec: 7,
11850                input_scale: 4,
11851                input_repr: 12345, // 1.2345
11852                output_prec: 7,
11853                output_scale: 6,
11854                expected_output_repr: Ok(1234500), // 1.234500
11855            },
11856            // same precision, increase scale, unsafe
11857            DecimalCastTestConfig {
11858                input_prec: 7,
11859                input_scale: 4,
11860                input_repr: 123456, // 12.3456
11861                output_prec: 7,
11862                output_scale: 6,
11863                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
11864            },
11865            // same precision, decrease scale, infallible
11866            DecimalCastTestConfig {
11867                input_prec: 7,
11868                input_scale: 5,
11869                input_repr: 1234567, // 12.34567
11870                output_prec: 7,
11871                output_scale: 4,
11872                expected_output_repr: Ok(123457), // 12.3457
11873            },
11874            // same precision, same scale, infallible
11875            DecimalCastTestConfig {
11876                input_prec: 7,
11877                input_scale: 5,
11878                input_repr: 9999999, // 99.99999
11879                output_prec: 7,
11880                output_scale: 5,
11881                expected_output_repr: Ok(9999999), // 99.99999
11882            },
11883            // precision increase, input scale & output scale = 0, infallible
11884            DecimalCastTestConfig {
11885                input_prec: 7,
11886                input_scale: 0,
11887                input_repr: 1234567, // 1234567
11888                output_prec: 8,
11889                output_scale: 0,
11890                expected_output_repr: Ok(1234567), // 1234567
11891            },
11892            // precision decrease, input scale & output scale = 0, failure
11893            DecimalCastTestConfig {
11894                input_prec: 7,
11895                input_scale: 0,
11896                input_repr: 1234567, // 1234567
11897                output_prec: 6,
11898                output_scale: 0,
11899                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
11900            },
11901            // precision decrease, input scale & output scale = 0, success
11902            DecimalCastTestConfig {
11903                input_prec: 7,
11904                input_scale: 0,
11905                input_repr: 123456, // 123456
11906                output_prec: 6,
11907                output_scale: 0,
11908                expected_output_repr: Ok(123456), // 123456
11909            },
11910        ];
11911
11912        for t in test_cases {
11913            run_decimal_cast_test_case_between_multiple_types(t);
11914        }
11915    }
11916
11917    #[test]
11918    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
11919        let test_cases = [
11920            DecimalCastTestConfig {
11921                input_prec: 5,
11922                input_scale: 0,
11923                input_repr: 99999,
11924                output_prec: 10,
11925                output_scale: 5,
11926                expected_output_repr: Ok(9999900000),
11927            },
11928            DecimalCastTestConfig {
11929                input_prec: 5,
11930                input_scale: 0,
11931                input_repr: -99999,
11932                output_prec: 10,
11933                output_scale: 5,
11934                expected_output_repr: Ok(-9999900000),
11935            },
11936            DecimalCastTestConfig {
11937                input_prec: 5,
11938                input_scale: 2,
11939                input_repr: 99999,
11940                output_prec: 10,
11941                output_scale: 5,
11942                expected_output_repr: Ok(99999000),
11943            },
11944            DecimalCastTestConfig {
11945                input_prec: 5,
11946                input_scale: -2,
11947                input_repr: -99999,
11948                output_prec: 10,
11949                output_scale: 3,
11950                expected_output_repr: Ok(-9999900000),
11951            },
11952            DecimalCastTestConfig {
11953                input_prec: 5,
11954                input_scale: 3,
11955                input_repr: -12345,
11956                output_prec: 6,
11957                output_scale: 5,
11958                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())
11959            },
11960        ];
11961
11962        for t in test_cases {
11963            run_decimal_cast_test_case_between_multiple_types(t);
11964        }
11965    }
11966
11967    #[test]
11968    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
11969        let test_cases = [
11970            DecimalCastTestConfig {
11971                input_prec: 5,
11972                input_scale: 0,
11973                input_repr: 99999,
11974                output_scale: -3,
11975                output_prec: 3,
11976                expected_output_repr: Ok(100),
11977            },
11978            DecimalCastTestConfig {
11979                input_prec: 5,
11980                input_scale: 0,
11981                input_repr: -99999,
11982                output_prec: 1,
11983                output_scale: -5,
11984                expected_output_repr: Ok(-1),
11985            },
11986            DecimalCastTestConfig {
11987                input_prec: 10,
11988                input_scale: 2,
11989                input_repr: 123456789,
11990                output_prec: 5,
11991                output_scale: -2,
11992                expected_output_repr: Ok(12346),
11993            },
11994            DecimalCastTestConfig {
11995                input_prec: 10,
11996                input_scale: 4,
11997                input_repr: -9876543210,
11998                output_prec: 7,
11999                output_scale: 0,
12000                expected_output_repr: Ok(-987654),
12001            },
12002            DecimalCastTestConfig {
12003                input_prec: 7,
12004                input_scale: 4,
12005                input_repr: 9999999,
12006                output_prec: 6,
12007                output_scale: 3,
12008                expected_output_repr:
12009                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12010            },
12011        ];
12012        for t in test_cases {
12013            run_decimal_cast_test_case_between_multiple_types(t);
12014        }
12015    }
12016
12017    #[test]
12018    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12019        let array = vec![Some(123456789)];
12020        let array = create_decimal128_array(array, 24, 2).unwrap();
12021        let input_type = DataType::Decimal128(24, 2);
12022        let output_type = DataType::Decimal128(6, 2);
12023        assert!(can_cast_types(&input_type, &output_type));
12024
12025        let options = CastOptions {
12026            safe: false,
12027            ..Default::default()
12028        };
12029        let result = cast_with_options(&array, &output_type, &options);
12030        assert_eq!(
12031            result.unwrap_err().to_string(),
12032            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12033        );
12034    }
12035
12036    #[test]
12037    fn test_decimal_to_decimal_same_scale() {
12038        let array = vec![Some(520)];
12039        let array = create_decimal128_array(array, 4, 2).unwrap();
12040        let input_type = DataType::Decimal128(4, 2);
12041        let output_type = DataType::Decimal128(3, 2);
12042        assert!(can_cast_types(&input_type, &output_type));
12043
12044        let options = CastOptions {
12045            safe: false,
12046            ..Default::default()
12047        };
12048        let result = cast_with_options(&array, &output_type, &options);
12049        assert_eq!(
12050            result.unwrap().as_primitive::<Decimal128Type>().value(0),
12051            520
12052        );
12053
12054        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
12055        assert_eq!(
12056            &cast(
12057                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12058                &DataType::Decimal128(2, 0)
12059            )
12060            .unwrap(),
12061            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
12062        );
12063    }
12064
12065    #[test]
12066    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
12067        let array = vec![Some(123456789)];
12068        let array = create_decimal128_array(array, 24, 4).unwrap();
12069        let input_type = DataType::Decimal128(24, 4);
12070        let output_type = DataType::Decimal128(6, 2);
12071        assert!(can_cast_types(&input_type, &output_type));
12072
12073        let options = CastOptions {
12074            safe: false,
12075            ..Default::default()
12076        };
12077        let result = cast_with_options(&array, &output_type, &options);
12078        assert_eq!(
12079            result.unwrap_err().to_string(),
12080            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12081        );
12082    }
12083
12084    #[test]
12085    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
12086        let array = vec![Some(123456789)];
12087        let array = create_decimal128_array(array, 24, 2).unwrap();
12088        let input_type = DataType::Decimal128(24, 2);
12089        let output_type = DataType::Decimal128(6, 3);
12090        assert!(can_cast_types(&input_type, &output_type));
12091
12092        let options = CastOptions {
12093            safe: false,
12094            ..Default::default()
12095        };
12096        let result = cast_with_options(&array, &output_type, &options);
12097        assert_eq!(
12098            result.unwrap_err().to_string(),
12099            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
12100        );
12101    }
12102
12103    #[test]
12104    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
12105        let array = vec![Some(123456789)];
12106        let array = create_decimal128_array(array, 24, 2).unwrap();
12107        let input_type = DataType::Decimal128(24, 2);
12108        let output_type = DataType::Decimal256(6, 2);
12109        assert!(can_cast_types(&input_type, &output_type));
12110
12111        let options = CastOptions {
12112            safe: false,
12113            ..Default::default()
12114        };
12115        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
12116        assert_eq!(
12117            result.to_string(),
12118            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
12119        );
12120    }
12121
12122    #[test]
12123    fn test_first_none() {
12124        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12125            None,
12126            Some(vec![Some(1), Some(2)]),
12127        ])) as ArrayRef;
12128        let data_type =
12129            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12130        let opt = CastOptions::default();
12131        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12132
12133        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12134            vec![None, Some(vec![Some(1), Some(2)])],
12135            2,
12136        )) as ArrayRef;
12137        assert_eq!(*fixed_array, *r);
12138    }
12139
12140    #[test]
12141    fn test_first_last_none() {
12142        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12143            None,
12144            Some(vec![Some(1), Some(2)]),
12145            None,
12146        ])) as ArrayRef;
12147        let data_type =
12148            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12149        let opt = CastOptions::default();
12150        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12151
12152        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12153            vec![None, Some(vec![Some(1), Some(2)]), None],
12154            2,
12155        )) as ArrayRef;
12156        assert_eq!(*fixed_array, *r);
12157    }
12158
12159    #[test]
12160    fn test_cast_decimal_error_output() {
12161        let array = Int64Array::from(vec![1]);
12162        let error = cast_with_options(
12163            &array,
12164            &DataType::Decimal32(1, 1),
12165            &CastOptions {
12166                safe: false,
12167                format_options: FormatOptions::default(),
12168            },
12169        )
12170        .unwrap_err();
12171        assert_eq!(
12172            error.to_string(),
12173            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
12174        );
12175
12176        let array = Int64Array::from(vec![-1]);
12177        let error = cast_with_options(
12178            &array,
12179            &DataType::Decimal32(1, 1),
12180            &CastOptions {
12181                safe: false,
12182                format_options: FormatOptions::default(),
12183            },
12184        )
12185        .unwrap_err();
12186        assert_eq!(
12187            error.to_string(),
12188            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
12189        );
12190    }
12191
12192    #[test]
12193    fn test_run_end_encoded_to_primitive() {
12194        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
12195        let run_ends = Int32Array::from(vec![2, 5, 6]);
12196        let values = Int32Array::from(vec![1, 2, 3]);
12197        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12198        let array_ref = Arc::new(run_array) as ArrayRef;
12199        // Cast to Int64
12200        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12201        // Verify the result is a RunArray with Int64 values
12202        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
12203        assert_eq!(
12204            result_run_array.values(),
12205            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
12206        );
12207    }
12208
12209    #[test]
12210    fn test_sliced_run_end_encoded_to_primitive() {
12211        let run_ends = Int32Array::from(vec![2, 5, 6]);
12212        let values = Int32Array::from(vec![1, 2, 3]);
12213        // [1, 1, 2, 2, 2, 3]
12214        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12215        let run_array = run_array.slice(3, 3); // [2, 2, 3]
12216        let array_ref = Arc::new(run_array) as ArrayRef;
12217
12218        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12219        let result_run_array = cast_result.as_primitive::<Int64Type>();
12220        assert_eq!(result_run_array.values(), &[2, 2, 3]);
12221    }
12222
12223    #[test]
12224    fn test_run_end_encoded_to_string() {
12225        let run_ends = Int32Array::from(vec![2, 3, 5]);
12226        let values = Int32Array::from(vec![10, 20, 30]);
12227        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12228        let array_ref = Arc::new(run_array) as ArrayRef;
12229
12230        // Cast to String
12231        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12232
12233        // Verify the result is a RunArray with String values
12234        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12235        // Check that values are correct
12236        assert_eq!(result_array.value(0), "10");
12237        assert_eq!(result_array.value(1), "10");
12238        assert_eq!(result_array.value(2), "20");
12239    }
12240
12241    #[test]
12242    fn test_primitive_to_run_end_encoded() {
12243        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
12244        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
12245        let array_ref = Arc::new(source_array) as ArrayRef;
12246
12247        // Cast to RunEndEncoded<Int32, Int32>
12248        let target_type = DataType::RunEndEncoded(
12249            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12250            Arc::new(Field::new("values", DataType::Int32, true)),
12251        );
12252        let cast_result = cast(&array_ref, &target_type).unwrap();
12253
12254        // Verify the result is a RunArray
12255        let result_run_array = cast_result
12256            .as_any()
12257            .downcast_ref::<RunArray<Int32Type>>()
12258            .unwrap();
12259
12260        // Check run structure: runs should end at positions [2, 5, 6]
12261        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
12262
12263        // Check values: should be [1, 2, 3]
12264        let values_array = result_run_array.values().as_primitive::<Int32Type>();
12265        assert_eq!(values_array.values(), &[1, 2, 3]);
12266    }
12267
12268    #[test]
12269    fn test_primitive_to_run_end_encoded_with_nulls() {
12270        let source_array = Int32Array::from(vec![
12271            Some(1),
12272            Some(1),
12273            None,
12274            None,
12275            Some(2),
12276            Some(2),
12277            Some(3),
12278            Some(3),
12279            None,
12280            None,
12281            Some(4),
12282            Some(4),
12283            Some(5),
12284            Some(5),
12285            None,
12286            None,
12287        ]);
12288        let array_ref = Arc::new(source_array) as ArrayRef;
12289        let target_type = DataType::RunEndEncoded(
12290            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12291            Arc::new(Field::new("values", DataType::Int32, true)),
12292        );
12293        let cast_result = cast(&array_ref, &target_type).unwrap();
12294        let result_run_array = cast_result
12295            .as_any()
12296            .downcast_ref::<RunArray<Int32Type>>()
12297            .unwrap();
12298        assert_eq!(
12299            result_run_array.run_ends().values(),
12300            &[2, 4, 6, 8, 10, 12, 14, 16]
12301        );
12302        assert_eq!(
12303            result_run_array
12304                .values()
12305                .as_primitive::<Int32Type>()
12306                .values(),
12307            &[1, 0, 2, 3, 0, 4, 5, 0]
12308        );
12309        assert_eq!(result_run_array.values().null_count(), 3);
12310    }
12311
12312    #[test]
12313    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
12314        let source_array = Int64Array::from(vec![
12315            Some(1),
12316            Some(1),
12317            None,
12318            None,
12319            None,
12320            None,
12321            None,
12322            None,
12323            None,
12324            None,
12325            Some(4),
12326            Some(20),
12327            Some(500),
12328            Some(500),
12329            None,
12330            None,
12331        ]);
12332        let array_ref = Arc::new(source_array) as ArrayRef;
12333        let target_type = DataType::RunEndEncoded(
12334            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12335            Arc::new(Field::new("values", DataType::Int64, true)),
12336        );
12337        let cast_result = cast(&array_ref, &target_type).unwrap();
12338        let result_run_array = cast_result
12339            .as_any()
12340            .downcast_ref::<RunArray<Int16Type>>()
12341            .unwrap();
12342        assert_eq!(
12343            result_run_array.run_ends().values(),
12344            &[2, 10, 11, 12, 14, 16]
12345        );
12346        assert_eq!(
12347            result_run_array
12348                .values()
12349                .as_primitive::<Int64Type>()
12350                .values(),
12351            &[1, 0, 4, 20, 500, 0]
12352        );
12353        assert_eq!(result_run_array.values().null_count(), 2);
12354    }
12355
12356    #[test]
12357    fn test_string_to_run_end_encoded() {
12358        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
12359        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
12360        let array_ref = Arc::new(source_array) as ArrayRef;
12361
12362        // Cast to RunEndEncoded<Int32, String>
12363        let target_type = DataType::RunEndEncoded(
12364            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12365            Arc::new(Field::new("values", DataType::Utf8, true)),
12366        );
12367        let cast_result = cast(&array_ref, &target_type).unwrap();
12368
12369        // Verify the result is a RunArray
12370        let result_run_array = cast_result
12371            .as_any()
12372            .downcast_ref::<RunArray<Int32Type>>()
12373            .unwrap();
12374
12375        // Check run structure: runs should end at positions [2, 3, 5]
12376        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
12377
12378        // Check values: should be ["a", "b", "c"]
12379        let values_array = result_run_array.values().as_string::<i32>();
12380        assert_eq!(values_array.value(0), "a");
12381        assert_eq!(values_array.value(1), "b");
12382        assert_eq!(values_array.value(2), "c");
12383    }
12384
12385    #[test]
12386    fn test_empty_array_to_run_end_encoded() {
12387        // Create an empty Int32 array
12388        let source_array = Int32Array::from(Vec::<i32>::new());
12389        let array_ref = Arc::new(source_array) as ArrayRef;
12390
12391        // Cast to RunEndEncoded<Int32, Int32>
12392        let target_type = DataType::RunEndEncoded(
12393            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12394            Arc::new(Field::new("values", DataType::Int32, true)),
12395        );
12396        let cast_result = cast(&array_ref, &target_type).unwrap();
12397
12398        // Verify the result is an empty RunArray
12399        let result_run_array = cast_result
12400            .as_any()
12401            .downcast_ref::<RunArray<Int32Type>>()
12402            .unwrap();
12403
12404        // Check that both run_ends and values are empty
12405        assert_eq!(result_run_array.run_ends().len(), 0);
12406        assert_eq!(result_run_array.values().len(), 0);
12407    }
12408
12409    #[test]
12410    fn test_run_end_encoded_with_nulls() {
12411        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
12412        let run_ends = Int32Array::from(vec![2, 3, 5]);
12413        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
12414        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12415        let array_ref = Arc::new(run_array) as ArrayRef;
12416
12417        // Cast to String
12418        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12419
12420        // Verify the result preserves nulls
12421        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12422        assert_eq!(result_run_array.value(0), "1");
12423        assert!(result_run_array.is_null(2));
12424        assert_eq!(result_run_array.value(4), "2");
12425    }
12426
12427    #[test]
12428    fn test_different_index_types() {
12429        // Test with Int16 index type
12430        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
12431        let array_ref = Arc::new(source_array) as ArrayRef;
12432
12433        let target_type = DataType::RunEndEncoded(
12434            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12435            Arc::new(Field::new("values", DataType::Int32, true)),
12436        );
12437        let cast_result = cast(&array_ref, &target_type).unwrap();
12438        assert_eq!(cast_result.data_type(), &target_type);
12439
12440        // Verify the cast worked correctly: values are [1, 2, 3]
12441        // and run-ends are [2, 3, 5]
12442        let run_array = cast_result
12443            .as_any()
12444            .downcast_ref::<RunArray<Int16Type>>()
12445            .unwrap();
12446        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12447        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12448        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12449        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
12450
12451        // Test again with Int64 index type
12452        let target_type = DataType::RunEndEncoded(
12453            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12454            Arc::new(Field::new("values", DataType::Int32, true)),
12455        );
12456        let cast_result = cast(&array_ref, &target_type).unwrap();
12457        assert_eq!(cast_result.data_type(), &target_type);
12458
12459        // Verify the cast worked correctly: values are [1, 2, 3]
12460        // and run-ends are [2, 3, 5]
12461        let run_array = cast_result
12462            .as_any()
12463            .downcast_ref::<RunArray<Int64Type>>()
12464            .unwrap();
12465        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12466        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12467        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12468        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
12469    }
12470
12471    #[test]
12472    fn test_unsupported_cast_to_run_end_encoded() {
12473        // Create a Struct array - complex nested type that might not be supported
12474        let field = Field::new("item", DataType::Int32, false);
12475        let struct_array = StructArray::from(vec![(
12476            Arc::new(field),
12477            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
12478        )]);
12479        let array_ref = Arc::new(struct_array) as ArrayRef;
12480
12481        // This should fail because:
12482        // 1. The target type is not RunEndEncoded
12483        // 2. The target type is not supported for casting from StructArray
12484        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
12485
12486        // Expect this to fail
12487        assert!(cast_result.is_err());
12488    }
12489
12490    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
12491    #[test]
12492    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
12493        // Construct a valid REE array with Int64 run-ends
12494        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12495        let values = StringArray::from(vec!["a", "b", "c"]);
12496
12497        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12498        let array_ref = Arc::new(ree_array) as ArrayRef;
12499
12500        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12501        let target_type = DataType::RunEndEncoded(
12502            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12503            Arc::new(Field::new("values", DataType::Utf8, true)),
12504        );
12505        let cast_options = CastOptions {
12506            safe: false, // This should make it fail instead of returning nulls
12507            format_options: FormatOptions::default(),
12508        };
12509
12510        // This should fail due to run-end overflow
12511        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12512            cast_with_options(&array_ref, &target_type, &cast_options);
12513
12514        let e = result.expect_err("Cast should have failed but succeeded");
12515        assert!(
12516            e.to_string()
12517                .contains("Cast error: Can't cast value 100000 to type Int16")
12518        );
12519    }
12520
12521    #[test]
12522    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
12523        // Construct a valid REE array with Int64 run-ends
12524        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12525        let values = StringArray::from(vec!["a", "b", "c"]);
12526
12527        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12528        let array_ref = Arc::new(ree_array) as ArrayRef;
12529
12530        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12531        let target_type = DataType::RunEndEncoded(
12532            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12533            Arc::new(Field::new("values", DataType::Utf8, true)),
12534        );
12535        let cast_options = CastOptions {
12536            safe: true,
12537            format_options: FormatOptions::default(),
12538        };
12539
12540        // This fails even though safe is true because the run_ends array has null values
12541        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12542            cast_with_options(&array_ref, &target_type, &cast_options);
12543        let e = result.expect_err("Cast should have failed but succeeded");
12544        assert!(
12545            e.to_string()
12546                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
12547        );
12548    }
12549
12550    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
12551    #[test]
12552    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
12553        // Construct a valid REE array with Int16 run-ends
12554        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
12555        let values = StringArray::from(vec!["a", "b", "c"]);
12556
12557        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12558        let array_ref = Arc::new(ree_array) as ArrayRef;
12559
12560        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
12561        let target_type = DataType::RunEndEncoded(
12562            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12563            Arc::new(Field::new("values", DataType::Utf8, true)),
12564        );
12565        let cast_options = CastOptions {
12566            safe: false,
12567            format_options: FormatOptions::default(),
12568        };
12569
12570        // This should succeed due to valid upcast
12571        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12572            cast_with_options(&array_ref, &target_type, &cast_options);
12573
12574        let array_ref = result.expect("Cast should have succeeded but failed");
12575        // Downcast to RunArray<Int64Type>
12576        let run_array = array_ref
12577            .as_any()
12578            .downcast_ref::<RunArray<Int64Type>>()
12579            .unwrap();
12580
12581        // Verify the cast worked correctly
12582        // Assert the values were cast correctly
12583        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
12584        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
12585        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
12586        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12587    }
12588
12589    #[test]
12590    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
12591        // Construct a valid dictionary encoded array
12592        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
12593        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
12594        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
12595
12596        // Attempt to cast to RunEndEncoded<Int64, Utf8>
12597        let target_type = DataType::RunEndEncoded(
12598            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12599            Arc::new(Field::new("values", DataType::Utf8, true)),
12600        );
12601        let cast_options = CastOptions {
12602            safe: false,
12603            format_options: FormatOptions::default(),
12604        };
12605
12606        // This should succeed
12607        let result = cast_with_options(&array_ref, &target_type, &cast_options)
12608            .expect("Cast should have succeeded but failed");
12609
12610        // Verify the cast worked correctly
12611        // Assert the values were cast correctly
12612        let run_array = result
12613            .as_any()
12614            .downcast_ref::<RunArray<Int64Type>>()
12615            .unwrap();
12616        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
12617        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
12618        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12619
12620        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
12621        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
12622    }
12623
12624    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
12625        vec![
12626            Some(vec![Some(1), Some(2), Some(3)]),
12627            Some(vec![Some(4), Some(5), Some(6)]),
12628            None,
12629            Some(vec![Some(7), Some(8), Some(9)]),
12630            Some(vec![None, Some(10)]),
12631        ]
12632    }
12633
12634    #[test]
12635    fn test_cast_list_view_to_list() {
12636        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12637        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12638        assert!(can_cast_types(list_view.data_type(), &target_type));
12639        let cast_result = cast(&list_view, &target_type).unwrap();
12640        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12641        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12642        assert_eq!(got_list, &expected_list);
12643    }
12644
12645    #[test]
12646    fn test_cast_list_to_list_view() {
12647        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12648        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12649        assert!(can_cast_types(list.data_type(), &target_type));
12650        let cast_result = cast(&list, &target_type).unwrap();
12651
12652        let got_list_view = cast_result
12653            .as_any()
12654            .downcast_ref::<ListViewArray>()
12655            .unwrap();
12656        let expected_list_view =
12657            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12658        assert_eq!(got_list_view, &expected_list_view);
12659    }
12660
12661    #[test]
12662    fn test_cast_large_list_view_to_large_list() {
12663        let list_view =
12664            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12665        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
12666        assert!(can_cast_types(list_view.data_type(), &target_type));
12667        let cast_result = cast(&list_view, &target_type).unwrap();
12668        let got_list = cast_result
12669            .as_any()
12670            .downcast_ref::<LargeListArray>()
12671            .unwrap();
12672
12673        let expected_list =
12674            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12675        assert_eq!(got_list, &expected_list);
12676    }
12677
12678    #[test]
12679    fn test_cast_large_list_to_large_list_view() {
12680        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12681        let target_type =
12682            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
12683        assert!(can_cast_types(list.data_type(), &target_type));
12684        let cast_result = cast(&list, &target_type).unwrap();
12685
12686        let got_list_view = cast_result
12687            .as_any()
12688            .downcast_ref::<LargeListViewArray>()
12689            .unwrap();
12690        let expected_list_view =
12691            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12692        assert_eq!(got_list_view, &expected_list_view);
12693    }
12694
12695    #[test]
12696    fn test_cast_list_view_to_list_out_of_order() {
12697        let list_view = ListViewArray::new(
12698            Arc::new(Field::new("item", DataType::Int32, true)),
12699            ScalarBuffer::from(vec![0, 6, 3]),
12700            ScalarBuffer::from(vec![3, 3, 3]),
12701            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
12702            None,
12703        );
12704        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12705        assert!(can_cast_types(list_view.data_type(), &target_type));
12706        let cast_result = cast(&list_view, &target_type).unwrap();
12707        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12708        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12709            Some(vec![Some(1), Some(2), Some(3)]),
12710            Some(vec![Some(7), Some(8), Some(9)]),
12711            Some(vec![Some(4), Some(5), Some(6)]),
12712        ]);
12713        assert_eq!(got_list, &expected_list);
12714    }
12715
12716    #[test]
12717    fn test_cast_list_view_to_list_overlapping() {
12718        let list_view = ListViewArray::new(
12719            Arc::new(Field::new("item", DataType::Int32, true)),
12720            ScalarBuffer::from(vec![0, 0]),
12721            ScalarBuffer::from(vec![1, 2]),
12722            Arc::new(Int32Array::from(vec![1, 2])),
12723            None,
12724        );
12725        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12726        assert!(can_cast_types(list_view.data_type(), &target_type));
12727        let cast_result = cast(&list_view, &target_type).unwrap();
12728        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12729        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12730            Some(vec![Some(1)]),
12731            Some(vec![Some(1), Some(2)]),
12732        ]);
12733        assert_eq!(got_list, &expected_list);
12734    }
12735
12736    #[test]
12737    fn test_cast_list_view_to_list_empty() {
12738        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
12739        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
12740        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12741        assert!(can_cast_types(list_view.data_type(), &target_type));
12742        let cast_result = cast(&list_view, &target_type).unwrap();
12743        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12744        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
12745        assert_eq!(got_list, &expected_list);
12746    }
12747
12748    #[test]
12749    fn test_cast_list_view_to_list_different_inner_type() {
12750        let values = int32_list_values();
12751        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
12752        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
12753        assert!(can_cast_types(list_view.data_type(), &target_type));
12754        let cast_result = cast(&list_view, &target_type).unwrap();
12755        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12756
12757        let expected_list =
12758            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
12759                list.map(|list| {
12760                    list.into_iter()
12761                        .map(|v| v.map(|v| v as i64))
12762                        .collect::<Vec<_>>()
12763                })
12764            }));
12765        assert_eq!(got_list, &expected_list);
12766    }
12767
12768    #[test]
12769    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
12770        let list_view = ListViewArray::new(
12771            Arc::new(Field::new("item", DataType::Int32, true)),
12772            ScalarBuffer::from(vec![0, 6, 3]),
12773            ScalarBuffer::from(vec![3, 3, 3]),
12774            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
12775            Some(NullBuffer::from(vec![false, true, false])),
12776        );
12777        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12778        assert!(can_cast_types(list_view.data_type(), &target_type));
12779        let cast_result = cast(&list_view, &target_type).unwrap();
12780        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12781        let expected_list = ListArray::new(
12782            Arc::new(Field::new("item", DataType::Int32, true)),
12783            OffsetBuffer::from_lengths([3, 3, 3]),
12784            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
12785            Some(NullBuffer::from(vec![false, true, false])),
12786        );
12787        assert_eq!(got_list, &expected_list);
12788    }
12789
12790    #[test]
12791    fn test_cast_list_view_to_large_list_view() {
12792        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12793        let target_type =
12794            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
12795        assert!(can_cast_types(list_view.data_type(), &target_type));
12796        let cast_result = cast(&list_view, &target_type).unwrap();
12797        let got = cast_result
12798            .as_any()
12799            .downcast_ref::<LargeListViewArray>()
12800            .unwrap();
12801
12802        let expected =
12803            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12804        assert_eq!(got, &expected);
12805    }
12806
12807    #[test]
12808    fn test_cast_large_list_view_to_list_view() {
12809        let list_view =
12810            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12811        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12812        assert!(can_cast_types(list_view.data_type(), &target_type));
12813        let cast_result = cast(&list_view, &target_type).unwrap();
12814        let got = cast_result
12815            .as_any()
12816            .downcast_ref::<ListViewArray>()
12817            .unwrap();
12818
12819        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12820        assert_eq!(got, &expected);
12821    }
12822
12823    #[test]
12824    fn test_cast_time32_second_to_int64() {
12825        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
12826        let array = Arc::new(array) as Arc<dyn Array>;
12827        let to_type = DataType::Int64;
12828        let cast_options = CastOptions::default();
12829
12830        assert!(can_cast_types(array.data_type(), &to_type));
12831
12832        let result = cast_with_options(&array, &to_type, &cast_options);
12833        assert!(
12834            result.is_ok(),
12835            "Failed to cast Time32(Second) to Int64: {:?}",
12836            result.err()
12837        );
12838
12839        let cast_array = result.unwrap();
12840        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12841
12842        assert_eq!(cast_array.value(0), 1000);
12843        assert_eq!(cast_array.value(1), 2000);
12844        assert_eq!(cast_array.value(2), 3000);
12845    }
12846
12847    #[test]
12848    fn test_cast_time32_millisecond_to_int64() {
12849        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
12850        let array = Arc::new(array) as Arc<dyn Array>;
12851        let to_type = DataType::Int64;
12852        let cast_options = CastOptions::default();
12853
12854        assert!(can_cast_types(array.data_type(), &to_type));
12855
12856        let result = cast_with_options(&array, &to_type, &cast_options);
12857        assert!(
12858            result.is_ok(),
12859            "Failed to cast Time32(Millisecond) to Int64: {:?}",
12860            result.err()
12861        );
12862
12863        let cast_array = result.unwrap();
12864        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12865
12866        assert_eq!(cast_array.value(0), 1000);
12867        assert_eq!(cast_array.value(1), 2000);
12868        assert_eq!(cast_array.value(2), 3000);
12869    }
12870
12871    #[test]
12872    fn test_cast_string_to_time32_second_to_int64() {
12873        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
12874        // raised in https://github.com/apache/datafusion/issues/19036
12875        let array = StringArray::from(vec!["03:12:44"]);
12876        let array = Arc::new(array) as Arc<dyn Array>;
12877        let cast_options = CastOptions::default();
12878
12879        // 1. Cast String to Time32(Second)
12880        let time32_type = DataType::Time32(TimeUnit::Second);
12881        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
12882
12883        // 2. Cast Time32(Second) to Int64
12884        let int64_type = DataType::Int64;
12885        assert!(can_cast_types(time32_array.data_type(), &int64_type));
12886
12887        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
12888
12889        assert!(
12890            result.is_ok(),
12891            "Failed to cast Time32(Second) to Int64: {:?}",
12892            result.err()
12893        );
12894
12895        let cast_array = result.unwrap();
12896        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12897
12898        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
12899        assert_eq!(cast_array.value(0), 11564);
12900    }
12901    #[test]
12902    fn test_string_dicts_to_binary_view() {
12903        let expected = BinaryViewArray::from_iter(vec![
12904            VIEW_TEST_DATA[1],
12905            VIEW_TEST_DATA[0],
12906            None,
12907            VIEW_TEST_DATA[3],
12908            None,
12909            VIEW_TEST_DATA[1],
12910            VIEW_TEST_DATA[4],
12911        ]);
12912
12913        let values_arrays: [ArrayRef; _] = [
12914            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
12915            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
12916            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
12917        ];
12918        for values in values_arrays {
12919            let keys =
12920                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
12921            let string_dict_array =
12922                DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
12923
12924            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
12925            assert_eq!(casted.as_ref(), &expected);
12926        }
12927    }
12928
12929    #[test]
12930    fn test_binary_dicts_to_string_view() {
12931        let expected = StringViewArray::from_iter(vec![
12932            VIEW_TEST_DATA[1],
12933            VIEW_TEST_DATA[0],
12934            None,
12935            VIEW_TEST_DATA[3],
12936            None,
12937            VIEW_TEST_DATA[1],
12938            VIEW_TEST_DATA[4],
12939        ]);
12940
12941        let values_arrays: [ArrayRef; _] = [
12942            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
12943            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
12944            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
12945        ];
12946        for values in values_arrays {
12947            let keys =
12948                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
12949            let string_dict_array =
12950                DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
12951
12952            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
12953            assert_eq!(casted.as_ref(), &expected);
12954        }
12955    }
12956
12957    #[test]
12958    fn test_cast_between_sliced_run_end_encoded() {
12959        let run_ends = Int16Array::from(vec![2, 5, 8]);
12960        let values = StringArray::from(vec!["a", "b", "c"]);
12961
12962        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12963        let ree_array = ree_array.slice(1, 2);
12964        let array_ref = Arc::new(ree_array) as ArrayRef;
12965
12966        let target_type = DataType::RunEndEncoded(
12967            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12968            Arc::new(Field::new("values", DataType::Utf8, true)),
12969        );
12970        let cast_options = CastOptions {
12971            safe: false,
12972            format_options: FormatOptions::default(),
12973        };
12974
12975        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
12976        let run_array = result.as_run::<Int64Type>();
12977        let run_array = run_array.downcast::<StringArray>().unwrap();
12978
12979        let expected = vec!["a", "b"];
12980        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
12981
12982        assert_eq!(expected, actual);
12983    }
12984}