arrow_cast/cast/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Cast kernels to convert [`ArrayRef`]  between supported datatypes.
19//!
20//! See [`cast_with_options`] for more information on specific conversions.
21//!
22//! Example:
23//!
24//! ```
25//! # use arrow_array::*;
26//! # use arrow_cast::cast;
27//! # use arrow_schema::DataType;
28//! # use std::sync::Arc;
29//! # use arrow_array::types::Float64Type;
30//! # use arrow_array::cast::AsArray;
31//! // int32 to float64
32//! let a = Int32Array::from(vec![5, 6, 7]);
33//! let b = cast(&a, &DataType::Float64).unwrap();
34//! let c = b.as_primitive::<Float64Type>();
35//! assert_eq!(5.0, c.value(0));
36//! assert_eq!(6.0, c.value(1));
37//! assert_eq!(7.0, c.value(2));
38//! ```
39
40mod decimal;
41mod dictionary;
42mod list;
43mod map;
44mod string;
45use crate::cast::decimal::*;
46use crate::cast::dictionary::*;
47use crate::cast::list::*;
48use crate::cast::map::*;
49use crate::cast::string::*;
50
51use arrow_buffer::IntervalMonthDayNano;
52use arrow_data::ByteView;
53use chrono::{NaiveTime, Offset, TimeZone, Utc};
54use std::cmp::Ordering;
55use std::sync::Arc;
56
57use crate::display::{ArrayFormatter, FormatOptions};
58use crate::parse::{
59    parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
60    string_to_datetime, Parser,
61};
62use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
63use arrow_buffer::{i256, ArrowNativeType, OffsetBuffer};
64use arrow_data::transform::MutableArrayData;
65use arrow_data::ArrayData;
66use arrow_schema::*;
67use arrow_select::take::take;
68use num::cast::AsPrimitive;
69use num::{NumCast, ToPrimitive};
70
71/// CastOptions provides a way to override the default cast behaviors
72#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub struct CastOptions<'a> {
74    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
75    pub safe: bool,
76    /// Formatting options when casting from temporal types to string
77    pub format_options: FormatOptions<'a>,
78}
79
80impl Default for CastOptions<'_> {
81    fn default() -> Self {
82        Self {
83            safe: true,
84            format_options: FormatOptions::default(),
85        }
86    }
87}
88
89/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
90///
91/// See [`cast_with_options`] for more information
92pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
93    use self::DataType::*;
94    use self::IntervalUnit::*;
95    use self::TimeUnit::*;
96    if from_type == to_type {
97        return true;
98    }
99
100    match (from_type, to_type) {
101        (
102            Null,
103            Boolean
104            | Int8
105            | UInt8
106            | Int16
107            | UInt16
108            | Int32
109            | UInt32
110            | Float32
111            | Date32
112            | Time32(_)
113            | Int64
114            | UInt64
115            | Float64
116            | Date64
117            | Timestamp(_, _)
118            | Time64(_)
119            | Duration(_)
120            | Interval(_)
121            | FixedSizeBinary(_)
122            | Binary
123            | Utf8
124            | LargeBinary
125            | LargeUtf8
126            | BinaryView
127            | Utf8View
128            | List(_)
129            | LargeList(_)
130            | FixedSizeList(_, _)
131            | Struct(_)
132            | Map(_, _)
133            | Dictionary(_, _),
134        ) => true,
135        // Dictionary/List conditions should be put in front of others
136        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
137            can_cast_types(from_value_type, to_value_type)
138        }
139        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
140        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
141        (List(list_from) | LargeList(list_from), List(list_to) | LargeList(list_to)) => {
142            can_cast_types(list_from.data_type(), list_to.data_type())
143        }
144        (List(list_from) | LargeList(list_from), Utf8 | LargeUtf8) => {
145            can_cast_types(list_from.data_type(), to_type)
146        }
147        (List(list_from) | LargeList(list_from), FixedSizeList(list_to, _)) => {
148            can_cast_types(list_from.data_type(), list_to.data_type())
149        }
150        (List(_), _) => false,
151        (FixedSizeList(list_from,_), List(list_to)) |
152        (FixedSizeList(list_from,_), LargeList(list_to)) => {
153            can_cast_types(list_from.data_type(), list_to.data_type())
154        }
155        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
156            can_cast_types(inner.data_type(), inner_to.data_type())
157        }
158        (_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
159        (_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
160        (_, FixedSizeList(list_to,size)) if *size == 1 => {
161            can_cast_types(from_type, list_to.data_type())},
162        (FixedSizeList(list_from,size), _) if *size == 1 => {
163            can_cast_types(list_from.data_type(), to_type)},
164        (Map(from_entries,ordered_from), Map(to_entries, ordered_to)) if ordered_from == ordered_to =>
165            match (key_field(from_entries), key_field(to_entries), value_field(from_entries), value_field(to_entries)) {
166                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) =>
167                    can_cast_types(from_key.data_type(), to_key.data_type()) && can_cast_types(from_value.data_type(), to_value.data_type()),
168                _ => false
169            },
170        // cast one decimal type to another decimal type
171        (Decimal128(_, _), Decimal128(_, _)) => true,
172        (Decimal256(_, _), Decimal256(_, _)) => true,
173        (Decimal128(_, _), Decimal256(_, _)) => true,
174        (Decimal256(_, _), Decimal128(_, _)) => true,
175        // unsigned integer to decimal
176        (UInt8 | UInt16 | UInt32 | UInt64, Decimal128(_, _)) |
177        (UInt8 | UInt16 | UInt32 | UInt64, Decimal256(_, _)) |
178        // signed numeric to decimal
179        (Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64, Decimal128(_, _)) |
180        (Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64, Decimal256(_, _)) |
181        // decimal to unsigned numeric
182        (Decimal128(_, _) | Decimal256(_, _), UInt8 | UInt16 | UInt32 | UInt64) |
183        // decimal to signed numeric
184        (Decimal128(_, _) | Decimal256(_, _), Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64) => true,
185        // decimal to string
186        (Decimal128(_, _) | Decimal256(_, _), Utf8View | Utf8 | LargeUtf8) => true,
187        // string to decimal
188        (Utf8View | Utf8 | LargeUtf8, Decimal128(_, _) | Decimal256(_, _)) => true,
189        (Struct(from_fields), Struct(to_fields)) => {
190            from_fields.len() == to_fields.len() &&
191                from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
192                    // Assume that nullability between two structs are compatible, if not,
193                    // cast kernel will return error.
194                    can_cast_types(f1.data_type(), f2.data_type())
195                })
196        }
197        (Struct(_), _) => false,
198        (_, Struct(_)) => false,
199        (_, Boolean) => {
200            DataType::is_integer(from_type)
201                || DataType::is_floating(from_type)
202                || from_type == &Utf8View
203                || from_type == &Utf8
204                || from_type == &LargeUtf8
205        }
206        (Boolean, _) => {
207            DataType::is_integer(to_type)
208                || DataType::is_floating(to_type)
209                || to_type == &Utf8View
210                || to_type == &Utf8
211                || to_type == &LargeUtf8
212        }
213
214        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
215        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
216        (FixedSizeBinary(_), Binary | LargeBinary) => true,
217        (
218            Utf8 | LargeUtf8 | Utf8View,
219            Binary
220            | LargeBinary
221            | Utf8
222            | LargeUtf8
223            | Date32
224            | Date64
225            | Time32(Second)
226            | Time32(Millisecond)
227            | Time64(Microsecond)
228            | Time64(Nanosecond)
229            | Timestamp(Second, _)
230            | Timestamp(Millisecond, _)
231            | Timestamp(Microsecond, _)
232            | Timestamp(Nanosecond, _)
233            | Interval(_)
234            | BinaryView,
235        ) => true,
236        (Utf8 | LargeUtf8, Utf8View) => true,
237        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
238        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
239        (_, Utf8 | LargeUtf8) => from_type.is_primitive(),
240        (_, Utf8View) => from_type.is_numeric(),
241
242        (_, Binary | LargeBinary) => from_type.is_integer(),
243
244        // start numeric casts
245        (
246            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
247            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
248        ) => true,
249        // end numeric casts
250
251        // temporal casts
252        (Int32, Date32 | Date64 | Time32(_)) => true,
253        (Date32, Int32 | Int64) => true,
254        (Time32(_), Int32) => true,
255        (Int64, Date64 | Date32 | Time64(_)) => true,
256        (Date64, Int64 | Int32) => true,
257        (Time64(_), Int64) => true,
258        (Date32 | Date64, Date32 | Date64) => true,
259        // time casts
260        (Time32(_), Time32(_)) => true,
261        (Time32(_), Time64(_)) => true,
262        (Time64(_), Time64(_)) => true,
263        (Time64(_), Time32(to_unit)) => {
264            matches!(to_unit, Second | Millisecond)
265        }
266        (Timestamp(_, _), _) if to_type.is_numeric() => true,
267        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
268        (Date64, Timestamp(_, _)) => true,
269        (Date32, Timestamp(_, _)) => true,
270        (
271            Timestamp(_, _),
272            Timestamp(_, _)
273            | Date32
274            | Date64
275            | Time32(Second)
276            | Time32(Millisecond)
277            | Time64(Microsecond)
278            | Time64(Nanosecond),
279        ) => true,
280        (_, Duration(_)) if from_type.is_numeric() => true,
281        (Duration(_), _) if to_type.is_numeric() => true,
282        (Duration(_), Duration(_)) => true,
283        (Interval(from_type), Int64) => {
284            match from_type {
285                YearMonth => true,
286                DayTime => true,
287                MonthDayNano => false, // Native type is i128
288            }
289        }
290        (Int32, Interval(to_type)) => match to_type {
291            YearMonth => true,
292            DayTime => false,
293            MonthDayNano => false,
294        },
295        (Duration(_), Interval(MonthDayNano)) => true,
296        (Interval(MonthDayNano), Duration(_)) => true,
297        (Interval(YearMonth), Interval(MonthDayNano)) => true,
298        (Interval(DayTime), Interval(MonthDayNano)) => true,
299        (_, _) => false,
300    }
301}
302
303/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
304///
305/// See [`cast_with_options`] for more information
306pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
307    cast_with_options(array, to_type, &CastOptions::default())
308}
309
310fn cast_integer_to_decimal<
311    T: ArrowPrimitiveType,
312    D: DecimalType + ArrowPrimitiveType<Native = M>,
313    M,
314>(
315    array: &PrimitiveArray<T>,
316    precision: u8,
317    scale: i8,
318    base: M,
319    cast_options: &CastOptions,
320) -> Result<ArrayRef, ArrowError>
321where
322    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
323    M: ArrowNativeTypeOp,
324{
325    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
326        ArrowError::CastError(format!(
327            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
328            D::PREFIX,
329            precision,
330            scale,
331        ))
332    })?;
333
334    let array = if scale < 0 {
335        match cast_options.safe {
336            true => array.unary_opt::<_, D>(|v| {
337                v.as_()
338                    .div_checked(scale_factor)
339                    .ok()
340                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
341            }),
342            false => array.try_unary::<_, D, _>(|v| {
343                v.as_()
344                    .div_checked(scale_factor)
345                    .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
346            })?,
347        }
348    } else {
349        match cast_options.safe {
350            true => array.unary_opt::<_, D>(|v| {
351                v.as_()
352                    .mul_checked(scale_factor)
353                    .ok()
354                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
355            }),
356            false => array.try_unary::<_, D, _>(|v| {
357                v.as_()
358                    .mul_checked(scale_factor)
359                    .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
360            })?,
361        }
362    };
363
364    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
365}
366
367/// Cast the array from interval year month to month day nano
368fn cast_interval_year_month_to_interval_month_day_nano(
369    array: &dyn Array,
370    _cast_options: &CastOptions,
371) -> Result<ArrayRef, ArrowError> {
372    let array = array.as_primitive::<IntervalYearMonthType>();
373
374    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
375        let months = IntervalYearMonthType::to_months(v);
376        IntervalMonthDayNanoType::make_value(months, 0, 0)
377    })))
378}
379
380/// Cast the array from interval day time to month day nano
381fn cast_interval_day_time_to_interval_month_day_nano(
382    array: &dyn Array,
383    _cast_options: &CastOptions,
384) -> Result<ArrayRef, ArrowError> {
385    let array = array.as_primitive::<IntervalDayTimeType>();
386    let mul = 1_000_000;
387
388    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
389        let (days, ms) = IntervalDayTimeType::to_parts(v);
390        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
391    })))
392}
393
394/// Cast the array from interval to duration
395fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
396    array: &dyn Array,
397    cast_options: &CastOptions,
398) -> Result<ArrayRef, ArrowError> {
399    let array = array.as_primitive::<IntervalMonthDayNanoType>();
400    let scale = match D::DATA_TYPE {
401        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
402        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
403        DataType::Duration(TimeUnit::Microsecond) => 1_000,
404        DataType::Duration(TimeUnit::Nanosecond) => 1,
405        _ => unreachable!(),
406    };
407
408    if cast_options.safe {
409        let iter = array.iter().map(|v| {
410            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
411        });
412        Ok(Arc::new(unsafe {
413            PrimitiveArray::<D>::from_trusted_len_iter(iter)
414        }))
415    } else {
416        let vec = array
417            .iter()
418            .map(|v| {
419                v.map(|v| match v.days == 0 && v.months == 0 {
420                    true => Ok((v.nanoseconds) / scale),
421                    _ => Err(ArrowError::ComputeError(
422                        "Cannot convert interval containing non-zero months or days to duration"
423                            .to_string(),
424                    )),
425                })
426                .transpose()
427            })
428            .collect::<Result<Vec<_>, _>>()?;
429        Ok(Arc::new(unsafe {
430            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
431        }))
432    }
433}
434
435/// Cast the array from duration and interval
436fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
437    array: &dyn Array,
438    cast_options: &CastOptions,
439) -> Result<ArrayRef, ArrowError> {
440    let array = array
441        .as_any()
442        .downcast_ref::<PrimitiveArray<D>>()
443        .ok_or_else(|| {
444            ArrowError::ComputeError(
445                "Internal Error: Cannot cast duration to DurationArray of expected type"
446                    .to_string(),
447            )
448        })?;
449
450    let scale = match array.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| {
461                v.checked_mul(scale)
462                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
463            })
464        });
465        Ok(Arc::new(unsafe {
466            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
467        }))
468    } else {
469        let vec = array
470            .iter()
471            .map(|v| {
472                v.map(|v| {
473                    if let Ok(v) = v.mul_checked(scale) {
474                        Ok(IntervalMonthDayNano::new(0, 0, v))
475                    } else {
476                        Err(ArrowError::ComputeError(format!(
477                            "Cannot cast to {:?}. Overflowing on {:?}",
478                            IntervalMonthDayNanoType::DATA_TYPE,
479                            v
480                        )))
481                    }
482                })
483                .transpose()
484            })
485            .collect::<Result<Vec<_>, _>>()?;
486        Ok(Arc::new(unsafe {
487            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
488        }))
489    }
490}
491
492/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
493fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
494    array: &dyn Array,
495) -> Result<ArrayRef, ArrowError> {
496    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
497}
498
499fn make_timestamp_array(
500    array: &PrimitiveArray<Int64Type>,
501    unit: TimeUnit,
502    tz: Option<Arc<str>>,
503) -> ArrayRef {
504    match unit {
505        TimeUnit::Second => Arc::new(
506            array
507                .reinterpret_cast::<TimestampSecondType>()
508                .with_timezone_opt(tz),
509        ),
510        TimeUnit::Millisecond => Arc::new(
511            array
512                .reinterpret_cast::<TimestampMillisecondType>()
513                .with_timezone_opt(tz),
514        ),
515        TimeUnit::Microsecond => Arc::new(
516            array
517                .reinterpret_cast::<TimestampMicrosecondType>()
518                .with_timezone_opt(tz),
519        ),
520        TimeUnit::Nanosecond => Arc::new(
521            array
522                .reinterpret_cast::<TimestampNanosecondType>()
523                .with_timezone_opt(tz),
524        ),
525    }
526}
527
528fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
529    match unit {
530        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
531        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
532        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
533        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
534    }
535}
536
537fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
538    v: i64,
539    tz: Option<Tz>,
540) -> Result<NaiveTime, ArrowError> {
541    let time = match tz {
542        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
543        None => as_datetime::<T>(v).map(|d| d.time()),
544    };
545
546    time.ok_or_else(|| {
547        ArrowError::CastError(format!(
548            "Failed to create naive time with {} {}",
549            std::any::type_name::<T>(),
550            v
551        ))
552    })
553}
554
555fn timestamp_to_date32<T: ArrowTimestampType>(
556    array: &PrimitiveArray<T>,
557) -> Result<ArrayRef, ArrowError> {
558    let err = |x: i64| {
559        ArrowError::CastError(format!(
560            "Cannot convert {} {x} to datetime",
561            std::any::type_name::<T>()
562        ))
563    };
564
565    let array: Date32Array = match array.timezone() {
566        Some(tz) => {
567            let tz: Tz = tz.parse()?;
568            array.try_unary(|x| {
569                as_datetime_with_timezone::<T>(x, tz)
570                    .ok_or_else(|| err(x))
571                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
572            })?
573        }
574        None => array.try_unary(|x| {
575            as_datetime::<T>(x)
576                .ok_or_else(|| err(x))
577                .map(|d| Date32Type::from_naive_date(d.date()))
578        })?,
579    };
580    Ok(Arc::new(array))
581}
582
583/// Try to cast `array` to `to_type` if possible.
584///
585/// Returns a new Array with type `to_type` if possible.
586///
587/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
588///
589/// # Behavior
590/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
591/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
592///   short variants are accepted, other strings return null or error
593/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
594///   in integer casts return null
595/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
596/// * `List` to `List`: the underlying data type is cast
597/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
598///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
599/// * Primitive to `List`: a list array with 1 value per slot is created
600/// * `Date32` and `Date64`: precision lost when going to higher interval
601/// * `Time32 and `Time64`: precision lost when going to higher interval
602/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
603/// * Temporal to/from backing Primitive: zero-copy with data type change
604/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
605///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
606///
607/// Unsupported Casts (check with `can_cast_types` before calling):
608/// * To or from `StructArray`
609/// * `List` to `Primitive`
610/// * `Interval` and `Duration`
611///
612/// # Timestamps and Timezones
613///
614/// Timestamps are stored with an optional timezone in Arrow.
615///
616/// ## Casting timestamps to a timestamp without timezone / UTC
617/// ```
618/// # use arrow_array::Int64Array;
619/// # use arrow_array::types::TimestampSecondType;
620/// # use arrow_cast::{cast, display};
621/// # use arrow_array::cast::AsArray;
622/// # use arrow_schema::{DataType, TimeUnit};
623/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
624/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
625/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
626/// let b = cast(&a, &data_type).unwrap();
627/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
628/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
629/// // use display to show them (note has no trailing Z)
630/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
631/// ```
632///
633/// ## Casting timestamps to a timestamp with timezone
634///
635/// Similarly to the previous example, if you cast numeric values to a timestamp
636/// with timezone, the cast kernel will not change the underlying values
637/// but display and other functions will interpret them as being in the provided timezone.
638///
639/// ```
640/// # use arrow_array::Int64Array;
641/// # use arrow_array::types::TimestampSecondType;
642/// # use arrow_cast::{cast, display};
643/// # use arrow_array::cast::AsArray;
644/// # use arrow_schema::{DataType, TimeUnit};
645/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
646/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
647/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
648/// let b = cast(&a, &data_type).unwrap();
649/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
650/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
651/// // displayed in the target timezone (note the offset -05:00)
652/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
653/// ```
654/// # Casting timestamps without timezone to timestamps with timezone
655///
656/// When casting from a timestamp without timezone to a timestamp with
657/// timezone, the cast kernel interprets the timestamp values as being in
658/// the destination timezone and then adjusts the underlying value to UTC as required
659///
660/// However, note that when casting from a timestamp with timezone BACK to a
661/// timestamp without timezone the cast kernel does not adjust the values.
662///
663/// Thus round trip casting a timestamp without timezone to a timestamp with
664/// timezone and back to a timestamp without timezone results in different
665/// values than the starting values.
666///
667/// ```
668/// # use arrow_array::Int64Array;
669/// # use arrow_array::types::{TimestampSecondType};
670/// # use arrow_cast::{cast, display};
671/// # use arrow_array::cast::AsArray;
672/// # use arrow_schema::{DataType, TimeUnit};
673/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
674/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
675/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
676/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
677/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
678/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
679/// // displayed without a timezone (note lack of offset or Z)
680/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
681///
682/// // Convert timestamps without a timezone to timestamps with a timezone
683/// let c = cast(&b, &data_type_tz).unwrap();
684/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
685/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
686/// // displayed with the target timezone offset (-05:00)
687/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
688///
689/// // Convert from timestamp with timezone back to timestamp without timezone
690/// let d = cast(&c, &data_type).unwrap();
691/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
692/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
693/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
694/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
695/// ```
696pub fn cast_with_options(
697    array: &dyn Array,
698    to_type: &DataType,
699    cast_options: &CastOptions,
700) -> Result<ArrayRef, ArrowError> {
701    use DataType::*;
702    let from_type = array.data_type();
703    // clone array if types are the same
704    if from_type == to_type {
705        return Ok(make_array(array.to_data()));
706    }
707    match (from_type, to_type) {
708        (
709            Null,
710            Boolean
711            | Int8
712            | UInt8
713            | Int16
714            | UInt16
715            | Int32
716            | UInt32
717            | Float32
718            | Date32
719            | Time32(_)
720            | Int64
721            | UInt64
722            | Float64
723            | Date64
724            | Timestamp(_, _)
725            | Time64(_)
726            | Duration(_)
727            | Interval(_)
728            | FixedSizeBinary(_)
729            | Binary
730            | Utf8
731            | LargeBinary
732            | LargeUtf8
733            | BinaryView
734            | Utf8View
735            | List(_)
736            | LargeList(_)
737            | FixedSizeList(_, _)
738            | Struct(_)
739            | Map(_, _)
740            | Dictionary(_, _),
741        ) => Ok(new_null_array(to_type, array.len())),
742        (Dictionary(index_type, _), _) => match **index_type {
743            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
744            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
745            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
746            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
747            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
748            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
749            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
750            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
751            _ => Err(ArrowError::CastError(format!(
752                "Casting from dictionary type {from_type:?} to {to_type:?} not supported",
753            ))),
754        },
755        (_, Dictionary(index_type, value_type)) => match **index_type {
756            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
757            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
758            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
759            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
760            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
761            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
762            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
763            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
764            _ => Err(ArrowError::CastError(format!(
765                "Casting from type {from_type:?} to dictionary type {to_type:?} not supported",
766            ))),
767        },
768        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
769        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
770        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
771        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
772        (List(_), FixedSizeList(field, size)) => {
773            let array = array.as_list::<i32>();
774            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
775        }
776        (LargeList(_), FixedSizeList(field, size)) => {
777            let array = array.as_list::<i64>();
778            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
779        }
780        (List(_) | LargeList(_), _) => match to_type {
781            Utf8 => value_to_string::<i32>(array, cast_options),
782            LargeUtf8 => value_to_string::<i64>(array, cast_options),
783            _ => Err(ArrowError::CastError(
784                "Cannot cast list to non-list data types".to_string(),
785            )),
786        },
787        (FixedSizeList(list_from, size), List(list_to)) => {
788            if list_to.data_type() != list_from.data_type() {
789                // To transform inner type, can first cast to FSL with new inner type.
790                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
791                let array = cast_with_options(array, &fsl_to, cast_options)?;
792                cast_fixed_size_list_to_list::<i32>(array.as_ref())
793            } else {
794                cast_fixed_size_list_to_list::<i32>(array)
795            }
796        }
797        (FixedSizeList(list_from, size), LargeList(list_to)) => {
798            if list_to.data_type() != list_from.data_type() {
799                // To transform inner type, can first cast to FSL with new inner type.
800                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
801                let array = cast_with_options(array, &fsl_to, cast_options)?;
802                cast_fixed_size_list_to_list::<i64>(array.as_ref())
803            } else {
804                cast_fixed_size_list_to_list::<i64>(array)
805            }
806        }
807        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
808            if size_from != size_to {
809                return Err(ArrowError::CastError(
810                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
811                ));
812            }
813            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
814            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
815            Ok(Arc::new(FixedSizeListArray::try_new(
816                list_to.clone(),
817                *size_from,
818                values,
819                array.nulls().cloned(),
820            )?))
821        }
822        (_, List(ref to)) => cast_values_to_list::<i32>(array, to, cast_options),
823        (_, LargeList(ref to)) => cast_values_to_list::<i64>(array, to, cast_options),
824        (_, FixedSizeList(ref to, size)) if *size == 1 => {
825            cast_values_to_fixed_size_list(array, to, *size, cast_options)
826        }
827        (FixedSizeList(_, size), _) if *size == 1 => {
828            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
829        }
830        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
831            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
832        }
833        // Decimal to decimal, same width
834        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
835            cast_decimal_to_decimal_same_type::<Decimal128Type>(
836                array.as_primitive(),
837                *p1,
838                *s1,
839                *p2,
840                *s2,
841                cast_options,
842            )
843        }
844        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
845            cast_decimal_to_decimal_same_type::<Decimal256Type>(
846                array.as_primitive(),
847                *p1,
848                *s1,
849                *p2,
850                *s2,
851                cast_options,
852            )
853        }
854        // Decimal to decimal, different width
855        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
856            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
857                array.as_primitive(),
858                *p1,
859                *s1,
860                *p2,
861                *s2,
862                cast_options,
863            )
864        }
865        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
866            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
867                array.as_primitive(),
868                *p1,
869                *s1,
870                *p2,
871                *s2,
872                cast_options,
873            )
874        }
875        // Decimal to non-decimal
876        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
877            cast_from_decimal::<Decimal128Type, _>(
878                array,
879                10_i128,
880                scale,
881                from_type,
882                to_type,
883                |x: i128| x as f64,
884                cast_options,
885            )
886        }
887        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
888            cast_from_decimal::<Decimal256Type, _>(
889                array,
890                i256::from_i128(10_i128),
891                scale,
892                from_type,
893                to_type,
894                |x: i256| x.to_f64().unwrap(),
895                cast_options,
896            )
897        }
898        // Non-decimal to decimal
899        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
900            cast_to_decimal::<Decimal128Type, _>(
901                array,
902                10_i128,
903                precision,
904                scale,
905                from_type,
906                to_type,
907                cast_options,
908            )
909        }
910        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
911            cast_to_decimal::<Decimal256Type, _>(
912                array,
913                i256::from_i128(10_i128),
914                precision,
915                scale,
916                from_type,
917                to_type,
918                cast_options,
919            )
920        }
921        (Struct(_), Struct(to_fields)) => {
922            let array = array.as_struct();
923            let fields = array
924                .columns()
925                .iter()
926                .zip(to_fields.iter())
927                .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
928                .collect::<Result<Vec<ArrayRef>, ArrowError>>()?;
929            let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
930            Ok(Arc::new(array) as ArrayRef)
931        }
932        (Struct(_), _) => Err(ArrowError::CastError(format!(
933            "Casting from {from_type:?} to {to_type:?} not supported"
934        ))),
935        (_, Struct(_)) => Err(ArrowError::CastError(format!(
936            "Casting from {from_type:?} to {to_type:?} not supported"
937        ))),
938        (_, Boolean) => match from_type {
939            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
940            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
941            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
942            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
943            Int8 => cast_numeric_to_bool::<Int8Type>(array),
944            Int16 => cast_numeric_to_bool::<Int16Type>(array),
945            Int32 => cast_numeric_to_bool::<Int32Type>(array),
946            Int64 => cast_numeric_to_bool::<Int64Type>(array),
947            Float16 => cast_numeric_to_bool::<Float16Type>(array),
948            Float32 => cast_numeric_to_bool::<Float32Type>(array),
949            Float64 => cast_numeric_to_bool::<Float64Type>(array),
950            Utf8View => cast_utf8view_to_boolean(array, cast_options),
951            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
952            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
953            _ => Err(ArrowError::CastError(format!(
954                "Casting from {from_type:?} to {to_type:?} not supported",
955            ))),
956        },
957        (Boolean, _) => match to_type {
958            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
959            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
960            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
961            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
962            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
963            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
964            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
965            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
966            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
967            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
968            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
969            Utf8View => value_to_string_view(array, cast_options),
970            Utf8 => value_to_string::<i32>(array, cast_options),
971            LargeUtf8 => value_to_string::<i64>(array, cast_options),
972            _ => Err(ArrowError::CastError(format!(
973                "Casting from {from_type:?} to {to_type:?} not supported",
974            ))),
975        },
976        (Utf8, _) => match to_type {
977            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
978            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
979            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
980            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
981            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
982            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
983            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
984            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
985            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
986            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
987            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
988            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
989            Binary => Ok(Arc::new(BinaryArray::from(
990                array.as_string::<i32>().clone(),
991            ))),
992            LargeBinary => {
993                let binary = BinaryArray::from(array.as_string::<i32>().clone());
994                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
995            }
996            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
997            BinaryView => Ok(Arc::new(
998                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
999            )),
1000            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1001            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1002            Time32(TimeUnit::Millisecond) => {
1003                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1004            }
1005            Time64(TimeUnit::Microsecond) => {
1006                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1007            }
1008            Time64(TimeUnit::Nanosecond) => {
1009                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1010            }
1011            Timestamp(TimeUnit::Second, to_tz) => {
1012                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1013            }
1014            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1015                i32,
1016                TimestampMillisecondType,
1017            >(array, to_tz, cast_options),
1018            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1019                i32,
1020                TimestampMicrosecondType,
1021            >(array, to_tz, cast_options),
1022            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1023                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1024            }
1025            Interval(IntervalUnit::YearMonth) => {
1026                cast_string_to_year_month_interval::<i32>(array, cast_options)
1027            }
1028            Interval(IntervalUnit::DayTime) => {
1029                cast_string_to_day_time_interval::<i32>(array, cast_options)
1030            }
1031            Interval(IntervalUnit::MonthDayNano) => {
1032                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1033            }
1034            _ => Err(ArrowError::CastError(format!(
1035                "Casting from {from_type:?} to {to_type:?} not supported",
1036            ))),
1037        },
1038        (Utf8View, _) => match to_type {
1039            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1040            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1041            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1042            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1043            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1044            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1045            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1046            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1047            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1048            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1049            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1050            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1051            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1052            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1053            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1054            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1055            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1056            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1057            Time32(TimeUnit::Millisecond) => {
1058                parse_string_view::<Time32MillisecondType>(array, cast_options)
1059            }
1060            Time64(TimeUnit::Microsecond) => {
1061                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1062            }
1063            Time64(TimeUnit::Nanosecond) => {
1064                parse_string_view::<Time64NanosecondType>(array, cast_options)
1065            }
1066            Timestamp(TimeUnit::Second, to_tz) => {
1067                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1068            }
1069            Timestamp(TimeUnit::Millisecond, to_tz) => {
1070                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1071            }
1072            Timestamp(TimeUnit::Microsecond, to_tz) => {
1073                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1074            }
1075            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1076                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1077            }
1078            Interval(IntervalUnit::YearMonth) => {
1079                cast_view_to_year_month_interval(array, cast_options)
1080            }
1081            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1082            Interval(IntervalUnit::MonthDayNano) => {
1083                cast_view_to_month_day_nano_interval(array, cast_options)
1084            }
1085            _ => Err(ArrowError::CastError(format!(
1086                "Casting from {from_type:?} to {to_type:?} not supported",
1087            ))),
1088        },
1089        (LargeUtf8, _) => match to_type {
1090            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1091            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1092            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1093            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1094            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1095            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1096            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1097            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1098            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1099            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1100            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1101            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1102            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1103            Binary => {
1104                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1105                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1106            }
1107            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1108                array.as_string::<i64>().clone(),
1109            ))),
1110            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1111            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1112                array
1113                    .as_string::<i64>()
1114                    .into_iter()
1115                    .map(|x| x.map(|x| x.as_bytes()))
1116                    .collect::<Vec<_>>(),
1117            ))),
1118            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1119            Time32(TimeUnit::Millisecond) => {
1120                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1121            }
1122            Time64(TimeUnit::Microsecond) => {
1123                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1124            }
1125            Time64(TimeUnit::Nanosecond) => {
1126                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1127            }
1128            Timestamp(TimeUnit::Second, to_tz) => {
1129                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1130            }
1131            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1132                i64,
1133                TimestampMillisecondType,
1134            >(array, to_tz, cast_options),
1135            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1136                i64,
1137                TimestampMicrosecondType,
1138            >(array, to_tz, cast_options),
1139            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1140                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1141            }
1142            Interval(IntervalUnit::YearMonth) => {
1143                cast_string_to_year_month_interval::<i64>(array, cast_options)
1144            }
1145            Interval(IntervalUnit::DayTime) => {
1146                cast_string_to_day_time_interval::<i64>(array, cast_options)
1147            }
1148            Interval(IntervalUnit::MonthDayNano) => {
1149                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1150            }
1151            _ => Err(ArrowError::CastError(format!(
1152                "Casting from {from_type:?} to {to_type:?} not supported",
1153            ))),
1154        },
1155        (Binary, _) => match to_type {
1156            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1157            LargeUtf8 => {
1158                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1159                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1160            }
1161            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1162            FixedSizeBinary(size) => {
1163                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1164            }
1165            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1166            Utf8View => Ok(Arc::new(StringViewArray::from(
1167                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1168            ))),
1169            _ => Err(ArrowError::CastError(format!(
1170                "Casting from {from_type:?} to {to_type:?} not supported",
1171            ))),
1172        },
1173        (LargeBinary, _) => match to_type {
1174            Utf8 => {
1175                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1176                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1177            }
1178            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1179            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1180            FixedSizeBinary(size) => {
1181                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1182            }
1183            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1184            Utf8View => {
1185                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1186                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1187            }
1188            _ => Err(ArrowError::CastError(format!(
1189                "Casting from {from_type:?} to {to_type:?} not supported",
1190            ))),
1191        },
1192        (FixedSizeBinary(size), _) => match to_type {
1193            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1194            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1195            _ => Err(ArrowError::CastError(format!(
1196                "Casting from {from_type:?} to {to_type:?} not supported",
1197            ))),
1198        },
1199        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1200        (BinaryView, LargeBinary) => {
1201            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1202        }
1203        (BinaryView, Utf8) => {
1204            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1205            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1206        }
1207        (BinaryView, LargeUtf8) => {
1208            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1209            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1210        }
1211        (BinaryView, Utf8View) => {
1212            Ok(Arc::new(array.as_binary_view().clone().to_string_view()?) as ArrayRef)
1213        }
1214        (BinaryView, _) => Err(ArrowError::CastError(format!(
1215            "Casting from {from_type:?} to {to_type:?} not supported",
1216        ))),
1217        (from_type, Utf8View) if from_type.is_primitive() => {
1218            value_to_string_view(array, cast_options)
1219        }
1220        (from_type, LargeUtf8) if from_type.is_primitive() => {
1221            value_to_string::<i64>(array, cast_options)
1222        }
1223        (from_type, Utf8) if from_type.is_primitive() => {
1224            value_to_string::<i32>(array, cast_options)
1225        }
1226        (from_type, Binary) if from_type.is_integer() => match from_type {
1227            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1228            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1229            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1230            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1231            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1232            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1233            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1234            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1235            _ => unreachable!(),
1236        },
1237        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1238            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1239            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1240            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1241            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1242            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1243            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1244            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1245            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1246            _ => unreachable!(),
1247        },
1248        // start numeric casts
1249        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1250        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1251        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1252        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1253        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1254        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1255        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1256        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1257        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1258        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1259
1260        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1261        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1262        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1263        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1264        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1265        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1266        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1267        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1268        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1269        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1270
1271        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1272        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1273        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1274        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1275        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1276        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1277        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1278        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1279        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1280        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1281
1282        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1283        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1284        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1285        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1286        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1287        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1288        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1289        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1290        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1291        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1292
1293        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1294        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1295        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1296        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1297        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1298        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1299        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1300        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1301        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1302        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1303
1304        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1305        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1306        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1307        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1308        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1309        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1310        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1311        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1312        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1313        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1314
1315        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1316        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1317        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1318        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1319        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1320        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1321        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1322        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1323        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1324        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1325
1326        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1327        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1328        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1329        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1330        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1331        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1332        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1333        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1334        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1335        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1336
1337        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1338        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1339        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1340        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1341        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1342        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1343        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1344        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1345        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1346        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1347
1348        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1349        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1350        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1351        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1352        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1353        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1354        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1355        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1356        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1357        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1358
1359        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1360        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1361        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1362        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1363        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1364        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1365        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1366        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1367        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1368        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1369        // end numeric casts
1370
1371        // temporal casts
1372        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1373        (Int32, Date64) => cast_with_options(
1374            &cast_with_options(array, &Date32, cast_options)?,
1375            &Date64,
1376            cast_options,
1377        ),
1378        (Int32, Time32(TimeUnit::Second)) => {
1379            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1380        }
1381        (Int32, Time32(TimeUnit::Millisecond)) => {
1382            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1383        }
1384        // No support for microsecond/nanosecond with i32
1385        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1386        (Date32, Int64) => cast_with_options(
1387            &cast_with_options(array, &Int32, cast_options)?,
1388            &Int64,
1389            cast_options,
1390        ),
1391        (Time32(TimeUnit::Second), Int32) => {
1392            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1393        }
1394        (Time32(TimeUnit::Millisecond), Int32) => {
1395            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1396        }
1397        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1398        (Int64, Date32) => cast_with_options(
1399            &cast_with_options(array, &Int32, cast_options)?,
1400            &Date32,
1401            cast_options,
1402        ),
1403        // No support for second/milliseconds with i64
1404        (Int64, Time64(TimeUnit::Microsecond)) => {
1405            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1406        }
1407        (Int64, Time64(TimeUnit::Nanosecond)) => {
1408            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1409        }
1410
1411        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1412        (Date64, Int32) => cast_with_options(
1413            &cast_with_options(array, &Int64, cast_options)?,
1414            &Int32,
1415            cast_options,
1416        ),
1417        (Time64(TimeUnit::Microsecond), Int64) => {
1418            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1419        }
1420        (Time64(TimeUnit::Nanosecond), Int64) => {
1421            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1422        }
1423        (Date32, Date64) => Ok(Arc::new(
1424            array
1425                .as_primitive::<Date32Type>()
1426                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1427        )),
1428        (Date64, Date32) => Ok(Arc::new(
1429            array
1430                .as_primitive::<Date64Type>()
1431                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1432        )),
1433
1434        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1435            array
1436                .as_primitive::<Time32SecondType>()
1437                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1438        )),
1439        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1440            array
1441                .as_primitive::<Time32SecondType>()
1442                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1443        )),
1444        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1445            array
1446                .as_primitive::<Time32SecondType>()
1447                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1448        )),
1449
1450        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1451            array
1452                .as_primitive::<Time32MillisecondType>()
1453                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1454        )),
1455        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1456            array
1457                .as_primitive::<Time32MillisecondType>()
1458                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1459        )),
1460        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1461            array
1462                .as_primitive::<Time32MillisecondType>()
1463                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1464        )),
1465
1466        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1467            array
1468                .as_primitive::<Time64MicrosecondType>()
1469                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1470        )),
1471        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1472            array
1473                .as_primitive::<Time64MicrosecondType>()
1474                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1475        )),
1476        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1477            array
1478                .as_primitive::<Time64MicrosecondType>()
1479                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1480        )),
1481
1482        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1483            array
1484                .as_primitive::<Time64NanosecondType>()
1485                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1486        )),
1487        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1488            array
1489                .as_primitive::<Time64NanosecondType>()
1490                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1491        )),
1492        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1493            array
1494                .as_primitive::<Time64NanosecondType>()
1495                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1496        )),
1497
1498        // Timestamp to integer/floating/decimals
1499        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1500            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1501            cast_with_options(&array, to_type, cast_options)
1502        }
1503        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1504            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1505            cast_with_options(&array, to_type, cast_options)
1506        }
1507        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1508            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1509            cast_with_options(&array, to_type, cast_options)
1510        }
1511        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1512            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1513            cast_with_options(&array, to_type, cast_options)
1514        }
1515
1516        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1517            let array = cast_with_options(array, &Int64, cast_options)?;
1518            Ok(make_timestamp_array(
1519                array.as_primitive(),
1520                *unit,
1521                tz.clone(),
1522            ))
1523        }
1524
1525        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1526            let array = cast_with_options(array, &Int64, cast_options)?;
1527            let time_array = array.as_primitive::<Int64Type>();
1528            let from_size = time_unit_multiple(from_unit);
1529            let to_size = time_unit_multiple(to_unit);
1530            // we either divide or multiply, depending on size of each unit
1531            // units are never the same when the types are the same
1532            let converted = match from_size.cmp(&to_size) {
1533                Ordering::Greater => {
1534                    let divisor = from_size / to_size;
1535                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1536                }
1537                Ordering::Equal => time_array.clone(),
1538                Ordering::Less => {
1539                    let mul = to_size / from_size;
1540                    if cast_options.safe {
1541                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1542                    } else {
1543                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1544                    }
1545                }
1546            };
1547            // Normalize timezone
1548            let adjusted = match (from_tz, to_tz) {
1549                // Only this case needs to be adjusted because we're casting from
1550                // unknown time offset to some time offset, we want the time to be
1551                // unchanged.
1552                //
1553                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1554                (None, Some(to_tz)) => {
1555                    let to_tz: Tz = to_tz.parse()?;
1556                    match to_unit {
1557                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1558                            converted,
1559                            &to_tz,
1560                            cast_options,
1561                        )?,
1562                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1563                            TimestampMillisecondType,
1564                        >(
1565                            converted, &to_tz, cast_options
1566                        )?,
1567                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1568                            TimestampMicrosecondType,
1569                        >(
1570                            converted, &to_tz, cast_options
1571                        )?,
1572                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1573                            TimestampNanosecondType,
1574                        >(
1575                            converted, &to_tz, cast_options
1576                        )?,
1577                    }
1578                }
1579                _ => converted,
1580            };
1581            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1582        }
1583        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1584            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1585        }
1586        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1587            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1588        }
1589        (Timestamp(TimeUnit::Second, _), Date32) => {
1590            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1591        }
1592        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1593            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1594        }
1595        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1596            true => {
1597                // change error to None
1598                array
1599                    .as_primitive::<TimestampSecondType>()
1600                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1601            }
1602            false => array
1603                .as_primitive::<TimestampSecondType>()
1604                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1605        })),
1606        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1607            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1608        }
1609        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1610            array
1611                .as_primitive::<TimestampMicrosecondType>()
1612                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1613        )),
1614        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1615            array
1616                .as_primitive::<TimestampNanosecondType>()
1617                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1618        )),
1619        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1620            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1621            Ok(Arc::new(
1622                array
1623                    .as_primitive::<TimestampSecondType>()
1624                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1625                        Ok(time_to_time64us(as_time_res_with_timezone::<
1626                            TimestampSecondType,
1627                        >(x, tz)?))
1628                    })?,
1629            ))
1630        }
1631        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1632            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1633            Ok(Arc::new(
1634                array
1635                    .as_primitive::<TimestampSecondType>()
1636                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1637                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1638                            TimestampSecondType,
1639                        >(x, tz)?))
1640                    })?,
1641            ))
1642        }
1643        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1644            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1645            Ok(Arc::new(
1646                array
1647                    .as_primitive::<TimestampMillisecondType>()
1648                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1649                        Ok(time_to_time64us(as_time_res_with_timezone::<
1650                            TimestampMillisecondType,
1651                        >(x, tz)?))
1652                    })?,
1653            ))
1654        }
1655        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1656            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1657            Ok(Arc::new(
1658                array
1659                    .as_primitive::<TimestampMillisecondType>()
1660                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1661                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1662                            TimestampMillisecondType,
1663                        >(x, tz)?))
1664                    })?,
1665            ))
1666        }
1667        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1668            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1669            Ok(Arc::new(
1670                array
1671                    .as_primitive::<TimestampMicrosecondType>()
1672                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1673                        Ok(time_to_time64us(as_time_res_with_timezone::<
1674                            TimestampMicrosecondType,
1675                        >(x, tz)?))
1676                    })?,
1677            ))
1678        }
1679        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1680            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1681            Ok(Arc::new(
1682                array
1683                    .as_primitive::<TimestampMicrosecondType>()
1684                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1685                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1686                            TimestampMicrosecondType,
1687                        >(x, tz)?))
1688                    })?,
1689            ))
1690        }
1691        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1692            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1693            Ok(Arc::new(
1694                array
1695                    .as_primitive::<TimestampNanosecondType>()
1696                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1697                        Ok(time_to_time64us(as_time_res_with_timezone::<
1698                            TimestampNanosecondType,
1699                        >(x, tz)?))
1700                    })?,
1701            ))
1702        }
1703        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1704            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1705            Ok(Arc::new(
1706                array
1707                    .as_primitive::<TimestampNanosecondType>()
1708                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1709                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1710                            TimestampNanosecondType,
1711                        >(x, tz)?))
1712                    })?,
1713            ))
1714        }
1715        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1716            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1717            Ok(Arc::new(
1718                array
1719                    .as_primitive::<TimestampSecondType>()
1720                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1721                        Ok(time_to_time32s(as_time_res_with_timezone::<
1722                            TimestampSecondType,
1723                        >(x, tz)?))
1724                    })?,
1725            ))
1726        }
1727        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1728            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1729            Ok(Arc::new(
1730                array
1731                    .as_primitive::<TimestampSecondType>()
1732                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1733                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1734                            TimestampSecondType,
1735                        >(x, tz)?))
1736                    })?,
1737            ))
1738        }
1739        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1740            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1741            Ok(Arc::new(
1742                array
1743                    .as_primitive::<TimestampMillisecondType>()
1744                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1745                        Ok(time_to_time32s(as_time_res_with_timezone::<
1746                            TimestampMillisecondType,
1747                        >(x, tz)?))
1748                    })?,
1749            ))
1750        }
1751        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
1752            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1753            Ok(Arc::new(
1754                array
1755                    .as_primitive::<TimestampMillisecondType>()
1756                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1757                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1758                            TimestampMillisecondType,
1759                        >(x, tz)?))
1760                    })?,
1761            ))
1762        }
1763        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
1764            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1765            Ok(Arc::new(
1766                array
1767                    .as_primitive::<TimestampMicrosecondType>()
1768                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1769                        Ok(time_to_time32s(as_time_res_with_timezone::<
1770                            TimestampMicrosecondType,
1771                        >(x, tz)?))
1772                    })?,
1773            ))
1774        }
1775        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
1776            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1777            Ok(Arc::new(
1778                array
1779                    .as_primitive::<TimestampMicrosecondType>()
1780                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1781                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1782                            TimestampMicrosecondType,
1783                        >(x, tz)?))
1784                    })?,
1785            ))
1786        }
1787        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
1788            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1789            Ok(Arc::new(
1790                array
1791                    .as_primitive::<TimestampNanosecondType>()
1792                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1793                        Ok(time_to_time32s(as_time_res_with_timezone::<
1794                            TimestampNanosecondType,
1795                        >(x, tz)?))
1796                    })?,
1797            ))
1798        }
1799        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
1800            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1801            Ok(Arc::new(
1802                array
1803                    .as_primitive::<TimestampNanosecondType>()
1804                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1805                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1806                            TimestampNanosecondType,
1807                        >(x, tz)?))
1808                    })?,
1809            ))
1810        }
1811        (Date64, Timestamp(TimeUnit::Second, _)) => {
1812            let array = array
1813                .as_primitive::<Date64Type>()
1814                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
1815
1816            cast_with_options(&array, to_type, cast_options)
1817        }
1818        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
1819            let array = array
1820                .as_primitive::<Date64Type>()
1821                .reinterpret_cast::<TimestampMillisecondType>();
1822
1823            cast_with_options(&array, to_type, cast_options)
1824        }
1825
1826        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
1827            let array = array
1828                .as_primitive::<Date64Type>()
1829                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
1830
1831            cast_with_options(&array, to_type, cast_options)
1832        }
1833        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
1834            let array = array
1835                .as_primitive::<Date64Type>()
1836                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
1837
1838            cast_with_options(&array, to_type, cast_options)
1839        }
1840        (Date32, Timestamp(TimeUnit::Second, _)) => {
1841            let array = array
1842                .as_primitive::<Date32Type>()
1843                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
1844
1845            cast_with_options(&array, to_type, cast_options)
1846        }
1847        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
1848            let array = array
1849                .as_primitive::<Date32Type>()
1850                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
1851
1852            cast_with_options(&array, to_type, cast_options)
1853        }
1854        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
1855            let array = array
1856                .as_primitive::<Date32Type>()
1857                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
1858
1859            cast_with_options(&array, to_type, cast_options)
1860        }
1861        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
1862            let array = array
1863                .as_primitive::<Date32Type>()
1864                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
1865
1866            cast_with_options(&array, to_type, cast_options)
1867        }
1868
1869        (_, Duration(unit)) if from_type.is_numeric() => {
1870            let array = cast_with_options(array, &Int64, cast_options)?;
1871            Ok(make_duration_array(array.as_primitive(), *unit))
1872        }
1873        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
1874            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
1875            cast_with_options(&array, to_type, cast_options)
1876        }
1877        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
1878            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
1879            cast_with_options(&array, to_type, cast_options)
1880        }
1881        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
1882            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
1883            cast_with_options(&array, to_type, cast_options)
1884        }
1885        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
1886            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
1887            cast_with_options(&array, to_type, cast_options)
1888        }
1889
1890        (Duration(from_unit), Duration(to_unit)) => {
1891            let array = cast_with_options(array, &Int64, cast_options)?;
1892            let time_array = array.as_primitive::<Int64Type>();
1893            let from_size = time_unit_multiple(from_unit);
1894            let to_size = time_unit_multiple(to_unit);
1895            // we either divide or multiply, depending on size of each unit
1896            // units are never the same when the types are the same
1897            let converted = match from_size.cmp(&to_size) {
1898                Ordering::Greater => {
1899                    let divisor = from_size / to_size;
1900                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1901                }
1902                Ordering::Equal => time_array.clone(),
1903                Ordering::Less => {
1904                    let mul = to_size / from_size;
1905                    if cast_options.safe {
1906                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1907                    } else {
1908                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1909                    }
1910                }
1911            };
1912            Ok(make_duration_array(&converted, *to_unit))
1913        }
1914
1915        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
1916            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
1917        }
1918        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
1919            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
1920        }
1921        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
1922            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
1923        }
1924        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
1925            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
1926        }
1927        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
1928            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
1929        }
1930        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
1931            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
1932        }
1933        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
1934            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
1935        }
1936        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
1937            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
1938        }
1939        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
1940            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
1941        }
1942        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
1943            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
1944        }
1945        (Int32, Interval(IntervalUnit::YearMonth)) => {
1946            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
1947        }
1948        (_, _) => Err(ArrowError::CastError(format!(
1949            "Casting from {from_type:?} to {to_type:?} not supported",
1950        ))),
1951    }
1952}
1953
1954fn cast_from_decimal<D, F>(
1955    array: &dyn Array,
1956    base: D::Native,
1957    scale: &i8,
1958    from_type: &DataType,
1959    to_type: &DataType,
1960    as_float: F,
1961    cast_options: &CastOptions,
1962) -> Result<ArrayRef, ArrowError>
1963where
1964    D: DecimalType + ArrowPrimitiveType,
1965    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
1966    F: Fn(D::Native) -> f64,
1967{
1968    use DataType::*;
1969    // cast decimal to other type
1970    match to_type {
1971        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
1972        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
1973        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
1974        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
1975        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
1976        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
1977        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
1978        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
1979        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
1980            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
1981        }),
1982        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
1983            as_float(x) / 10_f64.powi(*scale as i32)
1984        }),
1985        Utf8View => value_to_string_view(array, cast_options),
1986        Utf8 => value_to_string::<i32>(array, cast_options),
1987        LargeUtf8 => value_to_string::<i64>(array, cast_options),
1988        Null => Ok(new_null_array(to_type, array.len())),
1989        _ => Err(ArrowError::CastError(format!(
1990            "Casting from {from_type:?} to {to_type:?} not supported"
1991        ))),
1992    }
1993}
1994
1995fn cast_to_decimal<D, M>(
1996    array: &dyn Array,
1997    base: M,
1998    precision: &u8,
1999    scale: &i8,
2000    from_type: &DataType,
2001    to_type: &DataType,
2002    cast_options: &CastOptions,
2003) -> Result<ArrayRef, ArrowError>
2004where
2005    D: DecimalType + ArrowPrimitiveType<Native = M>,
2006    M: ArrowNativeTypeOp + DecimalCast,
2007    u8: num::traits::AsPrimitive<M>,
2008    u16: num::traits::AsPrimitive<M>,
2009    u32: num::traits::AsPrimitive<M>,
2010    u64: num::traits::AsPrimitive<M>,
2011    i8: num::traits::AsPrimitive<M>,
2012    i16: num::traits::AsPrimitive<M>,
2013    i32: num::traits::AsPrimitive<M>,
2014    i64: num::traits::AsPrimitive<M>,
2015{
2016    use DataType::*;
2017    // cast data to decimal
2018    match from_type {
2019        UInt8 => cast_integer_to_decimal::<_, D, M>(
2020            array.as_primitive::<UInt8Type>(),
2021            *precision,
2022            *scale,
2023            base,
2024            cast_options,
2025        ),
2026        UInt16 => cast_integer_to_decimal::<_, D, _>(
2027            array.as_primitive::<UInt16Type>(),
2028            *precision,
2029            *scale,
2030            base,
2031            cast_options,
2032        ),
2033        UInt32 => cast_integer_to_decimal::<_, D, _>(
2034            array.as_primitive::<UInt32Type>(),
2035            *precision,
2036            *scale,
2037            base,
2038            cast_options,
2039        ),
2040        UInt64 => cast_integer_to_decimal::<_, D, _>(
2041            array.as_primitive::<UInt64Type>(),
2042            *precision,
2043            *scale,
2044            base,
2045            cast_options,
2046        ),
2047        Int8 => cast_integer_to_decimal::<_, D, _>(
2048            array.as_primitive::<Int8Type>(),
2049            *precision,
2050            *scale,
2051            base,
2052            cast_options,
2053        ),
2054        Int16 => cast_integer_to_decimal::<_, D, _>(
2055            array.as_primitive::<Int16Type>(),
2056            *precision,
2057            *scale,
2058            base,
2059            cast_options,
2060        ),
2061        Int32 => cast_integer_to_decimal::<_, D, _>(
2062            array.as_primitive::<Int32Type>(),
2063            *precision,
2064            *scale,
2065            base,
2066            cast_options,
2067        ),
2068        Int64 => cast_integer_to_decimal::<_, D, _>(
2069            array.as_primitive::<Int64Type>(),
2070            *precision,
2071            *scale,
2072            base,
2073            cast_options,
2074        ),
2075        Float32 => cast_floating_point_to_decimal::<_, D>(
2076            array.as_primitive::<Float32Type>(),
2077            *precision,
2078            *scale,
2079            cast_options,
2080        ),
2081        Float64 => cast_floating_point_to_decimal::<_, D>(
2082            array.as_primitive::<Float64Type>(),
2083            *precision,
2084            *scale,
2085            cast_options,
2086        ),
2087        Utf8View | Utf8 => {
2088            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2089        }
2090        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2091        Null => Ok(new_null_array(to_type, array.len())),
2092        _ => Err(ArrowError::CastError(format!(
2093            "Casting from {from_type:?} to {to_type:?} not supported"
2094        ))),
2095    }
2096}
2097
2098/// Get the time unit as a multiple of a second
2099const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2100    match unit {
2101        TimeUnit::Second => 1,
2102        TimeUnit::Millisecond => MILLISECONDS,
2103        TimeUnit::Microsecond => MICROSECONDS,
2104        TimeUnit::Nanosecond => NANOSECONDS,
2105    }
2106}
2107
2108/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2109fn cast_numeric_arrays<FROM, TO>(
2110    from: &dyn Array,
2111    cast_options: &CastOptions,
2112) -> Result<ArrayRef, ArrowError>
2113where
2114    FROM: ArrowPrimitiveType,
2115    TO: ArrowPrimitiveType,
2116    FROM::Native: NumCast,
2117    TO::Native: NumCast,
2118{
2119    if cast_options.safe {
2120        // If the value can't be casted to the `TO::Native`, return null
2121        Ok(Arc::new(numeric_cast::<FROM, TO>(
2122            from.as_primitive::<FROM>(),
2123        )))
2124    } else {
2125        // If the value can't be casted to the `TO::Native`, return error
2126        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2127            from.as_primitive::<FROM>(),
2128        )?))
2129    }
2130}
2131
2132// Natural cast between numeric types
2133// If the value of T can't be casted to R, will throw error
2134fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2135where
2136    T: ArrowPrimitiveType,
2137    R: ArrowPrimitiveType,
2138    T::Native: NumCast,
2139    R::Native: NumCast,
2140{
2141    from.try_unary(|value| {
2142        num::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2143            ArrowError::CastError(format!(
2144                "Can't cast value {:?} to type {}",
2145                value,
2146                R::DATA_TYPE
2147            ))
2148        })
2149    })
2150}
2151
2152// Natural cast between numeric types
2153// If the value of T can't be casted to R, it will be converted to null
2154fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2155where
2156    T: ArrowPrimitiveType,
2157    R: ArrowPrimitiveType,
2158    T::Native: NumCast,
2159    R::Native: NumCast,
2160{
2161    from.unary_opt::<_, R>(num::cast::cast::<T::Native, R::Native>)
2162}
2163
2164fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2165    array: &dyn Array,
2166) -> Result<ArrayRef, ArrowError> {
2167    let array = array.as_primitive::<FROM>();
2168    let size = std::mem::size_of::<FROM::Native>();
2169    let offsets = OffsetBuffer::from_lengths(std::iter::repeat(size).take(array.len()));
2170    Ok(Arc::new(GenericBinaryArray::<O>::new(
2171        offsets,
2172        array.values().inner().clone(),
2173        array.nulls().cloned(),
2174    )))
2175}
2176
2177fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2178    array: PrimitiveArray<Int64Type>,
2179    to_tz: &Tz,
2180    cast_options: &CastOptions,
2181) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2182    let adjust = |o| {
2183        let local = as_datetime::<T>(o)?;
2184        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2185        T::make_value(local - offset.fix())
2186    };
2187    let adjusted = if cast_options.safe {
2188        array.unary_opt::<_, Int64Type>(adjust)
2189    } else {
2190        array.try_unary::<_, Int64Type, _>(|o| {
2191            adjust(o).ok_or_else(|| {
2192                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2193            })
2194        })?
2195    };
2196    Ok(adjusted)
2197}
2198
2199/// Cast numeric types to Boolean
2200///
2201/// Any zero value returns `false` while non-zero returns `true`
2202fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2203where
2204    FROM: ArrowPrimitiveType,
2205{
2206    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2207}
2208
2209fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2210where
2211    T: ArrowPrimitiveType + ArrowPrimitiveType,
2212{
2213    let mut b = BooleanBuilder::with_capacity(from.len());
2214
2215    for i in 0..from.len() {
2216        if from.is_null(i) {
2217            b.append_null();
2218        } else if from.value(i) != T::default_value() {
2219            b.append_value(true);
2220        } else {
2221            b.append_value(false);
2222        }
2223    }
2224
2225    Ok(b.finish())
2226}
2227
2228/// Cast Boolean types to numeric
2229///
2230/// `false` returns 0 while `true` returns 1
2231fn cast_bool_to_numeric<TO>(
2232    from: &dyn Array,
2233    cast_options: &CastOptions,
2234) -> Result<ArrayRef, ArrowError>
2235where
2236    TO: ArrowPrimitiveType,
2237    TO::Native: num::cast::NumCast,
2238{
2239    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2240        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2241        cast_options,
2242    )))
2243}
2244
2245fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2246where
2247    T: ArrowPrimitiveType,
2248    T::Native: num::NumCast,
2249{
2250    let iter = (0..from.len()).map(|i| {
2251        if from.is_null(i) {
2252            None
2253        } else if from.value(i) {
2254            // a workaround to cast a primitive to T::Native, infallible
2255            num::cast::cast(1)
2256        } else {
2257            Some(T::default_value())
2258        }
2259    });
2260    // Benefit:
2261    //     20% performance improvement
2262    // Soundness:
2263    //     The iterator is trustedLen because it comes from a Range
2264    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2265}
2266
2267/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2268fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2269    array: &dyn Array,
2270    byte_width: i32,
2271    cast_options: &CastOptions,
2272) -> Result<ArrayRef, ArrowError> {
2273    let array = array.as_binary::<O>();
2274    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2275
2276    for i in 0..array.len() {
2277        if array.is_null(i) {
2278            builder.append_null();
2279        } else {
2280            match builder.append_value(array.value(i)) {
2281                Ok(_) => {}
2282                Err(e) => match cast_options.safe {
2283                    true => builder.append_null(),
2284                    false => return Err(e),
2285                },
2286            }
2287        }
2288    }
2289
2290    Ok(Arc::new(builder.finish()))
2291}
2292
2293/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2294/// If the target one is too large for the source array it will return an Error.
2295fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2296    array: &dyn Array,
2297    byte_width: i32,
2298) -> Result<ArrayRef, ArrowError> {
2299    let array = array
2300        .as_any()
2301        .downcast_ref::<FixedSizeBinaryArray>()
2302        .unwrap();
2303
2304    let offsets: i128 = byte_width as i128 * array.len() as i128;
2305
2306    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2307    if is_binary && offsets > i32::MAX as i128 {
2308        return Err(ArrowError::ComputeError(
2309            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2310        ));
2311    } else if !is_binary && offsets > i64::MAX as i128 {
2312        return Err(ArrowError::ComputeError(
2313            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2314        ));
2315    }
2316
2317    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2318
2319    for i in 0..array.len() {
2320        if array.is_null(i) {
2321            builder.append_null();
2322        } else {
2323            builder.append_value(array.value(i));
2324        }
2325    }
2326
2327    Ok(Arc::new(builder.finish()))
2328}
2329
2330/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2331/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2332fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2333where
2334    FROM: ByteArrayType,
2335    TO: ByteArrayType<Native = FROM::Native>,
2336    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2337    TO::Offset: OffsetSizeTrait + NumCast,
2338{
2339    let data = array.to_data();
2340    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2341    let str_values_buf = data.buffers()[1].clone();
2342    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2343
2344    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2345    offsets
2346        .iter()
2347        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2348            let offset =
2349                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2350                    ArrowError::ComputeError(format!(
2351                        "{}{} array too large to cast to {}{} array",
2352                        FROM::Offset::PREFIX,
2353                        FROM::PREFIX,
2354                        TO::Offset::PREFIX,
2355                        TO::PREFIX
2356                    ))
2357                })?;
2358            offset_builder.append(offset);
2359            Ok(())
2360        })?;
2361
2362    let offset_buffer = offset_builder.finish();
2363
2364    let dtype = TO::DATA_TYPE;
2365
2366    let builder = ArrayData::builder(dtype)
2367        .offset(array.offset())
2368        .len(array.len())
2369        .add_buffer(offset_buffer)
2370        .add_buffer(str_values_buf)
2371        .nulls(data.nulls().cloned());
2372
2373    let array_data = unsafe { builder.build_unchecked() };
2374
2375    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2376}
2377
2378/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2379fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2380where
2381    FROM: ByteViewType,
2382    TO: ByteArrayType,
2383    FROM::Native: AsRef<TO::Native>,
2384{
2385    let data = array.to_data();
2386    let view_array = GenericByteViewArray::<FROM>::from(data);
2387
2388    let len = view_array.len();
2389    let bytes = view_array
2390        .views()
2391        .iter()
2392        .map(|v| ByteView::from(*v).length as usize)
2393        .sum::<usize>();
2394
2395    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2396
2397    for val in view_array.iter() {
2398        byte_array_builder.append_option(val);
2399    }
2400
2401    Ok(Arc::new(byte_array_builder.finish()))
2402}
2403
2404#[cfg(test)]
2405mod tests {
2406    use super::*;
2407    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2408    use chrono::NaiveDate;
2409    use half::f16;
2410
2411    #[derive(Clone)]
2412    struct DecimalCastTestConfig {
2413        input_prec: u8,
2414        input_scale: i8,
2415        input_repr: i128,
2416        output_prec: u8,
2417        output_scale: i8,
2418        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2419                                                    // template where the "{}" will be
2420                                                    // replaced with the decimal type name
2421                                                    // (e.g. Decimal128)
2422    }
2423
2424    macro_rules! generate_cast_test_case {
2425        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2426            let output =
2427                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2428
2429            // assert cast type
2430            let input_array_type = $INPUT_ARRAY.data_type();
2431            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2432            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2433            assert_eq!($OUTPUT_TYPE, result.data_type());
2434            assert_eq!(result.as_ref(), &output);
2435
2436            let cast_option = CastOptions {
2437                safe: false,
2438                format_options: FormatOptions::default(),
2439            };
2440            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2441            assert_eq!($OUTPUT_TYPE, result.data_type());
2442            assert_eq!(result.as_ref(), &output);
2443        };
2444    }
2445
2446    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2447    where
2448        I: DecimalType,
2449        O: DecimalType,
2450        I::Native: DecimalCast,
2451        O::Native: DecimalCast,
2452    {
2453        let array = vec![I::Native::from_decimal(t.input_repr)];
2454        let array = array
2455            .into_iter()
2456            .collect::<PrimitiveArray<I>>()
2457            .with_precision_and_scale(t.input_prec, t.input_scale)
2458            .unwrap();
2459        let input_type = array.data_type();
2460        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2461        assert!(can_cast_types(input_type, &output_type));
2462
2463        let options = CastOptions {
2464            safe: false,
2465            ..Default::default()
2466        };
2467        let result = cast_with_options(&array, &output_type, &options);
2468
2469        match t.expected_output_repr {
2470            Ok(v) => {
2471                let expected_array = vec![O::Native::from_decimal(v)];
2472                let expected_array = expected_array
2473                    .into_iter()
2474                    .collect::<PrimitiveArray<O>>()
2475                    .with_precision_and_scale(t.output_prec, t.output_scale)
2476                    .unwrap();
2477                assert_eq!(*result.unwrap(), expected_array);
2478            }
2479            Err(expected_output_message_template) => {
2480                assert!(result.is_err());
2481                let expected_error_message =
2482                    expected_output_message_template.replace("{}", O::PREFIX);
2483                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2484            }
2485        }
2486    }
2487
2488    fn create_decimal128_array(
2489        array: Vec<Option<i128>>,
2490        precision: u8,
2491        scale: i8,
2492    ) -> Result<Decimal128Array, ArrowError> {
2493        array
2494            .into_iter()
2495            .collect::<Decimal128Array>()
2496            .with_precision_and_scale(precision, scale)
2497    }
2498
2499    fn create_decimal256_array(
2500        array: Vec<Option<i256>>,
2501        precision: u8,
2502        scale: i8,
2503    ) -> Result<Decimal256Array, ArrowError> {
2504        array
2505            .into_iter()
2506            .collect::<Decimal256Array>()
2507            .with_precision_and_scale(precision, scale)
2508    }
2509
2510    #[test]
2511    #[cfg(not(feature = "force_validate"))]
2512    #[should_panic(
2513        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2514    )]
2515    fn test_cast_decimal_to_decimal_round_with_error() {
2516        // decimal256 to decimal128 overflow
2517        let array = vec![
2518            Some(i256::from_i128(1123454)),
2519            Some(i256::from_i128(2123456)),
2520            Some(i256::from_i128(-3123453)),
2521            Some(i256::from_i128(-3123456)),
2522            None,
2523            Some(i256::MAX),
2524            Some(i256::MIN),
2525        ];
2526        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2527        let array = Arc::new(input_decimal_array) as ArrayRef;
2528        let input_type = DataType::Decimal256(76, 4);
2529        let output_type = DataType::Decimal128(20, 3);
2530        assert!(can_cast_types(&input_type, &output_type));
2531        generate_cast_test_case!(
2532            &array,
2533            Decimal128Array,
2534            &output_type,
2535            vec![
2536                Some(112345_i128),
2537                Some(212346_i128),
2538                Some(-312345_i128),
2539                Some(-312346_i128),
2540                None,
2541                None,
2542                None,
2543            ]
2544        );
2545    }
2546
2547    #[test]
2548    #[cfg(not(feature = "force_validate"))]
2549    fn test_cast_decimal_to_decimal_round() {
2550        let array = vec![
2551            Some(1123454),
2552            Some(2123456),
2553            Some(-3123453),
2554            Some(-3123456),
2555            None,
2556        ];
2557        let array = create_decimal128_array(array, 20, 4).unwrap();
2558        // decimal128 to decimal128
2559        let input_type = DataType::Decimal128(20, 4);
2560        let output_type = DataType::Decimal128(20, 3);
2561        assert!(can_cast_types(&input_type, &output_type));
2562        generate_cast_test_case!(
2563            &array,
2564            Decimal128Array,
2565            &output_type,
2566            vec![
2567                Some(112345_i128),
2568                Some(212346_i128),
2569                Some(-312345_i128),
2570                Some(-312346_i128),
2571                None
2572            ]
2573        );
2574
2575        // decimal128 to decimal256
2576        let input_type = DataType::Decimal128(20, 4);
2577        let output_type = DataType::Decimal256(20, 3);
2578        assert!(can_cast_types(&input_type, &output_type));
2579        generate_cast_test_case!(
2580            &array,
2581            Decimal256Array,
2582            &output_type,
2583            vec![
2584                Some(i256::from_i128(112345_i128)),
2585                Some(i256::from_i128(212346_i128)),
2586                Some(i256::from_i128(-312345_i128)),
2587                Some(i256::from_i128(-312346_i128)),
2588                None
2589            ]
2590        );
2591
2592        // decimal256
2593        let array = vec![
2594            Some(i256::from_i128(1123454)),
2595            Some(i256::from_i128(2123456)),
2596            Some(i256::from_i128(-3123453)),
2597            Some(i256::from_i128(-3123456)),
2598            None,
2599        ];
2600        let array = create_decimal256_array(array, 20, 4).unwrap();
2601
2602        // decimal256 to decimal256
2603        let input_type = DataType::Decimal256(20, 4);
2604        let output_type = DataType::Decimal256(20, 3);
2605        assert!(can_cast_types(&input_type, &output_type));
2606        generate_cast_test_case!(
2607            &array,
2608            Decimal256Array,
2609            &output_type,
2610            vec![
2611                Some(i256::from_i128(112345_i128)),
2612                Some(i256::from_i128(212346_i128)),
2613                Some(i256::from_i128(-312345_i128)),
2614                Some(i256::from_i128(-312346_i128)),
2615                None
2616            ]
2617        );
2618        // decimal256 to decimal128
2619        let input_type = DataType::Decimal256(20, 4);
2620        let output_type = DataType::Decimal128(20, 3);
2621        assert!(can_cast_types(&input_type, &output_type));
2622        generate_cast_test_case!(
2623            &array,
2624            Decimal128Array,
2625            &output_type,
2626            vec![
2627                Some(112345_i128),
2628                Some(212346_i128),
2629                Some(-312345_i128),
2630                Some(-312346_i128),
2631                None
2632            ]
2633        );
2634    }
2635
2636    #[test]
2637    fn test_cast_decimal128_to_decimal128() {
2638        let input_type = DataType::Decimal128(20, 3);
2639        let output_type = DataType::Decimal128(20, 4);
2640        assert!(can_cast_types(&input_type, &output_type));
2641        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2642        let array = create_decimal128_array(array, 20, 3).unwrap();
2643        generate_cast_test_case!(
2644            &array,
2645            Decimal128Array,
2646            &output_type,
2647            vec![
2648                Some(11234560_i128),
2649                Some(21234560_i128),
2650                Some(31234560_i128),
2651                None
2652            ]
2653        );
2654        // negative test
2655        let array = vec![Some(123456), None];
2656        let array = create_decimal128_array(array, 10, 0).unwrap();
2657        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
2658        assert!(result_safe.is_ok());
2659        let options = CastOptions {
2660            safe: false,
2661            ..Default::default()
2662        };
2663
2664        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
2665        assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal128 of precision 2. Max is 99",
2666                   result_unsafe.unwrap_err().to_string());
2667    }
2668
2669    #[test]
2670    fn test_cast_decimal128_to_decimal128_dict() {
2671        let p = 20;
2672        let s = 3;
2673        let input_type = DataType::Decimal128(p, s);
2674        let output_type = DataType::Dictionary(
2675            Box::new(DataType::Int32),
2676            Box::new(DataType::Decimal128(p, s)),
2677        );
2678        assert!(can_cast_types(&input_type, &output_type));
2679        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2680        let array = create_decimal128_array(array, p, s).unwrap();
2681        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2682        assert_eq!(cast_array.data_type(), &output_type);
2683    }
2684
2685    #[test]
2686    fn test_cast_decimal256_to_decimal256_dict() {
2687        let p = 20;
2688        let s = 3;
2689        let input_type = DataType::Decimal256(p, s);
2690        let output_type = DataType::Dictionary(
2691            Box::new(DataType::Int32),
2692            Box::new(DataType::Decimal256(p, s)),
2693        );
2694        assert!(can_cast_types(&input_type, &output_type));
2695        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2696        let array = create_decimal128_array(array, p, s).unwrap();
2697        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2698        assert_eq!(cast_array.data_type(), &output_type);
2699    }
2700
2701    #[test]
2702    fn test_cast_decimal128_to_decimal128_overflow() {
2703        let input_type = DataType::Decimal128(38, 3);
2704        let output_type = DataType::Decimal128(38, 38);
2705        assert!(can_cast_types(&input_type, &output_type));
2706
2707        let array = vec![Some(i128::MAX)];
2708        let array = create_decimal128_array(array, 38, 3).unwrap();
2709        let result = cast_with_options(
2710            &array,
2711            &output_type,
2712            &CastOptions {
2713                safe: false,
2714                format_options: FormatOptions::default(),
2715            },
2716        );
2717        assert_eq!("Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
2718                   result.unwrap_err().to_string());
2719    }
2720
2721    #[test]
2722    fn test_cast_decimal128_to_decimal256_overflow() {
2723        let input_type = DataType::Decimal128(38, 3);
2724        let output_type = DataType::Decimal256(76, 76);
2725        assert!(can_cast_types(&input_type, &output_type));
2726
2727        let array = vec![Some(i128::MAX)];
2728        let array = create_decimal128_array(array, 38, 3).unwrap();
2729        let result = cast_with_options(
2730            &array,
2731            &output_type,
2732            &CastOptions {
2733                safe: false,
2734                format_options: FormatOptions::default(),
2735            },
2736        );
2737        assert_eq!("Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
2738                   result.unwrap_err().to_string());
2739    }
2740
2741    #[test]
2742    fn test_cast_decimal128_to_decimal256() {
2743        let input_type = DataType::Decimal128(20, 3);
2744        let output_type = DataType::Decimal256(20, 4);
2745        assert!(can_cast_types(&input_type, &output_type));
2746        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2747        let array = create_decimal128_array(array, 20, 3).unwrap();
2748        generate_cast_test_case!(
2749            &array,
2750            Decimal256Array,
2751            &output_type,
2752            vec![
2753                Some(i256::from_i128(11234560_i128)),
2754                Some(i256::from_i128(21234560_i128)),
2755                Some(i256::from_i128(31234560_i128)),
2756                None
2757            ]
2758        );
2759    }
2760
2761    #[test]
2762    fn test_cast_decimal256_to_decimal128_overflow() {
2763        let input_type = DataType::Decimal256(76, 5);
2764        let output_type = DataType::Decimal128(38, 7);
2765        assert!(can_cast_types(&input_type, &output_type));
2766        let array = vec![Some(i256::from_i128(i128::MAX))];
2767        let array = create_decimal256_array(array, 76, 5).unwrap();
2768        let result = cast_with_options(
2769            &array,
2770            &output_type,
2771            &CastOptions {
2772                safe: false,
2773                format_options: FormatOptions::default(),
2774            },
2775        );
2776        assert_eq!("Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
2777                   result.unwrap_err().to_string());
2778    }
2779
2780    #[test]
2781    fn test_cast_decimal256_to_decimal256_overflow() {
2782        let input_type = DataType::Decimal256(76, 5);
2783        let output_type = DataType::Decimal256(76, 55);
2784        assert!(can_cast_types(&input_type, &output_type));
2785        let array = vec![Some(i256::from_i128(i128::MAX))];
2786        let array = create_decimal256_array(array, 76, 5).unwrap();
2787        let result = cast_with_options(
2788            &array,
2789            &output_type,
2790            &CastOptions {
2791                safe: false,
2792                format_options: FormatOptions::default(),
2793            },
2794        );
2795        assert_eq!("Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
2796                   result.unwrap_err().to_string());
2797    }
2798
2799    #[test]
2800    fn test_cast_decimal256_to_decimal128() {
2801        let input_type = DataType::Decimal256(20, 3);
2802        let output_type = DataType::Decimal128(20, 4);
2803        assert!(can_cast_types(&input_type, &output_type));
2804        let array = vec![
2805            Some(i256::from_i128(1123456)),
2806            Some(i256::from_i128(2123456)),
2807            Some(i256::from_i128(3123456)),
2808            None,
2809        ];
2810        let array = create_decimal256_array(array, 20, 3).unwrap();
2811        generate_cast_test_case!(
2812            &array,
2813            Decimal128Array,
2814            &output_type,
2815            vec![
2816                Some(11234560_i128),
2817                Some(21234560_i128),
2818                Some(31234560_i128),
2819                None
2820            ]
2821        );
2822    }
2823
2824    #[test]
2825    fn test_cast_decimal256_to_decimal256() {
2826        let input_type = DataType::Decimal256(20, 3);
2827        let output_type = DataType::Decimal256(20, 4);
2828        assert!(can_cast_types(&input_type, &output_type));
2829        let array = vec![
2830            Some(i256::from_i128(1123456)),
2831            Some(i256::from_i128(2123456)),
2832            Some(i256::from_i128(3123456)),
2833            None,
2834        ];
2835        let array = create_decimal256_array(array, 20, 3).unwrap();
2836        generate_cast_test_case!(
2837            &array,
2838            Decimal256Array,
2839            &output_type,
2840            vec![
2841                Some(i256::from_i128(11234560_i128)),
2842                Some(i256::from_i128(21234560_i128)),
2843                Some(i256::from_i128(31234560_i128)),
2844                None
2845            ]
2846        );
2847    }
2848
2849    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
2850    where
2851        T: ArrowPrimitiveType + DecimalType,
2852    {
2853        // u8
2854        generate_cast_test_case!(
2855            array,
2856            UInt8Array,
2857            &DataType::UInt8,
2858            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
2859        );
2860        // u16
2861        generate_cast_test_case!(
2862            array,
2863            UInt16Array,
2864            &DataType::UInt16,
2865            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
2866        );
2867        // u32
2868        generate_cast_test_case!(
2869            array,
2870            UInt32Array,
2871            &DataType::UInt32,
2872            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
2873        );
2874        // u64
2875        generate_cast_test_case!(
2876            array,
2877            UInt64Array,
2878            &DataType::UInt64,
2879            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
2880        );
2881        // i8
2882        generate_cast_test_case!(
2883            array,
2884            Int8Array,
2885            &DataType::Int8,
2886            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
2887        );
2888        // i16
2889        generate_cast_test_case!(
2890            array,
2891            Int16Array,
2892            &DataType::Int16,
2893            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
2894        );
2895        // i32
2896        generate_cast_test_case!(
2897            array,
2898            Int32Array,
2899            &DataType::Int32,
2900            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
2901        );
2902        // i64
2903        generate_cast_test_case!(
2904            array,
2905            Int64Array,
2906            &DataType::Int64,
2907            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
2908        );
2909        // f32
2910        generate_cast_test_case!(
2911            array,
2912            Float32Array,
2913            &DataType::Float32,
2914            vec![
2915                Some(1.25_f32),
2916                Some(2.25_f32),
2917                Some(3.25_f32),
2918                None,
2919                Some(5.25_f32)
2920            ]
2921        );
2922        // f64
2923        generate_cast_test_case!(
2924            array,
2925            Float64Array,
2926            &DataType::Float64,
2927            vec![
2928                Some(1.25_f64),
2929                Some(2.25_f64),
2930                Some(3.25_f64),
2931                None,
2932                Some(5.25_f64)
2933            ]
2934        );
2935    }
2936
2937    #[test]
2938    fn test_cast_decimal128_to_numeric() {
2939        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
2940        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2941
2942        generate_decimal_to_numeric_cast_test_case(&array);
2943
2944        // overflow test: out of range of max u8
2945        let value_array: Vec<Option<i128>> = vec![Some(51300)];
2946        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2947        let casted_array = cast_with_options(
2948            &array,
2949            &DataType::UInt8,
2950            &CastOptions {
2951                safe: false,
2952                format_options: FormatOptions::default(),
2953            },
2954        );
2955        assert_eq!(
2956            "Cast error: value of 513 is out of range UInt8".to_string(),
2957            casted_array.unwrap_err().to_string()
2958        );
2959
2960        let casted_array = cast_with_options(
2961            &array,
2962            &DataType::UInt8,
2963            &CastOptions {
2964                safe: true,
2965                format_options: FormatOptions::default(),
2966            },
2967        );
2968        assert!(casted_array.is_ok());
2969        assert!(casted_array.unwrap().is_null(0));
2970
2971        // overflow test: out of range of max i8
2972        let value_array: Vec<Option<i128>> = vec![Some(24400)];
2973        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2974        let casted_array = cast_with_options(
2975            &array,
2976            &DataType::Int8,
2977            &CastOptions {
2978                safe: false,
2979                format_options: FormatOptions::default(),
2980            },
2981        );
2982        assert_eq!(
2983            "Cast error: value of 244 is out of range Int8".to_string(),
2984            casted_array.unwrap_err().to_string()
2985        );
2986
2987        let casted_array = cast_with_options(
2988            &array,
2989            &DataType::Int8,
2990            &CastOptions {
2991                safe: true,
2992                format_options: FormatOptions::default(),
2993            },
2994        );
2995        assert!(casted_array.is_ok());
2996        assert!(casted_array.unwrap().is_null(0));
2997
2998        // loss the precision: convert decimal to f32、f64
2999        // f32
3000        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3001        let value_array: Vec<Option<i128>> = vec![
3002            Some(125),
3003            Some(225),
3004            Some(325),
3005            None,
3006            Some(525),
3007            Some(112345678),
3008            Some(112345679),
3009        ];
3010        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3011        generate_cast_test_case!(
3012            &array,
3013            Float32Array,
3014            &DataType::Float32,
3015            vec![
3016                Some(1.25_f32),
3017                Some(2.25_f32),
3018                Some(3.25_f32),
3019                None,
3020                Some(5.25_f32),
3021                Some(1_123_456.7_f32),
3022                Some(1_123_456.7_f32)
3023            ]
3024        );
3025
3026        // f64
3027        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3028        let value_array: Vec<Option<i128>> = vec![
3029            Some(125),
3030            Some(225),
3031            Some(325),
3032            None,
3033            Some(525),
3034            Some(112345678901234568),
3035            Some(112345678901234560),
3036        ];
3037        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3038        generate_cast_test_case!(
3039            &array,
3040            Float64Array,
3041            &DataType::Float64,
3042            vec![
3043                Some(1.25_f64),
3044                Some(2.25_f64),
3045                Some(3.25_f64),
3046                None,
3047                Some(5.25_f64),
3048                Some(1_123_456_789_012_345.6_f64),
3049                Some(1_123_456_789_012_345.6_f64),
3050            ]
3051        );
3052    }
3053
3054    #[test]
3055    fn test_cast_decimal256_to_numeric() {
3056        let value_array: Vec<Option<i256>> = vec![
3057            Some(i256::from_i128(125)),
3058            Some(i256::from_i128(225)),
3059            Some(i256::from_i128(325)),
3060            None,
3061            Some(i256::from_i128(525)),
3062        ];
3063        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3064        // u8
3065        generate_cast_test_case!(
3066            &array,
3067            UInt8Array,
3068            &DataType::UInt8,
3069            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3070        );
3071        // u16
3072        generate_cast_test_case!(
3073            &array,
3074            UInt16Array,
3075            &DataType::UInt16,
3076            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3077        );
3078        // u32
3079        generate_cast_test_case!(
3080            &array,
3081            UInt32Array,
3082            &DataType::UInt32,
3083            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3084        );
3085        // u64
3086        generate_cast_test_case!(
3087            &array,
3088            UInt64Array,
3089            &DataType::UInt64,
3090            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3091        );
3092        // i8
3093        generate_cast_test_case!(
3094            &array,
3095            Int8Array,
3096            &DataType::Int8,
3097            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3098        );
3099        // i16
3100        generate_cast_test_case!(
3101            &array,
3102            Int16Array,
3103            &DataType::Int16,
3104            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3105        );
3106        // i32
3107        generate_cast_test_case!(
3108            &array,
3109            Int32Array,
3110            &DataType::Int32,
3111            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3112        );
3113        // i64
3114        generate_cast_test_case!(
3115            &array,
3116            Int64Array,
3117            &DataType::Int64,
3118            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3119        );
3120        // f32
3121        generate_cast_test_case!(
3122            &array,
3123            Float32Array,
3124            &DataType::Float32,
3125            vec![
3126                Some(1.25_f32),
3127                Some(2.25_f32),
3128                Some(3.25_f32),
3129                None,
3130                Some(5.25_f32)
3131            ]
3132        );
3133        // f64
3134        generate_cast_test_case!(
3135            &array,
3136            Float64Array,
3137            &DataType::Float64,
3138            vec![
3139                Some(1.25_f64),
3140                Some(2.25_f64),
3141                Some(3.25_f64),
3142                None,
3143                Some(5.25_f64)
3144            ]
3145        );
3146
3147        // overflow test: out of range of max i8
3148        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3149        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3150        let casted_array = cast_with_options(
3151            &array,
3152            &DataType::Int8,
3153            &CastOptions {
3154                safe: false,
3155                format_options: FormatOptions::default(),
3156            },
3157        );
3158        assert_eq!(
3159            "Cast error: value of 244 is out of range Int8".to_string(),
3160            casted_array.unwrap_err().to_string()
3161        );
3162
3163        let casted_array = cast_with_options(
3164            &array,
3165            &DataType::Int8,
3166            &CastOptions {
3167                safe: true,
3168                format_options: FormatOptions::default(),
3169            },
3170        );
3171        assert!(casted_array.is_ok());
3172        assert!(casted_array.unwrap().is_null(0));
3173
3174        // loss the precision: convert decimal to f32、f64
3175        // f32
3176        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3177        let value_array: Vec<Option<i256>> = vec![
3178            Some(i256::from_i128(125)),
3179            Some(i256::from_i128(225)),
3180            Some(i256::from_i128(325)),
3181            None,
3182            Some(i256::from_i128(525)),
3183            Some(i256::from_i128(112345678)),
3184            Some(i256::from_i128(112345679)),
3185        ];
3186        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3187        generate_cast_test_case!(
3188            &array,
3189            Float32Array,
3190            &DataType::Float32,
3191            vec![
3192                Some(1.25_f32),
3193                Some(2.25_f32),
3194                Some(3.25_f32),
3195                None,
3196                Some(5.25_f32),
3197                Some(1_123_456.7_f32),
3198                Some(1_123_456.7_f32)
3199            ]
3200        );
3201
3202        // f64
3203        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3204        let value_array: Vec<Option<i256>> = vec![
3205            Some(i256::from_i128(125)),
3206            Some(i256::from_i128(225)),
3207            Some(i256::from_i128(325)),
3208            None,
3209            Some(i256::from_i128(525)),
3210            Some(i256::from_i128(112345678901234568)),
3211            Some(i256::from_i128(112345678901234560)),
3212        ];
3213        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3214        generate_cast_test_case!(
3215            &array,
3216            Float64Array,
3217            &DataType::Float64,
3218            vec![
3219                Some(1.25_f64),
3220                Some(2.25_f64),
3221                Some(3.25_f64),
3222                None,
3223                Some(5.25_f64),
3224                Some(1_123_456_789_012_345.6_f64),
3225                Some(1_123_456_789_012_345.6_f64),
3226            ]
3227        );
3228    }
3229
3230    #[test]
3231    fn test_cast_numeric_to_decimal128() {
3232        let decimal_type = DataType::Decimal128(38, 6);
3233        // u8, u16, u32, u64
3234        let input_datas = vec![
3235            Arc::new(UInt8Array::from(vec![
3236                Some(1),
3237                Some(2),
3238                Some(3),
3239                None,
3240                Some(5),
3241            ])) as ArrayRef, // u8
3242            Arc::new(UInt16Array::from(vec![
3243                Some(1),
3244                Some(2),
3245                Some(3),
3246                None,
3247                Some(5),
3248            ])) as ArrayRef, // u16
3249            Arc::new(UInt32Array::from(vec![
3250                Some(1),
3251                Some(2),
3252                Some(3),
3253                None,
3254                Some(5),
3255            ])) as ArrayRef, // u32
3256            Arc::new(UInt64Array::from(vec![
3257                Some(1),
3258                Some(2),
3259                Some(3),
3260                None,
3261                Some(5),
3262            ])) as ArrayRef, // u64
3263        ];
3264
3265        for array in input_datas {
3266            generate_cast_test_case!(
3267                &array,
3268                Decimal128Array,
3269                &decimal_type,
3270                vec![
3271                    Some(1000000_i128),
3272                    Some(2000000_i128),
3273                    Some(3000000_i128),
3274                    None,
3275                    Some(5000000_i128)
3276                ]
3277            );
3278        }
3279
3280        // i8, i16, i32, i64
3281        let input_datas = vec![
3282            Arc::new(Int8Array::from(vec![
3283                Some(1),
3284                Some(2),
3285                Some(3),
3286                None,
3287                Some(5),
3288            ])) as ArrayRef, // i8
3289            Arc::new(Int16Array::from(vec![
3290                Some(1),
3291                Some(2),
3292                Some(3),
3293                None,
3294                Some(5),
3295            ])) as ArrayRef, // i16
3296            Arc::new(Int32Array::from(vec![
3297                Some(1),
3298                Some(2),
3299                Some(3),
3300                None,
3301                Some(5),
3302            ])) as ArrayRef, // i32
3303            Arc::new(Int64Array::from(vec![
3304                Some(1),
3305                Some(2),
3306                Some(3),
3307                None,
3308                Some(5),
3309            ])) as ArrayRef, // i64
3310        ];
3311        for array in input_datas {
3312            generate_cast_test_case!(
3313                &array,
3314                Decimal128Array,
3315                &decimal_type,
3316                vec![
3317                    Some(1000000_i128),
3318                    Some(2000000_i128),
3319                    Some(3000000_i128),
3320                    None,
3321                    Some(5000000_i128)
3322                ]
3323            );
3324        }
3325
3326        // test u8 to decimal type with overflow the result type
3327        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3328        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
3329        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3330        assert!(casted_array.is_ok());
3331        let array = casted_array.unwrap();
3332        let array: &Decimal128Array = array.as_primitive();
3333        assert!(array.is_null(4));
3334
3335        // test i8 to decimal type with overflow the result type
3336        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3337        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3338        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3339        assert!(casted_array.is_ok());
3340        let array = casted_array.unwrap();
3341        let array: &Decimal128Array = array.as_primitive();
3342        assert!(array.is_null(4));
3343
3344        // test f32 to decimal type
3345        let array = Float32Array::from(vec![
3346            Some(1.1),
3347            Some(2.2),
3348            Some(4.4),
3349            None,
3350            Some(1.123_456_4), // round down
3351            Some(1.123_456_7), // round up
3352        ]);
3353        let array = Arc::new(array) as ArrayRef;
3354        generate_cast_test_case!(
3355            &array,
3356            Decimal128Array,
3357            &decimal_type,
3358            vec![
3359                Some(1100000_i128),
3360                Some(2200000_i128),
3361                Some(4400000_i128),
3362                None,
3363                Some(1123456_i128), // round down
3364                Some(1123457_i128), // round up
3365            ]
3366        );
3367
3368        // test f64 to decimal type
3369        let array = Float64Array::from(vec![
3370            Some(1.1),
3371            Some(2.2),
3372            Some(4.4),
3373            None,
3374            Some(1.123_456_489_123_4),     // round up
3375            Some(1.123_456_789_123_4),     // round up
3376            Some(1.123_456_489_012_345_6), // round down
3377            Some(1.123_456_789_012_345_6), // round up
3378        ]);
3379        generate_cast_test_case!(
3380            &array,
3381            Decimal128Array,
3382            &decimal_type,
3383            vec![
3384                Some(1100000_i128),
3385                Some(2200000_i128),
3386                Some(4400000_i128),
3387                None,
3388                Some(1123456_i128), // round down
3389                Some(1123457_i128), // round up
3390                Some(1123456_i128), // round down
3391                Some(1123457_i128), // round up
3392            ]
3393        );
3394    }
3395
3396    #[test]
3397    fn test_cast_numeric_to_decimal256() {
3398        let decimal_type = DataType::Decimal256(76, 6);
3399        // u8, u16, u32, u64
3400        let input_datas = vec![
3401            Arc::new(UInt8Array::from(vec![
3402                Some(1),
3403                Some(2),
3404                Some(3),
3405                None,
3406                Some(5),
3407            ])) as ArrayRef, // u8
3408            Arc::new(UInt16Array::from(vec![
3409                Some(1),
3410                Some(2),
3411                Some(3),
3412                None,
3413                Some(5),
3414            ])) as ArrayRef, // u16
3415            Arc::new(UInt32Array::from(vec![
3416                Some(1),
3417                Some(2),
3418                Some(3),
3419                None,
3420                Some(5),
3421            ])) as ArrayRef, // u32
3422            Arc::new(UInt64Array::from(vec![
3423                Some(1),
3424                Some(2),
3425                Some(3),
3426                None,
3427                Some(5),
3428            ])) as ArrayRef, // u64
3429        ];
3430
3431        for array in input_datas {
3432            generate_cast_test_case!(
3433                &array,
3434                Decimal256Array,
3435                &decimal_type,
3436                vec![
3437                    Some(i256::from_i128(1000000_i128)),
3438                    Some(i256::from_i128(2000000_i128)),
3439                    Some(i256::from_i128(3000000_i128)),
3440                    None,
3441                    Some(i256::from_i128(5000000_i128))
3442                ]
3443            );
3444        }
3445
3446        // i8, i16, i32, i64
3447        let input_datas = vec![
3448            Arc::new(Int8Array::from(vec![
3449                Some(1),
3450                Some(2),
3451                Some(3),
3452                None,
3453                Some(5),
3454            ])) as ArrayRef, // i8
3455            Arc::new(Int16Array::from(vec![
3456                Some(1),
3457                Some(2),
3458                Some(3),
3459                None,
3460                Some(5),
3461            ])) as ArrayRef, // i16
3462            Arc::new(Int32Array::from(vec![
3463                Some(1),
3464                Some(2),
3465                Some(3),
3466                None,
3467                Some(5),
3468            ])) as ArrayRef, // i32
3469            Arc::new(Int64Array::from(vec![
3470                Some(1),
3471                Some(2),
3472                Some(3),
3473                None,
3474                Some(5),
3475            ])) as ArrayRef, // i64
3476        ];
3477        for array in input_datas {
3478            generate_cast_test_case!(
3479                &array,
3480                Decimal256Array,
3481                &decimal_type,
3482                vec![
3483                    Some(i256::from_i128(1000000_i128)),
3484                    Some(i256::from_i128(2000000_i128)),
3485                    Some(i256::from_i128(3000000_i128)),
3486                    None,
3487                    Some(i256::from_i128(5000000_i128))
3488                ]
3489            );
3490        }
3491
3492        // test i8 to decimal type with overflow the result type
3493        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3494        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3495        let array = Arc::new(array) as ArrayRef;
3496        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
3497        assert!(casted_array.is_ok());
3498        let array = casted_array.unwrap();
3499        let array: &Decimal256Array = array.as_primitive();
3500        assert!(array.is_null(4));
3501
3502        // test f32 to decimal type
3503        let array = Float32Array::from(vec![
3504            Some(1.1),
3505            Some(2.2),
3506            Some(4.4),
3507            None,
3508            Some(1.123_456_4), // round down
3509            Some(1.123_456_7), // round up
3510        ]);
3511        generate_cast_test_case!(
3512            &array,
3513            Decimal256Array,
3514            &decimal_type,
3515            vec![
3516                Some(i256::from_i128(1100000_i128)),
3517                Some(i256::from_i128(2200000_i128)),
3518                Some(i256::from_i128(4400000_i128)),
3519                None,
3520                Some(i256::from_i128(1123456_i128)), // round down
3521                Some(i256::from_i128(1123457_i128)), // round up
3522            ]
3523        );
3524
3525        // test f64 to decimal type
3526        let array = Float64Array::from(vec![
3527            Some(1.1),
3528            Some(2.2),
3529            Some(4.4),
3530            None,
3531            Some(1.123_456_489_123_4),     // round down
3532            Some(1.123_456_789_123_4),     // round up
3533            Some(1.123_456_489_012_345_6), // round down
3534            Some(1.123_456_789_012_345_6), // round up
3535        ]);
3536        generate_cast_test_case!(
3537            &array,
3538            Decimal256Array,
3539            &decimal_type,
3540            vec![
3541                Some(i256::from_i128(1100000_i128)),
3542                Some(i256::from_i128(2200000_i128)),
3543                Some(i256::from_i128(4400000_i128)),
3544                None,
3545                Some(i256::from_i128(1123456_i128)), // round down
3546                Some(i256::from_i128(1123457_i128)), // round up
3547                Some(i256::from_i128(1123456_i128)), // round down
3548                Some(i256::from_i128(1123457_i128)), // round up
3549            ]
3550        );
3551    }
3552
3553    #[test]
3554    fn test_cast_i32_to_f64() {
3555        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3556        let b = cast(&array, &DataType::Float64).unwrap();
3557        let c = b.as_primitive::<Float64Type>();
3558        assert_eq!(5.0, c.value(0));
3559        assert_eq!(6.0, c.value(1));
3560        assert_eq!(7.0, c.value(2));
3561        assert_eq!(8.0, c.value(3));
3562        assert_eq!(9.0, c.value(4));
3563    }
3564
3565    #[test]
3566    fn test_cast_i32_to_u8() {
3567        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3568        let b = cast(&array, &DataType::UInt8).unwrap();
3569        let c = b.as_primitive::<UInt8Type>();
3570        assert!(!c.is_valid(0));
3571        assert_eq!(6, c.value(1));
3572        assert!(!c.is_valid(2));
3573        assert_eq!(8, c.value(3));
3574        // overflows return None
3575        assert!(!c.is_valid(4));
3576    }
3577
3578    #[test]
3579    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
3580    fn test_cast_int32_to_u8_with_error() {
3581        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3582        // overflow with the error
3583        let cast_option = CastOptions {
3584            safe: false,
3585            format_options: FormatOptions::default(),
3586        };
3587        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
3588        assert!(result.is_err());
3589        result.unwrap();
3590    }
3591
3592    #[test]
3593    fn test_cast_i32_to_u8_sliced() {
3594        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3595        assert_eq!(0, array.offset());
3596        let array = array.slice(2, 3);
3597        let b = cast(&array, &DataType::UInt8).unwrap();
3598        assert_eq!(3, b.len());
3599        let c = b.as_primitive::<UInt8Type>();
3600        assert!(!c.is_valid(0));
3601        assert_eq!(8, c.value(1));
3602        // overflows return None
3603        assert!(!c.is_valid(2));
3604    }
3605
3606    #[test]
3607    fn test_cast_i32_to_i32() {
3608        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3609        let b = cast(&array, &DataType::Int32).unwrap();
3610        let c = b.as_primitive::<Int32Type>();
3611        assert_eq!(5, c.value(0));
3612        assert_eq!(6, c.value(1));
3613        assert_eq!(7, c.value(2));
3614        assert_eq!(8, c.value(3));
3615        assert_eq!(9, c.value(4));
3616    }
3617
3618    #[test]
3619    fn test_cast_i32_to_list_i32() {
3620        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3621        let b = cast(
3622            &array,
3623            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
3624        )
3625        .unwrap();
3626        assert_eq!(5, b.len());
3627        let arr = b.as_list::<i32>();
3628        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
3629        assert_eq!(1, arr.value_length(0));
3630        assert_eq!(1, arr.value_length(1));
3631        assert_eq!(1, arr.value_length(2));
3632        assert_eq!(1, arr.value_length(3));
3633        assert_eq!(1, arr.value_length(4));
3634        let c = arr.values().as_primitive::<Int32Type>();
3635        assert_eq!(5, c.value(0));
3636        assert_eq!(6, c.value(1));
3637        assert_eq!(7, c.value(2));
3638        assert_eq!(8, c.value(3));
3639        assert_eq!(9, c.value(4));
3640    }
3641
3642    #[test]
3643    fn test_cast_i32_to_list_i32_nullable() {
3644        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
3645        let b = cast(
3646            &array,
3647            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
3648        )
3649        .unwrap();
3650        assert_eq!(5, b.len());
3651        assert_eq!(0, b.null_count());
3652        let arr = b.as_list::<i32>();
3653        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
3654        assert_eq!(1, arr.value_length(0));
3655        assert_eq!(1, arr.value_length(1));
3656        assert_eq!(1, arr.value_length(2));
3657        assert_eq!(1, arr.value_length(3));
3658        assert_eq!(1, arr.value_length(4));
3659
3660        let c = arr.values().as_primitive::<Int32Type>();
3661        assert_eq!(1, c.null_count());
3662        assert_eq!(5, c.value(0));
3663        assert!(!c.is_valid(1));
3664        assert_eq!(7, c.value(2));
3665        assert_eq!(8, c.value(3));
3666        assert_eq!(9, c.value(4));
3667    }
3668
3669    #[test]
3670    fn test_cast_i32_to_list_f64_nullable_sliced() {
3671        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
3672        let array = array.slice(2, 4);
3673        let b = cast(
3674            &array,
3675            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
3676        )
3677        .unwrap();
3678        assert_eq!(4, b.len());
3679        assert_eq!(0, b.null_count());
3680        let arr = b.as_list::<i32>();
3681        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
3682        assert_eq!(1, arr.value_length(0));
3683        assert_eq!(1, arr.value_length(1));
3684        assert_eq!(1, arr.value_length(2));
3685        assert_eq!(1, arr.value_length(3));
3686        let c = arr.values().as_primitive::<Float64Type>();
3687        assert_eq!(1, c.null_count());
3688        assert_eq!(7.0, c.value(0));
3689        assert_eq!(8.0, c.value(1));
3690        assert!(!c.is_valid(2));
3691        assert_eq!(10.0, c.value(3));
3692    }
3693
3694    #[test]
3695    fn test_cast_int_to_utf8view() {
3696        let inputs = vec![
3697            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3698            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3699            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3700            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3701            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3702            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3703            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3704            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3705        ];
3706        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
3707            None,
3708            Some("8"),
3709            Some("9"),
3710            Some("10"),
3711        ]));
3712
3713        for array in inputs {
3714            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
3715            let arr = cast(&array, &DataType::Utf8View).unwrap();
3716            assert_eq!(expected.as_ref(), arr.as_ref());
3717        }
3718    }
3719
3720    #[test]
3721    fn test_cast_float_to_utf8view() {
3722        let inputs = vec![
3723            Arc::new(Float16Array::from(vec![
3724                Some(f16::from_f64(1.5)),
3725                Some(f16::from_f64(2.5)),
3726                None,
3727            ])) as ArrayRef,
3728            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
3729            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
3730        ];
3731
3732        let expected: ArrayRef =
3733            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
3734
3735        for array in inputs {
3736            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
3737            let arr = cast(&array, &DataType::Utf8View).unwrap();
3738            assert_eq!(expected.as_ref(), arr.as_ref());
3739        }
3740    }
3741
3742    #[test]
3743    fn test_cast_utf8_to_i32() {
3744        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3745        let b = cast(&array, &DataType::Int32).unwrap();
3746        let c = b.as_primitive::<Int32Type>();
3747        assert_eq!(5, c.value(0));
3748        assert_eq!(6, c.value(1));
3749        assert!(!c.is_valid(2));
3750        assert_eq!(8, c.value(3));
3751        assert!(!c.is_valid(4));
3752    }
3753
3754    #[test]
3755    fn test_cast_utf8view_to_i32() {
3756        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3757        let b = cast(&array, &DataType::Int32).unwrap();
3758        let c = b.as_primitive::<Int32Type>();
3759        assert_eq!(5, c.value(0));
3760        assert_eq!(6, c.value(1));
3761        assert!(!c.is_valid(2));
3762        assert_eq!(8, c.value(3));
3763        assert!(!c.is_valid(4));
3764    }
3765
3766    #[test]
3767    fn test_cast_utf8view_to_f32() {
3768        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
3769        let b = cast(&array, &DataType::Float32).unwrap();
3770        let c = b.as_primitive::<Float32Type>();
3771        assert_eq!(3.0, c.value(0));
3772        assert_eq!(4.56, c.value(1));
3773        assert!(!c.is_valid(2));
3774        assert_eq!(8.9, c.value(3));
3775    }
3776
3777    #[test]
3778    fn test_cast_utf8view_to_decimal128() {
3779        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
3780        let arr = Arc::new(array) as ArrayRef;
3781        generate_cast_test_case!(
3782            &arr,
3783            Decimal128Array,
3784            &DataType::Decimal128(4, 2),
3785            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
3786        );
3787    }
3788
3789    #[test]
3790    fn test_cast_with_options_utf8_to_i32() {
3791        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3792        let result = cast_with_options(
3793            &array,
3794            &DataType::Int32,
3795            &CastOptions {
3796                safe: false,
3797                format_options: FormatOptions::default(),
3798            },
3799        );
3800        match result {
3801            Ok(_) => panic!("expected error"),
3802            Err(e) => {
3803                assert!(
3804                    e.to_string()
3805                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
3806                    "Error: {e}"
3807                )
3808            }
3809        }
3810    }
3811
3812    #[test]
3813    fn test_cast_utf8_to_bool() {
3814        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3815        let casted = cast(&strings, &DataType::Boolean).unwrap();
3816        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
3817        assert_eq!(*as_boolean_array(&casted), expected);
3818    }
3819
3820    #[test]
3821    fn test_cast_utf8view_to_bool() {
3822        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3823        let casted = cast(&strings, &DataType::Boolean).unwrap();
3824        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
3825        assert_eq!(*as_boolean_array(&casted), expected);
3826    }
3827
3828    #[test]
3829    fn test_cast_with_options_utf8_to_bool() {
3830        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3831        let casted = cast_with_options(
3832            &strings,
3833            &DataType::Boolean,
3834            &CastOptions {
3835                safe: false,
3836                format_options: FormatOptions::default(),
3837            },
3838        );
3839        match casted {
3840            Ok(_) => panic!("expected error"),
3841            Err(e) => {
3842                assert!(e
3843                    .to_string()
3844                    .contains("Cast error: Cannot cast value 'invalid' to value of Boolean type"))
3845            }
3846        }
3847    }
3848
3849    #[test]
3850    fn test_cast_bool_to_i32() {
3851        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3852        let b = cast(&array, &DataType::Int32).unwrap();
3853        let c = b.as_primitive::<Int32Type>();
3854        assert_eq!(1, c.value(0));
3855        assert_eq!(0, c.value(1));
3856        assert!(!c.is_valid(2));
3857    }
3858
3859    #[test]
3860    fn test_cast_bool_to_utf8view() {
3861        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3862        let b = cast(&array, &DataType::Utf8View).unwrap();
3863        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
3864        assert_eq!("true", c.value(0));
3865        assert_eq!("false", c.value(1));
3866        assert!(!c.is_valid(2));
3867    }
3868
3869    #[test]
3870    fn test_cast_bool_to_utf8() {
3871        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3872        let b = cast(&array, &DataType::Utf8).unwrap();
3873        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
3874        assert_eq!("true", c.value(0));
3875        assert_eq!("false", c.value(1));
3876        assert!(!c.is_valid(2));
3877    }
3878
3879    #[test]
3880    fn test_cast_bool_to_large_utf8() {
3881        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3882        let b = cast(&array, &DataType::LargeUtf8).unwrap();
3883        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
3884        assert_eq!("true", c.value(0));
3885        assert_eq!("false", c.value(1));
3886        assert!(!c.is_valid(2));
3887    }
3888
3889    #[test]
3890    fn test_cast_bool_to_f64() {
3891        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3892        let b = cast(&array, &DataType::Float64).unwrap();
3893        let c = b.as_primitive::<Float64Type>();
3894        assert_eq!(1.0, c.value(0));
3895        assert_eq!(0.0, c.value(1));
3896        assert!(!c.is_valid(2));
3897    }
3898
3899    #[test]
3900    fn test_cast_integer_to_timestamp() {
3901        let array = Int64Array::from(vec![Some(2), Some(10), None]);
3902        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3903
3904        let array = Int8Array::from(vec![Some(2), Some(10), None]);
3905        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3906
3907        assert_eq!(&actual, &expected);
3908
3909        let array = Int16Array::from(vec![Some(2), Some(10), None]);
3910        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3911
3912        assert_eq!(&actual, &expected);
3913
3914        let array = Int32Array::from(vec![Some(2), Some(10), None]);
3915        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3916
3917        assert_eq!(&actual, &expected);
3918
3919        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
3920        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3921
3922        assert_eq!(&actual, &expected);
3923
3924        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
3925        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3926
3927        assert_eq!(&actual, &expected);
3928
3929        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
3930        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3931
3932        assert_eq!(&actual, &expected);
3933
3934        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
3935        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3936
3937        assert_eq!(&actual, &expected);
3938    }
3939
3940    #[test]
3941    fn test_cast_timestamp_to_integer() {
3942        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
3943            .with_timezone("UTC".to_string());
3944        let expected = cast(&array, &DataType::Int64).unwrap();
3945
3946        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
3947        assert_eq!(&actual, &expected);
3948
3949        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
3950        assert_eq!(&actual, &expected);
3951
3952        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
3953        assert_eq!(&actual, &expected);
3954
3955        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
3956        assert_eq!(&actual, &expected);
3957
3958        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
3959        assert_eq!(&actual, &expected);
3960
3961        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
3962        assert_eq!(&actual, &expected);
3963
3964        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
3965        assert_eq!(&actual, &expected);
3966    }
3967
3968    #[test]
3969    fn test_cast_floating_to_timestamp() {
3970        let array = Int64Array::from(vec![Some(2), Some(10), None]);
3971        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3972
3973        let array = Float16Array::from(vec![
3974            Some(f16::from_f32(2.0)),
3975            Some(f16::from_f32(10.6)),
3976            None,
3977        ]);
3978        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3979
3980        assert_eq!(&actual, &expected);
3981
3982        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
3983        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3984
3985        assert_eq!(&actual, &expected);
3986
3987        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
3988        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3989
3990        assert_eq!(&actual, &expected);
3991    }
3992
3993    #[test]
3994    fn test_cast_timestamp_to_floating() {
3995        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
3996            .with_timezone("UTC".to_string());
3997        let expected = cast(&array, &DataType::Int64).unwrap();
3998
3999        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4000        assert_eq!(&actual, &expected);
4001
4002        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4003        assert_eq!(&actual, &expected);
4004
4005        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4006        assert_eq!(&actual, &expected);
4007    }
4008
4009    #[test]
4010    fn test_cast_decimal_to_timestamp() {
4011        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4012        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4013
4014        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4015            .with_precision_and_scale(4, 2)
4016            .unwrap();
4017        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4018
4019        assert_eq!(&actual, &expected);
4020
4021        let array = Decimal256Array::from(vec![
4022            Some(i256::from_i128(2000)),
4023            Some(i256::from_i128(10000)),
4024            None,
4025        ])
4026        .with_precision_and_scale(5, 3)
4027        .unwrap();
4028        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4029
4030        assert_eq!(&actual, &expected);
4031    }
4032
4033    #[test]
4034    fn test_cast_timestamp_to_decimal() {
4035        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4036            .with_timezone("UTC".to_string());
4037        let expected = cast(&array, &DataType::Int64).unwrap();
4038
4039        let actual = cast(
4040            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4041            &DataType::Int64,
4042        )
4043        .unwrap();
4044        assert_eq!(&actual, &expected);
4045
4046        let actual = cast(
4047            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4048            &DataType::Int64,
4049        )
4050        .unwrap();
4051        assert_eq!(&actual, &expected);
4052    }
4053
4054    #[test]
4055    fn test_cast_list_i32_to_list_u16() {
4056        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 100000000]).into_data();
4057
4058        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
4059
4060        // Construct a list array from the above two
4061        // [[0,0,0], [-1, -2, -1], [2, 100000000]]
4062        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4063        let list_data = ArrayData::builder(list_data_type)
4064            .len(3)
4065            .add_buffer(value_offsets)
4066            .add_child_data(value_data)
4067            .build()
4068            .unwrap();
4069        let list_array = ListArray::from(list_data);
4070
4071        let cast_array = cast(
4072            &list_array,
4073            &DataType::List(Arc::new(Field::new_list_field(DataType::UInt16, true))),
4074        )
4075        .unwrap();
4076
4077        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4078        //
4079        // 3 negative values should get lost when casting to unsigned,
4080        // 1 value should overflow
4081        assert_eq!(0, cast_array.null_count());
4082
4083        // offsets should be the same
4084        let array = cast_array.as_list::<i32>();
4085        assert_eq!(list_array.value_offsets(), array.value_offsets());
4086
4087        assert_eq!(DataType::UInt16, array.value_type());
4088        assert_eq!(3, array.value_length(0));
4089        assert_eq!(3, array.value_length(1));
4090        assert_eq!(2, array.value_length(2));
4091
4092        // expect 4 nulls: negative numbers and overflow
4093        let u16arr = array.values().as_primitive::<UInt16Type>();
4094        assert_eq!(4, u16arr.null_count());
4095
4096        // expect 4 nulls: negative numbers and overflow
4097        let expected: UInt16Array =
4098            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4099                .into_iter()
4100                .collect();
4101
4102        assert_eq!(u16arr, &expected);
4103    }
4104
4105    #[test]
4106    fn test_cast_list_i32_to_list_timestamp() {
4107        // Construct a value array
4108        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4109
4110        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4111
4112        // Construct a list array from the above two
4113        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4114        let list_data = ArrayData::builder(list_data_type)
4115            .len(3)
4116            .add_buffer(value_offsets)
4117            .add_child_data(value_data)
4118            .build()
4119            .unwrap();
4120        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4121
4122        let actual = cast(
4123            &list_array,
4124            &DataType::List(Arc::new(Field::new_list_field(
4125                DataType::Timestamp(TimeUnit::Microsecond, None),
4126                true,
4127            ))),
4128        )
4129        .unwrap();
4130
4131        let expected = cast(
4132            &cast(
4133                &list_array,
4134                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4135            )
4136            .unwrap(),
4137            &DataType::List(Arc::new(Field::new_list_field(
4138                DataType::Timestamp(TimeUnit::Microsecond, None),
4139                true,
4140            ))),
4141        )
4142        .unwrap();
4143
4144        assert_eq!(&actual, &expected);
4145    }
4146
4147    #[test]
4148    fn test_cast_date32_to_date64() {
4149        let a = Date32Array::from(vec![10000, 17890]);
4150        let array = Arc::new(a) as ArrayRef;
4151        let b = cast(&array, &DataType::Date64).unwrap();
4152        let c = b.as_primitive::<Date64Type>();
4153        assert_eq!(864000000000, c.value(0));
4154        assert_eq!(1545696000000, c.value(1));
4155    }
4156
4157    #[test]
4158    fn test_cast_date64_to_date32() {
4159        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4160        let array = Arc::new(a) as ArrayRef;
4161        let b = cast(&array, &DataType::Date32).unwrap();
4162        let c = b.as_primitive::<Date32Type>();
4163        assert_eq!(10000, c.value(0));
4164        assert_eq!(17890, c.value(1));
4165        assert!(c.is_null(2));
4166    }
4167
4168    #[test]
4169    fn test_cast_string_to_integral_overflow() {
4170        let str = Arc::new(StringArray::from(vec![
4171            Some("123"),
4172            Some("-123"),
4173            Some("86374"),
4174            None,
4175        ])) as ArrayRef;
4176
4177        let options = CastOptions {
4178            safe: true,
4179            format_options: FormatOptions::default(),
4180        };
4181        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4182        let expected =
4183            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4184        assert_eq!(&res, &expected);
4185    }
4186
4187    #[test]
4188    fn test_cast_string_to_timestamp() {
4189        let a0 = Arc::new(StringViewArray::from(vec![
4190            Some("2020-09-08T12:00:00.123456789+00:00"),
4191            Some("Not a valid date"),
4192            None,
4193        ])) as ArrayRef;
4194        let a1 = Arc::new(StringArray::from(vec![
4195            Some("2020-09-08T12:00:00.123456789+00:00"),
4196            Some("Not a valid date"),
4197            None,
4198        ])) as ArrayRef;
4199        let a2 = Arc::new(LargeStringArray::from(vec![
4200            Some("2020-09-08T12:00:00.123456789+00:00"),
4201            Some("Not a valid date"),
4202            None,
4203        ])) as ArrayRef;
4204        for array in &[a0, a1, a2] {
4205            for time_unit in &[
4206                TimeUnit::Second,
4207                TimeUnit::Millisecond,
4208                TimeUnit::Microsecond,
4209                TimeUnit::Nanosecond,
4210            ] {
4211                let to_type = DataType::Timestamp(*time_unit, None);
4212                let b = cast(array, &to_type).unwrap();
4213
4214                match time_unit {
4215                    TimeUnit::Second => {
4216                        let c = b.as_primitive::<TimestampSecondType>();
4217                        assert_eq!(1599566400, c.value(0));
4218                        assert!(c.is_null(1));
4219                        assert!(c.is_null(2));
4220                    }
4221                    TimeUnit::Millisecond => {
4222                        let c = b
4223                            .as_any()
4224                            .downcast_ref::<TimestampMillisecondArray>()
4225                            .unwrap();
4226                        assert_eq!(1599566400123, c.value(0));
4227                        assert!(c.is_null(1));
4228                        assert!(c.is_null(2));
4229                    }
4230                    TimeUnit::Microsecond => {
4231                        let c = b
4232                            .as_any()
4233                            .downcast_ref::<TimestampMicrosecondArray>()
4234                            .unwrap();
4235                        assert_eq!(1599566400123456, c.value(0));
4236                        assert!(c.is_null(1));
4237                        assert!(c.is_null(2));
4238                    }
4239                    TimeUnit::Nanosecond => {
4240                        let c = b
4241                            .as_any()
4242                            .downcast_ref::<TimestampNanosecondArray>()
4243                            .unwrap();
4244                        assert_eq!(1599566400123456789, c.value(0));
4245                        assert!(c.is_null(1));
4246                        assert!(c.is_null(2));
4247                    }
4248                }
4249
4250                let options = CastOptions {
4251                    safe: false,
4252                    format_options: FormatOptions::default(),
4253                };
4254                let err = cast_with_options(array, &to_type, &options).unwrap_err();
4255                assert_eq!(
4256                    err.to_string(),
4257                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4258                );
4259            }
4260        }
4261    }
4262
4263    #[test]
4264    fn test_cast_string_to_timestamp_overflow() {
4265        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4266        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4267        let result = result.as_primitive::<TimestampSecondType>();
4268        assert_eq!(result.values(), &[247112596800]);
4269    }
4270
4271    #[test]
4272    fn test_cast_string_to_date32() {
4273        let a0 = Arc::new(StringViewArray::from(vec![
4274            Some("2018-12-25"),
4275            Some("Not a valid date"),
4276            None,
4277        ])) as ArrayRef;
4278        let a1 = Arc::new(StringArray::from(vec![
4279            Some("2018-12-25"),
4280            Some("Not a valid date"),
4281            None,
4282        ])) as ArrayRef;
4283        let a2 = Arc::new(LargeStringArray::from(vec![
4284            Some("2018-12-25"),
4285            Some("Not a valid date"),
4286            None,
4287        ])) as ArrayRef;
4288        for array in &[a0, a1, a2] {
4289            let to_type = DataType::Date32;
4290            let b = cast(array, &to_type).unwrap();
4291            let c = b.as_primitive::<Date32Type>();
4292            assert_eq!(17890, c.value(0));
4293            assert!(c.is_null(1));
4294            assert!(c.is_null(2));
4295
4296            let options = CastOptions {
4297                safe: false,
4298                format_options: FormatOptions::default(),
4299            };
4300            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4301            assert_eq!(
4302                err.to_string(),
4303                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
4304            );
4305        }
4306    }
4307
4308    #[test]
4309    fn test_cast_string_with_large_date_to_date32() {
4310        let array = Arc::new(StringArray::from(vec![
4311            Some("+10999-12-31"),
4312            Some("-0010-02-28"),
4313            Some("0010-02-28"),
4314            Some("0000-01-01"),
4315            Some("-0000-01-01"),
4316            Some("-0001-01-01"),
4317        ])) as ArrayRef;
4318        let to_type = DataType::Date32;
4319        let options = CastOptions {
4320            safe: false,
4321            format_options: FormatOptions::default(),
4322        };
4323        let b = cast_with_options(&array, &to_type, &options).unwrap();
4324        let c = b.as_primitive::<Date32Type>();
4325        assert_eq!(3298139, c.value(0)); // 10999-12-31
4326        assert_eq!(-723122, c.value(1)); // -0010-02-28
4327        assert_eq!(-715817, c.value(2)); // 0010-02-28
4328        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
4329        assert_eq!(-719528, c.value(3)); // 0000-01-01
4330        assert_eq!(-719528, c.value(4)); // -0000-01-01
4331        assert_eq!(-719893, c.value(5)); // -0001-01-01
4332    }
4333
4334    #[test]
4335    fn test_cast_invalid_string_with_large_date_to_date32() {
4336        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
4337        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
4338        let to_type = DataType::Date32;
4339        let options = CastOptions {
4340            safe: false,
4341            format_options: FormatOptions::default(),
4342        };
4343        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
4344        assert_eq!(
4345            err.to_string(),
4346            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
4347        );
4348    }
4349
4350    #[test]
4351    fn test_cast_string_format_yyyymmdd_to_date32() {
4352        let a0 = Arc::new(StringViewArray::from(vec![
4353            Some("2020-12-25"),
4354            Some("20201117"),
4355        ])) as ArrayRef;
4356        let a1 = Arc::new(StringArray::from(vec![
4357            Some("2020-12-25"),
4358            Some("20201117"),
4359        ])) as ArrayRef;
4360        let a2 = Arc::new(LargeStringArray::from(vec![
4361            Some("2020-12-25"),
4362            Some("20201117"),
4363        ])) as ArrayRef;
4364
4365        for array in &[a0, a1, a2] {
4366            let to_type = DataType::Date32;
4367            let options = CastOptions {
4368                safe: false,
4369                format_options: FormatOptions::default(),
4370            };
4371            let result = cast_with_options(&array, &to_type, &options).unwrap();
4372            let c = result.as_primitive::<Date32Type>();
4373            assert_eq!(
4374                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
4375                c.value_as_date(0)
4376            );
4377            assert_eq!(
4378                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
4379                c.value_as_date(1)
4380            );
4381        }
4382    }
4383
4384    #[test]
4385    fn test_cast_string_to_time32second() {
4386        let a0 = Arc::new(StringViewArray::from(vec![
4387            Some("08:08:35.091323414"),
4388            Some("08:08:60.091323414"), // leap second
4389            Some("08:08:61.091323414"), // not valid
4390            Some("Not a valid time"),
4391            None,
4392        ])) as ArrayRef;
4393        let a1 = Arc::new(StringArray::from(vec![
4394            Some("08:08:35.091323414"),
4395            Some("08:08:60.091323414"), // leap second
4396            Some("08:08:61.091323414"), // not valid
4397            Some("Not a valid time"),
4398            None,
4399        ])) as ArrayRef;
4400        let a2 = Arc::new(LargeStringArray::from(vec![
4401            Some("08:08:35.091323414"),
4402            Some("08:08:60.091323414"), // leap second
4403            Some("08:08:61.091323414"), // not valid
4404            Some("Not a valid time"),
4405            None,
4406        ])) as ArrayRef;
4407        for array in &[a0, a1, a2] {
4408            let to_type = DataType::Time32(TimeUnit::Second);
4409            let b = cast(array, &to_type).unwrap();
4410            let c = b.as_primitive::<Time32SecondType>();
4411            assert_eq!(29315, c.value(0));
4412            assert_eq!(29340, c.value(1));
4413            assert!(c.is_null(2));
4414            assert!(c.is_null(3));
4415            assert!(c.is_null(4));
4416
4417            let options = CastOptions {
4418                safe: false,
4419                format_options: FormatOptions::default(),
4420            };
4421            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4422            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Second) type");
4423        }
4424    }
4425
4426    #[test]
4427    fn test_cast_string_to_time32millisecond() {
4428        let a0 = Arc::new(StringViewArray::from(vec![
4429            Some("08:08:35.091323414"),
4430            Some("08:08:60.091323414"), // leap second
4431            Some("08:08:61.091323414"), // not valid
4432            Some("Not a valid time"),
4433            None,
4434        ])) as ArrayRef;
4435        let a1 = Arc::new(StringArray::from(vec![
4436            Some("08:08:35.091323414"),
4437            Some("08:08:60.091323414"), // leap second
4438            Some("08:08:61.091323414"), // not valid
4439            Some("Not a valid time"),
4440            None,
4441        ])) as ArrayRef;
4442        let a2 = Arc::new(LargeStringArray::from(vec![
4443            Some("08:08:35.091323414"),
4444            Some("08:08:60.091323414"), // leap second
4445            Some("08:08:61.091323414"), // not valid
4446            Some("Not a valid time"),
4447            None,
4448        ])) as ArrayRef;
4449        for array in &[a0, a1, a2] {
4450            let to_type = DataType::Time32(TimeUnit::Millisecond);
4451            let b = cast(array, &to_type).unwrap();
4452            let c = b.as_primitive::<Time32MillisecondType>();
4453            assert_eq!(29315091, c.value(0));
4454            assert_eq!(29340091, c.value(1));
4455            assert!(c.is_null(2));
4456            assert!(c.is_null(3));
4457            assert!(c.is_null(4));
4458
4459            let options = CastOptions {
4460                safe: false,
4461                format_options: FormatOptions::default(),
4462            };
4463            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4464            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Millisecond) type");
4465        }
4466    }
4467
4468    #[test]
4469    fn test_cast_string_to_time64microsecond() {
4470        let a0 = Arc::new(StringViewArray::from(vec![
4471            Some("08:08:35.091323414"),
4472            Some("Not a valid time"),
4473            None,
4474        ])) as ArrayRef;
4475        let a1 = Arc::new(StringArray::from(vec![
4476            Some("08:08:35.091323414"),
4477            Some("Not a valid time"),
4478            None,
4479        ])) as ArrayRef;
4480        let a2 = Arc::new(LargeStringArray::from(vec![
4481            Some("08:08:35.091323414"),
4482            Some("Not a valid time"),
4483            None,
4484        ])) as ArrayRef;
4485        for array in &[a0, a1, a2] {
4486            let to_type = DataType::Time64(TimeUnit::Microsecond);
4487            let b = cast(array, &to_type).unwrap();
4488            let c = b.as_primitive::<Time64MicrosecondType>();
4489            assert_eq!(29315091323, c.value(0));
4490            assert!(c.is_null(1));
4491            assert!(c.is_null(2));
4492
4493            let options = CastOptions {
4494                safe: false,
4495                format_options: FormatOptions::default(),
4496            };
4497            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4498            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Microsecond) type");
4499        }
4500    }
4501
4502    #[test]
4503    fn test_cast_string_to_time64nanosecond() {
4504        let a0 = Arc::new(StringViewArray::from(vec![
4505            Some("08:08:35.091323414"),
4506            Some("Not a valid time"),
4507            None,
4508        ])) as ArrayRef;
4509        let a1 = Arc::new(StringArray::from(vec![
4510            Some("08:08:35.091323414"),
4511            Some("Not a valid time"),
4512            None,
4513        ])) as ArrayRef;
4514        let a2 = Arc::new(LargeStringArray::from(vec![
4515            Some("08:08:35.091323414"),
4516            Some("Not a valid time"),
4517            None,
4518        ])) as ArrayRef;
4519        for array in &[a0, a1, a2] {
4520            let to_type = DataType::Time64(TimeUnit::Nanosecond);
4521            let b = cast(array, &to_type).unwrap();
4522            let c = b.as_primitive::<Time64NanosecondType>();
4523            assert_eq!(29315091323414, c.value(0));
4524            assert!(c.is_null(1));
4525            assert!(c.is_null(2));
4526
4527            let options = CastOptions {
4528                safe: false,
4529                format_options: FormatOptions::default(),
4530            };
4531            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4532            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Nanosecond) type");
4533        }
4534    }
4535
4536    #[test]
4537    fn test_cast_string_to_date64() {
4538        let a0 = Arc::new(StringViewArray::from(vec![
4539            Some("2020-09-08T12:00:00"),
4540            Some("Not a valid date"),
4541            None,
4542        ])) as ArrayRef;
4543        let a1 = Arc::new(StringArray::from(vec![
4544            Some("2020-09-08T12:00:00"),
4545            Some("Not a valid date"),
4546            None,
4547        ])) as ArrayRef;
4548        let a2 = Arc::new(LargeStringArray::from(vec![
4549            Some("2020-09-08T12:00:00"),
4550            Some("Not a valid date"),
4551            None,
4552        ])) as ArrayRef;
4553        for array in &[a0, a1, a2] {
4554            let to_type = DataType::Date64;
4555            let b = cast(array, &to_type).unwrap();
4556            let c = b.as_primitive::<Date64Type>();
4557            assert_eq!(1599566400000, c.value(0));
4558            assert!(c.is_null(1));
4559            assert!(c.is_null(2));
4560
4561            let options = CastOptions {
4562                safe: false,
4563                format_options: FormatOptions::default(),
4564            };
4565            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4566            assert_eq!(
4567                err.to_string(),
4568                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
4569            );
4570        }
4571    }
4572
4573    macro_rules! test_safe_string_to_interval {
4574        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
4575            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
4576
4577            let options = CastOptions {
4578                safe: true,
4579                format_options: FormatOptions::default(),
4580            };
4581
4582            let target_interval_array = cast_with_options(
4583                &source_string_array.clone(),
4584                &DataType::Interval($interval_unit),
4585                &options,
4586            )
4587            .unwrap()
4588            .as_any()
4589            .downcast_ref::<$array_ty>()
4590            .unwrap()
4591            .clone() as $array_ty;
4592
4593            let target_string_array =
4594                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
4595                    .unwrap()
4596                    .as_any()
4597                    .downcast_ref::<StringArray>()
4598                    .unwrap()
4599                    .clone();
4600
4601            let expect_string_array = StringArray::from($expect_vec);
4602
4603            assert_eq!(target_string_array, expect_string_array);
4604
4605            let target_large_string_array =
4606                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
4607                    .unwrap()
4608                    .as_any()
4609                    .downcast_ref::<LargeStringArray>()
4610                    .unwrap()
4611                    .clone();
4612
4613            let expect_large_string_array = LargeStringArray::from($expect_vec);
4614
4615            assert_eq!(target_large_string_array, expect_large_string_array);
4616        };
4617    }
4618
4619    #[test]
4620    fn test_cast_string_to_interval_year_month() {
4621        test_safe_string_to_interval!(
4622            vec![
4623                Some("1 year 1 month"),
4624                Some("1.5 years 13 month"),
4625                Some("30 days"),
4626                Some("31 days"),
4627                Some("2 months 31 days"),
4628                Some("2 months 31 days 1 second"),
4629                Some("foobar"),
4630            ],
4631            IntervalUnit::YearMonth,
4632            IntervalYearMonthArray,
4633            vec![
4634                Some("1 years 1 mons"),
4635                Some("2 years 7 mons"),
4636                None,
4637                None,
4638                None,
4639                None,
4640                None,
4641            ]
4642        );
4643    }
4644
4645    #[test]
4646    fn test_cast_string_to_interval_day_time() {
4647        test_safe_string_to_interval!(
4648            vec![
4649                Some("1 year 1 month"),
4650                Some("1.5 years 13 month"),
4651                Some("30 days"),
4652                Some("1 day 2 second 3.5 milliseconds"),
4653                Some("foobar"),
4654            ],
4655            IntervalUnit::DayTime,
4656            IntervalDayTimeArray,
4657            vec![
4658                Some("390 days"),
4659                Some("930 days"),
4660                Some("30 days"),
4661                None,
4662                None,
4663            ]
4664        );
4665    }
4666
4667    #[test]
4668    fn test_cast_string_to_interval_month_day_nano() {
4669        test_safe_string_to_interval!(
4670            vec![
4671                Some("1 year 1 month 1 day"),
4672                None,
4673                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
4674                Some("3 days"),
4675                Some("8 seconds"),
4676                None,
4677                Some("1 day 29800 milliseconds"),
4678                Some("3 months 1 second"),
4679                Some("6 minutes 120 second"),
4680                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
4681                Some("foobar"),
4682            ],
4683            IntervalUnit::MonthDayNano,
4684            IntervalMonthDayNanoArray,
4685            vec![
4686                Some("13 mons 1 days"),
4687                None,
4688                Some("31 mons 35 days 0.001400000 secs"),
4689                Some("3 days"),
4690                Some("8.000000000 secs"),
4691                None,
4692                Some("1 days 29.800000000 secs"),
4693                Some("3 mons 1.000000000 secs"),
4694                Some("8 mins"),
4695                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
4696                None,
4697            ]
4698        );
4699    }
4700
4701    macro_rules! test_unsafe_string_to_interval_err {
4702        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
4703            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
4704            let options = CastOptions {
4705                safe: false,
4706                format_options: FormatOptions::default(),
4707            };
4708            let arrow_err = cast_with_options(
4709                &string_array.clone(),
4710                &DataType::Interval($interval_unit),
4711                &options,
4712            )
4713            .unwrap_err();
4714            assert_eq!($error_msg, arrow_err.to_string());
4715        };
4716    }
4717
4718    #[test]
4719    fn test_cast_string_to_interval_err() {
4720        test_unsafe_string_to_interval_err!(
4721            vec![Some("foobar")],
4722            IntervalUnit::YearMonth,
4723            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4724        );
4725        test_unsafe_string_to_interval_err!(
4726            vec![Some("foobar")],
4727            IntervalUnit::DayTime,
4728            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4729        );
4730        test_unsafe_string_to_interval_err!(
4731            vec![Some("foobar")],
4732            IntervalUnit::MonthDayNano,
4733            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4734        );
4735        test_unsafe_string_to_interval_err!(
4736            vec![Some("2 months 31 days 1 second")],
4737            IntervalUnit::YearMonth,
4738            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
4739        );
4740        test_unsafe_string_to_interval_err!(
4741            vec![Some("1 day 1.5 milliseconds")],
4742            IntervalUnit::DayTime,
4743            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
4744        );
4745
4746        // overflow
4747        test_unsafe_string_to_interval_err!(
4748            vec![Some(format!(
4749                "{} century {} year {} month",
4750                i64::MAX - 2,
4751                i64::MAX - 2,
4752                i64::MAX - 2
4753            ))],
4754            IntervalUnit::DayTime,
4755            format!(
4756                "Arithmetic overflow: Overflow happened on: {} * 100",
4757                i64::MAX - 2
4758            )
4759        );
4760        test_unsafe_string_to_interval_err!(
4761            vec![Some(format!(
4762                "{} year {} month {} day",
4763                i64::MAX - 2,
4764                i64::MAX - 2,
4765                i64::MAX - 2
4766            ))],
4767            IntervalUnit::MonthDayNano,
4768            format!(
4769                "Arithmetic overflow: Overflow happened on: {} * 12",
4770                i64::MAX - 2
4771            )
4772        );
4773    }
4774
4775    #[test]
4776    fn test_cast_binary_to_fixed_size_binary() {
4777        let bytes_1 = "Hiiii".as_bytes();
4778        let bytes_2 = "Hello".as_bytes();
4779
4780        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4781        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
4782        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
4783
4784        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
4785        let down_cast = array_ref
4786            .as_any()
4787            .downcast_ref::<FixedSizeBinaryArray>()
4788            .unwrap();
4789        assert_eq!(bytes_1, down_cast.value(0));
4790        assert_eq!(bytes_2, down_cast.value(1));
4791        assert!(down_cast.is_null(2));
4792
4793        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
4794        let down_cast = array_ref
4795            .as_any()
4796            .downcast_ref::<FixedSizeBinaryArray>()
4797            .unwrap();
4798        assert_eq!(bytes_1, down_cast.value(0));
4799        assert_eq!(bytes_2, down_cast.value(1));
4800        assert!(down_cast.is_null(2));
4801
4802        // test error cases when the length of binary are not same
4803        let bytes_1 = "Hi".as_bytes();
4804        let bytes_2 = "Hello".as_bytes();
4805
4806        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4807        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
4808        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
4809
4810        let array_ref = cast_with_options(
4811            &a1,
4812            &DataType::FixedSizeBinary(5),
4813            &CastOptions {
4814                safe: false,
4815                format_options: FormatOptions::default(),
4816            },
4817        );
4818        assert!(array_ref.is_err());
4819
4820        let array_ref = cast_with_options(
4821            &a2,
4822            &DataType::FixedSizeBinary(5),
4823            &CastOptions {
4824                safe: false,
4825                format_options: FormatOptions::default(),
4826            },
4827        );
4828        assert!(array_ref.is_err());
4829    }
4830
4831    #[test]
4832    fn test_fixed_size_binary_to_binary() {
4833        let bytes_1 = "Hiiii".as_bytes();
4834        let bytes_2 = "Hello".as_bytes();
4835
4836        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4837        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
4838
4839        let array_ref = cast(&a1, &DataType::Binary).unwrap();
4840        let down_cast = array_ref.as_binary::<i32>();
4841        assert_eq!(bytes_1, down_cast.value(0));
4842        assert_eq!(bytes_2, down_cast.value(1));
4843        assert!(down_cast.is_null(2));
4844
4845        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
4846        let down_cast = array_ref.as_binary::<i64>();
4847        assert_eq!(bytes_1, down_cast.value(0));
4848        assert_eq!(bytes_2, down_cast.value(1));
4849        assert!(down_cast.is_null(2));
4850    }
4851
4852    #[test]
4853    fn test_numeric_to_binary() {
4854        let a = Int16Array::from(vec![Some(1), Some(511), None]);
4855
4856        let array_ref = cast(&a, &DataType::Binary).unwrap();
4857        let down_cast = array_ref.as_binary::<i32>();
4858        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
4859        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
4860        assert!(down_cast.is_null(2));
4861
4862        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
4863
4864        let array_ref = cast(&a, &DataType::Binary).unwrap();
4865        let down_cast = array_ref.as_binary::<i32>();
4866        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
4867        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
4868        assert!(down_cast.is_null(2));
4869    }
4870
4871    #[test]
4872    fn test_numeric_to_large_binary() {
4873        let a = Int16Array::from(vec![Some(1), Some(511), None]);
4874
4875        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
4876        let down_cast = array_ref.as_binary::<i64>();
4877        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
4878        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
4879        assert!(down_cast.is_null(2));
4880
4881        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
4882
4883        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
4884        let down_cast = array_ref.as_binary::<i64>();
4885        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
4886        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
4887        assert!(down_cast.is_null(2));
4888    }
4889
4890    #[test]
4891    fn test_cast_date32_to_int32() {
4892        let array = Date32Array::from(vec![10000, 17890]);
4893        let b = cast(&array, &DataType::Int32).unwrap();
4894        let c = b.as_primitive::<Int32Type>();
4895        assert_eq!(10000, c.value(0));
4896        assert_eq!(17890, c.value(1));
4897    }
4898
4899    #[test]
4900    fn test_cast_int32_to_date32() {
4901        let array = Int32Array::from(vec![10000, 17890]);
4902        let b = cast(&array, &DataType::Date32).unwrap();
4903        let c = b.as_primitive::<Date32Type>();
4904        assert_eq!(10000, c.value(0));
4905        assert_eq!(17890, c.value(1));
4906    }
4907
4908    #[test]
4909    fn test_cast_timestamp_to_date32() {
4910        let array =
4911            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
4912                .with_timezone("+00:00".to_string());
4913        let b = cast(&array, &DataType::Date32).unwrap();
4914        let c = b.as_primitive::<Date32Type>();
4915        assert_eq!(10000, c.value(0));
4916        assert_eq!(17890, c.value(1));
4917        assert!(c.is_null(2));
4918    }
4919    #[test]
4920    fn test_cast_timestamp_to_date32_zone() {
4921        let strings = StringArray::from_iter([
4922            Some("1970-01-01T00:00:01"),
4923            Some("1970-01-01T23:59:59"),
4924            None,
4925            Some("2020-03-01T02:00:23+00:00"),
4926        ]);
4927        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
4928        let timestamps = cast(&strings, &dt).unwrap();
4929        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
4930
4931        let c = dates.as_primitive::<Date32Type>();
4932        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
4933        assert_eq!(c.value_as_date(0).unwrap(), expected);
4934        assert_eq!(c.value_as_date(1).unwrap(), expected);
4935        assert!(c.is_null(2));
4936        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
4937        assert_eq!(c.value_as_date(3).unwrap(), expected);
4938    }
4939    #[test]
4940    fn test_cast_timestamp_to_date64() {
4941        let array =
4942            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
4943        let b = cast(&array, &DataType::Date64).unwrap();
4944        let c = b.as_primitive::<Date64Type>();
4945        assert_eq!(864000000005, c.value(0));
4946        assert_eq!(1545696000001, c.value(1));
4947        assert!(c.is_null(2));
4948
4949        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
4950        let b = cast(&array, &DataType::Date64).unwrap();
4951        let c = b.as_primitive::<Date64Type>();
4952        assert_eq!(864000000005000, c.value(0));
4953        assert_eq!(1545696000001000, c.value(1));
4954
4955        // test overflow, safe cast
4956        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
4957        let b = cast(&array, &DataType::Date64).unwrap();
4958        assert!(b.is_null(0));
4959        // test overflow, unsafe cast
4960        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
4961        let options = CastOptions {
4962            safe: false,
4963            format_options: FormatOptions::default(),
4964        };
4965        let b = cast_with_options(&array, &DataType::Date64, &options);
4966        assert!(b.is_err());
4967    }
4968
4969    #[test]
4970    fn test_cast_timestamp_to_time64() {
4971        // test timestamp secs
4972        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
4973            .with_timezone("+01:00".to_string());
4974        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
4975        let c = b.as_primitive::<Time64MicrosecondType>();
4976        assert_eq!(3605000000, c.value(0));
4977        assert_eq!(3601000000, c.value(1));
4978        assert!(c.is_null(2));
4979        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
4980        let c = b.as_primitive::<Time64NanosecondType>();
4981        assert_eq!(3605000000000, c.value(0));
4982        assert_eq!(3601000000000, c.value(1));
4983        assert!(c.is_null(2));
4984
4985        // test timestamp milliseconds
4986        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
4987            .with_timezone("+01:00".to_string());
4988        let array = Arc::new(a) as ArrayRef;
4989        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
4990        let c = b.as_primitive::<Time64MicrosecondType>();
4991        assert_eq!(3605000000, c.value(0));
4992        assert_eq!(3601000000, c.value(1));
4993        assert!(c.is_null(2));
4994        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
4995        let c = b.as_primitive::<Time64NanosecondType>();
4996        assert_eq!(3605000000000, c.value(0));
4997        assert_eq!(3601000000000, c.value(1));
4998        assert!(c.is_null(2));
4999
5000        // test timestamp microseconds
5001        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5002            .with_timezone("+01:00".to_string());
5003        let array = Arc::new(a) as ArrayRef;
5004        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5005        let c = b.as_primitive::<Time64MicrosecondType>();
5006        assert_eq!(3605000000, c.value(0));
5007        assert_eq!(3601000000, c.value(1));
5008        assert!(c.is_null(2));
5009        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5010        let c = b.as_primitive::<Time64NanosecondType>();
5011        assert_eq!(3605000000000, c.value(0));
5012        assert_eq!(3601000000000, c.value(1));
5013        assert!(c.is_null(2));
5014
5015        // test timestamp nanoseconds
5016        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5017            .with_timezone("+01:00".to_string());
5018        let array = Arc::new(a) as ArrayRef;
5019        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5020        let c = b.as_primitive::<Time64MicrosecondType>();
5021        assert_eq!(3605000000, c.value(0));
5022        assert_eq!(3601000000, c.value(1));
5023        assert!(c.is_null(2));
5024        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5025        let c = b.as_primitive::<Time64NanosecondType>();
5026        assert_eq!(3605000000000, c.value(0));
5027        assert_eq!(3601000000000, c.value(1));
5028        assert!(c.is_null(2));
5029
5030        // test overflow
5031        let a =
5032            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5033        let array = Arc::new(a) as ArrayRef;
5034        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5035        assert!(b.is_err());
5036        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5037        assert!(b.is_err());
5038        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5039        assert!(b.is_err());
5040    }
5041
5042    #[test]
5043    fn test_cast_timestamp_to_time32() {
5044        // test timestamp secs
5045        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5046            .with_timezone("+01:00".to_string());
5047        let array = Arc::new(a) as ArrayRef;
5048        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5049        let c = b.as_primitive::<Time32SecondType>();
5050        assert_eq!(3605, c.value(0));
5051        assert_eq!(3601, c.value(1));
5052        assert!(c.is_null(2));
5053        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5054        let c = b.as_primitive::<Time32MillisecondType>();
5055        assert_eq!(3605000, c.value(0));
5056        assert_eq!(3601000, c.value(1));
5057        assert!(c.is_null(2));
5058
5059        // test timestamp milliseconds
5060        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5061            .with_timezone("+01:00".to_string());
5062        let array = Arc::new(a) as ArrayRef;
5063        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5064        let c = b.as_primitive::<Time32SecondType>();
5065        assert_eq!(3605, c.value(0));
5066        assert_eq!(3601, c.value(1));
5067        assert!(c.is_null(2));
5068        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5069        let c = b.as_primitive::<Time32MillisecondType>();
5070        assert_eq!(3605000, c.value(0));
5071        assert_eq!(3601000, c.value(1));
5072        assert!(c.is_null(2));
5073
5074        // test timestamp microseconds
5075        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5076            .with_timezone("+01:00".to_string());
5077        let array = Arc::new(a) as ArrayRef;
5078        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5079        let c = b.as_primitive::<Time32SecondType>();
5080        assert_eq!(3605, c.value(0));
5081        assert_eq!(3601, c.value(1));
5082        assert!(c.is_null(2));
5083        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5084        let c = b.as_primitive::<Time32MillisecondType>();
5085        assert_eq!(3605000, c.value(0));
5086        assert_eq!(3601000, c.value(1));
5087        assert!(c.is_null(2));
5088
5089        // test timestamp nanoseconds
5090        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5091            .with_timezone("+01:00".to_string());
5092        let array = Arc::new(a) as ArrayRef;
5093        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5094        let c = b.as_primitive::<Time32SecondType>();
5095        assert_eq!(3605, c.value(0));
5096        assert_eq!(3601, c.value(1));
5097        assert!(c.is_null(2));
5098        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5099        let c = b.as_primitive::<Time32MillisecondType>();
5100        assert_eq!(3605000, c.value(0));
5101        assert_eq!(3601000, c.value(1));
5102        assert!(c.is_null(2));
5103
5104        // test overflow
5105        let a =
5106            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5107        let array = Arc::new(a) as ArrayRef;
5108        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5109        assert!(b.is_err());
5110        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5111        assert!(b.is_err());
5112    }
5113
5114    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
5115    #[test]
5116    fn test_cast_timestamp_with_timezone_1() {
5117        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5118            Some("2000-01-01T00:00:00.123456789"),
5119            Some("2010-01-01T00:00:00.123456789"),
5120            None,
5121        ]));
5122        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5123        let timestamp_array = cast(&string_array, &to_type).unwrap();
5124
5125        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5126        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5127
5128        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5129        let result = string_array.as_string::<i32>();
5130        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
5131        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5132        assert!(result.is_null(2));
5133    }
5134
5135    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
5136    #[test]
5137    fn test_cast_timestamp_with_timezone_2() {
5138        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5139            Some("2000-01-01T07:00:00.123456789"),
5140            Some("2010-01-01T07:00:00.123456789"),
5141            None,
5142        ]));
5143        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
5144        let timestamp_array = cast(&string_array, &to_type).unwrap();
5145
5146        // Check intermediate representation is correct
5147        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5148        let result = string_array.as_string::<i32>();
5149        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
5150        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5151        assert!(result.is_null(2));
5152
5153        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5154        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5155
5156        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5157        let result = string_array.as_string::<i32>();
5158        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
5159        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5160        assert!(result.is_null(2));
5161    }
5162
5163    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
5164    #[test]
5165    fn test_cast_timestamp_with_timezone_3() {
5166        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5167            Some("2000-01-01T07:00:00.123456789"),
5168            Some("2010-01-01T07:00:00.123456789"),
5169            None,
5170        ]));
5171        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5172        let timestamp_array = cast(&string_array, &to_type).unwrap();
5173
5174        // Check intermediate representation is correct
5175        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5176        let result = string_array.as_string::<i32>();
5177        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5178        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5179        assert!(result.is_null(2));
5180
5181        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5182        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5183
5184        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5185        let result = string_array.as_string::<i32>();
5186        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5187        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5188        assert!(result.is_null(2));
5189    }
5190
5191    #[test]
5192    fn test_cast_date64_to_timestamp() {
5193        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5194        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5195        let c = b.as_primitive::<TimestampSecondType>();
5196        assert_eq!(864000000, c.value(0));
5197        assert_eq!(1545696000, c.value(1));
5198        assert!(c.is_null(2));
5199    }
5200
5201    #[test]
5202    fn test_cast_date64_to_timestamp_ms() {
5203        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5204        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
5205        let c = b
5206            .as_any()
5207            .downcast_ref::<TimestampMillisecondArray>()
5208            .unwrap();
5209        assert_eq!(864000000005, c.value(0));
5210        assert_eq!(1545696000001, c.value(1));
5211        assert!(c.is_null(2));
5212    }
5213
5214    #[test]
5215    fn test_cast_date64_to_timestamp_us() {
5216        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5217        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5218        let c = b
5219            .as_any()
5220            .downcast_ref::<TimestampMicrosecondArray>()
5221            .unwrap();
5222        assert_eq!(864000000005000, c.value(0));
5223        assert_eq!(1545696000001000, c.value(1));
5224        assert!(c.is_null(2));
5225    }
5226
5227    #[test]
5228    fn test_cast_date64_to_timestamp_ns() {
5229        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5230        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
5231        let c = b
5232            .as_any()
5233            .downcast_ref::<TimestampNanosecondArray>()
5234            .unwrap();
5235        assert_eq!(864000000005000000, c.value(0));
5236        assert_eq!(1545696000001000000, c.value(1));
5237        assert!(c.is_null(2));
5238    }
5239
5240    #[test]
5241    fn test_cast_timestamp_to_i64() {
5242        let array =
5243            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5244                .with_timezone("UTC".to_string());
5245        let b = cast(&array, &DataType::Int64).unwrap();
5246        let c = b.as_primitive::<Int64Type>();
5247        assert_eq!(&DataType::Int64, c.data_type());
5248        assert_eq!(864000000005, c.value(0));
5249        assert_eq!(1545696000001, c.value(1));
5250        assert!(c.is_null(2));
5251    }
5252
5253    #[test]
5254    fn test_cast_date32_to_string() {
5255        let array = Date32Array::from(vec![10000, 17890]);
5256        let b = cast(&array, &DataType::Utf8).unwrap();
5257        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5258        assert_eq!(&DataType::Utf8, c.data_type());
5259        assert_eq!("1997-05-19", c.value(0));
5260        assert_eq!("2018-12-25", c.value(1));
5261    }
5262
5263    #[test]
5264    fn test_cast_date64_to_string() {
5265        let array = Date64Array::from(vec![10000 * 86400000, 17890 * 86400000]);
5266        let b = cast(&array, &DataType::Utf8).unwrap();
5267        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5268        assert_eq!(&DataType::Utf8, c.data_type());
5269        assert_eq!("1997-05-19T00:00:00", c.value(0));
5270        assert_eq!("2018-12-25T00:00:00", c.value(1));
5271    }
5272
5273    macro_rules! assert_cast_timestamp_to_string {
5274        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
5275            let out = cast(&$array, &$datatype).unwrap();
5276            let actual = out
5277                .as_any()
5278                .downcast_ref::<$output_array_type>()
5279                .unwrap()
5280                .into_iter()
5281                .collect::<Vec<_>>();
5282            assert_eq!(actual, $expected);
5283        }};
5284        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
5285            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
5286            let actual = out
5287                .as_any()
5288                .downcast_ref::<$output_array_type>()
5289                .unwrap()
5290                .into_iter()
5291                .collect::<Vec<_>>();
5292            assert_eq!(actual, $expected);
5293        }};
5294    }
5295
5296    #[test]
5297    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
5298        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5299        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
5300        let array = Arc::new(a) as ArrayRef;
5301
5302        let b = cast(
5303            &array,
5304            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5305        )
5306        .unwrap();
5307        let c = b.as_primitive::<TimestampSecondType>();
5308        let string_array = cast(&c, &DataType::Utf8).unwrap();
5309        let result = string_array.as_string::<i32>();
5310        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5311
5312        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5313        let c = b.as_primitive::<TimestampSecondType>();
5314        let string_array = cast(&c, &DataType::Utf8).unwrap();
5315        let result = string_array.as_string::<i32>();
5316        assert_eq!("2021-01-01T00:00:00", result.value(0));
5317    }
5318
5319    #[test]
5320    fn test_cast_date32_to_timestamp_with_timezone() {
5321        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5322        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5323        let array = Arc::new(a) as ArrayRef;
5324        let b = cast(
5325            &array,
5326            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5327        )
5328        .unwrap();
5329        let c = b.as_primitive::<TimestampSecondType>();
5330        assert_eq!(1609438500, c.value(0));
5331        assert_eq!(1640974500, c.value(1));
5332        assert!(c.is_null(2));
5333
5334        let string_array = cast(&c, &DataType::Utf8).unwrap();
5335        let result = string_array.as_string::<i32>();
5336        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5337        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5338    }
5339
5340    #[test]
5341    fn test_cast_date32_to_timestamp_with_timezone_ms() {
5342        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5343        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5344        let array = Arc::new(a) as ArrayRef;
5345        let b = cast(
5346            &array,
5347            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5348        )
5349        .unwrap();
5350        let c = b.as_primitive::<TimestampMillisecondType>();
5351        assert_eq!(1609438500000, c.value(0));
5352        assert_eq!(1640974500000, c.value(1));
5353        assert!(c.is_null(2));
5354
5355        let string_array = cast(&c, &DataType::Utf8).unwrap();
5356        let result = string_array.as_string::<i32>();
5357        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5358        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5359    }
5360
5361    #[test]
5362    fn test_cast_date32_to_timestamp_with_timezone_us() {
5363        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5364        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5365        let array = Arc::new(a) as ArrayRef;
5366        let b = cast(
5367            &array,
5368            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5369        )
5370        .unwrap();
5371        let c = b.as_primitive::<TimestampMicrosecondType>();
5372        assert_eq!(1609438500000000, c.value(0));
5373        assert_eq!(1640974500000000, c.value(1));
5374        assert!(c.is_null(2));
5375
5376        let string_array = cast(&c, &DataType::Utf8).unwrap();
5377        let result = string_array.as_string::<i32>();
5378        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5379        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5380    }
5381
5382    #[test]
5383    fn test_cast_date32_to_timestamp_with_timezone_ns() {
5384        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5385        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5386        let array = Arc::new(a) as ArrayRef;
5387        let b = cast(
5388            &array,
5389            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5390        )
5391        .unwrap();
5392        let c = b.as_primitive::<TimestampNanosecondType>();
5393        assert_eq!(1609438500000000000, c.value(0));
5394        assert_eq!(1640974500000000000, c.value(1));
5395        assert!(c.is_null(2));
5396
5397        let string_array = cast(&c, &DataType::Utf8).unwrap();
5398        let result = string_array.as_string::<i32>();
5399        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5400        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5401    }
5402
5403    #[test]
5404    fn test_cast_date64_to_timestamp_with_timezone() {
5405        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5406        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5407        let b = cast(
5408            &array,
5409            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5410        )
5411        .unwrap();
5412
5413        let c = b.as_primitive::<TimestampSecondType>();
5414        assert_eq!(863979300, c.value(0));
5415        assert_eq!(1545675300, c.value(1));
5416        assert!(c.is_null(2));
5417
5418        let string_array = cast(&c, &DataType::Utf8).unwrap();
5419        let result = string_array.as_string::<i32>();
5420        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
5421        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
5422    }
5423
5424    #[test]
5425    fn test_cast_date64_to_timestamp_with_timezone_ms() {
5426        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5427        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5428        let b = cast(
5429            &array,
5430            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5431        )
5432        .unwrap();
5433
5434        let c = b.as_primitive::<TimestampMillisecondType>();
5435        assert_eq!(863979300005, c.value(0));
5436        assert_eq!(1545675300001, c.value(1));
5437        assert!(c.is_null(2));
5438
5439        let string_array = cast(&c, &DataType::Utf8).unwrap();
5440        let result = string_array.as_string::<i32>();
5441        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5442        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5443    }
5444
5445    #[test]
5446    fn test_cast_date64_to_timestamp_with_timezone_us() {
5447        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5448        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5449        let b = cast(
5450            &array,
5451            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5452        )
5453        .unwrap();
5454
5455        let c = b.as_primitive::<TimestampMicrosecondType>();
5456        assert_eq!(863979300005000, c.value(0));
5457        assert_eq!(1545675300001000, c.value(1));
5458        assert!(c.is_null(2));
5459
5460        let string_array = cast(&c, &DataType::Utf8).unwrap();
5461        let result = string_array.as_string::<i32>();
5462        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5463        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5464    }
5465
5466    #[test]
5467    fn test_cast_date64_to_timestamp_with_timezone_ns() {
5468        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5469        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5470        let b = cast(
5471            &array,
5472            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5473        )
5474        .unwrap();
5475
5476        let c = b.as_primitive::<TimestampNanosecondType>();
5477        assert_eq!(863979300005000000, c.value(0));
5478        assert_eq!(1545675300001000000, c.value(1));
5479        assert!(c.is_null(2));
5480
5481        let string_array = cast(&c, &DataType::Utf8).unwrap();
5482        let result = string_array.as_string::<i32>();
5483        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5484        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5485    }
5486
5487    #[test]
5488    fn test_cast_timestamp_to_strings() {
5489        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
5490        let array =
5491            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5492        let expected = vec![
5493            Some("1997-05-19T00:00:03.005"),
5494            Some("2018-12-25T00:00:02.001"),
5495            None,
5496        ];
5497
5498        assert_cast_timestamp_to_string!(array, DataType::Utf8View, StringViewArray, expected);
5499        assert_cast_timestamp_to_string!(array, DataType::Utf8, StringArray, expected);
5500        assert_cast_timestamp_to_string!(array, DataType::LargeUtf8, LargeStringArray, expected);
5501    }
5502
5503    #[test]
5504    fn test_cast_timestamp_to_strings_opt() {
5505        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
5506        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5507        let cast_options = CastOptions {
5508            safe: true,
5509            format_options: FormatOptions::default()
5510                .with_timestamp_format(Some(ts_format))
5511                .with_timestamp_tz_format(Some(ts_format)),
5512        };
5513
5514        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
5515        let array_without_tz =
5516            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5517        let expected = vec![
5518            Some("1997-05-19 00:00:03.005000"),
5519            Some("2018-12-25 00:00:02.001000"),
5520            None,
5521        ];
5522        assert_cast_timestamp_to_string!(
5523            array_without_tz,
5524            DataType::Utf8View,
5525            StringViewArray,
5526            cast_options,
5527            expected
5528        );
5529        assert_cast_timestamp_to_string!(
5530            array_without_tz,
5531            DataType::Utf8,
5532            StringArray,
5533            cast_options,
5534            expected
5535        );
5536        assert_cast_timestamp_to_string!(
5537            array_without_tz,
5538            DataType::LargeUtf8,
5539            LargeStringArray,
5540            cast_options,
5541            expected
5542        );
5543
5544        let array_with_tz =
5545            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
5546                .with_timezone(tz.to_string());
5547        let expected = vec![
5548            Some("1997-05-19 05:45:03.005000"),
5549            Some("2018-12-25 05:45:02.001000"),
5550            None,
5551        ];
5552        assert_cast_timestamp_to_string!(
5553            array_with_tz,
5554            DataType::Utf8View,
5555            StringViewArray,
5556            cast_options,
5557            expected
5558        );
5559        assert_cast_timestamp_to_string!(
5560            array_with_tz,
5561            DataType::Utf8,
5562            StringArray,
5563            cast_options,
5564            expected
5565        );
5566        assert_cast_timestamp_to_string!(
5567            array_with_tz,
5568            DataType::LargeUtf8,
5569            LargeStringArray,
5570            cast_options,
5571            expected
5572        );
5573    }
5574
5575    #[test]
5576    fn test_cast_between_timestamps() {
5577        let array =
5578            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5579        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5580        let c = b.as_primitive::<TimestampSecondType>();
5581        assert_eq!(864000003, c.value(0));
5582        assert_eq!(1545696002, c.value(1));
5583        assert!(c.is_null(2));
5584    }
5585
5586    #[test]
5587    fn test_cast_duration_to_i64() {
5588        let base = vec![5, 6, 7, 8, 100000000];
5589
5590        let duration_arrays = vec![
5591            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
5592            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
5593            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
5594            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
5595        ];
5596
5597        for arr in duration_arrays {
5598            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
5599            let result = cast(&arr, &DataType::Int64).unwrap();
5600            let result = result.as_primitive::<Int64Type>();
5601            assert_eq!(base.as_slice(), result.values());
5602        }
5603    }
5604
5605    #[test]
5606    fn test_cast_between_durations_and_numerics() {
5607        fn test_cast_between_durations<FromType, ToType>()
5608        where
5609            FromType: ArrowPrimitiveType<Native = i64>,
5610            ToType: ArrowPrimitiveType<Native = i64>,
5611            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
5612        {
5613            let from_unit = match FromType::DATA_TYPE {
5614                DataType::Duration(unit) => unit,
5615                _ => panic!("Expected a duration type"),
5616            };
5617            let to_unit = match ToType::DATA_TYPE {
5618                DataType::Duration(unit) => unit,
5619                _ => panic!("Expected a duration type"),
5620            };
5621            let from_size = time_unit_multiple(&from_unit);
5622            let to_size = time_unit_multiple(&to_unit);
5623
5624            let (v1_before, v2_before) = (8640003005, 1696002001);
5625            let (v1_after, v2_after) = if from_size >= to_size {
5626                (
5627                    v1_before / (from_size / to_size),
5628                    v2_before / (from_size / to_size),
5629                )
5630            } else {
5631                (
5632                    v1_before * (to_size / from_size),
5633                    v2_before * (to_size / from_size),
5634                )
5635            };
5636
5637            let array =
5638                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
5639            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
5640            let c = b.as_primitive::<ToType>();
5641            assert_eq!(v1_after, c.value(0));
5642            assert_eq!(v2_after, c.value(1));
5643            assert!(c.is_null(2));
5644        }
5645
5646        // between each individual duration type
5647        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
5648        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
5649        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
5650        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
5651        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
5652        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
5653        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
5654        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
5655        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
5656        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
5657        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
5658        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
5659
5660        // cast failed
5661        let array = DurationSecondArray::from(vec![
5662            Some(i64::MAX),
5663            Some(8640203410378005),
5664            Some(10241096),
5665            None,
5666        ]);
5667        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
5668        let c = b.as_primitive::<DurationNanosecondType>();
5669        assert!(c.is_null(0));
5670        assert!(c.is_null(1));
5671        assert_eq!(10241096000000000, c.value(2));
5672        assert!(c.is_null(3));
5673
5674        // durations to numerics
5675        let array = DurationSecondArray::from(vec![
5676            Some(i64::MAX),
5677            Some(8640203410378005),
5678            Some(10241096),
5679            None,
5680        ]);
5681        let b = cast(&array, &DataType::Int64).unwrap();
5682        let c = b.as_primitive::<Int64Type>();
5683        assert_eq!(i64::MAX, c.value(0));
5684        assert_eq!(8640203410378005, c.value(1));
5685        assert_eq!(10241096, c.value(2));
5686        assert!(c.is_null(3));
5687
5688        let b = cast(&array, &DataType::Int32).unwrap();
5689        let c = b.as_primitive::<Int32Type>();
5690        assert_eq!(0, c.value(0));
5691        assert_eq!(0, c.value(1));
5692        assert_eq!(10241096, c.value(2));
5693        assert!(c.is_null(3));
5694
5695        // numerics to durations
5696        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
5697        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
5698        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
5699        assert_eq!(i32::MAX as i64, c.value(0));
5700        assert_eq!(802034103, c.value(1));
5701        assert_eq!(10241096, c.value(2));
5702        assert!(c.is_null(3));
5703    }
5704
5705    #[test]
5706    fn test_cast_to_strings() {
5707        let a = Int32Array::from(vec![1, 2, 3]);
5708        let out = cast(&a, &DataType::Utf8).unwrap();
5709        let out = out
5710            .as_any()
5711            .downcast_ref::<StringArray>()
5712            .unwrap()
5713            .into_iter()
5714            .collect::<Vec<_>>();
5715        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
5716        let out = cast(&a, &DataType::LargeUtf8).unwrap();
5717        let out = out
5718            .as_any()
5719            .downcast_ref::<LargeStringArray>()
5720            .unwrap()
5721            .into_iter()
5722            .collect::<Vec<_>>();
5723        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
5724    }
5725
5726    #[test]
5727    fn test_str_to_str_casts() {
5728        for data in [
5729            vec![Some("foo"), Some("bar"), Some("ham")],
5730            vec![Some("foo"), None, Some("bar")],
5731        ] {
5732            let a = LargeStringArray::from(data.clone());
5733            let to = cast(&a, &DataType::Utf8).unwrap();
5734            let expect = a
5735                .as_any()
5736                .downcast_ref::<LargeStringArray>()
5737                .unwrap()
5738                .into_iter()
5739                .collect::<Vec<_>>();
5740            let out = to
5741                .as_any()
5742                .downcast_ref::<StringArray>()
5743                .unwrap()
5744                .into_iter()
5745                .collect::<Vec<_>>();
5746            assert_eq!(expect, out);
5747
5748            let a = StringArray::from(data);
5749            let to = cast(&a, &DataType::LargeUtf8).unwrap();
5750            let expect = a
5751                .as_any()
5752                .downcast_ref::<StringArray>()
5753                .unwrap()
5754                .into_iter()
5755                .collect::<Vec<_>>();
5756            let out = to
5757                .as_any()
5758                .downcast_ref::<LargeStringArray>()
5759                .unwrap()
5760                .into_iter()
5761                .collect::<Vec<_>>();
5762            assert_eq!(expect, out);
5763        }
5764    }
5765
5766    const VIEW_TEST_DATA: [Option<&str>; 5] = [
5767        Some("hello"),
5768        Some("repeated"),
5769        None,
5770        Some("large payload over 12 bytes"),
5771        Some("repeated"),
5772    ];
5773
5774    #[test]
5775    fn test_string_view_to_binary_view() {
5776        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5777
5778        assert!(can_cast_types(
5779            string_view_array.data_type(),
5780            &DataType::BinaryView
5781        ));
5782
5783        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
5784        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5785
5786        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5787        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5788    }
5789
5790    #[test]
5791    fn test_binary_view_to_string_view() {
5792        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5793
5794        assert!(can_cast_types(
5795            binary_view_array.data_type(),
5796            &DataType::Utf8View
5797        ));
5798
5799        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
5800        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5801
5802        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5803        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5804    }
5805
5806    #[test]
5807    fn test_string_to_view() {
5808        _test_string_to_view::<i32>();
5809        _test_string_to_view::<i64>();
5810    }
5811
5812    fn _test_string_to_view<O>()
5813    where
5814        O: OffsetSizeTrait,
5815    {
5816        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
5817
5818        assert!(can_cast_types(
5819            string_array.data_type(),
5820            &DataType::Utf8View
5821        ));
5822
5823        assert!(can_cast_types(
5824            string_array.data_type(),
5825            &DataType::BinaryView
5826        ));
5827
5828        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
5829        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5830
5831        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
5832        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5833
5834        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5835        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5836
5837        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5838        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5839    }
5840
5841    #[test]
5842    fn test_bianry_to_view() {
5843        _test_binary_to_view::<i32>();
5844        _test_binary_to_view::<i64>();
5845    }
5846
5847    fn _test_binary_to_view<O>()
5848    where
5849        O: OffsetSizeTrait,
5850    {
5851        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
5852
5853        assert!(can_cast_types(
5854            binary_array.data_type(),
5855            &DataType::Utf8View
5856        ));
5857
5858        assert!(can_cast_types(
5859            binary_array.data_type(),
5860            &DataType::BinaryView
5861        ));
5862
5863        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
5864        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5865
5866        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
5867        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5868
5869        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5870        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5871
5872        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5873        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5874    }
5875
5876    #[test]
5877    fn test_dict_to_view() {
5878        let values = StringArray::from_iter(VIEW_TEST_DATA);
5879        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
5880        let string_dict_array =
5881            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
5882        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
5883
5884        let string_view_array = {
5885            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5886            for v in typed_dict.into_iter() {
5887                builder.append_option(v);
5888            }
5889            builder.finish()
5890        };
5891        let expected_string_array_type = string_view_array.data_type();
5892        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
5893        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
5894        assert_eq!(casted_string_array.as_ref(), &string_view_array);
5895
5896        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
5897        let binary_dict_array =
5898            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
5899        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
5900
5901        let binary_view_array = {
5902            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5903            for v in typed_binary_dict.into_iter() {
5904                builder.append_option(v);
5905            }
5906            builder.finish()
5907        };
5908        let expected_binary_array_type = binary_view_array.data_type();
5909        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
5910        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
5911        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
5912    }
5913
5914    #[test]
5915    fn test_view_to_dict() {
5916        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5917        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
5918        let casted_type = string_dict_array.data_type();
5919        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
5920        assert_eq!(casted_dict_array.data_type(), casted_type);
5921        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
5922
5923        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5924        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
5925        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
5926        let binary_dict_array =
5927            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
5928        let casted_type = binary_dict_array.data_type();
5929        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
5930        assert_eq!(casted_binary_array.data_type(), casted_type);
5931        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
5932    }
5933
5934    #[test]
5935    fn test_view_to_string() {
5936        _test_view_to_string::<i32>();
5937        _test_view_to_string::<i64>();
5938    }
5939
5940    fn _test_view_to_string<O>()
5941    where
5942        O: OffsetSizeTrait,
5943    {
5944        let string_view_array = {
5945            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5946            for s in VIEW_TEST_DATA.iter() {
5947                builder.append_option(*s);
5948            }
5949            builder.finish()
5950        };
5951
5952        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5953
5954        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
5955        let expected_type = expected_string_array.data_type();
5956
5957        assert!(can_cast_types(string_view_array.data_type(), expected_type));
5958        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
5959
5960        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
5961        assert_eq!(string_view_casted_array.data_type(), expected_type);
5962        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
5963
5964        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
5965        assert_eq!(binary_view_casted_array.data_type(), expected_type);
5966        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
5967    }
5968
5969    #[test]
5970    fn test_view_to_binary() {
5971        _test_view_to_binary::<i32>();
5972        _test_view_to_binary::<i64>();
5973    }
5974
5975    fn _test_view_to_binary<O>()
5976    where
5977        O: OffsetSizeTrait,
5978    {
5979        let view_array = {
5980            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5981            for s in VIEW_TEST_DATA.iter() {
5982                builder.append_option(*s);
5983            }
5984            builder.finish()
5985        };
5986
5987        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
5988        let expected_type = expected_binary_array.data_type();
5989
5990        assert!(can_cast_types(view_array.data_type(), expected_type));
5991
5992        let binary_array = cast(&view_array, expected_type).unwrap();
5993        assert_eq!(binary_array.data_type(), expected_type);
5994
5995        assert_eq!(binary_array.as_ref(), &expected_binary_array);
5996    }
5997
5998    #[test]
5999    fn test_cast_from_f64() {
6000        let f64_values: Vec<f64> = vec![
6001            i64::MIN as f64,
6002            i32::MIN as f64,
6003            i16::MIN as f64,
6004            i8::MIN as f64,
6005            0_f64,
6006            u8::MAX as f64,
6007            u16::MAX as f64,
6008            u32::MAX as f64,
6009            u64::MAX as f64,
6010        ];
6011        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6012
6013        let f64_expected = vec![
6014            -9223372036854776000.0,
6015            -2147483648.0,
6016            -32768.0,
6017            -128.0,
6018            0.0,
6019            255.0,
6020            65535.0,
6021            4294967295.0,
6022            18446744073709552000.0,
6023        ];
6024        assert_eq!(
6025            f64_expected,
6026            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6027                .iter()
6028                .map(|i| i.parse::<f64>().unwrap())
6029                .collect::<Vec<f64>>()
6030        );
6031
6032        let f32_expected = vec![
6033            -9223372000000000000.0,
6034            -2147483600.0,
6035            -32768.0,
6036            -128.0,
6037            0.0,
6038            255.0,
6039            65535.0,
6040            4294967300.0,
6041            18446744000000000000.0,
6042        ];
6043        assert_eq!(
6044            f32_expected,
6045            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6046                .iter()
6047                .map(|i| i.parse::<f32>().unwrap())
6048                .collect::<Vec<f32>>()
6049        );
6050
6051        let f16_expected = vec![
6052            f16::from_f64(-9223372000000000000.0),
6053            f16::from_f64(-2147483600.0),
6054            f16::from_f64(-32768.0),
6055            f16::from_f64(-128.0),
6056            f16::from_f64(0.0),
6057            f16::from_f64(255.0),
6058            f16::from_f64(65535.0),
6059            f16::from_f64(4294967300.0),
6060            f16::from_f64(18446744000000000000.0),
6061        ];
6062        assert_eq!(
6063            f16_expected,
6064            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6065                .iter()
6066                .map(|i| i.parse::<f16>().unwrap())
6067                .collect::<Vec<f16>>()
6068        );
6069
6070        let i64_expected = vec![
6071            "-9223372036854775808",
6072            "-2147483648",
6073            "-32768",
6074            "-128",
6075            "0",
6076            "255",
6077            "65535",
6078            "4294967295",
6079            "null",
6080        ];
6081        assert_eq!(
6082            i64_expected,
6083            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
6084        );
6085
6086        let i32_expected = vec![
6087            "null",
6088            "-2147483648",
6089            "-32768",
6090            "-128",
6091            "0",
6092            "255",
6093            "65535",
6094            "null",
6095            "null",
6096        ];
6097        assert_eq!(
6098            i32_expected,
6099            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
6100        );
6101
6102        let i16_expected = vec![
6103            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6104        ];
6105        assert_eq!(
6106            i16_expected,
6107            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
6108        );
6109
6110        let i8_expected = vec![
6111            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6112        ];
6113        assert_eq!(
6114            i8_expected,
6115            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
6116        );
6117
6118        let u64_expected = vec![
6119            "null",
6120            "null",
6121            "null",
6122            "null",
6123            "0",
6124            "255",
6125            "65535",
6126            "4294967295",
6127            "null",
6128        ];
6129        assert_eq!(
6130            u64_expected,
6131            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
6132        );
6133
6134        let u32_expected = vec![
6135            "null",
6136            "null",
6137            "null",
6138            "null",
6139            "0",
6140            "255",
6141            "65535",
6142            "4294967295",
6143            "null",
6144        ];
6145        assert_eq!(
6146            u32_expected,
6147            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
6148        );
6149
6150        let u16_expected = vec![
6151            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6152        ];
6153        assert_eq!(
6154            u16_expected,
6155            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
6156        );
6157
6158        let u8_expected = vec![
6159            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6160        ];
6161        assert_eq!(
6162            u8_expected,
6163            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
6164        );
6165    }
6166
6167    #[test]
6168    fn test_cast_from_f32() {
6169        let f32_values: Vec<f32> = vec![
6170            i32::MIN as f32,
6171            i32::MIN as f32,
6172            i16::MIN as f32,
6173            i8::MIN as f32,
6174            0_f32,
6175            u8::MAX as f32,
6176            u16::MAX as f32,
6177            u32::MAX as f32,
6178            u32::MAX as f32,
6179        ];
6180        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
6181
6182        let f64_expected = vec![
6183            "-2147483648.0",
6184            "-2147483648.0",
6185            "-32768.0",
6186            "-128.0",
6187            "0.0",
6188            "255.0",
6189            "65535.0",
6190            "4294967296.0",
6191            "4294967296.0",
6192        ];
6193        assert_eq!(
6194            f64_expected,
6195            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
6196        );
6197
6198        let f32_expected = vec![
6199            "-2147483600.0",
6200            "-2147483600.0",
6201            "-32768.0",
6202            "-128.0",
6203            "0.0",
6204            "255.0",
6205            "65535.0",
6206            "4294967300.0",
6207            "4294967300.0",
6208        ];
6209        assert_eq!(
6210            f32_expected,
6211            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
6212        );
6213
6214        let f16_expected = vec![
6215            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
6216        ];
6217        assert_eq!(
6218            f16_expected,
6219            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
6220        );
6221
6222        let i64_expected = vec![
6223            "-2147483648",
6224            "-2147483648",
6225            "-32768",
6226            "-128",
6227            "0",
6228            "255",
6229            "65535",
6230            "4294967296",
6231            "4294967296",
6232        ];
6233        assert_eq!(
6234            i64_expected,
6235            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
6236        );
6237
6238        let i32_expected = vec![
6239            "-2147483648",
6240            "-2147483648",
6241            "-32768",
6242            "-128",
6243            "0",
6244            "255",
6245            "65535",
6246            "null",
6247            "null",
6248        ];
6249        assert_eq!(
6250            i32_expected,
6251            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
6252        );
6253
6254        let i16_expected = vec![
6255            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6256        ];
6257        assert_eq!(
6258            i16_expected,
6259            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
6260        );
6261
6262        let i8_expected = vec![
6263            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6264        ];
6265        assert_eq!(
6266            i8_expected,
6267            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
6268        );
6269
6270        let u64_expected = vec![
6271            "null",
6272            "null",
6273            "null",
6274            "null",
6275            "0",
6276            "255",
6277            "65535",
6278            "4294967296",
6279            "4294967296",
6280        ];
6281        assert_eq!(
6282            u64_expected,
6283            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
6284        );
6285
6286        let u32_expected = vec![
6287            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6288        ];
6289        assert_eq!(
6290            u32_expected,
6291            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
6292        );
6293
6294        let u16_expected = vec![
6295            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6296        ];
6297        assert_eq!(
6298            u16_expected,
6299            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
6300        );
6301
6302        let u8_expected = vec![
6303            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6304        ];
6305        assert_eq!(
6306            u8_expected,
6307            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
6308        );
6309    }
6310
6311    #[test]
6312    fn test_cast_from_uint64() {
6313        let u64_values: Vec<u64> = vec![
6314            0,
6315            u8::MAX as u64,
6316            u16::MAX as u64,
6317            u32::MAX as u64,
6318            u64::MAX,
6319        ];
6320        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
6321
6322        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
6323        assert_eq!(
6324            f64_expected,
6325            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
6326                .iter()
6327                .map(|i| i.parse::<f64>().unwrap())
6328                .collect::<Vec<f64>>()
6329        );
6330
6331        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
6332        assert_eq!(
6333            f32_expected,
6334            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
6335                .iter()
6336                .map(|i| i.parse::<f32>().unwrap())
6337                .collect::<Vec<f32>>()
6338        );
6339
6340        let f16_expected = vec![
6341            f16::from_f64(0.0),
6342            f16::from_f64(255.0),
6343            f16::from_f64(65535.0),
6344            f16::from_f64(4294967300.0),
6345            f16::from_f64(18446744000000000000.0),
6346        ];
6347        assert_eq!(
6348            f16_expected,
6349            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
6350                .iter()
6351                .map(|i| i.parse::<f16>().unwrap())
6352                .collect::<Vec<f16>>()
6353        );
6354
6355        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
6356        assert_eq!(
6357            i64_expected,
6358            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
6359        );
6360
6361        let i32_expected = vec!["0", "255", "65535", "null", "null"];
6362        assert_eq!(
6363            i32_expected,
6364            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
6365        );
6366
6367        let i16_expected = vec!["0", "255", "null", "null", "null"];
6368        assert_eq!(
6369            i16_expected,
6370            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
6371        );
6372
6373        let i8_expected = vec!["0", "null", "null", "null", "null"];
6374        assert_eq!(
6375            i8_expected,
6376            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
6377        );
6378
6379        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
6380        assert_eq!(
6381            u64_expected,
6382            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
6383        );
6384
6385        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
6386        assert_eq!(
6387            u32_expected,
6388            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
6389        );
6390
6391        let u16_expected = vec!["0", "255", "65535", "null", "null"];
6392        assert_eq!(
6393            u16_expected,
6394            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
6395        );
6396
6397        let u8_expected = vec!["0", "255", "null", "null", "null"];
6398        assert_eq!(
6399            u8_expected,
6400            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
6401        );
6402    }
6403
6404    #[test]
6405    fn test_cast_from_uint32() {
6406        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
6407        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
6408
6409        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
6410        assert_eq!(
6411            f64_expected,
6412            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
6413        );
6414
6415        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
6416        assert_eq!(
6417            f32_expected,
6418            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
6419        );
6420
6421        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
6422        assert_eq!(
6423            f16_expected,
6424            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
6425        );
6426
6427        let i64_expected = vec!["0", "255", "65535", "4294967295"];
6428        assert_eq!(
6429            i64_expected,
6430            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
6431        );
6432
6433        let i32_expected = vec!["0", "255", "65535", "null"];
6434        assert_eq!(
6435            i32_expected,
6436            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
6437        );
6438
6439        let i16_expected = vec!["0", "255", "null", "null"];
6440        assert_eq!(
6441            i16_expected,
6442            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
6443        );
6444
6445        let i8_expected = vec!["0", "null", "null", "null"];
6446        assert_eq!(
6447            i8_expected,
6448            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
6449        );
6450
6451        let u64_expected = vec!["0", "255", "65535", "4294967295"];
6452        assert_eq!(
6453            u64_expected,
6454            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
6455        );
6456
6457        let u32_expected = vec!["0", "255", "65535", "4294967295"];
6458        assert_eq!(
6459            u32_expected,
6460            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
6461        );
6462
6463        let u16_expected = vec!["0", "255", "65535", "null"];
6464        assert_eq!(
6465            u16_expected,
6466            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
6467        );
6468
6469        let u8_expected = vec!["0", "255", "null", "null"];
6470        assert_eq!(
6471            u8_expected,
6472            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
6473        );
6474    }
6475
6476    #[test]
6477    fn test_cast_from_uint16() {
6478        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
6479        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
6480
6481        let f64_expected = vec!["0.0", "255.0", "65535.0"];
6482        assert_eq!(
6483            f64_expected,
6484            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
6485        );
6486
6487        let f32_expected = vec!["0.0", "255.0", "65535.0"];
6488        assert_eq!(
6489            f32_expected,
6490            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
6491        );
6492
6493        let f16_expected = vec!["0.0", "255.0", "inf"];
6494        assert_eq!(
6495            f16_expected,
6496            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
6497        );
6498
6499        let i64_expected = vec!["0", "255", "65535"];
6500        assert_eq!(
6501            i64_expected,
6502            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
6503        );
6504
6505        let i32_expected = vec!["0", "255", "65535"];
6506        assert_eq!(
6507            i32_expected,
6508            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
6509        );
6510
6511        let i16_expected = vec!["0", "255", "null"];
6512        assert_eq!(
6513            i16_expected,
6514            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
6515        );
6516
6517        let i8_expected = vec!["0", "null", "null"];
6518        assert_eq!(
6519            i8_expected,
6520            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
6521        );
6522
6523        let u64_expected = vec!["0", "255", "65535"];
6524        assert_eq!(
6525            u64_expected,
6526            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
6527        );
6528
6529        let u32_expected = vec!["0", "255", "65535"];
6530        assert_eq!(
6531            u32_expected,
6532            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
6533        );
6534
6535        let u16_expected = vec!["0", "255", "65535"];
6536        assert_eq!(
6537            u16_expected,
6538            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
6539        );
6540
6541        let u8_expected = vec!["0", "255", "null"];
6542        assert_eq!(
6543            u8_expected,
6544            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
6545        );
6546    }
6547
6548    #[test]
6549    fn test_cast_from_uint8() {
6550        let u8_values: Vec<u8> = vec![0, u8::MAX];
6551        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
6552
6553        let f64_expected = vec!["0.0", "255.0"];
6554        assert_eq!(
6555            f64_expected,
6556            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
6557        );
6558
6559        let f32_expected = vec!["0.0", "255.0"];
6560        assert_eq!(
6561            f32_expected,
6562            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
6563        );
6564
6565        let f16_expected = vec!["0.0", "255.0"];
6566        assert_eq!(
6567            f16_expected,
6568            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
6569        );
6570
6571        let i64_expected = vec!["0", "255"];
6572        assert_eq!(
6573            i64_expected,
6574            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
6575        );
6576
6577        let i32_expected = vec!["0", "255"];
6578        assert_eq!(
6579            i32_expected,
6580            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
6581        );
6582
6583        let i16_expected = vec!["0", "255"];
6584        assert_eq!(
6585            i16_expected,
6586            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
6587        );
6588
6589        let i8_expected = vec!["0", "null"];
6590        assert_eq!(
6591            i8_expected,
6592            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
6593        );
6594
6595        let u64_expected = vec!["0", "255"];
6596        assert_eq!(
6597            u64_expected,
6598            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
6599        );
6600
6601        let u32_expected = vec!["0", "255"];
6602        assert_eq!(
6603            u32_expected,
6604            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
6605        );
6606
6607        let u16_expected = vec!["0", "255"];
6608        assert_eq!(
6609            u16_expected,
6610            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
6611        );
6612
6613        let u8_expected = vec!["0", "255"];
6614        assert_eq!(
6615            u8_expected,
6616            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
6617        );
6618    }
6619
6620    #[test]
6621    fn test_cast_from_int64() {
6622        let i64_values: Vec<i64> = vec![
6623            i64::MIN,
6624            i32::MIN as i64,
6625            i16::MIN as i64,
6626            i8::MIN as i64,
6627            0,
6628            i8::MAX as i64,
6629            i16::MAX as i64,
6630            i32::MAX as i64,
6631            i64::MAX,
6632        ];
6633        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
6634
6635        let f64_expected = vec![
6636            -9223372036854776000.0,
6637            -2147483648.0,
6638            -32768.0,
6639            -128.0,
6640            0.0,
6641            127.0,
6642            32767.0,
6643            2147483647.0,
6644            9223372036854776000.0,
6645        ];
6646        assert_eq!(
6647            f64_expected,
6648            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
6649                .iter()
6650                .map(|i| i.parse::<f64>().unwrap())
6651                .collect::<Vec<f64>>()
6652        );
6653
6654        let f32_expected = vec![
6655            -9223372000000000000.0,
6656            -2147483600.0,
6657            -32768.0,
6658            -128.0,
6659            0.0,
6660            127.0,
6661            32767.0,
6662            2147483600.0,
6663            9223372000000000000.0,
6664        ];
6665        assert_eq!(
6666            f32_expected,
6667            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
6668                .iter()
6669                .map(|i| i.parse::<f32>().unwrap())
6670                .collect::<Vec<f32>>()
6671        );
6672
6673        let f16_expected = vec![
6674            f16::from_f64(-9223372000000000000.0),
6675            f16::from_f64(-2147483600.0),
6676            f16::from_f64(-32768.0),
6677            f16::from_f64(-128.0),
6678            f16::from_f64(0.0),
6679            f16::from_f64(127.0),
6680            f16::from_f64(32767.0),
6681            f16::from_f64(2147483600.0),
6682            f16::from_f64(9223372000000000000.0),
6683        ];
6684        assert_eq!(
6685            f16_expected,
6686            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
6687                .iter()
6688                .map(|i| i.parse::<f16>().unwrap())
6689                .collect::<Vec<f16>>()
6690        );
6691
6692        let i64_expected = vec![
6693            "-9223372036854775808",
6694            "-2147483648",
6695            "-32768",
6696            "-128",
6697            "0",
6698            "127",
6699            "32767",
6700            "2147483647",
6701            "9223372036854775807",
6702        ];
6703        assert_eq!(
6704            i64_expected,
6705            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
6706        );
6707
6708        let i32_expected = vec![
6709            "null",
6710            "-2147483648",
6711            "-32768",
6712            "-128",
6713            "0",
6714            "127",
6715            "32767",
6716            "2147483647",
6717            "null",
6718        ];
6719        assert_eq!(
6720            i32_expected,
6721            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
6722        );
6723
6724        assert_eq!(
6725            i32_expected,
6726            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
6727        );
6728
6729        let i16_expected = vec![
6730            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
6731        ];
6732        assert_eq!(
6733            i16_expected,
6734            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
6735        );
6736
6737        let i8_expected = vec![
6738            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
6739        ];
6740        assert_eq!(
6741            i8_expected,
6742            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
6743        );
6744
6745        let u64_expected = vec![
6746            "null",
6747            "null",
6748            "null",
6749            "null",
6750            "0",
6751            "127",
6752            "32767",
6753            "2147483647",
6754            "9223372036854775807",
6755        ];
6756        assert_eq!(
6757            u64_expected,
6758            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
6759        );
6760
6761        let u32_expected = vec![
6762            "null",
6763            "null",
6764            "null",
6765            "null",
6766            "0",
6767            "127",
6768            "32767",
6769            "2147483647",
6770            "null",
6771        ];
6772        assert_eq!(
6773            u32_expected,
6774            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
6775        );
6776
6777        let u16_expected = vec![
6778            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
6779        ];
6780        assert_eq!(
6781            u16_expected,
6782            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
6783        );
6784
6785        let u8_expected = vec![
6786            "null", "null", "null", "null", "0", "127", "null", "null", "null",
6787        ];
6788        assert_eq!(
6789            u8_expected,
6790            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
6791        );
6792    }
6793
6794    #[test]
6795    fn test_cast_from_int32() {
6796        let i32_values: Vec<i32> = vec![
6797            i32::MIN,
6798            i16::MIN as i32,
6799            i8::MIN as i32,
6800            0,
6801            i8::MAX as i32,
6802            i16::MAX as i32,
6803            i32::MAX,
6804        ];
6805        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
6806
6807        let f64_expected = vec![
6808            "-2147483648.0",
6809            "-32768.0",
6810            "-128.0",
6811            "0.0",
6812            "127.0",
6813            "32767.0",
6814            "2147483647.0",
6815        ];
6816        assert_eq!(
6817            f64_expected,
6818            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
6819        );
6820
6821        let f32_expected = vec![
6822            "-2147483600.0",
6823            "-32768.0",
6824            "-128.0",
6825            "0.0",
6826            "127.0",
6827            "32767.0",
6828            "2147483600.0",
6829        ];
6830        assert_eq!(
6831            f32_expected,
6832            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
6833        );
6834
6835        let f16_expected = vec![
6836            f16::from_f64(-2147483600.0),
6837            f16::from_f64(-32768.0),
6838            f16::from_f64(-128.0),
6839            f16::from_f64(0.0),
6840            f16::from_f64(127.0),
6841            f16::from_f64(32767.0),
6842            f16::from_f64(2147483600.0),
6843        ];
6844        assert_eq!(
6845            f16_expected,
6846            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
6847                .iter()
6848                .map(|i| i.parse::<f16>().unwrap())
6849                .collect::<Vec<f16>>()
6850        );
6851
6852        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
6853        assert_eq!(
6854            i16_expected,
6855            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
6856        );
6857
6858        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
6859        assert_eq!(
6860            i8_expected,
6861            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
6862        );
6863
6864        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
6865        assert_eq!(
6866            u64_expected,
6867            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
6868        );
6869
6870        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
6871        assert_eq!(
6872            u32_expected,
6873            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
6874        );
6875
6876        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
6877        assert_eq!(
6878            u16_expected,
6879            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
6880        );
6881
6882        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
6883        assert_eq!(
6884            u8_expected,
6885            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
6886        );
6887
6888        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
6889        let i64_expected = vec![
6890            "-185542587187200000",
6891            "-2831155200000",
6892            "-11059200000",
6893            "0",
6894            "10972800000",
6895            "2831068800000",
6896            "185542587100800000",
6897        ];
6898        assert_eq!(
6899            i64_expected,
6900            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
6901        );
6902    }
6903
6904    #[test]
6905    fn test_cast_from_int16() {
6906        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
6907        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
6908
6909        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
6910        assert_eq!(
6911            f64_expected,
6912            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
6913        );
6914
6915        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
6916        assert_eq!(
6917            f32_expected,
6918            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
6919        );
6920
6921        let f16_expected = vec![
6922            f16::from_f64(-32768.0),
6923            f16::from_f64(-128.0),
6924            f16::from_f64(0.0),
6925            f16::from_f64(127.0),
6926            f16::from_f64(32767.0),
6927        ];
6928        assert_eq!(
6929            f16_expected,
6930            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
6931                .iter()
6932                .map(|i| i.parse::<f16>().unwrap())
6933                .collect::<Vec<f16>>()
6934        );
6935
6936        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
6937        assert_eq!(
6938            i64_expected,
6939            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
6940        );
6941
6942        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
6943        assert_eq!(
6944            i32_expected,
6945            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
6946        );
6947
6948        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
6949        assert_eq!(
6950            i16_expected,
6951            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
6952        );
6953
6954        let i8_expected = vec!["null", "-128", "0", "127", "null"];
6955        assert_eq!(
6956            i8_expected,
6957            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
6958        );
6959
6960        let u64_expected = vec!["null", "null", "0", "127", "32767"];
6961        assert_eq!(
6962            u64_expected,
6963            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
6964        );
6965
6966        let u32_expected = vec!["null", "null", "0", "127", "32767"];
6967        assert_eq!(
6968            u32_expected,
6969            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
6970        );
6971
6972        let u16_expected = vec!["null", "null", "0", "127", "32767"];
6973        assert_eq!(
6974            u16_expected,
6975            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
6976        );
6977
6978        let u8_expected = vec!["null", "null", "0", "127", "null"];
6979        assert_eq!(
6980            u8_expected,
6981            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
6982        );
6983    }
6984
6985    #[test]
6986    fn test_cast_from_date32() {
6987        let i32_values: Vec<i32> = vec![
6988            i32::MIN,
6989            i16::MIN as i32,
6990            i8::MIN as i32,
6991            0,
6992            i8::MAX as i32,
6993            i16::MAX as i32,
6994            i32::MAX,
6995        ];
6996        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
6997
6998        let i64_expected = vec![
6999            "-2147483648",
7000            "-32768",
7001            "-128",
7002            "0",
7003            "127",
7004            "32767",
7005            "2147483647",
7006        ];
7007        assert_eq!(
7008            i64_expected,
7009            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7010        );
7011    }
7012
7013    #[test]
7014    fn test_cast_from_int8() {
7015        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7016        let i8_array = Int8Array::from(i8_values);
7017
7018        let f64_expected = vec!["-128.0", "0.0", "127.0"];
7019        assert_eq!(
7020            f64_expected,
7021            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7022        );
7023
7024        let f32_expected = vec!["-128.0", "0.0", "127.0"];
7025        assert_eq!(
7026            f32_expected,
7027            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7028        );
7029
7030        let f16_expected = vec!["-128.0", "0.0", "127.0"];
7031        assert_eq!(
7032            f16_expected,
7033            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7034        );
7035
7036        let i64_expected = vec!["-128", "0", "127"];
7037        assert_eq!(
7038            i64_expected,
7039            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7040        );
7041
7042        let i32_expected = vec!["-128", "0", "127"];
7043        assert_eq!(
7044            i32_expected,
7045            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7046        );
7047
7048        let i16_expected = vec!["-128", "0", "127"];
7049        assert_eq!(
7050            i16_expected,
7051            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7052        );
7053
7054        let i8_expected = vec!["-128", "0", "127"];
7055        assert_eq!(
7056            i8_expected,
7057            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7058        );
7059
7060        let u64_expected = vec!["null", "0", "127"];
7061        assert_eq!(
7062            u64_expected,
7063            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7064        );
7065
7066        let u32_expected = vec!["null", "0", "127"];
7067        assert_eq!(
7068            u32_expected,
7069            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7070        );
7071
7072        let u16_expected = vec!["null", "0", "127"];
7073        assert_eq!(
7074            u16_expected,
7075            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7076        );
7077
7078        let u8_expected = vec!["null", "0", "127"];
7079        assert_eq!(
7080            u8_expected,
7081            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
7082        );
7083    }
7084
7085    /// Convert `array` into a vector of strings by casting to data type dt
7086    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
7087    where
7088        T: ArrowPrimitiveType,
7089    {
7090        let c = cast(array, dt).unwrap();
7091        let a = c.as_primitive::<T>();
7092        let mut v: Vec<String> = vec![];
7093        for i in 0..array.len() {
7094            if a.is_null(i) {
7095                v.push("null".to_string())
7096            } else {
7097                v.push(format!("{:?}", a.value(i)));
7098            }
7099        }
7100        v
7101    }
7102
7103    #[test]
7104    fn test_cast_utf8_dict() {
7105        // FROM a dictionary with of Utf8 values
7106        use DataType::*;
7107
7108        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
7109        builder.append("one").unwrap();
7110        builder.append_null();
7111        builder.append("three").unwrap();
7112        let array: ArrayRef = Arc::new(builder.finish());
7113
7114        let expected = vec!["one", "null", "three"];
7115
7116        // Test casting TO StringArray
7117        let cast_type = Utf8;
7118        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
7119        assert_eq!(cast_array.data_type(), &cast_type);
7120        assert_eq!(array_to_strings(&cast_array), expected);
7121
7122        // Test casting TO Dictionary (with different index sizes)
7123
7124        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
7125        let cast_array = cast(&array, &cast_type).expect("cast failed");
7126        assert_eq!(cast_array.data_type(), &cast_type);
7127        assert_eq!(array_to_strings(&cast_array), expected);
7128
7129        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
7130        let cast_array = cast(&array, &cast_type).expect("cast failed");
7131        assert_eq!(cast_array.data_type(), &cast_type);
7132        assert_eq!(array_to_strings(&cast_array), expected);
7133
7134        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
7135        let cast_array = cast(&array, &cast_type).expect("cast failed");
7136        assert_eq!(cast_array.data_type(), &cast_type);
7137        assert_eq!(array_to_strings(&cast_array), expected);
7138
7139        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7140        let cast_array = cast(&array, &cast_type).expect("cast failed");
7141        assert_eq!(cast_array.data_type(), &cast_type);
7142        assert_eq!(array_to_strings(&cast_array), expected);
7143
7144        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
7145        let cast_array = cast(&array, &cast_type).expect("cast failed");
7146        assert_eq!(cast_array.data_type(), &cast_type);
7147        assert_eq!(array_to_strings(&cast_array), expected);
7148
7149        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
7150        let cast_array = cast(&array, &cast_type).expect("cast failed");
7151        assert_eq!(cast_array.data_type(), &cast_type);
7152        assert_eq!(array_to_strings(&cast_array), expected);
7153
7154        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
7155        let cast_array = cast(&array, &cast_type).expect("cast failed");
7156        assert_eq!(cast_array.data_type(), &cast_type);
7157        assert_eq!(array_to_strings(&cast_array), expected);
7158    }
7159
7160    #[test]
7161    fn test_cast_dict_to_dict_bad_index_value_primitive() {
7162        use DataType::*;
7163        // test converting from an array that has indexes of a type
7164        // that are out of bounds for a particular other kind of
7165        // index.
7166
7167        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
7168
7169        // add 200 distinct values (which can be stored by a
7170        // dictionary indexed by int32, but not a dictionary indexed
7171        // with int8)
7172        for i in 0..200 {
7173            builder.append(i).unwrap();
7174        }
7175        let array: ArrayRef = Arc::new(builder.finish());
7176
7177        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7178        let res = cast(&array, &cast_type);
7179        assert!(res.is_err());
7180        let actual_error = format!("{res:?}");
7181        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7182        assert!(
7183            actual_error.contains(expected_error),
7184            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7185        );
7186    }
7187
7188    #[test]
7189    fn test_cast_dict_to_dict_bad_index_value_utf8() {
7190        use DataType::*;
7191        // Same test as test_cast_dict_to_dict_bad_index_value but use
7192        // string values (and encode the expected behavior here);
7193
7194        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
7195
7196        // add 200 distinct values (which can be stored by a
7197        // dictionary indexed by int32, but not a dictionary indexed
7198        // with int8)
7199        for i in 0..200 {
7200            let val = format!("val{i}");
7201            builder.append(&val).unwrap();
7202        }
7203        let array = builder.finish();
7204
7205        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7206        let res = cast(&array, &cast_type);
7207        assert!(res.is_err());
7208        let actual_error = format!("{res:?}");
7209        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7210        assert!(
7211            actual_error.contains(expected_error),
7212            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7213        );
7214    }
7215
7216    #[test]
7217    fn test_cast_primitive_dict() {
7218        // FROM a dictionary with of INT32 values
7219        use DataType::*;
7220
7221        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
7222        builder.append(1).unwrap();
7223        builder.append_null();
7224        builder.append(3).unwrap();
7225        let array: ArrayRef = Arc::new(builder.finish());
7226
7227        let expected = vec!["1", "null", "3"];
7228
7229        // Test casting TO PrimitiveArray, different dictionary type
7230        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
7231        assert_eq!(array_to_strings(&cast_array), expected);
7232        assert_eq!(cast_array.data_type(), &Utf8);
7233
7234        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
7235        assert_eq!(array_to_strings(&cast_array), expected);
7236        assert_eq!(cast_array.data_type(), &Int64);
7237    }
7238
7239    #[test]
7240    fn test_cast_primitive_array_to_dict() {
7241        use DataType::*;
7242
7243        let mut builder = PrimitiveBuilder::<Int32Type>::new();
7244        builder.append_value(1);
7245        builder.append_null();
7246        builder.append_value(3);
7247        let array: ArrayRef = Arc::new(builder.finish());
7248
7249        let expected = vec!["1", "null", "3"];
7250
7251        // Cast to a dictionary (same value type, Int32)
7252        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
7253        let cast_array = cast(&array, &cast_type).expect("cast failed");
7254        assert_eq!(cast_array.data_type(), &cast_type);
7255        assert_eq!(array_to_strings(&cast_array), expected);
7256
7257        // Cast to a dictionary (different value type, Int8)
7258        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
7259        let cast_array = cast(&array, &cast_type).expect("cast failed");
7260        assert_eq!(cast_array.data_type(), &cast_type);
7261        assert_eq!(array_to_strings(&cast_array), expected);
7262    }
7263
7264    #[test]
7265    fn test_cast_time_array_to_dict() {
7266        use DataType::*;
7267
7268        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
7269
7270        let expected = vec!["1972-09-27", "null", "1975-06-24"];
7271
7272        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
7273        let cast_array = cast(&array, &cast_type).expect("cast failed");
7274        assert_eq!(cast_array.data_type(), &cast_type);
7275        assert_eq!(array_to_strings(&cast_array), expected);
7276    }
7277
7278    #[test]
7279    fn test_cast_timestamp_array_to_dict() {
7280        use DataType::*;
7281
7282        let array = Arc::new(
7283            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
7284        ) as ArrayRef;
7285
7286        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
7287
7288        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
7289        let cast_array = cast(&array, &cast_type).expect("cast failed");
7290        assert_eq!(cast_array.data_type(), &cast_type);
7291        assert_eq!(array_to_strings(&cast_array), expected);
7292    }
7293
7294    #[test]
7295    fn test_cast_string_array_to_dict() {
7296        use DataType::*;
7297
7298        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
7299
7300        let expected = vec!["one", "null", "three"];
7301
7302        // Cast to a dictionary (same value type, Utf8)
7303        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7304        let cast_array = cast(&array, &cast_type).expect("cast failed");
7305        assert_eq!(cast_array.data_type(), &cast_type);
7306        assert_eq!(array_to_strings(&cast_array), expected);
7307    }
7308
7309    #[test]
7310    fn test_cast_null_array_to_from_decimal_array() {
7311        let data_type = DataType::Decimal128(12, 4);
7312        let array = new_null_array(&DataType::Null, 4);
7313        assert_eq!(array.data_type(), &DataType::Null);
7314        let cast_array = cast(&array, &data_type).expect("cast failed");
7315        assert_eq!(cast_array.data_type(), &data_type);
7316        for i in 0..4 {
7317            assert!(cast_array.is_null(i));
7318        }
7319
7320        let array = new_null_array(&data_type, 4);
7321        assert_eq!(array.data_type(), &data_type);
7322        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
7323        assert_eq!(cast_array.data_type(), &DataType::Null);
7324        assert_eq!(cast_array.len(), 4);
7325        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
7326    }
7327
7328    #[test]
7329    fn test_cast_null_array_from_and_to_primitive_array() {
7330        macro_rules! typed_test {
7331            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
7332                {
7333                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
7334                    let expected = $ARR_TYPE::from(vec![None; 6]);
7335                    let cast_type = DataType::$DATATYPE;
7336                    let cast_array = cast(&array, &cast_type).expect("cast failed");
7337                    let cast_array = cast_array.as_primitive::<$TYPE>();
7338                    assert_eq!(cast_array.data_type(), &cast_type);
7339                    assert_eq!(cast_array, &expected);
7340                }
7341            }};
7342        }
7343
7344        typed_test!(Int16Array, Int16, Int16Type);
7345        typed_test!(Int32Array, Int32, Int32Type);
7346        typed_test!(Int64Array, Int64, Int64Type);
7347
7348        typed_test!(UInt16Array, UInt16, UInt16Type);
7349        typed_test!(UInt32Array, UInt32, UInt32Type);
7350        typed_test!(UInt64Array, UInt64, UInt64Type);
7351
7352        typed_test!(Float32Array, Float32, Float32Type);
7353        typed_test!(Float64Array, Float64, Float64Type);
7354
7355        typed_test!(Date32Array, Date32, Date32Type);
7356        typed_test!(Date64Array, Date64, Date64Type);
7357    }
7358
7359    fn cast_from_null_to_other(data_type: &DataType) {
7360        // Cast from null to data_type
7361        {
7362            let array = new_null_array(&DataType::Null, 4);
7363            assert_eq!(array.data_type(), &DataType::Null);
7364            let cast_array = cast(&array, data_type).expect("cast failed");
7365            assert_eq!(cast_array.data_type(), data_type);
7366            for i in 0..4 {
7367                assert!(cast_array.is_null(i));
7368            }
7369        }
7370    }
7371
7372    #[test]
7373    fn test_cast_null_from_and_to_variable_sized() {
7374        cast_from_null_to_other(&DataType::Utf8);
7375        cast_from_null_to_other(&DataType::LargeUtf8);
7376        cast_from_null_to_other(&DataType::Binary);
7377        cast_from_null_to_other(&DataType::LargeBinary);
7378    }
7379
7380    #[test]
7381    fn test_cast_null_from_and_to_nested_type() {
7382        // Cast null from and to map
7383        let data_type = DataType::Map(
7384            Arc::new(Field::new_struct(
7385                "entry",
7386                vec![
7387                    Field::new("key", DataType::Utf8, false),
7388                    Field::new("value", DataType::Int32, true),
7389                ],
7390                false,
7391            )),
7392            false,
7393        );
7394        cast_from_null_to_other(&data_type);
7395
7396        // Cast null from and to list
7397        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
7398        cast_from_null_to_other(&data_type);
7399        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
7400        cast_from_null_to_other(&data_type);
7401        let data_type =
7402            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
7403        cast_from_null_to_other(&data_type);
7404
7405        // Cast null from and to dictionary
7406        let values = vec![None, None, None, None] as Vec<Option<&str>>;
7407        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
7408        let array = Arc::new(array) as ArrayRef;
7409        let data_type = array.data_type().to_owned();
7410        cast_from_null_to_other(&data_type);
7411
7412        // Cast null from and to struct
7413        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
7414        cast_from_null_to_other(&data_type);
7415    }
7416
7417    /// Print the `DictionaryArray` `array` as a vector of strings
7418    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
7419        let options = FormatOptions::new().with_null("null");
7420        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
7421        (0..array.len())
7422            .map(|i| formatter.value(i).to_string())
7423            .collect()
7424    }
7425
7426    #[test]
7427    fn test_cast_utf8_to_date32() {
7428        use chrono::NaiveDate;
7429        let from_ymd = chrono::NaiveDate::from_ymd_opt;
7430        let since = chrono::NaiveDate::signed_duration_since;
7431
7432        let a = StringArray::from(vec![
7433            "2000-01-01",          // valid date with leading 0s
7434            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
7435            "2000-2-2",            // valid date without leading 0s
7436            "2000-00-00",          // invalid month and day
7437            "2000",                // just a year is invalid
7438        ]);
7439        let array = Arc::new(a) as ArrayRef;
7440        let b = cast(&array, &DataType::Date32).unwrap();
7441        let c = b.as_primitive::<Date32Type>();
7442
7443        // test valid inputs
7444        let date_value = since(
7445            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
7446            from_ymd(1970, 1, 1).unwrap(),
7447        )
7448        .num_days() as i32;
7449        assert!(c.is_valid(0)); // "2000-01-01"
7450        assert_eq!(date_value, c.value(0));
7451
7452        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
7453        assert_eq!(date_value, c.value(1));
7454
7455        let date_value = since(
7456            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
7457            from_ymd(1970, 1, 1).unwrap(),
7458        )
7459        .num_days() as i32;
7460        assert!(c.is_valid(2)); // "2000-2-2"
7461        assert_eq!(date_value, c.value(2));
7462
7463        // test invalid inputs
7464        assert!(!c.is_valid(3)); // "2000-00-00"
7465        assert!(!c.is_valid(4)); // "2000"
7466    }
7467
7468    #[test]
7469    fn test_cast_utf8_to_date64() {
7470        let a = StringArray::from(vec![
7471            "2000-01-01T12:00:00", // date + time valid
7472            "2020-12-15T12:34:56", // date + time valid
7473            "2020-2-2T12:34:56",   // valid date time without leading 0s
7474            "2000-00-00T12:00:00", // invalid month and day
7475            "2000-01-01 12:00:00", // missing the 'T'
7476            "2000-01-01",          // just a date is invalid
7477        ]);
7478        let array = Arc::new(a) as ArrayRef;
7479        let b = cast(&array, &DataType::Date64).unwrap();
7480        let c = b.as_primitive::<Date64Type>();
7481
7482        // test valid inputs
7483        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
7484        assert_eq!(946728000000, c.value(0));
7485        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
7486        assert_eq!(1608035696000, c.value(1));
7487        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
7488
7489        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
7490        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
7491        assert_eq!(946728000000, c.value(4));
7492        assert!(c.is_valid(5)); // "2000-01-01"
7493        assert_eq!(946684800000, c.value(5));
7494    }
7495
7496    #[test]
7497    fn test_can_cast_fsl_to_fsl() {
7498        let from_array = Arc::new(
7499            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
7500                [Some([Some(1.0), Some(2.0)]), None],
7501                2,
7502            ),
7503        ) as ArrayRef;
7504        let to_array = Arc::new(
7505            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
7506                [
7507                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
7508                    None,
7509                ],
7510                2,
7511            ),
7512        ) as ArrayRef;
7513
7514        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
7515        let actual = cast(&from_array, to_array.data_type()).unwrap();
7516        assert_eq!(actual.data_type(), to_array.data_type());
7517
7518        let invalid_target =
7519            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
7520        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
7521
7522        let invalid_size =
7523            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
7524        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
7525    }
7526
7527    #[test]
7528    fn test_can_cast_types_fixed_size_list_to_list() {
7529        // DataType::List
7530        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
7531        assert!(can_cast_types(
7532            array1.data_type(),
7533            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
7534        ));
7535
7536        // DataType::LargeList
7537        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
7538        assert!(can_cast_types(
7539            array2.data_type(),
7540            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
7541        ));
7542    }
7543
7544    #[test]
7545    fn test_cast_fixed_size_list_to_list() {
7546        // Important cases:
7547        // 1. With/without nulls
7548        // 2. LargeList and List
7549        // 3. With and without inner casts
7550
7551        let cases = [
7552            // fixed_size_list<i32, 2> => list<i32>
7553            (
7554                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7555                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
7556                    2,
7557                )) as ArrayRef,
7558                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
7559                    Some([Some(1), Some(1)]),
7560                    Some([Some(2), Some(2)]),
7561                ])) as ArrayRef,
7562            ),
7563            // fixed_size_list<i32, 2> => list<i32> (nullable)
7564            (
7565                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7566                    [None, Some([Some(2), Some(2)])],
7567                    2,
7568                )) as ArrayRef,
7569                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
7570                    None,
7571                    Some([Some(2), Some(2)]),
7572                ])) as ArrayRef,
7573            ),
7574            // fixed_size_list<i32, 2> => large_list<i64>
7575            (
7576                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7577                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
7578                    2,
7579                )) as ArrayRef,
7580                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
7581                    Some([Some(1), Some(1)]),
7582                    Some([Some(2), Some(2)]),
7583                ])) as ArrayRef,
7584            ),
7585            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
7586            (
7587                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7588                    [None, Some([Some(2), Some(2)])],
7589                    2,
7590                )) as ArrayRef,
7591                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
7592                    None,
7593                    Some([Some(2), Some(2)]),
7594                ])) as ArrayRef,
7595            ),
7596        ];
7597
7598        for (array, expected) in cases {
7599            let array = Arc::new(array) as ArrayRef;
7600
7601            assert!(
7602                can_cast_types(array.data_type(), expected.data_type()),
7603                "can_cast_types claims we cannot cast {:?} to {:?}",
7604                array.data_type(),
7605                expected.data_type()
7606            );
7607
7608            let list_array = cast(&array, expected.data_type())
7609                .unwrap_or_else(|_| panic!("Failed to cast {:?} to {:?}", array, expected));
7610            assert_eq!(
7611                list_array.as_ref(),
7612                &expected,
7613                "Incorrect result from casting {:?} to {:?}",
7614                array,
7615                expected
7616            );
7617        }
7618    }
7619
7620    #[test]
7621    fn test_cast_utf8_to_list() {
7622        // DataType::List
7623        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
7624        let field = Arc::new(Field::new("", DataType::Int32, false));
7625        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
7626        let actual = list_array.as_list_opt::<i32>().unwrap();
7627        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
7628        assert_eq!(&expect.value(0), &actual.value(0));
7629
7630        // DataType::LargeList
7631        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
7632        let actual = list_array.as_list_opt::<i64>().unwrap();
7633        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
7634        assert_eq!(&expect.value(0), &actual.value(0));
7635
7636        // DataType::FixedSizeList
7637        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7638        let actual = list_array.as_fixed_size_list_opt().unwrap();
7639        let expect =
7640            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
7641        assert_eq!(&expect.value(0), &actual.value(0));
7642    }
7643
7644    #[test]
7645    fn test_cast_single_element_fixed_size_list() {
7646        // FixedSizeList<T>[1] => T
7647        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7648            [(Some([Some(5)]))],
7649            1,
7650        )) as ArrayRef;
7651        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
7652        let actual: &Int32Array = casted_array.as_primitive();
7653        let expected = Int32Array::from(vec![Some(5)]);
7654        assert_eq!(&expected, actual);
7655
7656        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
7657        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7658            [(Some([Some(5)]))],
7659            1,
7660        )) as ArrayRef;
7661        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
7662        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
7663        let expected = Arc::new(FixedSizeListArray::new(
7664            to_field.clone(),
7665            1,
7666            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7667            None,
7668        )) as ArrayRef;
7669        assert_eq!(*expected, *actual);
7670
7671        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
7672        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7673            [(Some([Some(5)]))],
7674            1,
7675        )) as ArrayRef;
7676        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
7677        let to_field = Arc::new(Field::new(
7678            "dummy",
7679            DataType::FixedSizeList(to_field_inner.clone(), 1),
7680            false,
7681        ));
7682        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
7683        let expected = Arc::new(FixedSizeListArray::new(
7684            to_field.clone(),
7685            1,
7686            Arc::new(FixedSizeListArray::new(
7687                to_field_inner.clone(),
7688                1,
7689                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7690                None,
7691            )) as ArrayRef,
7692            None,
7693        )) as ArrayRef;
7694        assert_eq!(*expected, *actual);
7695
7696        // T => FixedSizeList<T>[1] (non-nullable)
7697        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
7698        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
7699        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7700        let actual = casted_array.as_fixed_size_list();
7701        let expected = Arc::new(FixedSizeListArray::new(
7702            field.clone(),
7703            1,
7704            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7705            None,
7706        )) as ArrayRef;
7707        assert_eq!(expected.as_ref(), actual);
7708
7709        // T => FixedSizeList<T>[1] (nullable)
7710        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
7711        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
7712        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7713        let actual = casted_array.as_fixed_size_list();
7714        let expected = Arc::new(FixedSizeListArray::new(
7715            field.clone(),
7716            1,
7717            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
7718            None,
7719        )) as ArrayRef;
7720        assert_eq!(expected.as_ref(), actual);
7721    }
7722
7723    #[test]
7724    fn test_cast_list_containers() {
7725        // large-list to list
7726        let array = Arc::new(make_large_list_array()) as ArrayRef;
7727        let list_array = cast(
7728            &array,
7729            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
7730        )
7731        .unwrap();
7732        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
7733        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
7734
7735        assert_eq!(&expected.value(0), &actual.value(0));
7736        assert_eq!(&expected.value(1), &actual.value(1));
7737        assert_eq!(&expected.value(2), &actual.value(2));
7738
7739        // list to large-list
7740        let array = Arc::new(make_list_array()) as ArrayRef;
7741        let large_list_array = cast(
7742            &array,
7743            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
7744        )
7745        .unwrap();
7746        let actual = large_list_array
7747            .as_any()
7748            .downcast_ref::<LargeListArray>()
7749            .unwrap();
7750        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
7751
7752        assert_eq!(&expected.value(0), &actual.value(0));
7753        assert_eq!(&expected.value(1), &actual.value(1));
7754        assert_eq!(&expected.value(2), &actual.value(2));
7755    }
7756
7757    #[test]
7758    fn test_cast_list_to_fsl() {
7759        // There four noteworthy cases we should handle:
7760        // 1. No nulls
7761        // 2. Nulls that are always empty
7762        // 3. Nulls that have varying lengths
7763        // 4. Nulls that are correctly sized (same as target list size)
7764
7765        // Non-null case
7766        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
7767        let values = vec![
7768            Some(vec![Some(1), Some(2), Some(3)]),
7769            Some(vec![Some(4), Some(5), Some(6)]),
7770        ];
7771        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
7772            values.clone(),
7773        )) as ArrayRef;
7774        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7775            values, 3,
7776        )) as ArrayRef;
7777        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
7778        assert_eq!(expected.as_ref(), actual.as_ref());
7779
7780        // Null cases
7781        // Array is [[1, 2, 3], null, [4, 5, 6], null]
7782        let cases = [
7783            (
7784                // Zero-length nulls
7785                vec![1, 2, 3, 4, 5, 6],
7786                vec![3, 0, 3, 0],
7787            ),
7788            (
7789                // Varying-length nulls
7790                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
7791                vec![3, 2, 3, 1],
7792            ),
7793            (
7794                // Correctly-sized nulls
7795                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
7796                vec![3, 3, 3, 3],
7797            ),
7798            (
7799                // Mixed nulls
7800                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
7801                vec![3, 0, 3, 3],
7802            ),
7803        ];
7804        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
7805
7806        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7807            vec![
7808                Some(vec![Some(1), Some(2), Some(3)]),
7809                None,
7810                Some(vec![Some(4), Some(5), Some(6)]),
7811                None,
7812            ],
7813            3,
7814        )) as ArrayRef;
7815
7816        for (values, lengths) in cases.iter() {
7817            let array = Arc::new(ListArray::new(
7818                field.clone(),
7819                OffsetBuffer::from_lengths(lengths.clone()),
7820                Arc::new(Int32Array::from(values.clone())),
7821                Some(null_buffer.clone()),
7822            )) as ArrayRef;
7823            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
7824            assert_eq!(expected.as_ref(), actual.as_ref());
7825        }
7826    }
7827
7828    #[test]
7829    fn test_cast_list_to_fsl_safety() {
7830        let values = vec![
7831            Some(vec![Some(1), Some(2), Some(3)]),
7832            Some(vec![Some(4), Some(5)]),
7833            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
7834            Some(vec![Some(3), Some(4), Some(5)]),
7835        ];
7836        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
7837            values.clone(),
7838        )) as ArrayRef;
7839
7840        let res = cast_with_options(
7841            array.as_ref(),
7842            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7843            &CastOptions {
7844                safe: false,
7845                ..Default::default()
7846            },
7847        );
7848        assert!(res.is_err());
7849        assert!(format!("{:?}", res)
7850            .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2"));
7851
7852        // When safe=true (default), the cast will fill nulls for lists that are
7853        // too short and truncate lists that are too long.
7854        let res = cast(
7855            array.as_ref(),
7856            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7857        )
7858        .unwrap();
7859        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7860            vec![
7861                Some(vec![Some(1), Some(2), Some(3)]),
7862                None, // Too short -> replaced with null
7863                None, // Too long -> replaced with null
7864                Some(vec![Some(3), Some(4), Some(5)]),
7865            ],
7866            3,
7867        )) as ArrayRef;
7868        assert_eq!(expected.as_ref(), res.as_ref());
7869
7870        // The safe option is false and the source array contains a null list.
7871        // issue: https://github.com/apache/arrow-rs/issues/5642
7872        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
7873            Some(vec![Some(1), Some(2), Some(3)]),
7874            None,
7875        ])) as ArrayRef;
7876        let res = cast_with_options(
7877            array.as_ref(),
7878            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7879            &CastOptions {
7880                safe: false,
7881                ..Default::default()
7882            },
7883        )
7884        .unwrap();
7885        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7886            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
7887            3,
7888        )) as ArrayRef;
7889        assert_eq!(expected.as_ref(), res.as_ref());
7890    }
7891
7892    #[test]
7893    fn test_cast_large_list_to_fsl() {
7894        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
7895        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
7896            values.clone(),
7897        )) as ArrayRef;
7898        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7899            values, 2,
7900        )) as ArrayRef;
7901        let actual = cast(
7902            array.as_ref(),
7903            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
7904        )
7905        .unwrap();
7906        assert_eq!(expected.as_ref(), actual.as_ref());
7907    }
7908
7909    #[test]
7910    fn test_cast_list_to_fsl_subcast() {
7911        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
7912            vec![
7913                Some(vec![Some(1), Some(2)]),
7914                Some(vec![Some(3), Some(i32::MAX)]),
7915            ],
7916        )) as ArrayRef;
7917        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
7918            vec![
7919                Some(vec![Some(1), Some(2)]),
7920                Some(vec![Some(3), Some(i32::MAX as i64)]),
7921            ],
7922            2,
7923        )) as ArrayRef;
7924        let actual = cast(
7925            array.as_ref(),
7926            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
7927        )
7928        .unwrap();
7929        assert_eq!(expected.as_ref(), actual.as_ref());
7930
7931        let res = cast_with_options(
7932            array.as_ref(),
7933            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
7934            &CastOptions {
7935                safe: false,
7936                ..Default::default()
7937            },
7938        );
7939        assert!(res.is_err());
7940        assert!(format!("{:?}", res).contains("Can't cast value 2147483647 to type Int16"));
7941    }
7942
7943    #[test]
7944    fn test_cast_list_to_fsl_empty() {
7945        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
7946        let array = new_empty_array(&DataType::List(field.clone()));
7947
7948        let target_type = DataType::FixedSizeList(field.clone(), 3);
7949        let expected = new_empty_array(&target_type);
7950
7951        let actual = cast(array.as_ref(), &target_type).unwrap();
7952        assert_eq!(expected.as_ref(), actual.as_ref());
7953    }
7954
7955    fn make_list_array() -> ListArray {
7956        // Construct a value array
7957        let value_data = ArrayData::builder(DataType::Int32)
7958            .len(8)
7959            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
7960            .build()
7961            .unwrap();
7962
7963        // Construct a buffer for value offsets, for the nested array:
7964        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
7965        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
7966
7967        // Construct a list array from the above two
7968        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
7969        let list_data = ArrayData::builder(list_data_type)
7970            .len(3)
7971            .add_buffer(value_offsets)
7972            .add_child_data(value_data)
7973            .build()
7974            .unwrap();
7975        ListArray::from(list_data)
7976    }
7977
7978    fn make_large_list_array() -> LargeListArray {
7979        // Construct a value array
7980        let value_data = ArrayData::builder(DataType::Int32)
7981            .len(8)
7982            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
7983            .build()
7984            .unwrap();
7985
7986        // Construct a buffer for value offsets, for the nested array:
7987        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
7988        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
7989
7990        // Construct a list array from the above two
7991        let list_data_type =
7992            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
7993        let list_data = ArrayData::builder(list_data_type)
7994            .len(3)
7995            .add_buffer(value_offsets)
7996            .add_child_data(value_data)
7997            .build()
7998            .unwrap();
7999        LargeListArray::from(list_data)
8000    }
8001
8002    fn make_fixed_size_list_array() -> FixedSizeListArray {
8003        // Construct a value array
8004        let value_data = ArrayData::builder(DataType::Int32)
8005            .len(8)
8006            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8007            .build()
8008            .unwrap();
8009
8010        let list_data_type =
8011            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8012        let list_data = ArrayData::builder(list_data_type)
8013            .len(2)
8014            .add_child_data(value_data)
8015            .build()
8016            .unwrap();
8017        FixedSizeListArray::from(list_data)
8018    }
8019
8020    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8021        // Construct a value array
8022        let value_data = ArrayData::builder(DataType::Int64)
8023            .len(8)
8024            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8025            .build()
8026            .unwrap();
8027
8028        let list_data_type =
8029            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8030        let list_data = ArrayData::builder(list_data_type)
8031            .len(2)
8032            .add_child_data(value_data)
8033            .build()
8034            .unwrap();
8035        FixedSizeListArray::from(list_data)
8036    }
8037
8038    #[test]
8039    fn test_cast_map_dont_allow_change_of_order() {
8040        let string_builder = StringBuilder::new();
8041        let value_builder = StringBuilder::new();
8042        let mut builder = MapBuilder::new(
8043            Some(MapFieldNames {
8044                entry: "entries".to_string(),
8045                key: "key".to_string(),
8046                value: "value".to_string(),
8047            }),
8048            string_builder,
8049            value_builder,
8050        );
8051
8052        builder.keys().append_value("0");
8053        builder.values().append_value("test_val_1");
8054        builder.append(true).unwrap();
8055        builder.keys().append_value("1");
8056        builder.values().append_value("test_val_2");
8057        builder.append(true).unwrap();
8058
8059        // map builder returns unsorted map by default
8060        let array = builder.finish();
8061
8062        let new_ordered = true;
8063        let new_type = DataType::Map(
8064            Arc::new(Field::new(
8065                "entries",
8066                DataType::Struct(
8067                    vec![
8068                        Field::new("key", DataType::Utf8, false),
8069                        Field::new("value", DataType::Utf8, false),
8070                    ]
8071                    .into(),
8072                ),
8073                false,
8074            )),
8075            new_ordered,
8076        );
8077
8078        let new_array_result = cast(&array, &new_type.clone());
8079        assert!(!can_cast_types(array.data_type(), &new_type));
8080        assert!(
8081            matches!(new_array_result, Err(ArrowError::CastError(t)) if t == r#"Casting from Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, false) to Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, true) not supported"#)
8082        );
8083    }
8084
8085    #[test]
8086    fn test_cast_map_dont_allow_when_container_cant_cast() {
8087        let string_builder = StringBuilder::new();
8088        let value_builder = IntervalDayTimeArray::builder(2);
8089        let mut builder = MapBuilder::new(
8090            Some(MapFieldNames {
8091                entry: "entries".to_string(),
8092                key: "key".to_string(),
8093                value: "value".to_string(),
8094            }),
8095            string_builder,
8096            value_builder,
8097        );
8098
8099        builder.keys().append_value("0");
8100        builder.values().append_value(IntervalDayTime::new(1, 1));
8101        builder.append(true).unwrap();
8102        builder.keys().append_value("1");
8103        builder.values().append_value(IntervalDayTime::new(2, 2));
8104        builder.append(true).unwrap();
8105
8106        // map builder returns unsorted map by default
8107        let array = builder.finish();
8108
8109        let new_ordered = true;
8110        let new_type = DataType::Map(
8111            Arc::new(Field::new(
8112                "entries",
8113                DataType::Struct(
8114                    vec![
8115                        Field::new("key", DataType::Utf8, false),
8116                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
8117                    ]
8118                    .into(),
8119                ),
8120                false,
8121            )),
8122            new_ordered,
8123        );
8124
8125        let new_array_result = cast(&array, &new_type.clone());
8126        assert!(!can_cast_types(array.data_type(), &new_type));
8127        assert!(
8128            matches!(new_array_result, Err(ArrowError::CastError(t)) if t == r#"Casting from Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Interval(DayTime), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, false) to Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Duration(Second), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, true) not supported"#)
8129        );
8130    }
8131
8132    #[test]
8133    fn test_cast_map_field_names() {
8134        let string_builder = StringBuilder::new();
8135        let value_builder = StringBuilder::new();
8136        let mut builder = MapBuilder::new(
8137            Some(MapFieldNames {
8138                entry: "entries".to_string(),
8139                key: "key".to_string(),
8140                value: "value".to_string(),
8141            }),
8142            string_builder,
8143            value_builder,
8144        );
8145
8146        builder.keys().append_value("0");
8147        builder.values().append_value("test_val_1");
8148        builder.append(true).unwrap();
8149        builder.keys().append_value("1");
8150        builder.values().append_value("test_val_2");
8151        builder.append(true).unwrap();
8152        builder.append(false).unwrap();
8153
8154        let array = builder.finish();
8155
8156        let new_type = DataType::Map(
8157            Arc::new(Field::new(
8158                "entries_new",
8159                DataType::Struct(
8160                    vec![
8161                        Field::new("key_new", DataType::Utf8, false),
8162                        Field::new("value_values", DataType::Utf8, false),
8163                    ]
8164                    .into(),
8165                ),
8166                false,
8167            )),
8168            false,
8169        );
8170
8171        assert_ne!(new_type, array.data_type().clone());
8172
8173        let new_array = cast(&array, &new_type.clone()).unwrap();
8174        assert_eq!(new_type, new_array.data_type().clone());
8175        let map_array = new_array.as_map();
8176
8177        assert_ne!(new_type, array.data_type().clone());
8178        assert_eq!(new_type, map_array.data_type().clone());
8179
8180        let key_string = map_array
8181            .keys()
8182            .as_any()
8183            .downcast_ref::<StringArray>()
8184            .unwrap()
8185            .into_iter()
8186            .flatten()
8187            .collect::<Vec<_>>();
8188        assert_eq!(&key_string, &vec!["0", "1"]);
8189
8190        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8191        let values_string = values_string_array
8192            .as_any()
8193            .downcast_ref::<StringArray>()
8194            .unwrap()
8195            .into_iter()
8196            .flatten()
8197            .collect::<Vec<_>>();
8198        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
8199
8200        assert_eq!(
8201            map_array.nulls(),
8202            Some(&NullBuffer::from(vec![true, true, false]))
8203        );
8204    }
8205
8206    #[test]
8207    fn test_cast_map_contained_values() {
8208        let string_builder = StringBuilder::new();
8209        let value_builder = Int8Builder::new();
8210        let mut builder = MapBuilder::new(
8211            Some(MapFieldNames {
8212                entry: "entries".to_string(),
8213                key: "key".to_string(),
8214                value: "value".to_string(),
8215            }),
8216            string_builder,
8217            value_builder,
8218        );
8219
8220        builder.keys().append_value("0");
8221        builder.values().append_value(44);
8222        builder.append(true).unwrap();
8223        builder.keys().append_value("1");
8224        builder.values().append_value(22);
8225        builder.append(true).unwrap();
8226
8227        let array = builder.finish();
8228
8229        let new_type = DataType::Map(
8230            Arc::new(Field::new(
8231                "entries",
8232                DataType::Struct(
8233                    vec![
8234                        Field::new("key", DataType::Utf8, false),
8235                        Field::new("value", DataType::Utf8, false),
8236                    ]
8237                    .into(),
8238                ),
8239                false,
8240            )),
8241            false,
8242        );
8243
8244        let new_array = cast(&array, &new_type.clone()).unwrap();
8245        assert_eq!(new_type, new_array.data_type().clone());
8246        let map_array = new_array.as_map();
8247
8248        assert_ne!(new_type, array.data_type().clone());
8249        assert_eq!(new_type, map_array.data_type().clone());
8250
8251        let key_string = map_array
8252            .keys()
8253            .as_any()
8254            .downcast_ref::<StringArray>()
8255            .unwrap()
8256            .into_iter()
8257            .flatten()
8258            .collect::<Vec<_>>();
8259        assert_eq!(&key_string, &vec!["0", "1"]);
8260
8261        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8262        let values_string = values_string_array
8263            .as_any()
8264            .downcast_ref::<StringArray>()
8265            .unwrap()
8266            .into_iter()
8267            .flatten()
8268            .collect::<Vec<_>>();
8269        assert_eq!(&values_string, &vec!["44", "22"]);
8270    }
8271
8272    #[test]
8273    fn test_utf8_cast_offsets() {
8274        // test if offset of the array is taken into account during cast
8275        let str_array = StringArray::from(vec!["a", "b", "c"]);
8276        let str_array = str_array.slice(1, 2);
8277
8278        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
8279
8280        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
8281        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
8282        assert_eq!(strs, &["b", "c"])
8283    }
8284
8285    #[test]
8286    fn test_list_cast_offsets() {
8287        // test if offset of the array is taken into account during cast
8288        let array1 = make_list_array().slice(1, 2);
8289        let array2 = Arc::new(make_list_array()) as ArrayRef;
8290
8291        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8292        let out1 = cast(&array1, &dt).unwrap();
8293        let out2 = cast(&array2, &dt).unwrap();
8294
8295        assert_eq!(&out1, &out2.slice(1, 2))
8296    }
8297
8298    #[test]
8299    fn test_list_to_string() {
8300        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
8301        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8302        let value_data = str_array.into_data();
8303
8304        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
8305        let list_data = ArrayData::builder(list_data_type)
8306            .len(3)
8307            .add_buffer(value_offsets)
8308            .add_child_data(value_data)
8309            .build()
8310            .unwrap();
8311        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
8312
8313        let out = cast(&array, &DataType::Utf8).unwrap();
8314        let out = out
8315            .as_any()
8316            .downcast_ref::<StringArray>()
8317            .unwrap()
8318            .into_iter()
8319            .flatten()
8320            .collect::<Vec<_>>();
8321        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8322
8323        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8324        let out = out
8325            .as_any()
8326            .downcast_ref::<LargeStringArray>()
8327            .unwrap()
8328            .into_iter()
8329            .flatten()
8330            .collect::<Vec<_>>();
8331        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8332
8333        let array = Arc::new(make_list_array()) as ArrayRef;
8334        let out = cast(&array, &DataType::Utf8).unwrap();
8335        let out = out
8336            .as_any()
8337            .downcast_ref::<StringArray>()
8338            .unwrap()
8339            .into_iter()
8340            .flatten()
8341            .collect::<Vec<_>>();
8342        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8343
8344        let array = Arc::new(make_large_list_array()) as ArrayRef;
8345        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8346        let out = out
8347            .as_any()
8348            .downcast_ref::<LargeStringArray>()
8349            .unwrap()
8350            .into_iter()
8351            .flatten()
8352            .collect::<Vec<_>>();
8353        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8354    }
8355
8356    #[test]
8357    fn test_cast_f64_to_decimal128() {
8358        // to reproduce https://github.com/apache/arrow-rs/issues/2997
8359
8360        let decimal_type = DataType::Decimal128(18, 2);
8361        let array = Float64Array::from(vec![
8362            Some(0.0699999999),
8363            Some(0.0659999999),
8364            Some(0.0650000000),
8365            Some(0.0649999999),
8366        ]);
8367        let array = Arc::new(array) as ArrayRef;
8368        generate_cast_test_case!(
8369            &array,
8370            Decimal128Array,
8371            &decimal_type,
8372            vec![
8373                Some(7_i128), // round up
8374                Some(7_i128), // round up
8375                Some(7_i128), // round up
8376                Some(6_i128), // round down
8377            ]
8378        );
8379
8380        let decimal_type = DataType::Decimal128(18, 3);
8381        let array = Float64Array::from(vec![
8382            Some(0.0699999999),
8383            Some(0.0659999999),
8384            Some(0.0650000000),
8385            Some(0.0649999999),
8386        ]);
8387        let array = Arc::new(array) as ArrayRef;
8388        generate_cast_test_case!(
8389            &array,
8390            Decimal128Array,
8391            &decimal_type,
8392            vec![
8393                Some(70_i128), // round up
8394                Some(66_i128), // round up
8395                Some(65_i128), // round down
8396                Some(65_i128), // round up
8397            ]
8398        );
8399    }
8400
8401    #[test]
8402    fn test_cast_numeric_to_decimal128_overflow() {
8403        let array = Int64Array::from(vec![i64::MAX]);
8404        let array = Arc::new(array) as ArrayRef;
8405        let casted_array = cast_with_options(
8406            &array,
8407            &DataType::Decimal128(38, 30),
8408            &CastOptions {
8409                safe: true,
8410                format_options: FormatOptions::default(),
8411            },
8412        );
8413        assert!(casted_array.is_ok());
8414        assert!(casted_array.unwrap().is_null(0));
8415
8416        let casted_array = cast_with_options(
8417            &array,
8418            &DataType::Decimal128(38, 30),
8419            &CastOptions {
8420                safe: false,
8421                format_options: FormatOptions::default(),
8422            },
8423        );
8424        assert!(casted_array.is_err());
8425    }
8426
8427    #[test]
8428    fn test_cast_numeric_to_decimal256_overflow() {
8429        let array = Int64Array::from(vec![i64::MAX]);
8430        let array = Arc::new(array) as ArrayRef;
8431        let casted_array = cast_with_options(
8432            &array,
8433            &DataType::Decimal256(76, 76),
8434            &CastOptions {
8435                safe: true,
8436                format_options: FormatOptions::default(),
8437            },
8438        );
8439        assert!(casted_array.is_ok());
8440        assert!(casted_array.unwrap().is_null(0));
8441
8442        let casted_array = cast_with_options(
8443            &array,
8444            &DataType::Decimal256(76, 76),
8445            &CastOptions {
8446                safe: false,
8447                format_options: FormatOptions::default(),
8448            },
8449        );
8450        assert!(casted_array.is_err());
8451    }
8452
8453    #[test]
8454    fn test_cast_floating_point_to_decimal128_precision_overflow() {
8455        let array = Float64Array::from(vec![1.1]);
8456        let array = Arc::new(array) as ArrayRef;
8457        let casted_array = cast_with_options(
8458            &array,
8459            &DataType::Decimal128(2, 2),
8460            &CastOptions {
8461                safe: true,
8462                format_options: FormatOptions::default(),
8463            },
8464        );
8465        assert!(casted_array.is_ok());
8466        assert!(casted_array.unwrap().is_null(0));
8467
8468        let casted_array = cast_with_options(
8469            &array,
8470            &DataType::Decimal128(2, 2),
8471            &CastOptions {
8472                safe: false,
8473                format_options: FormatOptions::default(),
8474            },
8475        );
8476        let err = casted_array.unwrap_err().to_string();
8477        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal128 of precision 2. Max is 99";
8478        assert!(
8479            err.contains(expected_error),
8480            "did not find expected error '{expected_error}' in actual error '{err}'"
8481        );
8482    }
8483
8484    #[test]
8485    fn test_cast_floating_point_to_decimal256_precision_overflow() {
8486        let array = Float64Array::from(vec![1.1]);
8487        let array = Arc::new(array) as ArrayRef;
8488        let casted_array = cast_with_options(
8489            &array,
8490            &DataType::Decimal256(2, 2),
8491            &CastOptions {
8492                safe: true,
8493                format_options: FormatOptions::default(),
8494            },
8495        );
8496        assert!(casted_array.is_ok());
8497        assert!(casted_array.unwrap().is_null(0));
8498
8499        let casted_array = cast_with_options(
8500            &array,
8501            &DataType::Decimal256(2, 2),
8502            &CastOptions {
8503                safe: false,
8504                format_options: FormatOptions::default(),
8505            },
8506        );
8507        let err = casted_array.unwrap_err().to_string();
8508        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal256 of precision 2. Max is 99";
8509        assert!(
8510            err.contains(expected_error),
8511            "did not find expected error '{expected_error}' in actual error '{err}'"
8512        );
8513    }
8514
8515    #[test]
8516    fn test_cast_floating_point_to_decimal128_overflow() {
8517        let array = Float64Array::from(vec![f64::MAX]);
8518        let array = Arc::new(array) as ArrayRef;
8519        let casted_array = cast_with_options(
8520            &array,
8521            &DataType::Decimal128(38, 30),
8522            &CastOptions {
8523                safe: true,
8524                format_options: FormatOptions::default(),
8525            },
8526        );
8527        assert!(casted_array.is_ok());
8528        assert!(casted_array.unwrap().is_null(0));
8529
8530        let casted_array = cast_with_options(
8531            &array,
8532            &DataType::Decimal128(38, 30),
8533            &CastOptions {
8534                safe: false,
8535                format_options: FormatOptions::default(),
8536            },
8537        );
8538        let err = casted_array.unwrap_err().to_string();
8539        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
8540        assert!(
8541            err.contains(expected_error),
8542            "did not find expected error '{expected_error}' in actual error '{err}'"
8543        );
8544    }
8545
8546    #[test]
8547    fn test_cast_floating_point_to_decimal256_overflow() {
8548        let array = Float64Array::from(vec![f64::MAX]);
8549        let array = Arc::new(array) as ArrayRef;
8550        let casted_array = cast_with_options(
8551            &array,
8552            &DataType::Decimal256(76, 50),
8553            &CastOptions {
8554                safe: true,
8555                format_options: FormatOptions::default(),
8556            },
8557        );
8558        assert!(casted_array.is_ok());
8559        assert!(casted_array.unwrap().is_null(0));
8560
8561        let casted_array = cast_with_options(
8562            &array,
8563            &DataType::Decimal256(76, 50),
8564            &CastOptions {
8565                safe: false,
8566                format_options: FormatOptions::default(),
8567            },
8568        );
8569        let err = casted_array.unwrap_err().to_string();
8570        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
8571        assert!(
8572            err.contains(expected_error),
8573            "did not find expected error '{expected_error}' in actual error '{err}'"
8574        );
8575    }
8576
8577    #[test]
8578    fn test_cast_decimal128_to_decimal128_negative_scale() {
8579        let input_type = DataType::Decimal128(20, 0);
8580        let output_type = DataType::Decimal128(20, -1);
8581        assert!(can_cast_types(&input_type, &output_type));
8582        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
8583        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
8584        let array = Arc::new(input_decimal_array) as ArrayRef;
8585        generate_cast_test_case!(
8586            &array,
8587            Decimal128Array,
8588            &output_type,
8589            vec![
8590                Some(112345_i128),
8591                Some(212346_i128),
8592                Some(312346_i128),
8593                None
8594            ]
8595        );
8596
8597        let casted_array = cast(&array, &output_type).unwrap();
8598        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8599
8600        assert_eq!("1123450", decimal_arr.value_as_string(0));
8601        assert_eq!("2123460", decimal_arr.value_as_string(1));
8602        assert_eq!("3123460", decimal_arr.value_as_string(2));
8603    }
8604
8605    #[test]
8606    fn test_cast_numeric_to_decimal128_negative() {
8607        let decimal_type = DataType::Decimal128(38, -1);
8608        let array = Arc::new(Int32Array::from(vec![
8609            Some(1123456),
8610            Some(2123456),
8611            Some(3123456),
8612        ])) as ArrayRef;
8613
8614        let casted_array = cast(&array, &decimal_type).unwrap();
8615        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8616
8617        assert_eq!("1123450", decimal_arr.value_as_string(0));
8618        assert_eq!("2123450", decimal_arr.value_as_string(1));
8619        assert_eq!("3123450", decimal_arr.value_as_string(2));
8620
8621        let array = Arc::new(Float32Array::from(vec![
8622            Some(1123.456),
8623            Some(2123.456),
8624            Some(3123.456),
8625        ])) as ArrayRef;
8626
8627        let casted_array = cast(&array, &decimal_type).unwrap();
8628        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8629
8630        assert_eq!("1120", decimal_arr.value_as_string(0));
8631        assert_eq!("2120", decimal_arr.value_as_string(1));
8632        assert_eq!("3120", decimal_arr.value_as_string(2));
8633    }
8634
8635    #[test]
8636    fn test_cast_decimal128_to_decimal128_negative() {
8637        let input_type = DataType::Decimal128(10, -1);
8638        let output_type = DataType::Decimal128(10, -2);
8639        assert!(can_cast_types(&input_type, &output_type));
8640        let array = vec![Some(123)];
8641        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
8642        let array = Arc::new(input_decimal_array) as ArrayRef;
8643        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
8644
8645        let casted_array = cast(&array, &output_type).unwrap();
8646        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8647
8648        assert_eq!("1200", decimal_arr.value_as_string(0));
8649
8650        let array = vec![Some(125)];
8651        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
8652        let array = Arc::new(input_decimal_array) as ArrayRef;
8653        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
8654
8655        let casted_array = cast(&array, &output_type).unwrap();
8656        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8657
8658        assert_eq!("1300", decimal_arr.value_as_string(0));
8659    }
8660
8661    #[test]
8662    fn test_cast_decimal128_to_decimal256_negative() {
8663        let input_type = DataType::Decimal128(10, 3);
8664        let output_type = DataType::Decimal256(10, 5);
8665        assert!(can_cast_types(&input_type, &output_type));
8666        let array = vec![Some(123456), Some(-123456)];
8667        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
8668        let array = Arc::new(input_decimal_array) as ArrayRef;
8669
8670        let hundred = i256::from_i128(100);
8671        generate_cast_test_case!(
8672            &array,
8673            Decimal256Array,
8674            &output_type,
8675            vec![
8676                Some(i256::from_i128(123456).mul_wrapping(hundred)),
8677                Some(i256::from_i128(-123456).mul_wrapping(hundred))
8678            ]
8679        );
8680    }
8681
8682    #[test]
8683    fn test_parse_string_to_decimal() {
8684        assert_eq!(
8685            Decimal128Type::format_decimal(
8686                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
8687                38,
8688                2,
8689            ),
8690            "123.45"
8691        );
8692        assert_eq!(
8693            Decimal128Type::format_decimal(
8694                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
8695                38,
8696                2,
8697            ),
8698            "12345.00"
8699        );
8700        assert_eq!(
8701            Decimal128Type::format_decimal(
8702                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
8703                38,
8704                2,
8705            ),
8706            "0.12"
8707        );
8708        assert_eq!(
8709            Decimal128Type::format_decimal(
8710                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
8711                38,
8712                2,
8713            ),
8714            "0.12"
8715        );
8716        assert_eq!(
8717            Decimal128Type::format_decimal(
8718                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
8719                38,
8720                2,
8721            ),
8722            "0.13"
8723        );
8724        assert_eq!(
8725            Decimal128Type::format_decimal(
8726                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
8727                38,
8728                2,
8729            ),
8730            "0.13"
8731        );
8732
8733        assert_eq!(
8734            Decimal256Type::format_decimal(
8735                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
8736                38,
8737                3,
8738            ),
8739            "123.450"
8740        );
8741        assert_eq!(
8742            Decimal256Type::format_decimal(
8743                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
8744                38,
8745                3,
8746            ),
8747            "12345.000"
8748        );
8749        assert_eq!(
8750            Decimal256Type::format_decimal(
8751                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
8752                38,
8753                3,
8754            ),
8755            "0.123"
8756        );
8757        assert_eq!(
8758            Decimal256Type::format_decimal(
8759                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
8760                38,
8761                3,
8762            ),
8763            "0.123"
8764        );
8765        assert_eq!(
8766            Decimal256Type::format_decimal(
8767                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
8768                38,
8769                3,
8770            ),
8771            "0.127"
8772        );
8773    }
8774
8775    fn test_cast_string_to_decimal(array: ArrayRef) {
8776        // Decimal128
8777        let output_type = DataType::Decimal128(38, 2);
8778        assert!(can_cast_types(array.data_type(), &output_type));
8779
8780        let casted_array = cast(&array, &output_type).unwrap();
8781        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8782
8783        assert_eq!("123.45", decimal_arr.value_as_string(0));
8784        assert_eq!("1.23", decimal_arr.value_as_string(1));
8785        assert_eq!("0.12", decimal_arr.value_as_string(2));
8786        assert_eq!("0.13", decimal_arr.value_as_string(3));
8787        assert_eq!("1.26", decimal_arr.value_as_string(4));
8788        assert_eq!("12345.00", decimal_arr.value_as_string(5));
8789        assert_eq!("12345.00", decimal_arr.value_as_string(6));
8790        assert_eq!("0.12", decimal_arr.value_as_string(7));
8791        assert_eq!("12.23", decimal_arr.value_as_string(8));
8792        assert!(decimal_arr.is_null(9));
8793        assert_eq!("0.00", decimal_arr.value_as_string(10));
8794        assert_eq!("0.00", decimal_arr.value_as_string(11));
8795        assert!(decimal_arr.is_null(12));
8796        assert_eq!("-1.23", decimal_arr.value_as_string(13));
8797        assert_eq!("-1.24", decimal_arr.value_as_string(14));
8798        assert_eq!("0.00", decimal_arr.value_as_string(15));
8799        assert_eq!("-123.00", decimal_arr.value_as_string(16));
8800        assert_eq!("-123.23", decimal_arr.value_as_string(17));
8801        assert_eq!("-0.12", decimal_arr.value_as_string(18));
8802        assert_eq!("1.23", decimal_arr.value_as_string(19));
8803        assert_eq!("1.24", decimal_arr.value_as_string(20));
8804        assert_eq!("0.00", decimal_arr.value_as_string(21));
8805        assert_eq!("123.00", decimal_arr.value_as_string(22));
8806        assert_eq!("123.23", decimal_arr.value_as_string(23));
8807        assert_eq!("0.12", decimal_arr.value_as_string(24));
8808        assert!(decimal_arr.is_null(25));
8809        assert!(decimal_arr.is_null(26));
8810        assert!(decimal_arr.is_null(27));
8811        assert_eq!("0.00", decimal_arr.value_as_string(28));
8812        assert_eq!("0.00", decimal_arr.value_as_string(29));
8813        assert_eq!("12345.00", decimal_arr.value_as_string(30));
8814        assert_eq!(decimal_arr.len(), 31);
8815
8816        // Decimal256
8817        let output_type = DataType::Decimal256(76, 3);
8818        assert!(can_cast_types(array.data_type(), &output_type));
8819
8820        let casted_array = cast(&array, &output_type).unwrap();
8821        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
8822
8823        assert_eq!("123.450", decimal_arr.value_as_string(0));
8824        assert_eq!("1.235", decimal_arr.value_as_string(1));
8825        assert_eq!("0.123", decimal_arr.value_as_string(2));
8826        assert_eq!("0.127", decimal_arr.value_as_string(3));
8827        assert_eq!("1.263", decimal_arr.value_as_string(4));
8828        assert_eq!("12345.000", decimal_arr.value_as_string(5));
8829        assert_eq!("12345.000", decimal_arr.value_as_string(6));
8830        assert_eq!("0.123", decimal_arr.value_as_string(7));
8831        assert_eq!("12.234", decimal_arr.value_as_string(8));
8832        assert!(decimal_arr.is_null(9));
8833        assert_eq!("0.000", decimal_arr.value_as_string(10));
8834        assert_eq!("0.000", decimal_arr.value_as_string(11));
8835        assert!(decimal_arr.is_null(12));
8836        assert_eq!("-1.235", decimal_arr.value_as_string(13));
8837        assert_eq!("-1.236", decimal_arr.value_as_string(14));
8838        assert_eq!("0.000", decimal_arr.value_as_string(15));
8839        assert_eq!("-123.000", decimal_arr.value_as_string(16));
8840        assert_eq!("-123.234", decimal_arr.value_as_string(17));
8841        assert_eq!("-0.123", decimal_arr.value_as_string(18));
8842        assert_eq!("1.235", decimal_arr.value_as_string(19));
8843        assert_eq!("1.236", decimal_arr.value_as_string(20));
8844        assert_eq!("0.000", decimal_arr.value_as_string(21));
8845        assert_eq!("123.000", decimal_arr.value_as_string(22));
8846        assert_eq!("123.234", decimal_arr.value_as_string(23));
8847        assert_eq!("0.123", decimal_arr.value_as_string(24));
8848        assert!(decimal_arr.is_null(25));
8849        assert!(decimal_arr.is_null(26));
8850        assert!(decimal_arr.is_null(27));
8851        assert_eq!("0.000", decimal_arr.value_as_string(28));
8852        assert_eq!("0.000", decimal_arr.value_as_string(29));
8853        assert_eq!("12345.000", decimal_arr.value_as_string(30));
8854        assert_eq!(decimal_arr.len(), 31);
8855    }
8856
8857    #[test]
8858    fn test_cast_utf8_to_decimal() {
8859        let str_array = StringArray::from(vec![
8860            Some("123.45"),
8861            Some("1.2345"),
8862            Some("0.12345"),
8863            Some("0.1267"),
8864            Some("1.263"),
8865            Some("12345.0"),
8866            Some("12345"),
8867            Some("000.123"),
8868            Some("12.234000"),
8869            None,
8870            Some(""),
8871            Some(" "),
8872            None,
8873            Some("-1.23499999"),
8874            Some("-1.23599999"),
8875            Some("-0.00001"),
8876            Some("-123"),
8877            Some("-123.234000"),
8878            Some("-000.123"),
8879            Some("+1.23499999"),
8880            Some("+1.23599999"),
8881            Some("+0.00001"),
8882            Some("+123"),
8883            Some("+123.234000"),
8884            Some("+000.123"),
8885            Some("1.-23499999"),
8886            Some("-1.-23499999"),
8887            Some("--1.23499999"),
8888            Some("0"),
8889            Some("000.000"),
8890            Some("0000000000000000012345.000"),
8891        ]);
8892        let array = Arc::new(str_array) as ArrayRef;
8893
8894        test_cast_string_to_decimal(array);
8895
8896        let test_cases = [
8897            (None, None),
8898            // (Some(""), None),
8899            // (Some("   "), None),
8900            (Some("0"), Some("0")),
8901            (Some("000.000"), Some("0")),
8902            (Some("12345"), Some("12345")),
8903            (Some("000000000000000000000000000012345"), Some("12345")),
8904            (Some("-123"), Some("-123")),
8905            (Some("+123"), Some("123")),
8906        ];
8907        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
8908        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
8909
8910        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
8911        test_cast_string_to_decimal_scale_zero(array, &expected);
8912    }
8913
8914    #[test]
8915    fn test_cast_large_utf8_to_decimal() {
8916        let str_array = LargeStringArray::from(vec![
8917            Some("123.45"),
8918            Some("1.2345"),
8919            Some("0.12345"),
8920            Some("0.1267"),
8921            Some("1.263"),
8922            Some("12345.0"),
8923            Some("12345"),
8924            Some("000.123"),
8925            Some("12.234000"),
8926            None,
8927            Some(""),
8928            Some(" "),
8929            None,
8930            Some("-1.23499999"),
8931            Some("-1.23599999"),
8932            Some("-0.00001"),
8933            Some("-123"),
8934            Some("-123.234000"),
8935            Some("-000.123"),
8936            Some("+1.23499999"),
8937            Some("+1.23599999"),
8938            Some("+0.00001"),
8939            Some("+123"),
8940            Some("+123.234000"),
8941            Some("+000.123"),
8942            Some("1.-23499999"),
8943            Some("-1.-23499999"),
8944            Some("--1.23499999"),
8945            Some("0"),
8946            Some("000.000"),
8947            Some("0000000000000000012345.000"),
8948        ]);
8949        let array = Arc::new(str_array) as ArrayRef;
8950
8951        test_cast_string_to_decimal(array);
8952
8953        let test_cases = [
8954            (None, None),
8955            (Some(""), None),
8956            (Some("   "), None),
8957            (Some("0"), Some("0")),
8958            (Some("000.000"), Some("0")),
8959            (Some("12345"), Some("12345")),
8960            (Some("000000000000000000000000000012345"), Some("12345")),
8961            (Some("-123"), Some("-123")),
8962            (Some("+123"), Some("123")),
8963        ];
8964        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
8965        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
8966
8967        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
8968        test_cast_string_to_decimal_scale_zero(array, &expected);
8969    }
8970
8971    fn test_cast_string_to_decimal_scale_zero(
8972        array: ArrayRef,
8973        expected_as_string: &[Option<&str>],
8974    ) {
8975        // Decimal128
8976        let output_type = DataType::Decimal128(38, 0);
8977        assert!(can_cast_types(array.data_type(), &output_type));
8978        let casted_array = cast(&array, &output_type).unwrap();
8979        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8980        assert_decimal_array_contents(decimal_arr, expected_as_string);
8981
8982        // Decimal256
8983        let output_type = DataType::Decimal256(76, 0);
8984        assert!(can_cast_types(array.data_type(), &output_type));
8985        let casted_array = cast(&array, &output_type).unwrap();
8986        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
8987        assert_decimal_array_contents(decimal_arr, expected_as_string);
8988    }
8989
8990    fn assert_decimal_array_contents<T>(
8991        array: &PrimitiveArray<T>,
8992        expected_as_string: &[Option<&str>],
8993    ) where
8994        T: DecimalType + ArrowPrimitiveType,
8995    {
8996        assert_eq!(array.len(), expected_as_string.len());
8997        for (i, expected) in expected_as_string.iter().enumerate() {
8998            let actual = if array.is_null(i) {
8999                None
9000            } else {
9001                Some(array.value_as_string(i))
9002            };
9003            let actual = actual.as_ref().map(|s| s.as_ref());
9004            assert_eq!(*expected, actual, "Expected at position {}", i);
9005        }
9006    }
9007
9008    #[test]
9009    fn test_cast_invalid_utf8_to_decimal() {
9010        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9011        let array = Arc::new(str_array) as ArrayRef;
9012
9013        // Safe cast
9014        let output_type = DataType::Decimal128(38, 2);
9015        let casted_array = cast(&array, &output_type).unwrap();
9016        assert!(casted_array.is_null(0));
9017        assert!(casted_array.is_null(1));
9018
9019        let output_type = DataType::Decimal256(76, 2);
9020        let casted_array = cast(&array, &output_type).unwrap();
9021        assert!(casted_array.is_null(0));
9022        assert!(casted_array.is_null(1));
9023
9024        // Non-safe cast
9025        let output_type = DataType::Decimal128(38, 2);
9026        let str_array = StringArray::from(vec!["4.4.5"]);
9027        let array = Arc::new(str_array) as ArrayRef;
9028        let option = CastOptions {
9029            safe: false,
9030            format_options: FormatOptions::default(),
9031        };
9032        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9033        assert!(casted_err
9034            .to_string()
9035            .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type"));
9036
9037        let str_array = StringArray::from(vec![". 0.123"]);
9038        let array = Arc::new(str_array) as ArrayRef;
9039        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9040        assert!(casted_err
9041            .to_string()
9042            .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type"));
9043    }
9044
9045    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
9046        let output_type = DataType::Decimal128(38, 2);
9047        let casted_array = cast(&overflow_array, &output_type).unwrap();
9048        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9049
9050        assert!(decimal_arr.is_null(0));
9051        assert!(decimal_arr.is_null(1));
9052        assert!(decimal_arr.is_null(2));
9053        assert_eq!(
9054            "999999999999999999999999999999999999.99",
9055            decimal_arr.value_as_string(3)
9056        );
9057        assert_eq!(
9058            "100000000000000000000000000000000000.00",
9059            decimal_arr.value_as_string(4)
9060        );
9061    }
9062
9063    #[test]
9064    fn test_cast_string_to_decimal128_precision_overflow() {
9065        let array = StringArray::from(vec!["1000".to_string()]);
9066        let array = Arc::new(array) as ArrayRef;
9067        let casted_array = cast_with_options(
9068            &array,
9069            &DataType::Decimal128(10, 8),
9070            &CastOptions {
9071                safe: true,
9072                format_options: FormatOptions::default(),
9073            },
9074        );
9075        assert!(casted_array.is_ok());
9076        assert!(casted_array.unwrap().is_null(0));
9077
9078        let err = cast_with_options(
9079            &array,
9080            &DataType::Decimal128(10, 8),
9081            &CastOptions {
9082                safe: false,
9083                format_options: FormatOptions::default(),
9084            },
9085        );
9086        assert_eq!("Invalid argument error: 100000000000 is too large to store in a Decimal128 of precision 10. Max is 9999999999", err.unwrap_err().to_string());
9087    }
9088
9089    #[test]
9090    fn test_cast_utf8_to_decimal128_overflow() {
9091        let overflow_str_array = StringArray::from(vec![
9092            i128::MAX.to_string(),
9093            i128::MIN.to_string(),
9094            "99999999999999999999999999999999999999".to_string(),
9095            "999999999999999999999999999999999999.99".to_string(),
9096            "99999999999999999999999999999999999.999".to_string(),
9097        ]);
9098        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9099
9100        test_cast_string_to_decimal128_overflow(overflow_array);
9101    }
9102
9103    #[test]
9104    fn test_cast_large_utf8_to_decimal128_overflow() {
9105        let overflow_str_array = LargeStringArray::from(vec![
9106            i128::MAX.to_string(),
9107            i128::MIN.to_string(),
9108            "99999999999999999999999999999999999999".to_string(),
9109            "999999999999999999999999999999999999.99".to_string(),
9110            "99999999999999999999999999999999999.999".to_string(),
9111        ]);
9112        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9113
9114        test_cast_string_to_decimal128_overflow(overflow_array);
9115    }
9116
9117    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
9118        let output_type = DataType::Decimal256(76, 2);
9119        let casted_array = cast(&overflow_array, &output_type).unwrap();
9120        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9121
9122        assert_eq!(
9123            "170141183460469231731687303715884105727.00",
9124            decimal_arr.value_as_string(0)
9125        );
9126        assert_eq!(
9127            "-170141183460469231731687303715884105728.00",
9128            decimal_arr.value_as_string(1)
9129        );
9130        assert_eq!(
9131            "99999999999999999999999999999999999999.00",
9132            decimal_arr.value_as_string(2)
9133        );
9134        assert_eq!(
9135            "999999999999999999999999999999999999.99",
9136            decimal_arr.value_as_string(3)
9137        );
9138        assert_eq!(
9139            "100000000000000000000000000000000000.00",
9140            decimal_arr.value_as_string(4)
9141        );
9142        assert!(decimal_arr.is_null(5));
9143        assert!(decimal_arr.is_null(6));
9144    }
9145
9146    #[test]
9147    fn test_cast_string_to_decimal256_precision_overflow() {
9148        let array = StringArray::from(vec!["1000".to_string()]);
9149        let array = Arc::new(array) as ArrayRef;
9150        let casted_array = cast_with_options(
9151            &array,
9152            &DataType::Decimal256(10, 8),
9153            &CastOptions {
9154                safe: true,
9155                format_options: FormatOptions::default(),
9156            },
9157        );
9158        assert!(casted_array.is_ok());
9159        assert!(casted_array.unwrap().is_null(0));
9160
9161        let err = cast_with_options(
9162            &array,
9163            &DataType::Decimal256(10, 8),
9164            &CastOptions {
9165                safe: false,
9166                format_options: FormatOptions::default(),
9167            },
9168        );
9169        assert_eq!("Invalid argument error: 100000000000 is too large to store in a Decimal256 of precision 10. Max is 9999999999", err.unwrap_err().to_string());
9170    }
9171
9172    #[test]
9173    fn test_cast_utf8_to_decimal256_overflow() {
9174        let overflow_str_array = StringArray::from(vec![
9175            i128::MAX.to_string(),
9176            i128::MIN.to_string(),
9177            "99999999999999999999999999999999999999".to_string(),
9178            "999999999999999999999999999999999999.99".to_string(),
9179            "99999999999999999999999999999999999.999".to_string(),
9180            i256::MAX.to_string(),
9181            i256::MIN.to_string(),
9182        ]);
9183        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9184
9185        test_cast_string_to_decimal256_overflow(overflow_array);
9186    }
9187
9188    #[test]
9189    fn test_cast_large_utf8_to_decimal256_overflow() {
9190        let overflow_str_array = LargeStringArray::from(vec![
9191            i128::MAX.to_string(),
9192            i128::MIN.to_string(),
9193            "99999999999999999999999999999999999999".to_string(),
9194            "999999999999999999999999999999999999.99".to_string(),
9195            "99999999999999999999999999999999999.999".to_string(),
9196            i256::MAX.to_string(),
9197            i256::MIN.to_string(),
9198        ]);
9199        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9200
9201        test_cast_string_to_decimal256_overflow(overflow_array);
9202    }
9203
9204    #[test]
9205    fn test_cast_outside_supported_range_for_nanoseconds() {
9206        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";
9207
9208        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
9209
9210        let cast_options = CastOptions {
9211            safe: false,
9212            format_options: FormatOptions::default(),
9213        };
9214
9215        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
9216            &array,
9217            &None::<Arc<str>>,
9218            &cast_options,
9219        );
9220
9221        let err = result.unwrap_err();
9222        assert_eq!(
9223            err.to_string(),
9224            format!(
9225                "Cast error: Overflow converting {} to Nanosecond. {}",
9226                array.value(0),
9227                EXPECTED_ERROR_MESSAGE
9228            )
9229        );
9230    }
9231
9232    #[test]
9233    fn test_cast_date32_to_timestamp() {
9234        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9235        let array = Arc::new(a) as ArrayRef;
9236        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
9237        let c = b.as_primitive::<TimestampSecondType>();
9238        assert_eq!(1609459200, c.value(0));
9239        assert_eq!(1640995200, c.value(1));
9240        assert!(c.is_null(2));
9241    }
9242
9243    #[test]
9244    fn test_cast_date32_to_timestamp_ms() {
9245        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9246        let array = Arc::new(a) as ArrayRef;
9247        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
9248        let c = b
9249            .as_any()
9250            .downcast_ref::<TimestampMillisecondArray>()
9251            .unwrap();
9252        assert_eq!(1609459200000, c.value(0));
9253        assert_eq!(1640995200000, c.value(1));
9254        assert!(c.is_null(2));
9255    }
9256
9257    #[test]
9258    fn test_cast_date32_to_timestamp_us() {
9259        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9260        let array = Arc::new(a) as ArrayRef;
9261        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
9262        let c = b
9263            .as_any()
9264            .downcast_ref::<TimestampMicrosecondArray>()
9265            .unwrap();
9266        assert_eq!(1609459200000000, c.value(0));
9267        assert_eq!(1640995200000000, c.value(1));
9268        assert!(c.is_null(2));
9269    }
9270
9271    #[test]
9272    fn test_cast_date32_to_timestamp_ns() {
9273        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9274        let array = Arc::new(a) as ArrayRef;
9275        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9276        let c = b
9277            .as_any()
9278            .downcast_ref::<TimestampNanosecondArray>()
9279            .unwrap();
9280        assert_eq!(1609459200000000000, c.value(0));
9281        assert_eq!(1640995200000000000, c.value(1));
9282        assert!(c.is_null(2));
9283    }
9284
9285    #[test]
9286    fn test_timezone_cast() {
9287        let a = StringArray::from(vec![
9288            "2000-01-01T12:00:00", // date + time valid
9289            "2020-12-15T12:34:56", // date + time valid
9290        ]);
9291        let array = Arc::new(a) as ArrayRef;
9292        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9293        let v = b.as_primitive::<TimestampNanosecondType>();
9294
9295        assert_eq!(v.value(0), 946728000000000000);
9296        assert_eq!(v.value(1), 1608035696000000000);
9297
9298        let b = cast(
9299            &b,
9300            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9301        )
9302        .unwrap();
9303        let v = b.as_primitive::<TimestampNanosecondType>();
9304
9305        assert_eq!(v.value(0), 946728000000000000);
9306        assert_eq!(v.value(1), 1608035696000000000);
9307
9308        let b = cast(
9309            &b,
9310            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
9311        )
9312        .unwrap();
9313        let v = b.as_primitive::<TimestampMillisecondType>();
9314
9315        assert_eq!(v.value(0), 946728000000);
9316        assert_eq!(v.value(1), 1608035696000);
9317    }
9318
9319    #[test]
9320    fn test_cast_utf8_to_timestamp() {
9321        fn test_tz(tz: Arc<str>) {
9322            let valid = StringArray::from(vec![
9323                "2023-01-01 04:05:06.789000-08:00",
9324                "2023-01-01 04:05:06.789000-07:00",
9325                "2023-01-01 04:05:06.789 -0800",
9326                "2023-01-01 04:05:06.789 -08:00",
9327                "2023-01-01 040506 +0730",
9328                "2023-01-01 040506 +07:30",
9329                "2023-01-01 04:05:06.789",
9330                "2023-01-01 04:05:06",
9331                "2023-01-01",
9332            ]);
9333
9334            let array = Arc::new(valid) as ArrayRef;
9335            let b = cast_with_options(
9336                &array,
9337                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
9338                &CastOptions {
9339                    safe: false,
9340                    format_options: FormatOptions::default(),
9341                },
9342            )
9343            .unwrap();
9344
9345            let tz = tz.as_ref().parse().unwrap();
9346
9347            let as_tz =
9348                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
9349
9350            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
9351            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
9352
9353            let values = b.as_primitive::<TimestampNanosecondType>().values();
9354            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
9355            let local_results: Vec<_> = values.iter().map(as_local).collect();
9356
9357            // Absolute timestamps should be parsed preserving the same UTC instant
9358            assert_eq!(
9359                &utc_results[..6],
9360                &[
9361                    "2023-01-01 12:05:06.789".to_string(),
9362                    "2023-01-01 11:05:06.789".to_string(),
9363                    "2023-01-01 12:05:06.789".to_string(),
9364                    "2023-01-01 12:05:06.789".to_string(),
9365                    "2022-12-31 20:35:06".to_string(),
9366                    "2022-12-31 20:35:06".to_string(),
9367                ]
9368            );
9369            // Non-absolute timestamps should be parsed preserving the same local instant
9370            assert_eq!(
9371                &local_results[6..],
9372                &[
9373                    "2023-01-01 04:05:06.789".to_string(),
9374                    "2023-01-01 04:05:06".to_string(),
9375                    "2023-01-01 00:00:00".to_string()
9376                ]
9377            )
9378        }
9379
9380        test_tz("+00:00".into());
9381        test_tz("+02:00".into());
9382    }
9383
9384    #[test]
9385    fn test_cast_invalid_utf8() {
9386        let v1: &[u8] = b"\xFF invalid";
9387        let v2: &[u8] = b"\x00 Foo";
9388        let s = BinaryArray::from(vec![v1, v2]);
9389        let options = CastOptions {
9390            safe: true,
9391            format_options: FormatOptions::default(),
9392        };
9393        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
9394        let a = array.as_string::<i32>();
9395        a.to_data().validate_full().unwrap();
9396
9397        assert_eq!(a.null_count(), 1);
9398        assert_eq!(a.len(), 2);
9399        assert!(a.is_null(0));
9400        assert_eq!(a.value(0), "");
9401        assert_eq!(a.value(1), "\x00 Foo");
9402    }
9403
9404    #[test]
9405    fn test_cast_utf8_to_timestamptz() {
9406        let valid = StringArray::from(vec!["2023-01-01"]);
9407
9408        let array = Arc::new(valid) as ArrayRef;
9409        let b = cast(
9410            &array,
9411            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9412        )
9413        .unwrap();
9414
9415        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
9416
9417        assert_eq!(b.data_type(), &expect);
9418        let c = b
9419            .as_any()
9420            .downcast_ref::<TimestampNanosecondArray>()
9421            .unwrap();
9422        assert_eq!(1672531200000000000, c.value(0));
9423    }
9424
9425    #[test]
9426    fn test_cast_decimal_to_string() {
9427        assert!(can_cast_types(
9428            &DataType::Decimal128(10, 4),
9429            &DataType::Utf8View
9430        ));
9431        assert!(can_cast_types(
9432            &DataType::Decimal256(38, 10),
9433            &DataType::Utf8View
9434        ));
9435
9436        macro_rules! assert_decimal_values {
9437            ($array:expr) => {
9438                let c = $array;
9439                assert_eq!("1123.454", c.value(0));
9440                assert_eq!("2123.456", c.value(1));
9441                assert_eq!("-3123.453", c.value(2));
9442                assert_eq!("-3123.456", c.value(3));
9443                assert_eq!("0.000", c.value(4));
9444                assert_eq!("0.123", c.value(5));
9445                assert_eq!("1234.567", c.value(6));
9446                assert_eq!("-1234.567", c.value(7));
9447                assert!(c.is_null(8));
9448            };
9449        }
9450
9451        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
9452            output_type: DataType,
9453            array: PrimitiveArray<IN>,
9454        ) {
9455            let b = cast(&array, &output_type).unwrap();
9456
9457            assert_eq!(b.data_type(), &output_type);
9458            match b.data_type() {
9459                DataType::Utf8View => {
9460                    let c = b.as_string_view();
9461                    assert_decimal_values!(c);
9462                }
9463                DataType::Utf8 | DataType::LargeUtf8 => {
9464                    let c = b.as_string::<OffsetSize>();
9465                    assert_decimal_values!(c);
9466                }
9467                _ => (),
9468            }
9469        }
9470
9471        let array128: Vec<Option<i128>> = vec![
9472            Some(1123454),
9473            Some(2123456),
9474            Some(-3123453),
9475            Some(-3123456),
9476            Some(0),
9477            Some(123),
9478            Some(123456789),
9479            Some(-123456789),
9480            None,
9481        ];
9482        let array256: Vec<Option<i256>> = array128
9483            .iter()
9484            .map(|num| num.map(i256::from_i128))
9485            .collect();
9486
9487        test_decimal_to_string::<Decimal128Type, i32>(
9488            DataType::Utf8View,
9489            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
9490        );
9491        test_decimal_to_string::<Decimal128Type, i32>(
9492            DataType::Utf8,
9493            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
9494        );
9495        test_decimal_to_string::<Decimal128Type, i64>(
9496            DataType::LargeUtf8,
9497            create_decimal128_array(array128, 7, 3).unwrap(),
9498        );
9499
9500        test_decimal_to_string::<Decimal256Type, i32>(
9501            DataType::Utf8View,
9502            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
9503        );
9504        test_decimal_to_string::<Decimal256Type, i32>(
9505            DataType::Utf8,
9506            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
9507        );
9508        test_decimal_to_string::<Decimal256Type, i64>(
9509            DataType::LargeUtf8,
9510            create_decimal256_array(array256, 7, 3).unwrap(),
9511        );
9512    }
9513
9514    #[test]
9515    fn test_cast_numeric_to_decimal128_precision_overflow() {
9516        let array = Int64Array::from(vec![1234567]);
9517        let array = Arc::new(array) as ArrayRef;
9518        let casted_array = cast_with_options(
9519            &array,
9520            &DataType::Decimal128(7, 3),
9521            &CastOptions {
9522                safe: true,
9523                format_options: FormatOptions::default(),
9524            },
9525        );
9526        assert!(casted_array.is_ok());
9527        assert!(casted_array.unwrap().is_null(0));
9528
9529        let err = cast_with_options(
9530            &array,
9531            &DataType::Decimal128(7, 3),
9532            &CastOptions {
9533                safe: false,
9534                format_options: FormatOptions::default(),
9535            },
9536        );
9537        assert_eq!("Invalid argument error: 1234567000 is too large to store in a Decimal128 of precision 7. Max is 9999999", err.unwrap_err().to_string());
9538    }
9539
9540    #[test]
9541    fn test_cast_numeric_to_decimal256_precision_overflow() {
9542        let array = Int64Array::from(vec![1234567]);
9543        let array = Arc::new(array) as ArrayRef;
9544        let casted_array = cast_with_options(
9545            &array,
9546            &DataType::Decimal256(7, 3),
9547            &CastOptions {
9548                safe: true,
9549                format_options: FormatOptions::default(),
9550            },
9551        );
9552        assert!(casted_array.is_ok());
9553        assert!(casted_array.unwrap().is_null(0));
9554
9555        let err = cast_with_options(
9556            &array,
9557            &DataType::Decimal256(7, 3),
9558            &CastOptions {
9559                safe: false,
9560                format_options: FormatOptions::default(),
9561            },
9562        );
9563        assert_eq!("Invalid argument error: 1234567000 is too large to store in a Decimal256 of precision 7. Max is 9999999", err.unwrap_err().to_string());
9564    }
9565
9566    /// helper function to test casting from duration to interval
9567    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
9568        array: Vec<i64>,
9569        cast_options: &CastOptions,
9570    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9571        let array = PrimitiveArray::<T>::new(array.into(), None);
9572        let array = Arc::new(array) as ArrayRef;
9573        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
9574        let out = cast_with_options(&array, &interval, cast_options)?;
9575        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
9576        Ok(out)
9577    }
9578
9579    #[test]
9580    fn test_cast_from_duration_to_interval() {
9581        // from duration second to interval month day nano
9582        let array = vec![1234567];
9583        let casted_array =
9584            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
9585                .unwrap();
9586        assert_eq!(
9587            casted_array.data_type(),
9588            &DataType::Interval(IntervalUnit::MonthDayNano)
9589        );
9590        assert_eq!(
9591            casted_array.value(0),
9592            IntervalMonthDayNano::new(0, 0, 1234567000000000)
9593        );
9594
9595        let array = vec![i64::MAX];
9596        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
9597            array.clone(),
9598            &CastOptions::default(),
9599        )
9600        .unwrap();
9601        assert!(!casted_array.is_valid(0));
9602
9603        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
9604            array,
9605            &CastOptions {
9606                safe: false,
9607                format_options: FormatOptions::default(),
9608            },
9609        );
9610        assert!(casted_array.is_err());
9611
9612        // from duration millisecond to interval month day nano
9613        let array = vec![1234567];
9614        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9615            array,
9616            &CastOptions::default(),
9617        )
9618        .unwrap();
9619        assert_eq!(
9620            casted_array.data_type(),
9621            &DataType::Interval(IntervalUnit::MonthDayNano)
9622        );
9623        assert_eq!(
9624            casted_array.value(0),
9625            IntervalMonthDayNano::new(0, 0, 1234567000000)
9626        );
9627
9628        let array = vec![i64::MAX];
9629        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9630            array.clone(),
9631            &CastOptions::default(),
9632        )
9633        .unwrap();
9634        assert!(!casted_array.is_valid(0));
9635
9636        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9637            array,
9638            &CastOptions {
9639                safe: false,
9640                format_options: FormatOptions::default(),
9641            },
9642        );
9643        assert!(casted_array.is_err());
9644
9645        // from duration microsecond to interval month day nano
9646        let array = vec![1234567];
9647        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9648            array,
9649            &CastOptions::default(),
9650        )
9651        .unwrap();
9652        assert_eq!(
9653            casted_array.data_type(),
9654            &DataType::Interval(IntervalUnit::MonthDayNano)
9655        );
9656        assert_eq!(
9657            casted_array.value(0),
9658            IntervalMonthDayNano::new(0, 0, 1234567000)
9659        );
9660
9661        let array = vec![i64::MAX];
9662        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9663            array.clone(),
9664            &CastOptions::default(),
9665        )
9666        .unwrap();
9667        assert!(!casted_array.is_valid(0));
9668
9669        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9670            array,
9671            &CastOptions {
9672                safe: false,
9673                format_options: FormatOptions::default(),
9674            },
9675        );
9676        assert!(casted_array.is_err());
9677
9678        // from duration nanosecond to interval month day nano
9679        let array = vec![1234567];
9680        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
9681            array,
9682            &CastOptions::default(),
9683        )
9684        .unwrap();
9685        assert_eq!(
9686            casted_array.data_type(),
9687            &DataType::Interval(IntervalUnit::MonthDayNano)
9688        );
9689        assert_eq!(
9690            casted_array.value(0),
9691            IntervalMonthDayNano::new(0, 0, 1234567)
9692        );
9693
9694        let array = vec![i64::MAX];
9695        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
9696            array,
9697            &CastOptions {
9698                safe: false,
9699                format_options: FormatOptions::default(),
9700            },
9701        )
9702        .unwrap();
9703        assert_eq!(
9704            casted_array.value(0),
9705            IntervalMonthDayNano::new(0, 0, i64::MAX)
9706        );
9707    }
9708
9709    /// helper function to test casting from interval to duration
9710    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
9711        array: &IntervalMonthDayNanoArray,
9712        cast_options: &CastOptions,
9713    ) -> Result<PrimitiveArray<T>, ArrowError> {
9714        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
9715        casted_array
9716            .as_any()
9717            .downcast_ref::<PrimitiveArray<T>>()
9718            .ok_or_else(|| {
9719                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
9720            })
9721            .cloned()
9722    }
9723
9724    #[test]
9725    fn test_cast_from_interval_to_duration() {
9726        let nullable = CastOptions::default();
9727        let fallible = CastOptions {
9728            safe: false,
9729            format_options: FormatOptions::default(),
9730        };
9731        let v = IntervalMonthDayNano::new(0, 0, 1234567);
9732
9733        // from interval month day nano to duration second
9734        let array = vec![v].into();
9735        let casted_array: DurationSecondArray =
9736            cast_from_interval_to_duration(&array, &nullable).unwrap();
9737        assert_eq!(casted_array.value(0), 0);
9738
9739        let array = vec![IntervalMonthDayNano::MAX].into();
9740        let casted_array: DurationSecondArray =
9741            cast_from_interval_to_duration(&array, &nullable).unwrap();
9742        assert!(!casted_array.is_valid(0));
9743
9744        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
9745        assert!(res.is_err());
9746
9747        // from interval month day nano to duration millisecond
9748        let array = vec![v].into();
9749        let casted_array: DurationMillisecondArray =
9750            cast_from_interval_to_duration(&array, &nullable).unwrap();
9751        assert_eq!(casted_array.value(0), 1);
9752
9753        let array = vec![IntervalMonthDayNano::MAX].into();
9754        let casted_array: DurationMillisecondArray =
9755            cast_from_interval_to_duration(&array, &nullable).unwrap();
9756        assert!(!casted_array.is_valid(0));
9757
9758        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
9759        assert!(res.is_err());
9760
9761        // from interval month day nano to duration microsecond
9762        let array = vec![v].into();
9763        let casted_array: DurationMicrosecondArray =
9764            cast_from_interval_to_duration(&array, &nullable).unwrap();
9765        assert_eq!(casted_array.value(0), 1234);
9766
9767        let array = vec![IntervalMonthDayNano::MAX].into();
9768        let casted_array =
9769            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
9770        assert!(!casted_array.is_valid(0));
9771
9772        let casted_array =
9773            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
9774        assert!(casted_array.is_err());
9775
9776        // from interval month day nano to duration nanosecond
9777        let array = vec![v].into();
9778        let casted_array: DurationNanosecondArray =
9779            cast_from_interval_to_duration(&array, &nullable).unwrap();
9780        assert_eq!(casted_array.value(0), 1234567);
9781
9782        let array = vec![IntervalMonthDayNano::MAX].into();
9783        let casted_array: DurationNanosecondArray =
9784            cast_from_interval_to_duration(&array, &nullable).unwrap();
9785        assert!(!casted_array.is_valid(0));
9786
9787        let casted_array =
9788            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
9789        assert!(casted_array.is_err());
9790
9791        let array = vec![
9792            IntervalMonthDayNanoType::make_value(0, 1, 0),
9793            IntervalMonthDayNanoType::make_value(-1, 0, 0),
9794            IntervalMonthDayNanoType::make_value(1, 1, 0),
9795            IntervalMonthDayNanoType::make_value(1, 0, 1),
9796            IntervalMonthDayNanoType::make_value(0, 0, -1),
9797        ]
9798        .into();
9799        let casted_array =
9800            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
9801        assert!(!casted_array.is_valid(0));
9802        assert!(!casted_array.is_valid(1));
9803        assert!(!casted_array.is_valid(2));
9804        assert!(!casted_array.is_valid(3));
9805        assert!(casted_array.is_valid(4));
9806        assert_eq!(casted_array.value(4), -1);
9807    }
9808
9809    /// helper function to test casting from interval year month to interval month day nano
9810    fn cast_from_interval_year_month_to_interval_month_day_nano(
9811        array: Vec<i32>,
9812        cast_options: &CastOptions,
9813    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9814        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
9815        let array = Arc::new(array) as ArrayRef;
9816        let casted_array = cast_with_options(
9817            &array,
9818            &DataType::Interval(IntervalUnit::MonthDayNano),
9819            cast_options,
9820        )?;
9821        casted_array
9822            .as_any()
9823            .downcast_ref::<IntervalMonthDayNanoArray>()
9824            .ok_or_else(|| {
9825                ArrowError::ComputeError(
9826                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
9827                )
9828            })
9829            .cloned()
9830    }
9831
9832    #[test]
9833    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
9834        // from interval year month to interval month day nano
9835        let array = vec![1234567];
9836        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
9837            array,
9838            &CastOptions::default(),
9839        )
9840        .unwrap();
9841        assert_eq!(
9842            casted_array.data_type(),
9843            &DataType::Interval(IntervalUnit::MonthDayNano)
9844        );
9845        assert_eq!(
9846            casted_array.value(0),
9847            IntervalMonthDayNano::new(1234567, 0, 0)
9848        );
9849    }
9850
9851    /// helper function to test casting from interval day time to interval month day nano
9852    fn cast_from_interval_day_time_to_interval_month_day_nano(
9853        array: Vec<IntervalDayTime>,
9854        cast_options: &CastOptions,
9855    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9856        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
9857        let array = Arc::new(array) as ArrayRef;
9858        let casted_array = cast_with_options(
9859            &array,
9860            &DataType::Interval(IntervalUnit::MonthDayNano),
9861            cast_options,
9862        )?;
9863        Ok(casted_array
9864            .as_primitive::<IntervalMonthDayNanoType>()
9865            .clone())
9866    }
9867
9868    #[test]
9869    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
9870        // from interval day time to interval month day nano
9871        let array = vec![IntervalDayTime::new(123, 0)];
9872        let casted_array =
9873            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
9874                .unwrap();
9875        assert_eq!(
9876            casted_array.data_type(),
9877            &DataType::Interval(IntervalUnit::MonthDayNano)
9878        );
9879        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
9880    }
9881
9882    #[test]
9883    fn test_cast_below_unixtimestamp() {
9884        let valid = StringArray::from(vec![
9885            "1900-01-03 23:59:59",
9886            "1969-12-31 00:00:01",
9887            "1989-12-31 00:00:01",
9888        ]);
9889
9890        let array = Arc::new(valid) as ArrayRef;
9891        let casted_array = cast_with_options(
9892            &array,
9893            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9894            &CastOptions {
9895                safe: false,
9896                format_options: FormatOptions::default(),
9897            },
9898        )
9899        .unwrap();
9900
9901        let ts_array = casted_array
9902            .as_primitive::<TimestampNanosecondType>()
9903            .values()
9904            .iter()
9905            .map(|ts| ts / 1_000_000)
9906            .collect::<Vec<_>>();
9907
9908        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
9909        let casted_array = cast(&array, &DataType::Date32).unwrap();
9910        let date_array = casted_array.as_primitive::<Date32Type>();
9911        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
9912        let string_array = casted_array.as_string::<i32>();
9913        assert_eq!("1900-01-03", string_array.value(0));
9914        assert_eq!("1969-12-31", string_array.value(1));
9915        assert_eq!("1989-12-31", string_array.value(2));
9916    }
9917
9918    #[test]
9919    fn test_nested_list() {
9920        let mut list = ListBuilder::new(Int32Builder::new());
9921        list.append_value([Some(1), Some(2), Some(3)]);
9922        list.append_value([Some(4), None, Some(6)]);
9923        let list = list.finish();
9924
9925        let to_field = Field::new("nested", list.data_type().clone(), false);
9926        let to = DataType::List(Arc::new(to_field));
9927        let out = cast(&list, &to).unwrap();
9928        let opts = FormatOptions::default().with_null("null");
9929        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
9930
9931        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
9932        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
9933    }
9934
9935    #[test]
9936    fn test_nested_list_cast() {
9937        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
9938        builder.append_value([Some([Some(1), Some(2), None]), None]);
9939        builder.append_value([None, Some([]), None]);
9940        builder.append_null();
9941        builder.append_value([Some([Some(2), Some(3)])]);
9942        let start = builder.finish();
9943
9944        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
9945        builder.append_value([Some([Some(1), Some(2), None]), None]);
9946        builder.append_value([None, Some([]), None]);
9947        builder.append_null();
9948        builder.append_value([Some([Some(2), Some(3)])]);
9949        let expected = builder.finish();
9950
9951        let actual = cast(&start, expected.data_type()).unwrap();
9952        assert_eq!(actual.as_ref(), &expected);
9953    }
9954
9955    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
9956        safe: true,
9957        format_options: FormatOptions::new(),
9958    };
9959
9960    #[test]
9961    #[allow(clippy::assertions_on_constants)]
9962    fn test_const_options() {
9963        assert!(CAST_OPTIONS.safe)
9964    }
9965
9966    #[test]
9967    fn test_list_format_options() {
9968        let options = CastOptions {
9969            safe: false,
9970            format_options: FormatOptions::default().with_null("null"),
9971        };
9972        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9973            Some(vec![Some(0), Some(1), Some(2)]),
9974            Some(vec![Some(0), None, Some(2)]),
9975        ]);
9976        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
9977        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
9978        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
9979    }
9980    #[test]
9981    fn test_cast_string_to_timestamp_invalid_tz() {
9982        // content after Z should be ignored
9983        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
9984        let array = StringArray::from(vec![Some(bad_timestamp)]);
9985
9986        let data_types = [
9987            DataType::Timestamp(TimeUnit::Second, None),
9988            DataType::Timestamp(TimeUnit::Millisecond, None),
9989            DataType::Timestamp(TimeUnit::Microsecond, None),
9990            DataType::Timestamp(TimeUnit::Nanosecond, None),
9991        ];
9992
9993        let cast_options = CastOptions {
9994            safe: false,
9995            ..Default::default()
9996        };
9997
9998        for dt in data_types {
9999            assert_eq!(
10000                cast_with_options(&array, &dt, &cast_options)
10001                    .unwrap_err()
10002                    .to_string(),
10003                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
10004            );
10005        }
10006    }
10007    #[test]
10008    fn test_cast_struct_to_struct() {
10009        let struct_type = DataType::Struct(
10010            vec![
10011                Field::new("a", DataType::Boolean, false),
10012                Field::new("b", DataType::Int32, false),
10013            ]
10014            .into(),
10015        );
10016        let to_type = DataType::Struct(
10017            vec![
10018                Field::new("a", DataType::Utf8, false),
10019                Field::new("b", DataType::Utf8, false),
10020            ]
10021            .into(),
10022        );
10023        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10024        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
10025        let struct_array = StructArray::from(vec![
10026            (
10027                Arc::new(Field::new("b", DataType::Boolean, false)),
10028                boolean.clone() as ArrayRef,
10029            ),
10030            (
10031                Arc::new(Field::new("c", DataType::Int32, false)),
10032                int.clone() as ArrayRef,
10033            ),
10034        ]);
10035        let casted_array = cast(&struct_array, &to_type).unwrap();
10036        let casted_array = casted_array.as_struct();
10037        assert_eq!(casted_array.data_type(), &to_type);
10038        let casted_boolean_array = casted_array
10039            .column(0)
10040            .as_string::<i32>()
10041            .into_iter()
10042            .flatten()
10043            .collect::<Vec<_>>();
10044        let casted_int_array = casted_array
10045            .column(1)
10046            .as_string::<i32>()
10047            .into_iter()
10048            .flatten()
10049            .collect::<Vec<_>>();
10050        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
10051        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
10052
10053        // test for can't cast
10054        let to_type = DataType::Struct(
10055            vec![
10056                Field::new("a", DataType::Date32, false),
10057                Field::new("b", DataType::Utf8, false),
10058            ]
10059            .into(),
10060        );
10061        assert!(!can_cast_types(&struct_type, &to_type));
10062        let result = cast(&struct_array, &to_type);
10063        assert_eq!(
10064            "Cast error: Casting from Boolean to Date32 not supported",
10065            result.unwrap_err().to_string()
10066        );
10067    }
10068
10069    #[test]
10070    fn test_cast_struct_to_struct_nullability() {
10071        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10072        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
10073        let struct_array = StructArray::from(vec![
10074            (
10075                Arc::new(Field::new("b", DataType::Boolean, false)),
10076                boolean.clone() as ArrayRef,
10077            ),
10078            (
10079                Arc::new(Field::new("c", DataType::Int32, true)),
10080                int.clone() as ArrayRef,
10081            ),
10082        ]);
10083
10084        // okay: nullable to nullable
10085        let to_type = DataType::Struct(
10086            vec![
10087                Field::new("a", DataType::Utf8, false),
10088                Field::new("b", DataType::Utf8, true),
10089            ]
10090            .into(),
10091        );
10092        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
10093
10094        // error: nullable to non-nullable
10095        let to_type = DataType::Struct(
10096            vec![
10097                Field::new("a", DataType::Utf8, false),
10098                Field::new("b", DataType::Utf8, false),
10099            ]
10100            .into(),
10101        );
10102        cast(&struct_array, &to_type)
10103            .expect_err("Cast nullable to non-nullable struct field should fail");
10104
10105        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10106        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
10107        let struct_array = StructArray::from(vec![
10108            (
10109                Arc::new(Field::new("b", DataType::Boolean, false)),
10110                boolean.clone() as ArrayRef,
10111            ),
10112            (
10113                Arc::new(Field::new("c", DataType::Int32, false)),
10114                int.clone() as ArrayRef,
10115            ),
10116        ]);
10117
10118        // okay: non-nullable to non-nullable
10119        let to_type = DataType::Struct(
10120            vec![
10121                Field::new("a", DataType::Utf8, false),
10122                Field::new("b", DataType::Utf8, false),
10123            ]
10124            .into(),
10125        );
10126        cast(&struct_array, &to_type)
10127            .expect("Cast non-nullable to non-nullable struct field should work");
10128
10129        // err: non-nullable to non-nullable but overflowing return null during casting
10130        let to_type = DataType::Struct(
10131            vec![
10132                Field::new("a", DataType::Utf8, false),
10133                Field::new("b", DataType::Int8, false),
10134            ]
10135            .into(),
10136        );
10137        cast(&struct_array, &to_type).expect_err(
10138            "Cast non-nullable to non-nullable struct field returning null should fail",
10139        );
10140    }
10141
10142    #[test]
10143    fn test_cast_struct_to_non_struct() {
10144        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
10145        let struct_array = StructArray::from(vec![(
10146            Arc::new(Field::new("a", DataType::Boolean, false)),
10147            boolean.clone() as ArrayRef,
10148        )]);
10149        let to_type = DataType::Utf8;
10150        let result = cast(&struct_array, &to_type);
10151        assert_eq!(
10152            r#"Cast error: Casting from Struct([Field { name: "a", data_type: Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]) to Utf8 not supported"#,
10153            result.unwrap_err().to_string()
10154        );
10155    }
10156
10157    #[test]
10158    fn test_cast_non_struct_to_struct() {
10159        let array = StringArray::from(vec!["a", "b"]);
10160        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
10161        let result = cast(&array, &to_type);
10162        assert_eq!(
10163            r#"Cast error: Casting from Utf8 to Struct([Field { name: "a", data_type: Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]) not supported"#,
10164            result.unwrap_err().to_string()
10165        );
10166    }
10167
10168    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
10169        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
10170        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
10171        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
10172        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
10173    }
10174
10175    #[test]
10176    fn test_decimal_to_decimal_coverage() {
10177        let test_cases = [
10178            // increase precision, increase scale, infallible
10179            DecimalCastTestConfig {
10180                input_prec: 5,
10181                input_scale: 1,
10182                input_repr: 99999, // 9999.9
10183                output_prec: 10,
10184                output_scale: 6,
10185                expected_output_repr: Ok(9999900000), // 9999.900000
10186            },
10187            // increase precision, increase scale, fallible, safe
10188            DecimalCastTestConfig {
10189                input_prec: 5,
10190                input_scale: 1,
10191                input_repr: 99, // 9999.9
10192                output_prec: 7,
10193                output_scale: 6,
10194                expected_output_repr: Ok(9900000), // 9.900000
10195            },
10196            // increase precision, increase scale, fallible, unsafe
10197            DecimalCastTestConfig {
10198                input_prec: 5,
10199                input_scale: 1,
10200                input_repr: 99999, // 9999.9
10201                output_prec: 7,
10202                output_scale: 6,
10203                expected_output_repr: Err("Invalid argument error: 9999900000 is too large to store in a {} of precision 7. Max is 9999999".to_string()) // max is 9.999999
10204            },
10205            // increase precision, decrease scale, always infallible
10206            DecimalCastTestConfig {
10207                input_prec: 5,
10208                input_scale: 3,
10209                input_repr: 99999, // 99.999
10210                output_prec: 10,
10211                output_scale: 2,
10212                expected_output_repr: Ok(10000), // 100.00
10213            },
10214            // increase precision, decrease scale, no rouding
10215            DecimalCastTestConfig {
10216                input_prec: 5,
10217                input_scale: 3,
10218                input_repr: 99994, // 99.994
10219                output_prec: 10,
10220                output_scale: 2,
10221                expected_output_repr: Ok(9999), // 99.99
10222            },
10223            // increase precision, don't change scale, always infallible
10224            DecimalCastTestConfig {
10225                input_prec: 5,
10226                input_scale: 3,
10227                input_repr: 99999, // 99.999
10228                output_prec: 10,
10229                output_scale: 3,
10230                expected_output_repr: Ok(99999), // 99.999
10231            },
10232            // decrease precision, increase scale, safe
10233            DecimalCastTestConfig {
10234                input_prec: 10,
10235                input_scale: 5,
10236                input_repr: 999999, // 9.99999
10237                output_prec: 8,
10238                output_scale: 7,
10239                expected_output_repr: Ok(99999900), // 9.9999900
10240            },
10241            // decrease precision, increase scale, unsafe
10242            DecimalCastTestConfig {
10243                input_prec: 10,
10244                input_scale: 5,
10245                input_repr: 9999999, // 99.99999
10246                output_prec: 8,
10247                output_scale: 7,
10248                expected_output_repr: Err("Invalid argument error: 999999900 is too large to store in a {} of precision 8. Max is 99999999".to_string()) // max is 9.9999999
10249            },
10250            // decrease precision, decrease scale, safe, infallible
10251            DecimalCastTestConfig {
10252                input_prec: 7,
10253                input_scale: 4,
10254                input_repr: 9999999, // 999.9999
10255                output_prec: 6,
10256                output_scale: 2,
10257                expected_output_repr: Ok(100000),
10258            },
10259            // decrease precision, decrease scale, safe, fallible
10260            DecimalCastTestConfig {
10261                input_prec: 10,
10262                input_scale: 5,
10263                input_repr: 12345678, // 123.45678
10264                output_prec: 8,
10265                output_scale: 3,
10266                expected_output_repr: Ok(123457), // 123.457
10267            },
10268            // decrease precision, decrease scale, unsafe
10269            DecimalCastTestConfig {
10270                input_prec: 10,
10271                input_scale: 5,
10272                input_repr: 9999999, // 99.99999
10273                output_prec: 4,
10274                output_scale: 3,
10275                expected_output_repr: Err("Invalid argument error: 100000 is too large to store in a {} of precision 4. Max is 9999".to_string()) // max is 9.999
10276            },
10277            // decrease precision, same scale, safe
10278            DecimalCastTestConfig {
10279                input_prec: 10,
10280                input_scale: 5,
10281                input_repr: 999999, // 9.99999
10282                output_prec: 6,
10283                output_scale: 5,
10284                expected_output_repr: Ok(999999), // 9.99999
10285            },
10286            // decrease precision, same scale, unsafe
10287            DecimalCastTestConfig {
10288                input_prec: 10,
10289                input_scale: 5,
10290                input_repr: 9999999, // 99.99999
10291                output_prec: 6,
10292                output_scale: 5,
10293                expected_output_repr: Err("Invalid argument error: 9999999 is too large to store in a {} of precision 6. Max is 999999".to_string()) // max is 9.99999
10294            },
10295            // same precision, increase scale, safe
10296            DecimalCastTestConfig {
10297                input_prec: 7,
10298                input_scale: 4,
10299                input_repr: 12345, // 1.2345
10300                output_prec: 7,
10301                output_scale: 6,
10302                expected_output_repr: Ok(1234500), // 1.234500
10303            },
10304            // same precision, increase scale, unsafe
10305            DecimalCastTestConfig {
10306                input_prec: 7,
10307                input_scale: 4,
10308                input_repr: 123456, // 12.3456
10309                output_prec: 7,
10310                output_scale: 6,
10311                expected_output_repr: Err("Invalid argument error: 12345600 is too large to store in a {} of precision 7. Max is 9999999".to_string()) // max is 9.99999
10312            },
10313            // same precision, decrease scale, infallible
10314            DecimalCastTestConfig {
10315                input_prec: 7,
10316                input_scale: 5,
10317                input_repr: 1234567, // 12.34567
10318                output_prec: 7,
10319                output_scale: 4,
10320                expected_output_repr: Ok(123457), // 12.3457
10321            },
10322            // same precision, same scale, infallible
10323            DecimalCastTestConfig {
10324                input_prec: 7,
10325                input_scale: 5,
10326                input_repr: 9999999, // 99.99999
10327                output_prec: 7,
10328                output_scale: 5,
10329                expected_output_repr: Ok(9999999), // 99.99999
10330            },
10331            // precision increase, input scale & output scale = 0, infallible
10332            DecimalCastTestConfig {
10333                input_prec: 7,
10334                input_scale: 0,
10335                input_repr: 1234567, // 1234567
10336                output_prec: 8,
10337                output_scale: 0,
10338                expected_output_repr: Ok(1234567), // 1234567
10339            },
10340            // precision decrease, input scale & output scale = 0, failure
10341            DecimalCastTestConfig {
10342                input_prec: 7,
10343                input_scale: 0,
10344                input_repr: 1234567, // 1234567
10345                output_prec: 6,
10346                output_scale: 0,
10347                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
10348            },
10349            // precision decrease, input scale & output scale = 0, success
10350            DecimalCastTestConfig {
10351                input_prec: 7,
10352                input_scale: 0,
10353                input_repr: 123456, // 123456
10354                output_prec: 6,
10355                output_scale: 0,
10356                expected_output_repr: Ok(123456), // 123456
10357            },
10358        ];
10359
10360        for t in test_cases {
10361            run_decimal_cast_test_case_between_multiple_types(t);
10362        }
10363    }
10364
10365    #[test]
10366    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
10367        let test_cases = [
10368            DecimalCastTestConfig {
10369                input_prec: 5,
10370                input_scale: 0,
10371                input_repr: 99999,
10372                output_prec: 10,
10373                output_scale: 5,
10374                expected_output_repr: Ok(9999900000),
10375            },
10376            DecimalCastTestConfig {
10377                input_prec: 5,
10378                input_scale: 0,
10379                input_repr: -99999,
10380                output_prec: 10,
10381                output_scale: 5,
10382                expected_output_repr: Ok(-9999900000),
10383            },
10384            DecimalCastTestConfig {
10385                input_prec: 5,
10386                input_scale: 2,
10387                input_repr: 99999,
10388                output_prec: 10,
10389                output_scale: 5,
10390                expected_output_repr: Ok(99999000),
10391            },
10392            DecimalCastTestConfig {
10393                input_prec: 5,
10394                input_scale: -2,
10395                input_repr: -99999,
10396                output_prec: 10,
10397                output_scale: 3,
10398                expected_output_repr: Ok(-9999900000),
10399            },
10400            DecimalCastTestConfig {
10401                input_prec: 5,
10402                input_scale: 3,
10403                input_repr: -12345,
10404                output_prec: 6,
10405                output_scale: 5,
10406                expected_output_repr: Err("Invalid argument error: -1234500 is too small to store in a {} of precision 6. Min is -999999".to_string())
10407            },
10408        ];
10409
10410        for t in test_cases {
10411            run_decimal_cast_test_case_between_multiple_types(t);
10412        }
10413    }
10414
10415    #[test]
10416    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
10417        let test_cases = [
10418            DecimalCastTestConfig {
10419                input_prec: 5,
10420                input_scale: 0,
10421                input_repr: 99999,
10422                output_scale: -3,
10423                output_prec: 3,
10424                expected_output_repr: Ok(100),
10425            },
10426            DecimalCastTestConfig {
10427                input_prec: 5,
10428                input_scale: 0,
10429                input_repr: -99999,
10430                output_prec: 1,
10431                output_scale: -5,
10432                expected_output_repr: Ok(-1),
10433            },
10434            DecimalCastTestConfig {
10435                input_prec: 10,
10436                input_scale: 2,
10437                input_repr: 123456789,
10438                output_prec: 5,
10439                output_scale: -2,
10440                expected_output_repr: Ok(12346),
10441            },
10442            DecimalCastTestConfig {
10443                input_prec: 10,
10444                input_scale: 4,
10445                input_repr: -9876543210,
10446                output_prec: 7,
10447                output_scale: 0,
10448                expected_output_repr: Ok(-987654),
10449            },
10450            DecimalCastTestConfig {
10451                input_prec: 7,
10452                input_scale: 4,
10453                input_repr: 9999999,
10454                output_prec: 6,
10455                output_scale: 3,
10456                expected_output_repr:
10457                    Err("Invalid argument error: 1000000 is too large to store in a {} of precision 6. Max is 999999".to_string()),
10458            },
10459        ];
10460        for t in test_cases {
10461            run_decimal_cast_test_case_between_multiple_types(t);
10462        }
10463    }
10464
10465    #[test]
10466    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
10467        let array = vec![Some(123456789)];
10468        let array = create_decimal128_array(array, 24, 2).unwrap();
10469        let input_type = DataType::Decimal128(24, 2);
10470        let output_type = DataType::Decimal128(6, 2);
10471        assert!(can_cast_types(&input_type, &output_type));
10472
10473        let options = CastOptions {
10474            safe: false,
10475            ..Default::default()
10476        };
10477        let result = cast_with_options(&array, &output_type, &options);
10478        assert_eq!(result.unwrap_err().to_string(),
10479                   "Invalid argument error: 123456789 is too large to store in a Decimal128 of precision 6. Max is 999999");
10480    }
10481
10482    #[test]
10483    fn test_decimal_to_decimal_same_scale() {
10484        let array = vec![Some(520)];
10485        let array = create_decimal128_array(array, 4, 2).unwrap();
10486        let input_type = DataType::Decimal128(4, 2);
10487        let output_type = DataType::Decimal128(3, 2);
10488        assert!(can_cast_types(&input_type, &output_type));
10489
10490        let options = CastOptions {
10491            safe: false,
10492            ..Default::default()
10493        };
10494        let result = cast_with_options(&array, &output_type, &options);
10495        assert_eq!(
10496            result.unwrap().as_primitive::<Decimal128Type>().value(0),
10497            520
10498        );
10499
10500        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
10501        assert_eq!(
10502            &cast(
10503                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
10504                &DataType::Decimal128(2, 0)
10505            )
10506            .unwrap(),
10507            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
10508        );
10509    }
10510
10511    #[test]
10512    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
10513        let array = vec![Some(123456789)];
10514        let array = create_decimal128_array(array, 24, 4).unwrap();
10515        let input_type = DataType::Decimal128(24, 4);
10516        let output_type = DataType::Decimal128(6, 2);
10517        assert!(can_cast_types(&input_type, &output_type));
10518
10519        let options = CastOptions {
10520            safe: false,
10521            ..Default::default()
10522        };
10523        let result = cast_with_options(&array, &output_type, &options);
10524        assert_eq!(result.unwrap_err().to_string(),
10525                   "Invalid argument error: 1234568 is too large to store in a Decimal128 of precision 6. Max is 999999");
10526    }
10527
10528    #[test]
10529    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
10530        let array = vec![Some(123456789)];
10531        let array = create_decimal128_array(array, 24, 2).unwrap();
10532        let input_type = DataType::Decimal128(24, 2);
10533        let output_type = DataType::Decimal128(6, 3);
10534        assert!(can_cast_types(&input_type, &output_type));
10535
10536        let options = CastOptions {
10537            safe: false,
10538            ..Default::default()
10539        };
10540        let result = cast_with_options(&array, &output_type, &options);
10541        assert_eq!(result.unwrap_err().to_string(),
10542                   "Invalid argument error: 1234567890 is too large to store in a Decimal128 of precision 6. Max is 999999");
10543    }
10544
10545    #[test]
10546    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
10547        let array = vec![Some(123456789)];
10548        let array = create_decimal128_array(array, 24, 2).unwrap();
10549        let input_type = DataType::Decimal128(24, 2);
10550        let output_type = DataType::Decimal256(6, 2);
10551        assert!(can_cast_types(&input_type, &output_type));
10552
10553        let options = CastOptions {
10554            safe: false,
10555            ..Default::default()
10556        };
10557        let result = cast_with_options(&array, &output_type, &options);
10558        assert_eq!(result.unwrap_err().to_string(),
10559                   "Invalid argument error: 123456789 is too large to store in a Decimal256 of precision 6. Max is 999999");
10560    }
10561
10562    #[test]
10563    fn test_first_none() {
10564        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
10565            None,
10566            Some(vec![Some(1), Some(2)]),
10567        ])) as ArrayRef;
10568        let data_type =
10569            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
10570        let opt = CastOptions::default();
10571        let r = cast_with_options(&array, &data_type, &opt).unwrap();
10572
10573        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10574            vec![None, Some(vec![Some(1), Some(2)])],
10575            2,
10576        )) as ArrayRef;
10577        assert_eq!(*fixed_array, *r);
10578    }
10579
10580    #[test]
10581    fn test_first_last_none() {
10582        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
10583            None,
10584            Some(vec![Some(1), Some(2)]),
10585            None,
10586        ])) as ArrayRef;
10587        let data_type =
10588            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
10589        let opt = CastOptions::default();
10590        let r = cast_with_options(&array, &data_type, &opt).unwrap();
10591
10592        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10593            vec![None, Some(vec![Some(1), Some(2)]), None],
10594            2,
10595        )) as ArrayRef;
10596        assert_eq!(*fixed_array, *r);
10597    }
10598}