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        (_, Boolean) => {
245            DataType::is_integer(from_type)
246                || DataType::is_floating(from_type)
247                || from_type == &Utf8View
248                || from_type == &Utf8
249                || from_type == &LargeUtf8
250        }
251        (Boolean, _) => {
252            DataType::is_integer(to_type)
253                || DataType::is_floating(to_type)
254                || to_type == &Utf8View
255                || to_type == &Utf8
256                || to_type == &LargeUtf8
257        }
258
259        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
260            true
261        }
262        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
263            true
264        }
265        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
266        (
267            Utf8 | LargeUtf8 | Utf8View,
268            Binary
269            | LargeBinary
270            | Utf8
271            | LargeUtf8
272            | Date32
273            | Date64
274            | Time32(Second)
275            | Time32(Millisecond)
276            | Time64(Microsecond)
277            | Time64(Nanosecond)
278            | Timestamp(Second, _)
279            | Timestamp(Millisecond, _)
280            | Timestamp(Microsecond, _)
281            | Timestamp(Nanosecond, _)
282            | Interval(_)
283            | BinaryView,
284        ) => true,
285        (Utf8 | LargeUtf8, Utf8View) => true,
286        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
287        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
288        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
289
290        (_, Binary | LargeBinary) => from_type.is_integer(),
291
292        // start numeric casts
293        (
294            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
295            | Float64,
296            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
297            | Float64,
298        ) => true,
299        // end numeric casts
300
301        // temporal casts
302        (Int32, Date32 | Date64 | Time32(_)) => true,
303        (Date32, Int32 | Int64) => true,
304        (Time32(_), Int32 | Int64) => true,
305        (Int64, Date64 | Date32 | Time64(_)) => true,
306        (Date64, Int64 | Int32) => true,
307        (Time64(_), Int64) => true,
308        (Date32 | Date64, Date32 | Date64) => true,
309        // time casts
310        (Time32(_), Time32(_)) => true,
311        (Time32(_), Time64(_)) => true,
312        (Time64(_), Time64(_)) => true,
313        (Time64(_), Time32(to_unit)) => {
314            matches!(to_unit, Second | Millisecond)
315        }
316        (Timestamp(_, _), _) if to_type.is_numeric() => true,
317        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
318        (Date64, Timestamp(_, _)) => true,
319        (Date32, Timestamp(_, _)) => true,
320        (
321            Timestamp(_, _),
322            Timestamp(_, _)
323            | Date32
324            | Date64
325            | Time32(Second)
326            | Time32(Millisecond)
327            | Time64(Microsecond)
328            | Time64(Nanosecond),
329        ) => true,
330        (_, Duration(_)) if from_type.is_numeric() => true,
331        (Duration(_), _) if to_type.is_numeric() => true,
332        (Duration(_), Duration(_)) => true,
333        (Interval(from_type), Int64) => {
334            match from_type {
335                YearMonth => true,
336                DayTime => true,
337                MonthDayNano => false, // Native type is i128
338            }
339        }
340        (Int32, Interval(to_type)) => match to_type {
341            YearMonth => true,
342            DayTime => false,
343            MonthDayNano => false,
344        },
345        (Duration(_), Interval(MonthDayNano)) => true,
346        (Interval(MonthDayNano), Duration(_)) => true,
347        (Interval(YearMonth), Interval(MonthDayNano)) => true,
348        (Interval(DayTime), Interval(MonthDayNano)) => true,
349        (_, _) => false,
350    }
351}
352
353/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
354///
355/// See [`cast_with_options`] for more information
356pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
357    cast_with_options(array, to_type, &CastOptions::default())
358}
359
360fn cast_integer_to_decimal<
361    T: ArrowPrimitiveType,
362    D: DecimalType + ArrowPrimitiveType<Native = M>,
363    M,
364>(
365    array: &PrimitiveArray<T>,
366    precision: u8,
367    scale: i8,
368    base: M,
369    cast_options: &CastOptions,
370) -> Result<ArrayRef, ArrowError>
371where
372    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
373    M: ArrowNativeTypeOp,
374{
375    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
376        ArrowError::CastError(format!(
377            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
378            D::PREFIX,
379            precision,
380            scale,
381        ))
382    })?;
383
384    let array = if scale < 0 {
385        match cast_options.safe {
386            true => array.unary_opt::<_, D>(|v| {
387                v.as_()
388                    .div_checked(scale_factor)
389                    .ok()
390                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
391            }),
392            false => array.try_unary::<_, D, _>(|v| {
393                v.as_()
394                    .div_checked(scale_factor)
395                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
396            })?,
397        }
398    } else {
399        match cast_options.safe {
400            true => array.unary_opt::<_, D>(|v| {
401                v.as_()
402                    .mul_checked(scale_factor)
403                    .ok()
404                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
405            }),
406            false => array.try_unary::<_, D, _>(|v| {
407                v.as_()
408                    .mul_checked(scale_factor)
409                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
410            })?,
411        }
412    };
413
414    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
415}
416
417/// Cast the array from interval year month to month day nano
418fn cast_interval_year_month_to_interval_month_day_nano(
419    array: &dyn Array,
420    _cast_options: &CastOptions,
421) -> Result<ArrayRef, ArrowError> {
422    let array = array.as_primitive::<IntervalYearMonthType>();
423
424    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
425        let months = IntervalYearMonthType::to_months(v);
426        IntervalMonthDayNanoType::make_value(months, 0, 0)
427    })))
428}
429
430/// Cast the array from interval day time to month day nano
431fn cast_interval_day_time_to_interval_month_day_nano(
432    array: &dyn Array,
433    _cast_options: &CastOptions,
434) -> Result<ArrayRef, ArrowError> {
435    let array = array.as_primitive::<IntervalDayTimeType>();
436    let mul = 1_000_000;
437
438    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
439        let (days, ms) = IntervalDayTimeType::to_parts(v);
440        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
441    })))
442}
443
444/// Cast the array from interval to duration
445fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
446    array: &dyn Array,
447    cast_options: &CastOptions,
448) -> Result<ArrayRef, ArrowError> {
449    let array = array.as_primitive::<IntervalMonthDayNanoType>();
450    let scale = match D::DATA_TYPE {
451        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
452        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
453        DataType::Duration(TimeUnit::Microsecond) => 1_000,
454        DataType::Duration(TimeUnit::Nanosecond) => 1,
455        _ => unreachable!(),
456    };
457
458    if cast_options.safe {
459        let iter = array.iter().map(|v| {
460            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
461        });
462        Ok(Arc::new(unsafe {
463            PrimitiveArray::<D>::from_trusted_len_iter(iter)
464        }))
465    } else {
466        let vec = array
467            .iter()
468            .map(|v| {
469                v.map(|v| match v.days == 0 && v.months == 0 {
470                    true => Ok((v.nanoseconds) / scale),
471                    _ => Err(ArrowError::ComputeError(
472                        "Cannot convert interval containing non-zero months or days to duration"
473                            .to_string(),
474                    )),
475                })
476                .transpose()
477            })
478            .collect::<Result<Vec<_>, _>>()?;
479        Ok(Arc::new(unsafe {
480            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
481        }))
482    }
483}
484
485/// Cast the array from duration and interval
486fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
487    array: &dyn Array,
488    cast_options: &CastOptions,
489) -> Result<ArrayRef, ArrowError> {
490    let array = array
491        .as_any()
492        .downcast_ref::<PrimitiveArray<D>>()
493        .ok_or_else(|| {
494            ArrowError::ComputeError(
495                "Internal Error: Cannot cast duration to DurationArray of expected type"
496                    .to_string(),
497            )
498        })?;
499
500    let scale = match array.data_type() {
501        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
502        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
503        DataType::Duration(TimeUnit::Microsecond) => 1_000,
504        DataType::Duration(TimeUnit::Nanosecond) => 1,
505        _ => unreachable!(),
506    };
507
508    if cast_options.safe {
509        let iter = array.iter().map(|v| {
510            v.and_then(|v| {
511                v.checked_mul(scale)
512                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
513            })
514        });
515        Ok(Arc::new(unsafe {
516            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
517        }))
518    } else {
519        let vec = array
520            .iter()
521            .map(|v| {
522                v.map(|v| {
523                    if let Ok(v) = v.mul_checked(scale) {
524                        Ok(IntervalMonthDayNano::new(0, 0, v))
525                    } else {
526                        Err(ArrowError::ComputeError(format!(
527                            "Cannot cast to {:?}. Overflowing on {:?}",
528                            IntervalMonthDayNanoType::DATA_TYPE,
529                            v
530                        )))
531                    }
532                })
533                .transpose()
534            })
535            .collect::<Result<Vec<_>, _>>()?;
536        Ok(Arc::new(unsafe {
537            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
538        }))
539    }
540}
541
542/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
543fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
544    array: &dyn Array,
545) -> Result<ArrayRef, ArrowError> {
546    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
547}
548
549fn make_timestamp_array(
550    array: &PrimitiveArray<Int64Type>,
551    unit: TimeUnit,
552    tz: Option<Arc<str>>,
553) -> ArrayRef {
554    match unit {
555        TimeUnit::Second => Arc::new(
556            array
557                .reinterpret_cast::<TimestampSecondType>()
558                .with_timezone_opt(tz),
559        ),
560        TimeUnit::Millisecond => Arc::new(
561            array
562                .reinterpret_cast::<TimestampMillisecondType>()
563                .with_timezone_opt(tz),
564        ),
565        TimeUnit::Microsecond => Arc::new(
566            array
567                .reinterpret_cast::<TimestampMicrosecondType>()
568                .with_timezone_opt(tz),
569        ),
570        TimeUnit::Nanosecond => Arc::new(
571            array
572                .reinterpret_cast::<TimestampNanosecondType>()
573                .with_timezone_opt(tz),
574        ),
575    }
576}
577
578fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
579    match unit {
580        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
581        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
582        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
583        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
584    }
585}
586
587fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
588    v: i64,
589    tz: Option<Tz>,
590) -> Result<NaiveTime, ArrowError> {
591    let time = match tz {
592        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
593        None => as_datetime::<T>(v).map(|d| d.time()),
594    };
595
596    time.ok_or_else(|| {
597        ArrowError::CastError(format!(
598            "Failed to create naive time with {} {}",
599            std::any::type_name::<T>(),
600            v
601        ))
602    })
603}
604
605fn timestamp_to_date32<T: ArrowTimestampType>(
606    array: &PrimitiveArray<T>,
607) -> Result<ArrayRef, ArrowError> {
608    let err = |x: i64| {
609        ArrowError::CastError(format!(
610            "Cannot convert {} {x} to datetime",
611            std::any::type_name::<T>()
612        ))
613    };
614
615    let array: Date32Array = match array.timezone() {
616        Some(tz) => {
617            let tz: Tz = tz.parse()?;
618            array.try_unary(|x| {
619                as_datetime_with_timezone::<T>(x, tz)
620                    .ok_or_else(|| err(x))
621                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
622            })?
623        }
624        None => array.try_unary(|x| {
625            as_datetime::<T>(x)
626                .ok_or_else(|| err(x))
627                .map(|d| Date32Type::from_naive_date(d.date()))
628        })?,
629    };
630    Ok(Arc::new(array))
631}
632
633/// Try to cast `array` to `to_type` if possible.
634///
635/// Returns a new Array with type `to_type` if possible.
636///
637/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
638///
639/// # Behavior
640/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
641/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
642///   short variants are accepted, other strings return null or error
643/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
644///   in integer casts return null
645/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
646/// * `List` to `List`: the underlying data type is cast
647/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
648///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
649/// * Primitive to `List`: a list array with 1 value per slot is created
650/// * `Date32` and `Date64`: precision lost when going to higher interval
651/// * `Time32 and `Time64`: precision lost when going to higher interval
652/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
653/// * Temporal to/from backing Primitive: zero-copy with data type change
654/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
655///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
656/// * `Decimal` to `Float32/Float64` is lossy and values outside the representable
657///   range become `INFINITY` or `-INFINITY` without error.
658///
659/// Unsupported Casts (check with `can_cast_types` before calling):
660/// * To or from `StructArray`
661/// * `List` to `Primitive`
662/// * `Interval` and `Duration`
663///
664/// # Durations and Intervals
665///
666/// Casting integer types directly to interval types such as
667/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
668/// is ambiguous. For example, the integer  could represent either nanoseconds
669/// or months.
670///
671/// To cast an integer type to an interval type, first convert to a Duration
672/// type, and then cast that to the desired interval type.
673///
674/// For example, to convert an `Int64` representing nanoseconds to an
675/// `IntervalMonthDayNano` you would first convert the `Int64` to a
676/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
677///
678/// # Timestamps and Timezones
679///
680/// Timestamps are stored with an optional timezone in Arrow.
681///
682/// ## Casting timestamps to a timestamp without timezone / UTC
683/// ```
684/// # use arrow_array::Int64Array;
685/// # use arrow_array::types::TimestampSecondType;
686/// # use arrow_cast::{cast, display};
687/// # use arrow_array::cast::AsArray;
688/// # use arrow_schema::{DataType, TimeUnit};
689/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
690/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
691/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
692/// let b = cast(&a, &data_type).unwrap();
693/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
694/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
695/// // use display to show them (note has no trailing Z)
696/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
697/// ```
698///
699/// ## Casting timestamps to a timestamp with timezone
700///
701/// Similarly to the previous example, if you cast numeric values to a timestamp
702/// with timezone, the cast kernel will not change the underlying values
703/// but display and other functions will interpret them as being in the provided timezone.
704///
705/// ```
706/// # use arrow_array::Int64Array;
707/// # use arrow_array::types::TimestampSecondType;
708/// # use arrow_cast::{cast, display};
709/// # use arrow_array::cast::AsArray;
710/// # use arrow_schema::{DataType, TimeUnit};
711/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
712/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
713/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
714/// let b = cast(&a, &data_type).unwrap();
715/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
716/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
717/// // displayed in the target timezone (note the offset -05:00)
718/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
719/// ```
720/// # Casting timestamps without timezone to timestamps with timezone
721///
722/// When casting from a timestamp without timezone to a timestamp with
723/// timezone, the cast kernel interprets the timestamp values as being in
724/// the destination timezone and then adjusts the underlying value to UTC as required
725///
726/// However, note that when casting from a timestamp with timezone BACK to a
727/// timestamp without timezone the cast kernel does not adjust the values.
728///
729/// Thus round trip casting a timestamp without timezone to a timestamp with
730/// timezone and back to a timestamp without timezone results in different
731/// values than the starting values.
732///
733/// ```
734/// # use arrow_array::Int64Array;
735/// # use arrow_array::types::{TimestampSecondType};
736/// # use arrow_cast::{cast, display};
737/// # use arrow_array::cast::AsArray;
738/// # use arrow_schema::{DataType, TimeUnit};
739/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
740/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
741/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
742/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
743/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
744/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
745/// // displayed without a timezone (note lack of offset or Z)
746/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
747///
748/// // Convert timestamps without a timezone to timestamps with a timezone
749/// let c = cast(&b, &data_type_tz).unwrap();
750/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
751/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
752/// // displayed with the target timezone offset (-05:00)
753/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
754///
755/// // Convert from timestamp with timezone back to timestamp without timezone
756/// let d = cast(&c, &data_type).unwrap();
757/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
758/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
759/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
760/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
761/// ```
762pub fn cast_with_options(
763    array: &dyn Array,
764    to_type: &DataType,
765    cast_options: &CastOptions,
766) -> Result<ArrayRef, ArrowError> {
767    use DataType::*;
768    let from_type = array.data_type();
769    // clone array if types are the same
770    if from_type == to_type {
771        return Ok(make_array(array.to_data()));
772    }
773    match (from_type, to_type) {
774        (Null, _) => Ok(new_null_array(to_type, array.len())),
775        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
776            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
777            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
778            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
779            _ => Err(ArrowError::CastError(format!(
780                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
781            ))),
782        },
783        (_, RunEndEncoded(index_type, value_type)) => {
784            let array_ref = make_array(array.to_data());
785            match index_type.data_type() {
786                Int16 => cast_to_run_end_encoded::<Int16Type>(
787                    &array_ref,
788                    value_type.data_type(),
789                    cast_options,
790                ),
791                Int32 => cast_to_run_end_encoded::<Int32Type>(
792                    &array_ref,
793                    value_type.data_type(),
794                    cast_options,
795                ),
796                Int64 => cast_to_run_end_encoded::<Int64Type>(
797                    &array_ref,
798                    value_type.data_type(),
799                    cast_options,
800                ),
801                _ => Err(ArrowError::CastError(format!(
802                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
803                ))),
804            }
805        }
806        (Dictionary(index_type, _), _) => match **index_type {
807            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
808            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
809            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
810            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
811            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
812            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
813            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
814            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
815            _ => Err(ArrowError::CastError(format!(
816                "Casting from dictionary type {from_type} to {to_type} not supported",
817            ))),
818        },
819        (_, Dictionary(index_type, value_type)) => match **index_type {
820            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
821            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
822            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
823            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
824            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
825            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
826            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
827            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
828            _ => Err(ArrowError::CastError(format!(
829                "Casting from type {from_type} to dictionary type {to_type} not supported",
830            ))),
831        },
832        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
833        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
834        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
835        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
836        (List(_), FixedSizeList(field, size)) => {
837            let array = array.as_list::<i32>();
838            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
839        }
840        (LargeList(_), FixedSizeList(field, size)) => {
841            let array = array.as_list::<i64>();
842            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
843        }
844        (ListView(_), List(list_to)) => cast_list_view_to_list::<i32>(array, list_to, cast_options),
845        (LargeListView(_), LargeList(list_to)) => {
846            cast_list_view_to_list::<i64>(array, list_to, cast_options)
847        }
848        (ListView(_), LargeListView(list_to)) => {
849            cast_list_view::<i32, i64>(array, list_to, cast_options)
850        }
851        (LargeListView(_), ListView(list_to)) => {
852            cast_list_view::<i64, i32>(array, list_to, cast_options)
853        }
854        (List(_), ListView(_)) => cast_list_to_list_view::<i32>(array),
855        (LargeList(_), LargeListView(_)) => cast_list_to_list_view::<i64>(array),
856        (List(_) | LargeList(_), _) => match to_type {
857            Utf8 => value_to_string::<i32>(array, cast_options),
858            LargeUtf8 => value_to_string::<i64>(array, cast_options),
859            _ => Err(ArrowError::CastError(
860                "Cannot cast list to non-list data types".to_string(),
861            )),
862        },
863        (FixedSizeList(list_from, size), List(list_to)) => {
864            if list_to.data_type() != list_from.data_type() {
865                // To transform inner type, can first cast to FSL with new inner type.
866                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
867                let array = cast_with_options(array, &fsl_to, cast_options)?;
868                cast_fixed_size_list_to_list::<i32>(array.as_ref())
869            } else {
870                cast_fixed_size_list_to_list::<i32>(array)
871            }
872        }
873        (FixedSizeList(list_from, size), LargeList(list_to)) => {
874            if list_to.data_type() != list_from.data_type() {
875                // To transform inner type, can first cast to FSL with new inner type.
876                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
877                let array = cast_with_options(array, &fsl_to, cast_options)?;
878                cast_fixed_size_list_to_list::<i64>(array.as_ref())
879            } else {
880                cast_fixed_size_list_to_list::<i64>(array)
881            }
882        }
883        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
884            if size_from != size_to {
885                return Err(ArrowError::CastError(
886                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
887                ));
888            }
889            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
890            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
891            Ok(Arc::new(FixedSizeListArray::try_new(
892                list_to.clone(),
893                *size_from,
894                values,
895                array.nulls().cloned(),
896            )?))
897        }
898        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
899        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
900        (_, FixedSizeList(to, size)) if *size == 1 => {
901            cast_values_to_fixed_size_list(array, to, *size, cast_options)
902        }
903        (FixedSizeList(_, size), _) if *size == 1 => {
904            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
905        }
906        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
907            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
908        }
909        // Decimal to decimal, same width
910        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
911            cast_decimal_to_decimal_same_type::<Decimal32Type>(
912                array.as_primitive(),
913                *p1,
914                *s1,
915                *p2,
916                *s2,
917                cast_options,
918            )
919        }
920        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
921            cast_decimal_to_decimal_same_type::<Decimal64Type>(
922                array.as_primitive(),
923                *p1,
924                *s1,
925                *p2,
926                *s2,
927                cast_options,
928            )
929        }
930        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
931            cast_decimal_to_decimal_same_type::<Decimal128Type>(
932                array.as_primitive(),
933                *p1,
934                *s1,
935                *p2,
936                *s2,
937                cast_options,
938            )
939        }
940        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
941            cast_decimal_to_decimal_same_type::<Decimal256Type>(
942                array.as_primitive(),
943                *p1,
944                *s1,
945                *p2,
946                *s2,
947                cast_options,
948            )
949        }
950        // Decimal to decimal, different width
951        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
952            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
953                array.as_primitive(),
954                *p1,
955                *s1,
956                *p2,
957                *s2,
958                cast_options,
959            )
960        }
961        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
962            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
963                array.as_primitive(),
964                *p1,
965                *s1,
966                *p2,
967                *s2,
968                cast_options,
969            )
970        }
971        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
972            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
973                array.as_primitive(),
974                *p1,
975                *s1,
976                *p2,
977                *s2,
978                cast_options,
979            )
980        }
981        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
982            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
983                array.as_primitive(),
984                *p1,
985                *s1,
986                *p2,
987                *s2,
988                cast_options,
989            )
990        }
991        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
992            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
993                array.as_primitive(),
994                *p1,
995                *s1,
996                *p2,
997                *s2,
998                cast_options,
999            )
1000        }
1001        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1002            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1003                array.as_primitive(),
1004                *p1,
1005                *s1,
1006                *p2,
1007                *s2,
1008                cast_options,
1009            )
1010        }
1011        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1012            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1013                array.as_primitive(),
1014                *p1,
1015                *s1,
1016                *p2,
1017                *s2,
1018                cast_options,
1019            )
1020        }
1021        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1022            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1023                array.as_primitive(),
1024                *p1,
1025                *s1,
1026                *p2,
1027                *s2,
1028                cast_options,
1029            )
1030        }
1031        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1032            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1033                array.as_primitive(),
1034                *p1,
1035                *s1,
1036                *p2,
1037                *s2,
1038                cast_options,
1039            )
1040        }
1041        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1042            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1043                array.as_primitive(),
1044                *p1,
1045                *s1,
1046                *p2,
1047                *s2,
1048                cast_options,
1049            )
1050        }
1051        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1052            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1053                array.as_primitive(),
1054                *p1,
1055                *s1,
1056                *p2,
1057                *s2,
1058                cast_options,
1059            )
1060        }
1061        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1062            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1063                array.as_primitive(),
1064                *p1,
1065                *s1,
1066                *p2,
1067                *s2,
1068                cast_options,
1069            )
1070        }
1071        // Decimal to non-decimal
1072        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1073            cast_from_decimal::<Decimal32Type, _>(
1074                array,
1075                10_i32,
1076                scale,
1077                from_type,
1078                to_type,
1079                |x: i32| x as f64,
1080                cast_options,
1081            )
1082        }
1083        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1084            cast_from_decimal::<Decimal64Type, _>(
1085                array,
1086                10_i64,
1087                scale,
1088                from_type,
1089                to_type,
1090                |x: i64| x as f64,
1091                cast_options,
1092            )
1093        }
1094        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1095            cast_from_decimal::<Decimal128Type, _>(
1096                array,
1097                10_i128,
1098                scale,
1099                from_type,
1100                to_type,
1101                |x: i128| x as f64,
1102                cast_options,
1103            )
1104        }
1105        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1106            cast_from_decimal::<Decimal256Type, _>(
1107                array,
1108                i256::from_i128(10_i128),
1109                scale,
1110                from_type,
1111                to_type,
1112                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1113                cast_options,
1114            )
1115        }
1116        // Non-decimal to decimal
1117        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1118            cast_to_decimal::<Decimal32Type, _>(
1119                array,
1120                10_i32,
1121                precision,
1122                scale,
1123                from_type,
1124                to_type,
1125                cast_options,
1126            )
1127        }
1128        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1129            cast_to_decimal::<Decimal64Type, _>(
1130                array,
1131                10_i64,
1132                precision,
1133                scale,
1134                from_type,
1135                to_type,
1136                cast_options,
1137            )
1138        }
1139        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1140            cast_to_decimal::<Decimal128Type, _>(
1141                array,
1142                10_i128,
1143                precision,
1144                scale,
1145                from_type,
1146                to_type,
1147                cast_options,
1148            )
1149        }
1150        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1151            cast_to_decimal::<Decimal256Type, _>(
1152                array,
1153                i256::from_i128(10_i128),
1154                precision,
1155                scale,
1156                from_type,
1157                to_type,
1158                cast_options,
1159            )
1160        }
1161        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1162            array.as_struct(),
1163            from_fields.clone(),
1164            to_fields.clone(),
1165            cast_options,
1166        ),
1167        (Struct(_), _) => Err(ArrowError::CastError(format!(
1168            "Casting from {from_type} to {to_type} not supported"
1169        ))),
1170        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1171            "Casting from {from_type} to {to_type} not supported"
1172        ))),
1173        (_, Boolean) => match from_type {
1174            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1175            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1176            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1177            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1178            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1179            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1180            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1181            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1182            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1183            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1184            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1185            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1186            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1187            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1188            _ => Err(ArrowError::CastError(format!(
1189                "Casting from {from_type} to {to_type} not supported",
1190            ))),
1191        },
1192        (Boolean, _) => match to_type {
1193            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1194            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1195            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1196            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1197            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1198            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1199            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1200            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1201            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1202            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1203            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1204            Utf8View => value_to_string_view(array, cast_options),
1205            Utf8 => value_to_string::<i32>(array, cast_options),
1206            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1207            _ => Err(ArrowError::CastError(format!(
1208                "Casting from {from_type} to {to_type} not supported",
1209            ))),
1210        },
1211        (Utf8, _) => match to_type {
1212            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1213            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1214            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1215            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1216            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1217            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1218            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1219            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1220            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1221            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1222            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1223            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1224            Binary => Ok(Arc::new(BinaryArray::from(
1225                array.as_string::<i32>().clone(),
1226            ))),
1227            LargeBinary => {
1228                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1229                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1230            }
1231            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1232            BinaryView => Ok(Arc::new(
1233                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1234            )),
1235            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1236            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1237            Time32(TimeUnit::Millisecond) => {
1238                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1239            }
1240            Time64(TimeUnit::Microsecond) => {
1241                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1242            }
1243            Time64(TimeUnit::Nanosecond) => {
1244                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1245            }
1246            Timestamp(TimeUnit::Second, to_tz) => {
1247                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1248            }
1249            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1250                i32,
1251                TimestampMillisecondType,
1252            >(array, to_tz, cast_options),
1253            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1254                i32,
1255                TimestampMicrosecondType,
1256            >(array, to_tz, cast_options),
1257            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1258                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1259            }
1260            Interval(IntervalUnit::YearMonth) => {
1261                cast_string_to_year_month_interval::<i32>(array, cast_options)
1262            }
1263            Interval(IntervalUnit::DayTime) => {
1264                cast_string_to_day_time_interval::<i32>(array, cast_options)
1265            }
1266            Interval(IntervalUnit::MonthDayNano) => {
1267                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1268            }
1269            _ => Err(ArrowError::CastError(format!(
1270                "Casting from {from_type} to {to_type} not supported",
1271            ))),
1272        },
1273        (Utf8View, _) => match to_type {
1274            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1275            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1276            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1277            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1278            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1279            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1280            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1281            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1282            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1283            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1284            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1285            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1286            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1287            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1288            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1289            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1290            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1291            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1292            Time32(TimeUnit::Millisecond) => {
1293                parse_string_view::<Time32MillisecondType>(array, cast_options)
1294            }
1295            Time64(TimeUnit::Microsecond) => {
1296                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1297            }
1298            Time64(TimeUnit::Nanosecond) => {
1299                parse_string_view::<Time64NanosecondType>(array, cast_options)
1300            }
1301            Timestamp(TimeUnit::Second, to_tz) => {
1302                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1303            }
1304            Timestamp(TimeUnit::Millisecond, to_tz) => {
1305                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1306            }
1307            Timestamp(TimeUnit::Microsecond, to_tz) => {
1308                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1309            }
1310            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1311                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1312            }
1313            Interval(IntervalUnit::YearMonth) => {
1314                cast_view_to_year_month_interval(array, cast_options)
1315            }
1316            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1317            Interval(IntervalUnit::MonthDayNano) => {
1318                cast_view_to_month_day_nano_interval(array, cast_options)
1319            }
1320            _ => Err(ArrowError::CastError(format!(
1321                "Casting from {from_type} to {to_type} not supported",
1322            ))),
1323        },
1324        (LargeUtf8, _) => match to_type {
1325            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1326            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1327            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1328            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1329            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1330            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1331            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1332            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1333            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1334            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1335            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1336            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1337            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1338            Binary => {
1339                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1340                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1341            }
1342            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1343                array.as_string::<i64>().clone(),
1344            ))),
1345            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1346            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1347                array
1348                    .as_string::<i64>()
1349                    .into_iter()
1350                    .map(|x| x.map(|x| x.as_bytes()))
1351                    .collect::<Vec<_>>(),
1352            ))),
1353            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1354            Time32(TimeUnit::Millisecond) => {
1355                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1356            }
1357            Time64(TimeUnit::Microsecond) => {
1358                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1359            }
1360            Time64(TimeUnit::Nanosecond) => {
1361                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1362            }
1363            Timestamp(TimeUnit::Second, to_tz) => {
1364                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1365            }
1366            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1367                i64,
1368                TimestampMillisecondType,
1369            >(array, to_tz, cast_options),
1370            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1371                i64,
1372                TimestampMicrosecondType,
1373            >(array, to_tz, cast_options),
1374            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1375                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1376            }
1377            Interval(IntervalUnit::YearMonth) => {
1378                cast_string_to_year_month_interval::<i64>(array, cast_options)
1379            }
1380            Interval(IntervalUnit::DayTime) => {
1381                cast_string_to_day_time_interval::<i64>(array, cast_options)
1382            }
1383            Interval(IntervalUnit::MonthDayNano) => {
1384                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1385            }
1386            _ => Err(ArrowError::CastError(format!(
1387                "Casting from {from_type} to {to_type} not supported",
1388            ))),
1389        },
1390        (Binary, _) => match to_type {
1391            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1392            LargeUtf8 => {
1393                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1394                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1395            }
1396            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1397            FixedSizeBinary(size) => {
1398                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1399            }
1400            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1401            Utf8View => Ok(Arc::new(StringViewArray::from(
1402                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1403            ))),
1404            _ => Err(ArrowError::CastError(format!(
1405                "Casting from {from_type} to {to_type} not supported",
1406            ))),
1407        },
1408        (LargeBinary, _) => match to_type {
1409            Utf8 => {
1410                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1411                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1412            }
1413            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1414            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1415            FixedSizeBinary(size) => {
1416                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1417            }
1418            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1419            Utf8View => {
1420                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1421                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1422            }
1423            _ => Err(ArrowError::CastError(format!(
1424                "Casting from {from_type} to {to_type} not supported",
1425            ))),
1426        },
1427        (FixedSizeBinary(size), _) => match to_type {
1428            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1429            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1430            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1431            _ => Err(ArrowError::CastError(format!(
1432                "Casting from {from_type} to {to_type} not supported",
1433            ))),
1434        },
1435        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1436        (BinaryView, LargeBinary) => {
1437            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1438        }
1439        (BinaryView, Utf8) => {
1440            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1441            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1442        }
1443        (BinaryView, LargeUtf8) => {
1444            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1445            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1446        }
1447        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1448        (BinaryView, _) => Err(ArrowError::CastError(format!(
1449            "Casting from {from_type} to {to_type} not supported",
1450        ))),
1451        (from_type, Utf8View) if from_type.is_primitive() => {
1452            value_to_string_view(array, cast_options)
1453        }
1454        (from_type, LargeUtf8) if from_type.is_primitive() => {
1455            value_to_string::<i64>(array, cast_options)
1456        }
1457        (from_type, Utf8) if from_type.is_primitive() => {
1458            value_to_string::<i32>(array, cast_options)
1459        }
1460        (from_type, Binary) if from_type.is_integer() => match from_type {
1461            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1462            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1463            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1464            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1465            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1466            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1467            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1468            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1469            _ => unreachable!(),
1470        },
1471        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1472            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1473            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1474            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1475            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1476            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1477            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1478            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1479            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1480            _ => unreachable!(),
1481        },
1482        // start numeric casts
1483        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1484        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1485        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1486        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1487        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1488        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1489        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1490        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1491        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1492        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1493
1494        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1495        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1496        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1497        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1498        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1499        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1500        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1501        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1502        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1503        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1504
1505        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1506        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1507        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1508        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1509        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1510        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1511        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1512        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1513        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1514        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1515
1516        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1517        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1518        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1519        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1520        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1521        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1522        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1523        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1524        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1525        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1526
1527        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1528        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1529        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1530        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1531        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1532        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1533        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1534        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1535        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1536        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1537
1538        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1539        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1540        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1541        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1542        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1543        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1544        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1545        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1546        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1547        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1548
1549        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1550        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1551        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1552        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1553        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1554        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1555        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1556        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1557        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1558        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1559
1560        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1561        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1562        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1563        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1564        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1565        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1566        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1567        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1568        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1569        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1570
1571        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1572        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1573        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1574        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1575        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1576        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1577        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1578        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1579        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1580        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1581
1582        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1583        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1584        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1585        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1586        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1587        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1588        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1589        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1590        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1591        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1592
1593        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1594        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1595        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1596        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1597        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1598        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1599        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1600        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1601        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1602        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1603        // end numeric casts
1604
1605        // temporal casts
1606        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1607        (Int32, Date64) => cast_with_options(
1608            &cast_with_options(array, &Date32, cast_options)?,
1609            &Date64,
1610            cast_options,
1611        ),
1612        (Int32, Time32(TimeUnit::Second)) => {
1613            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1614        }
1615        (Int32, Time32(TimeUnit::Millisecond)) => {
1616            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1617        }
1618        // No support for microsecond/nanosecond with i32
1619        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1620        (Date32, Int64) => cast_with_options(
1621            &cast_with_options(array, &Int32, cast_options)?,
1622            &Int64,
1623            cast_options,
1624        ),
1625        (Time32(TimeUnit::Second), Int32) => {
1626            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1627        }
1628        (Time32(TimeUnit::Millisecond), Int32) => {
1629            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1630        }
1631        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1632            &cast_with_options(array, &Int32, cast_options)?,
1633            &Int64,
1634            cast_options,
1635        ),
1636        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1637            &cast_with_options(array, &Int32, cast_options)?,
1638            &Int64,
1639            cast_options,
1640        ),
1641        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1642        (Int64, Date32) => cast_with_options(
1643            &cast_with_options(array, &Int32, cast_options)?,
1644            &Date32,
1645            cast_options,
1646        ),
1647        // No support for second/milliseconds with i64
1648        (Int64, Time64(TimeUnit::Microsecond)) => {
1649            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1650        }
1651        (Int64, Time64(TimeUnit::Nanosecond)) => {
1652            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1653        }
1654
1655        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1656        (Date64, Int32) => cast_with_options(
1657            &cast_with_options(array, &Int64, cast_options)?,
1658            &Int32,
1659            cast_options,
1660        ),
1661        (Time64(TimeUnit::Microsecond), Int64) => {
1662            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1663        }
1664        (Time64(TimeUnit::Nanosecond), Int64) => {
1665            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1666        }
1667        (Date32, Date64) => Ok(Arc::new(
1668            array
1669                .as_primitive::<Date32Type>()
1670                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1671        )),
1672        (Date64, Date32) => Ok(Arc::new(
1673            array
1674                .as_primitive::<Date64Type>()
1675                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1676        )),
1677
1678        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1679            array
1680                .as_primitive::<Time32SecondType>()
1681                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1682        )),
1683        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1684            array
1685                .as_primitive::<Time32SecondType>()
1686                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1687        )),
1688        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1689            array
1690                .as_primitive::<Time32SecondType>()
1691                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1692        )),
1693
1694        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1695            array
1696                .as_primitive::<Time32MillisecondType>()
1697                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1698        )),
1699        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1700            array
1701                .as_primitive::<Time32MillisecondType>()
1702                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1703        )),
1704        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1705            array
1706                .as_primitive::<Time32MillisecondType>()
1707                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1708        )),
1709
1710        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1711            array
1712                .as_primitive::<Time64MicrosecondType>()
1713                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1714        )),
1715        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1716            array
1717                .as_primitive::<Time64MicrosecondType>()
1718                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1719        )),
1720        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1721            array
1722                .as_primitive::<Time64MicrosecondType>()
1723                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1724        )),
1725
1726        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1727            array
1728                .as_primitive::<Time64NanosecondType>()
1729                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1730        )),
1731        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1732            array
1733                .as_primitive::<Time64NanosecondType>()
1734                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1735        )),
1736        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1737            array
1738                .as_primitive::<Time64NanosecondType>()
1739                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1740        )),
1741
1742        // Timestamp to integer/floating/decimals
1743        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1744            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1745            cast_with_options(&array, to_type, cast_options)
1746        }
1747        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1748            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1749            cast_with_options(&array, to_type, cast_options)
1750        }
1751        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1752            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1753            cast_with_options(&array, to_type, cast_options)
1754        }
1755        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1756            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1757            cast_with_options(&array, to_type, cast_options)
1758        }
1759
1760        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1761            let array = cast_with_options(array, &Int64, cast_options)?;
1762            Ok(make_timestamp_array(
1763                array.as_primitive(),
1764                *unit,
1765                tz.clone(),
1766            ))
1767        }
1768
1769        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1770            let array = cast_with_options(array, &Int64, cast_options)?;
1771            let time_array = array.as_primitive::<Int64Type>();
1772            let from_size = time_unit_multiple(from_unit);
1773            let to_size = time_unit_multiple(to_unit);
1774            // we either divide or multiply, depending on size of each unit
1775            // units are never the same when the types are the same
1776            let converted = match from_size.cmp(&to_size) {
1777                Ordering::Greater => {
1778                    let divisor = from_size / to_size;
1779                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1780                }
1781                Ordering::Equal => time_array.clone(),
1782                Ordering::Less => {
1783                    let mul = to_size / from_size;
1784                    if cast_options.safe {
1785                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1786                    } else {
1787                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1788                    }
1789                }
1790            };
1791            // Normalize timezone
1792            let adjusted = match (from_tz, to_tz) {
1793                // Only this case needs to be adjusted because we're casting from
1794                // unknown time offset to some time offset, we want the time to be
1795                // unchanged.
1796                //
1797                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1798                (None, Some(to_tz)) => {
1799                    let to_tz: Tz = to_tz.parse()?;
1800                    match to_unit {
1801                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1802                            converted,
1803                            &to_tz,
1804                            cast_options,
1805                        )?,
1806                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1807                            TimestampMillisecondType,
1808                        >(
1809                            converted, &to_tz, cast_options
1810                        )?,
1811                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1812                            TimestampMicrosecondType,
1813                        >(
1814                            converted, &to_tz, cast_options
1815                        )?,
1816                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1817                            TimestampNanosecondType,
1818                        >(
1819                            converted, &to_tz, cast_options
1820                        )?,
1821                    }
1822                }
1823                _ => converted,
1824            };
1825            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1826        }
1827        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1828            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1829        }
1830        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1831            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1832        }
1833        (Timestamp(TimeUnit::Second, _), Date32) => {
1834            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1835        }
1836        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1837            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1838        }
1839        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1840            true => {
1841                // change error to None
1842                array
1843                    .as_primitive::<TimestampSecondType>()
1844                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1845            }
1846            false => array
1847                .as_primitive::<TimestampSecondType>()
1848                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1849        })),
1850        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1851            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1852        }
1853        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1854            array
1855                .as_primitive::<TimestampMicrosecondType>()
1856                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1857        )),
1858        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1859            array
1860                .as_primitive::<TimestampNanosecondType>()
1861                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1862        )),
1863        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1864            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1865            Ok(Arc::new(
1866                array
1867                    .as_primitive::<TimestampSecondType>()
1868                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1869                        Ok(time_to_time64us(as_time_res_with_timezone::<
1870                            TimestampSecondType,
1871                        >(x, tz)?))
1872                    })?,
1873            ))
1874        }
1875        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1876            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1877            Ok(Arc::new(
1878                array
1879                    .as_primitive::<TimestampSecondType>()
1880                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1881                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1882                            TimestampSecondType,
1883                        >(x, tz)?))
1884                    })?,
1885            ))
1886        }
1887        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1888            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1889            Ok(Arc::new(
1890                array
1891                    .as_primitive::<TimestampMillisecondType>()
1892                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1893                        Ok(time_to_time64us(as_time_res_with_timezone::<
1894                            TimestampMillisecondType,
1895                        >(x, tz)?))
1896                    })?,
1897            ))
1898        }
1899        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1900            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1901            Ok(Arc::new(
1902                array
1903                    .as_primitive::<TimestampMillisecondType>()
1904                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1905                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1906                            TimestampMillisecondType,
1907                        >(x, tz)?))
1908                    })?,
1909            ))
1910        }
1911        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1912            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1913            Ok(Arc::new(
1914                array
1915                    .as_primitive::<TimestampMicrosecondType>()
1916                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1917                        Ok(time_to_time64us(as_time_res_with_timezone::<
1918                            TimestampMicrosecondType,
1919                        >(x, tz)?))
1920                    })?,
1921            ))
1922        }
1923        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1924            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1925            Ok(Arc::new(
1926                array
1927                    .as_primitive::<TimestampMicrosecondType>()
1928                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1929                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1930                            TimestampMicrosecondType,
1931                        >(x, tz)?))
1932                    })?,
1933            ))
1934        }
1935        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1936            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1937            Ok(Arc::new(
1938                array
1939                    .as_primitive::<TimestampNanosecondType>()
1940                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1941                        Ok(time_to_time64us(as_time_res_with_timezone::<
1942                            TimestampNanosecondType,
1943                        >(x, tz)?))
1944                    })?,
1945            ))
1946        }
1947        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1948            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1949            Ok(Arc::new(
1950                array
1951                    .as_primitive::<TimestampNanosecondType>()
1952                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1953                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1954                            TimestampNanosecondType,
1955                        >(x, tz)?))
1956                    })?,
1957            ))
1958        }
1959        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1960            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1961            Ok(Arc::new(
1962                array
1963                    .as_primitive::<TimestampSecondType>()
1964                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1965                        Ok(time_to_time32s(as_time_res_with_timezone::<
1966                            TimestampSecondType,
1967                        >(x, tz)?))
1968                    })?,
1969            ))
1970        }
1971        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1972            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1973            Ok(Arc::new(
1974                array
1975                    .as_primitive::<TimestampSecondType>()
1976                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1977                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1978                            TimestampSecondType,
1979                        >(x, tz)?))
1980                    })?,
1981            ))
1982        }
1983        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1984            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1985            Ok(Arc::new(
1986                array
1987                    .as_primitive::<TimestampMillisecondType>()
1988                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1989                        Ok(time_to_time32s(as_time_res_with_timezone::<
1990                            TimestampMillisecondType,
1991                        >(x, tz)?))
1992                    })?,
1993            ))
1994        }
1995        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
1996            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1997            Ok(Arc::new(
1998                array
1999                    .as_primitive::<TimestampMillisecondType>()
2000                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2001                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2002                            TimestampMillisecondType,
2003                        >(x, tz)?))
2004                    })?,
2005            ))
2006        }
2007        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2008            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2009            Ok(Arc::new(
2010                array
2011                    .as_primitive::<TimestampMicrosecondType>()
2012                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2013                        Ok(time_to_time32s(as_time_res_with_timezone::<
2014                            TimestampMicrosecondType,
2015                        >(x, tz)?))
2016                    })?,
2017            ))
2018        }
2019        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2020            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2021            Ok(Arc::new(
2022                array
2023                    .as_primitive::<TimestampMicrosecondType>()
2024                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2025                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2026                            TimestampMicrosecondType,
2027                        >(x, tz)?))
2028                    })?,
2029            ))
2030        }
2031        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2032            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2033            Ok(Arc::new(
2034                array
2035                    .as_primitive::<TimestampNanosecondType>()
2036                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2037                        Ok(time_to_time32s(as_time_res_with_timezone::<
2038                            TimestampNanosecondType,
2039                        >(x, tz)?))
2040                    })?,
2041            ))
2042        }
2043        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2044            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2045            Ok(Arc::new(
2046                array
2047                    .as_primitive::<TimestampNanosecondType>()
2048                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2049                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2050                            TimestampNanosecondType,
2051                        >(x, tz)?))
2052                    })?,
2053            ))
2054        }
2055        (Date64, Timestamp(TimeUnit::Second, _)) => {
2056            let array = array
2057                .as_primitive::<Date64Type>()
2058                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2059
2060            cast_with_options(&array, to_type, cast_options)
2061        }
2062        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2063            let array = array
2064                .as_primitive::<Date64Type>()
2065                .reinterpret_cast::<TimestampMillisecondType>();
2066
2067            cast_with_options(&array, to_type, cast_options)
2068        }
2069
2070        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2071            let array = array
2072                .as_primitive::<Date64Type>()
2073                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2074
2075            cast_with_options(&array, to_type, cast_options)
2076        }
2077        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2078            let array = array
2079                .as_primitive::<Date64Type>()
2080                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2081
2082            cast_with_options(&array, to_type, cast_options)
2083        }
2084        (Date32, Timestamp(TimeUnit::Second, _)) => {
2085            let array = array
2086                .as_primitive::<Date32Type>()
2087                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2088
2089            cast_with_options(&array, to_type, cast_options)
2090        }
2091        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2092            let array = array
2093                .as_primitive::<Date32Type>()
2094                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2095
2096            cast_with_options(&array, to_type, cast_options)
2097        }
2098        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2099            let array = array
2100                .as_primitive::<Date32Type>()
2101                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2102
2103            cast_with_options(&array, to_type, cast_options)
2104        }
2105        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2106            let array = array
2107                .as_primitive::<Date32Type>()
2108                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2109
2110            cast_with_options(&array, to_type, cast_options)
2111        }
2112
2113        (_, Duration(unit)) if from_type.is_numeric() => {
2114            let array = cast_with_options(array, &Int64, cast_options)?;
2115            Ok(make_duration_array(array.as_primitive(), *unit))
2116        }
2117        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2118            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2119            cast_with_options(&array, to_type, cast_options)
2120        }
2121        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2122            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2123            cast_with_options(&array, to_type, cast_options)
2124        }
2125        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2126            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2127            cast_with_options(&array, to_type, cast_options)
2128        }
2129        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2130            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2131            cast_with_options(&array, to_type, cast_options)
2132        }
2133
2134        (Duration(from_unit), Duration(to_unit)) => {
2135            let array = cast_with_options(array, &Int64, cast_options)?;
2136            let time_array = array.as_primitive::<Int64Type>();
2137            let from_size = time_unit_multiple(from_unit);
2138            let to_size = time_unit_multiple(to_unit);
2139            // we either divide or multiply, depending on size of each unit
2140            // units are never the same when the types are the same
2141            let converted = match from_size.cmp(&to_size) {
2142                Ordering::Greater => {
2143                    let divisor = from_size / to_size;
2144                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2145                }
2146                Ordering::Equal => time_array.clone(),
2147                Ordering::Less => {
2148                    let mul = to_size / from_size;
2149                    if cast_options.safe {
2150                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2151                    } else {
2152                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2153                    }
2154                }
2155            };
2156            Ok(make_duration_array(&converted, *to_unit))
2157        }
2158
2159        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2160            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2161        }
2162        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2163            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2164        }
2165        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2166            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2167        }
2168        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2169            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2170        }
2171        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2172            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2173        }
2174        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2175            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2176        }
2177        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2178            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2179        }
2180        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2181            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2182        }
2183        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2184            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2185        }
2186        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2187            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2188        }
2189        (Int32, Interval(IntervalUnit::YearMonth)) => {
2190            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2191        }
2192        (_, _) => Err(ArrowError::CastError(format!(
2193            "Casting from {from_type} to {to_type} not supported",
2194        ))),
2195    }
2196}
2197
2198fn cast_struct_to_struct(
2199    array: &StructArray,
2200    from_fields: Fields,
2201    to_fields: Fields,
2202    cast_options: &CastOptions,
2203) -> Result<ArrayRef, ArrowError> {
2204    // Fast path: if field names are in the same order, we can just zip and cast
2205    let fields_match_order = from_fields.len() == to_fields.len()
2206        && from_fields
2207            .iter()
2208            .zip(to_fields.iter())
2209            .all(|(f1, f2)| f1.name() == f2.name());
2210
2211    let fields = if fields_match_order {
2212        // Fast path: cast columns in order if their names match
2213        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2214    } else {
2215        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2216            from_fields
2217                .iter()
2218                .any(|from_field| from_field.name() == to_field.name())
2219        });
2220
2221        if all_fields_match_by_name {
2222            // Slow path: match fields by name and reorder
2223            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2224        } else {
2225            // Fallback: cast field by field in order
2226            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2227        }
2228    };
2229
2230    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2231    Ok(Arc::new(array) as ArrayRef)
2232}
2233
2234fn cast_struct_fields_by_name(
2235    array: &StructArray,
2236    from_fields: Fields,
2237    to_fields: Fields,
2238    cast_options: &CastOptions,
2239) -> Result<Vec<ArrayRef>, ArrowError> {
2240    to_fields
2241        .iter()
2242        .map(|to_field| {
2243            let from_field_idx = from_fields
2244                .iter()
2245                .position(|from_field| from_field.name() == to_field.name())
2246                .unwrap(); // safe because we checked above
2247            let column = array.column(from_field_idx);
2248            cast_with_options(column, to_field.data_type(), cast_options)
2249        })
2250        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2251}
2252
2253fn cast_struct_fields_in_order(
2254    array: &StructArray,
2255    to_fields: Fields,
2256    cast_options: &CastOptions,
2257) -> Result<Vec<ArrayRef>, ArrowError> {
2258    array
2259        .columns()
2260        .iter()
2261        .zip(to_fields.iter())
2262        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2263        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2264}
2265
2266fn cast_from_decimal<D, F>(
2267    array: &dyn Array,
2268    base: D::Native,
2269    scale: &i8,
2270    from_type: &DataType,
2271    to_type: &DataType,
2272    as_float: F,
2273    cast_options: &CastOptions,
2274) -> Result<ArrayRef, ArrowError>
2275where
2276    D: DecimalType + ArrowPrimitiveType,
2277    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2278    F: Fn(D::Native) -> f64,
2279{
2280    use DataType::*;
2281    // cast decimal to other type
2282    match to_type {
2283        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2284        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2285        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2286        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2287        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2288        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2289        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2290        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2291        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2292            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2293        }),
2294        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2295            as_float(x) / 10_f64.powi(*scale as i32)
2296        }),
2297        Utf8View => value_to_string_view(array, cast_options),
2298        Utf8 => value_to_string::<i32>(array, cast_options),
2299        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2300        Null => Ok(new_null_array(to_type, array.len())),
2301        _ => Err(ArrowError::CastError(format!(
2302            "Casting from {from_type} to {to_type} not supported"
2303        ))),
2304    }
2305}
2306
2307fn cast_to_decimal<D, M>(
2308    array: &dyn Array,
2309    base: M,
2310    precision: &u8,
2311    scale: &i8,
2312    from_type: &DataType,
2313    to_type: &DataType,
2314    cast_options: &CastOptions,
2315) -> Result<ArrayRef, ArrowError>
2316where
2317    D: DecimalType + ArrowPrimitiveType<Native = M>,
2318    M: ArrowNativeTypeOp + DecimalCast,
2319    u8: num_traits::AsPrimitive<M>,
2320    u16: num_traits::AsPrimitive<M>,
2321    u32: num_traits::AsPrimitive<M>,
2322    u64: num_traits::AsPrimitive<M>,
2323    i8: num_traits::AsPrimitive<M>,
2324    i16: num_traits::AsPrimitive<M>,
2325    i32: num_traits::AsPrimitive<M>,
2326    i64: num_traits::AsPrimitive<M>,
2327{
2328    use DataType::*;
2329    // cast data to decimal
2330    match from_type {
2331        UInt8 => cast_integer_to_decimal::<_, D, M>(
2332            array.as_primitive::<UInt8Type>(),
2333            *precision,
2334            *scale,
2335            base,
2336            cast_options,
2337        ),
2338        UInt16 => cast_integer_to_decimal::<_, D, _>(
2339            array.as_primitive::<UInt16Type>(),
2340            *precision,
2341            *scale,
2342            base,
2343            cast_options,
2344        ),
2345        UInt32 => cast_integer_to_decimal::<_, D, _>(
2346            array.as_primitive::<UInt32Type>(),
2347            *precision,
2348            *scale,
2349            base,
2350            cast_options,
2351        ),
2352        UInt64 => cast_integer_to_decimal::<_, D, _>(
2353            array.as_primitive::<UInt64Type>(),
2354            *precision,
2355            *scale,
2356            base,
2357            cast_options,
2358        ),
2359        Int8 => cast_integer_to_decimal::<_, D, _>(
2360            array.as_primitive::<Int8Type>(),
2361            *precision,
2362            *scale,
2363            base,
2364            cast_options,
2365        ),
2366        Int16 => cast_integer_to_decimal::<_, D, _>(
2367            array.as_primitive::<Int16Type>(),
2368            *precision,
2369            *scale,
2370            base,
2371            cast_options,
2372        ),
2373        Int32 => cast_integer_to_decimal::<_, D, _>(
2374            array.as_primitive::<Int32Type>(),
2375            *precision,
2376            *scale,
2377            base,
2378            cast_options,
2379        ),
2380        Int64 => cast_integer_to_decimal::<_, D, _>(
2381            array.as_primitive::<Int64Type>(),
2382            *precision,
2383            *scale,
2384            base,
2385            cast_options,
2386        ),
2387        Float32 => cast_floating_point_to_decimal::<_, D>(
2388            array.as_primitive::<Float32Type>(),
2389            *precision,
2390            *scale,
2391            cast_options,
2392        ),
2393        Float64 => cast_floating_point_to_decimal::<_, D>(
2394            array.as_primitive::<Float64Type>(),
2395            *precision,
2396            *scale,
2397            cast_options,
2398        ),
2399        Utf8View | Utf8 => {
2400            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2401        }
2402        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2403        Null => Ok(new_null_array(to_type, array.len())),
2404        _ => Err(ArrowError::CastError(format!(
2405            "Casting from {from_type} to {to_type} not supported"
2406        ))),
2407    }
2408}
2409
2410/// Get the time unit as a multiple of a second
2411const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2412    match unit {
2413        TimeUnit::Second => 1,
2414        TimeUnit::Millisecond => MILLISECONDS,
2415        TimeUnit::Microsecond => MICROSECONDS,
2416        TimeUnit::Nanosecond => NANOSECONDS,
2417    }
2418}
2419
2420/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2421fn cast_numeric_arrays<FROM, TO>(
2422    from: &dyn Array,
2423    cast_options: &CastOptions,
2424) -> Result<ArrayRef, ArrowError>
2425where
2426    FROM: ArrowPrimitiveType,
2427    TO: ArrowPrimitiveType,
2428    FROM::Native: NumCast,
2429    TO::Native: NumCast,
2430{
2431    if cast_options.safe {
2432        // If the value can't be casted to the `TO::Native`, return null
2433        Ok(Arc::new(numeric_cast::<FROM, TO>(
2434            from.as_primitive::<FROM>(),
2435        )))
2436    } else {
2437        // If the value can't be casted to the `TO::Native`, return error
2438        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2439            from.as_primitive::<FROM>(),
2440        )?))
2441    }
2442}
2443
2444// Natural cast between numeric types
2445// If the value of T can't be casted to R, will throw error
2446fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2447where
2448    T: ArrowPrimitiveType,
2449    R: ArrowPrimitiveType,
2450    T::Native: NumCast,
2451    R::Native: NumCast,
2452{
2453    from.try_unary(|value| {
2454        num_traits::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2455            ArrowError::CastError(format!(
2456                "Can't cast value {:?} to type {}",
2457                value,
2458                R::DATA_TYPE
2459            ))
2460        })
2461    })
2462}
2463
2464// Natural cast between numeric types
2465// If the value of T can't be casted to R, it will be converted to null
2466fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2467where
2468    T: ArrowPrimitiveType,
2469    R: ArrowPrimitiveType,
2470    T::Native: NumCast,
2471    R::Native: NumCast,
2472{
2473    from.unary_opt::<_, R>(num_traits::cast::cast::<T::Native, R::Native>)
2474}
2475
2476fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2477    array: &dyn Array,
2478) -> Result<ArrayRef, ArrowError> {
2479    let array = array.as_primitive::<FROM>();
2480    let size = std::mem::size_of::<FROM::Native>();
2481    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2482    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2483        offsets,
2484        array.values().inner().clone(),
2485        array.nulls().cloned(),
2486    )?))
2487}
2488
2489fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2490    array: PrimitiveArray<Int64Type>,
2491    to_tz: &Tz,
2492    cast_options: &CastOptions,
2493) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2494    let adjust = |o| {
2495        let local = as_datetime::<T>(o)?;
2496        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2497        T::make_value(local - offset.fix())
2498    };
2499    let adjusted = if cast_options.safe {
2500        array.unary_opt::<_, Int64Type>(adjust)
2501    } else {
2502        array.try_unary::<_, Int64Type, _>(|o| {
2503            adjust(o).ok_or_else(|| {
2504                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2505            })
2506        })?
2507    };
2508    Ok(adjusted)
2509}
2510
2511/// Cast numeric types to Boolean
2512///
2513/// Any zero value returns `false` while non-zero returns `true`
2514fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2515where
2516    FROM: ArrowPrimitiveType,
2517{
2518    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2519}
2520
2521fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2522where
2523    T: ArrowPrimitiveType + ArrowPrimitiveType,
2524{
2525    let mut b = BooleanBuilder::with_capacity(from.len());
2526
2527    for i in 0..from.len() {
2528        if from.is_null(i) {
2529            b.append_null();
2530        } else if from.value(i) != T::default_value() {
2531            b.append_value(true);
2532        } else {
2533            b.append_value(false);
2534        }
2535    }
2536
2537    Ok(b.finish())
2538}
2539
2540/// Cast Boolean types to numeric
2541///
2542/// `false` returns 0 while `true` returns 1
2543fn cast_bool_to_numeric<TO>(
2544    from: &dyn Array,
2545    cast_options: &CastOptions,
2546) -> Result<ArrayRef, ArrowError>
2547where
2548    TO: ArrowPrimitiveType,
2549    TO::Native: num_traits::cast::NumCast,
2550{
2551    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2552        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2553        cast_options,
2554    )))
2555}
2556
2557fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2558where
2559    T: ArrowPrimitiveType,
2560    T::Native: num_traits::NumCast,
2561{
2562    let iter = (0..from.len()).map(|i| {
2563        if from.is_null(i) {
2564            None
2565        } else if from.value(i) {
2566            // a workaround to cast a primitive to T::Native, infallible
2567            num_traits::cast::cast(1)
2568        } else {
2569            Some(T::default_value())
2570        }
2571    });
2572    // Benefit:
2573    //     20% performance improvement
2574    // Soundness:
2575    //     The iterator is trustedLen because it comes from a Range
2576    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2577}
2578
2579/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2580fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2581    array: &dyn Array,
2582    byte_width: i32,
2583    cast_options: &CastOptions,
2584) -> Result<ArrayRef, ArrowError> {
2585    let array = array.as_binary::<O>();
2586    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2587
2588    for i in 0..array.len() {
2589        if array.is_null(i) {
2590            builder.append_null();
2591        } else {
2592            match builder.append_value(array.value(i)) {
2593                Ok(_) => {}
2594                Err(e) => match cast_options.safe {
2595                    true => builder.append_null(),
2596                    false => return Err(e),
2597                },
2598            }
2599        }
2600    }
2601
2602    Ok(Arc::new(builder.finish()))
2603}
2604
2605/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2606/// If the target one is too large for the source array it will return an Error.
2607fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2608    array: &dyn Array,
2609    byte_width: i32,
2610) -> Result<ArrayRef, ArrowError> {
2611    let array = array
2612        .as_any()
2613        .downcast_ref::<FixedSizeBinaryArray>()
2614        .unwrap();
2615
2616    let offsets: i128 = byte_width as i128 * array.len() as i128;
2617
2618    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2619    if is_binary && offsets > i32::MAX as i128 {
2620        return Err(ArrowError::ComputeError(
2621            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2622        ));
2623    } else if !is_binary && offsets > i64::MAX as i128 {
2624        return Err(ArrowError::ComputeError(
2625            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2626        ));
2627    }
2628
2629    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2630
2631    for i in 0..array.len() {
2632        if array.is_null(i) {
2633            builder.append_null();
2634        } else {
2635            builder.append_value(array.value(i));
2636        }
2637    }
2638
2639    Ok(Arc::new(builder.finish()))
2640}
2641
2642fn cast_fixed_size_binary_to_binary_view(
2643    array: &dyn Array,
2644    _byte_width: i32,
2645) -> Result<ArrayRef, ArrowError> {
2646    let array = array
2647        .as_any()
2648        .downcast_ref::<FixedSizeBinaryArray>()
2649        .unwrap();
2650
2651    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2652    for i in 0..array.len() {
2653        if array.is_null(i) {
2654            builder.append_null();
2655        } else {
2656            builder.append_value(array.value(i));
2657        }
2658    }
2659
2660    Ok(Arc::new(builder.finish()))
2661}
2662
2663/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2664/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2665fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2666where
2667    FROM: ByteArrayType,
2668    TO: ByteArrayType<Native = FROM::Native>,
2669    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2670    TO::Offset: OffsetSizeTrait + NumCast,
2671{
2672    let data = array.to_data();
2673    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2674    let str_values_buf = data.buffers()[1].clone();
2675    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2676
2677    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2678    offsets
2679        .iter()
2680        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2681            let offset =
2682                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2683                    ArrowError::ComputeError(format!(
2684                        "{}{} array too large to cast to {}{} array",
2685                        FROM::Offset::PREFIX,
2686                        FROM::PREFIX,
2687                        TO::Offset::PREFIX,
2688                        TO::PREFIX
2689                    ))
2690                })?;
2691            offset_builder.append(offset);
2692            Ok(())
2693        })?;
2694
2695    let offset_buffer = offset_builder.finish();
2696
2697    let dtype = TO::DATA_TYPE;
2698
2699    let builder = ArrayData::builder(dtype)
2700        .offset(array.offset())
2701        .len(array.len())
2702        .add_buffer(offset_buffer)
2703        .add_buffer(str_values_buf)
2704        .nulls(data.nulls().cloned());
2705
2706    let array_data = unsafe { builder.build_unchecked() };
2707
2708    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2709}
2710
2711/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2712fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2713where
2714    FROM: ByteViewType,
2715    TO: ByteArrayType,
2716    FROM::Native: AsRef<TO::Native>,
2717{
2718    let data = array.to_data();
2719    let view_array = GenericByteViewArray::<FROM>::from(data);
2720
2721    let len = view_array.len();
2722    let bytes = view_array
2723        .views()
2724        .iter()
2725        .map(|v| ByteView::from(*v).length as usize)
2726        .sum::<usize>();
2727
2728    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2729
2730    for val in view_array.iter() {
2731        byte_array_builder.append_option(val);
2732    }
2733
2734    Ok(Arc::new(byte_array_builder.finish()))
2735}
2736
2737#[cfg(test)]
2738mod tests {
2739    use super::*;
2740    use DataType::*;
2741    use arrow_array::{Int64Array, RunArray, StringArray};
2742    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2743    use arrow_buffer::{ScalarBuffer, i256};
2744    use arrow_schema::{DataType, Field};
2745    use chrono::NaiveDate;
2746    use half::f16;
2747    use std::sync::Arc;
2748
2749    #[derive(Clone)]
2750    struct DecimalCastTestConfig {
2751        input_prec: u8,
2752        input_scale: i8,
2753        input_repr: i128,
2754        output_prec: u8,
2755        output_scale: i8,
2756        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2757                                                    // template where the "{}" will be
2758                                                    // replaced with the decimal type name
2759                                                    // (e.g. Decimal128)
2760    }
2761
2762    macro_rules! generate_cast_test_case {
2763        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2764            let output =
2765                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2766
2767            // assert cast type
2768            let input_array_type = $INPUT_ARRAY.data_type();
2769            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2770            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2771            assert_eq!($OUTPUT_TYPE, result.data_type());
2772            assert_eq!(result.as_ref(), &output);
2773
2774            let cast_option = CastOptions {
2775                safe: false,
2776                format_options: FormatOptions::default(),
2777            };
2778            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2779            assert_eq!($OUTPUT_TYPE, result.data_type());
2780            assert_eq!(result.as_ref(), &output);
2781        };
2782    }
2783
2784    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2785    where
2786        I: DecimalType,
2787        O: DecimalType,
2788        I::Native: DecimalCast,
2789        O::Native: DecimalCast,
2790    {
2791        let array = vec![I::Native::from_decimal(t.input_repr)];
2792        let array = array
2793            .into_iter()
2794            .collect::<PrimitiveArray<I>>()
2795            .with_precision_and_scale(t.input_prec, t.input_scale)
2796            .unwrap();
2797        let input_type = array.data_type();
2798        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2799        assert!(can_cast_types(input_type, &output_type));
2800
2801        let options = CastOptions {
2802            safe: false,
2803            ..Default::default()
2804        };
2805        let result = cast_with_options(&array, &output_type, &options);
2806
2807        match t.expected_output_repr {
2808            Ok(v) => {
2809                let expected_array = vec![O::Native::from_decimal(v)];
2810                let expected_array = expected_array
2811                    .into_iter()
2812                    .collect::<PrimitiveArray<O>>()
2813                    .with_precision_and_scale(t.output_prec, t.output_scale)
2814                    .unwrap();
2815                assert_eq!(*result.unwrap(), expected_array);
2816            }
2817            Err(expected_output_message_template) => {
2818                assert!(result.is_err());
2819                let expected_error_message =
2820                    expected_output_message_template.replace("{}", O::PREFIX);
2821                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2822            }
2823        }
2824    }
2825
2826    fn create_decimal32_array(
2827        array: Vec<Option<i32>>,
2828        precision: u8,
2829        scale: i8,
2830    ) -> Result<Decimal32Array, ArrowError> {
2831        array
2832            .into_iter()
2833            .collect::<Decimal32Array>()
2834            .with_precision_and_scale(precision, scale)
2835    }
2836
2837    fn create_decimal64_array(
2838        array: Vec<Option<i64>>,
2839        precision: u8,
2840        scale: i8,
2841    ) -> Result<Decimal64Array, ArrowError> {
2842        array
2843            .into_iter()
2844            .collect::<Decimal64Array>()
2845            .with_precision_and_scale(precision, scale)
2846    }
2847
2848    fn create_decimal128_array(
2849        array: Vec<Option<i128>>,
2850        precision: u8,
2851        scale: i8,
2852    ) -> Result<Decimal128Array, ArrowError> {
2853        array
2854            .into_iter()
2855            .collect::<Decimal128Array>()
2856            .with_precision_and_scale(precision, scale)
2857    }
2858
2859    fn create_decimal256_array(
2860        array: Vec<Option<i256>>,
2861        precision: u8,
2862        scale: i8,
2863    ) -> Result<Decimal256Array, ArrowError> {
2864        array
2865            .into_iter()
2866            .collect::<Decimal256Array>()
2867            .with_precision_and_scale(precision, scale)
2868    }
2869
2870    #[test]
2871    #[cfg(not(feature = "force_validate"))]
2872    #[should_panic(
2873        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2874    )]
2875    fn test_cast_decimal_to_decimal_round_with_error() {
2876        // decimal256 to decimal128 overflow
2877        let array = vec![
2878            Some(i256::from_i128(1123454)),
2879            Some(i256::from_i128(2123456)),
2880            Some(i256::from_i128(-3123453)),
2881            Some(i256::from_i128(-3123456)),
2882            None,
2883            Some(i256::MAX),
2884            Some(i256::MIN),
2885        ];
2886        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2887        let array = Arc::new(input_decimal_array) as ArrayRef;
2888        let input_type = DataType::Decimal256(76, 4);
2889        let output_type = DataType::Decimal128(20, 3);
2890        assert!(can_cast_types(&input_type, &output_type));
2891        generate_cast_test_case!(
2892            &array,
2893            Decimal128Array,
2894            &output_type,
2895            vec![
2896                Some(112345_i128),
2897                Some(212346_i128),
2898                Some(-312345_i128),
2899                Some(-312346_i128),
2900                None,
2901                None,
2902                None,
2903            ]
2904        );
2905    }
2906
2907    #[test]
2908    #[cfg(not(feature = "force_validate"))]
2909    fn test_cast_decimal_to_decimal_round() {
2910        let array = vec![
2911            Some(1123454),
2912            Some(2123456),
2913            Some(-3123453),
2914            Some(-3123456),
2915            None,
2916        ];
2917        let array = create_decimal128_array(array, 20, 4).unwrap();
2918        // decimal128 to decimal128
2919        let input_type = DataType::Decimal128(20, 4);
2920        let output_type = DataType::Decimal128(20, 3);
2921        assert!(can_cast_types(&input_type, &output_type));
2922        generate_cast_test_case!(
2923            &array,
2924            Decimal128Array,
2925            &output_type,
2926            vec![
2927                Some(112345_i128),
2928                Some(212346_i128),
2929                Some(-312345_i128),
2930                Some(-312346_i128),
2931                None
2932            ]
2933        );
2934
2935        // decimal128 to decimal256
2936        let input_type = DataType::Decimal128(20, 4);
2937        let output_type = DataType::Decimal256(20, 3);
2938        assert!(can_cast_types(&input_type, &output_type));
2939        generate_cast_test_case!(
2940            &array,
2941            Decimal256Array,
2942            &output_type,
2943            vec![
2944                Some(i256::from_i128(112345_i128)),
2945                Some(i256::from_i128(212346_i128)),
2946                Some(i256::from_i128(-312345_i128)),
2947                Some(i256::from_i128(-312346_i128)),
2948                None
2949            ]
2950        );
2951
2952        // decimal256
2953        let array = vec![
2954            Some(i256::from_i128(1123454)),
2955            Some(i256::from_i128(2123456)),
2956            Some(i256::from_i128(-3123453)),
2957            Some(i256::from_i128(-3123456)),
2958            None,
2959        ];
2960        let array = create_decimal256_array(array, 20, 4).unwrap();
2961
2962        // decimal256 to decimal256
2963        let input_type = DataType::Decimal256(20, 4);
2964        let output_type = DataType::Decimal256(20, 3);
2965        assert!(can_cast_types(&input_type, &output_type));
2966        generate_cast_test_case!(
2967            &array,
2968            Decimal256Array,
2969            &output_type,
2970            vec![
2971                Some(i256::from_i128(112345_i128)),
2972                Some(i256::from_i128(212346_i128)),
2973                Some(i256::from_i128(-312345_i128)),
2974                Some(i256::from_i128(-312346_i128)),
2975                None
2976            ]
2977        );
2978        // decimal256 to decimal128
2979        let input_type = DataType::Decimal256(20, 4);
2980        let output_type = DataType::Decimal128(20, 3);
2981        assert!(can_cast_types(&input_type, &output_type));
2982        generate_cast_test_case!(
2983            &array,
2984            Decimal128Array,
2985            &output_type,
2986            vec![
2987                Some(112345_i128),
2988                Some(212346_i128),
2989                Some(-312345_i128),
2990                Some(-312346_i128),
2991                None
2992            ]
2993        );
2994    }
2995
2996    #[test]
2997    fn test_cast_decimal32_to_decimal32() {
2998        // test changing precision
2999        let input_type = DataType::Decimal32(9, 3);
3000        let output_type = DataType::Decimal32(9, 4);
3001        assert!(can_cast_types(&input_type, &output_type));
3002        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3003        let array = create_decimal32_array(array, 9, 3).unwrap();
3004        generate_cast_test_case!(
3005            &array,
3006            Decimal32Array,
3007            &output_type,
3008            vec![
3009                Some(11234560_i32),
3010                Some(21234560_i32),
3011                Some(31234560_i32),
3012                None
3013            ]
3014        );
3015        // negative test
3016        let array = vec![Some(123456), None];
3017        let array = create_decimal32_array(array, 9, 0).unwrap();
3018        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3019        assert!(result_safe.is_ok());
3020        let options = CastOptions {
3021            safe: false,
3022            ..Default::default()
3023        };
3024
3025        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3026        assert_eq!(
3027            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3028            result_unsafe.unwrap_err().to_string()
3029        );
3030    }
3031
3032    #[test]
3033    fn test_cast_decimal64_to_decimal64() {
3034        // test changing precision
3035        let input_type = DataType::Decimal64(17, 3);
3036        let output_type = DataType::Decimal64(17, 4);
3037        assert!(can_cast_types(&input_type, &output_type));
3038        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3039        let array = create_decimal64_array(array, 17, 3).unwrap();
3040        generate_cast_test_case!(
3041            &array,
3042            Decimal64Array,
3043            &output_type,
3044            vec![
3045                Some(11234560_i64),
3046                Some(21234560_i64),
3047                Some(31234560_i64),
3048                None
3049            ]
3050        );
3051        // negative test
3052        let array = vec![Some(123456), None];
3053        let array = create_decimal64_array(array, 9, 0).unwrap();
3054        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3055        assert!(result_safe.is_ok());
3056        let options = CastOptions {
3057            safe: false,
3058            ..Default::default()
3059        };
3060
3061        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3062        assert_eq!(
3063            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3064            result_unsafe.unwrap_err().to_string()
3065        );
3066    }
3067
3068    #[test]
3069    fn test_cast_decimal128_to_decimal128() {
3070        // test changing precision
3071        let input_type = DataType::Decimal128(20, 3);
3072        let output_type = DataType::Decimal128(20, 4);
3073        assert!(can_cast_types(&input_type, &output_type));
3074        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3075        let array = create_decimal128_array(array, 20, 3).unwrap();
3076        generate_cast_test_case!(
3077            &array,
3078            Decimal128Array,
3079            &output_type,
3080            vec![
3081                Some(11234560_i128),
3082                Some(21234560_i128),
3083                Some(31234560_i128),
3084                None
3085            ]
3086        );
3087        // negative test
3088        let array = vec![Some(123456), None];
3089        let array = create_decimal128_array(array, 10, 0).unwrap();
3090        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3091        assert!(result_safe.is_ok());
3092        let options = CastOptions {
3093            safe: false,
3094            ..Default::default()
3095        };
3096
3097        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3098        assert_eq!(
3099            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3100            result_unsafe.unwrap_err().to_string()
3101        );
3102    }
3103
3104    #[test]
3105    fn test_cast_decimal32_to_decimal32_dict() {
3106        let p = 9;
3107        let s = 3;
3108        let input_type = DataType::Decimal32(p, s);
3109        let output_type = DataType::Dictionary(
3110            Box::new(DataType::Int32),
3111            Box::new(DataType::Decimal32(p, s)),
3112        );
3113        assert!(can_cast_types(&input_type, &output_type));
3114        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3115        let array = create_decimal32_array(array, p, s).unwrap();
3116        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3117        assert_eq!(cast_array.data_type(), &output_type);
3118    }
3119
3120    #[test]
3121    fn test_cast_decimal64_to_decimal64_dict() {
3122        let p = 15;
3123        let s = 3;
3124        let input_type = DataType::Decimal64(p, s);
3125        let output_type = DataType::Dictionary(
3126            Box::new(DataType::Int32),
3127            Box::new(DataType::Decimal64(p, s)),
3128        );
3129        assert!(can_cast_types(&input_type, &output_type));
3130        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3131        let array = create_decimal64_array(array, p, s).unwrap();
3132        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3133        assert_eq!(cast_array.data_type(), &output_type);
3134    }
3135
3136    #[test]
3137    fn test_cast_decimal128_to_decimal128_dict() {
3138        let p = 20;
3139        let s = 3;
3140        let input_type = DataType::Decimal128(p, s);
3141        let output_type = DataType::Dictionary(
3142            Box::new(DataType::Int32),
3143            Box::new(DataType::Decimal128(p, s)),
3144        );
3145        assert!(can_cast_types(&input_type, &output_type));
3146        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3147        let array = create_decimal128_array(array, p, s).unwrap();
3148        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3149        assert_eq!(cast_array.data_type(), &output_type);
3150    }
3151
3152    #[test]
3153    fn test_cast_decimal256_to_decimal256_dict() {
3154        let p = 20;
3155        let s = 3;
3156        let input_type = DataType::Decimal256(p, s);
3157        let output_type = DataType::Dictionary(
3158            Box::new(DataType::Int32),
3159            Box::new(DataType::Decimal256(p, s)),
3160        );
3161        assert!(can_cast_types(&input_type, &output_type));
3162        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3163        let array = create_decimal128_array(array, p, s).unwrap();
3164        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3165        assert_eq!(cast_array.data_type(), &output_type);
3166    }
3167
3168    #[test]
3169    fn test_cast_decimal32_to_decimal32_overflow() {
3170        let input_type = DataType::Decimal32(9, 3);
3171        let output_type = DataType::Decimal32(9, 9);
3172        assert!(can_cast_types(&input_type, &output_type));
3173
3174        let array = vec![Some(i32::MAX)];
3175        let array = create_decimal32_array(array, 9, 3).unwrap();
3176        let result = cast_with_options(
3177            &array,
3178            &output_type,
3179            &CastOptions {
3180                safe: false,
3181                format_options: FormatOptions::default(),
3182            },
3183        );
3184        assert_eq!(
3185            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3186            result.unwrap_err().to_string()
3187        );
3188    }
3189
3190    #[test]
3191    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3192        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3193        let array = create_decimal32_array(array, 9, 3).unwrap();
3194
3195        // Divide out all digits of precision -- rounding could still produce +/- 1
3196        let output_type = DataType::Decimal32(9, -6);
3197        assert!(can_cast_types(array.data_type(), &output_type));
3198        generate_cast_test_case!(
3199            &array,
3200            Decimal32Array,
3201            &output_type,
3202            vec![Some(-1), Some(0), Some(1), None]
3203        );
3204
3205        // Divide out more digits than we have precision -- all-zero result
3206        let output_type = DataType::Decimal32(9, -7);
3207        assert!(can_cast_types(array.data_type(), &output_type));
3208        generate_cast_test_case!(
3209            &array,
3210            Decimal32Array,
3211            &output_type,
3212            vec![Some(0), Some(0), Some(0), None]
3213        );
3214    }
3215
3216    #[test]
3217    fn test_cast_decimal64_to_decimal64_overflow() {
3218        let input_type = DataType::Decimal64(18, 3);
3219        let output_type = DataType::Decimal64(18, 18);
3220        assert!(can_cast_types(&input_type, &output_type));
3221
3222        let array = vec![Some(i64::MAX)];
3223        let array = create_decimal64_array(array, 18, 3).unwrap();
3224        let result = cast_with_options(
3225            &array,
3226            &output_type,
3227            &CastOptions {
3228                safe: false,
3229                format_options: FormatOptions::default(),
3230            },
3231        );
3232        assert_eq!(
3233            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3234            result.unwrap_err().to_string()
3235        );
3236    }
3237
3238    #[test]
3239    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3240        let array = vec![
3241            Some(-999999999999999999),
3242            Some(0),
3243            Some(999999999999999999),
3244            None,
3245        ];
3246        let array = create_decimal64_array(array, 18, 3).unwrap();
3247
3248        // Divide out all digits of precision -- rounding could still produce +/- 1
3249        let output_type = DataType::Decimal64(18, -15);
3250        assert!(can_cast_types(array.data_type(), &output_type));
3251        generate_cast_test_case!(
3252            &array,
3253            Decimal64Array,
3254            &output_type,
3255            vec![Some(-1), Some(0), Some(1), None]
3256        );
3257
3258        // Divide out more digits than we have precision -- all-zero result
3259        let output_type = DataType::Decimal64(18, -16);
3260        assert!(can_cast_types(array.data_type(), &output_type));
3261        generate_cast_test_case!(
3262            &array,
3263            Decimal64Array,
3264            &output_type,
3265            vec![Some(0), Some(0), Some(0), None]
3266        );
3267    }
3268
3269    #[test]
3270    fn test_cast_floating_to_decimals() {
3271        for output_type in [
3272            DataType::Decimal32(9, 3),
3273            DataType::Decimal64(9, 3),
3274            DataType::Decimal128(9, 3),
3275            DataType::Decimal256(9, 3),
3276        ] {
3277            let input_type = DataType::Float64;
3278            assert!(can_cast_types(&input_type, &output_type));
3279
3280            let array = vec![Some(1.1_f64)];
3281            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3282            let result = cast_with_options(
3283                &array,
3284                &output_type,
3285                &CastOptions {
3286                    safe: false,
3287                    format_options: FormatOptions::default(),
3288                },
3289            );
3290            assert!(
3291                result.is_ok(),
3292                "Failed to cast to {output_type} with: {}",
3293                result.unwrap_err()
3294            );
3295        }
3296    }
3297
3298    #[test]
3299    fn test_cast_decimal128_to_decimal128_overflow() {
3300        let input_type = DataType::Decimal128(38, 3);
3301        let output_type = DataType::Decimal128(38, 38);
3302        assert!(can_cast_types(&input_type, &output_type));
3303
3304        let array = vec![Some(i128::MAX)];
3305        let array = create_decimal128_array(array, 38, 3).unwrap();
3306        let result = cast_with_options(
3307            &array,
3308            &output_type,
3309            &CastOptions {
3310                safe: false,
3311                format_options: FormatOptions::default(),
3312            },
3313        );
3314        assert_eq!(
3315            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3316            result.unwrap_err().to_string()
3317        );
3318    }
3319
3320    #[test]
3321    fn test_cast_decimal128_to_decimal256_overflow() {
3322        let input_type = DataType::Decimal128(38, 3);
3323        let output_type = DataType::Decimal256(76, 76);
3324        assert!(can_cast_types(&input_type, &output_type));
3325
3326        let array = vec![Some(i128::MAX)];
3327        let array = create_decimal128_array(array, 38, 3).unwrap();
3328        let result = cast_with_options(
3329            &array,
3330            &output_type,
3331            &CastOptions {
3332                safe: false,
3333                format_options: FormatOptions::default(),
3334            },
3335        );
3336        assert_eq!(
3337            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3338            result.unwrap_err().to_string()
3339        );
3340    }
3341
3342    #[test]
3343    fn test_cast_decimal32_to_decimal256() {
3344        let input_type = DataType::Decimal32(8, 3);
3345        let output_type = DataType::Decimal256(20, 4);
3346        assert!(can_cast_types(&input_type, &output_type));
3347        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3348        let array = create_decimal32_array(array, 8, 3).unwrap();
3349        generate_cast_test_case!(
3350            &array,
3351            Decimal256Array,
3352            &output_type,
3353            vec![
3354                Some(i256::from_i128(11234560_i128)),
3355                Some(i256::from_i128(21234560_i128)),
3356                Some(i256::from_i128(31234560_i128)),
3357                None
3358            ]
3359        );
3360    }
3361    #[test]
3362    fn test_cast_decimal64_to_decimal256() {
3363        let input_type = DataType::Decimal64(12, 3);
3364        let output_type = DataType::Decimal256(20, 4);
3365        assert!(can_cast_types(&input_type, &output_type));
3366        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3367        let array = create_decimal64_array(array, 12, 3).unwrap();
3368        generate_cast_test_case!(
3369            &array,
3370            Decimal256Array,
3371            &output_type,
3372            vec![
3373                Some(i256::from_i128(11234560_i128)),
3374                Some(i256::from_i128(21234560_i128)),
3375                Some(i256::from_i128(31234560_i128)),
3376                None
3377            ]
3378        );
3379    }
3380    #[test]
3381    fn test_cast_decimal128_to_decimal256() {
3382        let input_type = DataType::Decimal128(20, 3);
3383        let output_type = DataType::Decimal256(20, 4);
3384        assert!(can_cast_types(&input_type, &output_type));
3385        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3386        let array = create_decimal128_array(array, 20, 3).unwrap();
3387        generate_cast_test_case!(
3388            &array,
3389            Decimal256Array,
3390            &output_type,
3391            vec![
3392                Some(i256::from_i128(11234560_i128)),
3393                Some(i256::from_i128(21234560_i128)),
3394                Some(i256::from_i128(31234560_i128)),
3395                None
3396            ]
3397        );
3398    }
3399
3400    #[test]
3401    fn test_cast_decimal256_to_decimal128_overflow() {
3402        let input_type = DataType::Decimal256(76, 5);
3403        let output_type = DataType::Decimal128(38, 7);
3404        assert!(can_cast_types(&input_type, &output_type));
3405        let array = vec![Some(i256::from_i128(i128::MAX))];
3406        let array = create_decimal256_array(array, 76, 5).unwrap();
3407        let result = cast_with_options(
3408            &array,
3409            &output_type,
3410            &CastOptions {
3411                safe: false,
3412                format_options: FormatOptions::default(),
3413            },
3414        );
3415        assert_eq!(
3416            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3417            result.unwrap_err().to_string()
3418        );
3419    }
3420
3421    #[test]
3422    fn test_cast_decimal256_to_decimal256_overflow() {
3423        let input_type = DataType::Decimal256(76, 5);
3424        let output_type = DataType::Decimal256(76, 55);
3425        assert!(can_cast_types(&input_type, &output_type));
3426        let array = vec![Some(i256::from_i128(i128::MAX))];
3427        let array = create_decimal256_array(array, 76, 5).unwrap();
3428        let result = cast_with_options(
3429            &array,
3430            &output_type,
3431            &CastOptions {
3432                safe: false,
3433                format_options: FormatOptions::default(),
3434            },
3435        );
3436        assert_eq!(
3437            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3438            result.unwrap_err().to_string()
3439        );
3440    }
3441
3442    #[test]
3443    fn test_cast_decimal256_to_decimal128() {
3444        let input_type = DataType::Decimal256(20, 3);
3445        let output_type = DataType::Decimal128(20, 4);
3446        assert!(can_cast_types(&input_type, &output_type));
3447        let array = vec![
3448            Some(i256::from_i128(1123456)),
3449            Some(i256::from_i128(2123456)),
3450            Some(i256::from_i128(3123456)),
3451            None,
3452        ];
3453        let array = create_decimal256_array(array, 20, 3).unwrap();
3454        generate_cast_test_case!(
3455            &array,
3456            Decimal128Array,
3457            &output_type,
3458            vec![
3459                Some(11234560_i128),
3460                Some(21234560_i128),
3461                Some(31234560_i128),
3462                None
3463            ]
3464        );
3465    }
3466
3467    #[test]
3468    fn test_cast_decimal256_to_decimal256() {
3469        let input_type = DataType::Decimal256(20, 3);
3470        let output_type = DataType::Decimal256(20, 4);
3471        assert!(can_cast_types(&input_type, &output_type));
3472        let array = vec![
3473            Some(i256::from_i128(1123456)),
3474            Some(i256::from_i128(2123456)),
3475            Some(i256::from_i128(3123456)),
3476            None,
3477        ];
3478        let array = create_decimal256_array(array, 20, 3).unwrap();
3479        generate_cast_test_case!(
3480            &array,
3481            Decimal256Array,
3482            &output_type,
3483            vec![
3484                Some(i256::from_i128(11234560_i128)),
3485                Some(i256::from_i128(21234560_i128)),
3486                Some(i256::from_i128(31234560_i128)),
3487                None
3488            ]
3489        );
3490    }
3491
3492    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3493    where
3494        T: ArrowPrimitiveType + DecimalType,
3495    {
3496        // u8
3497        generate_cast_test_case!(
3498            array,
3499            UInt8Array,
3500            &DataType::UInt8,
3501            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3502        );
3503        // u16
3504        generate_cast_test_case!(
3505            array,
3506            UInt16Array,
3507            &DataType::UInt16,
3508            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3509        );
3510        // u32
3511        generate_cast_test_case!(
3512            array,
3513            UInt32Array,
3514            &DataType::UInt32,
3515            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3516        );
3517        // u64
3518        generate_cast_test_case!(
3519            array,
3520            UInt64Array,
3521            &DataType::UInt64,
3522            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3523        );
3524        // i8
3525        generate_cast_test_case!(
3526            array,
3527            Int8Array,
3528            &DataType::Int8,
3529            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3530        );
3531        // i16
3532        generate_cast_test_case!(
3533            array,
3534            Int16Array,
3535            &DataType::Int16,
3536            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3537        );
3538        // i32
3539        generate_cast_test_case!(
3540            array,
3541            Int32Array,
3542            &DataType::Int32,
3543            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3544        );
3545        // i64
3546        generate_cast_test_case!(
3547            array,
3548            Int64Array,
3549            &DataType::Int64,
3550            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3551        );
3552        // f32
3553        generate_cast_test_case!(
3554            array,
3555            Float32Array,
3556            &DataType::Float32,
3557            vec![
3558                Some(1.25_f32),
3559                Some(2.25_f32),
3560                Some(3.25_f32),
3561                None,
3562                Some(5.25_f32)
3563            ]
3564        );
3565        // f64
3566        generate_cast_test_case!(
3567            array,
3568            Float64Array,
3569            &DataType::Float64,
3570            vec![
3571                Some(1.25_f64),
3572                Some(2.25_f64),
3573                Some(3.25_f64),
3574                None,
3575                Some(5.25_f64)
3576            ]
3577        );
3578    }
3579
3580    #[test]
3581    fn test_cast_decimal32_to_numeric() {
3582        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3583        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3584
3585        generate_decimal_to_numeric_cast_test_case(&array);
3586    }
3587
3588    #[test]
3589    fn test_cast_decimal64_to_numeric() {
3590        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3591        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3592
3593        generate_decimal_to_numeric_cast_test_case(&array);
3594    }
3595
3596    #[test]
3597    fn test_cast_decimal128_to_numeric() {
3598        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3599        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3600
3601        generate_decimal_to_numeric_cast_test_case(&array);
3602
3603        // overflow test: out of range of max u8
3604        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3605        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3606        let casted_array = cast_with_options(
3607            &array,
3608            &DataType::UInt8,
3609            &CastOptions {
3610                safe: false,
3611                format_options: FormatOptions::default(),
3612            },
3613        );
3614        assert_eq!(
3615            "Cast error: value of 513 is out of range UInt8".to_string(),
3616            casted_array.unwrap_err().to_string()
3617        );
3618
3619        let casted_array = cast_with_options(
3620            &array,
3621            &DataType::UInt8,
3622            &CastOptions {
3623                safe: true,
3624                format_options: FormatOptions::default(),
3625            },
3626        );
3627        assert!(casted_array.is_ok());
3628        assert!(casted_array.unwrap().is_null(0));
3629
3630        // overflow test: out of range of max i8
3631        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3632        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3633        let casted_array = cast_with_options(
3634            &array,
3635            &DataType::Int8,
3636            &CastOptions {
3637                safe: false,
3638                format_options: FormatOptions::default(),
3639            },
3640        );
3641        assert_eq!(
3642            "Cast error: value of 244 is out of range Int8".to_string(),
3643            casted_array.unwrap_err().to_string()
3644        );
3645
3646        let casted_array = cast_with_options(
3647            &array,
3648            &DataType::Int8,
3649            &CastOptions {
3650                safe: true,
3651                format_options: FormatOptions::default(),
3652            },
3653        );
3654        assert!(casted_array.is_ok());
3655        assert!(casted_array.unwrap().is_null(0));
3656
3657        // loss the precision: convert decimal to f32、f64
3658        // f32
3659        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3660        let value_array: Vec<Option<i128>> = vec![
3661            Some(125),
3662            Some(225),
3663            Some(325),
3664            None,
3665            Some(525),
3666            Some(112345678),
3667            Some(112345679),
3668        ];
3669        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3670        generate_cast_test_case!(
3671            &array,
3672            Float32Array,
3673            &DataType::Float32,
3674            vec![
3675                Some(1.25_f32),
3676                Some(2.25_f32),
3677                Some(3.25_f32),
3678                None,
3679                Some(5.25_f32),
3680                Some(1_123_456.7_f32),
3681                Some(1_123_456.7_f32)
3682            ]
3683        );
3684
3685        // f64
3686        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3687        let value_array: Vec<Option<i128>> = vec![
3688            Some(125),
3689            Some(225),
3690            Some(325),
3691            None,
3692            Some(525),
3693            Some(112345678901234568),
3694            Some(112345678901234560),
3695        ];
3696        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3697        generate_cast_test_case!(
3698            &array,
3699            Float64Array,
3700            &DataType::Float64,
3701            vec![
3702                Some(1.25_f64),
3703                Some(2.25_f64),
3704                Some(3.25_f64),
3705                None,
3706                Some(5.25_f64),
3707                Some(1_123_456_789_012_345.6_f64),
3708                Some(1_123_456_789_012_345.6_f64),
3709            ]
3710        );
3711    }
3712
3713    #[test]
3714    fn test_cast_decimal256_to_numeric() {
3715        let value_array: Vec<Option<i256>> = vec![
3716            Some(i256::from_i128(125)),
3717            Some(i256::from_i128(225)),
3718            Some(i256::from_i128(325)),
3719            None,
3720            Some(i256::from_i128(525)),
3721        ];
3722        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3723        // u8
3724        generate_cast_test_case!(
3725            &array,
3726            UInt8Array,
3727            &DataType::UInt8,
3728            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3729        );
3730        // u16
3731        generate_cast_test_case!(
3732            &array,
3733            UInt16Array,
3734            &DataType::UInt16,
3735            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3736        );
3737        // u32
3738        generate_cast_test_case!(
3739            &array,
3740            UInt32Array,
3741            &DataType::UInt32,
3742            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3743        );
3744        // u64
3745        generate_cast_test_case!(
3746            &array,
3747            UInt64Array,
3748            &DataType::UInt64,
3749            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3750        );
3751        // i8
3752        generate_cast_test_case!(
3753            &array,
3754            Int8Array,
3755            &DataType::Int8,
3756            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3757        );
3758        // i16
3759        generate_cast_test_case!(
3760            &array,
3761            Int16Array,
3762            &DataType::Int16,
3763            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3764        );
3765        // i32
3766        generate_cast_test_case!(
3767            &array,
3768            Int32Array,
3769            &DataType::Int32,
3770            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3771        );
3772        // i64
3773        generate_cast_test_case!(
3774            &array,
3775            Int64Array,
3776            &DataType::Int64,
3777            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3778        );
3779        // f32
3780        generate_cast_test_case!(
3781            &array,
3782            Float32Array,
3783            &DataType::Float32,
3784            vec![
3785                Some(1.25_f32),
3786                Some(2.25_f32),
3787                Some(3.25_f32),
3788                None,
3789                Some(5.25_f32)
3790            ]
3791        );
3792        // f64
3793        generate_cast_test_case!(
3794            &array,
3795            Float64Array,
3796            &DataType::Float64,
3797            vec![
3798                Some(1.25_f64),
3799                Some(2.25_f64),
3800                Some(3.25_f64),
3801                None,
3802                Some(5.25_f64)
3803            ]
3804        );
3805
3806        // overflow test: out of range of max i8
3807        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3808        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3809        let casted_array = cast_with_options(
3810            &array,
3811            &DataType::Int8,
3812            &CastOptions {
3813                safe: false,
3814                format_options: FormatOptions::default(),
3815            },
3816        );
3817        assert_eq!(
3818            "Cast error: value of 244 is out of range Int8".to_string(),
3819            casted_array.unwrap_err().to_string()
3820        );
3821
3822        let casted_array = cast_with_options(
3823            &array,
3824            &DataType::Int8,
3825            &CastOptions {
3826                safe: true,
3827                format_options: FormatOptions::default(),
3828            },
3829        );
3830        assert!(casted_array.is_ok());
3831        assert!(casted_array.unwrap().is_null(0));
3832
3833        // loss the precision: convert decimal to f32、f64
3834        // f32
3835        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3836        let value_array: Vec<Option<i256>> = vec![
3837            Some(i256::from_i128(125)),
3838            Some(i256::from_i128(225)),
3839            Some(i256::from_i128(325)),
3840            None,
3841            Some(i256::from_i128(525)),
3842            Some(i256::from_i128(112345678)),
3843            Some(i256::from_i128(112345679)),
3844        ];
3845        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3846        generate_cast_test_case!(
3847            &array,
3848            Float32Array,
3849            &DataType::Float32,
3850            vec![
3851                Some(1.25_f32),
3852                Some(2.25_f32),
3853                Some(3.25_f32),
3854                None,
3855                Some(5.25_f32),
3856                Some(1_123_456.7_f32),
3857                Some(1_123_456.7_f32)
3858            ]
3859        );
3860
3861        // f64
3862        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3863        let value_array: Vec<Option<i256>> = vec![
3864            Some(i256::from_i128(125)),
3865            Some(i256::from_i128(225)),
3866            Some(i256::from_i128(325)),
3867            None,
3868            Some(i256::from_i128(525)),
3869            Some(i256::from_i128(112345678901234568)),
3870            Some(i256::from_i128(112345678901234560)),
3871        ];
3872        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3873        generate_cast_test_case!(
3874            &array,
3875            Float64Array,
3876            &DataType::Float64,
3877            vec![
3878                Some(1.25_f64),
3879                Some(2.25_f64),
3880                Some(3.25_f64),
3881                None,
3882                Some(5.25_f64),
3883                Some(1_123_456_789_012_345.6_f64),
3884                Some(1_123_456_789_012_345.6_f64),
3885            ]
3886        );
3887    }
3888
3889    #[test]
3890    fn test_cast_numeric_to_decimal128() {
3891        let decimal_type = DataType::Decimal128(38, 6);
3892        // u8, u16, u32, u64
3893        let input_datas = vec![
3894            Arc::new(UInt8Array::from(vec![
3895                Some(1),
3896                Some(2),
3897                Some(3),
3898                None,
3899                Some(5),
3900            ])) as ArrayRef, // u8
3901            Arc::new(UInt16Array::from(vec![
3902                Some(1),
3903                Some(2),
3904                Some(3),
3905                None,
3906                Some(5),
3907            ])) as ArrayRef, // u16
3908            Arc::new(UInt32Array::from(vec![
3909                Some(1),
3910                Some(2),
3911                Some(3),
3912                None,
3913                Some(5),
3914            ])) as ArrayRef, // u32
3915            Arc::new(UInt64Array::from(vec![
3916                Some(1),
3917                Some(2),
3918                Some(3),
3919                None,
3920                Some(5),
3921            ])) as ArrayRef, // u64
3922        ];
3923
3924        for array in input_datas {
3925            generate_cast_test_case!(
3926                &array,
3927                Decimal128Array,
3928                &decimal_type,
3929                vec![
3930                    Some(1000000_i128),
3931                    Some(2000000_i128),
3932                    Some(3000000_i128),
3933                    None,
3934                    Some(5000000_i128)
3935                ]
3936            );
3937        }
3938
3939        // i8, i16, i32, i64
3940        let input_datas = vec![
3941            Arc::new(Int8Array::from(vec![
3942                Some(1),
3943                Some(2),
3944                Some(3),
3945                None,
3946                Some(5),
3947            ])) as ArrayRef, // i8
3948            Arc::new(Int16Array::from(vec![
3949                Some(1),
3950                Some(2),
3951                Some(3),
3952                None,
3953                Some(5),
3954            ])) as ArrayRef, // i16
3955            Arc::new(Int32Array::from(vec![
3956                Some(1),
3957                Some(2),
3958                Some(3),
3959                None,
3960                Some(5),
3961            ])) as ArrayRef, // i32
3962            Arc::new(Int64Array::from(vec![
3963                Some(1),
3964                Some(2),
3965                Some(3),
3966                None,
3967                Some(5),
3968            ])) as ArrayRef, // i64
3969        ];
3970        for array in input_datas {
3971            generate_cast_test_case!(
3972                &array,
3973                Decimal128Array,
3974                &decimal_type,
3975                vec![
3976                    Some(1000000_i128),
3977                    Some(2000000_i128),
3978                    Some(3000000_i128),
3979                    None,
3980                    Some(5000000_i128)
3981                ]
3982            );
3983        }
3984
3985        // test u8 to decimal type with overflow the result type
3986        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3987        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
3988        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3989        assert!(casted_array.is_ok());
3990        let array = casted_array.unwrap();
3991        let array: &Decimal128Array = array.as_primitive();
3992        assert!(array.is_null(4));
3993
3994        // test i8 to decimal type with overflow the result type
3995        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3996        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3997        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3998        assert!(casted_array.is_ok());
3999        let array = casted_array.unwrap();
4000        let array: &Decimal128Array = array.as_primitive();
4001        assert!(array.is_null(4));
4002
4003        // test f32 to decimal type
4004        let array = Float32Array::from(vec![
4005            Some(1.1),
4006            Some(2.2),
4007            Some(4.4),
4008            None,
4009            Some(1.123_456_4), // round down
4010            Some(1.123_456_7), // round up
4011        ]);
4012        let array = Arc::new(array) as ArrayRef;
4013        generate_cast_test_case!(
4014            &array,
4015            Decimal128Array,
4016            &decimal_type,
4017            vec![
4018                Some(1100000_i128),
4019                Some(2200000_i128),
4020                Some(4400000_i128),
4021                None,
4022                Some(1123456_i128), // round down
4023                Some(1123457_i128), // round up
4024            ]
4025        );
4026
4027        // test f64 to decimal type
4028        let array = Float64Array::from(vec![
4029            Some(1.1),
4030            Some(2.2),
4031            Some(4.4),
4032            None,
4033            Some(1.123_456_489_123_4),     // round up
4034            Some(1.123_456_789_123_4),     // round up
4035            Some(1.123_456_489_012_345_6), // round down
4036            Some(1.123_456_789_012_345_6), // round up
4037        ]);
4038        generate_cast_test_case!(
4039            &array,
4040            Decimal128Array,
4041            &decimal_type,
4042            vec![
4043                Some(1100000_i128),
4044                Some(2200000_i128),
4045                Some(4400000_i128),
4046                None,
4047                Some(1123456_i128), // round down
4048                Some(1123457_i128), // round up
4049                Some(1123456_i128), // round down
4050                Some(1123457_i128), // round up
4051            ]
4052        );
4053    }
4054
4055    #[test]
4056    fn test_cast_numeric_to_decimal256() {
4057        let decimal_type = DataType::Decimal256(76, 6);
4058        // u8, u16, u32, u64
4059        let input_datas = vec![
4060            Arc::new(UInt8Array::from(vec![
4061                Some(1),
4062                Some(2),
4063                Some(3),
4064                None,
4065                Some(5),
4066            ])) as ArrayRef, // u8
4067            Arc::new(UInt16Array::from(vec![
4068                Some(1),
4069                Some(2),
4070                Some(3),
4071                None,
4072                Some(5),
4073            ])) as ArrayRef, // u16
4074            Arc::new(UInt32Array::from(vec![
4075                Some(1),
4076                Some(2),
4077                Some(3),
4078                None,
4079                Some(5),
4080            ])) as ArrayRef, // u32
4081            Arc::new(UInt64Array::from(vec![
4082                Some(1),
4083                Some(2),
4084                Some(3),
4085                None,
4086                Some(5),
4087            ])) as ArrayRef, // u64
4088        ];
4089
4090        for array in input_datas {
4091            generate_cast_test_case!(
4092                &array,
4093                Decimal256Array,
4094                &decimal_type,
4095                vec![
4096                    Some(i256::from_i128(1000000_i128)),
4097                    Some(i256::from_i128(2000000_i128)),
4098                    Some(i256::from_i128(3000000_i128)),
4099                    None,
4100                    Some(i256::from_i128(5000000_i128))
4101                ]
4102            );
4103        }
4104
4105        // i8, i16, i32, i64
4106        let input_datas = vec![
4107            Arc::new(Int8Array::from(vec![
4108                Some(1),
4109                Some(2),
4110                Some(3),
4111                None,
4112                Some(5),
4113            ])) as ArrayRef, // i8
4114            Arc::new(Int16Array::from(vec![
4115                Some(1),
4116                Some(2),
4117                Some(3),
4118                None,
4119                Some(5),
4120            ])) as ArrayRef, // i16
4121            Arc::new(Int32Array::from(vec![
4122                Some(1),
4123                Some(2),
4124                Some(3),
4125                None,
4126                Some(5),
4127            ])) as ArrayRef, // i32
4128            Arc::new(Int64Array::from(vec![
4129                Some(1),
4130                Some(2),
4131                Some(3),
4132                None,
4133                Some(5),
4134            ])) as ArrayRef, // i64
4135        ];
4136        for array in input_datas {
4137            generate_cast_test_case!(
4138                &array,
4139                Decimal256Array,
4140                &decimal_type,
4141                vec![
4142                    Some(i256::from_i128(1000000_i128)),
4143                    Some(i256::from_i128(2000000_i128)),
4144                    Some(i256::from_i128(3000000_i128)),
4145                    None,
4146                    Some(i256::from_i128(5000000_i128))
4147                ]
4148            );
4149        }
4150
4151        // test i8 to decimal type with overflow the result type
4152        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4153        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4154        let array = Arc::new(array) as ArrayRef;
4155        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4156        assert!(casted_array.is_ok());
4157        let array = casted_array.unwrap();
4158        let array: &Decimal256Array = array.as_primitive();
4159        assert!(array.is_null(4));
4160
4161        // test f32 to decimal type
4162        let array = Float32Array::from(vec![
4163            Some(1.1),
4164            Some(2.2),
4165            Some(4.4),
4166            None,
4167            Some(1.123_456_4), // round down
4168            Some(1.123_456_7), // round up
4169        ]);
4170        generate_cast_test_case!(
4171            &array,
4172            Decimal256Array,
4173            &decimal_type,
4174            vec![
4175                Some(i256::from_i128(1100000_i128)),
4176                Some(i256::from_i128(2200000_i128)),
4177                Some(i256::from_i128(4400000_i128)),
4178                None,
4179                Some(i256::from_i128(1123456_i128)), // round down
4180                Some(i256::from_i128(1123457_i128)), // round up
4181            ]
4182        );
4183
4184        // test f64 to decimal type
4185        let array = Float64Array::from(vec![
4186            Some(1.1),
4187            Some(2.2),
4188            Some(4.4),
4189            None,
4190            Some(1.123_456_489_123_4),     // round down
4191            Some(1.123_456_789_123_4),     // round up
4192            Some(1.123_456_489_012_345_6), // round down
4193            Some(1.123_456_789_012_345_6), // round up
4194        ]);
4195        generate_cast_test_case!(
4196            &array,
4197            Decimal256Array,
4198            &decimal_type,
4199            vec![
4200                Some(i256::from_i128(1100000_i128)),
4201                Some(i256::from_i128(2200000_i128)),
4202                Some(i256::from_i128(4400000_i128)),
4203                None,
4204                Some(i256::from_i128(1123456_i128)), // round down
4205                Some(i256::from_i128(1123457_i128)), // round up
4206                Some(i256::from_i128(1123456_i128)), // round down
4207                Some(i256::from_i128(1123457_i128)), // round up
4208            ]
4209        );
4210    }
4211
4212    #[test]
4213    fn test_cast_i32_to_f64() {
4214        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4215        let b = cast(&array, &DataType::Float64).unwrap();
4216        let c = b.as_primitive::<Float64Type>();
4217        assert_eq!(5.0, c.value(0));
4218        assert_eq!(6.0, c.value(1));
4219        assert_eq!(7.0, c.value(2));
4220        assert_eq!(8.0, c.value(3));
4221        assert_eq!(9.0, c.value(4));
4222    }
4223
4224    #[test]
4225    fn test_cast_i32_to_u8() {
4226        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4227        let b = cast(&array, &DataType::UInt8).unwrap();
4228        let c = b.as_primitive::<UInt8Type>();
4229        assert!(!c.is_valid(0));
4230        assert_eq!(6, c.value(1));
4231        assert!(!c.is_valid(2));
4232        assert_eq!(8, c.value(3));
4233        // overflows return None
4234        assert!(!c.is_valid(4));
4235    }
4236
4237    #[test]
4238    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4239    fn test_cast_int32_to_u8_with_error() {
4240        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4241        // overflow with the error
4242        let cast_option = CastOptions {
4243            safe: false,
4244            format_options: FormatOptions::default(),
4245        };
4246        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4247        assert!(result.is_err());
4248        result.unwrap();
4249    }
4250
4251    #[test]
4252    fn test_cast_i32_to_u8_sliced() {
4253        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4254        assert_eq!(0, array.offset());
4255        let array = array.slice(2, 3);
4256        let b = cast(&array, &DataType::UInt8).unwrap();
4257        assert_eq!(3, b.len());
4258        let c = b.as_primitive::<UInt8Type>();
4259        assert!(!c.is_valid(0));
4260        assert_eq!(8, c.value(1));
4261        // overflows return None
4262        assert!(!c.is_valid(2));
4263    }
4264
4265    #[test]
4266    fn test_cast_i32_to_i32() {
4267        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4268        let b = cast(&array, &DataType::Int32).unwrap();
4269        let c = b.as_primitive::<Int32Type>();
4270        assert_eq!(5, c.value(0));
4271        assert_eq!(6, c.value(1));
4272        assert_eq!(7, c.value(2));
4273        assert_eq!(8, c.value(3));
4274        assert_eq!(9, c.value(4));
4275    }
4276
4277    #[test]
4278    fn test_cast_i32_to_list_i32() {
4279        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4280        let b = cast(
4281            &array,
4282            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4283        )
4284        .unwrap();
4285        assert_eq!(5, b.len());
4286        let arr = b.as_list::<i32>();
4287        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4288        assert_eq!(1, arr.value_length(0));
4289        assert_eq!(1, arr.value_length(1));
4290        assert_eq!(1, arr.value_length(2));
4291        assert_eq!(1, arr.value_length(3));
4292        assert_eq!(1, arr.value_length(4));
4293        let c = arr.values().as_primitive::<Int32Type>();
4294        assert_eq!(5, c.value(0));
4295        assert_eq!(6, c.value(1));
4296        assert_eq!(7, c.value(2));
4297        assert_eq!(8, c.value(3));
4298        assert_eq!(9, c.value(4));
4299    }
4300
4301    #[test]
4302    fn test_cast_i32_to_list_i32_nullable() {
4303        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4304        let b = cast(
4305            &array,
4306            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4307        )
4308        .unwrap();
4309        assert_eq!(5, b.len());
4310        assert_eq!(0, b.null_count());
4311        let arr = b.as_list::<i32>();
4312        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4313        assert_eq!(1, arr.value_length(0));
4314        assert_eq!(1, arr.value_length(1));
4315        assert_eq!(1, arr.value_length(2));
4316        assert_eq!(1, arr.value_length(3));
4317        assert_eq!(1, arr.value_length(4));
4318
4319        let c = arr.values().as_primitive::<Int32Type>();
4320        assert_eq!(1, c.null_count());
4321        assert_eq!(5, c.value(0));
4322        assert!(!c.is_valid(1));
4323        assert_eq!(7, c.value(2));
4324        assert_eq!(8, c.value(3));
4325        assert_eq!(9, c.value(4));
4326    }
4327
4328    #[test]
4329    fn test_cast_i32_to_list_f64_nullable_sliced() {
4330        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4331        let array = array.slice(2, 4);
4332        let b = cast(
4333            &array,
4334            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4335        )
4336        .unwrap();
4337        assert_eq!(4, b.len());
4338        assert_eq!(0, b.null_count());
4339        let arr = b.as_list::<i32>();
4340        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4341        assert_eq!(1, arr.value_length(0));
4342        assert_eq!(1, arr.value_length(1));
4343        assert_eq!(1, arr.value_length(2));
4344        assert_eq!(1, arr.value_length(3));
4345        let c = arr.values().as_primitive::<Float64Type>();
4346        assert_eq!(1, c.null_count());
4347        assert_eq!(7.0, c.value(0));
4348        assert_eq!(8.0, c.value(1));
4349        assert!(!c.is_valid(2));
4350        assert_eq!(10.0, c.value(3));
4351    }
4352
4353    #[test]
4354    fn test_cast_int_to_utf8view() {
4355        let inputs = vec![
4356            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4357            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4358            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4359            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4360            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4361            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4362            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4363            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4364        ];
4365        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4366            None,
4367            Some("8"),
4368            Some("9"),
4369            Some("10"),
4370        ]));
4371
4372        for array in inputs {
4373            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4374            let arr = cast(&array, &DataType::Utf8View).unwrap();
4375            assert_eq!(expected.as_ref(), arr.as_ref());
4376        }
4377    }
4378
4379    #[test]
4380    fn test_cast_float_to_utf8view() {
4381        let inputs = vec![
4382            Arc::new(Float16Array::from(vec![
4383                Some(f16::from_f64(1.5)),
4384                Some(f16::from_f64(2.5)),
4385                None,
4386            ])) as ArrayRef,
4387            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4388            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4389        ];
4390
4391        let expected: ArrayRef =
4392            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4393
4394        for array in inputs {
4395            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4396            let arr = cast(&array, &DataType::Utf8View).unwrap();
4397            assert_eq!(expected.as_ref(), arr.as_ref());
4398        }
4399    }
4400
4401    #[test]
4402    fn test_cast_utf8_to_i32() {
4403        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4404        let b = cast(&array, &DataType::Int32).unwrap();
4405        let c = b.as_primitive::<Int32Type>();
4406        assert_eq!(5, c.value(0));
4407        assert_eq!(6, c.value(1));
4408        assert!(!c.is_valid(2));
4409        assert_eq!(8, c.value(3));
4410        assert!(!c.is_valid(4));
4411    }
4412
4413    #[test]
4414    fn test_cast_utf8view_to_i32() {
4415        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4416        let b = cast(&array, &DataType::Int32).unwrap();
4417        let c = b.as_primitive::<Int32Type>();
4418        assert_eq!(5, c.value(0));
4419        assert_eq!(6, c.value(1));
4420        assert!(!c.is_valid(2));
4421        assert_eq!(8, c.value(3));
4422        assert!(!c.is_valid(4));
4423    }
4424
4425    #[test]
4426    fn test_cast_utf8view_to_f32() {
4427        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4428        let b = cast(&array, &DataType::Float32).unwrap();
4429        let c = b.as_primitive::<Float32Type>();
4430        assert_eq!(3.0, c.value(0));
4431        assert_eq!(4.56, c.value(1));
4432        assert!(!c.is_valid(2));
4433        assert_eq!(8.9, c.value(3));
4434    }
4435
4436    #[test]
4437    fn test_cast_utf8view_to_decimal128() {
4438        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4439        let arr = Arc::new(array) as ArrayRef;
4440        generate_cast_test_case!(
4441            &arr,
4442            Decimal128Array,
4443            &DataType::Decimal128(4, 2),
4444            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4445        );
4446    }
4447
4448    #[test]
4449    fn test_cast_with_options_utf8_to_i32() {
4450        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4451        let result = cast_with_options(
4452            &array,
4453            &DataType::Int32,
4454            &CastOptions {
4455                safe: false,
4456                format_options: FormatOptions::default(),
4457            },
4458        );
4459        match result {
4460            Ok(_) => panic!("expected error"),
4461            Err(e) => {
4462                assert!(
4463                    e.to_string()
4464                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4465                    "Error: {e}"
4466                )
4467            }
4468        }
4469    }
4470
4471    #[test]
4472    fn test_cast_utf8_to_bool() {
4473        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4474        let casted = cast(&strings, &DataType::Boolean).unwrap();
4475        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4476        assert_eq!(*as_boolean_array(&casted), expected);
4477    }
4478
4479    #[test]
4480    fn test_cast_utf8view_to_bool() {
4481        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4482        let casted = cast(&strings, &DataType::Boolean).unwrap();
4483        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4484        assert_eq!(*as_boolean_array(&casted), expected);
4485    }
4486
4487    #[test]
4488    fn test_cast_with_options_utf8_to_bool() {
4489        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4490        let casted = cast_with_options(
4491            &strings,
4492            &DataType::Boolean,
4493            &CastOptions {
4494                safe: false,
4495                format_options: FormatOptions::default(),
4496            },
4497        );
4498        match casted {
4499            Ok(_) => panic!("expected error"),
4500            Err(e) => {
4501                assert!(
4502                    e.to_string().contains(
4503                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4504                    )
4505                )
4506            }
4507        }
4508    }
4509
4510    #[test]
4511    fn test_cast_bool_to_i32() {
4512        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4513        let b = cast(&array, &DataType::Int32).unwrap();
4514        let c = b.as_primitive::<Int32Type>();
4515        assert_eq!(1, c.value(0));
4516        assert_eq!(0, c.value(1));
4517        assert!(!c.is_valid(2));
4518    }
4519
4520    #[test]
4521    fn test_cast_bool_to_utf8view() {
4522        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4523        let b = cast(&array, &DataType::Utf8View).unwrap();
4524        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4525        assert_eq!("true", c.value(0));
4526        assert_eq!("false", c.value(1));
4527        assert!(!c.is_valid(2));
4528    }
4529
4530    #[test]
4531    fn test_cast_bool_to_utf8() {
4532        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4533        let b = cast(&array, &DataType::Utf8).unwrap();
4534        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4535        assert_eq!("true", c.value(0));
4536        assert_eq!("false", c.value(1));
4537        assert!(!c.is_valid(2));
4538    }
4539
4540    #[test]
4541    fn test_cast_bool_to_large_utf8() {
4542        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4543        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4544        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4545        assert_eq!("true", c.value(0));
4546        assert_eq!("false", c.value(1));
4547        assert!(!c.is_valid(2));
4548    }
4549
4550    #[test]
4551    fn test_cast_bool_to_f64() {
4552        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4553        let b = cast(&array, &DataType::Float64).unwrap();
4554        let c = b.as_primitive::<Float64Type>();
4555        assert_eq!(1.0, c.value(0));
4556        assert_eq!(0.0, c.value(1));
4557        assert!(!c.is_valid(2));
4558    }
4559
4560    #[test]
4561    fn test_cast_integer_to_timestamp() {
4562        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4563        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4564
4565        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4566        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4567
4568        assert_eq!(&actual, &expected);
4569
4570        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4571        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4572
4573        assert_eq!(&actual, &expected);
4574
4575        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4576        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4577
4578        assert_eq!(&actual, &expected);
4579
4580        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4581        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4582
4583        assert_eq!(&actual, &expected);
4584
4585        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4586        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4587
4588        assert_eq!(&actual, &expected);
4589
4590        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4591        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4592
4593        assert_eq!(&actual, &expected);
4594
4595        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4596        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4597
4598        assert_eq!(&actual, &expected);
4599    }
4600
4601    #[test]
4602    fn test_cast_timestamp_to_integer() {
4603        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4604            .with_timezone("UTC".to_string());
4605        let expected = cast(&array, &DataType::Int64).unwrap();
4606
4607        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4608        assert_eq!(&actual, &expected);
4609
4610        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4611        assert_eq!(&actual, &expected);
4612
4613        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4614        assert_eq!(&actual, &expected);
4615
4616        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4617        assert_eq!(&actual, &expected);
4618
4619        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4620        assert_eq!(&actual, &expected);
4621
4622        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4623        assert_eq!(&actual, &expected);
4624
4625        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4626        assert_eq!(&actual, &expected);
4627    }
4628
4629    #[test]
4630    fn test_cast_floating_to_timestamp() {
4631        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4632        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4633
4634        let array = Float16Array::from(vec![
4635            Some(f16::from_f32(2.0)),
4636            Some(f16::from_f32(10.6)),
4637            None,
4638        ]);
4639        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4640
4641        assert_eq!(&actual, &expected);
4642
4643        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4644        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4645
4646        assert_eq!(&actual, &expected);
4647
4648        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4649        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4650
4651        assert_eq!(&actual, &expected);
4652    }
4653
4654    #[test]
4655    fn test_cast_timestamp_to_floating() {
4656        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4657            .with_timezone("UTC".to_string());
4658        let expected = cast(&array, &DataType::Int64).unwrap();
4659
4660        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4661        assert_eq!(&actual, &expected);
4662
4663        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4664        assert_eq!(&actual, &expected);
4665
4666        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4667        assert_eq!(&actual, &expected);
4668    }
4669
4670    #[test]
4671    fn test_cast_decimal_to_timestamp() {
4672        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4673        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4674
4675        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4676            .with_precision_and_scale(4, 2)
4677            .unwrap();
4678        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4679
4680        assert_eq!(&actual, &expected);
4681
4682        let array = Decimal256Array::from(vec![
4683            Some(i256::from_i128(2000)),
4684            Some(i256::from_i128(10000)),
4685            None,
4686        ])
4687        .with_precision_and_scale(5, 3)
4688        .unwrap();
4689        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4690
4691        assert_eq!(&actual, &expected);
4692    }
4693
4694    #[test]
4695    fn test_cast_timestamp_to_decimal() {
4696        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4697            .with_timezone("UTC".to_string());
4698        let expected = cast(&array, &DataType::Int64).unwrap();
4699
4700        let actual = cast(
4701            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4702            &DataType::Int64,
4703        )
4704        .unwrap();
4705        assert_eq!(&actual, &expected);
4706
4707        let actual = cast(
4708            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4709            &DataType::Int64,
4710        )
4711        .unwrap();
4712        assert_eq!(&actual, &expected);
4713    }
4714
4715    #[test]
4716    fn test_cast_list_i32_to_list_u16() {
4717        let values = vec![
4718            Some(vec![Some(0), Some(0), Some(0)]),
4719            Some(vec![Some(-1), Some(-2), Some(-1)]),
4720            Some(vec![Some(2), Some(100000000)]),
4721        ];
4722        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
4723
4724        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
4725        assert!(can_cast_types(list_array.data_type(), &target_type));
4726        let cast_array = cast(&list_array, &target_type).unwrap();
4727
4728        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4729        //
4730        // 3 negative values should get lost when casting to unsigned,
4731        // 1 value should overflow
4732        assert_eq!(0, cast_array.null_count());
4733
4734        // offsets should be the same
4735        let array = cast_array.as_list::<i32>();
4736        assert_eq!(list_array.value_offsets(), array.value_offsets());
4737
4738        assert_eq!(DataType::UInt16, array.value_type());
4739        assert_eq!(3, array.value_length(0));
4740        assert_eq!(3, array.value_length(1));
4741        assert_eq!(2, array.value_length(2));
4742
4743        // expect 4 nulls: negative numbers and overflow
4744        let u16arr = array.values().as_primitive::<UInt16Type>();
4745        assert_eq!(4, u16arr.null_count());
4746
4747        // expect 4 nulls: negative numbers and overflow
4748        let expected: UInt16Array =
4749            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4750                .into_iter()
4751                .collect();
4752
4753        assert_eq!(u16arr, &expected);
4754    }
4755
4756    #[test]
4757    fn test_cast_list_i32_to_list_timestamp() {
4758        // Construct a value array
4759        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4760
4761        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4762
4763        // Construct a list array from the above two
4764        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4765        let list_data = ArrayData::builder(list_data_type)
4766            .len(3)
4767            .add_buffer(value_offsets)
4768            .add_child_data(value_data)
4769            .build()
4770            .unwrap();
4771        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4772
4773        let actual = cast(
4774            &list_array,
4775            &DataType::List(Arc::new(Field::new_list_field(
4776                DataType::Timestamp(TimeUnit::Microsecond, None),
4777                true,
4778            ))),
4779        )
4780        .unwrap();
4781
4782        let expected = cast(
4783            &cast(
4784                &list_array,
4785                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4786            )
4787            .unwrap(),
4788            &DataType::List(Arc::new(Field::new_list_field(
4789                DataType::Timestamp(TimeUnit::Microsecond, None),
4790                true,
4791            ))),
4792        )
4793        .unwrap();
4794
4795        assert_eq!(&actual, &expected);
4796    }
4797
4798    #[test]
4799    fn test_cast_date32_to_date64() {
4800        let a = Date32Array::from(vec![10000, 17890]);
4801        let array = Arc::new(a) as ArrayRef;
4802        let b = cast(&array, &DataType::Date64).unwrap();
4803        let c = b.as_primitive::<Date64Type>();
4804        assert_eq!(864000000000, c.value(0));
4805        assert_eq!(1545696000000, c.value(1));
4806    }
4807
4808    #[test]
4809    fn test_cast_date64_to_date32() {
4810        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4811        let array = Arc::new(a) as ArrayRef;
4812        let b = cast(&array, &DataType::Date32).unwrap();
4813        let c = b.as_primitive::<Date32Type>();
4814        assert_eq!(10000, c.value(0));
4815        assert_eq!(17890, c.value(1));
4816        assert!(c.is_null(2));
4817    }
4818
4819    #[test]
4820    fn test_cast_string_to_integral_overflow() {
4821        let str = Arc::new(StringArray::from(vec![
4822            Some("123"),
4823            Some("-123"),
4824            Some("86374"),
4825            None,
4826        ])) as ArrayRef;
4827
4828        let options = CastOptions {
4829            safe: true,
4830            format_options: FormatOptions::default(),
4831        };
4832        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4833        let expected =
4834            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4835        assert_eq!(&res, &expected);
4836    }
4837
4838    #[test]
4839    fn test_cast_string_to_timestamp() {
4840        let a0 = Arc::new(StringViewArray::from(vec![
4841            Some("2020-09-08T12:00:00.123456789+00:00"),
4842            Some("Not a valid date"),
4843            None,
4844        ])) as ArrayRef;
4845        let a1 = Arc::new(StringArray::from(vec![
4846            Some("2020-09-08T12:00:00.123456789+00:00"),
4847            Some("Not a valid date"),
4848            None,
4849        ])) as ArrayRef;
4850        let a2 = Arc::new(LargeStringArray::from(vec![
4851            Some("2020-09-08T12:00:00.123456789+00:00"),
4852            Some("Not a valid date"),
4853            None,
4854        ])) as ArrayRef;
4855        for array in &[a0, a1, a2] {
4856            for time_unit in &[
4857                TimeUnit::Second,
4858                TimeUnit::Millisecond,
4859                TimeUnit::Microsecond,
4860                TimeUnit::Nanosecond,
4861            ] {
4862                let to_type = DataType::Timestamp(*time_unit, None);
4863                let b = cast(array, &to_type).unwrap();
4864
4865                match time_unit {
4866                    TimeUnit::Second => {
4867                        let c = b.as_primitive::<TimestampSecondType>();
4868                        assert_eq!(1599566400, c.value(0));
4869                        assert!(c.is_null(1));
4870                        assert!(c.is_null(2));
4871                    }
4872                    TimeUnit::Millisecond => {
4873                        let c = b
4874                            .as_any()
4875                            .downcast_ref::<TimestampMillisecondArray>()
4876                            .unwrap();
4877                        assert_eq!(1599566400123, c.value(0));
4878                        assert!(c.is_null(1));
4879                        assert!(c.is_null(2));
4880                    }
4881                    TimeUnit::Microsecond => {
4882                        let c = b
4883                            .as_any()
4884                            .downcast_ref::<TimestampMicrosecondArray>()
4885                            .unwrap();
4886                        assert_eq!(1599566400123456, c.value(0));
4887                        assert!(c.is_null(1));
4888                        assert!(c.is_null(2));
4889                    }
4890                    TimeUnit::Nanosecond => {
4891                        let c = b
4892                            .as_any()
4893                            .downcast_ref::<TimestampNanosecondArray>()
4894                            .unwrap();
4895                        assert_eq!(1599566400123456789, c.value(0));
4896                        assert!(c.is_null(1));
4897                        assert!(c.is_null(2));
4898                    }
4899                }
4900
4901                let options = CastOptions {
4902                    safe: false,
4903                    format_options: FormatOptions::default(),
4904                };
4905                let err = cast_with_options(array, &to_type, &options).unwrap_err();
4906                assert_eq!(
4907                    err.to_string(),
4908                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4909                );
4910            }
4911        }
4912    }
4913
4914    #[test]
4915    fn test_cast_string_to_timestamp_overflow() {
4916        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4917        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4918        let result = result.as_primitive::<TimestampSecondType>();
4919        assert_eq!(result.values(), &[247112596800]);
4920    }
4921
4922    #[test]
4923    fn test_cast_string_to_date32() {
4924        let a0 = Arc::new(StringViewArray::from(vec![
4925            Some("2018-12-25"),
4926            Some("Not a valid date"),
4927            None,
4928        ])) as ArrayRef;
4929        let a1 = Arc::new(StringArray::from(vec![
4930            Some("2018-12-25"),
4931            Some("Not a valid date"),
4932            None,
4933        ])) as ArrayRef;
4934        let a2 = Arc::new(LargeStringArray::from(vec![
4935            Some("2018-12-25"),
4936            Some("Not a valid date"),
4937            None,
4938        ])) as ArrayRef;
4939        for array in &[a0, a1, a2] {
4940            let to_type = DataType::Date32;
4941            let b = cast(array, &to_type).unwrap();
4942            let c = b.as_primitive::<Date32Type>();
4943            assert_eq!(17890, c.value(0));
4944            assert!(c.is_null(1));
4945            assert!(c.is_null(2));
4946
4947            let options = CastOptions {
4948                safe: false,
4949                format_options: FormatOptions::default(),
4950            };
4951            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4952            assert_eq!(
4953                err.to_string(),
4954                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
4955            );
4956        }
4957    }
4958
4959    #[test]
4960    fn test_cast_string_with_large_date_to_date32() {
4961        let array = Arc::new(StringArray::from(vec![
4962            Some("+10999-12-31"),
4963            Some("-0010-02-28"),
4964            Some("0010-02-28"),
4965            Some("0000-01-01"),
4966            Some("-0000-01-01"),
4967            Some("-0001-01-01"),
4968        ])) as ArrayRef;
4969        let to_type = DataType::Date32;
4970        let options = CastOptions {
4971            safe: false,
4972            format_options: FormatOptions::default(),
4973        };
4974        let b = cast_with_options(&array, &to_type, &options).unwrap();
4975        let c = b.as_primitive::<Date32Type>();
4976        assert_eq!(3298139, c.value(0)); // 10999-12-31
4977        assert_eq!(-723122, c.value(1)); // -0010-02-28
4978        assert_eq!(-715817, c.value(2)); // 0010-02-28
4979        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
4980        assert_eq!(-719528, c.value(3)); // 0000-01-01
4981        assert_eq!(-719528, c.value(4)); // -0000-01-01
4982        assert_eq!(-719893, c.value(5)); // -0001-01-01
4983    }
4984
4985    #[test]
4986    fn test_cast_invalid_string_with_large_date_to_date32() {
4987        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
4988        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
4989        let to_type = DataType::Date32;
4990        let options = CastOptions {
4991            safe: false,
4992            format_options: FormatOptions::default(),
4993        };
4994        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
4995        assert_eq!(
4996            err.to_string(),
4997            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
4998        );
4999    }
5000
5001    #[test]
5002    fn test_cast_string_format_yyyymmdd_to_date32() {
5003        let a0 = Arc::new(StringViewArray::from(vec![
5004            Some("2020-12-25"),
5005            Some("20201117"),
5006        ])) as ArrayRef;
5007        let a1 = Arc::new(StringArray::from(vec![
5008            Some("2020-12-25"),
5009            Some("20201117"),
5010        ])) as ArrayRef;
5011        let a2 = Arc::new(LargeStringArray::from(vec![
5012            Some("2020-12-25"),
5013            Some("20201117"),
5014        ])) as ArrayRef;
5015
5016        for array in &[a0, a1, a2] {
5017            let to_type = DataType::Date32;
5018            let options = CastOptions {
5019                safe: false,
5020                format_options: FormatOptions::default(),
5021            };
5022            let result = cast_with_options(&array, &to_type, &options).unwrap();
5023            let c = result.as_primitive::<Date32Type>();
5024            assert_eq!(
5025                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5026                c.value_as_date(0)
5027            );
5028            assert_eq!(
5029                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5030                c.value_as_date(1)
5031            );
5032        }
5033    }
5034
5035    #[test]
5036    fn test_cast_string_to_time32second() {
5037        let a0 = Arc::new(StringViewArray::from(vec![
5038            Some("08:08:35.091323414"),
5039            Some("08:08:60.091323414"), // leap second
5040            Some("08:08:61.091323414"), // not valid
5041            Some("Not a valid time"),
5042            None,
5043        ])) as ArrayRef;
5044        let a1 = Arc::new(StringArray::from(vec![
5045            Some("08:08:35.091323414"),
5046            Some("08:08:60.091323414"), // leap second
5047            Some("08:08:61.091323414"), // not valid
5048            Some("Not a valid time"),
5049            None,
5050        ])) as ArrayRef;
5051        let a2 = Arc::new(LargeStringArray::from(vec![
5052            Some("08:08:35.091323414"),
5053            Some("08:08:60.091323414"), // leap second
5054            Some("08:08:61.091323414"), // not valid
5055            Some("Not a valid time"),
5056            None,
5057        ])) as ArrayRef;
5058        for array in &[a0, a1, a2] {
5059            let to_type = DataType::Time32(TimeUnit::Second);
5060            let b = cast(array, &to_type).unwrap();
5061            let c = b.as_primitive::<Time32SecondType>();
5062            assert_eq!(29315, c.value(0));
5063            assert_eq!(29340, c.value(1));
5064            assert!(c.is_null(2));
5065            assert!(c.is_null(3));
5066            assert!(c.is_null(4));
5067
5068            let options = CastOptions {
5069                safe: false,
5070                format_options: FormatOptions::default(),
5071            };
5072            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5073            assert_eq!(
5074                err.to_string(),
5075                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5076            );
5077        }
5078    }
5079
5080    #[test]
5081    fn test_cast_string_to_time32millisecond() {
5082        let a0 = Arc::new(StringViewArray::from(vec![
5083            Some("08:08:35.091323414"),
5084            Some("08:08:60.091323414"), // leap second
5085            Some("08:08:61.091323414"), // not valid
5086            Some("Not a valid time"),
5087            None,
5088        ])) as ArrayRef;
5089        let a1 = Arc::new(StringArray::from(vec![
5090            Some("08:08:35.091323414"),
5091            Some("08:08:60.091323414"), // leap second
5092            Some("08:08:61.091323414"), // not valid
5093            Some("Not a valid time"),
5094            None,
5095        ])) as ArrayRef;
5096        let a2 = Arc::new(LargeStringArray::from(vec![
5097            Some("08:08:35.091323414"),
5098            Some("08:08:60.091323414"), // leap second
5099            Some("08:08:61.091323414"), // not valid
5100            Some("Not a valid time"),
5101            None,
5102        ])) as ArrayRef;
5103        for array in &[a0, a1, a2] {
5104            let to_type = DataType::Time32(TimeUnit::Millisecond);
5105            let b = cast(array, &to_type).unwrap();
5106            let c = b.as_primitive::<Time32MillisecondType>();
5107            assert_eq!(29315091, c.value(0));
5108            assert_eq!(29340091, c.value(1));
5109            assert!(c.is_null(2));
5110            assert!(c.is_null(3));
5111            assert!(c.is_null(4));
5112
5113            let options = CastOptions {
5114                safe: false,
5115                format_options: FormatOptions::default(),
5116            };
5117            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5118            assert_eq!(
5119                err.to_string(),
5120                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5121            );
5122        }
5123    }
5124
5125    #[test]
5126    fn test_cast_string_to_time64microsecond() {
5127        let a0 = Arc::new(StringViewArray::from(vec![
5128            Some("08:08:35.091323414"),
5129            Some("Not a valid time"),
5130            None,
5131        ])) as ArrayRef;
5132        let a1 = Arc::new(StringArray::from(vec![
5133            Some("08:08:35.091323414"),
5134            Some("Not a valid time"),
5135            None,
5136        ])) as ArrayRef;
5137        let a2 = Arc::new(LargeStringArray::from(vec![
5138            Some("08:08:35.091323414"),
5139            Some("Not a valid time"),
5140            None,
5141        ])) as ArrayRef;
5142        for array in &[a0, a1, a2] {
5143            let to_type = DataType::Time64(TimeUnit::Microsecond);
5144            let b = cast(array, &to_type).unwrap();
5145            let c = b.as_primitive::<Time64MicrosecondType>();
5146            assert_eq!(29315091323, c.value(0));
5147            assert!(c.is_null(1));
5148            assert!(c.is_null(2));
5149
5150            let options = CastOptions {
5151                safe: false,
5152                format_options: FormatOptions::default(),
5153            };
5154            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5155            assert_eq!(
5156                err.to_string(),
5157                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5158            );
5159        }
5160    }
5161
5162    #[test]
5163    fn test_cast_string_to_time64nanosecond() {
5164        let a0 = Arc::new(StringViewArray::from(vec![
5165            Some("08:08:35.091323414"),
5166            Some("Not a valid time"),
5167            None,
5168        ])) as ArrayRef;
5169        let a1 = Arc::new(StringArray::from(vec![
5170            Some("08:08:35.091323414"),
5171            Some("Not a valid time"),
5172            None,
5173        ])) as ArrayRef;
5174        let a2 = Arc::new(LargeStringArray::from(vec![
5175            Some("08:08:35.091323414"),
5176            Some("Not a valid time"),
5177            None,
5178        ])) as ArrayRef;
5179        for array in &[a0, a1, a2] {
5180            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5181            let b = cast(array, &to_type).unwrap();
5182            let c = b.as_primitive::<Time64NanosecondType>();
5183            assert_eq!(29315091323414, c.value(0));
5184            assert!(c.is_null(1));
5185            assert!(c.is_null(2));
5186
5187            let options = CastOptions {
5188                safe: false,
5189                format_options: FormatOptions::default(),
5190            };
5191            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5192            assert_eq!(
5193                err.to_string(),
5194                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5195            );
5196        }
5197    }
5198
5199    #[test]
5200    fn test_cast_string_to_date64() {
5201        let a0 = Arc::new(StringViewArray::from(vec![
5202            Some("2020-09-08T12:00:00"),
5203            Some("Not a valid date"),
5204            None,
5205        ])) as ArrayRef;
5206        let a1 = Arc::new(StringArray::from(vec![
5207            Some("2020-09-08T12:00:00"),
5208            Some("Not a valid date"),
5209            None,
5210        ])) as ArrayRef;
5211        let a2 = Arc::new(LargeStringArray::from(vec![
5212            Some("2020-09-08T12:00:00"),
5213            Some("Not a valid date"),
5214            None,
5215        ])) as ArrayRef;
5216        for array in &[a0, a1, a2] {
5217            let to_type = DataType::Date64;
5218            let b = cast(array, &to_type).unwrap();
5219            let c = b.as_primitive::<Date64Type>();
5220            assert_eq!(1599566400000, c.value(0));
5221            assert!(c.is_null(1));
5222            assert!(c.is_null(2));
5223
5224            let options = CastOptions {
5225                safe: false,
5226                format_options: FormatOptions::default(),
5227            };
5228            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5229            assert_eq!(
5230                err.to_string(),
5231                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5232            );
5233        }
5234    }
5235
5236    macro_rules! test_safe_string_to_interval {
5237        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5238            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5239
5240            let options = CastOptions {
5241                safe: true,
5242                format_options: FormatOptions::default(),
5243            };
5244
5245            let target_interval_array = cast_with_options(
5246                &source_string_array.clone(),
5247                &DataType::Interval($interval_unit),
5248                &options,
5249            )
5250            .unwrap()
5251            .as_any()
5252            .downcast_ref::<$array_ty>()
5253            .unwrap()
5254            .clone() as $array_ty;
5255
5256            let target_string_array =
5257                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5258                    .unwrap()
5259                    .as_any()
5260                    .downcast_ref::<StringArray>()
5261                    .unwrap()
5262                    .clone();
5263
5264            let expect_string_array = StringArray::from($expect_vec);
5265
5266            assert_eq!(target_string_array, expect_string_array);
5267
5268            let target_large_string_array =
5269                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5270                    .unwrap()
5271                    .as_any()
5272                    .downcast_ref::<LargeStringArray>()
5273                    .unwrap()
5274                    .clone();
5275
5276            let expect_large_string_array = LargeStringArray::from($expect_vec);
5277
5278            assert_eq!(target_large_string_array, expect_large_string_array);
5279        };
5280    }
5281
5282    #[test]
5283    fn test_cast_string_to_interval_year_month() {
5284        test_safe_string_to_interval!(
5285            vec![
5286                Some("1 year 1 month"),
5287                Some("1.5 years 13 month"),
5288                Some("30 days"),
5289                Some("31 days"),
5290                Some("2 months 31 days"),
5291                Some("2 months 31 days 1 second"),
5292                Some("foobar"),
5293            ],
5294            IntervalUnit::YearMonth,
5295            IntervalYearMonthArray,
5296            vec![
5297                Some("1 years 1 mons"),
5298                Some("2 years 7 mons"),
5299                None,
5300                None,
5301                None,
5302                None,
5303                None,
5304            ]
5305        );
5306    }
5307
5308    #[test]
5309    fn test_cast_string_to_interval_day_time() {
5310        test_safe_string_to_interval!(
5311            vec![
5312                Some("1 year 1 month"),
5313                Some("1.5 years 13 month"),
5314                Some("30 days"),
5315                Some("1 day 2 second 3.5 milliseconds"),
5316                Some("foobar"),
5317            ],
5318            IntervalUnit::DayTime,
5319            IntervalDayTimeArray,
5320            vec![
5321                Some("390 days"),
5322                Some("930 days"),
5323                Some("30 days"),
5324                None,
5325                None,
5326            ]
5327        );
5328    }
5329
5330    #[test]
5331    fn test_cast_string_to_interval_month_day_nano() {
5332        test_safe_string_to_interval!(
5333            vec![
5334                Some("1 year 1 month 1 day"),
5335                None,
5336                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5337                Some("3 days"),
5338                Some("8 seconds"),
5339                None,
5340                Some("1 day 29800 milliseconds"),
5341                Some("3 months 1 second"),
5342                Some("6 minutes 120 second"),
5343                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5344                Some("foobar"),
5345            ],
5346            IntervalUnit::MonthDayNano,
5347            IntervalMonthDayNanoArray,
5348            vec![
5349                Some("13 mons 1 days"),
5350                None,
5351                Some("31 mons 35 days 0.001400000 secs"),
5352                Some("3 days"),
5353                Some("8.000000000 secs"),
5354                None,
5355                Some("1 days 29.800000000 secs"),
5356                Some("3 mons 1.000000000 secs"),
5357                Some("8 mins"),
5358                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5359                None,
5360            ]
5361        );
5362    }
5363
5364    macro_rules! test_unsafe_string_to_interval_err {
5365        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5366            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5367            let options = CastOptions {
5368                safe: false,
5369                format_options: FormatOptions::default(),
5370            };
5371            let arrow_err = cast_with_options(
5372                &string_array.clone(),
5373                &DataType::Interval($interval_unit),
5374                &options,
5375            )
5376            .unwrap_err();
5377            assert_eq!($error_msg, arrow_err.to_string());
5378        };
5379    }
5380
5381    #[test]
5382    fn test_cast_string_to_interval_err() {
5383        test_unsafe_string_to_interval_err!(
5384            vec![Some("foobar")],
5385            IntervalUnit::YearMonth,
5386            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5387        );
5388        test_unsafe_string_to_interval_err!(
5389            vec![Some("foobar")],
5390            IntervalUnit::DayTime,
5391            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5392        );
5393        test_unsafe_string_to_interval_err!(
5394            vec![Some("foobar")],
5395            IntervalUnit::MonthDayNano,
5396            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5397        );
5398        test_unsafe_string_to_interval_err!(
5399            vec![Some("2 months 31 days 1 second")],
5400            IntervalUnit::YearMonth,
5401            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5402        );
5403        test_unsafe_string_to_interval_err!(
5404            vec![Some("1 day 1.5 milliseconds")],
5405            IntervalUnit::DayTime,
5406            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5407        );
5408
5409        // overflow
5410        test_unsafe_string_to_interval_err!(
5411            vec![Some(format!(
5412                "{} century {} year {} month",
5413                i64::MAX - 2,
5414                i64::MAX - 2,
5415                i64::MAX - 2
5416            ))],
5417            IntervalUnit::DayTime,
5418            format!(
5419                "Arithmetic overflow: Overflow happened on: {} * 100",
5420                i64::MAX - 2
5421            )
5422        );
5423        test_unsafe_string_to_interval_err!(
5424            vec![Some(format!(
5425                "{} year {} month {} day",
5426                i64::MAX - 2,
5427                i64::MAX - 2,
5428                i64::MAX - 2
5429            ))],
5430            IntervalUnit::MonthDayNano,
5431            format!(
5432                "Arithmetic overflow: Overflow happened on: {} * 12",
5433                i64::MAX - 2
5434            )
5435        );
5436    }
5437
5438    #[test]
5439    fn test_cast_binary_to_fixed_size_binary() {
5440        let bytes_1 = "Hiiii".as_bytes();
5441        let bytes_2 = "Hello".as_bytes();
5442
5443        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5444        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5445        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5446
5447        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5448        let down_cast = array_ref
5449            .as_any()
5450            .downcast_ref::<FixedSizeBinaryArray>()
5451            .unwrap();
5452        assert_eq!(bytes_1, down_cast.value(0));
5453        assert_eq!(bytes_2, down_cast.value(1));
5454        assert!(down_cast.is_null(2));
5455
5456        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5457        let down_cast = array_ref
5458            .as_any()
5459            .downcast_ref::<FixedSizeBinaryArray>()
5460            .unwrap();
5461        assert_eq!(bytes_1, down_cast.value(0));
5462        assert_eq!(bytes_2, down_cast.value(1));
5463        assert!(down_cast.is_null(2));
5464
5465        // test error cases when the length of binary are not same
5466        let bytes_1 = "Hi".as_bytes();
5467        let bytes_2 = "Hello".as_bytes();
5468
5469        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5470        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5471        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5472
5473        let array_ref = cast_with_options(
5474            &a1,
5475            &DataType::FixedSizeBinary(5),
5476            &CastOptions {
5477                safe: false,
5478                format_options: FormatOptions::default(),
5479            },
5480        );
5481        assert!(array_ref.is_err());
5482
5483        let array_ref = cast_with_options(
5484            &a2,
5485            &DataType::FixedSizeBinary(5),
5486            &CastOptions {
5487                safe: false,
5488                format_options: FormatOptions::default(),
5489            },
5490        );
5491        assert!(array_ref.is_err());
5492    }
5493
5494    #[test]
5495    fn test_fixed_size_binary_to_binary() {
5496        let bytes_1 = "Hiiii".as_bytes();
5497        let bytes_2 = "Hello".as_bytes();
5498
5499        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5500        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5501
5502        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5503        let down_cast = array_ref.as_binary::<i32>();
5504        assert_eq!(bytes_1, down_cast.value(0));
5505        assert_eq!(bytes_2, down_cast.value(1));
5506        assert!(down_cast.is_null(2));
5507
5508        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5509        let down_cast = array_ref.as_binary::<i64>();
5510        assert_eq!(bytes_1, down_cast.value(0));
5511        assert_eq!(bytes_2, down_cast.value(1));
5512        assert!(down_cast.is_null(2));
5513
5514        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5515        let down_cast = array_ref.as_binary_view();
5516        assert_eq!(bytes_1, down_cast.value(0));
5517        assert_eq!(bytes_2, down_cast.value(1));
5518        assert!(down_cast.is_null(2));
5519    }
5520
5521    #[test]
5522    fn test_fixed_size_binary_to_dictionary() {
5523        let bytes_1 = "Hiiii".as_bytes();
5524        let bytes_2 = "Hello".as_bytes();
5525
5526        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5527        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5528
5529        let cast_type = DataType::Dictionary(
5530            Box::new(DataType::Int8),
5531            Box::new(DataType::FixedSizeBinary(5)),
5532        );
5533        let cast_array = cast(&a1, &cast_type).unwrap();
5534        assert_eq!(cast_array.data_type(), &cast_type);
5535        assert_eq!(
5536            array_to_strings(&cast_array),
5537            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5538        );
5539        // dictionary should only have two distinct values
5540        let dict_array = cast_array
5541            .as_any()
5542            .downcast_ref::<DictionaryArray<Int8Type>>()
5543            .unwrap();
5544        assert_eq!(dict_array.values().len(), 2);
5545    }
5546
5547    #[test]
5548    fn test_binary_to_dictionary() {
5549        let mut builder = GenericBinaryBuilder::<i32>::new();
5550        builder.append_value(b"hello");
5551        builder.append_value(b"hiiii");
5552        builder.append_value(b"hiiii"); // duplicate
5553        builder.append_null();
5554        builder.append_value(b"rustt");
5555
5556        let a1 = builder.finish();
5557
5558        let cast_type = DataType::Dictionary(
5559            Box::new(DataType::Int8),
5560            Box::new(DataType::FixedSizeBinary(5)),
5561        );
5562        let cast_array = cast(&a1, &cast_type).unwrap();
5563        assert_eq!(cast_array.data_type(), &cast_type);
5564        assert_eq!(
5565            array_to_strings(&cast_array),
5566            vec![
5567                "68656c6c6f",
5568                "6869696969",
5569                "6869696969",
5570                "null",
5571                "7275737474"
5572            ]
5573        );
5574        // dictionary should only have three distinct values
5575        let dict_array = cast_array
5576            .as_any()
5577            .downcast_ref::<DictionaryArray<Int8Type>>()
5578            .unwrap();
5579        assert_eq!(dict_array.values().len(), 3);
5580    }
5581
5582    #[test]
5583    fn test_numeric_to_binary() {
5584        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5585
5586        let array_ref = cast(&a, &DataType::Binary).unwrap();
5587        let down_cast = array_ref.as_binary::<i32>();
5588        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5589        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5590        assert!(down_cast.is_null(2));
5591
5592        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5593
5594        let array_ref = cast(&a, &DataType::Binary).unwrap();
5595        let down_cast = array_ref.as_binary::<i32>();
5596        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5597        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5598        assert!(down_cast.is_null(2));
5599    }
5600
5601    #[test]
5602    fn test_numeric_to_large_binary() {
5603        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5604
5605        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5606        let down_cast = array_ref.as_binary::<i64>();
5607        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5608        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5609        assert!(down_cast.is_null(2));
5610
5611        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5612
5613        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5614        let down_cast = array_ref.as_binary::<i64>();
5615        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5616        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5617        assert!(down_cast.is_null(2));
5618    }
5619
5620    #[test]
5621    fn test_cast_date32_to_int32() {
5622        let array = Date32Array::from(vec![10000, 17890]);
5623        let b = cast(&array, &DataType::Int32).unwrap();
5624        let c = b.as_primitive::<Int32Type>();
5625        assert_eq!(10000, c.value(0));
5626        assert_eq!(17890, c.value(1));
5627    }
5628
5629    #[test]
5630    fn test_cast_int32_to_date32() {
5631        let array = Int32Array::from(vec![10000, 17890]);
5632        let b = cast(&array, &DataType::Date32).unwrap();
5633        let c = b.as_primitive::<Date32Type>();
5634        assert_eq!(10000, c.value(0));
5635        assert_eq!(17890, c.value(1));
5636    }
5637
5638    #[test]
5639    fn test_cast_timestamp_to_date32() {
5640        let array =
5641            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5642                .with_timezone("+00:00".to_string());
5643        let b = cast(&array, &DataType::Date32).unwrap();
5644        let c = b.as_primitive::<Date32Type>();
5645        assert_eq!(10000, c.value(0));
5646        assert_eq!(17890, c.value(1));
5647        assert!(c.is_null(2));
5648    }
5649    #[test]
5650    fn test_cast_timestamp_to_date32_zone() {
5651        let strings = StringArray::from_iter([
5652            Some("1970-01-01T00:00:01"),
5653            Some("1970-01-01T23:59:59"),
5654            None,
5655            Some("2020-03-01T02:00:23+00:00"),
5656        ]);
5657        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
5658        let timestamps = cast(&strings, &dt).unwrap();
5659        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
5660
5661        let c = dates.as_primitive::<Date32Type>();
5662        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
5663        assert_eq!(c.value_as_date(0).unwrap(), expected);
5664        assert_eq!(c.value_as_date(1).unwrap(), expected);
5665        assert!(c.is_null(2));
5666        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
5667        assert_eq!(c.value_as_date(3).unwrap(), expected);
5668    }
5669    #[test]
5670    fn test_cast_timestamp_to_date64() {
5671        let array =
5672            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
5673        let b = cast(&array, &DataType::Date64).unwrap();
5674        let c = b.as_primitive::<Date64Type>();
5675        assert_eq!(864000000005, c.value(0));
5676        assert_eq!(1545696000001, c.value(1));
5677        assert!(c.is_null(2));
5678
5679        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
5680        let b = cast(&array, &DataType::Date64).unwrap();
5681        let c = b.as_primitive::<Date64Type>();
5682        assert_eq!(864000000005000, c.value(0));
5683        assert_eq!(1545696000001000, c.value(1));
5684
5685        // test overflow, safe cast
5686        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5687        let b = cast(&array, &DataType::Date64).unwrap();
5688        assert!(b.is_null(0));
5689        // test overflow, unsafe cast
5690        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5691        let options = CastOptions {
5692            safe: false,
5693            format_options: FormatOptions::default(),
5694        };
5695        let b = cast_with_options(&array, &DataType::Date64, &options);
5696        assert!(b.is_err());
5697    }
5698
5699    #[test]
5700    fn test_cast_timestamp_to_time64() {
5701        // test timestamp secs
5702        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5703            .with_timezone("+01:00".to_string());
5704        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5705        let c = b.as_primitive::<Time64MicrosecondType>();
5706        assert_eq!(3605000000, c.value(0));
5707        assert_eq!(3601000000, c.value(1));
5708        assert!(c.is_null(2));
5709        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5710        let c = b.as_primitive::<Time64NanosecondType>();
5711        assert_eq!(3605000000000, c.value(0));
5712        assert_eq!(3601000000000, c.value(1));
5713        assert!(c.is_null(2));
5714
5715        // test timestamp milliseconds
5716        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5717            .with_timezone("+01:00".to_string());
5718        let array = Arc::new(a) as ArrayRef;
5719        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5720        let c = b.as_primitive::<Time64MicrosecondType>();
5721        assert_eq!(3605000000, c.value(0));
5722        assert_eq!(3601000000, c.value(1));
5723        assert!(c.is_null(2));
5724        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5725        let c = b.as_primitive::<Time64NanosecondType>();
5726        assert_eq!(3605000000000, c.value(0));
5727        assert_eq!(3601000000000, c.value(1));
5728        assert!(c.is_null(2));
5729
5730        // test timestamp microseconds
5731        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5732            .with_timezone("+01:00".to_string());
5733        let array = Arc::new(a) as ArrayRef;
5734        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5735        let c = b.as_primitive::<Time64MicrosecondType>();
5736        assert_eq!(3605000000, c.value(0));
5737        assert_eq!(3601000000, c.value(1));
5738        assert!(c.is_null(2));
5739        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5740        let c = b.as_primitive::<Time64NanosecondType>();
5741        assert_eq!(3605000000000, c.value(0));
5742        assert_eq!(3601000000000, c.value(1));
5743        assert!(c.is_null(2));
5744
5745        // test timestamp nanoseconds
5746        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5747            .with_timezone("+01:00".to_string());
5748        let array = Arc::new(a) as ArrayRef;
5749        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5750        let c = b.as_primitive::<Time64MicrosecondType>();
5751        assert_eq!(3605000000, c.value(0));
5752        assert_eq!(3601000000, c.value(1));
5753        assert!(c.is_null(2));
5754        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5755        let c = b.as_primitive::<Time64NanosecondType>();
5756        assert_eq!(3605000000000, c.value(0));
5757        assert_eq!(3601000000000, c.value(1));
5758        assert!(c.is_null(2));
5759
5760        // test overflow
5761        let a =
5762            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5763        let array = Arc::new(a) as ArrayRef;
5764        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5765        assert!(b.is_err());
5766        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5767        assert!(b.is_err());
5768        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5769        assert!(b.is_err());
5770    }
5771
5772    #[test]
5773    fn test_cast_timestamp_to_time32() {
5774        // test timestamp secs
5775        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5776            .with_timezone("+01:00".to_string());
5777        let array = Arc::new(a) as ArrayRef;
5778        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5779        let c = b.as_primitive::<Time32SecondType>();
5780        assert_eq!(3605, c.value(0));
5781        assert_eq!(3601, c.value(1));
5782        assert!(c.is_null(2));
5783        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5784        let c = b.as_primitive::<Time32MillisecondType>();
5785        assert_eq!(3605000, c.value(0));
5786        assert_eq!(3601000, c.value(1));
5787        assert!(c.is_null(2));
5788
5789        // test timestamp milliseconds
5790        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5791            .with_timezone("+01:00".to_string());
5792        let array = Arc::new(a) as ArrayRef;
5793        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5794        let c = b.as_primitive::<Time32SecondType>();
5795        assert_eq!(3605, c.value(0));
5796        assert_eq!(3601, c.value(1));
5797        assert!(c.is_null(2));
5798        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5799        let c = b.as_primitive::<Time32MillisecondType>();
5800        assert_eq!(3605000, c.value(0));
5801        assert_eq!(3601000, c.value(1));
5802        assert!(c.is_null(2));
5803
5804        // test timestamp microseconds
5805        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5806            .with_timezone("+01:00".to_string());
5807        let array = Arc::new(a) as ArrayRef;
5808        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5809        let c = b.as_primitive::<Time32SecondType>();
5810        assert_eq!(3605, c.value(0));
5811        assert_eq!(3601, c.value(1));
5812        assert!(c.is_null(2));
5813        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5814        let c = b.as_primitive::<Time32MillisecondType>();
5815        assert_eq!(3605000, c.value(0));
5816        assert_eq!(3601000, c.value(1));
5817        assert!(c.is_null(2));
5818
5819        // test timestamp nanoseconds
5820        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5821            .with_timezone("+01:00".to_string());
5822        let array = Arc::new(a) as ArrayRef;
5823        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5824        let c = b.as_primitive::<Time32SecondType>();
5825        assert_eq!(3605, c.value(0));
5826        assert_eq!(3601, c.value(1));
5827        assert!(c.is_null(2));
5828        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5829        let c = b.as_primitive::<Time32MillisecondType>();
5830        assert_eq!(3605000, c.value(0));
5831        assert_eq!(3601000, c.value(1));
5832        assert!(c.is_null(2));
5833
5834        // test overflow
5835        let a =
5836            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5837        let array = Arc::new(a) as ArrayRef;
5838        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5839        assert!(b.is_err());
5840        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5841        assert!(b.is_err());
5842    }
5843
5844    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
5845    #[test]
5846    fn test_cast_timestamp_with_timezone_1() {
5847        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5848            Some("2000-01-01T00:00:00.123456789"),
5849            Some("2010-01-01T00:00:00.123456789"),
5850            None,
5851        ]));
5852        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5853        let timestamp_array = cast(&string_array, &to_type).unwrap();
5854
5855        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5856        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5857
5858        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5859        let result = string_array.as_string::<i32>();
5860        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
5861        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5862        assert!(result.is_null(2));
5863    }
5864
5865    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
5866    #[test]
5867    fn test_cast_timestamp_with_timezone_2() {
5868        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5869            Some("2000-01-01T07:00:00.123456789"),
5870            Some("2010-01-01T07:00:00.123456789"),
5871            None,
5872        ]));
5873        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
5874        let timestamp_array = cast(&string_array, &to_type).unwrap();
5875
5876        // Check intermediate representation is correct
5877        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5878        let result = string_array.as_string::<i32>();
5879        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
5880        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5881        assert!(result.is_null(2));
5882
5883        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5884        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5885
5886        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5887        let result = string_array.as_string::<i32>();
5888        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
5889        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5890        assert!(result.is_null(2));
5891    }
5892
5893    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
5894    #[test]
5895    fn test_cast_timestamp_with_timezone_3() {
5896        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5897            Some("2000-01-01T07:00:00.123456789"),
5898            Some("2010-01-01T07:00:00.123456789"),
5899            None,
5900        ]));
5901        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5902        let timestamp_array = cast(&string_array, &to_type).unwrap();
5903
5904        // Check intermediate representation is correct
5905        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5906        let result = string_array.as_string::<i32>();
5907        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5908        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5909        assert!(result.is_null(2));
5910
5911        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5912        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5913
5914        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5915        let result = string_array.as_string::<i32>();
5916        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5917        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5918        assert!(result.is_null(2));
5919    }
5920
5921    #[test]
5922    fn test_cast_date64_to_timestamp() {
5923        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5924        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5925        let c = b.as_primitive::<TimestampSecondType>();
5926        assert_eq!(864000000, c.value(0));
5927        assert_eq!(1545696000, c.value(1));
5928        assert!(c.is_null(2));
5929    }
5930
5931    #[test]
5932    fn test_cast_date64_to_timestamp_ms() {
5933        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5934        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
5935        let c = b
5936            .as_any()
5937            .downcast_ref::<TimestampMillisecondArray>()
5938            .unwrap();
5939        assert_eq!(864000000005, c.value(0));
5940        assert_eq!(1545696000001, c.value(1));
5941        assert!(c.is_null(2));
5942    }
5943
5944    #[test]
5945    fn test_cast_date64_to_timestamp_us() {
5946        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5947        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5948        let c = b
5949            .as_any()
5950            .downcast_ref::<TimestampMicrosecondArray>()
5951            .unwrap();
5952        assert_eq!(864000000005000, c.value(0));
5953        assert_eq!(1545696000001000, c.value(1));
5954        assert!(c.is_null(2));
5955    }
5956
5957    #[test]
5958    fn test_cast_date64_to_timestamp_ns() {
5959        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5960        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
5961        let c = b
5962            .as_any()
5963            .downcast_ref::<TimestampNanosecondArray>()
5964            .unwrap();
5965        assert_eq!(864000000005000000, c.value(0));
5966        assert_eq!(1545696000001000000, c.value(1));
5967        assert!(c.is_null(2));
5968    }
5969
5970    #[test]
5971    fn test_cast_timestamp_to_i64() {
5972        let array =
5973            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5974                .with_timezone("UTC".to_string());
5975        let b = cast(&array, &DataType::Int64).unwrap();
5976        let c = b.as_primitive::<Int64Type>();
5977        assert_eq!(&DataType::Int64, c.data_type());
5978        assert_eq!(864000000005, c.value(0));
5979        assert_eq!(1545696000001, c.value(1));
5980        assert!(c.is_null(2));
5981    }
5982
5983    macro_rules! assert_cast {
5984        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
5985            assert!(can_cast_types($array.data_type(), &$datatype));
5986            let out = cast(&$array, &$datatype).unwrap();
5987            let actual = out
5988                .as_any()
5989                .downcast_ref::<$output_array_type>()
5990                .unwrap()
5991                .into_iter()
5992                .collect::<Vec<_>>();
5993            assert_eq!(actual, $expected);
5994        }};
5995        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
5996            assert!(can_cast_types($array.data_type(), &$datatype));
5997            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
5998            let actual = out
5999                .as_any()
6000                .downcast_ref::<$output_array_type>()
6001                .unwrap()
6002                .into_iter()
6003                .collect::<Vec<_>>();
6004            assert_eq!(actual, $expected);
6005        }};
6006    }
6007
6008    #[test]
6009    fn test_cast_date32_to_string() {
6010        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6011        let expected = vec![
6012            Some("1970-01-01"),
6013            Some("1997-05-19"),
6014            Some("2005-09-10"),
6015            Some("2018-12-25"),
6016            None,
6017        ];
6018
6019        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6020        assert_cast!(array, DataType::Utf8, StringArray, expected);
6021        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6022    }
6023
6024    #[test]
6025    fn test_cast_date64_to_string() {
6026        let array = Date64Array::from(vec![
6027            Some(0),
6028            Some(10000 * 86400000),
6029            Some(13036 * 86400000),
6030            Some(17890 * 86400000),
6031            None,
6032        ]);
6033        let expected = vec![
6034            Some("1970-01-01T00:00:00"),
6035            Some("1997-05-19T00:00:00"),
6036            Some("2005-09-10T00:00:00"),
6037            Some("2018-12-25T00:00:00"),
6038            None,
6039        ];
6040
6041        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6042        assert_cast!(array, DataType::Utf8, StringArray, expected);
6043        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6044    }
6045
6046    #[test]
6047    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6048        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6049        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6050        let array = Arc::new(a) as ArrayRef;
6051
6052        let b = cast(
6053            &array,
6054            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6055        )
6056        .unwrap();
6057        let c = b.as_primitive::<TimestampSecondType>();
6058        let string_array = cast(&c, &DataType::Utf8).unwrap();
6059        let result = string_array.as_string::<i32>();
6060        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6061
6062        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6063        let c = b.as_primitive::<TimestampSecondType>();
6064        let string_array = cast(&c, &DataType::Utf8).unwrap();
6065        let result = string_array.as_string::<i32>();
6066        assert_eq!("2021-01-01T00:00:00", result.value(0));
6067    }
6068
6069    #[test]
6070    fn test_cast_date32_to_timestamp_with_timezone() {
6071        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6072        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6073        let array = Arc::new(a) as ArrayRef;
6074        let b = cast(
6075            &array,
6076            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6077        )
6078        .unwrap();
6079        let c = b.as_primitive::<TimestampSecondType>();
6080        assert_eq!(1609438500, c.value(0));
6081        assert_eq!(1640974500, c.value(1));
6082        assert!(c.is_null(2));
6083
6084        let string_array = cast(&c, &DataType::Utf8).unwrap();
6085        let result = string_array.as_string::<i32>();
6086        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6087        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6088    }
6089
6090    #[test]
6091    fn test_cast_date32_to_timestamp_with_timezone_ms() {
6092        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6093        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6094        let array = Arc::new(a) as ArrayRef;
6095        let b = cast(
6096            &array,
6097            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6098        )
6099        .unwrap();
6100        let c = b.as_primitive::<TimestampMillisecondType>();
6101        assert_eq!(1609438500000, c.value(0));
6102        assert_eq!(1640974500000, c.value(1));
6103        assert!(c.is_null(2));
6104
6105        let string_array = cast(&c, &DataType::Utf8).unwrap();
6106        let result = string_array.as_string::<i32>();
6107        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6108        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6109    }
6110
6111    #[test]
6112    fn test_cast_date32_to_timestamp_with_timezone_us() {
6113        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6114        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6115        let array = Arc::new(a) as ArrayRef;
6116        let b = cast(
6117            &array,
6118            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6119        )
6120        .unwrap();
6121        let c = b.as_primitive::<TimestampMicrosecondType>();
6122        assert_eq!(1609438500000000, c.value(0));
6123        assert_eq!(1640974500000000, c.value(1));
6124        assert!(c.is_null(2));
6125
6126        let string_array = cast(&c, &DataType::Utf8).unwrap();
6127        let result = string_array.as_string::<i32>();
6128        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6129        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6130    }
6131
6132    #[test]
6133    fn test_cast_date32_to_timestamp_with_timezone_ns() {
6134        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6135        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6136        let array = Arc::new(a) as ArrayRef;
6137        let b = cast(
6138            &array,
6139            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6140        )
6141        .unwrap();
6142        let c = b.as_primitive::<TimestampNanosecondType>();
6143        assert_eq!(1609438500000000000, c.value(0));
6144        assert_eq!(1640974500000000000, c.value(1));
6145        assert!(c.is_null(2));
6146
6147        let string_array = cast(&c, &DataType::Utf8).unwrap();
6148        let result = string_array.as_string::<i32>();
6149        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6150        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6151    }
6152
6153    #[test]
6154    fn test_cast_date64_to_timestamp_with_timezone() {
6155        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6156        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6157        let b = cast(
6158            &array,
6159            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6160        )
6161        .unwrap();
6162
6163        let c = b.as_primitive::<TimestampSecondType>();
6164        assert_eq!(863979300, c.value(0));
6165        assert_eq!(1545675300, c.value(1));
6166        assert!(c.is_null(2));
6167
6168        let string_array = cast(&c, &DataType::Utf8).unwrap();
6169        let result = string_array.as_string::<i32>();
6170        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
6171        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6172    }
6173
6174    #[test]
6175    fn test_cast_date64_to_timestamp_with_timezone_ms() {
6176        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6177        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6178        let b = cast(
6179            &array,
6180            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6181        )
6182        .unwrap();
6183
6184        let c = b.as_primitive::<TimestampMillisecondType>();
6185        assert_eq!(863979300005, c.value(0));
6186        assert_eq!(1545675300001, c.value(1));
6187        assert!(c.is_null(2));
6188
6189        let string_array = cast(&c, &DataType::Utf8).unwrap();
6190        let result = string_array.as_string::<i32>();
6191        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6192        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6193    }
6194
6195    #[test]
6196    fn test_cast_date64_to_timestamp_with_timezone_us() {
6197        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6198        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6199        let b = cast(
6200            &array,
6201            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6202        )
6203        .unwrap();
6204
6205        let c = b.as_primitive::<TimestampMicrosecondType>();
6206        assert_eq!(863979300005000, c.value(0));
6207        assert_eq!(1545675300001000, c.value(1));
6208        assert!(c.is_null(2));
6209
6210        let string_array = cast(&c, &DataType::Utf8).unwrap();
6211        let result = string_array.as_string::<i32>();
6212        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6213        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6214    }
6215
6216    #[test]
6217    fn test_cast_date64_to_timestamp_with_timezone_ns() {
6218        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6219        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6220        let b = cast(
6221            &array,
6222            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6223        )
6224        .unwrap();
6225
6226        let c = b.as_primitive::<TimestampNanosecondType>();
6227        assert_eq!(863979300005000000, c.value(0));
6228        assert_eq!(1545675300001000000, c.value(1));
6229        assert!(c.is_null(2));
6230
6231        let string_array = cast(&c, &DataType::Utf8).unwrap();
6232        let result = string_array.as_string::<i32>();
6233        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6234        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6235    }
6236
6237    #[test]
6238    fn test_cast_timestamp_to_strings() {
6239        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6240        let array =
6241            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6242        let expected = vec![
6243            Some("1997-05-19T00:00:03.005"),
6244            Some("2018-12-25T00:00:02.001"),
6245            None,
6246        ];
6247
6248        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6249        assert_cast!(array, DataType::Utf8, StringArray, expected);
6250        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6251    }
6252
6253    #[test]
6254    fn test_cast_timestamp_to_strings_opt() {
6255        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6256        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6257        let cast_options = CastOptions {
6258            safe: true,
6259            format_options: FormatOptions::default()
6260                .with_timestamp_format(Some(ts_format))
6261                .with_timestamp_tz_format(Some(ts_format)),
6262        };
6263
6264        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6265        let array_without_tz =
6266            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6267        let expected = vec![
6268            Some("1997-05-19 00:00:03.005000"),
6269            Some("2018-12-25 00:00:02.001000"),
6270            None,
6271        ];
6272        assert_cast!(
6273            array_without_tz,
6274            DataType::Utf8View,
6275            StringViewArray,
6276            cast_options,
6277            expected
6278        );
6279        assert_cast!(
6280            array_without_tz,
6281            DataType::Utf8,
6282            StringArray,
6283            cast_options,
6284            expected
6285        );
6286        assert_cast!(
6287            array_without_tz,
6288            DataType::LargeUtf8,
6289            LargeStringArray,
6290            cast_options,
6291            expected
6292        );
6293
6294        let array_with_tz =
6295            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6296                .with_timezone(tz.to_string());
6297        let expected = vec![
6298            Some("1997-05-19 05:45:03.005000"),
6299            Some("2018-12-25 05:45:02.001000"),
6300            None,
6301        ];
6302        assert_cast!(
6303            array_with_tz,
6304            DataType::Utf8View,
6305            StringViewArray,
6306            cast_options,
6307            expected
6308        );
6309        assert_cast!(
6310            array_with_tz,
6311            DataType::Utf8,
6312            StringArray,
6313            cast_options,
6314            expected
6315        );
6316        assert_cast!(
6317            array_with_tz,
6318            DataType::LargeUtf8,
6319            LargeStringArray,
6320            cast_options,
6321            expected
6322        );
6323    }
6324
6325    #[test]
6326    fn test_cast_between_timestamps() {
6327        let array =
6328            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6329        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6330        let c = b.as_primitive::<TimestampSecondType>();
6331        assert_eq!(864000003, c.value(0));
6332        assert_eq!(1545696002, c.value(1));
6333        assert!(c.is_null(2));
6334    }
6335
6336    #[test]
6337    fn test_cast_duration_to_i64() {
6338        let base = vec![5, 6, 7, 8, 100000000];
6339
6340        let duration_arrays = vec![
6341            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6342            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6343            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6344            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6345        ];
6346
6347        for arr in duration_arrays {
6348            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6349            let result = cast(&arr, &DataType::Int64).unwrap();
6350            let result = result.as_primitive::<Int64Type>();
6351            assert_eq!(base.as_slice(), result.values());
6352        }
6353    }
6354
6355    #[test]
6356    fn test_cast_between_durations_and_numerics() {
6357        fn test_cast_between_durations<FromType, ToType>()
6358        where
6359            FromType: ArrowPrimitiveType<Native = i64>,
6360            ToType: ArrowPrimitiveType<Native = i64>,
6361            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6362        {
6363            let from_unit = match FromType::DATA_TYPE {
6364                DataType::Duration(unit) => unit,
6365                _ => panic!("Expected a duration type"),
6366            };
6367            let to_unit = match ToType::DATA_TYPE {
6368                DataType::Duration(unit) => unit,
6369                _ => panic!("Expected a duration type"),
6370            };
6371            let from_size = time_unit_multiple(&from_unit);
6372            let to_size = time_unit_multiple(&to_unit);
6373
6374            let (v1_before, v2_before) = (8640003005, 1696002001);
6375            let (v1_after, v2_after) = if from_size >= to_size {
6376                (
6377                    v1_before / (from_size / to_size),
6378                    v2_before / (from_size / to_size),
6379                )
6380            } else {
6381                (
6382                    v1_before * (to_size / from_size),
6383                    v2_before * (to_size / from_size),
6384                )
6385            };
6386
6387            let array =
6388                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6389            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6390            let c = b.as_primitive::<ToType>();
6391            assert_eq!(v1_after, c.value(0));
6392            assert_eq!(v2_after, c.value(1));
6393            assert!(c.is_null(2));
6394        }
6395
6396        // between each individual duration type
6397        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6398        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6399        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6400        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6401        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6402        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6403        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6404        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6405        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6406        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6407        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6408        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6409
6410        // cast failed
6411        let array = DurationSecondArray::from(vec![
6412            Some(i64::MAX),
6413            Some(8640203410378005),
6414            Some(10241096),
6415            None,
6416        ]);
6417        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6418        let c = b.as_primitive::<DurationNanosecondType>();
6419        assert!(c.is_null(0));
6420        assert!(c.is_null(1));
6421        assert_eq!(10241096000000000, c.value(2));
6422        assert!(c.is_null(3));
6423
6424        // durations to numerics
6425        let array = DurationSecondArray::from(vec![
6426            Some(i64::MAX),
6427            Some(8640203410378005),
6428            Some(10241096),
6429            None,
6430        ]);
6431        let b = cast(&array, &DataType::Int64).unwrap();
6432        let c = b.as_primitive::<Int64Type>();
6433        assert_eq!(i64::MAX, c.value(0));
6434        assert_eq!(8640203410378005, c.value(1));
6435        assert_eq!(10241096, c.value(2));
6436        assert!(c.is_null(3));
6437
6438        let b = cast(&array, &DataType::Int32).unwrap();
6439        let c = b.as_primitive::<Int32Type>();
6440        assert_eq!(0, c.value(0));
6441        assert_eq!(0, c.value(1));
6442        assert_eq!(10241096, c.value(2));
6443        assert!(c.is_null(3));
6444
6445        // numerics to durations
6446        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6447        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6448        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6449        assert_eq!(i32::MAX as i64, c.value(0));
6450        assert_eq!(802034103, c.value(1));
6451        assert_eq!(10241096, c.value(2));
6452        assert!(c.is_null(3));
6453    }
6454
6455    #[test]
6456    fn test_cast_to_strings() {
6457        let a = Int32Array::from(vec![1, 2, 3]);
6458        let out = cast(&a, &DataType::Utf8).unwrap();
6459        let out = out
6460            .as_any()
6461            .downcast_ref::<StringArray>()
6462            .unwrap()
6463            .into_iter()
6464            .collect::<Vec<_>>();
6465        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6466        let out = cast(&a, &DataType::LargeUtf8).unwrap();
6467        let out = out
6468            .as_any()
6469            .downcast_ref::<LargeStringArray>()
6470            .unwrap()
6471            .into_iter()
6472            .collect::<Vec<_>>();
6473        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6474    }
6475
6476    #[test]
6477    fn test_str_to_str_casts() {
6478        for data in [
6479            vec![Some("foo"), Some("bar"), Some("ham")],
6480            vec![Some("foo"), None, Some("bar")],
6481        ] {
6482            let a = LargeStringArray::from(data.clone());
6483            let to = cast(&a, &DataType::Utf8).unwrap();
6484            let expect = a
6485                .as_any()
6486                .downcast_ref::<LargeStringArray>()
6487                .unwrap()
6488                .into_iter()
6489                .collect::<Vec<_>>();
6490            let out = to
6491                .as_any()
6492                .downcast_ref::<StringArray>()
6493                .unwrap()
6494                .into_iter()
6495                .collect::<Vec<_>>();
6496            assert_eq!(expect, out);
6497
6498            let a = StringArray::from(data);
6499            let to = cast(&a, &DataType::LargeUtf8).unwrap();
6500            let expect = a
6501                .as_any()
6502                .downcast_ref::<StringArray>()
6503                .unwrap()
6504                .into_iter()
6505                .collect::<Vec<_>>();
6506            let out = to
6507                .as_any()
6508                .downcast_ref::<LargeStringArray>()
6509                .unwrap()
6510                .into_iter()
6511                .collect::<Vec<_>>();
6512            assert_eq!(expect, out);
6513        }
6514    }
6515
6516    const VIEW_TEST_DATA: [Option<&str>; 5] = [
6517        Some("hello"),
6518        Some("repeated"),
6519        None,
6520        Some("large payload over 12 bytes"),
6521        Some("repeated"),
6522    ];
6523
6524    #[test]
6525    fn test_string_view_to_binary_view() {
6526        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6527
6528        assert!(can_cast_types(
6529            string_view_array.data_type(),
6530            &DataType::BinaryView
6531        ));
6532
6533        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
6534        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6535
6536        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6537        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6538    }
6539
6540    #[test]
6541    fn test_binary_view_to_string_view() {
6542        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6543
6544        assert!(can_cast_types(
6545            binary_view_array.data_type(),
6546            &DataType::Utf8View
6547        ));
6548
6549        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
6550        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6551
6552        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6553        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6554    }
6555
6556    #[test]
6557    fn test_binary_view_to_string_view_with_invalid_utf8() {
6558        let binary_view_array = BinaryViewArray::from_iter(vec![
6559            Some("valid".as_bytes()),
6560            Some(&[0xff]),
6561            Some("utf8".as_bytes()),
6562            None,
6563        ]);
6564
6565        let strict_options = CastOptions {
6566            safe: false,
6567            ..Default::default()
6568        };
6569
6570        assert!(
6571            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
6572        );
6573
6574        let safe_options = CastOptions {
6575            safe: true,
6576            ..Default::default()
6577        };
6578
6579        let string_view_array =
6580            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
6581        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6582
6583        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
6584
6585        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
6586    }
6587
6588    #[test]
6589    fn test_string_to_view() {
6590        _test_string_to_view::<i32>();
6591        _test_string_to_view::<i64>();
6592    }
6593
6594    fn _test_string_to_view<O>()
6595    where
6596        O: OffsetSizeTrait,
6597    {
6598        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6599
6600        assert!(can_cast_types(
6601            string_array.data_type(),
6602            &DataType::Utf8View
6603        ));
6604
6605        assert!(can_cast_types(
6606            string_array.data_type(),
6607            &DataType::BinaryView
6608        ));
6609
6610        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
6611        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6612
6613        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
6614        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6615
6616        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6617        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6618
6619        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6620        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6621    }
6622
6623    #[test]
6624    fn test_bianry_to_view() {
6625        _test_binary_to_view::<i32>();
6626        _test_binary_to_view::<i64>();
6627    }
6628
6629    fn _test_binary_to_view<O>()
6630    where
6631        O: OffsetSizeTrait,
6632    {
6633        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6634
6635        assert!(can_cast_types(
6636            binary_array.data_type(),
6637            &DataType::Utf8View
6638        ));
6639
6640        assert!(can_cast_types(
6641            binary_array.data_type(),
6642            &DataType::BinaryView
6643        ));
6644
6645        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
6646        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6647
6648        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
6649        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6650
6651        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6652        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6653
6654        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6655        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6656    }
6657
6658    #[test]
6659    fn test_dict_to_view() {
6660        let values = StringArray::from_iter(VIEW_TEST_DATA);
6661        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
6662        let string_dict_array =
6663            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
6664        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
6665
6666        let string_view_array = {
6667            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6668            for v in typed_dict.into_iter() {
6669                builder.append_option(v);
6670            }
6671            builder.finish()
6672        };
6673        let expected_string_array_type = string_view_array.data_type();
6674        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
6675        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
6676        assert_eq!(casted_string_array.as_ref(), &string_view_array);
6677
6678        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
6679        let binary_dict_array =
6680            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
6681        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
6682
6683        let binary_view_array = {
6684            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6685            for v in typed_binary_dict.into_iter() {
6686                builder.append_option(v);
6687            }
6688            builder.finish()
6689        };
6690        let expected_binary_array_type = binary_view_array.data_type();
6691        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
6692        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
6693        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
6694    }
6695
6696    #[test]
6697    fn test_view_to_dict() {
6698        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6699        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
6700        let casted_type = string_dict_array.data_type();
6701        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
6702        assert_eq!(casted_dict_array.data_type(), casted_type);
6703        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
6704
6705        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6706        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
6707        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
6708        let binary_dict_array =
6709            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
6710        let casted_type = binary_dict_array.data_type();
6711        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
6712        assert_eq!(casted_binary_array.data_type(), casted_type);
6713        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
6714    }
6715
6716    #[test]
6717    fn test_view_to_string() {
6718        _test_view_to_string::<i32>();
6719        _test_view_to_string::<i64>();
6720    }
6721
6722    fn _test_view_to_string<O>()
6723    where
6724        O: OffsetSizeTrait,
6725    {
6726        let string_view_array = {
6727            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6728            for s in VIEW_TEST_DATA.iter() {
6729                builder.append_option(*s);
6730            }
6731            builder.finish()
6732        };
6733
6734        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6735
6736        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6737        let expected_type = expected_string_array.data_type();
6738
6739        assert!(can_cast_types(string_view_array.data_type(), expected_type));
6740        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
6741
6742        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
6743        assert_eq!(string_view_casted_array.data_type(), expected_type);
6744        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
6745
6746        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
6747        assert_eq!(binary_view_casted_array.data_type(), expected_type);
6748        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
6749    }
6750
6751    #[test]
6752    fn test_view_to_binary() {
6753        _test_view_to_binary::<i32>();
6754        _test_view_to_binary::<i64>();
6755    }
6756
6757    fn _test_view_to_binary<O>()
6758    where
6759        O: OffsetSizeTrait,
6760    {
6761        let view_array = {
6762            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6763            for s in VIEW_TEST_DATA.iter() {
6764                builder.append_option(*s);
6765            }
6766            builder.finish()
6767        };
6768
6769        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6770        let expected_type = expected_binary_array.data_type();
6771
6772        assert!(can_cast_types(view_array.data_type(), expected_type));
6773
6774        let binary_array = cast(&view_array, expected_type).unwrap();
6775        assert_eq!(binary_array.data_type(), expected_type);
6776
6777        assert_eq!(binary_array.as_ref(), &expected_binary_array);
6778    }
6779
6780    #[test]
6781    fn test_cast_from_f64() {
6782        let f64_values: Vec<f64> = vec![
6783            i64::MIN as f64,
6784            i32::MIN as f64,
6785            i16::MIN as f64,
6786            i8::MIN as f64,
6787            0_f64,
6788            u8::MAX as f64,
6789            u16::MAX as f64,
6790            u32::MAX as f64,
6791            u64::MAX as f64,
6792        ];
6793        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6794
6795        let f64_expected = vec![
6796            -9223372036854776000.0,
6797            -2147483648.0,
6798            -32768.0,
6799            -128.0,
6800            0.0,
6801            255.0,
6802            65535.0,
6803            4294967295.0,
6804            18446744073709552000.0,
6805        ];
6806        assert_eq!(
6807            f64_expected,
6808            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6809                .iter()
6810                .map(|i| i.parse::<f64>().unwrap())
6811                .collect::<Vec<f64>>()
6812        );
6813
6814        let f32_expected = vec![
6815            -9223372000000000000.0,
6816            -2147483600.0,
6817            -32768.0,
6818            -128.0,
6819            0.0,
6820            255.0,
6821            65535.0,
6822            4294967300.0,
6823            18446744000000000000.0,
6824        ];
6825        assert_eq!(
6826            f32_expected,
6827            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6828                .iter()
6829                .map(|i| i.parse::<f32>().unwrap())
6830                .collect::<Vec<f32>>()
6831        );
6832
6833        let f16_expected = vec![
6834            f16::from_f64(-9223372000000000000.0),
6835            f16::from_f64(-2147483600.0),
6836            f16::from_f64(-32768.0),
6837            f16::from_f64(-128.0),
6838            f16::from_f64(0.0),
6839            f16::from_f64(255.0),
6840            f16::from_f64(65535.0),
6841            f16::from_f64(4294967300.0),
6842            f16::from_f64(18446744000000000000.0),
6843        ];
6844        assert_eq!(
6845            f16_expected,
6846            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6847                .iter()
6848                .map(|i| i.parse::<f16>().unwrap())
6849                .collect::<Vec<f16>>()
6850        );
6851
6852        let i64_expected = vec![
6853            "-9223372036854775808",
6854            "-2147483648",
6855            "-32768",
6856            "-128",
6857            "0",
6858            "255",
6859            "65535",
6860            "4294967295",
6861            "null",
6862        ];
6863        assert_eq!(
6864            i64_expected,
6865            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
6866        );
6867
6868        let i32_expected = vec![
6869            "null",
6870            "-2147483648",
6871            "-32768",
6872            "-128",
6873            "0",
6874            "255",
6875            "65535",
6876            "null",
6877            "null",
6878        ];
6879        assert_eq!(
6880            i32_expected,
6881            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
6882        );
6883
6884        let i16_expected = vec![
6885            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6886        ];
6887        assert_eq!(
6888            i16_expected,
6889            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
6890        );
6891
6892        let i8_expected = vec![
6893            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6894        ];
6895        assert_eq!(
6896            i8_expected,
6897            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
6898        );
6899
6900        let u64_expected = vec![
6901            "null",
6902            "null",
6903            "null",
6904            "null",
6905            "0",
6906            "255",
6907            "65535",
6908            "4294967295",
6909            "null",
6910        ];
6911        assert_eq!(
6912            u64_expected,
6913            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
6914        );
6915
6916        let u32_expected = vec![
6917            "null",
6918            "null",
6919            "null",
6920            "null",
6921            "0",
6922            "255",
6923            "65535",
6924            "4294967295",
6925            "null",
6926        ];
6927        assert_eq!(
6928            u32_expected,
6929            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
6930        );
6931
6932        let u16_expected = vec![
6933            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6934        ];
6935        assert_eq!(
6936            u16_expected,
6937            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
6938        );
6939
6940        let u8_expected = vec![
6941            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6942        ];
6943        assert_eq!(
6944            u8_expected,
6945            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
6946        );
6947    }
6948
6949    #[test]
6950    fn test_cast_from_f32() {
6951        let f32_values: Vec<f32> = vec![
6952            i32::MIN as f32,
6953            i32::MIN as f32,
6954            i16::MIN as f32,
6955            i8::MIN as f32,
6956            0_f32,
6957            u8::MAX as f32,
6958            u16::MAX as f32,
6959            u32::MAX as f32,
6960            u32::MAX as f32,
6961        ];
6962        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
6963
6964        let f64_expected = vec![
6965            "-2147483648.0",
6966            "-2147483648.0",
6967            "-32768.0",
6968            "-128.0",
6969            "0.0",
6970            "255.0",
6971            "65535.0",
6972            "4294967296.0",
6973            "4294967296.0",
6974        ];
6975        assert_eq!(
6976            f64_expected,
6977            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
6978        );
6979
6980        let f32_expected = vec![
6981            "-2147483600.0",
6982            "-2147483600.0",
6983            "-32768.0",
6984            "-128.0",
6985            "0.0",
6986            "255.0",
6987            "65535.0",
6988            "4294967300.0",
6989            "4294967300.0",
6990        ];
6991        assert_eq!(
6992            f32_expected,
6993            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
6994        );
6995
6996        let f16_expected = vec![
6997            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
6998        ];
6999        assert_eq!(
7000            f16_expected,
7001            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7002        );
7003
7004        let i64_expected = vec![
7005            "-2147483648",
7006            "-2147483648",
7007            "-32768",
7008            "-128",
7009            "0",
7010            "255",
7011            "65535",
7012            "4294967296",
7013            "4294967296",
7014        ];
7015        assert_eq!(
7016            i64_expected,
7017            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7018        );
7019
7020        let i32_expected = vec![
7021            "-2147483648",
7022            "-2147483648",
7023            "-32768",
7024            "-128",
7025            "0",
7026            "255",
7027            "65535",
7028            "null",
7029            "null",
7030        ];
7031        assert_eq!(
7032            i32_expected,
7033            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7034        );
7035
7036        let i16_expected = vec![
7037            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7038        ];
7039        assert_eq!(
7040            i16_expected,
7041            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7042        );
7043
7044        let i8_expected = vec![
7045            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7046        ];
7047        assert_eq!(
7048            i8_expected,
7049            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7050        );
7051
7052        let u64_expected = vec![
7053            "null",
7054            "null",
7055            "null",
7056            "null",
7057            "0",
7058            "255",
7059            "65535",
7060            "4294967296",
7061            "4294967296",
7062        ];
7063        assert_eq!(
7064            u64_expected,
7065            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7066        );
7067
7068        let u32_expected = vec![
7069            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7070        ];
7071        assert_eq!(
7072            u32_expected,
7073            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7074        );
7075
7076        let u16_expected = vec![
7077            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7078        ];
7079        assert_eq!(
7080            u16_expected,
7081            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7082        );
7083
7084        let u8_expected = vec![
7085            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7086        ];
7087        assert_eq!(
7088            u8_expected,
7089            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7090        );
7091    }
7092
7093    #[test]
7094    fn test_cast_from_uint64() {
7095        let u64_values: Vec<u64> = vec![
7096            0,
7097            u8::MAX as u64,
7098            u16::MAX as u64,
7099            u32::MAX as u64,
7100            u64::MAX,
7101        ];
7102        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7103
7104        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7105        assert_eq!(
7106            f64_expected,
7107            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7108                .iter()
7109                .map(|i| i.parse::<f64>().unwrap())
7110                .collect::<Vec<f64>>()
7111        );
7112
7113        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7114        assert_eq!(
7115            f32_expected,
7116            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7117                .iter()
7118                .map(|i| i.parse::<f32>().unwrap())
7119                .collect::<Vec<f32>>()
7120        );
7121
7122        let f16_expected = vec![
7123            f16::from_f64(0.0),
7124            f16::from_f64(255.0),
7125            f16::from_f64(65535.0),
7126            f16::from_f64(4294967300.0),
7127            f16::from_f64(18446744000000000000.0),
7128        ];
7129        assert_eq!(
7130            f16_expected,
7131            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7132                .iter()
7133                .map(|i| i.parse::<f16>().unwrap())
7134                .collect::<Vec<f16>>()
7135        );
7136
7137        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7138        assert_eq!(
7139            i64_expected,
7140            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7141        );
7142
7143        let i32_expected = vec!["0", "255", "65535", "null", "null"];
7144        assert_eq!(
7145            i32_expected,
7146            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7147        );
7148
7149        let i16_expected = vec!["0", "255", "null", "null", "null"];
7150        assert_eq!(
7151            i16_expected,
7152            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7153        );
7154
7155        let i8_expected = vec!["0", "null", "null", "null", "null"];
7156        assert_eq!(
7157            i8_expected,
7158            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7159        );
7160
7161        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7162        assert_eq!(
7163            u64_expected,
7164            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7165        );
7166
7167        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7168        assert_eq!(
7169            u32_expected,
7170            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7171        );
7172
7173        let u16_expected = vec!["0", "255", "65535", "null", "null"];
7174        assert_eq!(
7175            u16_expected,
7176            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7177        );
7178
7179        let u8_expected = vec!["0", "255", "null", "null", "null"];
7180        assert_eq!(
7181            u8_expected,
7182            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7183        );
7184    }
7185
7186    #[test]
7187    fn test_cast_from_uint32() {
7188        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7189        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7190
7191        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7192        assert_eq!(
7193            f64_expected,
7194            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7195        );
7196
7197        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7198        assert_eq!(
7199            f32_expected,
7200            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7201        );
7202
7203        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7204        assert_eq!(
7205            f16_expected,
7206            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7207        );
7208
7209        let i64_expected = vec!["0", "255", "65535", "4294967295"];
7210        assert_eq!(
7211            i64_expected,
7212            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7213        );
7214
7215        let i32_expected = vec!["0", "255", "65535", "null"];
7216        assert_eq!(
7217            i32_expected,
7218            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7219        );
7220
7221        let i16_expected = vec!["0", "255", "null", "null"];
7222        assert_eq!(
7223            i16_expected,
7224            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7225        );
7226
7227        let i8_expected = vec!["0", "null", "null", "null"];
7228        assert_eq!(
7229            i8_expected,
7230            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7231        );
7232
7233        let u64_expected = vec!["0", "255", "65535", "4294967295"];
7234        assert_eq!(
7235            u64_expected,
7236            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7237        );
7238
7239        let u32_expected = vec!["0", "255", "65535", "4294967295"];
7240        assert_eq!(
7241            u32_expected,
7242            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7243        );
7244
7245        let u16_expected = vec!["0", "255", "65535", "null"];
7246        assert_eq!(
7247            u16_expected,
7248            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7249        );
7250
7251        let u8_expected = vec!["0", "255", "null", "null"];
7252        assert_eq!(
7253            u8_expected,
7254            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7255        );
7256    }
7257
7258    #[test]
7259    fn test_cast_from_uint16() {
7260        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7261        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7262
7263        let f64_expected = vec!["0.0", "255.0", "65535.0"];
7264        assert_eq!(
7265            f64_expected,
7266            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7267        );
7268
7269        let f32_expected = vec!["0.0", "255.0", "65535.0"];
7270        assert_eq!(
7271            f32_expected,
7272            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7273        );
7274
7275        let f16_expected = vec!["0.0", "255.0", "inf"];
7276        assert_eq!(
7277            f16_expected,
7278            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7279        );
7280
7281        let i64_expected = vec!["0", "255", "65535"];
7282        assert_eq!(
7283            i64_expected,
7284            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7285        );
7286
7287        let i32_expected = vec!["0", "255", "65535"];
7288        assert_eq!(
7289            i32_expected,
7290            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7291        );
7292
7293        let i16_expected = vec!["0", "255", "null"];
7294        assert_eq!(
7295            i16_expected,
7296            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7297        );
7298
7299        let i8_expected = vec!["0", "null", "null"];
7300        assert_eq!(
7301            i8_expected,
7302            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7303        );
7304
7305        let u64_expected = vec!["0", "255", "65535"];
7306        assert_eq!(
7307            u64_expected,
7308            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7309        );
7310
7311        let u32_expected = vec!["0", "255", "65535"];
7312        assert_eq!(
7313            u32_expected,
7314            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7315        );
7316
7317        let u16_expected = vec!["0", "255", "65535"];
7318        assert_eq!(
7319            u16_expected,
7320            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7321        );
7322
7323        let u8_expected = vec!["0", "255", "null"];
7324        assert_eq!(
7325            u8_expected,
7326            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7327        );
7328    }
7329
7330    #[test]
7331    fn test_cast_from_uint8() {
7332        let u8_values: Vec<u8> = vec![0, u8::MAX];
7333        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7334
7335        let f64_expected = vec!["0.0", "255.0"];
7336        assert_eq!(
7337            f64_expected,
7338            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7339        );
7340
7341        let f32_expected = vec!["0.0", "255.0"];
7342        assert_eq!(
7343            f32_expected,
7344            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7345        );
7346
7347        let f16_expected = vec!["0.0", "255.0"];
7348        assert_eq!(
7349            f16_expected,
7350            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7351        );
7352
7353        let i64_expected = vec!["0", "255"];
7354        assert_eq!(
7355            i64_expected,
7356            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7357        );
7358
7359        let i32_expected = vec!["0", "255"];
7360        assert_eq!(
7361            i32_expected,
7362            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7363        );
7364
7365        let i16_expected = vec!["0", "255"];
7366        assert_eq!(
7367            i16_expected,
7368            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7369        );
7370
7371        let i8_expected = vec!["0", "null"];
7372        assert_eq!(
7373            i8_expected,
7374            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7375        );
7376
7377        let u64_expected = vec!["0", "255"];
7378        assert_eq!(
7379            u64_expected,
7380            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7381        );
7382
7383        let u32_expected = vec!["0", "255"];
7384        assert_eq!(
7385            u32_expected,
7386            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7387        );
7388
7389        let u16_expected = vec!["0", "255"];
7390        assert_eq!(
7391            u16_expected,
7392            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7393        );
7394
7395        let u8_expected = vec!["0", "255"];
7396        assert_eq!(
7397            u8_expected,
7398            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7399        );
7400    }
7401
7402    #[test]
7403    fn test_cast_from_int64() {
7404        let i64_values: Vec<i64> = vec![
7405            i64::MIN,
7406            i32::MIN as i64,
7407            i16::MIN as i64,
7408            i8::MIN as i64,
7409            0,
7410            i8::MAX as i64,
7411            i16::MAX as i64,
7412            i32::MAX as i64,
7413            i64::MAX,
7414        ];
7415        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7416
7417        let f64_expected = vec![
7418            -9223372036854776000.0,
7419            -2147483648.0,
7420            -32768.0,
7421            -128.0,
7422            0.0,
7423            127.0,
7424            32767.0,
7425            2147483647.0,
7426            9223372036854776000.0,
7427        ];
7428        assert_eq!(
7429            f64_expected,
7430            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7431                .iter()
7432                .map(|i| i.parse::<f64>().unwrap())
7433                .collect::<Vec<f64>>()
7434        );
7435
7436        let f32_expected = vec![
7437            -9223372000000000000.0,
7438            -2147483600.0,
7439            -32768.0,
7440            -128.0,
7441            0.0,
7442            127.0,
7443            32767.0,
7444            2147483600.0,
7445            9223372000000000000.0,
7446        ];
7447        assert_eq!(
7448            f32_expected,
7449            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7450                .iter()
7451                .map(|i| i.parse::<f32>().unwrap())
7452                .collect::<Vec<f32>>()
7453        );
7454
7455        let f16_expected = vec![
7456            f16::from_f64(-9223372000000000000.0),
7457            f16::from_f64(-2147483600.0),
7458            f16::from_f64(-32768.0),
7459            f16::from_f64(-128.0),
7460            f16::from_f64(0.0),
7461            f16::from_f64(127.0),
7462            f16::from_f64(32767.0),
7463            f16::from_f64(2147483600.0),
7464            f16::from_f64(9223372000000000000.0),
7465        ];
7466        assert_eq!(
7467            f16_expected,
7468            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7469                .iter()
7470                .map(|i| i.parse::<f16>().unwrap())
7471                .collect::<Vec<f16>>()
7472        );
7473
7474        let i64_expected = vec![
7475            "-9223372036854775808",
7476            "-2147483648",
7477            "-32768",
7478            "-128",
7479            "0",
7480            "127",
7481            "32767",
7482            "2147483647",
7483            "9223372036854775807",
7484        ];
7485        assert_eq!(
7486            i64_expected,
7487            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
7488        );
7489
7490        let i32_expected = vec![
7491            "null",
7492            "-2147483648",
7493            "-32768",
7494            "-128",
7495            "0",
7496            "127",
7497            "32767",
7498            "2147483647",
7499            "null",
7500        ];
7501        assert_eq!(
7502            i32_expected,
7503            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
7504        );
7505
7506        assert_eq!(
7507            i32_expected,
7508            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
7509        );
7510
7511        let i16_expected = vec![
7512            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
7513        ];
7514        assert_eq!(
7515            i16_expected,
7516            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
7517        );
7518
7519        let i8_expected = vec![
7520            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
7521        ];
7522        assert_eq!(
7523            i8_expected,
7524            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
7525        );
7526
7527        let u64_expected = vec![
7528            "null",
7529            "null",
7530            "null",
7531            "null",
7532            "0",
7533            "127",
7534            "32767",
7535            "2147483647",
7536            "9223372036854775807",
7537        ];
7538        assert_eq!(
7539            u64_expected,
7540            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
7541        );
7542
7543        let u32_expected = vec![
7544            "null",
7545            "null",
7546            "null",
7547            "null",
7548            "0",
7549            "127",
7550            "32767",
7551            "2147483647",
7552            "null",
7553        ];
7554        assert_eq!(
7555            u32_expected,
7556            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
7557        );
7558
7559        let u16_expected = vec![
7560            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
7561        ];
7562        assert_eq!(
7563            u16_expected,
7564            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
7565        );
7566
7567        let u8_expected = vec![
7568            "null", "null", "null", "null", "0", "127", "null", "null", "null",
7569        ];
7570        assert_eq!(
7571            u8_expected,
7572            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
7573        );
7574    }
7575
7576    #[test]
7577    fn test_cast_from_int32() {
7578        let i32_values: Vec<i32> = vec![
7579            i32::MIN,
7580            i16::MIN as i32,
7581            i8::MIN as i32,
7582            0,
7583            i8::MAX as i32,
7584            i16::MAX as i32,
7585            i32::MAX,
7586        ];
7587        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
7588
7589        let f64_expected = vec![
7590            "-2147483648.0",
7591            "-32768.0",
7592            "-128.0",
7593            "0.0",
7594            "127.0",
7595            "32767.0",
7596            "2147483647.0",
7597        ];
7598        assert_eq!(
7599            f64_expected,
7600            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
7601        );
7602
7603        let f32_expected = vec![
7604            "-2147483600.0",
7605            "-32768.0",
7606            "-128.0",
7607            "0.0",
7608            "127.0",
7609            "32767.0",
7610            "2147483600.0",
7611        ];
7612        assert_eq!(
7613            f32_expected,
7614            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
7615        );
7616
7617        let f16_expected = vec![
7618            f16::from_f64(-2147483600.0),
7619            f16::from_f64(-32768.0),
7620            f16::from_f64(-128.0),
7621            f16::from_f64(0.0),
7622            f16::from_f64(127.0),
7623            f16::from_f64(32767.0),
7624            f16::from_f64(2147483600.0),
7625        ];
7626        assert_eq!(
7627            f16_expected,
7628            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
7629                .iter()
7630                .map(|i| i.parse::<f16>().unwrap())
7631                .collect::<Vec<f16>>()
7632        );
7633
7634        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
7635        assert_eq!(
7636            i16_expected,
7637            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
7638        );
7639
7640        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
7641        assert_eq!(
7642            i8_expected,
7643            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
7644        );
7645
7646        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7647        assert_eq!(
7648            u64_expected,
7649            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
7650        );
7651
7652        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7653        assert_eq!(
7654            u32_expected,
7655            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
7656        );
7657
7658        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
7659        assert_eq!(
7660            u16_expected,
7661            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
7662        );
7663
7664        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
7665        assert_eq!(
7666            u8_expected,
7667            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
7668        );
7669
7670        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
7671        let i64_expected = vec![
7672            "-185542587187200000",
7673            "-2831155200000",
7674            "-11059200000",
7675            "0",
7676            "10972800000",
7677            "2831068800000",
7678            "185542587100800000",
7679        ];
7680        assert_eq!(
7681            i64_expected,
7682            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
7683        );
7684    }
7685
7686    #[test]
7687    fn test_cast_from_int16() {
7688        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
7689        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
7690
7691        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7692        assert_eq!(
7693            f64_expected,
7694            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
7695        );
7696
7697        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7698        assert_eq!(
7699            f32_expected,
7700            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
7701        );
7702
7703        let f16_expected = vec![
7704            f16::from_f64(-32768.0),
7705            f16::from_f64(-128.0),
7706            f16::from_f64(0.0),
7707            f16::from_f64(127.0),
7708            f16::from_f64(32767.0),
7709        ];
7710        assert_eq!(
7711            f16_expected,
7712            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
7713                .iter()
7714                .map(|i| i.parse::<f16>().unwrap())
7715                .collect::<Vec<f16>>()
7716        );
7717
7718        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
7719        assert_eq!(
7720            i64_expected,
7721            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
7722        );
7723
7724        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
7725        assert_eq!(
7726            i32_expected,
7727            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
7728        );
7729
7730        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
7731        assert_eq!(
7732            i16_expected,
7733            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
7734        );
7735
7736        let i8_expected = vec!["null", "-128", "0", "127", "null"];
7737        assert_eq!(
7738            i8_expected,
7739            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
7740        );
7741
7742        let u64_expected = vec!["null", "null", "0", "127", "32767"];
7743        assert_eq!(
7744            u64_expected,
7745            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
7746        );
7747
7748        let u32_expected = vec!["null", "null", "0", "127", "32767"];
7749        assert_eq!(
7750            u32_expected,
7751            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
7752        );
7753
7754        let u16_expected = vec!["null", "null", "0", "127", "32767"];
7755        assert_eq!(
7756            u16_expected,
7757            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
7758        );
7759
7760        let u8_expected = vec!["null", "null", "0", "127", "null"];
7761        assert_eq!(
7762            u8_expected,
7763            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
7764        );
7765    }
7766
7767    #[test]
7768    fn test_cast_from_date32() {
7769        let i32_values: Vec<i32> = vec![
7770            i32::MIN,
7771            i16::MIN as i32,
7772            i8::MIN as i32,
7773            0,
7774            i8::MAX as i32,
7775            i16::MAX as i32,
7776            i32::MAX,
7777        ];
7778        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
7779
7780        let i64_expected = vec![
7781            "-2147483648",
7782            "-32768",
7783            "-128",
7784            "0",
7785            "127",
7786            "32767",
7787            "2147483647",
7788        ];
7789        assert_eq!(
7790            i64_expected,
7791            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7792        );
7793    }
7794
7795    #[test]
7796    fn test_cast_from_int8() {
7797        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7798        let i8_array = Int8Array::from(i8_values);
7799
7800        let f64_expected = vec!["-128.0", "0.0", "127.0"];
7801        assert_eq!(
7802            f64_expected,
7803            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7804        );
7805
7806        let f32_expected = vec!["-128.0", "0.0", "127.0"];
7807        assert_eq!(
7808            f32_expected,
7809            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7810        );
7811
7812        let f16_expected = vec!["-128.0", "0.0", "127.0"];
7813        assert_eq!(
7814            f16_expected,
7815            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7816        );
7817
7818        let i64_expected = vec!["-128", "0", "127"];
7819        assert_eq!(
7820            i64_expected,
7821            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7822        );
7823
7824        let i32_expected = vec!["-128", "0", "127"];
7825        assert_eq!(
7826            i32_expected,
7827            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7828        );
7829
7830        let i16_expected = vec!["-128", "0", "127"];
7831        assert_eq!(
7832            i16_expected,
7833            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7834        );
7835
7836        let i8_expected = vec!["-128", "0", "127"];
7837        assert_eq!(
7838            i8_expected,
7839            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7840        );
7841
7842        let u64_expected = vec!["null", "0", "127"];
7843        assert_eq!(
7844            u64_expected,
7845            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7846        );
7847
7848        let u32_expected = vec!["null", "0", "127"];
7849        assert_eq!(
7850            u32_expected,
7851            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7852        );
7853
7854        let u16_expected = vec!["null", "0", "127"];
7855        assert_eq!(
7856            u16_expected,
7857            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7858        );
7859
7860        let u8_expected = vec!["null", "0", "127"];
7861        assert_eq!(
7862            u8_expected,
7863            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
7864        );
7865    }
7866
7867    /// Convert `array` into a vector of strings by casting to data type dt
7868    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
7869    where
7870        T: ArrowPrimitiveType,
7871    {
7872        let c = cast(array, dt).unwrap();
7873        let a = c.as_primitive::<T>();
7874        let mut v: Vec<String> = vec![];
7875        for i in 0..array.len() {
7876            if a.is_null(i) {
7877                v.push("null".to_string())
7878            } else {
7879                v.push(format!("{:?}", a.value(i)));
7880            }
7881        }
7882        v
7883    }
7884
7885    #[test]
7886    fn test_cast_utf8_dict() {
7887        // FROM a dictionary with of Utf8 values
7888        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
7889        builder.append("one").unwrap();
7890        builder.append_null();
7891        builder.append("three").unwrap();
7892        let array: ArrayRef = Arc::new(builder.finish());
7893
7894        let expected = vec!["one", "null", "three"];
7895
7896        // Test casting TO StringArray
7897        let cast_type = Utf8;
7898        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
7899        assert_eq!(cast_array.data_type(), &cast_type);
7900        assert_eq!(array_to_strings(&cast_array), expected);
7901
7902        // Test casting TO Dictionary (with different index sizes)
7903
7904        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
7905        let cast_array = cast(&array, &cast_type).expect("cast failed");
7906        assert_eq!(cast_array.data_type(), &cast_type);
7907        assert_eq!(array_to_strings(&cast_array), expected);
7908
7909        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
7910        let cast_array = cast(&array, &cast_type).expect("cast failed");
7911        assert_eq!(cast_array.data_type(), &cast_type);
7912        assert_eq!(array_to_strings(&cast_array), expected);
7913
7914        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
7915        let cast_array = cast(&array, &cast_type).expect("cast failed");
7916        assert_eq!(cast_array.data_type(), &cast_type);
7917        assert_eq!(array_to_strings(&cast_array), expected);
7918
7919        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7920        let cast_array = cast(&array, &cast_type).expect("cast failed");
7921        assert_eq!(cast_array.data_type(), &cast_type);
7922        assert_eq!(array_to_strings(&cast_array), expected);
7923
7924        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
7925        let cast_array = cast(&array, &cast_type).expect("cast failed");
7926        assert_eq!(cast_array.data_type(), &cast_type);
7927        assert_eq!(array_to_strings(&cast_array), expected);
7928
7929        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
7930        let cast_array = cast(&array, &cast_type).expect("cast failed");
7931        assert_eq!(cast_array.data_type(), &cast_type);
7932        assert_eq!(array_to_strings(&cast_array), expected);
7933
7934        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
7935        let cast_array = cast(&array, &cast_type).expect("cast failed");
7936        assert_eq!(cast_array.data_type(), &cast_type);
7937        assert_eq!(array_to_strings(&cast_array), expected);
7938    }
7939
7940    #[test]
7941    fn test_cast_dict_to_dict_bad_index_value_primitive() {
7942        // test converting from an array that has indexes of a type
7943        // that are out of bounds for a particular other kind of
7944        // index.
7945
7946        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
7947
7948        // add 200 distinct values (which can be stored by a
7949        // dictionary indexed by int32, but not a dictionary indexed
7950        // with int8)
7951        for i in 0..200 {
7952            builder.append(i).unwrap();
7953        }
7954        let array: ArrayRef = Arc::new(builder.finish());
7955
7956        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7957        let res = cast(&array, &cast_type);
7958        assert!(res.is_err());
7959        let actual_error = format!("{res:?}");
7960        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7961        assert!(
7962            actual_error.contains(expected_error),
7963            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7964        );
7965    }
7966
7967    #[test]
7968    fn test_cast_dict_to_dict_bad_index_value_utf8() {
7969        // Same test as test_cast_dict_to_dict_bad_index_value but use
7970        // string values (and encode the expected behavior here);
7971
7972        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
7973
7974        // add 200 distinct values (which can be stored by a
7975        // dictionary indexed by int32, but not a dictionary indexed
7976        // with int8)
7977        for i in 0..200 {
7978            let val = format!("val{i}");
7979            builder.append(&val).unwrap();
7980        }
7981        let array = builder.finish();
7982
7983        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7984        let res = cast(&array, &cast_type);
7985        assert!(res.is_err());
7986        let actual_error = format!("{res:?}");
7987        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7988        assert!(
7989            actual_error.contains(expected_error),
7990            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7991        );
7992    }
7993
7994    #[test]
7995    fn test_cast_primitive_dict() {
7996        // FROM a dictionary with of INT32 values
7997        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
7998        builder.append(1).unwrap();
7999        builder.append_null();
8000        builder.append(3).unwrap();
8001        let array: ArrayRef = Arc::new(builder.finish());
8002
8003        let expected = vec!["1", "null", "3"];
8004
8005        // Test casting TO PrimitiveArray, different dictionary type
8006        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8007        assert_eq!(array_to_strings(&cast_array), expected);
8008        assert_eq!(cast_array.data_type(), &Utf8);
8009
8010        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8011        assert_eq!(array_to_strings(&cast_array), expected);
8012        assert_eq!(cast_array.data_type(), &Int64);
8013    }
8014
8015    #[test]
8016    fn test_cast_primitive_array_to_dict() {
8017        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8018        builder.append_value(1);
8019        builder.append_null();
8020        builder.append_value(3);
8021        let array: ArrayRef = Arc::new(builder.finish());
8022
8023        let expected = vec!["1", "null", "3"];
8024
8025        // Cast to a dictionary (same value type, Int32)
8026        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8027        let cast_array = cast(&array, &cast_type).expect("cast failed");
8028        assert_eq!(cast_array.data_type(), &cast_type);
8029        assert_eq!(array_to_strings(&cast_array), expected);
8030
8031        // Cast to a dictionary (different value type, Int8)
8032        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8033        let cast_array = cast(&array, &cast_type).expect("cast failed");
8034        assert_eq!(cast_array.data_type(), &cast_type);
8035        assert_eq!(array_to_strings(&cast_array), expected);
8036    }
8037
8038    #[test]
8039    fn test_cast_time_array_to_dict() {
8040        use DataType::*;
8041
8042        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8043
8044        let expected = vec!["1972-09-27", "null", "1975-06-24"];
8045
8046        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8047        let cast_array = cast(&array, &cast_type).expect("cast failed");
8048        assert_eq!(cast_array.data_type(), &cast_type);
8049        assert_eq!(array_to_strings(&cast_array), expected);
8050    }
8051
8052    #[test]
8053    fn test_cast_timestamp_array_to_dict() {
8054        use DataType::*;
8055
8056        let array = Arc::new(
8057            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8058        ) as ArrayRef;
8059
8060        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8061
8062        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8063        let cast_array = cast(&array, &cast_type).expect("cast failed");
8064        assert_eq!(cast_array.data_type(), &cast_type);
8065        assert_eq!(array_to_strings(&cast_array), expected);
8066    }
8067
8068    #[test]
8069    fn test_cast_string_array_to_dict() {
8070        use DataType::*;
8071
8072        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8073
8074        let expected = vec!["one", "null", "three"];
8075
8076        // Cast to a dictionary (same value type, Utf8)
8077        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8078        let cast_array = cast(&array, &cast_type).expect("cast failed");
8079        assert_eq!(cast_array.data_type(), &cast_type);
8080        assert_eq!(array_to_strings(&cast_array), expected);
8081    }
8082
8083    #[test]
8084    fn test_cast_null_array_to_from_decimal_array() {
8085        let data_type = DataType::Decimal128(12, 4);
8086        let array = new_null_array(&DataType::Null, 4);
8087        assert_eq!(array.data_type(), &DataType::Null);
8088        let cast_array = cast(&array, &data_type).expect("cast failed");
8089        assert_eq!(cast_array.data_type(), &data_type);
8090        for i in 0..4 {
8091            assert!(cast_array.is_null(i));
8092        }
8093
8094        let array = new_null_array(&data_type, 4);
8095        assert_eq!(array.data_type(), &data_type);
8096        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8097        assert_eq!(cast_array.data_type(), &DataType::Null);
8098        assert_eq!(cast_array.len(), 4);
8099        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8100    }
8101
8102    #[test]
8103    fn test_cast_null_array_from_and_to_primitive_array() {
8104        macro_rules! typed_test {
8105            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8106                {
8107                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
8108                    let expected = $ARR_TYPE::from(vec![None; 6]);
8109                    let cast_type = DataType::$DATATYPE;
8110                    let cast_array = cast(&array, &cast_type).expect("cast failed");
8111                    let cast_array = cast_array.as_primitive::<$TYPE>();
8112                    assert_eq!(cast_array.data_type(), &cast_type);
8113                    assert_eq!(cast_array, &expected);
8114                }
8115            }};
8116        }
8117
8118        typed_test!(Int16Array, Int16, Int16Type);
8119        typed_test!(Int32Array, Int32, Int32Type);
8120        typed_test!(Int64Array, Int64, Int64Type);
8121
8122        typed_test!(UInt16Array, UInt16, UInt16Type);
8123        typed_test!(UInt32Array, UInt32, UInt32Type);
8124        typed_test!(UInt64Array, UInt64, UInt64Type);
8125
8126        typed_test!(Float16Array, Float16, Float16Type);
8127        typed_test!(Float32Array, Float32, Float32Type);
8128        typed_test!(Float64Array, Float64, Float64Type);
8129
8130        typed_test!(Date32Array, Date32, Date32Type);
8131        typed_test!(Date64Array, Date64, Date64Type);
8132    }
8133
8134    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
8135        // Cast from null to data_type
8136        let array = new_null_array(&DataType::Null, 4);
8137        assert_eq!(array.data_type(), &DataType::Null);
8138        let cast_array = cast(&array, data_type).expect("cast failed");
8139        assert_eq!(cast_array.data_type(), data_type);
8140        for i in 0..4 {
8141            if is_complex {
8142                assert!(cast_array.logical_nulls().unwrap().is_null(i));
8143            } else {
8144                assert!(cast_array.is_null(i));
8145            }
8146        }
8147    }
8148
8149    fn cast_from_null_to_other(data_type: &DataType) {
8150        cast_from_null_to_other_base(data_type, false);
8151    }
8152
8153    fn cast_from_null_to_other_complex(data_type: &DataType) {
8154        cast_from_null_to_other_base(data_type, true);
8155    }
8156
8157    #[test]
8158    fn test_cast_null_from_and_to_variable_sized() {
8159        cast_from_null_to_other(&DataType::Utf8);
8160        cast_from_null_to_other(&DataType::LargeUtf8);
8161        cast_from_null_to_other(&DataType::Binary);
8162        cast_from_null_to_other(&DataType::LargeBinary);
8163    }
8164
8165    #[test]
8166    fn test_cast_null_from_and_to_nested_type() {
8167        // Cast null from and to map
8168        let data_type = DataType::Map(
8169            Arc::new(Field::new_struct(
8170                "entry",
8171                vec![
8172                    Field::new("key", DataType::Utf8, false),
8173                    Field::new("value", DataType::Int32, true),
8174                ],
8175                false,
8176            )),
8177            false,
8178        );
8179        cast_from_null_to_other(&data_type);
8180
8181        // Cast null from and to list
8182        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8183        cast_from_null_to_other(&data_type);
8184        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8185        cast_from_null_to_other(&data_type);
8186        let data_type =
8187            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8188        cast_from_null_to_other(&data_type);
8189
8190        // Cast null from and to dictionary
8191        let values = vec![None, None, None, None] as Vec<Option<&str>>;
8192        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8193        let array = Arc::new(array) as ArrayRef;
8194        let data_type = array.data_type().to_owned();
8195        cast_from_null_to_other(&data_type);
8196
8197        // Cast null from and to struct
8198        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8199        cast_from_null_to_other(&data_type);
8200
8201        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
8202        cast_from_null_to_other(&target_type);
8203
8204        let target_type =
8205            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
8206        cast_from_null_to_other(&target_type);
8207
8208        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
8209        let target_type = DataType::Union(fields, UnionMode::Sparse);
8210        cast_from_null_to_other_complex(&target_type);
8211
8212        let target_type = DataType::RunEndEncoded(
8213            Arc::new(Field::new("item", DataType::Int32, true)),
8214            Arc::new(Field::new("item", DataType::Int32, true)),
8215        );
8216        cast_from_null_to_other_complex(&target_type);
8217    }
8218
8219    /// Print the `DictionaryArray` `array` as a vector of strings
8220    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8221        let options = FormatOptions::new().with_null("null");
8222        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8223        (0..array.len())
8224            .map(|i| formatter.value(i).to_string())
8225            .collect()
8226    }
8227
8228    #[test]
8229    fn test_cast_utf8_to_date32() {
8230        use chrono::NaiveDate;
8231        let from_ymd = chrono::NaiveDate::from_ymd_opt;
8232        let since = chrono::NaiveDate::signed_duration_since;
8233
8234        let a = StringArray::from(vec![
8235            "2000-01-01",          // valid date with leading 0s
8236            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
8237            "2000-2-2",            // valid date without leading 0s
8238            "2000-00-00",          // invalid month and day
8239            "2000",                // just a year is invalid
8240        ]);
8241        let array = Arc::new(a) as ArrayRef;
8242        let b = cast(&array, &DataType::Date32).unwrap();
8243        let c = b.as_primitive::<Date32Type>();
8244
8245        // test valid inputs
8246        let date_value = since(
8247            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8248            from_ymd(1970, 1, 1).unwrap(),
8249        )
8250        .num_days() as i32;
8251        assert!(c.is_valid(0)); // "2000-01-01"
8252        assert_eq!(date_value, c.value(0));
8253
8254        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
8255        assert_eq!(date_value, c.value(1));
8256
8257        let date_value = since(
8258            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8259            from_ymd(1970, 1, 1).unwrap(),
8260        )
8261        .num_days() as i32;
8262        assert!(c.is_valid(2)); // "2000-2-2"
8263        assert_eq!(date_value, c.value(2));
8264
8265        // test invalid inputs
8266        assert!(!c.is_valid(3)); // "2000-00-00"
8267        assert!(!c.is_valid(4)); // "2000"
8268    }
8269
8270    #[test]
8271    fn test_cast_utf8_to_date64() {
8272        let a = StringArray::from(vec![
8273            "2000-01-01T12:00:00", // date + time valid
8274            "2020-12-15T12:34:56", // date + time valid
8275            "2020-2-2T12:34:56",   // valid date time without leading 0s
8276            "2000-00-00T12:00:00", // invalid month and day
8277            "2000-01-01 12:00:00", // missing the 'T'
8278            "2000-01-01",          // just a date is invalid
8279        ]);
8280        let array = Arc::new(a) as ArrayRef;
8281        let b = cast(&array, &DataType::Date64).unwrap();
8282        let c = b.as_primitive::<Date64Type>();
8283
8284        // test valid inputs
8285        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
8286        assert_eq!(946728000000, c.value(0));
8287        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
8288        assert_eq!(1608035696000, c.value(1));
8289        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
8290
8291        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
8292        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
8293        assert_eq!(946728000000, c.value(4));
8294        assert!(c.is_valid(5)); // "2000-01-01"
8295        assert_eq!(946684800000, c.value(5));
8296    }
8297
8298    #[test]
8299    fn test_can_cast_fsl_to_fsl() {
8300        let from_array = Arc::new(
8301            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8302                [Some([Some(1.0), Some(2.0)]), None],
8303                2,
8304            ),
8305        ) as ArrayRef;
8306        let to_array = Arc::new(
8307            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8308                [
8309                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8310                    None,
8311                ],
8312                2,
8313            ),
8314        ) as ArrayRef;
8315
8316        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8317        let actual = cast(&from_array, to_array.data_type()).unwrap();
8318        assert_eq!(actual.data_type(), to_array.data_type());
8319
8320        let invalid_target =
8321            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8322        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8323
8324        let invalid_size =
8325            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8326        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8327    }
8328
8329    #[test]
8330    fn test_can_cast_types_fixed_size_list_to_list() {
8331        // DataType::List
8332        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
8333        assert!(can_cast_types(
8334            array1.data_type(),
8335            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8336        ));
8337
8338        // DataType::LargeList
8339        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
8340        assert!(can_cast_types(
8341            array2.data_type(),
8342            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8343        ));
8344    }
8345
8346    #[test]
8347    fn test_cast_fixed_size_list_to_list() {
8348        // Important cases:
8349        // 1. With/without nulls
8350        // 2. LargeList and List
8351        // 3. With and without inner casts
8352
8353        let cases = [
8354            // fixed_size_list<i32, 2> => list<i32>
8355            (
8356                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8357                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8358                    2,
8359                )) as ArrayRef,
8360                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8361                    Some([Some(1), Some(1)]),
8362                    Some([Some(2), Some(2)]),
8363                ])) as ArrayRef,
8364            ),
8365            // fixed_size_list<i32, 2> => list<i32> (nullable)
8366            (
8367                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8368                    [None, Some([Some(2), Some(2)])],
8369                    2,
8370                )) as ArrayRef,
8371                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8372                    None,
8373                    Some([Some(2), Some(2)]),
8374                ])) as ArrayRef,
8375            ),
8376            // fixed_size_list<i32, 2> => large_list<i64>
8377            (
8378                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8379                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8380                    2,
8381                )) as ArrayRef,
8382                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8383                    Some([Some(1), Some(1)]),
8384                    Some([Some(2), Some(2)]),
8385                ])) as ArrayRef,
8386            ),
8387            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
8388            (
8389                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8390                    [None, Some([Some(2), Some(2)])],
8391                    2,
8392                )) as ArrayRef,
8393                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8394                    None,
8395                    Some([Some(2), Some(2)]),
8396                ])) as ArrayRef,
8397            ),
8398        ];
8399
8400        for (array, expected) in cases {
8401            let array = Arc::new(array) as ArrayRef;
8402
8403            assert!(
8404                can_cast_types(array.data_type(), expected.data_type()),
8405                "can_cast_types claims we cannot cast {:?} to {:?}",
8406                array.data_type(),
8407                expected.data_type()
8408            );
8409
8410            let list_array = cast(&array, expected.data_type())
8411                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8412            assert_eq!(
8413                list_array.as_ref(),
8414                &expected,
8415                "Incorrect result from casting {array:?} to {expected:?}",
8416            );
8417        }
8418    }
8419
8420    #[test]
8421    fn test_cast_utf8_to_list() {
8422        // DataType::List
8423        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8424        let field = Arc::new(Field::new("", DataType::Int32, false));
8425        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8426        let actual = list_array.as_list_opt::<i32>().unwrap();
8427        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8428        assert_eq!(&expect.value(0), &actual.value(0));
8429
8430        // DataType::LargeList
8431        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8432        let actual = list_array.as_list_opt::<i64>().unwrap();
8433        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8434        assert_eq!(&expect.value(0), &actual.value(0));
8435
8436        // DataType::FixedSizeList
8437        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8438        let actual = list_array.as_fixed_size_list_opt().unwrap();
8439        let expect =
8440            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
8441        assert_eq!(&expect.value(0), &actual.value(0));
8442    }
8443
8444    #[test]
8445    fn test_cast_single_element_fixed_size_list() {
8446        // FixedSizeList<T>[1] => T
8447        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8448            [(Some([Some(5)]))],
8449            1,
8450        )) as ArrayRef;
8451        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
8452        let actual: &Int32Array = casted_array.as_primitive();
8453        let expected = Int32Array::from(vec![Some(5)]);
8454        assert_eq!(&expected, actual);
8455
8456        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
8457        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8458            [(Some([Some(5)]))],
8459            1,
8460        )) as ArrayRef;
8461        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
8462        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8463        let expected = Arc::new(FixedSizeListArray::new(
8464            to_field.clone(),
8465            1,
8466            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8467            None,
8468        )) as ArrayRef;
8469        assert_eq!(*expected, *actual);
8470
8471        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
8472        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8473            [(Some([Some(5)]))],
8474            1,
8475        )) as ArrayRef;
8476        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
8477        let to_field = Arc::new(Field::new(
8478            "dummy",
8479            DataType::FixedSizeList(to_field_inner.clone(), 1),
8480            false,
8481        ));
8482        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8483        let expected = Arc::new(FixedSizeListArray::new(
8484            to_field.clone(),
8485            1,
8486            Arc::new(FixedSizeListArray::new(
8487                to_field_inner.clone(),
8488                1,
8489                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8490                None,
8491            )) as ArrayRef,
8492            None,
8493        )) as ArrayRef;
8494        assert_eq!(*expected, *actual);
8495
8496        // T => FixedSizeList<T>[1] (non-nullable)
8497        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
8498        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
8499        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8500        let actual = casted_array.as_fixed_size_list();
8501        let expected = Arc::new(FixedSizeListArray::new(
8502            field.clone(),
8503            1,
8504            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8505            None,
8506        )) as ArrayRef;
8507        assert_eq!(expected.as_ref(), actual);
8508
8509        // T => FixedSizeList<T>[1] (nullable)
8510        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
8511        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
8512        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8513        let actual = casted_array.as_fixed_size_list();
8514        let expected = Arc::new(FixedSizeListArray::new(
8515            field.clone(),
8516            1,
8517            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
8518            None,
8519        )) as ArrayRef;
8520        assert_eq!(expected.as_ref(), actual);
8521    }
8522
8523    #[test]
8524    fn test_cast_list_containers() {
8525        // large-list to list
8526        let array = Arc::new(make_large_list_array()) as ArrayRef;
8527        let list_array = cast(
8528            &array,
8529            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
8530        )
8531        .unwrap();
8532        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
8533        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
8534
8535        assert_eq!(&expected.value(0), &actual.value(0));
8536        assert_eq!(&expected.value(1), &actual.value(1));
8537        assert_eq!(&expected.value(2), &actual.value(2));
8538
8539        // list to large-list
8540        let array = Arc::new(make_list_array()) as ArrayRef;
8541        let large_list_array = cast(
8542            &array,
8543            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
8544        )
8545        .unwrap();
8546        let actual = large_list_array
8547            .as_any()
8548            .downcast_ref::<LargeListArray>()
8549            .unwrap();
8550        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
8551
8552        assert_eq!(&expected.value(0), &actual.value(0));
8553        assert_eq!(&expected.value(1), &actual.value(1));
8554        assert_eq!(&expected.value(2), &actual.value(2));
8555    }
8556
8557    #[test]
8558    fn test_cast_list_to_fsl() {
8559        // There four noteworthy cases we should handle:
8560        // 1. No nulls
8561        // 2. Nulls that are always empty
8562        // 3. Nulls that have varying lengths
8563        // 4. Nulls that are correctly sized (same as target list size)
8564
8565        // Non-null case
8566        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8567        let values = vec![
8568            Some(vec![Some(1), Some(2), Some(3)]),
8569            Some(vec![Some(4), Some(5), Some(6)]),
8570        ];
8571        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8572            values.clone(),
8573        )) as ArrayRef;
8574        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8575            values, 3,
8576        )) as ArrayRef;
8577        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8578        assert_eq!(expected.as_ref(), actual.as_ref());
8579
8580        // Null cases
8581        // Array is [[1, 2, 3], null, [4, 5, 6], null]
8582        let cases = [
8583            (
8584                // Zero-length nulls
8585                vec![1, 2, 3, 4, 5, 6],
8586                vec![3, 0, 3, 0],
8587            ),
8588            (
8589                // Varying-length nulls
8590                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
8591                vec![3, 2, 3, 1],
8592            ),
8593            (
8594                // Correctly-sized nulls
8595                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
8596                vec![3, 3, 3, 3],
8597            ),
8598            (
8599                // Mixed nulls
8600                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
8601                vec![3, 0, 3, 3],
8602            ),
8603        ];
8604        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
8605
8606        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8607            vec![
8608                Some(vec![Some(1), Some(2), Some(3)]),
8609                None,
8610                Some(vec![Some(4), Some(5), Some(6)]),
8611                None,
8612            ],
8613            3,
8614        )) as ArrayRef;
8615
8616        for (values, lengths) in cases.iter() {
8617            let array = Arc::new(ListArray::new(
8618                field.clone(),
8619                OffsetBuffer::from_lengths(lengths.clone()),
8620                Arc::new(Int32Array::from(values.clone())),
8621                Some(null_buffer.clone()),
8622            )) as ArrayRef;
8623            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8624            assert_eq!(expected.as_ref(), actual.as_ref());
8625        }
8626    }
8627
8628    #[test]
8629    fn test_cast_list_to_fsl_safety() {
8630        let values = vec![
8631            Some(vec![Some(1), Some(2), Some(3)]),
8632            Some(vec![Some(4), Some(5)]),
8633            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
8634            Some(vec![Some(3), Some(4), Some(5)]),
8635        ];
8636        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8637            values.clone(),
8638        )) as ArrayRef;
8639
8640        let res = cast_with_options(
8641            array.as_ref(),
8642            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8643            &CastOptions {
8644                safe: false,
8645                ..Default::default()
8646            },
8647        );
8648        assert!(res.is_err());
8649        assert!(
8650            format!("{res:?}")
8651                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
8652        );
8653
8654        // When safe=true (default), the cast will fill nulls for lists that are
8655        // too short and truncate lists that are too long.
8656        let res = cast(
8657            array.as_ref(),
8658            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8659        )
8660        .unwrap();
8661        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8662            vec![
8663                Some(vec![Some(1), Some(2), Some(3)]),
8664                None, // Too short -> replaced with null
8665                None, // Too long -> replaced with null
8666                Some(vec![Some(3), Some(4), Some(5)]),
8667            ],
8668            3,
8669        )) as ArrayRef;
8670        assert_eq!(expected.as_ref(), res.as_ref());
8671
8672        // The safe option is false and the source array contains a null list.
8673        // issue: https://github.com/apache/arrow-rs/issues/5642
8674        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
8675            Some(vec![Some(1), Some(2), Some(3)]),
8676            None,
8677        ])) as ArrayRef;
8678        let res = cast_with_options(
8679            array.as_ref(),
8680            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8681            &CastOptions {
8682                safe: false,
8683                ..Default::default()
8684            },
8685        )
8686        .unwrap();
8687        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8688            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
8689            3,
8690        )) as ArrayRef;
8691        assert_eq!(expected.as_ref(), res.as_ref());
8692    }
8693
8694    #[test]
8695    fn test_cast_large_list_to_fsl() {
8696        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
8697        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8698            values.clone(),
8699        )) as ArrayRef;
8700        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8701            values, 2,
8702        )) as ArrayRef;
8703        let actual = cast(
8704            array.as_ref(),
8705            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
8706        )
8707        .unwrap();
8708        assert_eq!(expected.as_ref(), actual.as_ref());
8709    }
8710
8711    #[test]
8712    fn test_cast_list_to_fsl_subcast() {
8713        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8714            vec![
8715                Some(vec![Some(1), Some(2)]),
8716                Some(vec![Some(3), Some(i32::MAX)]),
8717            ],
8718        )) as ArrayRef;
8719        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
8720            vec![
8721                Some(vec![Some(1), Some(2)]),
8722                Some(vec![Some(3), Some(i32::MAX as i64)]),
8723            ],
8724            2,
8725        )) as ArrayRef;
8726        let actual = cast(
8727            array.as_ref(),
8728            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
8729        )
8730        .unwrap();
8731        assert_eq!(expected.as_ref(), actual.as_ref());
8732
8733        let res = cast_with_options(
8734            array.as_ref(),
8735            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
8736            &CastOptions {
8737                safe: false,
8738                ..Default::default()
8739            },
8740        );
8741        assert!(res.is_err());
8742        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
8743    }
8744
8745    #[test]
8746    fn test_cast_list_to_fsl_empty() {
8747        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8748        let array = new_empty_array(&DataType::List(field.clone()));
8749
8750        let target_type = DataType::FixedSizeList(field.clone(), 3);
8751        let expected = new_empty_array(&target_type);
8752
8753        let actual = cast(array.as_ref(), &target_type).unwrap();
8754        assert_eq!(expected.as_ref(), actual.as_ref());
8755    }
8756
8757    fn make_list_array() -> ListArray {
8758        // Construct a value array
8759        let value_data = ArrayData::builder(DataType::Int32)
8760            .len(8)
8761            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8762            .build()
8763            .unwrap();
8764
8765        // Construct a buffer for value offsets, for the nested array:
8766        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8767        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8768
8769        // Construct a list array from the above two
8770        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8771        let list_data = ArrayData::builder(list_data_type)
8772            .len(3)
8773            .add_buffer(value_offsets)
8774            .add_child_data(value_data)
8775            .build()
8776            .unwrap();
8777        ListArray::from(list_data)
8778    }
8779
8780    fn make_large_list_array() -> LargeListArray {
8781        // Construct a value array
8782        let value_data = ArrayData::builder(DataType::Int32)
8783            .len(8)
8784            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8785            .build()
8786            .unwrap();
8787
8788        // Construct a buffer for value offsets, for the nested array:
8789        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8790        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
8791
8792        // Construct a list array from the above two
8793        let list_data_type =
8794            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8795        let list_data = ArrayData::builder(list_data_type)
8796            .len(3)
8797            .add_buffer(value_offsets)
8798            .add_child_data(value_data)
8799            .build()
8800            .unwrap();
8801        LargeListArray::from(list_data)
8802    }
8803
8804    fn make_fixed_size_list_array() -> FixedSizeListArray {
8805        // Construct a value array
8806        let value_data = ArrayData::builder(DataType::Int32)
8807            .len(8)
8808            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8809            .build()
8810            .unwrap();
8811
8812        let list_data_type =
8813            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8814        let list_data = ArrayData::builder(list_data_type)
8815            .len(2)
8816            .add_child_data(value_data)
8817            .build()
8818            .unwrap();
8819        FixedSizeListArray::from(list_data)
8820    }
8821
8822    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8823        // Construct a value array
8824        let value_data = ArrayData::builder(DataType::Int64)
8825            .len(8)
8826            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8827            .build()
8828            .unwrap();
8829
8830        let list_data_type =
8831            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8832        let list_data = ArrayData::builder(list_data_type)
8833            .len(2)
8834            .add_child_data(value_data)
8835            .build()
8836            .unwrap();
8837        FixedSizeListArray::from(list_data)
8838    }
8839
8840    #[test]
8841    fn test_cast_map_dont_allow_change_of_order() {
8842        let string_builder = StringBuilder::new();
8843        let value_builder = StringBuilder::new();
8844        let mut builder = MapBuilder::new(
8845            Some(MapFieldNames {
8846                entry: "entries".to_string(),
8847                key: "key".to_string(),
8848                value: "value".to_string(),
8849            }),
8850            string_builder,
8851            value_builder,
8852        );
8853
8854        builder.keys().append_value("0");
8855        builder.values().append_value("test_val_1");
8856        builder.append(true).unwrap();
8857        builder.keys().append_value("1");
8858        builder.values().append_value("test_val_2");
8859        builder.append(true).unwrap();
8860
8861        // map builder returns unsorted map by default
8862        let array = builder.finish();
8863
8864        let new_ordered = true;
8865        let new_type = DataType::Map(
8866            Arc::new(Field::new(
8867                "entries",
8868                DataType::Struct(
8869                    vec![
8870                        Field::new("key", DataType::Utf8, false),
8871                        Field::new("value", DataType::Utf8, false),
8872                    ]
8873                    .into(),
8874                ),
8875                false,
8876            )),
8877            new_ordered,
8878        );
8879
8880        let new_array_result = cast(&array, &new_type.clone());
8881        assert!(!can_cast_types(array.data_type(), &new_type));
8882        let Err(ArrowError::CastError(t)) = new_array_result else {
8883            panic!();
8884        };
8885        assert_eq!(
8886            t,
8887            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"#
8888        );
8889    }
8890
8891    #[test]
8892    fn test_cast_map_dont_allow_when_container_cant_cast() {
8893        let string_builder = StringBuilder::new();
8894        let value_builder = IntervalDayTimeArray::builder(2);
8895        let mut builder = MapBuilder::new(
8896            Some(MapFieldNames {
8897                entry: "entries".to_string(),
8898                key: "key".to_string(),
8899                value: "value".to_string(),
8900            }),
8901            string_builder,
8902            value_builder,
8903        );
8904
8905        builder.keys().append_value("0");
8906        builder.values().append_value(IntervalDayTime::new(1, 1));
8907        builder.append(true).unwrap();
8908        builder.keys().append_value("1");
8909        builder.values().append_value(IntervalDayTime::new(2, 2));
8910        builder.append(true).unwrap();
8911
8912        // map builder returns unsorted map by default
8913        let array = builder.finish();
8914
8915        let new_ordered = true;
8916        let new_type = DataType::Map(
8917            Arc::new(Field::new(
8918                "entries",
8919                DataType::Struct(
8920                    vec![
8921                        Field::new("key", DataType::Utf8, false),
8922                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
8923                    ]
8924                    .into(),
8925                ),
8926                false,
8927            )),
8928            new_ordered,
8929        );
8930
8931        let new_array_result = cast(&array, &new_type.clone());
8932        assert!(!can_cast_types(array.data_type(), &new_type));
8933        let Err(ArrowError::CastError(t)) = new_array_result else {
8934            panic!();
8935        };
8936        assert_eq!(
8937            t,
8938            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"#
8939        );
8940    }
8941
8942    #[test]
8943    fn test_cast_map_field_names() {
8944        let string_builder = StringBuilder::new();
8945        let value_builder = StringBuilder::new();
8946        let mut builder = MapBuilder::new(
8947            Some(MapFieldNames {
8948                entry: "entries".to_string(),
8949                key: "key".to_string(),
8950                value: "value".to_string(),
8951            }),
8952            string_builder,
8953            value_builder,
8954        );
8955
8956        builder.keys().append_value("0");
8957        builder.values().append_value("test_val_1");
8958        builder.append(true).unwrap();
8959        builder.keys().append_value("1");
8960        builder.values().append_value("test_val_2");
8961        builder.append(true).unwrap();
8962        builder.append(false).unwrap();
8963
8964        let array = builder.finish();
8965
8966        let new_type = DataType::Map(
8967            Arc::new(Field::new(
8968                "entries_new",
8969                DataType::Struct(
8970                    vec![
8971                        Field::new("key_new", DataType::Utf8, false),
8972                        Field::new("value_values", DataType::Utf8, false),
8973                    ]
8974                    .into(),
8975                ),
8976                false,
8977            )),
8978            false,
8979        );
8980
8981        assert_ne!(new_type, array.data_type().clone());
8982
8983        let new_array = cast(&array, &new_type.clone()).unwrap();
8984        assert_eq!(new_type, new_array.data_type().clone());
8985        let map_array = new_array.as_map();
8986
8987        assert_ne!(new_type, array.data_type().clone());
8988        assert_eq!(new_type, map_array.data_type().clone());
8989
8990        let key_string = map_array
8991            .keys()
8992            .as_any()
8993            .downcast_ref::<StringArray>()
8994            .unwrap()
8995            .into_iter()
8996            .flatten()
8997            .collect::<Vec<_>>();
8998        assert_eq!(&key_string, &vec!["0", "1"]);
8999
9000        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9001        let values_string = values_string_array
9002            .as_any()
9003            .downcast_ref::<StringArray>()
9004            .unwrap()
9005            .into_iter()
9006            .flatten()
9007            .collect::<Vec<_>>();
9008        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
9009
9010        assert_eq!(
9011            map_array.nulls(),
9012            Some(&NullBuffer::from(vec![true, true, false]))
9013        );
9014    }
9015
9016    #[test]
9017    fn test_cast_map_contained_values() {
9018        let string_builder = StringBuilder::new();
9019        let value_builder = Int8Builder::new();
9020        let mut builder = MapBuilder::new(
9021            Some(MapFieldNames {
9022                entry: "entries".to_string(),
9023                key: "key".to_string(),
9024                value: "value".to_string(),
9025            }),
9026            string_builder,
9027            value_builder,
9028        );
9029
9030        builder.keys().append_value("0");
9031        builder.values().append_value(44);
9032        builder.append(true).unwrap();
9033        builder.keys().append_value("1");
9034        builder.values().append_value(22);
9035        builder.append(true).unwrap();
9036
9037        let array = builder.finish();
9038
9039        let new_type = DataType::Map(
9040            Arc::new(Field::new(
9041                "entries",
9042                DataType::Struct(
9043                    vec![
9044                        Field::new("key", DataType::Utf8, false),
9045                        Field::new("value", DataType::Utf8, false),
9046                    ]
9047                    .into(),
9048                ),
9049                false,
9050            )),
9051            false,
9052        );
9053
9054        let new_array = cast(&array, &new_type.clone()).unwrap();
9055        assert_eq!(new_type, new_array.data_type().clone());
9056        let map_array = new_array.as_map();
9057
9058        assert_ne!(new_type, array.data_type().clone());
9059        assert_eq!(new_type, map_array.data_type().clone());
9060
9061        let key_string = map_array
9062            .keys()
9063            .as_any()
9064            .downcast_ref::<StringArray>()
9065            .unwrap()
9066            .into_iter()
9067            .flatten()
9068            .collect::<Vec<_>>();
9069        assert_eq!(&key_string, &vec!["0", "1"]);
9070
9071        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9072        let values_string = values_string_array
9073            .as_any()
9074            .downcast_ref::<StringArray>()
9075            .unwrap()
9076            .into_iter()
9077            .flatten()
9078            .collect::<Vec<_>>();
9079        assert_eq!(&values_string, &vec!["44", "22"]);
9080    }
9081
9082    #[test]
9083    fn test_utf8_cast_offsets() {
9084        // test if offset of the array is taken into account during cast
9085        let str_array = StringArray::from(vec!["a", "b", "c"]);
9086        let str_array = str_array.slice(1, 2);
9087
9088        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9089
9090        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9091        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9092        assert_eq!(strs, &["b", "c"])
9093    }
9094
9095    #[test]
9096    fn test_list_cast_offsets() {
9097        // test if offset of the array is taken into account during cast
9098        let array1 = make_list_array().slice(1, 2);
9099        let array2 = Arc::new(make_list_array()) as ArrayRef;
9100
9101        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9102        let out1 = cast(&array1, &dt).unwrap();
9103        let out2 = cast(&array2, &dt).unwrap();
9104
9105        assert_eq!(&out1, &out2.slice(1, 2))
9106    }
9107
9108    #[test]
9109    fn test_list_to_string() {
9110        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
9111        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
9112        let value_data = str_array.into_data();
9113
9114        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
9115        let list_data = ArrayData::builder(list_data_type)
9116            .len(3)
9117            .add_buffer(value_offsets)
9118            .add_child_data(value_data)
9119            .build()
9120            .unwrap();
9121        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
9122
9123        let out = cast(&array, &DataType::Utf8).unwrap();
9124        let out = out
9125            .as_any()
9126            .downcast_ref::<StringArray>()
9127            .unwrap()
9128            .into_iter()
9129            .flatten()
9130            .collect::<Vec<_>>();
9131        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9132
9133        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9134        let out = out
9135            .as_any()
9136            .downcast_ref::<LargeStringArray>()
9137            .unwrap()
9138            .into_iter()
9139            .flatten()
9140            .collect::<Vec<_>>();
9141        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9142
9143        let array = Arc::new(make_list_array()) as ArrayRef;
9144        let out = cast(&array, &DataType::Utf8).unwrap();
9145        let out = out
9146            .as_any()
9147            .downcast_ref::<StringArray>()
9148            .unwrap()
9149            .into_iter()
9150            .flatten()
9151            .collect::<Vec<_>>();
9152        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9153
9154        let array = Arc::new(make_large_list_array()) as ArrayRef;
9155        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9156        let out = out
9157            .as_any()
9158            .downcast_ref::<LargeStringArray>()
9159            .unwrap()
9160            .into_iter()
9161            .flatten()
9162            .collect::<Vec<_>>();
9163        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9164    }
9165
9166    #[test]
9167    fn test_cast_f64_to_decimal128() {
9168        // to reproduce https://github.com/apache/arrow-rs/issues/2997
9169
9170        let decimal_type = DataType::Decimal128(18, 2);
9171        let array = Float64Array::from(vec![
9172            Some(0.0699999999),
9173            Some(0.0659999999),
9174            Some(0.0650000000),
9175            Some(0.0649999999),
9176        ]);
9177        let array = Arc::new(array) as ArrayRef;
9178        generate_cast_test_case!(
9179            &array,
9180            Decimal128Array,
9181            &decimal_type,
9182            vec![
9183                Some(7_i128), // round up
9184                Some(7_i128), // round up
9185                Some(7_i128), // round up
9186                Some(6_i128), // round down
9187            ]
9188        );
9189
9190        let decimal_type = DataType::Decimal128(18, 3);
9191        let array = Float64Array::from(vec![
9192            Some(0.0699999999),
9193            Some(0.0659999999),
9194            Some(0.0650000000),
9195            Some(0.0649999999),
9196        ]);
9197        let array = Arc::new(array) as ArrayRef;
9198        generate_cast_test_case!(
9199            &array,
9200            Decimal128Array,
9201            &decimal_type,
9202            vec![
9203                Some(70_i128), // round up
9204                Some(66_i128), // round up
9205                Some(65_i128), // round down
9206                Some(65_i128), // round up
9207            ]
9208        );
9209    }
9210
9211    #[test]
9212    fn test_cast_numeric_to_decimal128_overflow() {
9213        let array = Int64Array::from(vec![i64::MAX]);
9214        let array = Arc::new(array) as ArrayRef;
9215        let casted_array = cast_with_options(
9216            &array,
9217            &DataType::Decimal128(38, 30),
9218            &CastOptions {
9219                safe: true,
9220                format_options: FormatOptions::default(),
9221            },
9222        );
9223        assert!(casted_array.is_ok());
9224        assert!(casted_array.unwrap().is_null(0));
9225
9226        let casted_array = cast_with_options(
9227            &array,
9228            &DataType::Decimal128(38, 30),
9229            &CastOptions {
9230                safe: false,
9231                format_options: FormatOptions::default(),
9232            },
9233        );
9234        assert!(casted_array.is_err());
9235    }
9236
9237    #[test]
9238    fn test_cast_numeric_to_decimal256_overflow() {
9239        let array = Int64Array::from(vec![i64::MAX]);
9240        let array = Arc::new(array) as ArrayRef;
9241        let casted_array = cast_with_options(
9242            &array,
9243            &DataType::Decimal256(76, 76),
9244            &CastOptions {
9245                safe: true,
9246                format_options: FormatOptions::default(),
9247            },
9248        );
9249        assert!(casted_array.is_ok());
9250        assert!(casted_array.unwrap().is_null(0));
9251
9252        let casted_array = cast_with_options(
9253            &array,
9254            &DataType::Decimal256(76, 76),
9255            &CastOptions {
9256                safe: false,
9257                format_options: FormatOptions::default(),
9258            },
9259        );
9260        assert!(casted_array.is_err());
9261    }
9262
9263    #[test]
9264    fn test_cast_floating_point_to_decimal128_precision_overflow() {
9265        let array = Float64Array::from(vec![1.1]);
9266        let array = Arc::new(array) as ArrayRef;
9267        let casted_array = cast_with_options(
9268            &array,
9269            &DataType::Decimal128(2, 2),
9270            &CastOptions {
9271                safe: true,
9272                format_options: FormatOptions::default(),
9273            },
9274        );
9275        assert!(casted_array.is_ok());
9276        assert!(casted_array.unwrap().is_null(0));
9277
9278        let casted_array = cast_with_options(
9279            &array,
9280            &DataType::Decimal128(2, 2),
9281            &CastOptions {
9282                safe: false,
9283                format_options: FormatOptions::default(),
9284            },
9285        );
9286        let err = casted_array.unwrap_err().to_string();
9287        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
9288        assert!(
9289            err.contains(expected_error),
9290            "did not find expected error '{expected_error}' in actual error '{err}'"
9291        );
9292    }
9293
9294    #[test]
9295    fn test_cast_floating_point_to_decimal256_precision_overflow() {
9296        let array = Float64Array::from(vec![1.1]);
9297        let array = Arc::new(array) as ArrayRef;
9298        let casted_array = cast_with_options(
9299            &array,
9300            &DataType::Decimal256(2, 2),
9301            &CastOptions {
9302                safe: true,
9303                format_options: FormatOptions::default(),
9304            },
9305        );
9306        assert!(casted_array.is_ok());
9307        assert!(casted_array.unwrap().is_null(0));
9308
9309        let casted_array = cast_with_options(
9310            &array,
9311            &DataType::Decimal256(2, 2),
9312            &CastOptions {
9313                safe: false,
9314                format_options: FormatOptions::default(),
9315            },
9316        );
9317        let err = casted_array.unwrap_err().to_string();
9318        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
9319        assert_eq!(err, expected_error);
9320    }
9321
9322    #[test]
9323    fn test_cast_floating_point_to_decimal128_overflow() {
9324        let array = Float64Array::from(vec![f64::MAX]);
9325        let array = Arc::new(array) as ArrayRef;
9326        let casted_array = cast_with_options(
9327            &array,
9328            &DataType::Decimal128(38, 30),
9329            &CastOptions {
9330                safe: true,
9331                format_options: FormatOptions::default(),
9332            },
9333        );
9334        assert!(casted_array.is_ok());
9335        assert!(casted_array.unwrap().is_null(0));
9336
9337        let casted_array = cast_with_options(
9338            &array,
9339            &DataType::Decimal128(38, 30),
9340            &CastOptions {
9341                safe: false,
9342                format_options: FormatOptions::default(),
9343            },
9344        );
9345        let err = casted_array.unwrap_err().to_string();
9346        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
9347        assert!(
9348            err.contains(expected_error),
9349            "did not find expected error '{expected_error}' in actual error '{err}'"
9350        );
9351    }
9352
9353    #[test]
9354    fn test_cast_floating_point_to_decimal256_overflow() {
9355        let array = Float64Array::from(vec![f64::MAX]);
9356        let array = Arc::new(array) as ArrayRef;
9357        let casted_array = cast_with_options(
9358            &array,
9359            &DataType::Decimal256(76, 50),
9360            &CastOptions {
9361                safe: true,
9362                format_options: FormatOptions::default(),
9363            },
9364        );
9365        assert!(casted_array.is_ok());
9366        assert!(casted_array.unwrap().is_null(0));
9367
9368        let casted_array = cast_with_options(
9369            &array,
9370            &DataType::Decimal256(76, 50),
9371            &CastOptions {
9372                safe: false,
9373                format_options: FormatOptions::default(),
9374            },
9375        );
9376        let err = casted_array.unwrap_err().to_string();
9377        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
9378        assert!(
9379            err.contains(expected_error),
9380            "did not find expected error '{expected_error}' in actual error '{err}'"
9381        );
9382    }
9383    #[test]
9384    fn test_cast_decimal256_to_f64_no_overflow() {
9385        // Test casting i256::MAX: should produce a large finite positive value
9386        let array = vec![Some(i256::MAX)];
9387        let array = create_decimal256_array(array, 76, 2).unwrap();
9388        let array = Arc::new(array) as ArrayRef;
9389
9390        let result = cast(&array, &DataType::Float64).unwrap();
9391        let result = result.as_primitive::<Float64Type>();
9392        assert!(result.value(0).is_finite());
9393        assert!(result.value(0) > 0.0); // Positive result
9394
9395        // Test casting i256::MIN: should produce a large finite negative value
9396        let array = vec![Some(i256::MIN)];
9397        let array = create_decimal256_array(array, 76, 2).unwrap();
9398        let array = Arc::new(array) as ArrayRef;
9399
9400        let result = cast(&array, &DataType::Float64).unwrap();
9401        let result = result.as_primitive::<Float64Type>();
9402        assert!(result.value(0).is_finite());
9403        assert!(result.value(0) < 0.0); // Negative result
9404    }
9405
9406    #[test]
9407    fn test_cast_decimal128_to_decimal128_negative_scale() {
9408        let input_type = DataType::Decimal128(20, 0);
9409        let output_type = DataType::Decimal128(20, -1);
9410        assert!(can_cast_types(&input_type, &output_type));
9411        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
9412        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
9413        let array = Arc::new(input_decimal_array) as ArrayRef;
9414        generate_cast_test_case!(
9415            &array,
9416            Decimal128Array,
9417            &output_type,
9418            vec![
9419                Some(112345_i128),
9420                Some(212346_i128),
9421                Some(312346_i128),
9422                None
9423            ]
9424        );
9425
9426        let casted_array = cast(&array, &output_type).unwrap();
9427        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9428
9429        assert_eq!("1123450", decimal_arr.value_as_string(0));
9430        assert_eq!("2123460", decimal_arr.value_as_string(1));
9431        assert_eq!("3123460", decimal_arr.value_as_string(2));
9432    }
9433
9434    #[test]
9435    fn decimal128_min_max_to_f64() {
9436        // Ensure Decimal128 i128::MIN/MAX round-trip cast
9437        let min128 = i128::MIN;
9438        let max128 = i128::MAX;
9439        assert_eq!(min128 as f64, min128 as f64);
9440        assert_eq!(max128 as f64, max128 as f64);
9441    }
9442
9443    #[test]
9444    fn test_cast_numeric_to_decimal128_negative() {
9445        let decimal_type = DataType::Decimal128(38, -1);
9446        let array = Arc::new(Int32Array::from(vec![
9447            Some(1123456),
9448            Some(2123456),
9449            Some(3123456),
9450        ])) as ArrayRef;
9451
9452        let casted_array = cast(&array, &decimal_type).unwrap();
9453        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9454
9455        assert_eq!("1123450", decimal_arr.value_as_string(0));
9456        assert_eq!("2123450", decimal_arr.value_as_string(1));
9457        assert_eq!("3123450", decimal_arr.value_as_string(2));
9458
9459        let array = Arc::new(Float32Array::from(vec![
9460            Some(1123.456),
9461            Some(2123.456),
9462            Some(3123.456),
9463        ])) as ArrayRef;
9464
9465        let casted_array = cast(&array, &decimal_type).unwrap();
9466        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9467
9468        assert_eq!("1120", decimal_arr.value_as_string(0));
9469        assert_eq!("2120", decimal_arr.value_as_string(1));
9470        assert_eq!("3120", decimal_arr.value_as_string(2));
9471    }
9472
9473    #[test]
9474    fn test_cast_decimal128_to_decimal128_negative() {
9475        let input_type = DataType::Decimal128(10, -1);
9476        let output_type = DataType::Decimal128(10, -2);
9477        assert!(can_cast_types(&input_type, &output_type));
9478        let array = vec![Some(123)];
9479        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9480        let array = Arc::new(input_decimal_array) as ArrayRef;
9481        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
9482
9483        let casted_array = cast(&array, &output_type).unwrap();
9484        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9485
9486        assert_eq!("1200", decimal_arr.value_as_string(0));
9487
9488        let array = vec![Some(125)];
9489        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9490        let array = Arc::new(input_decimal_array) as ArrayRef;
9491        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
9492
9493        let casted_array = cast(&array, &output_type).unwrap();
9494        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9495
9496        assert_eq!("1300", decimal_arr.value_as_string(0));
9497    }
9498
9499    #[test]
9500    fn test_cast_decimal128_to_decimal256_negative() {
9501        let input_type = DataType::Decimal128(10, 3);
9502        let output_type = DataType::Decimal256(10, 5);
9503        assert!(can_cast_types(&input_type, &output_type));
9504        let array = vec![Some(123456), Some(-123456)];
9505        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
9506        let array = Arc::new(input_decimal_array) as ArrayRef;
9507
9508        let hundred = i256::from_i128(100);
9509        generate_cast_test_case!(
9510            &array,
9511            Decimal256Array,
9512            &output_type,
9513            vec![
9514                Some(i256::from_i128(123456).mul_wrapping(hundred)),
9515                Some(i256::from_i128(-123456).mul_wrapping(hundred))
9516            ]
9517        );
9518    }
9519
9520    #[test]
9521    fn test_parse_string_to_decimal() {
9522        assert_eq!(
9523            Decimal128Type::format_decimal(
9524                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
9525                38,
9526                2,
9527            ),
9528            "123.45"
9529        );
9530        assert_eq!(
9531            Decimal128Type::format_decimal(
9532                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
9533                38,
9534                2,
9535            ),
9536            "12345.00"
9537        );
9538        assert_eq!(
9539            Decimal128Type::format_decimal(
9540                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
9541                38,
9542                2,
9543            ),
9544            "0.12"
9545        );
9546        assert_eq!(
9547            Decimal128Type::format_decimal(
9548                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
9549                38,
9550                2,
9551            ),
9552            "0.12"
9553        );
9554        assert_eq!(
9555            Decimal128Type::format_decimal(
9556                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9557                38,
9558                2,
9559            ),
9560            "0.13"
9561        );
9562        assert_eq!(
9563            Decimal128Type::format_decimal(
9564                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9565                38,
9566                2,
9567            ),
9568            "0.13"
9569        );
9570
9571        assert_eq!(
9572            Decimal256Type::format_decimal(
9573                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
9574                38,
9575                3,
9576            ),
9577            "123.450"
9578        );
9579        assert_eq!(
9580            Decimal256Type::format_decimal(
9581                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
9582                38,
9583                3,
9584            ),
9585            "12345.000"
9586        );
9587        assert_eq!(
9588            Decimal256Type::format_decimal(
9589                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
9590                38,
9591                3,
9592            ),
9593            "0.123"
9594        );
9595        assert_eq!(
9596            Decimal256Type::format_decimal(
9597                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
9598                38,
9599                3,
9600            ),
9601            "0.123"
9602        );
9603        assert_eq!(
9604            Decimal256Type::format_decimal(
9605                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
9606                38,
9607                3,
9608            ),
9609            "0.127"
9610        );
9611    }
9612
9613    fn test_cast_string_to_decimal(array: ArrayRef) {
9614        // Decimal128
9615        let output_type = DataType::Decimal128(38, 2);
9616        assert!(can_cast_types(array.data_type(), &output_type));
9617
9618        let casted_array = cast(&array, &output_type).unwrap();
9619        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9620
9621        assert_eq!("123.45", decimal_arr.value_as_string(0));
9622        assert_eq!("1.23", decimal_arr.value_as_string(1));
9623        assert_eq!("0.12", decimal_arr.value_as_string(2));
9624        assert_eq!("0.13", decimal_arr.value_as_string(3));
9625        assert_eq!("1.26", decimal_arr.value_as_string(4));
9626        assert_eq!("12345.00", decimal_arr.value_as_string(5));
9627        assert_eq!("12345.00", decimal_arr.value_as_string(6));
9628        assert_eq!("0.12", decimal_arr.value_as_string(7));
9629        assert_eq!("12.23", decimal_arr.value_as_string(8));
9630        assert!(decimal_arr.is_null(9));
9631        assert_eq!("0.00", decimal_arr.value_as_string(10));
9632        assert_eq!("0.00", decimal_arr.value_as_string(11));
9633        assert!(decimal_arr.is_null(12));
9634        assert_eq!("-1.23", decimal_arr.value_as_string(13));
9635        assert_eq!("-1.24", decimal_arr.value_as_string(14));
9636        assert_eq!("0.00", decimal_arr.value_as_string(15));
9637        assert_eq!("-123.00", decimal_arr.value_as_string(16));
9638        assert_eq!("-123.23", decimal_arr.value_as_string(17));
9639        assert_eq!("-0.12", decimal_arr.value_as_string(18));
9640        assert_eq!("1.23", decimal_arr.value_as_string(19));
9641        assert_eq!("1.24", decimal_arr.value_as_string(20));
9642        assert_eq!("0.00", decimal_arr.value_as_string(21));
9643        assert_eq!("123.00", decimal_arr.value_as_string(22));
9644        assert_eq!("123.23", decimal_arr.value_as_string(23));
9645        assert_eq!("0.12", decimal_arr.value_as_string(24));
9646        assert!(decimal_arr.is_null(25));
9647        assert!(decimal_arr.is_null(26));
9648        assert!(decimal_arr.is_null(27));
9649        assert_eq!("0.00", decimal_arr.value_as_string(28));
9650        assert_eq!("0.00", decimal_arr.value_as_string(29));
9651        assert_eq!("12345.00", decimal_arr.value_as_string(30));
9652        assert_eq!(decimal_arr.len(), 31);
9653
9654        // Decimal256
9655        let output_type = DataType::Decimal256(76, 3);
9656        assert!(can_cast_types(array.data_type(), &output_type));
9657
9658        let casted_array = cast(&array, &output_type).unwrap();
9659        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9660
9661        assert_eq!("123.450", decimal_arr.value_as_string(0));
9662        assert_eq!("1.235", decimal_arr.value_as_string(1));
9663        assert_eq!("0.123", decimal_arr.value_as_string(2));
9664        assert_eq!("0.127", decimal_arr.value_as_string(3));
9665        assert_eq!("1.263", decimal_arr.value_as_string(4));
9666        assert_eq!("12345.000", decimal_arr.value_as_string(5));
9667        assert_eq!("12345.000", decimal_arr.value_as_string(6));
9668        assert_eq!("0.123", decimal_arr.value_as_string(7));
9669        assert_eq!("12.234", decimal_arr.value_as_string(8));
9670        assert!(decimal_arr.is_null(9));
9671        assert_eq!("0.000", decimal_arr.value_as_string(10));
9672        assert_eq!("0.000", decimal_arr.value_as_string(11));
9673        assert!(decimal_arr.is_null(12));
9674        assert_eq!("-1.235", decimal_arr.value_as_string(13));
9675        assert_eq!("-1.236", decimal_arr.value_as_string(14));
9676        assert_eq!("0.000", decimal_arr.value_as_string(15));
9677        assert_eq!("-123.000", decimal_arr.value_as_string(16));
9678        assert_eq!("-123.234", decimal_arr.value_as_string(17));
9679        assert_eq!("-0.123", decimal_arr.value_as_string(18));
9680        assert_eq!("1.235", decimal_arr.value_as_string(19));
9681        assert_eq!("1.236", decimal_arr.value_as_string(20));
9682        assert_eq!("0.000", decimal_arr.value_as_string(21));
9683        assert_eq!("123.000", decimal_arr.value_as_string(22));
9684        assert_eq!("123.234", decimal_arr.value_as_string(23));
9685        assert_eq!("0.123", decimal_arr.value_as_string(24));
9686        assert!(decimal_arr.is_null(25));
9687        assert!(decimal_arr.is_null(26));
9688        assert!(decimal_arr.is_null(27));
9689        assert_eq!("0.000", decimal_arr.value_as_string(28));
9690        assert_eq!("0.000", decimal_arr.value_as_string(29));
9691        assert_eq!("12345.000", decimal_arr.value_as_string(30));
9692        assert_eq!(decimal_arr.len(), 31);
9693    }
9694
9695    #[test]
9696    fn test_cast_utf8_to_decimal() {
9697        let str_array = StringArray::from(vec![
9698            Some("123.45"),
9699            Some("1.2345"),
9700            Some("0.12345"),
9701            Some("0.1267"),
9702            Some("1.263"),
9703            Some("12345.0"),
9704            Some("12345"),
9705            Some("000.123"),
9706            Some("12.234000"),
9707            None,
9708            Some(""),
9709            Some(" "),
9710            None,
9711            Some("-1.23499999"),
9712            Some("-1.23599999"),
9713            Some("-0.00001"),
9714            Some("-123"),
9715            Some("-123.234000"),
9716            Some("-000.123"),
9717            Some("+1.23499999"),
9718            Some("+1.23599999"),
9719            Some("+0.00001"),
9720            Some("+123"),
9721            Some("+123.234000"),
9722            Some("+000.123"),
9723            Some("1.-23499999"),
9724            Some("-1.-23499999"),
9725            Some("--1.23499999"),
9726            Some("0"),
9727            Some("000.000"),
9728            Some("0000000000000000012345.000"),
9729        ]);
9730        let array = Arc::new(str_array) as ArrayRef;
9731
9732        test_cast_string_to_decimal(array);
9733
9734        let test_cases = [
9735            (None, None),
9736            // (Some(""), None),
9737            // (Some("   "), None),
9738            (Some("0"), Some("0")),
9739            (Some("000.000"), Some("0")),
9740            (Some("12345"), Some("12345")),
9741            (Some("000000000000000000000000000012345"), Some("12345")),
9742            (Some("-123"), Some("-123")),
9743            (Some("+123"), Some("123")),
9744        ];
9745        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9746        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9747
9748        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
9749        test_cast_string_to_decimal_scale_zero(array, &expected);
9750    }
9751
9752    #[test]
9753    fn test_cast_large_utf8_to_decimal() {
9754        let str_array = LargeStringArray::from(vec![
9755            Some("123.45"),
9756            Some("1.2345"),
9757            Some("0.12345"),
9758            Some("0.1267"),
9759            Some("1.263"),
9760            Some("12345.0"),
9761            Some("12345"),
9762            Some("000.123"),
9763            Some("12.234000"),
9764            None,
9765            Some(""),
9766            Some(" "),
9767            None,
9768            Some("-1.23499999"),
9769            Some("-1.23599999"),
9770            Some("-0.00001"),
9771            Some("-123"),
9772            Some("-123.234000"),
9773            Some("-000.123"),
9774            Some("+1.23499999"),
9775            Some("+1.23599999"),
9776            Some("+0.00001"),
9777            Some("+123"),
9778            Some("+123.234000"),
9779            Some("+000.123"),
9780            Some("1.-23499999"),
9781            Some("-1.-23499999"),
9782            Some("--1.23499999"),
9783            Some("0"),
9784            Some("000.000"),
9785            Some("0000000000000000012345.000"),
9786        ]);
9787        let array = Arc::new(str_array) as ArrayRef;
9788
9789        test_cast_string_to_decimal(array);
9790
9791        let test_cases = [
9792            (None, None),
9793            (Some(""), None),
9794            (Some("   "), None),
9795            (Some("0"), Some("0")),
9796            (Some("000.000"), Some("0")),
9797            (Some("12345"), Some("12345")),
9798            (Some("000000000000000000000000000012345"), Some("12345")),
9799            (Some("-123"), Some("-123")),
9800            (Some("+123"), Some("123")),
9801        ];
9802        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9803        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9804
9805        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
9806        test_cast_string_to_decimal_scale_zero(array, &expected);
9807    }
9808
9809    fn test_cast_string_to_decimal_scale_zero(
9810        array: ArrayRef,
9811        expected_as_string: &[Option<&str>],
9812    ) {
9813        // Decimal128
9814        let output_type = DataType::Decimal128(38, 0);
9815        assert!(can_cast_types(array.data_type(), &output_type));
9816        let casted_array = cast(&array, &output_type).unwrap();
9817        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9818        assert_decimal_array_contents(decimal_arr, expected_as_string);
9819
9820        // Decimal256
9821        let output_type = DataType::Decimal256(76, 0);
9822        assert!(can_cast_types(array.data_type(), &output_type));
9823        let casted_array = cast(&array, &output_type).unwrap();
9824        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9825        assert_decimal_array_contents(decimal_arr, expected_as_string);
9826    }
9827
9828    fn assert_decimal_array_contents<T>(
9829        array: &PrimitiveArray<T>,
9830        expected_as_string: &[Option<&str>],
9831    ) where
9832        T: DecimalType + ArrowPrimitiveType,
9833    {
9834        assert_eq!(array.len(), expected_as_string.len());
9835        for (i, expected) in expected_as_string.iter().enumerate() {
9836            let actual = if array.is_null(i) {
9837                None
9838            } else {
9839                Some(array.value_as_string(i))
9840            };
9841            let actual = actual.as_ref().map(|s| s.as_ref());
9842            assert_eq!(*expected, actual, "Expected at position {i}");
9843        }
9844    }
9845
9846    #[test]
9847    fn test_cast_invalid_utf8_to_decimal() {
9848        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9849        let array = Arc::new(str_array) as ArrayRef;
9850
9851        // Safe cast
9852        let output_type = DataType::Decimal128(38, 2);
9853        let casted_array = cast(&array, &output_type).unwrap();
9854        assert!(casted_array.is_null(0));
9855        assert!(casted_array.is_null(1));
9856
9857        let output_type = DataType::Decimal256(76, 2);
9858        let casted_array = cast(&array, &output_type).unwrap();
9859        assert!(casted_array.is_null(0));
9860        assert!(casted_array.is_null(1));
9861
9862        // Non-safe cast
9863        let output_type = DataType::Decimal128(38, 2);
9864        let str_array = StringArray::from(vec!["4.4.5"]);
9865        let array = Arc::new(str_array) as ArrayRef;
9866        let option = CastOptions {
9867            safe: false,
9868            format_options: FormatOptions::default(),
9869        };
9870        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9871        assert!(
9872            casted_err
9873                .to_string()
9874                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
9875        );
9876
9877        let str_array = StringArray::from(vec![". 0.123"]);
9878        let array = Arc::new(str_array) as ArrayRef;
9879        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9880        assert!(
9881            casted_err
9882                .to_string()
9883                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
9884        );
9885    }
9886
9887    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
9888        let output_type = DataType::Decimal128(38, 2);
9889        let casted_array = cast(&overflow_array, &output_type).unwrap();
9890        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9891
9892        assert!(decimal_arr.is_null(0));
9893        assert!(decimal_arr.is_null(1));
9894        assert!(decimal_arr.is_null(2));
9895        assert_eq!(
9896            "999999999999999999999999999999999999.99",
9897            decimal_arr.value_as_string(3)
9898        );
9899        assert_eq!(
9900            "100000000000000000000000000000000000.00",
9901            decimal_arr.value_as_string(4)
9902        );
9903    }
9904
9905    #[test]
9906    fn test_cast_string_to_decimal128_precision_overflow() {
9907        let array = StringArray::from(vec!["1000".to_string()]);
9908        let array = Arc::new(array) as ArrayRef;
9909        let casted_array = cast_with_options(
9910            &array,
9911            &DataType::Decimal128(10, 8),
9912            &CastOptions {
9913                safe: true,
9914                format_options: FormatOptions::default(),
9915            },
9916        );
9917        assert!(casted_array.is_ok());
9918        assert!(casted_array.unwrap().is_null(0));
9919
9920        let err = cast_with_options(
9921            &array,
9922            &DataType::Decimal128(10, 8),
9923            &CastOptions {
9924                safe: false,
9925                format_options: FormatOptions::default(),
9926            },
9927        );
9928        assert_eq!(
9929            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
9930            err.unwrap_err().to_string()
9931        );
9932    }
9933
9934    #[test]
9935    fn test_cast_utf8_to_decimal128_overflow() {
9936        let overflow_str_array = StringArray::from(vec![
9937            i128::MAX.to_string(),
9938            i128::MIN.to_string(),
9939            "99999999999999999999999999999999999999".to_string(),
9940            "999999999999999999999999999999999999.99".to_string(),
9941            "99999999999999999999999999999999999.999".to_string(),
9942        ]);
9943        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9944
9945        test_cast_string_to_decimal128_overflow(overflow_array);
9946    }
9947
9948    #[test]
9949    fn test_cast_large_utf8_to_decimal128_overflow() {
9950        let overflow_str_array = LargeStringArray::from(vec![
9951            i128::MAX.to_string(),
9952            i128::MIN.to_string(),
9953            "99999999999999999999999999999999999999".to_string(),
9954            "999999999999999999999999999999999999.99".to_string(),
9955            "99999999999999999999999999999999999.999".to_string(),
9956        ]);
9957        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9958
9959        test_cast_string_to_decimal128_overflow(overflow_array);
9960    }
9961
9962    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
9963        let output_type = DataType::Decimal256(76, 2);
9964        let casted_array = cast(&overflow_array, &output_type).unwrap();
9965        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9966
9967        assert_eq!(
9968            "170141183460469231731687303715884105727.00",
9969            decimal_arr.value_as_string(0)
9970        );
9971        assert_eq!(
9972            "-170141183460469231731687303715884105728.00",
9973            decimal_arr.value_as_string(1)
9974        );
9975        assert_eq!(
9976            "99999999999999999999999999999999999999.00",
9977            decimal_arr.value_as_string(2)
9978        );
9979        assert_eq!(
9980            "999999999999999999999999999999999999.99",
9981            decimal_arr.value_as_string(3)
9982        );
9983        assert_eq!(
9984            "100000000000000000000000000000000000.00",
9985            decimal_arr.value_as_string(4)
9986        );
9987        assert!(decimal_arr.is_null(5));
9988        assert!(decimal_arr.is_null(6));
9989    }
9990
9991    #[test]
9992    fn test_cast_string_to_decimal256_precision_overflow() {
9993        let array = StringArray::from(vec!["1000".to_string()]);
9994        let array = Arc::new(array) as ArrayRef;
9995        let casted_array = cast_with_options(
9996            &array,
9997            &DataType::Decimal256(10, 8),
9998            &CastOptions {
9999                safe: true,
10000                format_options: FormatOptions::default(),
10001            },
10002        );
10003        assert!(casted_array.is_ok());
10004        assert!(casted_array.unwrap().is_null(0));
10005
10006        let err = cast_with_options(
10007            &array,
10008            &DataType::Decimal256(10, 8),
10009            &CastOptions {
10010                safe: false,
10011                format_options: FormatOptions::default(),
10012            },
10013        );
10014        assert_eq!(
10015            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
10016            err.unwrap_err().to_string()
10017        );
10018    }
10019
10020    #[test]
10021    fn test_cast_utf8_to_decimal256_overflow() {
10022        let overflow_str_array = StringArray::from(vec![
10023            i128::MAX.to_string(),
10024            i128::MIN.to_string(),
10025            "99999999999999999999999999999999999999".to_string(),
10026            "999999999999999999999999999999999999.99".to_string(),
10027            "99999999999999999999999999999999999.999".to_string(),
10028            i256::MAX.to_string(),
10029            i256::MIN.to_string(),
10030        ]);
10031        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10032
10033        test_cast_string_to_decimal256_overflow(overflow_array);
10034    }
10035
10036    #[test]
10037    fn test_cast_large_utf8_to_decimal256_overflow() {
10038        let overflow_str_array = LargeStringArray::from(vec![
10039            i128::MAX.to_string(),
10040            i128::MIN.to_string(),
10041            "99999999999999999999999999999999999999".to_string(),
10042            "999999999999999999999999999999999999.99".to_string(),
10043            "99999999999999999999999999999999999.999".to_string(),
10044            i256::MAX.to_string(),
10045            i256::MIN.to_string(),
10046        ]);
10047        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10048
10049        test_cast_string_to_decimal256_overflow(overflow_array);
10050    }
10051
10052    #[test]
10053    fn test_cast_outside_supported_range_for_nanoseconds() {
10054        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";
10055
10056        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
10057
10058        let cast_options = CastOptions {
10059            safe: false,
10060            format_options: FormatOptions::default(),
10061        };
10062
10063        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
10064            &array,
10065            &None::<Arc<str>>,
10066            &cast_options,
10067        );
10068
10069        let err = result.unwrap_err();
10070        assert_eq!(
10071            err.to_string(),
10072            format!(
10073                "Cast error: Overflow converting {} to Nanosecond. {}",
10074                array.value(0),
10075                EXPECTED_ERROR_MESSAGE
10076            )
10077        );
10078    }
10079
10080    #[test]
10081    fn test_cast_date32_to_timestamp() {
10082        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10083        let array = Arc::new(a) as ArrayRef;
10084        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10085        let c = b.as_primitive::<TimestampSecondType>();
10086        assert_eq!(1609459200, c.value(0));
10087        assert_eq!(1640995200, c.value(1));
10088        assert!(c.is_null(2));
10089    }
10090
10091    #[test]
10092    fn test_cast_date32_to_timestamp_ms() {
10093        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10094        let array = Arc::new(a) as ArrayRef;
10095        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10096        let c = b
10097            .as_any()
10098            .downcast_ref::<TimestampMillisecondArray>()
10099            .unwrap();
10100        assert_eq!(1609459200000, c.value(0));
10101        assert_eq!(1640995200000, c.value(1));
10102        assert!(c.is_null(2));
10103    }
10104
10105    #[test]
10106    fn test_cast_date32_to_timestamp_us() {
10107        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10108        let array = Arc::new(a) as ArrayRef;
10109        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10110        let c = b
10111            .as_any()
10112            .downcast_ref::<TimestampMicrosecondArray>()
10113            .unwrap();
10114        assert_eq!(1609459200000000, c.value(0));
10115        assert_eq!(1640995200000000, c.value(1));
10116        assert!(c.is_null(2));
10117    }
10118
10119    #[test]
10120    fn test_cast_date32_to_timestamp_ns() {
10121        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10122        let array = Arc::new(a) as ArrayRef;
10123        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10124        let c = b
10125            .as_any()
10126            .downcast_ref::<TimestampNanosecondArray>()
10127            .unwrap();
10128        assert_eq!(1609459200000000000, c.value(0));
10129        assert_eq!(1640995200000000000, c.value(1));
10130        assert!(c.is_null(2));
10131    }
10132
10133    #[test]
10134    fn test_timezone_cast() {
10135        let a = StringArray::from(vec![
10136            "2000-01-01T12:00:00", // date + time valid
10137            "2020-12-15T12:34:56", // date + time valid
10138        ]);
10139        let array = Arc::new(a) as ArrayRef;
10140        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10141        let v = b.as_primitive::<TimestampNanosecondType>();
10142
10143        assert_eq!(v.value(0), 946728000000000000);
10144        assert_eq!(v.value(1), 1608035696000000000);
10145
10146        let b = cast(
10147            &b,
10148            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10149        )
10150        .unwrap();
10151        let v = b.as_primitive::<TimestampNanosecondType>();
10152
10153        assert_eq!(v.value(0), 946728000000000000);
10154        assert_eq!(v.value(1), 1608035696000000000);
10155
10156        let b = cast(
10157            &b,
10158            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10159        )
10160        .unwrap();
10161        let v = b.as_primitive::<TimestampMillisecondType>();
10162
10163        assert_eq!(v.value(0), 946728000000);
10164        assert_eq!(v.value(1), 1608035696000);
10165    }
10166
10167    #[test]
10168    fn test_cast_utf8_to_timestamp() {
10169        fn test_tz(tz: Arc<str>) {
10170            let valid = StringArray::from(vec![
10171                "2023-01-01 04:05:06.789000-08:00",
10172                "2023-01-01 04:05:06.789000-07:00",
10173                "2023-01-01 04:05:06.789 -0800",
10174                "2023-01-01 04:05:06.789 -08:00",
10175                "2023-01-01 040506 +0730",
10176                "2023-01-01 040506 +07:30",
10177                "2023-01-01 04:05:06.789",
10178                "2023-01-01 04:05:06",
10179                "2023-01-01",
10180            ]);
10181
10182            let array = Arc::new(valid) as ArrayRef;
10183            let b = cast_with_options(
10184                &array,
10185                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
10186                &CastOptions {
10187                    safe: false,
10188                    format_options: FormatOptions::default(),
10189                },
10190            )
10191            .unwrap();
10192
10193            let tz = tz.as_ref().parse().unwrap();
10194
10195            let as_tz =
10196                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
10197
10198            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
10199            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
10200
10201            let values = b.as_primitive::<TimestampNanosecondType>().values();
10202            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
10203            let local_results: Vec<_> = values.iter().map(as_local).collect();
10204
10205            // Absolute timestamps should be parsed preserving the same UTC instant
10206            assert_eq!(
10207                &utc_results[..6],
10208                &[
10209                    "2023-01-01 12:05:06.789".to_string(),
10210                    "2023-01-01 11:05:06.789".to_string(),
10211                    "2023-01-01 12:05:06.789".to_string(),
10212                    "2023-01-01 12:05:06.789".to_string(),
10213                    "2022-12-31 20:35:06".to_string(),
10214                    "2022-12-31 20:35:06".to_string(),
10215                ]
10216            );
10217            // Non-absolute timestamps should be parsed preserving the same local instant
10218            assert_eq!(
10219                &local_results[6..],
10220                &[
10221                    "2023-01-01 04:05:06.789".to_string(),
10222                    "2023-01-01 04:05:06".to_string(),
10223                    "2023-01-01 00:00:00".to_string()
10224                ]
10225            )
10226        }
10227
10228        test_tz("+00:00".into());
10229        test_tz("+02:00".into());
10230    }
10231
10232    #[test]
10233    fn test_cast_invalid_utf8() {
10234        let v1: &[u8] = b"\xFF invalid";
10235        let v2: &[u8] = b"\x00 Foo";
10236        let s = BinaryArray::from(vec![v1, v2]);
10237        let options = CastOptions {
10238            safe: true,
10239            format_options: FormatOptions::default(),
10240        };
10241        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
10242        let a = array.as_string::<i32>();
10243        a.to_data().validate_full().unwrap();
10244
10245        assert_eq!(a.null_count(), 1);
10246        assert_eq!(a.len(), 2);
10247        assert!(a.is_null(0));
10248        assert_eq!(a.value(0), "");
10249        assert_eq!(a.value(1), "\x00 Foo");
10250    }
10251
10252    #[test]
10253    fn test_cast_utf8_to_timestamptz() {
10254        let valid = StringArray::from(vec!["2023-01-01"]);
10255
10256        let array = Arc::new(valid) as ArrayRef;
10257        let b = cast(
10258            &array,
10259            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10260        )
10261        .unwrap();
10262
10263        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
10264
10265        assert_eq!(b.data_type(), &expect);
10266        let c = b
10267            .as_any()
10268            .downcast_ref::<TimestampNanosecondArray>()
10269            .unwrap();
10270        assert_eq!(1672531200000000000, c.value(0));
10271    }
10272
10273    #[test]
10274    fn test_cast_decimal_to_string() {
10275        assert!(can_cast_types(
10276            &DataType::Decimal32(9, 4),
10277            &DataType::Utf8View
10278        ));
10279        assert!(can_cast_types(
10280            &DataType::Decimal64(16, 4),
10281            &DataType::Utf8View
10282        ));
10283        assert!(can_cast_types(
10284            &DataType::Decimal128(10, 4),
10285            &DataType::Utf8View
10286        ));
10287        assert!(can_cast_types(
10288            &DataType::Decimal256(38, 10),
10289            &DataType::Utf8View
10290        ));
10291
10292        macro_rules! assert_decimal_values {
10293            ($array:expr) => {
10294                let c = $array;
10295                assert_eq!("1123.454", c.value(0));
10296                assert_eq!("2123.456", c.value(1));
10297                assert_eq!("-3123.453", c.value(2));
10298                assert_eq!("-3123.456", c.value(3));
10299                assert_eq!("0.000", c.value(4));
10300                assert_eq!("0.123", c.value(5));
10301                assert_eq!("1234.567", c.value(6));
10302                assert_eq!("-1234.567", c.value(7));
10303                assert!(c.is_null(8));
10304            };
10305        }
10306
10307        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
10308            output_type: DataType,
10309            array: PrimitiveArray<IN>,
10310        ) {
10311            let b = cast(&array, &output_type).unwrap();
10312
10313            assert_eq!(b.data_type(), &output_type);
10314            match b.data_type() {
10315                DataType::Utf8View => {
10316                    let c = b.as_string_view();
10317                    assert_decimal_values!(c);
10318                }
10319                DataType::Utf8 | DataType::LargeUtf8 => {
10320                    let c = b.as_string::<OffsetSize>();
10321                    assert_decimal_values!(c);
10322                }
10323                _ => (),
10324            }
10325        }
10326
10327        let array32: Vec<Option<i32>> = vec![
10328            Some(1123454),
10329            Some(2123456),
10330            Some(-3123453),
10331            Some(-3123456),
10332            Some(0),
10333            Some(123),
10334            Some(123456789),
10335            Some(-123456789),
10336            None,
10337        ];
10338        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
10339        let array128: Vec<Option<i128>> =
10340            array64.iter().map(|num| num.map(|x| x as i128)).collect();
10341        let array256: Vec<Option<i256>> = array128
10342            .iter()
10343            .map(|num| num.map(i256::from_i128))
10344            .collect();
10345
10346        test_decimal_to_string::<Decimal32Type, i32>(
10347            DataType::Utf8View,
10348            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10349        );
10350        test_decimal_to_string::<Decimal32Type, i32>(
10351            DataType::Utf8,
10352            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10353        );
10354        test_decimal_to_string::<Decimal32Type, i64>(
10355            DataType::LargeUtf8,
10356            create_decimal32_array(array32, 7, 3).unwrap(),
10357        );
10358
10359        test_decimal_to_string::<Decimal64Type, i32>(
10360            DataType::Utf8View,
10361            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10362        );
10363        test_decimal_to_string::<Decimal64Type, i32>(
10364            DataType::Utf8,
10365            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10366        );
10367        test_decimal_to_string::<Decimal64Type, i64>(
10368            DataType::LargeUtf8,
10369            create_decimal64_array(array64, 7, 3).unwrap(),
10370        );
10371
10372        test_decimal_to_string::<Decimal128Type, i32>(
10373            DataType::Utf8View,
10374            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10375        );
10376        test_decimal_to_string::<Decimal128Type, i32>(
10377            DataType::Utf8,
10378            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10379        );
10380        test_decimal_to_string::<Decimal128Type, i64>(
10381            DataType::LargeUtf8,
10382            create_decimal128_array(array128, 7, 3).unwrap(),
10383        );
10384
10385        test_decimal_to_string::<Decimal256Type, i32>(
10386            DataType::Utf8View,
10387            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10388        );
10389        test_decimal_to_string::<Decimal256Type, i32>(
10390            DataType::Utf8,
10391            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10392        );
10393        test_decimal_to_string::<Decimal256Type, i64>(
10394            DataType::LargeUtf8,
10395            create_decimal256_array(array256, 7, 3).unwrap(),
10396        );
10397    }
10398
10399    #[test]
10400    fn test_cast_numeric_to_decimal128_precision_overflow() {
10401        let array = Int64Array::from(vec![1234567]);
10402        let array = Arc::new(array) as ArrayRef;
10403        let casted_array = cast_with_options(
10404            &array,
10405            &DataType::Decimal128(7, 3),
10406            &CastOptions {
10407                safe: true,
10408                format_options: FormatOptions::default(),
10409            },
10410        );
10411        assert!(casted_array.is_ok());
10412        assert!(casted_array.unwrap().is_null(0));
10413
10414        let err = cast_with_options(
10415            &array,
10416            &DataType::Decimal128(7, 3),
10417            &CastOptions {
10418                safe: false,
10419                format_options: FormatOptions::default(),
10420            },
10421        );
10422        assert_eq!(
10423            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
10424            err.unwrap_err().to_string()
10425        );
10426    }
10427
10428    #[test]
10429    fn test_cast_numeric_to_decimal256_precision_overflow() {
10430        let array = Int64Array::from(vec![1234567]);
10431        let array = Arc::new(array) as ArrayRef;
10432        let casted_array = cast_with_options(
10433            &array,
10434            &DataType::Decimal256(7, 3),
10435            &CastOptions {
10436                safe: true,
10437                format_options: FormatOptions::default(),
10438            },
10439        );
10440        assert!(casted_array.is_ok());
10441        assert!(casted_array.unwrap().is_null(0));
10442
10443        let err = cast_with_options(
10444            &array,
10445            &DataType::Decimal256(7, 3),
10446            &CastOptions {
10447                safe: false,
10448                format_options: FormatOptions::default(),
10449            },
10450        );
10451        assert_eq!(
10452            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
10453            err.unwrap_err().to_string()
10454        );
10455    }
10456
10457    /// helper function to test casting from duration to interval
10458    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
10459        array: Vec<i64>,
10460        cast_options: &CastOptions,
10461    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10462        let array = PrimitiveArray::<T>::new(array.into(), None);
10463        let array = Arc::new(array) as ArrayRef;
10464        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
10465        let out = cast_with_options(&array, &interval, cast_options)?;
10466        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
10467        Ok(out)
10468    }
10469
10470    #[test]
10471    fn test_cast_from_duration_to_interval() {
10472        // from duration second to interval month day nano
10473        let array = vec![1234567];
10474        let casted_array =
10475            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
10476                .unwrap();
10477        assert_eq!(
10478            casted_array.data_type(),
10479            &DataType::Interval(IntervalUnit::MonthDayNano)
10480        );
10481        assert_eq!(
10482            casted_array.value(0),
10483            IntervalMonthDayNano::new(0, 0, 1234567000000000)
10484        );
10485
10486        let array = vec![i64::MAX];
10487        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10488            array.clone(),
10489            &CastOptions::default(),
10490        )
10491        .unwrap();
10492        assert!(!casted_array.is_valid(0));
10493
10494        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10495            array,
10496            &CastOptions {
10497                safe: false,
10498                format_options: FormatOptions::default(),
10499            },
10500        );
10501        assert!(casted_array.is_err());
10502
10503        // from duration millisecond to interval month day nano
10504        let array = vec![1234567];
10505        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10506            array,
10507            &CastOptions::default(),
10508        )
10509        .unwrap();
10510        assert_eq!(
10511            casted_array.data_type(),
10512            &DataType::Interval(IntervalUnit::MonthDayNano)
10513        );
10514        assert_eq!(
10515            casted_array.value(0),
10516            IntervalMonthDayNano::new(0, 0, 1234567000000)
10517        );
10518
10519        let array = vec![i64::MAX];
10520        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10521            array.clone(),
10522            &CastOptions::default(),
10523        )
10524        .unwrap();
10525        assert!(!casted_array.is_valid(0));
10526
10527        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10528            array,
10529            &CastOptions {
10530                safe: false,
10531                format_options: FormatOptions::default(),
10532            },
10533        );
10534        assert!(casted_array.is_err());
10535
10536        // from duration microsecond to interval month day nano
10537        let array = vec![1234567];
10538        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10539            array,
10540            &CastOptions::default(),
10541        )
10542        .unwrap();
10543        assert_eq!(
10544            casted_array.data_type(),
10545            &DataType::Interval(IntervalUnit::MonthDayNano)
10546        );
10547        assert_eq!(
10548            casted_array.value(0),
10549            IntervalMonthDayNano::new(0, 0, 1234567000)
10550        );
10551
10552        let array = vec![i64::MAX];
10553        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10554            array.clone(),
10555            &CastOptions::default(),
10556        )
10557        .unwrap();
10558        assert!(!casted_array.is_valid(0));
10559
10560        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10561            array,
10562            &CastOptions {
10563                safe: false,
10564                format_options: FormatOptions::default(),
10565            },
10566        );
10567        assert!(casted_array.is_err());
10568
10569        // from duration nanosecond to interval month day nano
10570        let array = vec![1234567];
10571        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10572            array,
10573            &CastOptions::default(),
10574        )
10575        .unwrap();
10576        assert_eq!(
10577            casted_array.data_type(),
10578            &DataType::Interval(IntervalUnit::MonthDayNano)
10579        );
10580        assert_eq!(
10581            casted_array.value(0),
10582            IntervalMonthDayNano::new(0, 0, 1234567)
10583        );
10584
10585        let array = vec![i64::MAX];
10586        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10587            array,
10588            &CastOptions {
10589                safe: false,
10590                format_options: FormatOptions::default(),
10591            },
10592        )
10593        .unwrap();
10594        assert_eq!(
10595            casted_array.value(0),
10596            IntervalMonthDayNano::new(0, 0, i64::MAX)
10597        );
10598    }
10599
10600    /// helper function to test casting from interval to duration
10601    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
10602        array: &IntervalMonthDayNanoArray,
10603        cast_options: &CastOptions,
10604    ) -> Result<PrimitiveArray<T>, ArrowError> {
10605        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
10606        casted_array
10607            .as_any()
10608            .downcast_ref::<PrimitiveArray<T>>()
10609            .ok_or_else(|| {
10610                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
10611            })
10612            .cloned()
10613    }
10614
10615    #[test]
10616    fn test_cast_from_interval_to_duration() {
10617        let nullable = CastOptions::default();
10618        let fallible = CastOptions {
10619            safe: false,
10620            format_options: FormatOptions::default(),
10621        };
10622        let v = IntervalMonthDayNano::new(0, 0, 1234567);
10623
10624        // from interval month day nano to duration second
10625        let array = vec![v].into();
10626        let casted_array: DurationSecondArray =
10627            cast_from_interval_to_duration(&array, &nullable).unwrap();
10628        assert_eq!(casted_array.value(0), 0);
10629
10630        let array = vec![IntervalMonthDayNano::MAX].into();
10631        let casted_array: DurationSecondArray =
10632            cast_from_interval_to_duration(&array, &nullable).unwrap();
10633        assert!(!casted_array.is_valid(0));
10634
10635        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
10636        assert!(res.is_err());
10637
10638        // from interval month day nano to duration millisecond
10639        let array = vec![v].into();
10640        let casted_array: DurationMillisecondArray =
10641            cast_from_interval_to_duration(&array, &nullable).unwrap();
10642        assert_eq!(casted_array.value(0), 1);
10643
10644        let array = vec![IntervalMonthDayNano::MAX].into();
10645        let casted_array: DurationMillisecondArray =
10646            cast_from_interval_to_duration(&array, &nullable).unwrap();
10647        assert!(!casted_array.is_valid(0));
10648
10649        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
10650        assert!(res.is_err());
10651
10652        // from interval month day nano to duration microsecond
10653        let array = vec![v].into();
10654        let casted_array: DurationMicrosecondArray =
10655            cast_from_interval_to_duration(&array, &nullable).unwrap();
10656        assert_eq!(casted_array.value(0), 1234);
10657
10658        let array = vec![IntervalMonthDayNano::MAX].into();
10659        let casted_array =
10660            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
10661        assert!(!casted_array.is_valid(0));
10662
10663        let casted_array =
10664            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
10665        assert!(casted_array.is_err());
10666
10667        // from interval month day nano to duration nanosecond
10668        let array = vec![v].into();
10669        let casted_array: DurationNanosecondArray =
10670            cast_from_interval_to_duration(&array, &nullable).unwrap();
10671        assert_eq!(casted_array.value(0), 1234567);
10672
10673        let array = vec![IntervalMonthDayNano::MAX].into();
10674        let casted_array: DurationNanosecondArray =
10675            cast_from_interval_to_duration(&array, &nullable).unwrap();
10676        assert!(!casted_array.is_valid(0));
10677
10678        let casted_array =
10679            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
10680        assert!(casted_array.is_err());
10681
10682        let array = vec![
10683            IntervalMonthDayNanoType::make_value(0, 1, 0),
10684            IntervalMonthDayNanoType::make_value(-1, 0, 0),
10685            IntervalMonthDayNanoType::make_value(1, 1, 0),
10686            IntervalMonthDayNanoType::make_value(1, 0, 1),
10687            IntervalMonthDayNanoType::make_value(0, 0, -1),
10688        ]
10689        .into();
10690        let casted_array =
10691            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
10692        assert!(!casted_array.is_valid(0));
10693        assert!(!casted_array.is_valid(1));
10694        assert!(!casted_array.is_valid(2));
10695        assert!(!casted_array.is_valid(3));
10696        assert!(casted_array.is_valid(4));
10697        assert_eq!(casted_array.value(4), -1);
10698    }
10699
10700    /// helper function to test casting from interval year month to interval month day nano
10701    fn cast_from_interval_year_month_to_interval_month_day_nano(
10702        array: Vec<i32>,
10703        cast_options: &CastOptions,
10704    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10705        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
10706        let array = Arc::new(array) as ArrayRef;
10707        let casted_array = cast_with_options(
10708            &array,
10709            &DataType::Interval(IntervalUnit::MonthDayNano),
10710            cast_options,
10711        )?;
10712        casted_array
10713            .as_any()
10714            .downcast_ref::<IntervalMonthDayNanoArray>()
10715            .ok_or_else(|| {
10716                ArrowError::ComputeError(
10717                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
10718                )
10719            })
10720            .cloned()
10721    }
10722
10723    #[test]
10724    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
10725        // from interval year month to interval month day nano
10726        let array = vec![1234567];
10727        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
10728            array,
10729            &CastOptions::default(),
10730        )
10731        .unwrap();
10732        assert_eq!(
10733            casted_array.data_type(),
10734            &DataType::Interval(IntervalUnit::MonthDayNano)
10735        );
10736        assert_eq!(
10737            casted_array.value(0),
10738            IntervalMonthDayNano::new(1234567, 0, 0)
10739        );
10740    }
10741
10742    /// helper function to test casting from interval day time to interval month day nano
10743    fn cast_from_interval_day_time_to_interval_month_day_nano(
10744        array: Vec<IntervalDayTime>,
10745        cast_options: &CastOptions,
10746    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10747        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
10748        let array = Arc::new(array) as ArrayRef;
10749        let casted_array = cast_with_options(
10750            &array,
10751            &DataType::Interval(IntervalUnit::MonthDayNano),
10752            cast_options,
10753        )?;
10754        Ok(casted_array
10755            .as_primitive::<IntervalMonthDayNanoType>()
10756            .clone())
10757    }
10758
10759    #[test]
10760    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
10761        // from interval day time to interval month day nano
10762        let array = vec![IntervalDayTime::new(123, 0)];
10763        let casted_array =
10764            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
10765                .unwrap();
10766        assert_eq!(
10767            casted_array.data_type(),
10768            &DataType::Interval(IntervalUnit::MonthDayNano)
10769        );
10770        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
10771    }
10772
10773    #[test]
10774    fn test_cast_below_unixtimestamp() {
10775        let valid = StringArray::from(vec![
10776            "1900-01-03 23:59:59",
10777            "1969-12-31 00:00:01",
10778            "1989-12-31 00:00:01",
10779        ]);
10780
10781        let array = Arc::new(valid) as ArrayRef;
10782        let casted_array = cast_with_options(
10783            &array,
10784            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10785            &CastOptions {
10786                safe: false,
10787                format_options: FormatOptions::default(),
10788            },
10789        )
10790        .unwrap();
10791
10792        let ts_array = casted_array
10793            .as_primitive::<TimestampNanosecondType>()
10794            .values()
10795            .iter()
10796            .map(|ts| ts / 1_000_000)
10797            .collect::<Vec<_>>();
10798
10799        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
10800        let casted_array = cast(&array, &DataType::Date32).unwrap();
10801        let date_array = casted_array.as_primitive::<Date32Type>();
10802        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
10803        let string_array = casted_array.as_string::<i32>();
10804        assert_eq!("1900-01-03", string_array.value(0));
10805        assert_eq!("1969-12-31", string_array.value(1));
10806        assert_eq!("1989-12-31", string_array.value(2));
10807    }
10808
10809    #[test]
10810    fn test_nested_list() {
10811        let mut list = ListBuilder::new(Int32Builder::new());
10812        list.append_value([Some(1), Some(2), Some(3)]);
10813        list.append_value([Some(4), None, Some(6)]);
10814        let list = list.finish();
10815
10816        let to_field = Field::new("nested", list.data_type().clone(), false);
10817        let to = DataType::List(Arc::new(to_field));
10818        let out = cast(&list, &to).unwrap();
10819        let opts = FormatOptions::default().with_null("null");
10820        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
10821
10822        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
10823        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
10824    }
10825
10826    #[test]
10827    fn test_nested_list_cast() {
10828        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
10829        builder.append_value([Some([Some(1), Some(2), None]), None]);
10830        builder.append_value([None, Some([]), None]);
10831        builder.append_null();
10832        builder.append_value([Some([Some(2), Some(3)])]);
10833        let start = builder.finish();
10834
10835        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
10836        builder.append_value([Some([Some(1), Some(2), None]), None]);
10837        builder.append_value([None, Some([]), None]);
10838        builder.append_null();
10839        builder.append_value([Some([Some(2), Some(3)])]);
10840        let expected = builder.finish();
10841
10842        let actual = cast(&start, expected.data_type()).unwrap();
10843        assert_eq!(actual.as_ref(), &expected);
10844    }
10845
10846    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
10847        safe: true,
10848        format_options: FormatOptions::new(),
10849    };
10850
10851    #[test]
10852    #[allow(clippy::assertions_on_constants)]
10853    fn test_const_options() {
10854        assert!(CAST_OPTIONS.safe)
10855    }
10856
10857    #[test]
10858    fn test_list_format_options() {
10859        let options = CastOptions {
10860            safe: false,
10861            format_options: FormatOptions::default().with_null("null"),
10862        };
10863        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10864            Some(vec![Some(0), Some(1), Some(2)]),
10865            Some(vec![Some(0), None, Some(2)]),
10866        ]);
10867        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
10868        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
10869        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
10870    }
10871    #[test]
10872    fn test_cast_string_to_timestamp_invalid_tz() {
10873        // content after Z should be ignored
10874        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
10875        let array = StringArray::from(vec![Some(bad_timestamp)]);
10876
10877        let data_types = [
10878            DataType::Timestamp(TimeUnit::Second, None),
10879            DataType::Timestamp(TimeUnit::Millisecond, None),
10880            DataType::Timestamp(TimeUnit::Microsecond, None),
10881            DataType::Timestamp(TimeUnit::Nanosecond, None),
10882        ];
10883
10884        let cast_options = CastOptions {
10885            safe: false,
10886            ..Default::default()
10887        };
10888
10889        for dt in data_types {
10890            assert_eq!(
10891                cast_with_options(&array, &dt, &cast_options)
10892                    .unwrap_err()
10893                    .to_string(),
10894                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
10895            );
10896        }
10897    }
10898    #[test]
10899    fn test_cast_struct_to_struct() {
10900        let struct_type = DataType::Struct(
10901            vec![
10902                Field::new("a", DataType::Boolean, false),
10903                Field::new("b", DataType::Int32, false),
10904            ]
10905            .into(),
10906        );
10907        let to_type = DataType::Struct(
10908            vec![
10909                Field::new("a", DataType::Utf8, false),
10910                Field::new("b", DataType::Utf8, false),
10911            ]
10912            .into(),
10913        );
10914        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10915        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
10916        let struct_array = StructArray::from(vec![
10917            (
10918                Arc::new(Field::new("b", DataType::Boolean, false)),
10919                boolean.clone() as ArrayRef,
10920            ),
10921            (
10922                Arc::new(Field::new("c", DataType::Int32, false)),
10923                int.clone() as ArrayRef,
10924            ),
10925        ]);
10926        let casted_array = cast(&struct_array, &to_type).unwrap();
10927        let casted_array = casted_array.as_struct();
10928        assert_eq!(casted_array.data_type(), &to_type);
10929        let casted_boolean_array = casted_array
10930            .column(0)
10931            .as_string::<i32>()
10932            .into_iter()
10933            .flatten()
10934            .collect::<Vec<_>>();
10935        let casted_int_array = casted_array
10936            .column(1)
10937            .as_string::<i32>()
10938            .into_iter()
10939            .flatten()
10940            .collect::<Vec<_>>();
10941        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
10942        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
10943
10944        // test for can't cast
10945        let to_type = DataType::Struct(
10946            vec![
10947                Field::new("a", DataType::Date32, false),
10948                Field::new("b", DataType::Utf8, false),
10949            ]
10950            .into(),
10951        );
10952        assert!(!can_cast_types(&struct_type, &to_type));
10953        let result = cast(&struct_array, &to_type);
10954        assert_eq!(
10955            "Cast error: Casting from Boolean to Date32 not supported",
10956            result.unwrap_err().to_string()
10957        );
10958    }
10959
10960    #[test]
10961    fn test_cast_struct_to_struct_nullability() {
10962        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10963        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
10964        let struct_array = StructArray::from(vec![
10965            (
10966                Arc::new(Field::new("b", DataType::Boolean, false)),
10967                boolean.clone() as ArrayRef,
10968            ),
10969            (
10970                Arc::new(Field::new("c", DataType::Int32, true)),
10971                int.clone() as ArrayRef,
10972            ),
10973        ]);
10974
10975        // okay: nullable to nullable
10976        let to_type = DataType::Struct(
10977            vec![
10978                Field::new("a", DataType::Utf8, false),
10979                Field::new("b", DataType::Utf8, true),
10980            ]
10981            .into(),
10982        );
10983        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
10984
10985        // error: nullable to non-nullable
10986        let to_type = DataType::Struct(
10987            vec![
10988                Field::new("a", DataType::Utf8, false),
10989                Field::new("b", DataType::Utf8, false),
10990            ]
10991            .into(),
10992        );
10993        cast(&struct_array, &to_type)
10994            .expect_err("Cast nullable to non-nullable struct field should fail");
10995
10996        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10997        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
10998        let struct_array = StructArray::from(vec![
10999            (
11000                Arc::new(Field::new("b", DataType::Boolean, false)),
11001                boolean.clone() as ArrayRef,
11002            ),
11003            (
11004                Arc::new(Field::new("c", DataType::Int32, false)),
11005                int.clone() as ArrayRef,
11006            ),
11007        ]);
11008
11009        // okay: non-nullable to non-nullable
11010        let to_type = DataType::Struct(
11011            vec![
11012                Field::new("a", DataType::Utf8, false),
11013                Field::new("b", DataType::Utf8, false),
11014            ]
11015            .into(),
11016        );
11017        cast(&struct_array, &to_type)
11018            .expect("Cast non-nullable to non-nullable struct field should work");
11019
11020        // err: non-nullable to non-nullable but overflowing return null during casting
11021        let to_type = DataType::Struct(
11022            vec![
11023                Field::new("a", DataType::Utf8, false),
11024                Field::new("b", DataType::Int8, false),
11025            ]
11026            .into(),
11027        );
11028        cast(&struct_array, &to_type).expect_err(
11029            "Cast non-nullable to non-nullable struct field returning null should fail",
11030        );
11031    }
11032
11033    #[test]
11034    fn test_cast_struct_to_non_struct() {
11035        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
11036        let struct_array = StructArray::from(vec![(
11037            Arc::new(Field::new("a", DataType::Boolean, false)),
11038            boolean.clone() as ArrayRef,
11039        )]);
11040        let to_type = DataType::Utf8;
11041        let result = cast(&struct_array, &to_type);
11042        assert_eq!(
11043            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
11044            result.unwrap_err().to_string()
11045        );
11046    }
11047
11048    #[test]
11049    fn test_cast_non_struct_to_struct() {
11050        let array = StringArray::from(vec!["a", "b"]);
11051        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
11052        let result = cast(&array, &to_type);
11053        assert_eq!(
11054            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
11055            result.unwrap_err().to_string()
11056        );
11057    }
11058
11059    #[test]
11060    fn test_cast_struct_with_different_field_order() {
11061        // Test slow path: fields are in different order
11062        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11063        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11064        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11065
11066        let struct_array = StructArray::from(vec![
11067            (
11068                Arc::new(Field::new("a", DataType::Boolean, false)),
11069                boolean.clone() as ArrayRef,
11070            ),
11071            (
11072                Arc::new(Field::new("b", DataType::Int32, false)),
11073                int.clone() as ArrayRef,
11074            ),
11075            (
11076                Arc::new(Field::new("c", DataType::Utf8, false)),
11077                string.clone() as ArrayRef,
11078            ),
11079        ]);
11080
11081        // Target has fields in different order: c, a, b instead of a, b, c
11082        let to_type = DataType::Struct(
11083            vec![
11084                Field::new("c", DataType::Utf8, false),
11085                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
11086                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
11087            ]
11088            .into(),
11089        );
11090
11091        let result = cast(&struct_array, &to_type).unwrap();
11092        let result_struct = result.as_struct();
11093
11094        assert_eq!(result_struct.data_type(), &to_type);
11095        assert_eq!(result_struct.num_columns(), 3);
11096
11097        // Verify field "c" (originally position 2, now position 0) remains Utf8
11098        let c_column = result_struct.column(0).as_string::<i32>();
11099        assert_eq!(
11100            c_column.into_iter().flatten().collect::<Vec<_>>(),
11101            vec!["foo", "bar", "baz", "qux"]
11102        );
11103
11104        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
11105        let a_column = result_struct.column(1).as_string::<i32>();
11106        assert_eq!(
11107            a_column.into_iter().flatten().collect::<Vec<_>>(),
11108            vec!["false", "false", "true", "true"]
11109        );
11110
11111        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
11112        let b_column = result_struct.column(2).as_string::<i32>();
11113        assert_eq!(
11114            b_column.into_iter().flatten().collect::<Vec<_>>(),
11115            vec!["42", "28", "19", "31"]
11116        );
11117    }
11118
11119    #[test]
11120    fn test_cast_struct_with_missing_field() {
11121        // Test that casting fails when target has a field not present in source
11122        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
11123        let struct_array = StructArray::from(vec![(
11124            Arc::new(Field::new("a", DataType::Boolean, false)),
11125            boolean.clone() as ArrayRef,
11126        )]);
11127
11128        let to_type = DataType::Struct(
11129            vec![
11130                Field::new("a", DataType::Utf8, false),
11131                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
11132            ]
11133            .into(),
11134        );
11135
11136        let result = cast(&struct_array, &to_type);
11137        assert!(result.is_err());
11138        assert_eq!(
11139            result.unwrap_err().to_string(),
11140            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
11141        );
11142    }
11143
11144    #[test]
11145    fn test_cast_struct_with_subset_of_fields() {
11146        // Test casting to a struct with fewer fields (selecting a subset)
11147        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11148        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11149        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11150
11151        let struct_array = StructArray::from(vec![
11152            (
11153                Arc::new(Field::new("a", DataType::Boolean, false)),
11154                boolean.clone() as ArrayRef,
11155            ),
11156            (
11157                Arc::new(Field::new("b", DataType::Int32, false)),
11158                int.clone() as ArrayRef,
11159            ),
11160            (
11161                Arc::new(Field::new("c", DataType::Utf8, false)),
11162                string.clone() as ArrayRef,
11163            ),
11164        ]);
11165
11166        // Target has only fields "c" and "a", omitting "b"
11167        let to_type = DataType::Struct(
11168            vec![
11169                Field::new("c", DataType::Utf8, false),
11170                Field::new("a", DataType::Utf8, false),
11171            ]
11172            .into(),
11173        );
11174
11175        let result = cast(&struct_array, &to_type).unwrap();
11176        let result_struct = result.as_struct();
11177
11178        assert_eq!(result_struct.data_type(), &to_type);
11179        assert_eq!(result_struct.num_columns(), 2);
11180
11181        // Verify field "c" remains Utf8
11182        let c_column = result_struct.column(0).as_string::<i32>();
11183        assert_eq!(
11184            c_column.into_iter().flatten().collect::<Vec<_>>(),
11185            vec!["foo", "bar", "baz", "qux"]
11186        );
11187
11188        // Verify field "a" was cast from Boolean to Utf8
11189        let a_column = result_struct.column(1).as_string::<i32>();
11190        assert_eq!(
11191            a_column.into_iter().flatten().collect::<Vec<_>>(),
11192            vec!["false", "false", "true", "true"]
11193        );
11194    }
11195
11196    #[test]
11197    fn test_can_cast_struct_rename_field() {
11198        // Test that can_cast_types returns false when target has a field not in source
11199        let from_type = DataType::Struct(
11200            vec![
11201                Field::new("a", DataType::Int32, false),
11202                Field::new("b", DataType::Utf8, false),
11203            ]
11204            .into(),
11205        );
11206
11207        let to_type = DataType::Struct(
11208            vec![
11209                Field::new("a", DataType::Int64, false),
11210                Field::new("c", DataType::Boolean, false), // Field "c" not in source
11211            ]
11212            .into(),
11213        );
11214
11215        assert!(can_cast_types(&from_type, &to_type));
11216    }
11217
11218    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
11219        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
11220        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
11221        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
11222        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
11223    }
11224
11225    #[test]
11226    fn test_decimal_to_decimal_coverage() {
11227        let test_cases = [
11228            // increase precision, increase scale, infallible
11229            DecimalCastTestConfig {
11230                input_prec: 5,
11231                input_scale: 1,
11232                input_repr: 99999, // 9999.9
11233                output_prec: 10,
11234                output_scale: 6,
11235                expected_output_repr: Ok(9999900000), // 9999.900000
11236            },
11237            // increase precision, increase scale, fallible, safe
11238            DecimalCastTestConfig {
11239                input_prec: 5,
11240                input_scale: 1,
11241                input_repr: 99, // 9999.9
11242                output_prec: 7,
11243                output_scale: 6,
11244                expected_output_repr: Ok(9900000), // 9.900000
11245            },
11246            // increase precision, increase scale, fallible, unsafe
11247            DecimalCastTestConfig {
11248                input_prec: 5,
11249                input_scale: 1,
11250                input_repr: 99999, // 9999.9
11251                output_prec: 7,
11252                output_scale: 6,
11253                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
11254            },
11255            // increase precision, decrease scale, always infallible
11256            DecimalCastTestConfig {
11257                input_prec: 5,
11258                input_scale: 3,
11259                input_repr: 99999, // 99.999
11260                output_prec: 10,
11261                output_scale: 2,
11262                expected_output_repr: Ok(10000), // 100.00
11263            },
11264            // increase precision, decrease scale, no rouding
11265            DecimalCastTestConfig {
11266                input_prec: 5,
11267                input_scale: 3,
11268                input_repr: 99994, // 99.994
11269                output_prec: 10,
11270                output_scale: 2,
11271                expected_output_repr: Ok(9999), // 99.99
11272            },
11273            // increase precision, don't change scale, always infallible
11274            DecimalCastTestConfig {
11275                input_prec: 5,
11276                input_scale: 3,
11277                input_repr: 99999, // 99.999
11278                output_prec: 10,
11279                output_scale: 3,
11280                expected_output_repr: Ok(99999), // 99.999
11281            },
11282            // decrease precision, increase scale, safe
11283            DecimalCastTestConfig {
11284                input_prec: 10,
11285                input_scale: 5,
11286                input_repr: 999999, // 9.99999
11287                output_prec: 8,
11288                output_scale: 7,
11289                expected_output_repr: Ok(99999900), // 9.9999900
11290            },
11291            // decrease precision, increase scale, unsafe
11292            DecimalCastTestConfig {
11293                input_prec: 10,
11294                input_scale: 5,
11295                input_repr: 9999999, // 99.99999
11296                output_prec: 8,
11297                output_scale: 7,
11298                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
11299            },
11300            // decrease precision, decrease scale, safe, infallible
11301            DecimalCastTestConfig {
11302                input_prec: 7,
11303                input_scale: 4,
11304                input_repr: 9999999, // 999.9999
11305                output_prec: 6,
11306                output_scale: 2,
11307                expected_output_repr: Ok(100000),
11308            },
11309            // decrease precision, decrease scale, safe, fallible
11310            DecimalCastTestConfig {
11311                input_prec: 10,
11312                input_scale: 5,
11313                input_repr: 12345678, // 123.45678
11314                output_prec: 8,
11315                output_scale: 3,
11316                expected_output_repr: Ok(123457), // 123.457
11317            },
11318            // decrease precision, decrease scale, unsafe
11319            DecimalCastTestConfig {
11320                input_prec: 10,
11321                input_scale: 5,
11322                input_repr: 9999999, // 99.99999
11323                output_prec: 4,
11324                output_scale: 3,
11325                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
11326            },
11327            // decrease precision, same scale, safe
11328            DecimalCastTestConfig {
11329                input_prec: 10,
11330                input_scale: 5,
11331                input_repr: 999999, // 9.99999
11332                output_prec: 6,
11333                output_scale: 5,
11334                expected_output_repr: Ok(999999), // 9.99999
11335            },
11336            // decrease precision, same scale, unsafe
11337            DecimalCastTestConfig {
11338                input_prec: 10,
11339                input_scale: 5,
11340                input_repr: 9999999, // 99.99999
11341                output_prec: 6,
11342                output_scale: 5,
11343                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
11344            },
11345            // same precision, increase scale, safe
11346            DecimalCastTestConfig {
11347                input_prec: 7,
11348                input_scale: 4,
11349                input_repr: 12345, // 1.2345
11350                output_prec: 7,
11351                output_scale: 6,
11352                expected_output_repr: Ok(1234500), // 1.234500
11353            },
11354            // same precision, increase scale, unsafe
11355            DecimalCastTestConfig {
11356                input_prec: 7,
11357                input_scale: 4,
11358                input_repr: 123456, // 12.3456
11359                output_prec: 7,
11360                output_scale: 6,
11361                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
11362            },
11363            // same precision, decrease scale, infallible
11364            DecimalCastTestConfig {
11365                input_prec: 7,
11366                input_scale: 5,
11367                input_repr: 1234567, // 12.34567
11368                output_prec: 7,
11369                output_scale: 4,
11370                expected_output_repr: Ok(123457), // 12.3457
11371            },
11372            // same precision, same scale, infallible
11373            DecimalCastTestConfig {
11374                input_prec: 7,
11375                input_scale: 5,
11376                input_repr: 9999999, // 99.99999
11377                output_prec: 7,
11378                output_scale: 5,
11379                expected_output_repr: Ok(9999999), // 99.99999
11380            },
11381            // precision increase, input scale & output scale = 0, infallible
11382            DecimalCastTestConfig {
11383                input_prec: 7,
11384                input_scale: 0,
11385                input_repr: 1234567, // 1234567
11386                output_prec: 8,
11387                output_scale: 0,
11388                expected_output_repr: Ok(1234567), // 1234567
11389            },
11390            // precision decrease, input scale & output scale = 0, failure
11391            DecimalCastTestConfig {
11392                input_prec: 7,
11393                input_scale: 0,
11394                input_repr: 1234567, // 1234567
11395                output_prec: 6,
11396                output_scale: 0,
11397                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
11398            },
11399            // precision decrease, input scale & output scale = 0, success
11400            DecimalCastTestConfig {
11401                input_prec: 7,
11402                input_scale: 0,
11403                input_repr: 123456, // 123456
11404                output_prec: 6,
11405                output_scale: 0,
11406                expected_output_repr: Ok(123456), // 123456
11407            },
11408        ];
11409
11410        for t in test_cases {
11411            run_decimal_cast_test_case_between_multiple_types(t);
11412        }
11413    }
11414
11415    #[test]
11416    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
11417        let test_cases = [
11418            DecimalCastTestConfig {
11419                input_prec: 5,
11420                input_scale: 0,
11421                input_repr: 99999,
11422                output_prec: 10,
11423                output_scale: 5,
11424                expected_output_repr: Ok(9999900000),
11425            },
11426            DecimalCastTestConfig {
11427                input_prec: 5,
11428                input_scale: 0,
11429                input_repr: -99999,
11430                output_prec: 10,
11431                output_scale: 5,
11432                expected_output_repr: Ok(-9999900000),
11433            },
11434            DecimalCastTestConfig {
11435                input_prec: 5,
11436                input_scale: 2,
11437                input_repr: 99999,
11438                output_prec: 10,
11439                output_scale: 5,
11440                expected_output_repr: Ok(99999000),
11441            },
11442            DecimalCastTestConfig {
11443                input_prec: 5,
11444                input_scale: -2,
11445                input_repr: -99999,
11446                output_prec: 10,
11447                output_scale: 3,
11448                expected_output_repr: Ok(-9999900000),
11449            },
11450            DecimalCastTestConfig {
11451                input_prec: 5,
11452                input_scale: 3,
11453                input_repr: -12345,
11454                output_prec: 6,
11455                output_scale: 5,
11456                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())
11457            },
11458        ];
11459
11460        for t in test_cases {
11461            run_decimal_cast_test_case_between_multiple_types(t);
11462        }
11463    }
11464
11465    #[test]
11466    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
11467        let test_cases = [
11468            DecimalCastTestConfig {
11469                input_prec: 5,
11470                input_scale: 0,
11471                input_repr: 99999,
11472                output_scale: -3,
11473                output_prec: 3,
11474                expected_output_repr: Ok(100),
11475            },
11476            DecimalCastTestConfig {
11477                input_prec: 5,
11478                input_scale: 0,
11479                input_repr: -99999,
11480                output_prec: 1,
11481                output_scale: -5,
11482                expected_output_repr: Ok(-1),
11483            },
11484            DecimalCastTestConfig {
11485                input_prec: 10,
11486                input_scale: 2,
11487                input_repr: 123456789,
11488                output_prec: 5,
11489                output_scale: -2,
11490                expected_output_repr: Ok(12346),
11491            },
11492            DecimalCastTestConfig {
11493                input_prec: 10,
11494                input_scale: 4,
11495                input_repr: -9876543210,
11496                output_prec: 7,
11497                output_scale: 0,
11498                expected_output_repr: Ok(-987654),
11499            },
11500            DecimalCastTestConfig {
11501                input_prec: 7,
11502                input_scale: 4,
11503                input_repr: 9999999,
11504                output_prec: 6,
11505                output_scale: 3,
11506                expected_output_repr:
11507                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
11508            },
11509        ];
11510        for t in test_cases {
11511            run_decimal_cast_test_case_between_multiple_types(t);
11512        }
11513    }
11514
11515    #[test]
11516    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
11517        let array = vec![Some(123456789)];
11518        let array = create_decimal128_array(array, 24, 2).unwrap();
11519        let input_type = DataType::Decimal128(24, 2);
11520        let output_type = DataType::Decimal128(6, 2);
11521        assert!(can_cast_types(&input_type, &output_type));
11522
11523        let options = CastOptions {
11524            safe: false,
11525            ..Default::default()
11526        };
11527        let result = cast_with_options(&array, &output_type, &options);
11528        assert_eq!(
11529            result.unwrap_err().to_string(),
11530            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11531        );
11532    }
11533
11534    #[test]
11535    fn test_decimal_to_decimal_same_scale() {
11536        let array = vec![Some(520)];
11537        let array = create_decimal128_array(array, 4, 2).unwrap();
11538        let input_type = DataType::Decimal128(4, 2);
11539        let output_type = DataType::Decimal128(3, 2);
11540        assert!(can_cast_types(&input_type, &output_type));
11541
11542        let options = CastOptions {
11543            safe: false,
11544            ..Default::default()
11545        };
11546        let result = cast_with_options(&array, &output_type, &options);
11547        assert_eq!(
11548            result.unwrap().as_primitive::<Decimal128Type>().value(0),
11549            520
11550        );
11551
11552        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
11553        assert_eq!(
11554            &cast(
11555                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
11556                &DataType::Decimal128(2, 0)
11557            )
11558            .unwrap(),
11559            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
11560        );
11561    }
11562
11563    #[test]
11564    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
11565        let array = vec![Some(123456789)];
11566        let array = create_decimal128_array(array, 24, 4).unwrap();
11567        let input_type = DataType::Decimal128(24, 4);
11568        let output_type = DataType::Decimal128(6, 2);
11569        assert!(can_cast_types(&input_type, &output_type));
11570
11571        let options = CastOptions {
11572            safe: false,
11573            ..Default::default()
11574        };
11575        let result = cast_with_options(&array, &output_type, &options);
11576        assert_eq!(
11577            result.unwrap_err().to_string(),
11578            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11579        );
11580    }
11581
11582    #[test]
11583    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
11584        let array = vec![Some(123456789)];
11585        let array = create_decimal128_array(array, 24, 2).unwrap();
11586        let input_type = DataType::Decimal128(24, 2);
11587        let output_type = DataType::Decimal128(6, 3);
11588        assert!(can_cast_types(&input_type, &output_type));
11589
11590        let options = CastOptions {
11591            safe: false,
11592            ..Default::default()
11593        };
11594        let result = cast_with_options(&array, &output_type, &options);
11595        assert_eq!(
11596            result.unwrap_err().to_string(),
11597            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
11598        );
11599    }
11600
11601    #[test]
11602    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
11603        let array = vec![Some(123456789)];
11604        let array = create_decimal128_array(array, 24, 2).unwrap();
11605        let input_type = DataType::Decimal128(24, 2);
11606        let output_type = DataType::Decimal256(6, 2);
11607        assert!(can_cast_types(&input_type, &output_type));
11608
11609        let options = CastOptions {
11610            safe: false,
11611            ..Default::default()
11612        };
11613        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
11614        assert_eq!(
11615            result.to_string(),
11616            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
11617        );
11618    }
11619
11620    #[test]
11621    fn test_first_none() {
11622        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11623            None,
11624            Some(vec![Some(1), Some(2)]),
11625        ])) as ArrayRef;
11626        let data_type =
11627            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11628        let opt = CastOptions::default();
11629        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11630
11631        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11632            vec![None, Some(vec![Some(1), Some(2)])],
11633            2,
11634        )) as ArrayRef;
11635        assert_eq!(*fixed_array, *r);
11636    }
11637
11638    #[test]
11639    fn test_first_last_none() {
11640        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11641            None,
11642            Some(vec![Some(1), Some(2)]),
11643            None,
11644        ])) as ArrayRef;
11645        let data_type =
11646            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11647        let opt = CastOptions::default();
11648        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11649
11650        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11651            vec![None, Some(vec![Some(1), Some(2)]), None],
11652            2,
11653        )) as ArrayRef;
11654        assert_eq!(*fixed_array, *r);
11655    }
11656
11657    #[test]
11658    fn test_cast_decimal_error_output() {
11659        let array = Int64Array::from(vec![1]);
11660        let error = cast_with_options(
11661            &array,
11662            &DataType::Decimal32(1, 1),
11663            &CastOptions {
11664                safe: false,
11665                format_options: FormatOptions::default(),
11666            },
11667        )
11668        .unwrap_err();
11669        assert_eq!(
11670            error.to_string(),
11671            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
11672        );
11673
11674        let array = Int64Array::from(vec![-1]);
11675        let error = cast_with_options(
11676            &array,
11677            &DataType::Decimal32(1, 1),
11678            &CastOptions {
11679                safe: false,
11680                format_options: FormatOptions::default(),
11681            },
11682        )
11683        .unwrap_err();
11684        assert_eq!(
11685            error.to_string(),
11686            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
11687        );
11688    }
11689
11690    #[test]
11691    fn test_run_end_encoded_to_primitive() {
11692        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
11693        let run_ends = Int32Array::from(vec![2, 5, 6]);
11694        let values = Int32Array::from(vec![1, 2, 3]);
11695        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11696        let array_ref = Arc::new(run_array) as ArrayRef;
11697        // Cast to Int64
11698        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
11699        // Verify the result is a RunArray with Int64 values
11700        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
11701        assert_eq!(
11702            result_run_array.values(),
11703            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
11704        );
11705    }
11706
11707    #[test]
11708    fn test_sliced_run_end_encoded_to_primitive() {
11709        let run_ends = Int32Array::from(vec![2, 5, 6]);
11710        let values = Int32Array::from(vec![1, 2, 3]);
11711        // [1, 1, 2, 2, 2, 3]
11712        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11713        let run_array = run_array.slice(3, 3); // [2, 2, 3]
11714        let array_ref = Arc::new(run_array) as ArrayRef;
11715
11716        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
11717        let result_run_array = cast_result.as_primitive::<Int64Type>();
11718        assert_eq!(result_run_array.values(), &[2, 2, 3]);
11719    }
11720
11721    #[test]
11722    fn test_run_end_encoded_to_string() {
11723        let run_ends = Int32Array::from(vec![2, 3, 5]);
11724        let values = Int32Array::from(vec![10, 20, 30]);
11725        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11726        let array_ref = Arc::new(run_array) as ArrayRef;
11727
11728        // Cast to String
11729        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
11730
11731        // Verify the result is a RunArray with String values
11732        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
11733        // Check that values are correct
11734        assert_eq!(result_array.value(0), "10");
11735        assert_eq!(result_array.value(1), "10");
11736        assert_eq!(result_array.value(2), "20");
11737    }
11738
11739    #[test]
11740    fn test_primitive_to_run_end_encoded() {
11741        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
11742        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
11743        let array_ref = Arc::new(source_array) as ArrayRef;
11744
11745        // Cast to RunEndEncoded<Int32, Int32>
11746        let target_type = DataType::RunEndEncoded(
11747            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11748            Arc::new(Field::new("values", DataType::Int32, true)),
11749        );
11750        let cast_result = cast(&array_ref, &target_type).unwrap();
11751
11752        // Verify the result is a RunArray
11753        let result_run_array = cast_result
11754            .as_any()
11755            .downcast_ref::<RunArray<Int32Type>>()
11756            .unwrap();
11757
11758        // Check run structure: runs should end at positions [2, 5, 6]
11759        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
11760
11761        // Check values: should be [1, 2, 3]
11762        let values_array = result_run_array.values().as_primitive::<Int32Type>();
11763        assert_eq!(values_array.values(), &[1, 2, 3]);
11764    }
11765
11766    #[test]
11767    fn test_primitive_to_run_end_encoded_with_nulls() {
11768        let source_array = Int32Array::from(vec![
11769            Some(1),
11770            Some(1),
11771            None,
11772            None,
11773            Some(2),
11774            Some(2),
11775            Some(3),
11776            Some(3),
11777            None,
11778            None,
11779            Some(4),
11780            Some(4),
11781            Some(5),
11782            Some(5),
11783            None,
11784            None,
11785        ]);
11786        let array_ref = Arc::new(source_array) as ArrayRef;
11787        let target_type = DataType::RunEndEncoded(
11788            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11789            Arc::new(Field::new("values", DataType::Int32, true)),
11790        );
11791        let cast_result = cast(&array_ref, &target_type).unwrap();
11792        let result_run_array = cast_result
11793            .as_any()
11794            .downcast_ref::<RunArray<Int32Type>>()
11795            .unwrap();
11796        assert_eq!(
11797            result_run_array.run_ends().values(),
11798            &[2, 4, 6, 8, 10, 12, 14, 16]
11799        );
11800        assert_eq!(
11801            result_run_array
11802                .values()
11803                .as_primitive::<Int32Type>()
11804                .values(),
11805            &[1, 0, 2, 3, 0, 4, 5, 0]
11806        );
11807        assert_eq!(result_run_array.values().null_count(), 3);
11808    }
11809
11810    #[test]
11811    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
11812        let source_array = Int64Array::from(vec![
11813            Some(1),
11814            Some(1),
11815            None,
11816            None,
11817            None,
11818            None,
11819            None,
11820            None,
11821            None,
11822            None,
11823            Some(4),
11824            Some(20),
11825            Some(500),
11826            Some(500),
11827            None,
11828            None,
11829        ]);
11830        let array_ref = Arc::new(source_array) as ArrayRef;
11831        let target_type = DataType::RunEndEncoded(
11832            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11833            Arc::new(Field::new("values", DataType::Int64, true)),
11834        );
11835        let cast_result = cast(&array_ref, &target_type).unwrap();
11836        let result_run_array = cast_result
11837            .as_any()
11838            .downcast_ref::<RunArray<Int16Type>>()
11839            .unwrap();
11840        assert_eq!(
11841            result_run_array.run_ends().values(),
11842            &[2, 10, 11, 12, 14, 16]
11843        );
11844        assert_eq!(
11845            result_run_array
11846                .values()
11847                .as_primitive::<Int64Type>()
11848                .values(),
11849            &[1, 0, 4, 20, 500, 0]
11850        );
11851        assert_eq!(result_run_array.values().null_count(), 2);
11852    }
11853
11854    #[test]
11855    fn test_string_to_run_end_encoded() {
11856        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
11857        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
11858        let array_ref = Arc::new(source_array) as ArrayRef;
11859
11860        // Cast to RunEndEncoded<Int32, String>
11861        let target_type = DataType::RunEndEncoded(
11862            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11863            Arc::new(Field::new("values", DataType::Utf8, true)),
11864        );
11865        let cast_result = cast(&array_ref, &target_type).unwrap();
11866
11867        // Verify the result is a RunArray
11868        let result_run_array = cast_result
11869            .as_any()
11870            .downcast_ref::<RunArray<Int32Type>>()
11871            .unwrap();
11872
11873        // Check run structure: runs should end at positions [2, 3, 5]
11874        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
11875
11876        // Check values: should be ["a", "b", "c"]
11877        let values_array = result_run_array.values().as_string::<i32>();
11878        assert_eq!(values_array.value(0), "a");
11879        assert_eq!(values_array.value(1), "b");
11880        assert_eq!(values_array.value(2), "c");
11881    }
11882
11883    #[test]
11884    fn test_empty_array_to_run_end_encoded() {
11885        // Create an empty Int32 array
11886        let source_array = Int32Array::from(Vec::<i32>::new());
11887        let array_ref = Arc::new(source_array) as ArrayRef;
11888
11889        // Cast to RunEndEncoded<Int32, Int32>
11890        let target_type = DataType::RunEndEncoded(
11891            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11892            Arc::new(Field::new("values", DataType::Int32, true)),
11893        );
11894        let cast_result = cast(&array_ref, &target_type).unwrap();
11895
11896        // Verify the result is an empty RunArray
11897        let result_run_array = cast_result
11898            .as_any()
11899            .downcast_ref::<RunArray<Int32Type>>()
11900            .unwrap();
11901
11902        // Check that both run_ends and values are empty
11903        assert_eq!(result_run_array.run_ends().len(), 0);
11904        assert_eq!(result_run_array.values().len(), 0);
11905    }
11906
11907    #[test]
11908    fn test_run_end_encoded_with_nulls() {
11909        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
11910        let run_ends = Int32Array::from(vec![2, 3, 5]);
11911        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
11912        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11913        let array_ref = Arc::new(run_array) as ArrayRef;
11914
11915        // Cast to String
11916        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
11917
11918        // Verify the result preserves nulls
11919        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
11920        assert_eq!(result_run_array.value(0), "1");
11921        assert!(result_run_array.is_null(2));
11922        assert_eq!(result_run_array.value(4), "2");
11923    }
11924
11925    #[test]
11926    fn test_different_index_types() {
11927        // Test with Int16 index type
11928        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
11929        let array_ref = Arc::new(source_array) as ArrayRef;
11930
11931        let target_type = DataType::RunEndEncoded(
11932            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11933            Arc::new(Field::new("values", DataType::Int32, true)),
11934        );
11935        let cast_result = cast(&array_ref, &target_type).unwrap();
11936        assert_eq!(cast_result.data_type(), &target_type);
11937
11938        // Verify the cast worked correctly: values are [1, 2, 3]
11939        // and run-ends are [2, 3, 5]
11940        let run_array = cast_result
11941            .as_any()
11942            .downcast_ref::<RunArray<Int16Type>>()
11943            .unwrap();
11944        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
11945        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
11946        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
11947        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
11948
11949        // Test again with Int64 index type
11950        let target_type = DataType::RunEndEncoded(
11951            Arc::new(Field::new("run_ends", DataType::Int64, false)),
11952            Arc::new(Field::new("values", DataType::Int32, true)),
11953        );
11954        let cast_result = cast(&array_ref, &target_type).unwrap();
11955        assert_eq!(cast_result.data_type(), &target_type);
11956
11957        // Verify the cast worked correctly: values are [1, 2, 3]
11958        // and run-ends are [2, 3, 5]
11959        let run_array = cast_result
11960            .as_any()
11961            .downcast_ref::<RunArray<Int64Type>>()
11962            .unwrap();
11963        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
11964        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
11965        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
11966        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
11967    }
11968
11969    #[test]
11970    fn test_unsupported_cast_to_run_end_encoded() {
11971        // Create a Struct array - complex nested type that might not be supported
11972        let field = Field::new("item", DataType::Int32, false);
11973        let struct_array = StructArray::from(vec![(
11974            Arc::new(field),
11975            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
11976        )]);
11977        let array_ref = Arc::new(struct_array) as ArrayRef;
11978
11979        // This should fail because:
11980        // 1. The target type is not RunEndEncoded
11981        // 2. The target type is not supported for casting from StructArray
11982        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
11983
11984        // Expect this to fail
11985        assert!(cast_result.is_err());
11986    }
11987
11988    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
11989    #[test]
11990    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
11991        // Construct a valid REE array with Int64 run-ends
11992        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
11993        let values = StringArray::from(vec!["a", "b", "c"]);
11994
11995        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
11996        let array_ref = Arc::new(ree_array) as ArrayRef;
11997
11998        // Attempt to cast to RunEndEncoded<Int16, Utf8>
11999        let target_type = DataType::RunEndEncoded(
12000            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12001            Arc::new(Field::new("values", DataType::Utf8, true)),
12002        );
12003        let cast_options = CastOptions {
12004            safe: false, // This should make it fail instead of returning nulls
12005            format_options: FormatOptions::default(),
12006        };
12007
12008        // This should fail due to run-end overflow
12009        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12010            cast_with_options(&array_ref, &target_type, &cast_options);
12011
12012        let e = result.expect_err("Cast should have failed but succeeded");
12013        assert!(
12014            e.to_string()
12015                .contains("Cast error: Can't cast value 100000 to type Int16")
12016        );
12017    }
12018
12019    #[test]
12020    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
12021        // Construct a valid REE array with Int64 run-ends
12022        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12023        let values = StringArray::from(vec!["a", "b", "c"]);
12024
12025        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12026        let array_ref = Arc::new(ree_array) as ArrayRef;
12027
12028        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12029        let target_type = DataType::RunEndEncoded(
12030            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12031            Arc::new(Field::new("values", DataType::Utf8, true)),
12032        );
12033        let cast_options = CastOptions {
12034            safe: true,
12035            format_options: FormatOptions::default(),
12036        };
12037
12038        // This fails even though safe is true because the run_ends array has null values
12039        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12040            cast_with_options(&array_ref, &target_type, &cast_options);
12041        let e = result.expect_err("Cast should have failed but succeeded");
12042        assert!(
12043            e.to_string()
12044                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
12045        );
12046    }
12047
12048    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
12049    #[test]
12050    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
12051        // Construct a valid REE array with Int16 run-ends
12052        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
12053        let values = StringArray::from(vec!["a", "b", "c"]);
12054
12055        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12056        let array_ref = Arc::new(ree_array) as ArrayRef;
12057
12058        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
12059        let target_type = DataType::RunEndEncoded(
12060            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12061            Arc::new(Field::new("values", DataType::Utf8, true)),
12062        );
12063        let cast_options = CastOptions {
12064            safe: false,
12065            format_options: FormatOptions::default(),
12066        };
12067
12068        // This should succeed due to valid upcast
12069        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12070            cast_with_options(&array_ref, &target_type, &cast_options);
12071
12072        let array_ref = result.expect("Cast should have succeeded but failed");
12073        // Downcast to RunArray<Int64Type>
12074        let run_array = array_ref
12075            .as_any()
12076            .downcast_ref::<RunArray<Int64Type>>()
12077            .unwrap();
12078
12079        // Verify the cast worked correctly
12080        // Assert the values were cast correctly
12081        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
12082        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
12083        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
12084        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12085    }
12086
12087    #[test]
12088    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
12089        // Construct a valid dictionary encoded array
12090        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
12091        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
12092        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
12093
12094        // Attempt to cast to RunEndEncoded<Int64, Utf8>
12095        let target_type = DataType::RunEndEncoded(
12096            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12097            Arc::new(Field::new("values", DataType::Utf8, true)),
12098        );
12099        let cast_options = CastOptions {
12100            safe: false,
12101            format_options: FormatOptions::default(),
12102        };
12103
12104        // This should succeed
12105        let result = cast_with_options(&array_ref, &target_type, &cast_options)
12106            .expect("Cast should have succeeded but failed");
12107
12108        // Verify the cast worked correctly
12109        // Assert the values were cast correctly
12110        let run_array = result
12111            .as_any()
12112            .downcast_ref::<RunArray<Int64Type>>()
12113            .unwrap();
12114        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
12115        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
12116        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12117
12118        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
12119        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
12120    }
12121
12122    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
12123        vec![
12124            Some(vec![Some(1), Some(2), Some(3)]),
12125            Some(vec![Some(4), Some(5), Some(6)]),
12126            None,
12127            Some(vec![Some(7), Some(8), Some(9)]),
12128            Some(vec![None, Some(10)]),
12129        ]
12130    }
12131
12132    #[test]
12133    fn test_cast_list_view_to_list() {
12134        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12135        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12136        assert!(can_cast_types(list_view.data_type(), &target_type));
12137        let cast_result = cast(&list_view, &target_type).unwrap();
12138        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12139        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12140        assert_eq!(got_list, &expected_list);
12141    }
12142
12143    #[test]
12144    fn test_cast_list_to_list_view() {
12145        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12146        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12147        assert!(can_cast_types(list.data_type(), &target_type));
12148        let cast_result = cast(&list, &target_type).unwrap();
12149
12150        let got_list_view = cast_result
12151            .as_any()
12152            .downcast_ref::<ListViewArray>()
12153            .unwrap();
12154        let expected_list_view =
12155            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12156        assert_eq!(got_list_view, &expected_list_view);
12157    }
12158
12159    #[test]
12160    fn test_cast_large_list_view_to_large_list() {
12161        let list_view =
12162            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12163        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
12164        assert!(can_cast_types(list_view.data_type(), &target_type));
12165        let cast_result = cast(&list_view, &target_type).unwrap();
12166        let got_list = cast_result
12167            .as_any()
12168            .downcast_ref::<LargeListArray>()
12169            .unwrap();
12170
12171        let expected_list =
12172            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12173        assert_eq!(got_list, &expected_list);
12174    }
12175
12176    #[test]
12177    fn test_cast_large_list_to_large_list_view() {
12178        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12179        let target_type =
12180            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
12181        assert!(can_cast_types(list.data_type(), &target_type));
12182        let cast_result = cast(&list, &target_type).unwrap();
12183
12184        let got_list_view = cast_result
12185            .as_any()
12186            .downcast_ref::<LargeListViewArray>()
12187            .unwrap();
12188        let expected_list_view =
12189            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12190        assert_eq!(got_list_view, &expected_list_view);
12191    }
12192
12193    #[test]
12194    fn test_cast_list_view_to_list_out_of_order() {
12195        let list_view = ListViewArray::new(
12196            Arc::new(Field::new("item", DataType::Int32, true)),
12197            ScalarBuffer::from(vec![0, 6, 3]),
12198            ScalarBuffer::from(vec![3, 3, 3]),
12199            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
12200            None,
12201        );
12202        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12203        assert!(can_cast_types(list_view.data_type(), &target_type));
12204        let cast_result = cast(&list_view, &target_type).unwrap();
12205        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12206        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12207            Some(vec![Some(1), Some(2), Some(3)]),
12208            Some(vec![Some(7), Some(8), Some(9)]),
12209            Some(vec![Some(4), Some(5), Some(6)]),
12210        ]);
12211        assert_eq!(got_list, &expected_list);
12212    }
12213
12214    #[test]
12215    fn test_cast_list_view_to_list_overlapping() {
12216        let list_view = ListViewArray::new(
12217            Arc::new(Field::new("item", DataType::Int32, true)),
12218            ScalarBuffer::from(vec![0, 0]),
12219            ScalarBuffer::from(vec![1, 2]),
12220            Arc::new(Int32Array::from(vec![1, 2])),
12221            None,
12222        );
12223        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12224        assert!(can_cast_types(list_view.data_type(), &target_type));
12225        let cast_result = cast(&list_view, &target_type).unwrap();
12226        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12227        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12228            Some(vec![Some(1)]),
12229            Some(vec![Some(1), Some(2)]),
12230        ]);
12231        assert_eq!(got_list, &expected_list);
12232    }
12233
12234    #[test]
12235    fn test_cast_list_view_to_list_empty() {
12236        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
12237        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
12238        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12239        assert!(can_cast_types(list_view.data_type(), &target_type));
12240        let cast_result = cast(&list_view, &target_type).unwrap();
12241        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12242        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
12243        assert_eq!(got_list, &expected_list);
12244    }
12245
12246    #[test]
12247    fn test_cast_list_view_to_list_different_inner_type() {
12248        let values = int32_list_values();
12249        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
12250        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
12251        assert!(can_cast_types(list_view.data_type(), &target_type));
12252        let cast_result = cast(&list_view, &target_type).unwrap();
12253        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12254
12255        let expected_list =
12256            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
12257                list.map(|list| {
12258                    list.into_iter()
12259                        .map(|v| v.map(|v| v as i64))
12260                        .collect::<Vec<_>>()
12261                })
12262            }));
12263        assert_eq!(got_list, &expected_list);
12264    }
12265
12266    #[test]
12267    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
12268        let list_view = ListViewArray::new(
12269            Arc::new(Field::new("item", DataType::Int32, true)),
12270            ScalarBuffer::from(vec![0, 6, 3]),
12271            ScalarBuffer::from(vec![3, 3, 3]),
12272            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
12273            Some(NullBuffer::from(vec![false, true, false])),
12274        );
12275        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12276        assert!(can_cast_types(list_view.data_type(), &target_type));
12277        let cast_result = cast(&list_view, &target_type).unwrap();
12278        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12279        let expected_list = ListArray::new(
12280            Arc::new(Field::new("item", DataType::Int32, true)),
12281            OffsetBuffer::from_lengths([3, 3, 3]),
12282            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
12283            Some(NullBuffer::from(vec![false, true, false])),
12284        );
12285        assert_eq!(got_list, &expected_list);
12286    }
12287
12288    #[test]
12289    fn test_cast_list_view_to_large_list_view() {
12290        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12291        let target_type =
12292            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
12293        assert!(can_cast_types(list_view.data_type(), &target_type));
12294        let cast_result = cast(&list_view, &target_type).unwrap();
12295        let got = cast_result
12296            .as_any()
12297            .downcast_ref::<LargeListViewArray>()
12298            .unwrap();
12299
12300        let expected =
12301            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12302        assert_eq!(got, &expected);
12303    }
12304
12305    #[test]
12306    fn test_cast_large_list_view_to_list_view() {
12307        let list_view =
12308            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12309        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12310        assert!(can_cast_types(list_view.data_type(), &target_type));
12311        let cast_result = cast(&list_view, &target_type).unwrap();
12312        let got = cast_result
12313            .as_any()
12314            .downcast_ref::<ListViewArray>()
12315            .unwrap();
12316
12317        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12318        assert_eq!(got, &expected);
12319    }
12320
12321    #[test]
12322    fn test_cast_time32_second_to_int64() {
12323        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
12324        let array = Arc::new(array) as Arc<dyn Array>;
12325        let to_type = DataType::Int64;
12326        let cast_options = CastOptions::default();
12327
12328        assert!(can_cast_types(array.data_type(), &to_type));
12329
12330        let result = cast_with_options(&array, &to_type, &cast_options);
12331        assert!(
12332            result.is_ok(),
12333            "Failed to cast Time32(Second) to Int64: {:?}",
12334            result.err()
12335        );
12336
12337        let cast_array = result.unwrap();
12338        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12339
12340        assert_eq!(cast_array.value(0), 1000);
12341        assert_eq!(cast_array.value(1), 2000);
12342        assert_eq!(cast_array.value(2), 3000);
12343    }
12344
12345    #[test]
12346    fn test_cast_time32_millisecond_to_int64() {
12347        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
12348        let array = Arc::new(array) as Arc<dyn Array>;
12349        let to_type = DataType::Int64;
12350        let cast_options = CastOptions::default();
12351
12352        assert!(can_cast_types(array.data_type(), &to_type));
12353
12354        let result = cast_with_options(&array, &to_type, &cast_options);
12355        assert!(
12356            result.is_ok(),
12357            "Failed to cast Time32(Millisecond) to Int64: {:?}",
12358            result.err()
12359        );
12360
12361        let cast_array = result.unwrap();
12362        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12363
12364        assert_eq!(cast_array.value(0), 1000);
12365        assert_eq!(cast_array.value(1), 2000);
12366        assert_eq!(cast_array.value(2), 3000);
12367    }
12368
12369    #[test]
12370    fn test_cast_string_to_time32_second_to_int64() {
12371        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
12372        // raised in https://github.com/apache/datafusion/issues/19036
12373        let array = StringArray::from(vec!["03:12:44"]);
12374        let array = Arc::new(array) as Arc<dyn Array>;
12375        let cast_options = CastOptions::default();
12376
12377        // 1. Cast String to Time32(Second)
12378        let time32_type = DataType::Time32(TimeUnit::Second);
12379        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
12380
12381        // 2. Cast Time32(Second) to Int64
12382        let int64_type = DataType::Int64;
12383        assert!(can_cast_types(time32_array.data_type(), &int64_type));
12384
12385        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
12386
12387        assert!(
12388            result.is_ok(),
12389            "Failed to cast Time32(Second) to Int64: {:?}",
12390            result.err()
12391        );
12392
12393        let cast_array = result.unwrap();
12394        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12395
12396        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
12397        assert_eq!(cast_array.value(0), 11564);
12398    }
12399    #[test]
12400    fn test_string_dicts_to_binary_view() {
12401        let expected = BinaryViewArray::from_iter(vec![
12402            VIEW_TEST_DATA[1],
12403            VIEW_TEST_DATA[0],
12404            None,
12405            VIEW_TEST_DATA[3],
12406            None,
12407            VIEW_TEST_DATA[1],
12408            VIEW_TEST_DATA[4],
12409        ]);
12410
12411        let values_arrays: [ArrayRef; _] = [
12412            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
12413            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
12414            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
12415        ];
12416        for values in values_arrays {
12417            let keys =
12418                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
12419            let string_dict_array =
12420                DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
12421
12422            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
12423            assert_eq!(casted.as_ref(), &expected);
12424        }
12425    }
12426
12427    #[test]
12428    fn test_binary_dicts_to_string_view() {
12429        let expected = StringViewArray::from_iter(vec![
12430            VIEW_TEST_DATA[1],
12431            VIEW_TEST_DATA[0],
12432            None,
12433            VIEW_TEST_DATA[3],
12434            None,
12435            VIEW_TEST_DATA[1],
12436            VIEW_TEST_DATA[4],
12437        ]);
12438
12439        let values_arrays: [ArrayRef; _] = [
12440            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
12441            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
12442            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
12443        ];
12444        for values in values_arrays {
12445            let keys =
12446                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
12447            let string_dict_array =
12448                DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
12449
12450            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
12451            assert_eq!(casted.as_ref(), &expected);
12452        }
12453    }
12454}