Coverage Report

Created: 2025-08-26 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/Users/andrewlamb/Software/arrow-rs/arrow-cast/src/cast/mod.rs
Line
Count
Source
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
40
mod decimal;
41
mod dictionary;
42
mod list;
43
mod map;
44
mod string;
45
use crate::cast::decimal::*;
46
use crate::cast::dictionary::*;
47
use crate::cast::list::*;
48
use crate::cast::map::*;
49
use crate::cast::string::*;
50
51
use arrow_buffer::IntervalMonthDayNano;
52
use arrow_data::ByteView;
53
use chrono::{NaiveTime, Offset, TimeZone, Utc};
54
use std::cmp::Ordering;
55
use std::sync::Arc;
56
57
use crate::display::{ArrayFormatter, FormatOptions};
58
use crate::parse::{
59
    parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
60
    string_to_datetime, Parser,
61
};
62
use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
63
use arrow_buffer::{i256, ArrowNativeType, OffsetBuffer};
64
use arrow_data::transform::MutableArrayData;
65
use arrow_data::ArrayData;
66
use arrow_schema::*;
67
use arrow_select::take::take;
68
use num::cast::AsPrimitive;
69
use num::{NumCast, ToPrimitive};
70
71
/// CastOptions provides a way to override the default cast behaviors
72
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73
pub 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
80
impl Default for CastOptions<'_> {
81
0
    fn default() -> Self {
82
0
        Self {
83
0
            safe: true,
84
0
            format_options: FormatOptions::default(),
85
0
        }
86
0
    }
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
92
0
pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
93
    use self::DataType::*;
94
    use self::IntervalUnit::*;
95
    use self::TimeUnit::*;
96
0
    if from_type == to_type {
97
0
        return true;
98
0
    }
99
100
0
    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
0
        ) => true,
135
        // Dictionary/List conditions should be put in front of others
136
0
        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
137
0
            can_cast_types(from_value_type, to_value_type)
138
        }
139
0
        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
140
0
        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
141
0
        (List(list_from) | LargeList(list_from), List(list_to) | LargeList(list_to)) => {
142
0
            can_cast_types(list_from.data_type(), list_to.data_type())
143
        }
144
0
        (List(list_from) | LargeList(list_from), Utf8 | LargeUtf8) => {
145
0
            can_cast_types(list_from.data_type(), to_type)
146
        }
147
0
        (List(list_from) | LargeList(list_from), FixedSizeList(list_to, _)) => {
148
0
            can_cast_types(list_from.data_type(), list_to.data_type())
149
        }
150
0
        (List(_), _) => false,
151
0
        (FixedSizeList(list_from, _), List(list_to))
152
0
        | (FixedSizeList(list_from, _), LargeList(list_to)) => {
153
0
            can_cast_types(list_from.data_type(), list_to.data_type())
154
        }
155
0
        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
156
0
            can_cast_types(inner.data_type(), inner_to.data_type())
157
        }
158
0
        (_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
159
0
        (_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
160
0
        (_, FixedSizeList(list_to, size)) if *size == 1 => {
161
0
            can_cast_types(from_type, list_to.data_type())
162
        }
163
0
        (FixedSizeList(list_from, size), _) if *size == 1 => {
164
0
            can_cast_types(list_from.data_type(), to_type)
165
        }
166
0
        (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
167
0
            if ordered_from == ordered_to =>
168
        {
169
            match (
170
0
                key_field(from_entries),
171
0
                key_field(to_entries),
172
0
                value_field(from_entries),
173
0
                value_field(to_entries),
174
            ) {
175
0
                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
176
0
                    can_cast_types(from_key.data_type(), to_key.data_type())
177
0
                        && can_cast_types(from_value.data_type(), to_value.data_type())
178
                }
179
0
                _ => false,
180
            }
181
        }
182
        // cast one decimal type to another decimal type
183
        (
184
            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
185
            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
186
0
        ) => true,
187
        // unsigned integer to decimal
188
        (
189
            UInt8 | UInt16 | UInt32 | UInt64,
190
            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
191
0
        ) => true,
192
        // signed numeric to decimal
193
        (
194
            Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
195
            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
196
0
        ) => true,
197
        // decimal to unsigned numeric
198
        (
199
            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
200
            UInt8 | UInt16 | UInt32 | UInt64,
201
0
        ) => true,
202
        // decimal to signed numeric
203
        (
204
            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
205
            Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
206
0
        ) => true,
207
        // decimal to string
208
        (
209
            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
210
            Utf8View | Utf8 | LargeUtf8,
211
0
        ) => true,
212
        // string to decimal
213
        (
214
            Utf8View | Utf8 | LargeUtf8,
215
            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
216
0
        ) => true,
217
0
        (Struct(from_fields), Struct(to_fields)) => {
218
0
            from_fields.len() == to_fields.len()
219
0
                && from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
220
                    // Assume that nullability between two structs are compatible, if not,
221
                    // cast kernel will return error.
222
0
                    can_cast_types(f1.data_type(), f2.data_type())
223
0
                })
224
        }
225
0
        (Struct(_), _) => false,
226
0
        (_, Struct(_)) => false,
227
        (_, Boolean) => {
228
0
            DataType::is_integer(from_type)
229
0
                || DataType::is_floating(from_type)
230
0
                || from_type == &Utf8View
231
0
                || from_type == &Utf8
232
0
                || from_type == &LargeUtf8
233
        }
234
        (Boolean, _) => {
235
0
            DataType::is_integer(to_type)
236
0
                || DataType::is_floating(to_type)
237
0
                || to_type == &Utf8View
238
0
                || to_type == &Utf8
239
0
                || to_type == &LargeUtf8
240
        }
241
242
        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
243
0
            true
244
        }
245
        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
246
0
            true
247
        }
248
0
        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
249
        (
250
            Utf8 | LargeUtf8 | Utf8View,
251
            Binary
252
            | LargeBinary
253
            | Utf8
254
            | LargeUtf8
255
            | Date32
256
            | Date64
257
            | Time32(Second)
258
            | Time32(Millisecond)
259
            | Time64(Microsecond)
260
            | Time64(Nanosecond)
261
            | Timestamp(Second, _)
262
            | Timestamp(Millisecond, _)
263
            | Timestamp(Microsecond, _)
264
            | Timestamp(Nanosecond, _)
265
            | Interval(_)
266
            | BinaryView,
267
0
        ) => true,
268
0
        (Utf8 | LargeUtf8, Utf8View) => true,
269
0
        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
270
0
        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
271
0
        (_, Utf8 | LargeUtf8) => from_type.is_primitive(),
272
0
        (_, Utf8View) => from_type.is_numeric(),
273
274
0
        (_, Binary | LargeBinary) => from_type.is_integer(),
275
276
        // start numeric casts
277
        (
278
            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
279
            | Float64,
280
            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
281
            | Float64,
282
0
        ) => true,
283
        // end numeric casts
284
285
        // temporal casts
286
0
        (Int32, Date32 | Date64 | Time32(_)) => true,
287
0
        (Date32, Int32 | Int64) => true,
288
0
        (Time32(_), Int32) => true,
289
0
        (Int64, Date64 | Date32 | Time64(_)) => true,
290
0
        (Date64, Int64 | Int32) => true,
291
0
        (Time64(_), Int64) => true,
292
0
        (Date32 | Date64, Date32 | Date64) => true,
293
        // time casts
294
0
        (Time32(_), Time32(_)) => true,
295
0
        (Time32(_), Time64(_)) => true,
296
0
        (Time64(_), Time64(_)) => true,
297
0
        (Time64(_), Time32(to_unit)) => {
298
0
            matches!(to_unit, Second | Millisecond)
299
        }
300
0
        (Timestamp(_, _), _) if to_type.is_numeric() => true,
301
0
        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
302
0
        (Date64, Timestamp(_, _)) => true,
303
0
        (Date32, Timestamp(_, _)) => true,
304
        (
305
            Timestamp(_, _),
306
            Timestamp(_, _)
307
            | Date32
308
            | Date64
309
            | Time32(Second)
310
            | Time32(Millisecond)
311
            | Time64(Microsecond)
312
            | Time64(Nanosecond),
313
0
        ) => true,
314
0
        (_, Duration(_)) if from_type.is_numeric() => true,
315
0
        (Duration(_), _) if to_type.is_numeric() => true,
316
0
        (Duration(_), Duration(_)) => true,
317
0
        (Interval(from_type), Int64) => {
318
0
            match from_type {
319
0
                YearMonth => true,
320
0
                DayTime => true,
321
0
                MonthDayNano => false, // Native type is i128
322
            }
323
        }
324
0
        (Int32, Interval(to_type)) => match to_type {
325
0
            YearMonth => true,
326
0
            DayTime => false,
327
0
            MonthDayNano => false,
328
        },
329
0
        (Duration(_), Interval(MonthDayNano)) => true,
330
0
        (Interval(MonthDayNano), Duration(_)) => true,
331
0
        (Interval(YearMonth), Interval(MonthDayNano)) => true,
332
0
        (Interval(DayTime), Interval(MonthDayNano)) => true,
333
0
        (_, _) => false,
334
    }
335
0
}
336
337
/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
338
///
339
/// See [`cast_with_options`] for more information
340
0
pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
341
0
    cast_with_options(array, to_type, &CastOptions::default())
342
0
}
343
344
0
fn cast_integer_to_decimal<
345
0
    T: ArrowPrimitiveType,
346
0
    D: DecimalType + ArrowPrimitiveType<Native = M>,
347
0
    M,
348
0
>(
349
0
    array: &PrimitiveArray<T>,
350
0
    precision: u8,
351
0
    scale: i8,
352
0
    base: M,
353
0
    cast_options: &CastOptions,
354
0
) -> Result<ArrayRef, ArrowError>
355
0
where
356
0
    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
357
0
    M: ArrowNativeTypeOp,
358
{
359
0
    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
360
0
        ArrowError::CastError(format!(
361
0
            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
362
0
            D::PREFIX,
363
0
            precision,
364
0
            scale,
365
0
        ))
366
0
    })?;
367
368
0
    let array = if scale < 0 {
369
0
        match cast_options.safe {
370
0
            true => array.unary_opt::<_, D>(|v| {
371
0
                v.as_()
372
0
                    .div_checked(scale_factor)
373
0
                    .ok()
374
0
                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
375
0
            }),
376
0
            false => array.try_unary::<_, D, _>(|v| {
377
0
                v.as_()
378
0
                    .div_checked(scale_factor)
379
0
                    .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
380
0
            })?,
381
        }
382
    } else {
383
0
        match cast_options.safe {
384
0
            true => array.unary_opt::<_, D>(|v| {
385
0
                v.as_()
386
0
                    .mul_checked(scale_factor)
387
0
                    .ok()
388
0
                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
389
0
            }),
390
0
            false => array.try_unary::<_, D, _>(|v| {
391
0
                v.as_()
392
0
                    .mul_checked(scale_factor)
393
0
                    .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
394
0
            })?,
395
        }
396
    };
397
398
0
    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
399
0
}
400
401
/// Cast the array from interval year month to month day nano
402
0
fn cast_interval_year_month_to_interval_month_day_nano(
403
0
    array: &dyn Array,
404
0
    _cast_options: &CastOptions,
405
0
) -> Result<ArrayRef, ArrowError> {
406
0
    let array = array.as_primitive::<IntervalYearMonthType>();
407
408
0
    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
409
0
        let months = IntervalYearMonthType::to_months(v);
410
0
        IntervalMonthDayNanoType::make_value(months, 0, 0)
411
0
    })))
412
0
}
413
414
/// Cast the array from interval day time to month day nano
415
0
fn cast_interval_day_time_to_interval_month_day_nano(
416
0
    array: &dyn Array,
417
0
    _cast_options: &CastOptions,
418
0
) -> Result<ArrayRef, ArrowError> {
419
0
    let array = array.as_primitive::<IntervalDayTimeType>();
420
0
    let mul = 1_000_000;
421
422
0
    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
423
0
        let (days, ms) = IntervalDayTimeType::to_parts(v);
424
0
        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
425
0
    })))
426
0
}
427
428
/// Cast the array from interval to duration
429
0
fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
430
0
    array: &dyn Array,
431
0
    cast_options: &CastOptions,
432
0
) -> Result<ArrayRef, ArrowError> {
433
0
    let array = array.as_primitive::<IntervalMonthDayNanoType>();
434
0
    let scale = match D::DATA_TYPE {
435
0
        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
436
0
        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
437
0
        DataType::Duration(TimeUnit::Microsecond) => 1_000,
438
0
        DataType::Duration(TimeUnit::Nanosecond) => 1,
439
0
        _ => unreachable!(),
440
    };
441
442
0
    if cast_options.safe {
443
0
        let iter = array.iter().map(|v| {
444
0
            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
445
0
        });
446
0
        Ok(Arc::new(unsafe {
447
0
            PrimitiveArray::<D>::from_trusted_len_iter(iter)
448
0
        }))
449
    } else {
450
0
        let vec = array
451
0
            .iter()
452
0
            .map(|v| {
453
0
                v.map(|v| match v.days == 0 && v.months == 0 {
454
0
                    true => Ok((v.nanoseconds) / scale),
455
0
                    _ => Err(ArrowError::ComputeError(
456
0
                        "Cannot convert interval containing non-zero months or days to duration"
457
0
                            .to_string(),
458
0
                    )),
459
0
                })
460
0
                .transpose()
461
0
            })
462
0
            .collect::<Result<Vec<_>, _>>()?;
463
0
        Ok(Arc::new(unsafe {
464
0
            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
465
0
        }))
466
    }
467
0
}
468
469
/// Cast the array from duration and interval
470
0
fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
471
0
    array: &dyn Array,
472
0
    cast_options: &CastOptions,
473
0
) -> Result<ArrayRef, ArrowError> {
474
0
    let array = array
475
0
        .as_any()
476
0
        .downcast_ref::<PrimitiveArray<D>>()
477
0
        .ok_or_else(|| {
478
0
            ArrowError::ComputeError(
479
0
                "Internal Error: Cannot cast duration to DurationArray of expected type"
480
0
                    .to_string(),
481
0
            )
482
0
        })?;
483
484
0
    let scale = match array.data_type() {
485
0
        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
486
0
        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
487
0
        DataType::Duration(TimeUnit::Microsecond) => 1_000,
488
0
        DataType::Duration(TimeUnit::Nanosecond) => 1,
489
0
        _ => unreachable!(),
490
    };
491
492
0
    if cast_options.safe {
493
0
        let iter = array.iter().map(|v| {
494
0
            v.and_then(|v| {
495
0
                v.checked_mul(scale)
496
0
                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
497
0
            })
498
0
        });
499
0
        Ok(Arc::new(unsafe {
500
0
            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
501
0
        }))
502
    } else {
503
0
        let vec = array
504
0
            .iter()
505
0
            .map(|v| {
506
0
                v.map(|v| {
507
0
                    if let Ok(v) = v.mul_checked(scale) {
508
0
                        Ok(IntervalMonthDayNano::new(0, 0, v))
509
                    } else {
510
0
                        Err(ArrowError::ComputeError(format!(
511
0
                            "Cannot cast to {:?}. Overflowing on {:?}",
512
0
                            IntervalMonthDayNanoType::DATA_TYPE,
513
0
                            v
514
0
                        )))
515
                    }
516
0
                })
517
0
                .transpose()
518
0
            })
519
0
            .collect::<Result<Vec<_>, _>>()?;
520
0
        Ok(Arc::new(unsafe {
521
0
            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
522
0
        }))
523
    }
524
0
}
525
526
/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
527
0
fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
528
0
    array: &dyn Array,
529
0
) -> Result<ArrayRef, ArrowError> {
530
0
    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
531
0
}
532
533
0
fn make_timestamp_array(
534
0
    array: &PrimitiveArray<Int64Type>,
535
0
    unit: TimeUnit,
536
0
    tz: Option<Arc<str>>,
537
0
) -> ArrayRef {
538
0
    match unit {
539
0
        TimeUnit::Second => Arc::new(
540
0
            array
541
0
                .reinterpret_cast::<TimestampSecondType>()
542
0
                .with_timezone_opt(tz),
543
0
        ),
544
0
        TimeUnit::Millisecond => Arc::new(
545
0
            array
546
0
                .reinterpret_cast::<TimestampMillisecondType>()
547
0
                .with_timezone_opt(tz),
548
0
        ),
549
0
        TimeUnit::Microsecond => Arc::new(
550
0
            array
551
0
                .reinterpret_cast::<TimestampMicrosecondType>()
552
0
                .with_timezone_opt(tz),
553
0
        ),
554
0
        TimeUnit::Nanosecond => Arc::new(
555
0
            array
556
0
                .reinterpret_cast::<TimestampNanosecondType>()
557
0
                .with_timezone_opt(tz),
558
0
        ),
559
    }
560
0
}
561
562
0
fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
563
0
    match unit {
564
0
        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
565
0
        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
566
0
        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
567
0
        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
568
    }
569
0
}
570
571
0
fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
572
0
    v: i64,
573
0
    tz: Option<Tz>,
574
0
) -> Result<NaiveTime, ArrowError> {
575
0
    let time = match tz {
576
0
        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
577
0
        None => as_datetime::<T>(v).map(|d| d.time()),
578
    };
579
580
0
    time.ok_or_else(|| {
581
0
        ArrowError::CastError(format!(
582
0
            "Failed to create naive time with {} {}",
583
0
            std::any::type_name::<T>(),
584
0
            v
585
0
        ))
586
0
    })
587
0
}
588
589
0
fn timestamp_to_date32<T: ArrowTimestampType>(
590
0
    array: &PrimitiveArray<T>,
591
0
) -> Result<ArrayRef, ArrowError> {
592
0
    let err = |x: i64| {
593
0
        ArrowError::CastError(format!(
594
0
            "Cannot convert {} {x} to datetime",
595
0
            std::any::type_name::<T>()
596
0
        ))
597
0
    };
598
599
0
    let array: Date32Array = match array.timezone() {
600
0
        Some(tz) => {
601
0
            let tz: Tz = tz.parse()?;
602
0
            array.try_unary(|x| {
603
0
                as_datetime_with_timezone::<T>(x, tz)
604
0
                    .ok_or_else(|| err(x))
605
0
                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
606
0
            })?
607
        }
608
0
        None => array.try_unary(|x| {
609
0
            as_datetime::<T>(x)
610
0
                .ok_or_else(|| err(x))
611
0
                .map(|d| Date32Type::from_naive_date(d.date()))
612
0
        })?,
613
    };
614
0
    Ok(Arc::new(array))
615
0
}
616
617
/// Try to cast `array` to `to_type` if possible.
618
///
619
/// Returns a new Array with type `to_type` if possible.
620
///
621
/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
622
///
623
/// # Behavior
624
/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
625
/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
626
///   short variants are accepted, other strings return null or error
627
/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
628
///   in integer casts return null
629
/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
630
/// * `List` to `List`: the underlying data type is cast
631
/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
632
///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
633
/// * Primitive to `List`: a list array with 1 value per slot is created
634
/// * `Date32` and `Date64`: precision lost when going to higher interval
635
/// * `Time32 and `Time64`: precision lost when going to higher interval
636
/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
637
/// * Temporal to/from backing Primitive: zero-copy with data type change
638
/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
639
///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
640
/// * `Decimal` to `Float32/Float64` is lossy and values outside the representable
641
///   range become `INFINITY` or `-INFINITY` without error.
642
///
643
/// Unsupported Casts (check with `can_cast_types` before calling):
644
/// * To or from `StructArray`
645
/// * `List` to `Primitive`
646
/// * `Interval` and `Duration`
647
///
648
/// # Durations and Intervals
649
///
650
/// Casting integer types directly to interval types such as
651
/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
652
/// is ambiguous. For example, the integer  could represent either nanoseconds
653
/// or months.
654
///
655
/// To cast an integer type to an interval type, first convert to a Duration
656
/// type, and then cast that to the desired interval type.
657
///
658
/// For example, to convert an `Int64` representing nanoseconds to an
659
/// `IntervalMonthDayNano` you would first convert the `Int64` to a
660
/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
661
///
662
/// # Timestamps and Timezones
663
///
664
/// Timestamps are stored with an optional timezone in Arrow.
665
///
666
/// ## Casting timestamps to a timestamp without timezone / UTC
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
/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
674
/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
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();
677
/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
678
/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
679
/// // use display to show them (note has no trailing Z)
680
/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
681
/// ```
682
///
683
/// ## Casting timestamps to a timestamp with timezone
684
///
685
/// Similarly to the previous example, if you cast numeric values to a timestamp
686
/// with timezone, the cast kernel will not change the underlying values
687
/// but display and other functions will interpret them as being in the provided timezone.
688
///
689
/// ```
690
/// # use arrow_array::Int64Array;
691
/// # use arrow_array::types::TimestampSecondType;
692
/// # use arrow_cast::{cast, display};
693
/// # use arrow_array::cast::AsArray;
694
/// # use arrow_schema::{DataType, TimeUnit};
695
/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
696
/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
697
/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
698
/// let b = cast(&a, &data_type).unwrap();
699
/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
700
/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
701
/// // displayed in the target timezone (note the offset -05:00)
702
/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
703
/// ```
704
/// # Casting timestamps without timezone to timestamps with timezone
705
///
706
/// When casting from a timestamp without timezone to a timestamp with
707
/// timezone, the cast kernel interprets the timestamp values as being in
708
/// the destination timezone and then adjusts the underlying value to UTC as required
709
///
710
/// However, note that when casting from a timestamp with timezone BACK to a
711
/// timestamp without timezone the cast kernel does not adjust the values.
712
///
713
/// Thus round trip casting a timestamp without timezone to a timestamp with
714
/// timezone and back to a timestamp without timezone results in different
715
/// values than the starting values.
716
///
717
/// ```
718
/// # use arrow_array::Int64Array;
719
/// # use arrow_array::types::{TimestampSecondType};
720
/// # use arrow_cast::{cast, display};
721
/// # use arrow_array::cast::AsArray;
722
/// # use arrow_schema::{DataType, TimeUnit};
723
/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
724
/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
725
/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
726
/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
727
/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
728
/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
729
/// // displayed without a timezone (note lack of offset or Z)
730
/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
731
///
732
/// // Convert timestamps without a timezone to timestamps with a timezone
733
/// let c = cast(&b, &data_type_tz).unwrap();
734
/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
735
/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
736
/// // displayed with the target timezone offset (-05:00)
737
/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
738
///
739
/// // Convert from timestamp with timezone back to timestamp without timezone
740
/// let d = cast(&c, &data_type).unwrap();
741
/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
742
/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
743
/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
744
/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
745
/// ```
746
0
pub fn cast_with_options(
747
0
    array: &dyn Array,
748
0
    to_type: &DataType,
749
0
    cast_options: &CastOptions,
750
0
) -> Result<ArrayRef, ArrowError> {
751
    use DataType::*;
752
0
    let from_type = array.data_type();
753
    // clone array if types are the same
754
0
    if from_type == to_type {
755
0
        return Ok(make_array(array.to_data()));
756
0
    }
757
0
    match (from_type, to_type) {
758
        (
759
            Null,
760
            Boolean
761
            | Int8
762
            | UInt8
763
            | Int16
764
            | UInt16
765
            | Int32
766
            | UInt32
767
            | Float32
768
            | Date32
769
            | Time32(_)
770
            | Int64
771
            | UInt64
772
            | Float64
773
            | Date64
774
            | Timestamp(_, _)
775
            | Time64(_)
776
            | Duration(_)
777
            | Interval(_)
778
            | FixedSizeBinary(_)
779
            | Binary
780
            | Utf8
781
            | LargeBinary
782
            | LargeUtf8
783
            | BinaryView
784
            | Utf8View
785
            | List(_)
786
            | LargeList(_)
787
            | FixedSizeList(_, _)
788
            | Struct(_)
789
            | Map(_, _)
790
            | Dictionary(_, _),
791
0
        ) => Ok(new_null_array(to_type, array.len())),
792
0
        (Dictionary(index_type, _), _) => match **index_type {
793
0
            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
794
0
            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
795
0
            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
796
0
            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
797
0
            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
798
0
            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
799
0
            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
800
0
            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
801
0
            _ => Err(ArrowError::CastError(format!(
802
0
                "Casting from dictionary type {from_type:?} to {to_type:?} not supported",
803
0
            ))),
804
        },
805
0
        (_, Dictionary(index_type, value_type)) => match **index_type {
806
0
            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
807
0
            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
808
0
            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
809
0
            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
810
0
            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
811
0
            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
812
0
            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
813
0
            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
814
0
            _ => Err(ArrowError::CastError(format!(
815
0
                "Casting from type {from_type:?} to dictionary type {to_type:?} not supported",
816
0
            ))),
817
        },
818
0
        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
819
0
        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
820
0
        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
821
0
        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
822
0
        (List(_), FixedSizeList(field, size)) => {
823
0
            let array = array.as_list::<i32>();
824
0
            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
825
        }
826
0
        (LargeList(_), FixedSizeList(field, size)) => {
827
0
            let array = array.as_list::<i64>();
828
0
            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
829
        }
830
0
        (List(_) | LargeList(_), _) => match to_type {
831
0
            Utf8 => value_to_string::<i32>(array, cast_options),
832
0
            LargeUtf8 => value_to_string::<i64>(array, cast_options),
833
0
            _ => Err(ArrowError::CastError(
834
0
                "Cannot cast list to non-list data types".to_string(),
835
0
            )),
836
        },
837
0
        (FixedSizeList(list_from, size), List(list_to)) => {
838
0
            if list_to.data_type() != list_from.data_type() {
839
                // To transform inner type, can first cast to FSL with new inner type.
840
0
                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
841
0
                let array = cast_with_options(array, &fsl_to, cast_options)?;
842
0
                cast_fixed_size_list_to_list::<i32>(array.as_ref())
843
            } else {
844
0
                cast_fixed_size_list_to_list::<i32>(array)
845
            }
846
        }
847
0
        (FixedSizeList(list_from, size), LargeList(list_to)) => {
848
0
            if list_to.data_type() != list_from.data_type() {
849
                // To transform inner type, can first cast to FSL with new inner type.
850
0
                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
851
0
                let array = cast_with_options(array, &fsl_to, cast_options)?;
852
0
                cast_fixed_size_list_to_list::<i64>(array.as_ref())
853
            } else {
854
0
                cast_fixed_size_list_to_list::<i64>(array)
855
            }
856
        }
857
0
        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
858
0
            if size_from != size_to {
859
0
                return Err(ArrowError::CastError(
860
0
                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
861
0
                ));
862
0
            }
863
0
            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
864
0
            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
865
0
            Ok(Arc::new(FixedSizeListArray::try_new(
866
0
                list_to.clone(),
867
0
                *size_from,
868
0
                values,
869
0
                array.nulls().cloned(),
870
0
            )?))
871
        }
872
0
        (_, List(ref to)) => cast_values_to_list::<i32>(array, to, cast_options),
873
0
        (_, LargeList(ref to)) => cast_values_to_list::<i64>(array, to, cast_options),
874
0
        (_, FixedSizeList(ref to, size)) if *size == 1 => {
875
0
            cast_values_to_fixed_size_list(array, to, *size, cast_options)
876
        }
877
0
        (FixedSizeList(_, size), _) if *size == 1 => {
878
0
            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
879
        }
880
0
        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
881
0
            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
882
        }
883
        // Decimal to decimal, same width
884
0
        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
885
0
            cast_decimal_to_decimal_same_type::<Decimal32Type>(
886
0
                array.as_primitive(),
887
0
                *p1,
888
0
                *s1,
889
0
                *p2,
890
0
                *s2,
891
0
                cast_options,
892
            )
893
        }
894
0
        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
895
0
            cast_decimal_to_decimal_same_type::<Decimal64Type>(
896
0
                array.as_primitive(),
897
0
                *p1,
898
0
                *s1,
899
0
                *p2,
900
0
                *s2,
901
0
                cast_options,
902
            )
903
        }
904
0
        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
905
0
            cast_decimal_to_decimal_same_type::<Decimal128Type>(
906
0
                array.as_primitive(),
907
0
                *p1,
908
0
                *s1,
909
0
                *p2,
910
0
                *s2,
911
0
                cast_options,
912
            )
913
        }
914
0
        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
915
0
            cast_decimal_to_decimal_same_type::<Decimal256Type>(
916
0
                array.as_primitive(),
917
0
                *p1,
918
0
                *s1,
919
0
                *p2,
920
0
                *s2,
921
0
                cast_options,
922
            )
923
        }
924
        // Decimal to decimal, different width
925
0
        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
926
0
            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
927
0
                array.as_primitive(),
928
0
                *p1,
929
0
                *s1,
930
0
                *p2,
931
0
                *s2,
932
0
                cast_options,
933
            )
934
        }
935
0
        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
936
0
            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
937
0
                array.as_primitive(),
938
0
                *p1,
939
0
                *s1,
940
0
                *p2,
941
0
                *s2,
942
0
                cast_options,
943
            )
944
        }
945
0
        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
946
0
            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
947
0
                array.as_primitive(),
948
0
                *p1,
949
0
                *s1,
950
0
                *p2,
951
0
                *s2,
952
0
                cast_options,
953
            )
954
        }
955
0
        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
956
0
            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
957
0
                array.as_primitive(),
958
0
                *p1,
959
0
                *s1,
960
0
                *p2,
961
0
                *s2,
962
0
                cast_options,
963
            )
964
        }
965
0
        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
966
0
            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
967
0
                array.as_primitive(),
968
0
                *p1,
969
0
                *s1,
970
0
                *p2,
971
0
                *s2,
972
0
                cast_options,
973
            )
974
        }
975
0
        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
976
0
            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
977
0
                array.as_primitive(),
978
0
                *p1,
979
0
                *s1,
980
0
                *p2,
981
0
                *s2,
982
0
                cast_options,
983
            )
984
        }
985
0
        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
986
0
            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
987
0
                array.as_primitive(),
988
0
                *p1,
989
0
                *s1,
990
0
                *p2,
991
0
                *s2,
992
0
                cast_options,
993
            )
994
        }
995
0
        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
996
0
            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
997
0
                array.as_primitive(),
998
0
                *p1,
999
0
                *s1,
1000
0
                *p2,
1001
0
                *s2,
1002
0
                cast_options,
1003
            )
1004
        }
1005
0
        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1006
0
            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1007
0
                array.as_primitive(),
1008
0
                *p1,
1009
0
                *s1,
1010
0
                *p2,
1011
0
                *s2,
1012
0
                cast_options,
1013
            )
1014
        }
1015
0
        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1016
0
            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1017
0
                array.as_primitive(),
1018
0
                *p1,
1019
0
                *s1,
1020
0
                *p2,
1021
0
                *s2,
1022
0
                cast_options,
1023
            )
1024
        }
1025
0
        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1026
0
            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1027
0
                array.as_primitive(),
1028
0
                *p1,
1029
0
                *s1,
1030
0
                *p2,
1031
0
                *s2,
1032
0
                cast_options,
1033
            )
1034
        }
1035
0
        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1036
0
            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1037
0
                array.as_primitive(),
1038
0
                *p1,
1039
0
                *s1,
1040
0
                *p2,
1041
0
                *s2,
1042
0
                cast_options,
1043
            )
1044
        }
1045
        // Decimal to non-decimal
1046
0
        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1047
0
            cast_from_decimal::<Decimal32Type, _>(
1048
0
                array,
1049
                10_i32,
1050
0
                scale,
1051
0
                from_type,
1052
0
                to_type,
1053
0
                |x: i32| x as f64,
1054
0
                cast_options,
1055
            )
1056
        }
1057
0
        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1058
0
            cast_from_decimal::<Decimal64Type, _>(
1059
0
                array,
1060
                10_i64,
1061
0
                scale,
1062
0
                from_type,
1063
0
                to_type,
1064
0
                |x: i64| x as f64,
1065
0
                cast_options,
1066
            )
1067
        }
1068
0
        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1069
0
            cast_from_decimal::<Decimal128Type, _>(
1070
0
                array,
1071
                10_i128,
1072
0
                scale,
1073
0
                from_type,
1074
0
                to_type,
1075
0
                |x: i128| x as f64,
1076
0
                cast_options,
1077
            )
1078
        }
1079
0
        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1080
0
            cast_from_decimal::<Decimal256Type, _>(
1081
0
                array,
1082
0
                i256::from_i128(10_i128),
1083
0
                scale,
1084
0
                from_type,
1085
0
                to_type,
1086
0
                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1087
0
                cast_options,
1088
            )
1089
        }
1090
        // Non-decimal to decimal
1091
0
        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1092
0
            cast_to_decimal::<Decimal32Type, _>(
1093
0
                array,
1094
                10_i32,
1095
0
                precision,
1096
0
                scale,
1097
0
                from_type,
1098
0
                to_type,
1099
0
                cast_options,
1100
            )
1101
        }
1102
0
        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1103
0
            cast_to_decimal::<Decimal64Type, _>(
1104
0
                array,
1105
                10_i64,
1106
0
                precision,
1107
0
                scale,
1108
0
                from_type,
1109
0
                to_type,
1110
0
                cast_options,
1111
            )
1112
        }
1113
0
        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1114
0
            cast_to_decimal::<Decimal128Type, _>(
1115
0
                array,
1116
                10_i128,
1117
0
                precision,
1118
0
                scale,
1119
0
                from_type,
1120
0
                to_type,
1121
0
                cast_options,
1122
            )
1123
        }
1124
0
        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1125
0
            cast_to_decimal::<Decimal256Type, _>(
1126
0
                array,
1127
0
                i256::from_i128(10_i128),
1128
0
                precision,
1129
0
                scale,
1130
0
                from_type,
1131
0
                to_type,
1132
0
                cast_options,
1133
            )
1134
        }
1135
0
        (Struct(_), Struct(to_fields)) => {
1136
0
            let array = array.as_struct();
1137
0
            let fields = array
1138
0
                .columns()
1139
0
                .iter()
1140
0
                .zip(to_fields.iter())
1141
0
                .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
1142
0
                .collect::<Result<Vec<ArrayRef>, ArrowError>>()?;
1143
0
            let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
1144
0
            Ok(Arc::new(array) as ArrayRef)
1145
        }
1146
0
        (Struct(_), _) => Err(ArrowError::CastError(format!(
1147
0
            "Casting from {from_type:?} to {to_type:?} not supported"
1148
0
        ))),
1149
0
        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1150
0
            "Casting from {from_type:?} to {to_type:?} not supported"
1151
0
        ))),
1152
0
        (_, Boolean) => match from_type {
1153
0
            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1154
0
            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1155
0
            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1156
0
            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1157
0
            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1158
0
            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1159
0
            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1160
0
            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1161
0
            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1162
0
            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1163
0
            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1164
0
            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1165
0
            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1166
0
            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1167
0
            _ => Err(ArrowError::CastError(format!(
1168
0
                "Casting from {from_type:?} to {to_type:?} not supported",
1169
0
            ))),
1170
        },
1171
0
        (Boolean, _) => match to_type {
1172
0
            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1173
0
            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1174
0
            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1175
0
            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1176
0
            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1177
0
            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1178
0
            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1179
0
            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1180
0
            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1181
0
            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1182
0
            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1183
0
            Utf8View => value_to_string_view(array, cast_options),
1184
0
            Utf8 => value_to_string::<i32>(array, cast_options),
1185
0
            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1186
0
            _ => Err(ArrowError::CastError(format!(
1187
0
                "Casting from {from_type:?} to {to_type:?} not supported",
1188
0
            ))),
1189
        },
1190
0
        (Utf8, _) => match to_type {
1191
0
            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1192
0
            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1193
0
            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1194
0
            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1195
0
            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1196
0
            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1197
0
            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1198
0
            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1199
0
            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1200
0
            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1201
0
            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1202
0
            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1203
0
            Binary => Ok(Arc::new(BinaryArray::from(
1204
0
                array.as_string::<i32>().clone(),
1205
0
            ))),
1206
            LargeBinary => {
1207
0
                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1208
0
                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1209
            }
1210
0
            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1211
0
            BinaryView => Ok(Arc::new(
1212
0
                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1213
0
            )),
1214
0
            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1215
0
            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1216
            Time32(TimeUnit::Millisecond) => {
1217
0
                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1218
            }
1219
            Time64(TimeUnit::Microsecond) => {
1220
0
                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1221
            }
1222
            Time64(TimeUnit::Nanosecond) => {
1223
0
                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1224
            }
1225
0
            Timestamp(TimeUnit::Second, to_tz) => {
1226
0
                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1227
            }
1228
0
            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1229
0
                i32,
1230
0
                TimestampMillisecondType,
1231
0
            >(array, to_tz, cast_options),
1232
0
            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1233
0
                i32,
1234
0
                TimestampMicrosecondType,
1235
0
            >(array, to_tz, cast_options),
1236
0
            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1237
0
                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1238
            }
1239
            Interval(IntervalUnit::YearMonth) => {
1240
0
                cast_string_to_year_month_interval::<i32>(array, cast_options)
1241
            }
1242
            Interval(IntervalUnit::DayTime) => {
1243
0
                cast_string_to_day_time_interval::<i32>(array, cast_options)
1244
            }
1245
            Interval(IntervalUnit::MonthDayNano) => {
1246
0
                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1247
            }
1248
0
            _ => Err(ArrowError::CastError(format!(
1249
0
                "Casting from {from_type:?} to {to_type:?} not supported",
1250
0
            ))),
1251
        },
1252
0
        (Utf8View, _) => match to_type {
1253
0
            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1254
0
            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1255
0
            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1256
0
            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1257
0
            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1258
0
            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1259
0
            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1260
0
            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1261
0
            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1262
0
            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1263
0
            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1264
0
            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1265
0
            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1266
0
            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1267
0
            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1268
0
            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1269
0
            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1270
0
            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1271
            Time32(TimeUnit::Millisecond) => {
1272
0
                parse_string_view::<Time32MillisecondType>(array, cast_options)
1273
            }
1274
            Time64(TimeUnit::Microsecond) => {
1275
0
                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1276
            }
1277
            Time64(TimeUnit::Nanosecond) => {
1278
0
                parse_string_view::<Time64NanosecondType>(array, cast_options)
1279
            }
1280
0
            Timestamp(TimeUnit::Second, to_tz) => {
1281
0
                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1282
            }
1283
0
            Timestamp(TimeUnit::Millisecond, to_tz) => {
1284
0
                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1285
            }
1286
0
            Timestamp(TimeUnit::Microsecond, to_tz) => {
1287
0
                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1288
            }
1289
0
            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1290
0
                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1291
            }
1292
            Interval(IntervalUnit::YearMonth) => {
1293
0
                cast_view_to_year_month_interval(array, cast_options)
1294
            }
1295
0
            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1296
            Interval(IntervalUnit::MonthDayNano) => {
1297
0
                cast_view_to_month_day_nano_interval(array, cast_options)
1298
            }
1299
0
            _ => Err(ArrowError::CastError(format!(
1300
0
                "Casting from {from_type:?} to {to_type:?} not supported",
1301
0
            ))),
1302
        },
1303
0
        (LargeUtf8, _) => match to_type {
1304
0
            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1305
0
            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1306
0
            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1307
0
            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1308
0
            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1309
0
            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1310
0
            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1311
0
            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1312
0
            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1313
0
            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1314
0
            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1315
0
            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1316
0
            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1317
            Binary => {
1318
0
                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1319
0
                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1320
            }
1321
0
            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1322
0
                array.as_string::<i64>().clone(),
1323
0
            ))),
1324
0
            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1325
0
            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1326
0
                array
1327
0
                    .as_string::<i64>()
1328
0
                    .into_iter()
1329
0
                    .map(|x| x.map(|x| x.as_bytes()))
1330
0
                    .collect::<Vec<_>>(),
1331
            ))),
1332
0
            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1333
            Time32(TimeUnit::Millisecond) => {
1334
0
                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1335
            }
1336
            Time64(TimeUnit::Microsecond) => {
1337
0
                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1338
            }
1339
            Time64(TimeUnit::Nanosecond) => {
1340
0
                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1341
            }
1342
0
            Timestamp(TimeUnit::Second, to_tz) => {
1343
0
                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1344
            }
1345
0
            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1346
0
                i64,
1347
0
                TimestampMillisecondType,
1348
0
            >(array, to_tz, cast_options),
1349
0
            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1350
0
                i64,
1351
0
                TimestampMicrosecondType,
1352
0
            >(array, to_tz, cast_options),
1353
0
            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1354
0
                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1355
            }
1356
            Interval(IntervalUnit::YearMonth) => {
1357
0
                cast_string_to_year_month_interval::<i64>(array, cast_options)
1358
            }
1359
            Interval(IntervalUnit::DayTime) => {
1360
0
                cast_string_to_day_time_interval::<i64>(array, cast_options)
1361
            }
1362
            Interval(IntervalUnit::MonthDayNano) => {
1363
0
                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1364
            }
1365
0
            _ => Err(ArrowError::CastError(format!(
1366
0
                "Casting from {from_type:?} to {to_type:?} not supported",
1367
0
            ))),
1368
        },
1369
0
        (Binary, _) => match to_type {
1370
0
            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1371
            LargeUtf8 => {
1372
0
                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1373
0
                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1374
            }
1375
0
            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1376
0
            FixedSizeBinary(size) => {
1377
0
                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1378
            }
1379
0
            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1380
0
            Utf8View => Ok(Arc::new(StringViewArray::from(
1381
0
                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1382
            ))),
1383
0
            _ => Err(ArrowError::CastError(format!(
1384
0
                "Casting from {from_type:?} to {to_type:?} not supported",
1385
0
            ))),
1386
        },
1387
0
        (LargeBinary, _) => match to_type {
1388
            Utf8 => {
1389
0
                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1390
0
                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1391
            }
1392
0
            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1393
0
            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1394
0
            FixedSizeBinary(size) => {
1395
0
                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1396
            }
1397
0
            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1398
            Utf8View => {
1399
0
                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1400
0
                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1401
            }
1402
0
            _ => Err(ArrowError::CastError(format!(
1403
0
                "Casting from {from_type:?} to {to_type:?} not supported",
1404
0
            ))),
1405
        },
1406
0
        (FixedSizeBinary(size), _) => match to_type {
1407
0
            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1408
0
            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1409
0
            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1410
0
            _ => Err(ArrowError::CastError(format!(
1411
0
                "Casting from {from_type:?} to {to_type:?} not supported",
1412
0
            ))),
1413
        },
1414
0
        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1415
        (BinaryView, LargeBinary) => {
1416
0
            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1417
        }
1418
        (BinaryView, Utf8) => {
1419
0
            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1420
0
            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1421
        }
1422
        (BinaryView, LargeUtf8) => {
1423
0
            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1424
0
            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1425
        }
1426
        (BinaryView, Utf8View) => {
1427
0
            Ok(Arc::new(array.as_binary_view().clone().to_string_view()?) as ArrayRef)
1428
        }
1429
0
        (BinaryView, _) => Err(ArrowError::CastError(format!(
1430
0
            "Casting from {from_type:?} to {to_type:?} not supported",
1431
0
        ))),
1432
0
        (from_type, Utf8View) if from_type.is_primitive() => {
1433
0
            value_to_string_view(array, cast_options)
1434
        }
1435
0
        (from_type, LargeUtf8) if from_type.is_primitive() => {
1436
0
            value_to_string::<i64>(array, cast_options)
1437
        }
1438
0
        (from_type, Utf8) if from_type.is_primitive() => {
1439
0
            value_to_string::<i32>(array, cast_options)
1440
        }
1441
0
        (from_type, Binary) if from_type.is_integer() => match from_type {
1442
0
            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1443
0
            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1444
0
            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1445
0
            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1446
0
            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1447
0
            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1448
0
            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1449
0
            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1450
0
            _ => unreachable!(),
1451
        },
1452
0
        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1453
0
            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1454
0
            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1455
0
            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1456
0
            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1457
0
            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1458
0
            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1459
0
            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1460
0
            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1461
0
            _ => unreachable!(),
1462
        },
1463
        // start numeric casts
1464
0
        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1465
0
        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1466
0
        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1467
0
        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1468
0
        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1469
0
        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1470
0
        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1471
0
        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1472
0
        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1473
0
        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1474
1475
0
        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1476
0
        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1477
0
        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1478
0
        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1479
0
        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1480
0
        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1481
0
        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1482
0
        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1483
0
        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1484
0
        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1485
1486
0
        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1487
0
        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1488
0
        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1489
0
        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1490
0
        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1491
0
        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1492
0
        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1493
0
        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1494
0
        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1495
0
        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1496
1497
0
        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1498
0
        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1499
0
        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1500
0
        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1501
0
        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1502
0
        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1503
0
        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1504
0
        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1505
0
        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1506
0
        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1507
1508
0
        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1509
0
        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1510
0
        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1511
0
        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1512
0
        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1513
0
        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1514
0
        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1515
0
        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1516
0
        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1517
0
        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1518
1519
0
        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1520
0
        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1521
0
        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1522
0
        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1523
0
        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1524
0
        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1525
0
        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1526
0
        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1527
0
        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1528
0
        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1529
1530
0
        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1531
0
        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1532
0
        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1533
0
        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1534
0
        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1535
0
        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1536
0
        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1537
0
        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1538
0
        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1539
0
        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1540
1541
0
        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1542
0
        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1543
0
        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1544
0
        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1545
0
        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1546
0
        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1547
0
        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1548
0
        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1549
0
        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1550
0
        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1551
1552
0
        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1553
0
        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1554
0
        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1555
0
        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1556
0
        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1557
0
        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1558
0
        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1559
0
        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1560
0
        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1561
0
        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1562
1563
0
        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1564
0
        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1565
0
        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1566
0
        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1567
0
        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1568
0
        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1569
0
        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1570
0
        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1571
0
        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1572
0
        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1573
1574
0
        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1575
0
        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1576
0
        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1577
0
        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1578
0
        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1579
0
        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1580
0
        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1581
0
        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1582
0
        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1583
0
        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1584
        // end numeric casts
1585
1586
        // temporal casts
1587
0
        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1588
0
        (Int32, Date64) => cast_with_options(
1589
0
            &cast_with_options(array, &Date32, cast_options)?,
1590
0
            &Date64,
1591
0
            cast_options,
1592
        ),
1593
        (Int32, Time32(TimeUnit::Second)) => {
1594
0
            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1595
        }
1596
        (Int32, Time32(TimeUnit::Millisecond)) => {
1597
0
            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1598
        }
1599
        // No support for microsecond/nanosecond with i32
1600
0
        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1601
0
        (Date32, Int64) => cast_with_options(
1602
0
            &cast_with_options(array, &Int32, cast_options)?,
1603
0
            &Int64,
1604
0
            cast_options,
1605
        ),
1606
        (Time32(TimeUnit::Second), Int32) => {
1607
0
            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1608
        }
1609
        (Time32(TimeUnit::Millisecond), Int32) => {
1610
0
            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1611
        }
1612
0
        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1613
0
        (Int64, Date32) => cast_with_options(
1614
0
            &cast_with_options(array, &Int32, cast_options)?,
1615
0
            &Date32,
1616
0
            cast_options,
1617
        ),
1618
        // No support for second/milliseconds with i64
1619
        (Int64, Time64(TimeUnit::Microsecond)) => {
1620
0
            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1621
        }
1622
        (Int64, Time64(TimeUnit::Nanosecond)) => {
1623
0
            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1624
        }
1625
1626
0
        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1627
0
        (Date64, Int32) => cast_with_options(
1628
0
            &cast_with_options(array, &Int64, cast_options)?,
1629
0
            &Int32,
1630
0
            cast_options,
1631
        ),
1632
        (Time64(TimeUnit::Microsecond), Int64) => {
1633
0
            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1634
        }
1635
        (Time64(TimeUnit::Nanosecond), Int64) => {
1636
0
            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1637
        }
1638
0
        (Date32, Date64) => Ok(Arc::new(
1639
0
            array
1640
0
                .as_primitive::<Date32Type>()
1641
0
                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1642
        )),
1643
0
        (Date64, Date32) => Ok(Arc::new(
1644
0
            array
1645
0
                .as_primitive::<Date64Type>()
1646
0
                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1647
        )),
1648
1649
0
        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1650
0
            array
1651
0
                .as_primitive::<Time32SecondType>()
1652
0
                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1653
        )),
1654
0
        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1655
0
            array
1656
0
                .as_primitive::<Time32SecondType>()
1657
0
                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1658
        )),
1659
0
        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1660
0
            array
1661
0
                .as_primitive::<Time32SecondType>()
1662
0
                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1663
        )),
1664
1665
0
        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1666
0
            array
1667
0
                .as_primitive::<Time32MillisecondType>()
1668
0
                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1669
        )),
1670
0
        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1671
0
            array
1672
0
                .as_primitive::<Time32MillisecondType>()
1673
0
                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1674
        )),
1675
0
        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1676
0
            array
1677
0
                .as_primitive::<Time32MillisecondType>()
1678
0
                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1679
        )),
1680
1681
0
        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1682
0
            array
1683
0
                .as_primitive::<Time64MicrosecondType>()
1684
0
                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1685
        )),
1686
0
        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1687
0
            array
1688
0
                .as_primitive::<Time64MicrosecondType>()
1689
0
                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1690
        )),
1691
0
        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1692
0
            array
1693
0
                .as_primitive::<Time64MicrosecondType>()
1694
0
                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1695
        )),
1696
1697
0
        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1698
0
            array
1699
0
                .as_primitive::<Time64NanosecondType>()
1700
0
                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1701
        )),
1702
0
        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1703
0
            array
1704
0
                .as_primitive::<Time64NanosecondType>()
1705
0
                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1706
        )),
1707
0
        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1708
0
            array
1709
0
                .as_primitive::<Time64NanosecondType>()
1710
0
                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1711
        )),
1712
1713
        // Timestamp to integer/floating/decimals
1714
0
        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1715
0
            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1716
0
            cast_with_options(&array, to_type, cast_options)
1717
        }
1718
0
        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1719
0
            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1720
0
            cast_with_options(&array, to_type, cast_options)
1721
        }
1722
0
        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1723
0
            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1724
0
            cast_with_options(&array, to_type, cast_options)
1725
        }
1726
0
        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1727
0
            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1728
0
            cast_with_options(&array, to_type, cast_options)
1729
        }
1730
1731
0
        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1732
0
            let array = cast_with_options(array, &Int64, cast_options)?;
1733
0
            Ok(make_timestamp_array(
1734
0
                array.as_primitive(),
1735
0
                *unit,
1736
0
                tz.clone(),
1737
0
            ))
1738
        }
1739
1740
0
        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1741
0
            let array = cast_with_options(array, &Int64, cast_options)?;
1742
0
            let time_array = array.as_primitive::<Int64Type>();
1743
0
            let from_size = time_unit_multiple(from_unit);
1744
0
            let to_size = time_unit_multiple(to_unit);
1745
            // we either divide or multiply, depending on size of each unit
1746
            // units are never the same when the types are the same
1747
0
            let converted = match from_size.cmp(&to_size) {
1748
                Ordering::Greater => {
1749
0
                    let divisor = from_size / to_size;
1750
0
                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1751
                }
1752
0
                Ordering::Equal => time_array.clone(),
1753
                Ordering::Less => {
1754
0
                    let mul = to_size / from_size;
1755
0
                    if cast_options.safe {
1756
0
                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1757
                    } else {
1758
0
                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1759
                    }
1760
                }
1761
            };
1762
            // Normalize timezone
1763
0
            let adjusted = match (from_tz, to_tz) {
1764
                // Only this case needs to be adjusted because we're casting from
1765
                // unknown time offset to some time offset, we want the time to be
1766
                // unchanged.
1767
                //
1768
                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1769
0
                (None, Some(to_tz)) => {
1770
0
                    let to_tz: Tz = to_tz.parse()?;
1771
0
                    match to_unit {
1772
0
                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1773
0
                            converted,
1774
0
                            &to_tz,
1775
0
                            cast_options,
1776
0
                        )?,
1777
0
                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1778
0
                            TimestampMillisecondType,
1779
0
                        >(
1780
0
                            converted, &to_tz, cast_options
1781
0
                        )?,
1782
0
                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1783
0
                            TimestampMicrosecondType,
1784
0
                        >(
1785
0
                            converted, &to_tz, cast_options
1786
0
                        )?,
1787
0
                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1788
0
                            TimestampNanosecondType,
1789
0
                        >(
1790
0
                            converted, &to_tz, cast_options
1791
0
                        )?,
1792
                    }
1793
                }
1794
0
                _ => converted,
1795
            };
1796
0
            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1797
        }
1798
        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1799
0
            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1800
        }
1801
        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1802
0
            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1803
        }
1804
        (Timestamp(TimeUnit::Second, _), Date32) => {
1805
0
            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1806
        }
1807
        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1808
0
            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1809
        }
1810
0
        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1811
            true => {
1812
                // change error to None
1813
0
                array
1814
0
                    .as_primitive::<TimestampSecondType>()
1815
0
                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1816
            }
1817
0
            false => array
1818
0
                .as_primitive::<TimestampSecondType>()
1819
0
                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1820
        })),
1821
        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1822
0
            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1823
        }
1824
0
        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1825
0
            array
1826
0
                .as_primitive::<TimestampMicrosecondType>()
1827
0
                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1828
        )),
1829
0
        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1830
0
            array
1831
0
                .as_primitive::<TimestampNanosecondType>()
1832
0
                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1833
        )),
1834
0
        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1835
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1836
0
            Ok(Arc::new(
1837
0
                array
1838
0
                    .as_primitive::<TimestampSecondType>()
1839
0
                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1840
0
                        Ok(time_to_time64us(as_time_res_with_timezone::<
1841
0
                            TimestampSecondType,
1842
0
                        >(x, tz)?))
1843
0
                    })?,
1844
            ))
1845
        }
1846
0
        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1847
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1848
0
            Ok(Arc::new(
1849
0
                array
1850
0
                    .as_primitive::<TimestampSecondType>()
1851
0
                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1852
0
                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1853
0
                            TimestampSecondType,
1854
0
                        >(x, tz)?))
1855
0
                    })?,
1856
            ))
1857
        }
1858
0
        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1859
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1860
0
            Ok(Arc::new(
1861
0
                array
1862
0
                    .as_primitive::<TimestampMillisecondType>()
1863
0
                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1864
0
                        Ok(time_to_time64us(as_time_res_with_timezone::<
1865
0
                            TimestampMillisecondType,
1866
0
                        >(x, tz)?))
1867
0
                    })?,
1868
            ))
1869
        }
1870
0
        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1871
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1872
0
            Ok(Arc::new(
1873
0
                array
1874
0
                    .as_primitive::<TimestampMillisecondType>()
1875
0
                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1876
0
                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1877
0
                            TimestampMillisecondType,
1878
0
                        >(x, tz)?))
1879
0
                    })?,
1880
            ))
1881
        }
1882
0
        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1883
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1884
0
            Ok(Arc::new(
1885
0
                array
1886
0
                    .as_primitive::<TimestampMicrosecondType>()
1887
0
                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1888
0
                        Ok(time_to_time64us(as_time_res_with_timezone::<
1889
0
                            TimestampMicrosecondType,
1890
0
                        >(x, tz)?))
1891
0
                    })?,
1892
            ))
1893
        }
1894
0
        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1895
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1896
0
            Ok(Arc::new(
1897
0
                array
1898
0
                    .as_primitive::<TimestampMicrosecondType>()
1899
0
                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1900
0
                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1901
0
                            TimestampMicrosecondType,
1902
0
                        >(x, tz)?))
1903
0
                    })?,
1904
            ))
1905
        }
1906
0
        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1907
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1908
0
            Ok(Arc::new(
1909
0
                array
1910
0
                    .as_primitive::<TimestampNanosecondType>()
1911
0
                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1912
0
                        Ok(time_to_time64us(as_time_res_with_timezone::<
1913
0
                            TimestampNanosecondType,
1914
0
                        >(x, tz)?))
1915
0
                    })?,
1916
            ))
1917
        }
1918
0
        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1919
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1920
0
            Ok(Arc::new(
1921
0
                array
1922
0
                    .as_primitive::<TimestampNanosecondType>()
1923
0
                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1924
0
                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1925
0
                            TimestampNanosecondType,
1926
0
                        >(x, tz)?))
1927
0
                    })?,
1928
            ))
1929
        }
1930
0
        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1931
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1932
0
            Ok(Arc::new(
1933
0
                array
1934
0
                    .as_primitive::<TimestampSecondType>()
1935
0
                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1936
0
                        Ok(time_to_time32s(as_time_res_with_timezone::<
1937
0
                            TimestampSecondType,
1938
0
                        >(x, tz)?))
1939
0
                    })?,
1940
            ))
1941
        }
1942
0
        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1943
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1944
0
            Ok(Arc::new(
1945
0
                array
1946
0
                    .as_primitive::<TimestampSecondType>()
1947
0
                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1948
0
                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1949
0
                            TimestampSecondType,
1950
0
                        >(x, tz)?))
1951
0
                    })?,
1952
            ))
1953
        }
1954
0
        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1955
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1956
0
            Ok(Arc::new(
1957
0
                array
1958
0
                    .as_primitive::<TimestampMillisecondType>()
1959
0
                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1960
0
                        Ok(time_to_time32s(as_time_res_with_timezone::<
1961
0
                            TimestampMillisecondType,
1962
0
                        >(x, tz)?))
1963
0
                    })?,
1964
            ))
1965
        }
1966
0
        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
1967
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1968
0
            Ok(Arc::new(
1969
0
                array
1970
0
                    .as_primitive::<TimestampMillisecondType>()
1971
0
                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1972
0
                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1973
0
                            TimestampMillisecondType,
1974
0
                        >(x, tz)?))
1975
0
                    })?,
1976
            ))
1977
        }
1978
0
        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
1979
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1980
0
            Ok(Arc::new(
1981
0
                array
1982
0
                    .as_primitive::<TimestampMicrosecondType>()
1983
0
                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1984
0
                        Ok(time_to_time32s(as_time_res_with_timezone::<
1985
0
                            TimestampMicrosecondType,
1986
0
                        >(x, tz)?))
1987
0
                    })?,
1988
            ))
1989
        }
1990
0
        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
1991
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1992
0
            Ok(Arc::new(
1993
0
                array
1994
0
                    .as_primitive::<TimestampMicrosecondType>()
1995
0
                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1996
0
                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1997
0
                            TimestampMicrosecondType,
1998
0
                        >(x, tz)?))
1999
0
                    })?,
2000
            ))
2001
        }
2002
0
        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2003
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2004
0
            Ok(Arc::new(
2005
0
                array
2006
0
                    .as_primitive::<TimestampNanosecondType>()
2007
0
                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2008
0
                        Ok(time_to_time32s(as_time_res_with_timezone::<
2009
0
                            TimestampNanosecondType,
2010
0
                        >(x, tz)?))
2011
0
                    })?,
2012
            ))
2013
        }
2014
0
        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2015
0
            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2016
0
            Ok(Arc::new(
2017
0
                array
2018
0
                    .as_primitive::<TimestampNanosecondType>()
2019
0
                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2020
0
                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2021
0
                            TimestampNanosecondType,
2022
0
                        >(x, tz)?))
2023
0
                    })?,
2024
            ))
2025
        }
2026
        (Date64, Timestamp(TimeUnit::Second, _)) => {
2027
0
            let array = array
2028
0
                .as_primitive::<Date64Type>()
2029
0
                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2030
2031
0
            cast_with_options(&array, to_type, cast_options)
2032
        }
2033
        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2034
0
            let array = array
2035
0
                .as_primitive::<Date64Type>()
2036
0
                .reinterpret_cast::<TimestampMillisecondType>();
2037
2038
0
            cast_with_options(&array, to_type, cast_options)
2039
        }
2040
2041
        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2042
0
            let array = array
2043
0
                .as_primitive::<Date64Type>()
2044
0
                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2045
2046
0
            cast_with_options(&array, to_type, cast_options)
2047
        }
2048
        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2049
0
            let array = array
2050
0
                .as_primitive::<Date64Type>()
2051
0
                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2052
2053
0
            cast_with_options(&array, to_type, cast_options)
2054
        }
2055
        (Date32, Timestamp(TimeUnit::Second, _)) => {
2056
0
            let array = array
2057
0
                .as_primitive::<Date32Type>()
2058
0
                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2059
2060
0
            cast_with_options(&array, to_type, cast_options)
2061
        }
2062
        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2063
0
            let array = array
2064
0
                .as_primitive::<Date32Type>()
2065
0
                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2066
2067
0
            cast_with_options(&array, to_type, cast_options)
2068
        }
2069
        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2070
0
            let array = array
2071
0
                .as_primitive::<Date32Type>()
2072
0
                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2073
2074
0
            cast_with_options(&array, to_type, cast_options)
2075
        }
2076
        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2077
0
            let array = array
2078
0
                .as_primitive::<Date32Type>()
2079
0
                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2080
2081
0
            cast_with_options(&array, to_type, cast_options)
2082
        }
2083
2084
0
        (_, Duration(unit)) if from_type.is_numeric() => {
2085
0
            let array = cast_with_options(array, &Int64, cast_options)?;
2086
0
            Ok(make_duration_array(array.as_primitive(), *unit))
2087
        }
2088
0
        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2089
0
            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2090
0
            cast_with_options(&array, to_type, cast_options)
2091
        }
2092
0
        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2093
0
            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2094
0
            cast_with_options(&array, to_type, cast_options)
2095
        }
2096
0
        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2097
0
            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2098
0
            cast_with_options(&array, to_type, cast_options)
2099
        }
2100
0
        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2101
0
            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2102
0
            cast_with_options(&array, to_type, cast_options)
2103
        }
2104
2105
0
        (Duration(from_unit), Duration(to_unit)) => {
2106
0
            let array = cast_with_options(array, &Int64, cast_options)?;
2107
0
            let time_array = array.as_primitive::<Int64Type>();
2108
0
            let from_size = time_unit_multiple(from_unit);
2109
0
            let to_size = time_unit_multiple(to_unit);
2110
            // we either divide or multiply, depending on size of each unit
2111
            // units are never the same when the types are the same
2112
0
            let converted = match from_size.cmp(&to_size) {
2113
                Ordering::Greater => {
2114
0
                    let divisor = from_size / to_size;
2115
0
                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2116
                }
2117
0
                Ordering::Equal => time_array.clone(),
2118
                Ordering::Less => {
2119
0
                    let mul = to_size / from_size;
2120
0
                    if cast_options.safe {
2121
0
                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2122
                    } else {
2123
0
                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2124
                    }
2125
                }
2126
            };
2127
0
            Ok(make_duration_array(&converted, *to_unit))
2128
        }
2129
2130
        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2131
0
            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2132
        }
2133
        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2134
0
            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2135
        }
2136
        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2137
0
            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2138
        }
2139
        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2140
0
            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2141
        }
2142
        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2143
0
            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2144
        }
2145
        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2146
0
            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2147
        }
2148
        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2149
0
            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2150
        }
2151
        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2152
0
            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2153
        }
2154
        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2155
0
            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2156
        }
2157
        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2158
0
            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2159
        }
2160
        (Int32, Interval(IntervalUnit::YearMonth)) => {
2161
0
            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2162
        }
2163
0
        (_, _) => Err(ArrowError::CastError(format!(
2164
0
            "Casting from {from_type:?} to {to_type:?} not supported",
2165
0
        ))),
2166
    }
2167
0
}
2168
2169
0
fn cast_from_decimal<D, F>(
2170
0
    array: &dyn Array,
2171
0
    base: D::Native,
2172
0
    scale: &i8,
2173
0
    from_type: &DataType,
2174
0
    to_type: &DataType,
2175
0
    as_float: F,
2176
0
    cast_options: &CastOptions,
2177
0
) -> Result<ArrayRef, ArrowError>
2178
0
where
2179
0
    D: DecimalType + ArrowPrimitiveType,
2180
0
    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2181
0
    F: Fn(D::Native) -> f64,
2182
{
2183
    use DataType::*;
2184
    // cast decimal to other type
2185
0
    match to_type {
2186
0
        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2187
0
        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2188
0
        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2189
0
        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2190
0
        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2191
0
        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2192
0
        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2193
0
        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2194
0
        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2195
0
            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2196
0
        }),
2197
0
        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2198
0
            as_float(x) / 10_f64.powi(*scale as i32)
2199
0
        }),
2200
0
        Utf8View => value_to_string_view(array, cast_options),
2201
0
        Utf8 => value_to_string::<i32>(array, cast_options),
2202
0
        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2203
0
        Null => Ok(new_null_array(to_type, array.len())),
2204
0
        _ => Err(ArrowError::CastError(format!(
2205
0
            "Casting from {from_type:?} to {to_type:?} not supported"
2206
0
        ))),
2207
    }
2208
0
}
2209
2210
0
fn cast_to_decimal<D, M>(
2211
0
    array: &dyn Array,
2212
0
    base: M,
2213
0
    precision: &u8,
2214
0
    scale: &i8,
2215
0
    from_type: &DataType,
2216
0
    to_type: &DataType,
2217
0
    cast_options: &CastOptions,
2218
0
) -> Result<ArrayRef, ArrowError>
2219
0
where
2220
0
    D: DecimalType + ArrowPrimitiveType<Native = M>,
2221
0
    M: ArrowNativeTypeOp + DecimalCast,
2222
0
    u8: num::traits::AsPrimitive<M>,
2223
0
    u16: num::traits::AsPrimitive<M>,
2224
0
    u32: num::traits::AsPrimitive<M>,
2225
0
    u64: num::traits::AsPrimitive<M>,
2226
0
    i8: num::traits::AsPrimitive<M>,
2227
0
    i16: num::traits::AsPrimitive<M>,
2228
0
    i32: num::traits::AsPrimitive<M>,
2229
0
    i64: num::traits::AsPrimitive<M>,
2230
{
2231
    use DataType::*;
2232
    // cast data to decimal
2233
0
    match from_type {
2234
0
        UInt8 => cast_integer_to_decimal::<_, D, M>(
2235
0
            array.as_primitive::<UInt8Type>(),
2236
0
            *precision,
2237
0
            *scale,
2238
0
            base,
2239
0
            cast_options,
2240
        ),
2241
0
        UInt16 => cast_integer_to_decimal::<_, D, _>(
2242
0
            array.as_primitive::<UInt16Type>(),
2243
0
            *precision,
2244
0
            *scale,
2245
0
            base,
2246
0
            cast_options,
2247
        ),
2248
0
        UInt32 => cast_integer_to_decimal::<_, D, _>(
2249
0
            array.as_primitive::<UInt32Type>(),
2250
0
            *precision,
2251
0
            *scale,
2252
0
            base,
2253
0
            cast_options,
2254
        ),
2255
0
        UInt64 => cast_integer_to_decimal::<_, D, _>(
2256
0
            array.as_primitive::<UInt64Type>(),
2257
0
            *precision,
2258
0
            *scale,
2259
0
            base,
2260
0
            cast_options,
2261
        ),
2262
0
        Int8 => cast_integer_to_decimal::<_, D, _>(
2263
0
            array.as_primitive::<Int8Type>(),
2264
0
            *precision,
2265
0
            *scale,
2266
0
            base,
2267
0
            cast_options,
2268
        ),
2269
0
        Int16 => cast_integer_to_decimal::<_, D, _>(
2270
0
            array.as_primitive::<Int16Type>(),
2271
0
            *precision,
2272
0
            *scale,
2273
0
            base,
2274
0
            cast_options,
2275
        ),
2276
0
        Int32 => cast_integer_to_decimal::<_, D, _>(
2277
0
            array.as_primitive::<Int32Type>(),
2278
0
            *precision,
2279
0
            *scale,
2280
0
            base,
2281
0
            cast_options,
2282
        ),
2283
0
        Int64 => cast_integer_to_decimal::<_, D, _>(
2284
0
            array.as_primitive::<Int64Type>(),
2285
0
            *precision,
2286
0
            *scale,
2287
0
            base,
2288
0
            cast_options,
2289
        ),
2290
0
        Float32 => cast_floating_point_to_decimal::<_, D>(
2291
0
            array.as_primitive::<Float32Type>(),
2292
0
            *precision,
2293
0
            *scale,
2294
0
            cast_options,
2295
        ),
2296
0
        Float64 => cast_floating_point_to_decimal::<_, D>(
2297
0
            array.as_primitive::<Float64Type>(),
2298
0
            *precision,
2299
0
            *scale,
2300
0
            cast_options,
2301
        ),
2302
        Utf8View | Utf8 => {
2303
0
            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2304
        }
2305
0
        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2306
0
        Null => Ok(new_null_array(to_type, array.len())),
2307
0
        _ => Err(ArrowError::CastError(format!(
2308
0
            "Casting from {from_type:?} to {to_type:?} not supported"
2309
0
        ))),
2310
    }
2311
0
}
2312
2313
/// Get the time unit as a multiple of a second
2314
0
const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2315
0
    match unit {
2316
0
        TimeUnit::Second => 1,
2317
0
        TimeUnit::Millisecond => MILLISECONDS,
2318
0
        TimeUnit::Microsecond => MICROSECONDS,
2319
0
        TimeUnit::Nanosecond => NANOSECONDS,
2320
    }
2321
0
}
2322
2323
/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2324
0
fn cast_numeric_arrays<FROM, TO>(
2325
0
    from: &dyn Array,
2326
0
    cast_options: &CastOptions,
2327
0
) -> Result<ArrayRef, ArrowError>
2328
0
where
2329
0
    FROM: ArrowPrimitiveType,
2330
0
    TO: ArrowPrimitiveType,
2331
0
    FROM::Native: NumCast,
2332
0
    TO::Native: NumCast,
2333
{
2334
0
    if cast_options.safe {
2335
        // If the value can't be casted to the `TO::Native`, return null
2336
0
        Ok(Arc::new(numeric_cast::<FROM, TO>(
2337
0
            from.as_primitive::<FROM>(),
2338
0
        )))
2339
    } else {
2340
        // If the value can't be casted to the `TO::Native`, return error
2341
0
        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2342
0
            from.as_primitive::<FROM>(),
2343
0
        )?))
2344
    }
2345
0
}
2346
2347
// Natural cast between numeric types
2348
// If the value of T can't be casted to R, will throw error
2349
0
fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2350
0
where
2351
0
    T: ArrowPrimitiveType,
2352
0
    R: ArrowPrimitiveType,
2353
0
    T::Native: NumCast,
2354
0
    R::Native: NumCast,
2355
{
2356
0
    from.try_unary(|value| {
2357
0
        num::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2358
0
            ArrowError::CastError(format!(
2359
0
                "Can't cast value {:?} to type {}",
2360
0
                value,
2361
0
                R::DATA_TYPE
2362
0
            ))
2363
0
        })
2364
0
    })
2365
0
}
2366
2367
// Natural cast between numeric types
2368
// If the value of T can't be casted to R, it will be converted to null
2369
0
fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2370
0
where
2371
0
    T: ArrowPrimitiveType,
2372
0
    R: ArrowPrimitiveType,
2373
0
    T::Native: NumCast,
2374
0
    R::Native: NumCast,
2375
{
2376
0
    from.unary_opt::<_, R>(num::cast::cast::<T::Native, R::Native>)
2377
0
}
2378
2379
0
fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2380
0
    array: &dyn Array,
2381
0
) -> Result<ArrayRef, ArrowError> {
2382
0
    let array = array.as_primitive::<FROM>();
2383
0
    let size = std::mem::size_of::<FROM::Native>();
2384
0
    let offsets = OffsetBuffer::from_lengths(std::iter::repeat_n(size, array.len()));
2385
0
    Ok(Arc::new(GenericBinaryArray::<O>::new(
2386
0
        offsets,
2387
0
        array.values().inner().clone(),
2388
0
        array.nulls().cloned(),
2389
0
    )))
2390
0
}
2391
2392
0
fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2393
0
    array: PrimitiveArray<Int64Type>,
2394
0
    to_tz: &Tz,
2395
0
    cast_options: &CastOptions,
2396
0
) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2397
0
    let adjust = |o| {
2398
0
        let local = as_datetime::<T>(o)?;
2399
0
        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2400
0
        T::make_value(local - offset.fix())
2401
0
    };
2402
0
    let adjusted = if cast_options.safe {
2403
0
        array.unary_opt::<_, Int64Type>(adjust)
2404
    } else {
2405
0
        array.try_unary::<_, Int64Type, _>(|o| {
2406
0
            adjust(o).ok_or_else(|| {
2407
0
                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2408
0
            })
2409
0
        })?
2410
    };
2411
0
    Ok(adjusted)
2412
0
}
2413
2414
/// Cast numeric types to Boolean
2415
///
2416
/// Any zero value returns `false` while non-zero returns `true`
2417
0
fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2418
0
where
2419
0
    FROM: ArrowPrimitiveType,
2420
{
2421
0
    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2422
0
}
2423
2424
0
fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2425
0
where
2426
0
    T: ArrowPrimitiveType + ArrowPrimitiveType,
2427
{
2428
0
    let mut b = BooleanBuilder::with_capacity(from.len());
2429
2430
0
    for i in 0..from.len() {
2431
0
        if from.is_null(i) {
2432
0
            b.append_null();
2433
0
        } else if from.value(i) != T::default_value() {
2434
0
            b.append_value(true);
2435
0
        } else {
2436
0
            b.append_value(false);
2437
0
        }
2438
    }
2439
2440
0
    Ok(b.finish())
2441
0
}
2442
2443
/// Cast Boolean types to numeric
2444
///
2445
/// `false` returns 0 while `true` returns 1
2446
0
fn cast_bool_to_numeric<TO>(
2447
0
    from: &dyn Array,
2448
0
    cast_options: &CastOptions,
2449
0
) -> Result<ArrayRef, ArrowError>
2450
0
where
2451
0
    TO: ArrowPrimitiveType,
2452
0
    TO::Native: num::cast::NumCast,
2453
{
2454
0
    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2455
0
        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2456
0
        cast_options,
2457
0
    )))
2458
0
}
2459
2460
0
fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2461
0
where
2462
0
    T: ArrowPrimitiveType,
2463
0
    T::Native: num::NumCast,
2464
{
2465
0
    let iter = (0..from.len()).map(|i| {
2466
0
        if from.is_null(i) {
2467
0
            None
2468
0
        } else if from.value(i) {
2469
            // a workaround to cast a primitive to T::Native, infallible
2470
0
            num::cast::cast(1)
2471
        } else {
2472
0
            Some(T::default_value())
2473
        }
2474
0
    });
2475
    // Benefit:
2476
    //     20% performance improvement
2477
    // Soundness:
2478
    //     The iterator is trustedLen because it comes from a Range
2479
0
    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2480
0
}
2481
2482
/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2483
0
fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2484
0
    array: &dyn Array,
2485
0
    byte_width: i32,
2486
0
    cast_options: &CastOptions,
2487
0
) -> Result<ArrayRef, ArrowError> {
2488
0
    let array = array.as_binary::<O>();
2489
0
    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2490
2491
0
    for i in 0..array.len() {
2492
0
        if array.is_null(i) {
2493
0
            builder.append_null();
2494
0
        } else {
2495
0
            match builder.append_value(array.value(i)) {
2496
0
                Ok(_) => {}
2497
0
                Err(e) => match cast_options.safe {
2498
0
                    true => builder.append_null(),
2499
0
                    false => return Err(e),
2500
                },
2501
            }
2502
        }
2503
    }
2504
2505
0
    Ok(Arc::new(builder.finish()))
2506
0
}
2507
2508
/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2509
/// If the target one is too large for the source array it will return an Error.
2510
0
fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2511
0
    array: &dyn Array,
2512
0
    byte_width: i32,
2513
0
) -> Result<ArrayRef, ArrowError> {
2514
0
    let array = array
2515
0
        .as_any()
2516
0
        .downcast_ref::<FixedSizeBinaryArray>()
2517
0
        .unwrap();
2518
2519
0
    let offsets: i128 = byte_width as i128 * array.len() as i128;
2520
2521
0
    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2522
0
    if is_binary && offsets > i32::MAX as i128 {
2523
0
        return Err(ArrowError::ComputeError(
2524
0
            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2525
0
        ));
2526
0
    } else if !is_binary && offsets > i64::MAX as i128 {
2527
0
        return Err(ArrowError::ComputeError(
2528
0
            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2529
0
        ));
2530
0
    }
2531
2532
0
    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2533
2534
0
    for i in 0..array.len() {
2535
0
        if array.is_null(i) {
2536
0
            builder.append_null();
2537
0
        } else {
2538
0
            builder.append_value(array.value(i));
2539
0
        }
2540
    }
2541
2542
0
    Ok(Arc::new(builder.finish()))
2543
0
}
2544
2545
0
fn cast_fixed_size_binary_to_binary_view(
2546
0
    array: &dyn Array,
2547
0
    _byte_width: i32,
2548
0
) -> Result<ArrayRef, ArrowError> {
2549
0
    let array = array
2550
0
        .as_any()
2551
0
        .downcast_ref::<FixedSizeBinaryArray>()
2552
0
        .unwrap();
2553
2554
0
    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2555
0
    for i in 0..array.len() {
2556
0
        if array.is_null(i) {
2557
0
            builder.append_null();
2558
0
        } else {
2559
0
            builder.append_value(array.value(i));
2560
0
        }
2561
    }
2562
2563
0
    Ok(Arc::new(builder.finish()))
2564
0
}
2565
2566
/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2567
/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2568
0
fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2569
0
where
2570
0
    FROM: ByteArrayType,
2571
0
    TO: ByteArrayType<Native = FROM::Native>,
2572
0
    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2573
0
    TO::Offset: OffsetSizeTrait + NumCast,
2574
{
2575
0
    let data = array.to_data();
2576
0
    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2577
0
    let str_values_buf = data.buffers()[1].clone();
2578
0
    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2579
2580
0
    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2581
0
    offsets
2582
0
        .iter()
2583
0
        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2584
0
            let offset =
2585
0
                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2586
0
                    ArrowError::ComputeError(format!(
2587
0
                        "{}{} array too large to cast to {}{} array",
2588
0
                        FROM::Offset::PREFIX,
2589
0
                        FROM::PREFIX,
2590
0
                        TO::Offset::PREFIX,
2591
0
                        TO::PREFIX
2592
0
                    ))
2593
0
                })?;
2594
0
            offset_builder.append(offset);
2595
0
            Ok(())
2596
0
        })?;
2597
2598
0
    let offset_buffer = offset_builder.finish();
2599
2600
0
    let dtype = TO::DATA_TYPE;
2601
2602
0
    let builder = ArrayData::builder(dtype)
2603
0
        .offset(array.offset())
2604
0
        .len(array.len())
2605
0
        .add_buffer(offset_buffer)
2606
0
        .add_buffer(str_values_buf)
2607
0
        .nulls(data.nulls().cloned());
2608
2609
0
    let array_data = unsafe { builder.build_unchecked() };
2610
2611
0
    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2612
0
}
2613
2614
/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2615
0
fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2616
0
where
2617
0
    FROM: ByteViewType,
2618
0
    TO: ByteArrayType,
2619
0
    FROM::Native: AsRef<TO::Native>,
2620
{
2621
0
    let data = array.to_data();
2622
0
    let view_array = GenericByteViewArray::<FROM>::from(data);
2623
2624
0
    let len = view_array.len();
2625
0
    let bytes = view_array
2626
0
        .views()
2627
0
        .iter()
2628
0
        .map(|v| ByteView::from(*v).length as usize)
2629
0
        .sum::<usize>();
2630
2631
0
    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2632
2633
0
    for val in view_array.iter() {
2634
0
        byte_array_builder.append_option(val);
2635
0
    }
2636
2637
0
    Ok(Arc::new(byte_array_builder.finish()))
2638
0
}
2639
2640
#[cfg(test)]
2641
mod tests {
2642
    use super::*;
2643
    use arrow_buffer::i256;
2644
    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2645
    use chrono::NaiveDate;
2646
    use half::f16;
2647
2648
    #[derive(Clone)]
2649
    struct DecimalCastTestConfig {
2650
        input_prec: u8,
2651
        input_scale: i8,
2652
        input_repr: i128,
2653
        output_prec: u8,
2654
        output_scale: i8,
2655
        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2656
                                                    // template where the "{}" will be
2657
                                                    // replaced with the decimal type name
2658
                                                    // (e.g. Decimal128)
2659
    }
2660
2661
    macro_rules! generate_cast_test_case {
2662
        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2663
            let output =
2664
                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2665
2666
            // assert cast type
2667
            let input_array_type = $INPUT_ARRAY.data_type();
2668
            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2669
            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2670
            assert_eq!($OUTPUT_TYPE, result.data_type());
2671
            assert_eq!(result.as_ref(), &output);
2672
2673
            let cast_option = CastOptions {
2674
                safe: false,
2675
                format_options: FormatOptions::default(),
2676
            };
2677
            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2678
            assert_eq!($OUTPUT_TYPE, result.data_type());
2679
            assert_eq!(result.as_ref(), &output);
2680
        };
2681
    }
2682
2683
    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2684
    where
2685
        I: DecimalType,
2686
        O: DecimalType,
2687
        I::Native: DecimalCast,
2688
        O::Native: DecimalCast,
2689
    {
2690
        let array = vec![I::Native::from_decimal(t.input_repr)];
2691
        let array = array
2692
            .into_iter()
2693
            .collect::<PrimitiveArray<I>>()
2694
            .with_precision_and_scale(t.input_prec, t.input_scale)
2695
            .unwrap();
2696
        let input_type = array.data_type();
2697
        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2698
        assert!(can_cast_types(input_type, &output_type));
2699
2700
        let options = CastOptions {
2701
            safe: false,
2702
            ..Default::default()
2703
        };
2704
        let result = cast_with_options(&array, &output_type, &options);
2705
2706
        match t.expected_output_repr {
2707
            Ok(v) => {
2708
                let expected_array = vec![O::Native::from_decimal(v)];
2709
                let expected_array = expected_array
2710
                    .into_iter()
2711
                    .collect::<PrimitiveArray<O>>()
2712
                    .with_precision_and_scale(t.output_prec, t.output_scale)
2713
                    .unwrap();
2714
                assert_eq!(*result.unwrap(), expected_array);
2715
            }
2716
            Err(expected_output_message_template) => {
2717
                assert!(result.is_err());
2718
                let expected_error_message =
2719
                    expected_output_message_template.replace("{}", O::PREFIX);
2720
                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2721
            }
2722
        }
2723
    }
2724
2725
    fn create_decimal32_array(
2726
        array: Vec<Option<i32>>,
2727
        precision: u8,
2728
        scale: i8,
2729
    ) -> Result<Decimal32Array, ArrowError> {
2730
        array
2731
            .into_iter()
2732
            .collect::<Decimal32Array>()
2733
            .with_precision_and_scale(precision, scale)
2734
    }
2735
2736
    fn create_decimal64_array(
2737
        array: Vec<Option<i64>>,
2738
        precision: u8,
2739
        scale: i8,
2740
    ) -> Result<Decimal64Array, ArrowError> {
2741
        array
2742
            .into_iter()
2743
            .collect::<Decimal64Array>()
2744
            .with_precision_and_scale(precision, scale)
2745
    }
2746
2747
    fn create_decimal128_array(
2748
        array: Vec<Option<i128>>,
2749
        precision: u8,
2750
        scale: i8,
2751
    ) -> Result<Decimal128Array, ArrowError> {
2752
        array
2753
            .into_iter()
2754
            .collect::<Decimal128Array>()
2755
            .with_precision_and_scale(precision, scale)
2756
    }
2757
2758
    fn create_decimal256_array(
2759
        array: Vec<Option<i256>>,
2760
        precision: u8,
2761
        scale: i8,
2762
    ) -> Result<Decimal256Array, ArrowError> {
2763
        array
2764
            .into_iter()
2765
            .collect::<Decimal256Array>()
2766
            .with_precision_and_scale(precision, scale)
2767
    }
2768
2769
    #[test]
2770
    #[cfg(not(feature = "force_validate"))]
2771
    #[should_panic(
2772
        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2773
    )]
2774
    fn test_cast_decimal_to_decimal_round_with_error() {
2775
        // decimal256 to decimal128 overflow
2776
        let array = vec![
2777
            Some(i256::from_i128(1123454)),
2778
            Some(i256::from_i128(2123456)),
2779
            Some(i256::from_i128(-3123453)),
2780
            Some(i256::from_i128(-3123456)),
2781
            None,
2782
            Some(i256::MAX),
2783
            Some(i256::MIN),
2784
        ];
2785
        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2786
        let array = Arc::new(input_decimal_array) as ArrayRef;
2787
        let input_type = DataType::Decimal256(76, 4);
2788
        let output_type = DataType::Decimal128(20, 3);
2789
        assert!(can_cast_types(&input_type, &output_type));
2790
        generate_cast_test_case!(
2791
            &array,
2792
            Decimal128Array,
2793
            &output_type,
2794
            vec![
2795
                Some(112345_i128),
2796
                Some(212346_i128),
2797
                Some(-312345_i128),
2798
                Some(-312346_i128),
2799
                None,
2800
                None,
2801
                None,
2802
            ]
2803
        );
2804
    }
2805
2806
    #[test]
2807
    #[cfg(not(feature = "force_validate"))]
2808
    fn test_cast_decimal_to_decimal_round() {
2809
        let array = vec![
2810
            Some(1123454),
2811
            Some(2123456),
2812
            Some(-3123453),
2813
            Some(-3123456),
2814
            None,
2815
        ];
2816
        let array = create_decimal128_array(array, 20, 4).unwrap();
2817
        // decimal128 to decimal128
2818
        let input_type = DataType::Decimal128(20, 4);
2819
        let output_type = DataType::Decimal128(20, 3);
2820
        assert!(can_cast_types(&input_type, &output_type));
2821
        generate_cast_test_case!(
2822
            &array,
2823
            Decimal128Array,
2824
            &output_type,
2825
            vec![
2826
                Some(112345_i128),
2827
                Some(212346_i128),
2828
                Some(-312345_i128),
2829
                Some(-312346_i128),
2830
                None
2831
            ]
2832
        );
2833
2834
        // decimal128 to decimal256
2835
        let input_type = DataType::Decimal128(20, 4);
2836
        let output_type = DataType::Decimal256(20, 3);
2837
        assert!(can_cast_types(&input_type, &output_type));
2838
        generate_cast_test_case!(
2839
            &array,
2840
            Decimal256Array,
2841
            &output_type,
2842
            vec![
2843
                Some(i256::from_i128(112345_i128)),
2844
                Some(i256::from_i128(212346_i128)),
2845
                Some(i256::from_i128(-312345_i128)),
2846
                Some(i256::from_i128(-312346_i128)),
2847
                None
2848
            ]
2849
        );
2850
2851
        // decimal256
2852
        let array = vec![
2853
            Some(i256::from_i128(1123454)),
2854
            Some(i256::from_i128(2123456)),
2855
            Some(i256::from_i128(-3123453)),
2856
            Some(i256::from_i128(-3123456)),
2857
            None,
2858
        ];
2859
        let array = create_decimal256_array(array, 20, 4).unwrap();
2860
2861
        // decimal256 to decimal256
2862
        let input_type = DataType::Decimal256(20, 4);
2863
        let output_type = DataType::Decimal256(20, 3);
2864
        assert!(can_cast_types(&input_type, &output_type));
2865
        generate_cast_test_case!(
2866
            &array,
2867
            Decimal256Array,
2868
            &output_type,
2869
            vec![
2870
                Some(i256::from_i128(112345_i128)),
2871
                Some(i256::from_i128(212346_i128)),
2872
                Some(i256::from_i128(-312345_i128)),
2873
                Some(i256::from_i128(-312346_i128)),
2874
                None
2875
            ]
2876
        );
2877
        // decimal256 to decimal128
2878
        let input_type = DataType::Decimal256(20, 4);
2879
        let output_type = DataType::Decimal128(20, 3);
2880
        assert!(can_cast_types(&input_type, &output_type));
2881
        generate_cast_test_case!(
2882
            &array,
2883
            Decimal128Array,
2884
            &output_type,
2885
            vec![
2886
                Some(112345_i128),
2887
                Some(212346_i128),
2888
                Some(-312345_i128),
2889
                Some(-312346_i128),
2890
                None
2891
            ]
2892
        );
2893
    }
2894
2895
    #[test]
2896
    fn test_cast_decimal32_to_decimal32() {
2897
        // test changing precision
2898
        let input_type = DataType::Decimal32(9, 3);
2899
        let output_type = DataType::Decimal32(9, 4);
2900
        assert!(can_cast_types(&input_type, &output_type));
2901
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2902
        let array = create_decimal32_array(array, 9, 3).unwrap();
2903
        generate_cast_test_case!(
2904
            &array,
2905
            Decimal32Array,
2906
            &output_type,
2907
            vec![
2908
                Some(11234560_i32),
2909
                Some(21234560_i32),
2910
                Some(31234560_i32),
2911
                None
2912
            ]
2913
        );
2914
        // negative test
2915
        let array = vec![Some(123456), None];
2916
        let array = create_decimal32_array(array, 9, 0).unwrap();
2917
        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
2918
        assert!(result_safe.is_ok());
2919
        let options = CastOptions {
2920
            safe: false,
2921
            ..Default::default()
2922
        };
2923
2924
        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
2925
        assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal32 of precision 2. Max is 99",
2926
                   result_unsafe.unwrap_err().to_string());
2927
    }
2928
2929
    #[test]
2930
    fn test_cast_decimal64_to_decimal64() {
2931
        // test changing precision
2932
        let input_type = DataType::Decimal64(17, 3);
2933
        let output_type = DataType::Decimal64(17, 4);
2934
        assert!(can_cast_types(&input_type, &output_type));
2935
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2936
        let array = create_decimal64_array(array, 17, 3).unwrap();
2937
        generate_cast_test_case!(
2938
            &array,
2939
            Decimal64Array,
2940
            &output_type,
2941
            vec![
2942
                Some(11234560_i64),
2943
                Some(21234560_i64),
2944
                Some(31234560_i64),
2945
                None
2946
            ]
2947
        );
2948
        // negative test
2949
        let array = vec![Some(123456), None];
2950
        let array = create_decimal64_array(array, 9, 0).unwrap();
2951
        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
2952
        assert!(result_safe.is_ok());
2953
        let options = CastOptions {
2954
            safe: false,
2955
            ..Default::default()
2956
        };
2957
2958
        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
2959
        assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal64 of precision 2. Max is 99",
2960
                   result_unsafe.unwrap_err().to_string());
2961
    }
2962
2963
    #[test]
2964
    fn test_cast_decimal128_to_decimal128() {
2965
        // test changing precision
2966
        let input_type = DataType::Decimal128(20, 3);
2967
        let output_type = DataType::Decimal128(20, 4);
2968
        assert!(can_cast_types(&input_type, &output_type));
2969
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2970
        let array = create_decimal128_array(array, 20, 3).unwrap();
2971
        generate_cast_test_case!(
2972
            &array,
2973
            Decimal128Array,
2974
            &output_type,
2975
            vec![
2976
                Some(11234560_i128),
2977
                Some(21234560_i128),
2978
                Some(31234560_i128),
2979
                None
2980
            ]
2981
        );
2982
        // negative test
2983
        let array = vec![Some(123456), None];
2984
        let array = create_decimal128_array(array, 10, 0).unwrap();
2985
        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
2986
        assert!(result_safe.is_ok());
2987
        let options = CastOptions {
2988
            safe: false,
2989
            ..Default::default()
2990
        };
2991
2992
        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
2993
        assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal128 of precision 2. Max is 99",
2994
                   result_unsafe.unwrap_err().to_string());
2995
    }
2996
2997
    #[test]
2998
    fn test_cast_decimal32_to_decimal32_dict() {
2999
        let p = 9;
3000
        let s = 3;
3001
        let input_type = DataType::Decimal32(p, s);
3002
        let output_type = DataType::Dictionary(
3003
            Box::new(DataType::Int32),
3004
            Box::new(DataType::Decimal32(p, s)),
3005
        );
3006
        assert!(can_cast_types(&input_type, &output_type));
3007
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3008
        let array = create_decimal32_array(array, p, s).unwrap();
3009
        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3010
        assert_eq!(cast_array.data_type(), &output_type);
3011
    }
3012
3013
    #[test]
3014
    fn test_cast_decimal64_to_decimal64_dict() {
3015
        let p = 15;
3016
        let s = 3;
3017
        let input_type = DataType::Decimal64(p, s);
3018
        let output_type = DataType::Dictionary(
3019
            Box::new(DataType::Int32),
3020
            Box::new(DataType::Decimal64(p, s)),
3021
        );
3022
        assert!(can_cast_types(&input_type, &output_type));
3023
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3024
        let array = create_decimal64_array(array, p, s).unwrap();
3025
        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3026
        assert_eq!(cast_array.data_type(), &output_type);
3027
    }
3028
3029
    #[test]
3030
    fn test_cast_decimal128_to_decimal128_dict() {
3031
        let p = 20;
3032
        let s = 3;
3033
        let input_type = DataType::Decimal128(p, s);
3034
        let output_type = DataType::Dictionary(
3035
            Box::new(DataType::Int32),
3036
            Box::new(DataType::Decimal128(p, s)),
3037
        );
3038
        assert!(can_cast_types(&input_type, &output_type));
3039
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3040
        let array = create_decimal128_array(array, p, s).unwrap();
3041
        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3042
        assert_eq!(cast_array.data_type(), &output_type);
3043
    }
3044
3045
    #[test]
3046
    fn test_cast_decimal256_to_decimal256_dict() {
3047
        let p = 20;
3048
        let s = 3;
3049
        let input_type = DataType::Decimal256(p, s);
3050
        let output_type = DataType::Dictionary(
3051
            Box::new(DataType::Int32),
3052
            Box::new(DataType::Decimal256(p, s)),
3053
        );
3054
        assert!(can_cast_types(&input_type, &output_type));
3055
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3056
        let array = create_decimal128_array(array, p, s).unwrap();
3057
        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3058
        assert_eq!(cast_array.data_type(), &output_type);
3059
    }
3060
3061
    #[test]
3062
    fn test_cast_decimal32_to_decimal32_overflow() {
3063
        let input_type = DataType::Decimal32(9, 3);
3064
        let output_type = DataType::Decimal32(9, 9);
3065
        assert!(can_cast_types(&input_type, &output_type));
3066
3067
        let array = vec![Some(i32::MAX)];
3068
        let array = create_decimal32_array(array, 9, 3).unwrap();
3069
        let result = cast_with_options(
3070
            &array,
3071
            &output_type,
3072
            &CastOptions {
3073
                safe: false,
3074
                format_options: FormatOptions::default(),
3075
            },
3076
        );
3077
        assert_eq!(
3078
            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3079
            result.unwrap_err().to_string()
3080
        );
3081
    }
3082
3083
    #[test]
3084
    fn test_cast_decimal64_to_decimal64_overflow() {
3085
        let input_type = DataType::Decimal64(18, 3);
3086
        let output_type = DataType::Decimal64(18, 18);
3087
        assert!(can_cast_types(&input_type, &output_type));
3088
3089
        let array = vec![Some(i64::MAX)];
3090
        let array = create_decimal64_array(array, 18, 3).unwrap();
3091
        let result = cast_with_options(
3092
            &array,
3093
            &output_type,
3094
            &CastOptions {
3095
                safe: false,
3096
                format_options: FormatOptions::default(),
3097
            },
3098
        );
3099
        assert_eq!(
3100
            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3101
            result.unwrap_err().to_string()
3102
        );
3103
    }
3104
3105
    #[test]
3106
    fn test_cast_decimal128_to_decimal128_overflow() {
3107
        let input_type = DataType::Decimal128(38, 3);
3108
        let output_type = DataType::Decimal128(38, 38);
3109
        assert!(can_cast_types(&input_type, &output_type));
3110
3111
        let array = vec![Some(i128::MAX)];
3112
        let array = create_decimal128_array(array, 38, 3).unwrap();
3113
        let result = cast_with_options(
3114
            &array,
3115
            &output_type,
3116
            &CastOptions {
3117
                safe: false,
3118
                format_options: FormatOptions::default(),
3119
            },
3120
        );
3121
        assert_eq!("Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3122
                   result.unwrap_err().to_string());
3123
    }
3124
3125
    #[test]
3126
    fn test_cast_decimal128_to_decimal256_overflow() {
3127
        let input_type = DataType::Decimal128(38, 3);
3128
        let output_type = DataType::Decimal256(76, 76);
3129
        assert!(can_cast_types(&input_type, &output_type));
3130
3131
        let array = vec![Some(i128::MAX)];
3132
        let array = create_decimal128_array(array, 38, 3).unwrap();
3133
        let result = cast_with_options(
3134
            &array,
3135
            &output_type,
3136
            &CastOptions {
3137
                safe: false,
3138
                format_options: FormatOptions::default(),
3139
            },
3140
        );
3141
        assert_eq!("Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3142
                   result.unwrap_err().to_string());
3143
    }
3144
3145
    #[test]
3146
    fn test_cast_decimal32_to_decimal256() {
3147
        let input_type = DataType::Decimal32(8, 3);
3148
        let output_type = DataType::Decimal256(20, 4);
3149
        assert!(can_cast_types(&input_type, &output_type));
3150
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3151
        let array = create_decimal32_array(array, 8, 3).unwrap();
3152
        generate_cast_test_case!(
3153
            &array,
3154
            Decimal256Array,
3155
            &output_type,
3156
            vec![
3157
                Some(i256::from_i128(11234560_i128)),
3158
                Some(i256::from_i128(21234560_i128)),
3159
                Some(i256::from_i128(31234560_i128)),
3160
                None
3161
            ]
3162
        );
3163
    }
3164
    #[test]
3165
    fn test_cast_decimal64_to_decimal256() {
3166
        let input_type = DataType::Decimal64(12, 3);
3167
        let output_type = DataType::Decimal256(20, 4);
3168
        assert!(can_cast_types(&input_type, &output_type));
3169
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3170
        let array = create_decimal64_array(array, 12, 3).unwrap();
3171
        generate_cast_test_case!(
3172
            &array,
3173
            Decimal256Array,
3174
            &output_type,
3175
            vec![
3176
                Some(i256::from_i128(11234560_i128)),
3177
                Some(i256::from_i128(21234560_i128)),
3178
                Some(i256::from_i128(31234560_i128)),
3179
                None
3180
            ]
3181
        );
3182
    }
3183
    #[test]
3184
    fn test_cast_decimal128_to_decimal256() {
3185
        let input_type = DataType::Decimal128(20, 3);
3186
        let output_type = DataType::Decimal256(20, 4);
3187
        assert!(can_cast_types(&input_type, &output_type));
3188
        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3189
        let array = create_decimal128_array(array, 20, 3).unwrap();
3190
        generate_cast_test_case!(
3191
            &array,
3192
            Decimal256Array,
3193
            &output_type,
3194
            vec![
3195
                Some(i256::from_i128(11234560_i128)),
3196
                Some(i256::from_i128(21234560_i128)),
3197
                Some(i256::from_i128(31234560_i128)),
3198
                None
3199
            ]
3200
        );
3201
    }
3202
3203
    #[test]
3204
    fn test_cast_decimal256_to_decimal128_overflow() {
3205
        let input_type = DataType::Decimal256(76, 5);
3206
        let output_type = DataType::Decimal128(38, 7);
3207
        assert!(can_cast_types(&input_type, &output_type));
3208
        let array = vec![Some(i256::from_i128(i128::MAX))];
3209
        let array = create_decimal256_array(array, 76, 5).unwrap();
3210
        let result = cast_with_options(
3211
            &array,
3212
            &output_type,
3213
            &CastOptions {
3214
                safe: false,
3215
                format_options: FormatOptions::default(),
3216
            },
3217
        );
3218
        assert_eq!("Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3219
                   result.unwrap_err().to_string());
3220
    }
3221
3222
    #[test]
3223
    fn test_cast_decimal256_to_decimal256_overflow() {
3224
        let input_type = DataType::Decimal256(76, 5);
3225
        let output_type = DataType::Decimal256(76, 55);
3226
        assert!(can_cast_types(&input_type, &output_type));
3227
        let array = vec![Some(i256::from_i128(i128::MAX))];
3228
        let array = create_decimal256_array(array, 76, 5).unwrap();
3229
        let result = cast_with_options(
3230
            &array,
3231
            &output_type,
3232
            &CastOptions {
3233
                safe: false,
3234
                format_options: FormatOptions::default(),
3235
            },
3236
        );
3237
        assert_eq!("Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3238
                   result.unwrap_err().to_string());
3239
    }
3240
3241
    #[test]
3242
    fn test_cast_decimal256_to_decimal128() {
3243
        let input_type = DataType::Decimal256(20, 3);
3244
        let output_type = DataType::Decimal128(20, 4);
3245
        assert!(can_cast_types(&input_type, &output_type));
3246
        let array = vec![
3247
            Some(i256::from_i128(1123456)),
3248
            Some(i256::from_i128(2123456)),
3249
            Some(i256::from_i128(3123456)),
3250
            None,
3251
        ];
3252
        let array = create_decimal256_array(array, 20, 3).unwrap();
3253
        generate_cast_test_case!(
3254
            &array,
3255
            Decimal128Array,
3256
            &output_type,
3257
            vec![
3258
                Some(11234560_i128),
3259
                Some(21234560_i128),
3260
                Some(31234560_i128),
3261
                None
3262
            ]
3263
        );
3264
    }
3265
3266
    #[test]
3267
    fn test_cast_decimal256_to_decimal256() {
3268
        let input_type = DataType::Decimal256(20, 3);
3269
        let output_type = DataType::Decimal256(20, 4);
3270
        assert!(can_cast_types(&input_type, &output_type));
3271
        let array = vec![
3272
            Some(i256::from_i128(1123456)),
3273
            Some(i256::from_i128(2123456)),
3274
            Some(i256::from_i128(3123456)),
3275
            None,
3276
        ];
3277
        let array = create_decimal256_array(array, 20, 3).unwrap();
3278
        generate_cast_test_case!(
3279
            &array,
3280
            Decimal256Array,
3281
            &output_type,
3282
            vec![
3283
                Some(i256::from_i128(11234560_i128)),
3284
                Some(i256::from_i128(21234560_i128)),
3285
                Some(i256::from_i128(31234560_i128)),
3286
                None
3287
            ]
3288
        );
3289
    }
3290
3291
    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3292
    where
3293
        T: ArrowPrimitiveType + DecimalType,
3294
    {
3295
        // u8
3296
        generate_cast_test_case!(
3297
            array,
3298
            UInt8Array,
3299
            &DataType::UInt8,
3300
            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3301
        );
3302
        // u16
3303
        generate_cast_test_case!(
3304
            array,
3305
            UInt16Array,
3306
            &DataType::UInt16,
3307
            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3308
        );
3309
        // u32
3310
        generate_cast_test_case!(
3311
            array,
3312
            UInt32Array,
3313
            &DataType::UInt32,
3314
            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3315
        );
3316
        // u64
3317
        generate_cast_test_case!(
3318
            array,
3319
            UInt64Array,
3320
            &DataType::UInt64,
3321
            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3322
        );
3323
        // i8
3324
        generate_cast_test_case!(
3325
            array,
3326
            Int8Array,
3327
            &DataType::Int8,
3328
            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3329
        );
3330
        // i16
3331
        generate_cast_test_case!(
3332
            array,
3333
            Int16Array,
3334
            &DataType::Int16,
3335
            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3336
        );
3337
        // i32
3338
        generate_cast_test_case!(
3339
            array,
3340
            Int32Array,
3341
            &DataType::Int32,
3342
            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3343
        );
3344
        // i64
3345
        generate_cast_test_case!(
3346
            array,
3347
            Int64Array,
3348
            &DataType::Int64,
3349
            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3350
        );
3351
        // f32
3352
        generate_cast_test_case!(
3353
            array,
3354
            Float32Array,
3355
            &DataType::Float32,
3356
            vec![
3357
                Some(1.25_f32),
3358
                Some(2.25_f32),
3359
                Some(3.25_f32),
3360
                None,
3361
                Some(5.25_f32)
3362
            ]
3363
        );
3364
        // f64
3365
        generate_cast_test_case!(
3366
            array,
3367
            Float64Array,
3368
            &DataType::Float64,
3369
            vec![
3370
                Some(1.25_f64),
3371
                Some(2.25_f64),
3372
                Some(3.25_f64),
3373
                None,
3374
                Some(5.25_f64)
3375
            ]
3376
        );
3377
    }
3378
3379
    #[test]
3380
    fn test_cast_decimal32_to_numeric() {
3381
        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3382
        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3383
3384
        generate_decimal_to_numeric_cast_test_case(&array);
3385
    }
3386
3387
    #[test]
3388
    fn test_cast_decimal64_to_numeric() {
3389
        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3390
        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3391
3392
        generate_decimal_to_numeric_cast_test_case(&array);
3393
    }
3394
3395
    #[test]
3396
    fn test_cast_decimal128_to_numeric() {
3397
        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3398
        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3399
3400
        generate_decimal_to_numeric_cast_test_case(&array);
3401
3402
        // overflow test: out of range of max u8
3403
        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3404
        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3405
        let casted_array = cast_with_options(
3406
            &array,
3407
            &DataType::UInt8,
3408
            &CastOptions {
3409
                safe: false,
3410
                format_options: FormatOptions::default(),
3411
            },
3412
        );
3413
        assert_eq!(
3414
            "Cast error: value of 513 is out of range UInt8".to_string(),
3415
            casted_array.unwrap_err().to_string()
3416
        );
3417
3418
        let casted_array = cast_with_options(
3419
            &array,
3420
            &DataType::UInt8,
3421
            &CastOptions {
3422
                safe: true,
3423
                format_options: FormatOptions::default(),
3424
            },
3425
        );
3426
        assert!(casted_array.is_ok());
3427
        assert!(casted_array.unwrap().is_null(0));
3428
3429
        // overflow test: out of range of max i8
3430
        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3431
        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3432
        let casted_array = cast_with_options(
3433
            &array,
3434
            &DataType::Int8,
3435
            &CastOptions {
3436
                safe: false,
3437
                format_options: FormatOptions::default(),
3438
            },
3439
        );
3440
        assert_eq!(
3441
            "Cast error: value of 244 is out of range Int8".to_string(),
3442
            casted_array.unwrap_err().to_string()
3443
        );
3444
3445
        let casted_array = cast_with_options(
3446
            &array,
3447
            &DataType::Int8,
3448
            &CastOptions {
3449
                safe: true,
3450
                format_options: FormatOptions::default(),
3451
            },
3452
        );
3453
        assert!(casted_array.is_ok());
3454
        assert!(casted_array.unwrap().is_null(0));
3455
3456
        // loss the precision: convert decimal to f32、f64
3457
        // f32
3458
        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3459
        let value_array: Vec<Option<i128>> = vec![
3460
            Some(125),
3461
            Some(225),
3462
            Some(325),
3463
            None,
3464
            Some(525),
3465
            Some(112345678),
3466
            Some(112345679),
3467
        ];
3468
        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3469
        generate_cast_test_case!(
3470
            &array,
3471
            Float32Array,
3472
            &DataType::Float32,
3473
            vec![
3474
                Some(1.25_f32),
3475
                Some(2.25_f32),
3476
                Some(3.25_f32),
3477
                None,
3478
                Some(5.25_f32),
3479
                Some(1_123_456.7_f32),
3480
                Some(1_123_456.7_f32)
3481
            ]
3482
        );
3483
3484
        // f64
3485
        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3486
        let value_array: Vec<Option<i128>> = vec![
3487
            Some(125),
3488
            Some(225),
3489
            Some(325),
3490
            None,
3491
            Some(525),
3492
            Some(112345678901234568),
3493
            Some(112345678901234560),
3494
        ];
3495
        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3496
        generate_cast_test_case!(
3497
            &array,
3498
            Float64Array,
3499
            &DataType::Float64,
3500
            vec![
3501
                Some(1.25_f64),
3502
                Some(2.25_f64),
3503
                Some(3.25_f64),
3504
                None,
3505
                Some(5.25_f64),
3506
                Some(1_123_456_789_012_345.6_f64),
3507
                Some(1_123_456_789_012_345.6_f64),
3508
            ]
3509
        );
3510
    }
3511
3512
    #[test]
3513
    fn test_cast_decimal256_to_numeric() {
3514
        let value_array: Vec<Option<i256>> = vec![
3515
            Some(i256::from_i128(125)),
3516
            Some(i256::from_i128(225)),
3517
            Some(i256::from_i128(325)),
3518
            None,
3519
            Some(i256::from_i128(525)),
3520
        ];
3521
        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3522
        // u8
3523
        generate_cast_test_case!(
3524
            &array,
3525
            UInt8Array,
3526
            &DataType::UInt8,
3527
            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3528
        );
3529
        // u16
3530
        generate_cast_test_case!(
3531
            &array,
3532
            UInt16Array,
3533
            &DataType::UInt16,
3534
            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3535
        );
3536
        // u32
3537
        generate_cast_test_case!(
3538
            &array,
3539
            UInt32Array,
3540
            &DataType::UInt32,
3541
            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3542
        );
3543
        // u64
3544
        generate_cast_test_case!(
3545
            &array,
3546
            UInt64Array,
3547
            &DataType::UInt64,
3548
            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3549
        );
3550
        // i8
3551
        generate_cast_test_case!(
3552
            &array,
3553
            Int8Array,
3554
            &DataType::Int8,
3555
            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3556
        );
3557
        // i16
3558
        generate_cast_test_case!(
3559
            &array,
3560
            Int16Array,
3561
            &DataType::Int16,
3562
            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3563
        );
3564
        // i32
3565
        generate_cast_test_case!(
3566
            &array,
3567
            Int32Array,
3568
            &DataType::Int32,
3569
            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3570
        );
3571
        // i64
3572
        generate_cast_test_case!(
3573
            &array,
3574
            Int64Array,
3575
            &DataType::Int64,
3576
            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3577
        );
3578
        // f32
3579
        generate_cast_test_case!(
3580
            &array,
3581
            Float32Array,
3582
            &DataType::Float32,
3583
            vec![
3584
                Some(1.25_f32),
3585
                Some(2.25_f32),
3586
                Some(3.25_f32),
3587
                None,
3588
                Some(5.25_f32)
3589
            ]
3590
        );
3591
        // f64
3592
        generate_cast_test_case!(
3593
            &array,
3594
            Float64Array,
3595
            &DataType::Float64,
3596
            vec![
3597
                Some(1.25_f64),
3598
                Some(2.25_f64),
3599
                Some(3.25_f64),
3600
                None,
3601
                Some(5.25_f64)
3602
            ]
3603
        );
3604
3605
        // overflow test: out of range of max i8
3606
        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3607
        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3608
        let casted_array = cast_with_options(
3609
            &array,
3610
            &DataType::Int8,
3611
            &CastOptions {
3612
                safe: false,
3613
                format_options: FormatOptions::default(),
3614
            },
3615
        );
3616
        assert_eq!(
3617
            "Cast error: value of 244 is out of range Int8".to_string(),
3618
            casted_array.unwrap_err().to_string()
3619
        );
3620
3621
        let casted_array = cast_with_options(
3622
            &array,
3623
            &DataType::Int8,
3624
            &CastOptions {
3625
                safe: true,
3626
                format_options: FormatOptions::default(),
3627
            },
3628
        );
3629
        assert!(casted_array.is_ok());
3630
        assert!(casted_array.unwrap().is_null(0));
3631
3632
        // loss the precision: convert decimal to f32、f64
3633
        // f32
3634
        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3635
        let value_array: Vec<Option<i256>> = vec![
3636
            Some(i256::from_i128(125)),
3637
            Some(i256::from_i128(225)),
3638
            Some(i256::from_i128(325)),
3639
            None,
3640
            Some(i256::from_i128(525)),
3641
            Some(i256::from_i128(112345678)),
3642
            Some(i256::from_i128(112345679)),
3643
        ];
3644
        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3645
        generate_cast_test_case!(
3646
            &array,
3647
            Float32Array,
3648
            &DataType::Float32,
3649
            vec![
3650
                Some(1.25_f32),
3651
                Some(2.25_f32),
3652
                Some(3.25_f32),
3653
                None,
3654
                Some(5.25_f32),
3655
                Some(1_123_456.7_f32),
3656
                Some(1_123_456.7_f32)
3657
            ]
3658
        );
3659
3660
        // f64
3661
        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3662
        let value_array: Vec<Option<i256>> = vec![
3663
            Some(i256::from_i128(125)),
3664
            Some(i256::from_i128(225)),
3665
            Some(i256::from_i128(325)),
3666
            None,
3667
            Some(i256::from_i128(525)),
3668
            Some(i256::from_i128(112345678901234568)),
3669
            Some(i256::from_i128(112345678901234560)),
3670
        ];
3671
        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3672
        generate_cast_test_case!(
3673
            &array,
3674
            Float64Array,
3675
            &DataType::Float64,
3676
            vec![
3677
                Some(1.25_f64),
3678
                Some(2.25_f64),
3679
                Some(3.25_f64),
3680
                None,
3681
                Some(5.25_f64),
3682
                Some(1_123_456_789_012_345.6_f64),
3683
                Some(1_123_456_789_012_345.6_f64),
3684
            ]
3685
        );
3686
    }
3687
3688
    #[test]
3689
    fn test_cast_numeric_to_decimal128() {
3690
        let decimal_type = DataType::Decimal128(38, 6);
3691
        // u8, u16, u32, u64
3692
        let input_datas = vec![
3693
            Arc::new(UInt8Array::from(vec![
3694
                Some(1),
3695
                Some(2),
3696
                Some(3),
3697
                None,
3698
                Some(5),
3699
            ])) as ArrayRef, // u8
3700
            Arc::new(UInt16Array::from(vec![
3701
                Some(1),
3702
                Some(2),
3703
                Some(3),
3704
                None,
3705
                Some(5),
3706
            ])) as ArrayRef, // u16
3707
            Arc::new(UInt32Array::from(vec![
3708
                Some(1),
3709
                Some(2),
3710
                Some(3),
3711
                None,
3712
                Some(5),
3713
            ])) as ArrayRef, // u32
3714
            Arc::new(UInt64Array::from(vec![
3715
                Some(1),
3716
                Some(2),
3717
                Some(3),
3718
                None,
3719
                Some(5),
3720
            ])) as ArrayRef, // u64
3721
        ];
3722
3723
        for array in input_datas {
3724
            generate_cast_test_case!(
3725
                &array,
3726
                Decimal128Array,
3727
                &decimal_type,
3728
                vec![
3729
                    Some(1000000_i128),
3730
                    Some(2000000_i128),
3731
                    Some(3000000_i128),
3732
                    None,
3733
                    Some(5000000_i128)
3734
                ]
3735
            );
3736
        }
3737
3738
        // i8, i16, i32, i64
3739
        let input_datas = vec![
3740
            Arc::new(Int8Array::from(vec![
3741
                Some(1),
3742
                Some(2),
3743
                Some(3),
3744
                None,
3745
                Some(5),
3746
            ])) as ArrayRef, // i8
3747
            Arc::new(Int16Array::from(vec![
3748
                Some(1),
3749
                Some(2),
3750
                Some(3),
3751
                None,
3752
                Some(5),
3753
            ])) as ArrayRef, // i16
3754
            Arc::new(Int32Array::from(vec![
3755
                Some(1),
3756
                Some(2),
3757
                Some(3),
3758
                None,
3759
                Some(5),
3760
            ])) as ArrayRef, // i32
3761
            Arc::new(Int64Array::from(vec![
3762
                Some(1),
3763
                Some(2),
3764
                Some(3),
3765
                None,
3766
                Some(5),
3767
            ])) as ArrayRef, // i64
3768
        ];
3769
        for array in input_datas {
3770
            generate_cast_test_case!(
3771
                &array,
3772
                Decimal128Array,
3773
                &decimal_type,
3774
                vec![
3775
                    Some(1000000_i128),
3776
                    Some(2000000_i128),
3777
                    Some(3000000_i128),
3778
                    None,
3779
                    Some(5000000_i128)
3780
                ]
3781
            );
3782
        }
3783
3784
        // test u8 to decimal type with overflow the result type
3785
        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3786
        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
3787
        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3788
        assert!(casted_array.is_ok());
3789
        let array = casted_array.unwrap();
3790
        let array: &Decimal128Array = array.as_primitive();
3791
        assert!(array.is_null(4));
3792
3793
        // test i8 to decimal type with overflow the result type
3794
        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3795
        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3796
        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3797
        assert!(casted_array.is_ok());
3798
        let array = casted_array.unwrap();
3799
        let array: &Decimal128Array = array.as_primitive();
3800
        assert!(array.is_null(4));
3801
3802
        // test f32 to decimal type
3803
        let array = Float32Array::from(vec![
3804
            Some(1.1),
3805
            Some(2.2),
3806
            Some(4.4),
3807
            None,
3808
            Some(1.123_456_4), // round down
3809
            Some(1.123_456_7), // round up
3810
        ]);
3811
        let array = Arc::new(array) as ArrayRef;
3812
        generate_cast_test_case!(
3813
            &array,
3814
            Decimal128Array,
3815
            &decimal_type,
3816
            vec![
3817
                Some(1100000_i128),
3818
                Some(2200000_i128),
3819
                Some(4400000_i128),
3820
                None,
3821
                Some(1123456_i128), // round down
3822
                Some(1123457_i128), // round up
3823
            ]
3824
        );
3825
3826
        // test f64 to decimal type
3827
        let array = Float64Array::from(vec![
3828
            Some(1.1),
3829
            Some(2.2),
3830
            Some(4.4),
3831
            None,
3832
            Some(1.123_456_489_123_4),     // round up
3833
            Some(1.123_456_789_123_4),     // round up
3834
            Some(1.123_456_489_012_345_6), // round down
3835
            Some(1.123_456_789_012_345_6), // round up
3836
        ]);
3837
        generate_cast_test_case!(
3838
            &array,
3839
            Decimal128Array,
3840
            &decimal_type,
3841
            vec![
3842
                Some(1100000_i128),
3843
                Some(2200000_i128),
3844
                Some(4400000_i128),
3845
                None,
3846
                Some(1123456_i128), // round down
3847
                Some(1123457_i128), // round up
3848
                Some(1123456_i128), // round down
3849
                Some(1123457_i128), // round up
3850
            ]
3851
        );
3852
    }
3853
3854
    #[test]
3855
    fn test_cast_numeric_to_decimal256() {
3856
        let decimal_type = DataType::Decimal256(76, 6);
3857
        // u8, u16, u32, u64
3858
        let input_datas = vec![
3859
            Arc::new(UInt8Array::from(vec![
3860
                Some(1),
3861
                Some(2),
3862
                Some(3),
3863
                None,
3864
                Some(5),
3865
            ])) as ArrayRef, // u8
3866
            Arc::new(UInt16Array::from(vec![
3867
                Some(1),
3868
                Some(2),
3869
                Some(3),
3870
                None,
3871
                Some(5),
3872
            ])) as ArrayRef, // u16
3873
            Arc::new(UInt32Array::from(vec![
3874
                Some(1),
3875
                Some(2),
3876
                Some(3),
3877
                None,
3878
                Some(5),
3879
            ])) as ArrayRef, // u32
3880
            Arc::new(UInt64Array::from(vec![
3881
                Some(1),
3882
                Some(2),
3883
                Some(3),
3884
                None,
3885
                Some(5),
3886
            ])) as ArrayRef, // u64
3887
        ];
3888
3889
        for array in input_datas {
3890
            generate_cast_test_case!(
3891
                &array,
3892
                Decimal256Array,
3893
                &decimal_type,
3894
                vec![
3895
                    Some(i256::from_i128(1000000_i128)),
3896
                    Some(i256::from_i128(2000000_i128)),
3897
                    Some(i256::from_i128(3000000_i128)),
3898
                    None,
3899
                    Some(i256::from_i128(5000000_i128))
3900
                ]
3901
            );
3902
        }
3903
3904
        // i8, i16, i32, i64
3905
        let input_datas = vec![
3906
            Arc::new(Int8Array::from(vec![
3907
                Some(1),
3908
                Some(2),
3909
                Some(3),
3910
                None,
3911
                Some(5),
3912
            ])) as ArrayRef, // i8
3913
            Arc::new(Int16Array::from(vec![
3914
                Some(1),
3915
                Some(2),
3916
                Some(3),
3917
                None,
3918
                Some(5),
3919
            ])) as ArrayRef, // i16
3920
            Arc::new(Int32Array::from(vec![
3921
                Some(1),
3922
                Some(2),
3923
                Some(3),
3924
                None,
3925
                Some(5),
3926
            ])) as ArrayRef, // i32
3927
            Arc::new(Int64Array::from(vec![
3928
                Some(1),
3929
                Some(2),
3930
                Some(3),
3931
                None,
3932
                Some(5),
3933
            ])) as ArrayRef, // i64
3934
        ];
3935
        for array in input_datas {
3936
            generate_cast_test_case!(
3937
                &array,
3938
                Decimal256Array,
3939
                &decimal_type,
3940
                vec![
3941
                    Some(i256::from_i128(1000000_i128)),
3942
                    Some(i256::from_i128(2000000_i128)),
3943
                    Some(i256::from_i128(3000000_i128)),
3944
                    None,
3945
                    Some(i256::from_i128(5000000_i128))
3946
                ]
3947
            );
3948
        }
3949
3950
        // test i8 to decimal type with overflow the result type
3951
        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3952
        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3953
        let array = Arc::new(array) as ArrayRef;
3954
        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
3955
        assert!(casted_array.is_ok());
3956
        let array = casted_array.unwrap();
3957
        let array: &Decimal256Array = array.as_primitive();
3958
        assert!(array.is_null(4));
3959
3960
        // test f32 to decimal type
3961
        let array = Float32Array::from(vec![
3962
            Some(1.1),
3963
            Some(2.2),
3964
            Some(4.4),
3965
            None,
3966
            Some(1.123_456_4), // round down
3967
            Some(1.123_456_7), // round up
3968
        ]);
3969
        generate_cast_test_case!(
3970
            &array,
3971
            Decimal256Array,
3972
            &decimal_type,
3973
            vec![
3974
                Some(i256::from_i128(1100000_i128)),
3975
                Some(i256::from_i128(2200000_i128)),
3976
                Some(i256::from_i128(4400000_i128)),
3977
                None,
3978
                Some(i256::from_i128(1123456_i128)), // round down
3979
                Some(i256::from_i128(1123457_i128)), // round up
3980
            ]
3981
        );
3982
3983
        // test f64 to decimal type
3984
        let array = Float64Array::from(vec![
3985
            Some(1.1),
3986
            Some(2.2),
3987
            Some(4.4),
3988
            None,
3989
            Some(1.123_456_489_123_4),     // round down
3990
            Some(1.123_456_789_123_4),     // round up
3991
            Some(1.123_456_489_012_345_6), // round down
3992
            Some(1.123_456_789_012_345_6), // round up
3993
        ]);
3994
        generate_cast_test_case!(
3995
            &array,
3996
            Decimal256Array,
3997
            &decimal_type,
3998
            vec![
3999
                Some(i256::from_i128(1100000_i128)),
4000
                Some(i256::from_i128(2200000_i128)),
4001
                Some(i256::from_i128(4400000_i128)),
4002
                None,
4003
                Some(i256::from_i128(1123456_i128)), // round down
4004
                Some(i256::from_i128(1123457_i128)), // round up
4005
                Some(i256::from_i128(1123456_i128)), // round down
4006
                Some(i256::from_i128(1123457_i128)), // round up
4007
            ]
4008
        );
4009
    }
4010
4011
    #[test]
4012
    fn test_cast_i32_to_f64() {
4013
        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4014
        let b = cast(&array, &DataType::Float64).unwrap();
4015
        let c = b.as_primitive::<Float64Type>();
4016
        assert_eq!(5.0, c.value(0));
4017
        assert_eq!(6.0, c.value(1));
4018
        assert_eq!(7.0, c.value(2));
4019
        assert_eq!(8.0, c.value(3));
4020
        assert_eq!(9.0, c.value(4));
4021
    }
4022
4023
    #[test]
4024
    fn test_cast_i32_to_u8() {
4025
        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4026
        let b = cast(&array, &DataType::UInt8).unwrap();
4027
        let c = b.as_primitive::<UInt8Type>();
4028
        assert!(!c.is_valid(0));
4029
        assert_eq!(6, c.value(1));
4030
        assert!(!c.is_valid(2));
4031
        assert_eq!(8, c.value(3));
4032
        // overflows return None
4033
        assert!(!c.is_valid(4));
4034
    }
4035
4036
    #[test]
4037
    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4038
    fn test_cast_int32_to_u8_with_error() {
4039
        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4040
        // overflow with the error
4041
        let cast_option = CastOptions {
4042
            safe: false,
4043
            format_options: FormatOptions::default(),
4044
        };
4045
        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4046
        assert!(result.is_err());
4047
        result.unwrap();
4048
    }
4049
4050
    #[test]
4051
    fn test_cast_i32_to_u8_sliced() {
4052
        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4053
        assert_eq!(0, array.offset());
4054
        let array = array.slice(2, 3);
4055
        let b = cast(&array, &DataType::UInt8).unwrap();
4056
        assert_eq!(3, b.len());
4057
        let c = b.as_primitive::<UInt8Type>();
4058
        assert!(!c.is_valid(0));
4059
        assert_eq!(8, c.value(1));
4060
        // overflows return None
4061
        assert!(!c.is_valid(2));
4062
    }
4063
4064
    #[test]
4065
    fn test_cast_i32_to_i32() {
4066
        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4067
        let b = cast(&array, &DataType::Int32).unwrap();
4068
        let c = b.as_primitive::<Int32Type>();
4069
        assert_eq!(5, c.value(0));
4070
        assert_eq!(6, c.value(1));
4071
        assert_eq!(7, c.value(2));
4072
        assert_eq!(8, c.value(3));
4073
        assert_eq!(9, c.value(4));
4074
    }
4075
4076
    #[test]
4077
    fn test_cast_i32_to_list_i32() {
4078
        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4079
        let b = cast(
4080
            &array,
4081
            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4082
        )
4083
        .unwrap();
4084
        assert_eq!(5, b.len());
4085
        let arr = b.as_list::<i32>();
4086
        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4087
        assert_eq!(1, arr.value_length(0));
4088
        assert_eq!(1, arr.value_length(1));
4089
        assert_eq!(1, arr.value_length(2));
4090
        assert_eq!(1, arr.value_length(3));
4091
        assert_eq!(1, arr.value_length(4));
4092
        let c = arr.values().as_primitive::<Int32Type>();
4093
        assert_eq!(5, c.value(0));
4094
        assert_eq!(6, c.value(1));
4095
        assert_eq!(7, c.value(2));
4096
        assert_eq!(8, c.value(3));
4097
        assert_eq!(9, c.value(4));
4098
    }
4099
4100
    #[test]
4101
    fn test_cast_i32_to_list_i32_nullable() {
4102
        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4103
        let b = cast(
4104
            &array,
4105
            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4106
        )
4107
        .unwrap();
4108
        assert_eq!(5, b.len());
4109
        assert_eq!(0, b.null_count());
4110
        let arr = b.as_list::<i32>();
4111
        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4112
        assert_eq!(1, arr.value_length(0));
4113
        assert_eq!(1, arr.value_length(1));
4114
        assert_eq!(1, arr.value_length(2));
4115
        assert_eq!(1, arr.value_length(3));
4116
        assert_eq!(1, arr.value_length(4));
4117
4118
        let c = arr.values().as_primitive::<Int32Type>();
4119
        assert_eq!(1, c.null_count());
4120
        assert_eq!(5, c.value(0));
4121
        assert!(!c.is_valid(1));
4122
        assert_eq!(7, c.value(2));
4123
        assert_eq!(8, c.value(3));
4124
        assert_eq!(9, c.value(4));
4125
    }
4126
4127
    #[test]
4128
    fn test_cast_i32_to_list_f64_nullable_sliced() {
4129
        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4130
        let array = array.slice(2, 4);
4131
        let b = cast(
4132
            &array,
4133
            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4134
        )
4135
        .unwrap();
4136
        assert_eq!(4, b.len());
4137
        assert_eq!(0, b.null_count());
4138
        let arr = b.as_list::<i32>();
4139
        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4140
        assert_eq!(1, arr.value_length(0));
4141
        assert_eq!(1, arr.value_length(1));
4142
        assert_eq!(1, arr.value_length(2));
4143
        assert_eq!(1, arr.value_length(3));
4144
        let c = arr.values().as_primitive::<Float64Type>();
4145
        assert_eq!(1, c.null_count());
4146
        assert_eq!(7.0, c.value(0));
4147
        assert_eq!(8.0, c.value(1));
4148
        assert!(!c.is_valid(2));
4149
        assert_eq!(10.0, c.value(3));
4150
    }
4151
4152
    #[test]
4153
    fn test_cast_int_to_utf8view() {
4154
        let inputs = vec![
4155
            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4156
            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4157
            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4158
            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4159
            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4160
            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4161
            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4162
            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4163
        ];
4164
        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4165
            None,
4166
            Some("8"),
4167
            Some("9"),
4168
            Some("10"),
4169
        ]));
4170
4171
        for array in inputs {
4172
            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4173
            let arr = cast(&array, &DataType::Utf8View).unwrap();
4174
            assert_eq!(expected.as_ref(), arr.as_ref());
4175
        }
4176
    }
4177
4178
    #[test]
4179
    fn test_cast_float_to_utf8view() {
4180
        let inputs = vec![
4181
            Arc::new(Float16Array::from(vec![
4182
                Some(f16::from_f64(1.5)),
4183
                Some(f16::from_f64(2.5)),
4184
                None,
4185
            ])) as ArrayRef,
4186
            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4187
            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4188
        ];
4189
4190
        let expected: ArrayRef =
4191
            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4192
4193
        for array in inputs {
4194
            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4195
            let arr = cast(&array, &DataType::Utf8View).unwrap();
4196
            assert_eq!(expected.as_ref(), arr.as_ref());
4197
        }
4198
    }
4199
4200
    #[test]
4201
    fn test_cast_utf8_to_i32() {
4202
        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4203
        let b = cast(&array, &DataType::Int32).unwrap();
4204
        let c = b.as_primitive::<Int32Type>();
4205
        assert_eq!(5, c.value(0));
4206
        assert_eq!(6, c.value(1));
4207
        assert!(!c.is_valid(2));
4208
        assert_eq!(8, c.value(3));
4209
        assert!(!c.is_valid(4));
4210
    }
4211
4212
    #[test]
4213
    fn test_cast_utf8view_to_i32() {
4214
        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4215
        let b = cast(&array, &DataType::Int32).unwrap();
4216
        let c = b.as_primitive::<Int32Type>();
4217
        assert_eq!(5, c.value(0));
4218
        assert_eq!(6, c.value(1));
4219
        assert!(!c.is_valid(2));
4220
        assert_eq!(8, c.value(3));
4221
        assert!(!c.is_valid(4));
4222
    }
4223
4224
    #[test]
4225
    fn test_cast_utf8view_to_f32() {
4226
        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4227
        let b = cast(&array, &DataType::Float32).unwrap();
4228
        let c = b.as_primitive::<Float32Type>();
4229
        assert_eq!(3.0, c.value(0));
4230
        assert_eq!(4.56, c.value(1));
4231
        assert!(!c.is_valid(2));
4232
        assert_eq!(8.9, c.value(3));
4233
    }
4234
4235
    #[test]
4236
    fn test_cast_utf8view_to_decimal128() {
4237
        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4238
        let arr = Arc::new(array) as ArrayRef;
4239
        generate_cast_test_case!(
4240
            &arr,
4241
            Decimal128Array,
4242
            &DataType::Decimal128(4, 2),
4243
            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4244
        );
4245
    }
4246
4247
    #[test]
4248
    fn test_cast_with_options_utf8_to_i32() {
4249
        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4250
        let result = cast_with_options(
4251
            &array,
4252
            &DataType::Int32,
4253
            &CastOptions {
4254
                safe: false,
4255
                format_options: FormatOptions::default(),
4256
            },
4257
        );
4258
        match result {
4259
            Ok(_) => panic!("expected error"),
4260
            Err(e) => {
4261
                assert!(
4262
                    e.to_string()
4263
                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4264
                    "Error: {e}"
4265
                )
4266
            }
4267
        }
4268
    }
4269
4270
    #[test]
4271
    fn test_cast_utf8_to_bool() {
4272
        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4273
        let casted = cast(&strings, &DataType::Boolean).unwrap();
4274
        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4275
        assert_eq!(*as_boolean_array(&casted), expected);
4276
    }
4277
4278
    #[test]
4279
    fn test_cast_utf8view_to_bool() {
4280
        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4281
        let casted = cast(&strings, &DataType::Boolean).unwrap();
4282
        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4283
        assert_eq!(*as_boolean_array(&casted), expected);
4284
    }
4285
4286
    #[test]
4287
    fn test_cast_with_options_utf8_to_bool() {
4288
        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4289
        let casted = cast_with_options(
4290
            &strings,
4291
            &DataType::Boolean,
4292
            &CastOptions {
4293
                safe: false,
4294
                format_options: FormatOptions::default(),
4295
            },
4296
        );
4297
        match casted {
4298
            Ok(_) => panic!("expected error"),
4299
            Err(e) => {
4300
                assert!(e
4301
                    .to_string()
4302
                    .contains("Cast error: Cannot cast value 'invalid' to value of Boolean type"))
4303
            }
4304
        }
4305
    }
4306
4307
    #[test]
4308
    fn test_cast_bool_to_i32() {
4309
        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4310
        let b = cast(&array, &DataType::Int32).unwrap();
4311
        let c = b.as_primitive::<Int32Type>();
4312
        assert_eq!(1, c.value(0));
4313
        assert_eq!(0, c.value(1));
4314
        assert!(!c.is_valid(2));
4315
    }
4316
4317
    #[test]
4318
    fn test_cast_bool_to_utf8view() {
4319
        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4320
        let b = cast(&array, &DataType::Utf8View).unwrap();
4321
        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4322
        assert_eq!("true", c.value(0));
4323
        assert_eq!("false", c.value(1));
4324
        assert!(!c.is_valid(2));
4325
    }
4326
4327
    #[test]
4328
    fn test_cast_bool_to_utf8() {
4329
        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4330
        let b = cast(&array, &DataType::Utf8).unwrap();
4331
        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4332
        assert_eq!("true", c.value(0));
4333
        assert_eq!("false", c.value(1));
4334
        assert!(!c.is_valid(2));
4335
    }
4336
4337
    #[test]
4338
    fn test_cast_bool_to_large_utf8() {
4339
        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4340
        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4341
        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4342
        assert_eq!("true", c.value(0));
4343
        assert_eq!("false", c.value(1));
4344
        assert!(!c.is_valid(2));
4345
    }
4346
4347
    #[test]
4348
    fn test_cast_bool_to_f64() {
4349
        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4350
        let b = cast(&array, &DataType::Float64).unwrap();
4351
        let c = b.as_primitive::<Float64Type>();
4352
        assert_eq!(1.0, c.value(0));
4353
        assert_eq!(0.0, c.value(1));
4354
        assert!(!c.is_valid(2));
4355
    }
4356
4357
    #[test]
4358
    fn test_cast_integer_to_timestamp() {
4359
        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4360
        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4361
4362
        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4363
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4364
4365
        assert_eq!(&actual, &expected);
4366
4367
        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4368
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4369
4370
        assert_eq!(&actual, &expected);
4371
4372
        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4373
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4374
4375
        assert_eq!(&actual, &expected);
4376
4377
        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4378
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4379
4380
        assert_eq!(&actual, &expected);
4381
4382
        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4383
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4384
4385
        assert_eq!(&actual, &expected);
4386
4387
        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4388
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4389
4390
        assert_eq!(&actual, &expected);
4391
4392
        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4393
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4394
4395
        assert_eq!(&actual, &expected);
4396
    }
4397
4398
    #[test]
4399
    fn test_cast_timestamp_to_integer() {
4400
        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4401
            .with_timezone("UTC".to_string());
4402
        let expected = cast(&array, &DataType::Int64).unwrap();
4403
4404
        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4405
        assert_eq!(&actual, &expected);
4406
4407
        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4408
        assert_eq!(&actual, &expected);
4409
4410
        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4411
        assert_eq!(&actual, &expected);
4412
4413
        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4414
        assert_eq!(&actual, &expected);
4415
4416
        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4417
        assert_eq!(&actual, &expected);
4418
4419
        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4420
        assert_eq!(&actual, &expected);
4421
4422
        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4423
        assert_eq!(&actual, &expected);
4424
    }
4425
4426
    #[test]
4427
    fn test_cast_floating_to_timestamp() {
4428
        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4429
        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4430
4431
        let array = Float16Array::from(vec![
4432
            Some(f16::from_f32(2.0)),
4433
            Some(f16::from_f32(10.6)),
4434
            None,
4435
        ]);
4436
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4437
4438
        assert_eq!(&actual, &expected);
4439
4440
        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4441
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4442
4443
        assert_eq!(&actual, &expected);
4444
4445
        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4446
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4447
4448
        assert_eq!(&actual, &expected);
4449
    }
4450
4451
    #[test]
4452
    fn test_cast_timestamp_to_floating() {
4453
        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4454
            .with_timezone("UTC".to_string());
4455
        let expected = cast(&array, &DataType::Int64).unwrap();
4456
4457
        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4458
        assert_eq!(&actual, &expected);
4459
4460
        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4461
        assert_eq!(&actual, &expected);
4462
4463
        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4464
        assert_eq!(&actual, &expected);
4465
    }
4466
4467
    #[test]
4468
    fn test_cast_decimal_to_timestamp() {
4469
        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4470
        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4471
4472
        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4473
            .with_precision_and_scale(4, 2)
4474
            .unwrap();
4475
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4476
4477
        assert_eq!(&actual, &expected);
4478
4479
        let array = Decimal256Array::from(vec![
4480
            Some(i256::from_i128(2000)),
4481
            Some(i256::from_i128(10000)),
4482
            None,
4483
        ])
4484
        .with_precision_and_scale(5, 3)
4485
        .unwrap();
4486
        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4487
4488
        assert_eq!(&actual, &expected);
4489
    }
4490
4491
    #[test]
4492
    fn test_cast_timestamp_to_decimal() {
4493
        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4494
            .with_timezone("UTC".to_string());
4495
        let expected = cast(&array, &DataType::Int64).unwrap();
4496
4497
        let actual = cast(
4498
            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4499
            &DataType::Int64,
4500
        )
4501
        .unwrap();
4502
        assert_eq!(&actual, &expected);
4503
4504
        let actual = cast(
4505
            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4506
            &DataType::Int64,
4507
        )
4508
        .unwrap();
4509
        assert_eq!(&actual, &expected);
4510
    }
4511
4512
    #[test]
4513
    fn test_cast_list_i32_to_list_u16() {
4514
        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 100000000]).into_data();
4515
4516
        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
4517
4518
        // Construct a list array from the above two
4519
        // [[0,0,0], [-1, -2, -1], [2, 100000000]]
4520
        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4521
        let list_data = ArrayData::builder(list_data_type)
4522
            .len(3)
4523
            .add_buffer(value_offsets)
4524
            .add_child_data(value_data)
4525
            .build()
4526
            .unwrap();
4527
        let list_array = ListArray::from(list_data);
4528
4529
        let cast_array = cast(
4530
            &list_array,
4531
            &DataType::List(Arc::new(Field::new_list_field(DataType::UInt16, true))),
4532
        )
4533
        .unwrap();
4534
4535
        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4536
        //
4537
        // 3 negative values should get lost when casting to unsigned,
4538
        // 1 value should overflow
4539
        assert_eq!(0, cast_array.null_count());
4540
4541
        // offsets should be the same
4542
        let array = cast_array.as_list::<i32>();
4543
        assert_eq!(list_array.value_offsets(), array.value_offsets());
4544
4545
        assert_eq!(DataType::UInt16, array.value_type());
4546
        assert_eq!(3, array.value_length(0));
4547
        assert_eq!(3, array.value_length(1));
4548
        assert_eq!(2, array.value_length(2));
4549
4550
        // expect 4 nulls: negative numbers and overflow
4551
        let u16arr = array.values().as_primitive::<UInt16Type>();
4552
        assert_eq!(4, u16arr.null_count());
4553
4554
        // expect 4 nulls: negative numbers and overflow
4555
        let expected: UInt16Array =
4556
            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4557
                .into_iter()
4558
                .collect();
4559
4560
        assert_eq!(u16arr, &expected);
4561
    }
4562
4563
    #[test]
4564
    fn test_cast_list_i32_to_list_timestamp() {
4565
        // Construct a value array
4566
        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4567
4568
        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4569
4570
        // Construct a list array from the above two
4571
        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4572
        let list_data = ArrayData::builder(list_data_type)
4573
            .len(3)
4574
            .add_buffer(value_offsets)
4575
            .add_child_data(value_data)
4576
            .build()
4577
            .unwrap();
4578
        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4579
4580
        let actual = cast(
4581
            &list_array,
4582
            &DataType::List(Arc::new(Field::new_list_field(
4583
                DataType::Timestamp(TimeUnit::Microsecond, None),
4584
                true,
4585
            ))),
4586
        )
4587
        .unwrap();
4588
4589
        let expected = cast(
4590
            &cast(
4591
                &list_array,
4592
                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4593
            )
4594
            .unwrap(),
4595
            &DataType::List(Arc::new(Field::new_list_field(
4596
                DataType::Timestamp(TimeUnit::Microsecond, None),
4597
                true,
4598
            ))),
4599
        )
4600
        .unwrap();
4601
4602
        assert_eq!(&actual, &expected);
4603
    }
4604
4605
    #[test]
4606
    fn test_cast_date32_to_date64() {
4607
        let a = Date32Array::from(vec![10000, 17890]);
4608
        let array = Arc::new(a) as ArrayRef;
4609
        let b = cast(&array, &DataType::Date64).unwrap();
4610
        let c = b.as_primitive::<Date64Type>();
4611
        assert_eq!(864000000000, c.value(0));
4612
        assert_eq!(1545696000000, c.value(1));
4613
    }
4614
4615
    #[test]
4616
    fn test_cast_date64_to_date32() {
4617
        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4618
        let array = Arc::new(a) as ArrayRef;
4619
        let b = cast(&array, &DataType::Date32).unwrap();
4620
        let c = b.as_primitive::<Date32Type>();
4621
        assert_eq!(10000, c.value(0));
4622
        assert_eq!(17890, c.value(1));
4623
        assert!(c.is_null(2));
4624
    }
4625
4626
    #[test]
4627
    fn test_cast_string_to_integral_overflow() {
4628
        let str = Arc::new(StringArray::from(vec![
4629
            Some("123"),
4630
            Some("-123"),
4631
            Some("86374"),
4632
            None,
4633
        ])) as ArrayRef;
4634
4635
        let options = CastOptions {
4636
            safe: true,
4637
            format_options: FormatOptions::default(),
4638
        };
4639
        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4640
        let expected =
4641
            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4642
        assert_eq!(&res, &expected);
4643
    }
4644
4645
    #[test]
4646
    fn test_cast_string_to_timestamp() {
4647
        let a0 = Arc::new(StringViewArray::from(vec![
4648
            Some("2020-09-08T12:00:00.123456789+00:00"),
4649
            Some("Not a valid date"),
4650
            None,
4651
        ])) as ArrayRef;
4652
        let a1 = Arc::new(StringArray::from(vec![
4653
            Some("2020-09-08T12:00:00.123456789+00:00"),
4654
            Some("Not a valid date"),
4655
            None,
4656
        ])) as ArrayRef;
4657
        let a2 = Arc::new(LargeStringArray::from(vec![
4658
            Some("2020-09-08T12:00:00.123456789+00:00"),
4659
            Some("Not a valid date"),
4660
            None,
4661
        ])) as ArrayRef;
4662
        for array in &[a0, a1, a2] {
4663
            for time_unit in &[
4664
                TimeUnit::Second,
4665
                TimeUnit::Millisecond,
4666
                TimeUnit::Microsecond,
4667
                TimeUnit::Nanosecond,
4668
            ] {
4669
                let to_type = DataType::Timestamp(*time_unit, None);
4670
                let b = cast(array, &to_type).unwrap();
4671
4672
                match time_unit {
4673
                    TimeUnit::Second => {
4674
                        let c = b.as_primitive::<TimestampSecondType>();
4675
                        assert_eq!(1599566400, c.value(0));
4676
                        assert!(c.is_null(1));
4677
                        assert!(c.is_null(2));
4678
                    }
4679
                    TimeUnit::Millisecond => {
4680
                        let c = b
4681
                            .as_any()
4682
                            .downcast_ref::<TimestampMillisecondArray>()
4683
                            .unwrap();
4684
                        assert_eq!(1599566400123, c.value(0));
4685
                        assert!(c.is_null(1));
4686
                        assert!(c.is_null(2));
4687
                    }
4688
                    TimeUnit::Microsecond => {
4689
                        let c = b
4690
                            .as_any()
4691
                            .downcast_ref::<TimestampMicrosecondArray>()
4692
                            .unwrap();
4693
                        assert_eq!(1599566400123456, c.value(0));
4694
                        assert!(c.is_null(1));
4695
                        assert!(c.is_null(2));
4696
                    }
4697
                    TimeUnit::Nanosecond => {
4698
                        let c = b
4699
                            .as_any()
4700
                            .downcast_ref::<TimestampNanosecondArray>()
4701
                            .unwrap();
4702
                        assert_eq!(1599566400123456789, c.value(0));
4703
                        assert!(c.is_null(1));
4704
                        assert!(c.is_null(2));
4705
                    }
4706
                }
4707
4708
                let options = CastOptions {
4709
                    safe: false,
4710
                    format_options: FormatOptions::default(),
4711
                };
4712
                let err = cast_with_options(array, &to_type, &options).unwrap_err();
4713
                assert_eq!(
4714
                    err.to_string(),
4715
                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4716
                );
4717
            }
4718
        }
4719
    }
4720
4721
    #[test]
4722
    fn test_cast_string_to_timestamp_overflow() {
4723
        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4724
        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4725
        let result = result.as_primitive::<TimestampSecondType>();
4726
        assert_eq!(result.values(), &[247112596800]);
4727
    }
4728
4729
    #[test]
4730
    fn test_cast_string_to_date32() {
4731
        let a0 = Arc::new(StringViewArray::from(vec![
4732
            Some("2018-12-25"),
4733
            Some("Not a valid date"),
4734
            None,
4735
        ])) as ArrayRef;
4736
        let a1 = Arc::new(StringArray::from(vec![
4737
            Some("2018-12-25"),
4738
            Some("Not a valid date"),
4739
            None,
4740
        ])) as ArrayRef;
4741
        let a2 = Arc::new(LargeStringArray::from(vec![
4742
            Some("2018-12-25"),
4743
            Some("Not a valid date"),
4744
            None,
4745
        ])) as ArrayRef;
4746
        for array in &[a0, a1, a2] {
4747
            let to_type = DataType::Date32;
4748
            let b = cast(array, &to_type).unwrap();
4749
            let c = b.as_primitive::<Date32Type>();
4750
            assert_eq!(17890, c.value(0));
4751
            assert!(c.is_null(1));
4752
            assert!(c.is_null(2));
4753
4754
            let options = CastOptions {
4755
                safe: false,
4756
                format_options: FormatOptions::default(),
4757
            };
4758
            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4759
            assert_eq!(
4760
                err.to_string(),
4761
                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
4762
            );
4763
        }
4764
    }
4765
4766
    #[test]
4767
    fn test_cast_string_with_large_date_to_date32() {
4768
        let array = Arc::new(StringArray::from(vec![
4769
            Some("+10999-12-31"),
4770
            Some("-0010-02-28"),
4771
            Some("0010-02-28"),
4772
            Some("0000-01-01"),
4773
            Some("-0000-01-01"),
4774
            Some("-0001-01-01"),
4775
        ])) as ArrayRef;
4776
        let to_type = DataType::Date32;
4777
        let options = CastOptions {
4778
            safe: false,
4779
            format_options: FormatOptions::default(),
4780
        };
4781
        let b = cast_with_options(&array, &to_type, &options).unwrap();
4782
        let c = b.as_primitive::<Date32Type>();
4783
        assert_eq!(3298139, c.value(0)); // 10999-12-31
4784
        assert_eq!(-723122, c.value(1)); // -0010-02-28
4785
        assert_eq!(-715817, c.value(2)); // 0010-02-28
4786
        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
4787
        assert_eq!(-719528, c.value(3)); // 0000-01-01
4788
        assert_eq!(-719528, c.value(4)); // -0000-01-01
4789
        assert_eq!(-719893, c.value(5)); // -0001-01-01
4790
    }
4791
4792
    #[test]
4793
    fn test_cast_invalid_string_with_large_date_to_date32() {
4794
        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
4795
        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
4796
        let to_type = DataType::Date32;
4797
        let options = CastOptions {
4798
            safe: false,
4799
            format_options: FormatOptions::default(),
4800
        };
4801
        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
4802
        assert_eq!(
4803
            err.to_string(),
4804
            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
4805
        );
4806
    }
4807
4808
    #[test]
4809
    fn test_cast_string_format_yyyymmdd_to_date32() {
4810
        let a0 = Arc::new(StringViewArray::from(vec![
4811
            Some("2020-12-25"),
4812
            Some("20201117"),
4813
        ])) as ArrayRef;
4814
        let a1 = Arc::new(StringArray::from(vec![
4815
            Some("2020-12-25"),
4816
            Some("20201117"),
4817
        ])) as ArrayRef;
4818
        let a2 = Arc::new(LargeStringArray::from(vec![
4819
            Some("2020-12-25"),
4820
            Some("20201117"),
4821
        ])) as ArrayRef;
4822
4823
        for array in &[a0, a1, a2] {
4824
            let to_type = DataType::Date32;
4825
            let options = CastOptions {
4826
                safe: false,
4827
                format_options: FormatOptions::default(),
4828
            };
4829
            let result = cast_with_options(&array, &to_type, &options).unwrap();
4830
            let c = result.as_primitive::<Date32Type>();
4831
            assert_eq!(
4832
                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
4833
                c.value_as_date(0)
4834
            );
4835
            assert_eq!(
4836
                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
4837
                c.value_as_date(1)
4838
            );
4839
        }
4840
    }
4841
4842
    #[test]
4843
    fn test_cast_string_to_time32second() {
4844
        let a0 = Arc::new(StringViewArray::from(vec![
4845
            Some("08:08:35.091323414"),
4846
            Some("08:08:60.091323414"), // leap second
4847
            Some("08:08:61.091323414"), // not valid
4848
            Some("Not a valid time"),
4849
            None,
4850
        ])) as ArrayRef;
4851
        let a1 = Arc::new(StringArray::from(vec![
4852
            Some("08:08:35.091323414"),
4853
            Some("08:08:60.091323414"), // leap second
4854
            Some("08:08:61.091323414"), // not valid
4855
            Some("Not a valid time"),
4856
            None,
4857
        ])) as ArrayRef;
4858
        let a2 = Arc::new(LargeStringArray::from(vec![
4859
            Some("08:08:35.091323414"),
4860
            Some("08:08:60.091323414"), // leap second
4861
            Some("08:08:61.091323414"), // not valid
4862
            Some("Not a valid time"),
4863
            None,
4864
        ])) as ArrayRef;
4865
        for array in &[a0, a1, a2] {
4866
            let to_type = DataType::Time32(TimeUnit::Second);
4867
            let b = cast(array, &to_type).unwrap();
4868
            let c = b.as_primitive::<Time32SecondType>();
4869
            assert_eq!(29315, c.value(0));
4870
            assert_eq!(29340, c.value(1));
4871
            assert!(c.is_null(2));
4872
            assert!(c.is_null(3));
4873
            assert!(c.is_null(4));
4874
4875
            let options = CastOptions {
4876
                safe: false,
4877
                format_options: FormatOptions::default(),
4878
            };
4879
            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4880
            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Second) type");
4881
        }
4882
    }
4883
4884
    #[test]
4885
    fn test_cast_string_to_time32millisecond() {
4886
        let a0 = Arc::new(StringViewArray::from(vec![
4887
            Some("08:08:35.091323414"),
4888
            Some("08:08:60.091323414"), // leap second
4889
            Some("08:08:61.091323414"), // not valid
4890
            Some("Not a valid time"),
4891
            None,
4892
        ])) as ArrayRef;
4893
        let a1 = Arc::new(StringArray::from(vec![
4894
            Some("08:08:35.091323414"),
4895
            Some("08:08:60.091323414"), // leap second
4896
            Some("08:08:61.091323414"), // not valid
4897
            Some("Not a valid time"),
4898
            None,
4899
        ])) as ArrayRef;
4900
        let a2 = Arc::new(LargeStringArray::from(vec![
4901
            Some("08:08:35.091323414"),
4902
            Some("08:08:60.091323414"), // leap second
4903
            Some("08:08:61.091323414"), // not valid
4904
            Some("Not a valid time"),
4905
            None,
4906
        ])) as ArrayRef;
4907
        for array in &[a0, a1, a2] {
4908
            let to_type = DataType::Time32(TimeUnit::Millisecond);
4909
            let b = cast(array, &to_type).unwrap();
4910
            let c = b.as_primitive::<Time32MillisecondType>();
4911
            assert_eq!(29315091, c.value(0));
4912
            assert_eq!(29340091, c.value(1));
4913
            assert!(c.is_null(2));
4914
            assert!(c.is_null(3));
4915
            assert!(c.is_null(4));
4916
4917
            let options = CastOptions {
4918
                safe: false,
4919
                format_options: FormatOptions::default(),
4920
            };
4921
            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4922
            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Millisecond) type");
4923
        }
4924
    }
4925
4926
    #[test]
4927
    fn test_cast_string_to_time64microsecond() {
4928
        let a0 = Arc::new(StringViewArray::from(vec![
4929
            Some("08:08:35.091323414"),
4930
            Some("Not a valid time"),
4931
            None,
4932
        ])) as ArrayRef;
4933
        let a1 = Arc::new(StringArray::from(vec![
4934
            Some("08:08:35.091323414"),
4935
            Some("Not a valid time"),
4936
            None,
4937
        ])) as ArrayRef;
4938
        let a2 = Arc::new(LargeStringArray::from(vec![
4939
            Some("08:08:35.091323414"),
4940
            Some("Not a valid time"),
4941
            None,
4942
        ])) as ArrayRef;
4943
        for array in &[a0, a1, a2] {
4944
            let to_type = DataType::Time64(TimeUnit::Microsecond);
4945
            let b = cast(array, &to_type).unwrap();
4946
            let c = b.as_primitive::<Time64MicrosecondType>();
4947
            assert_eq!(29315091323, c.value(0));
4948
            assert!(c.is_null(1));
4949
            assert!(c.is_null(2));
4950
4951
            let options = CastOptions {
4952
                safe: false,
4953
                format_options: FormatOptions::default(),
4954
            };
4955
            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4956
            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Microsecond) type");
4957
        }
4958
    }
4959
4960
    #[test]
4961
    fn test_cast_string_to_time64nanosecond() {
4962
        let a0 = Arc::new(StringViewArray::from(vec![
4963
            Some("08:08:35.091323414"),
4964
            Some("Not a valid time"),
4965
            None,
4966
        ])) as ArrayRef;
4967
        let a1 = Arc::new(StringArray::from(vec![
4968
            Some("08:08:35.091323414"),
4969
            Some("Not a valid time"),
4970
            None,
4971
        ])) as ArrayRef;
4972
        let a2 = Arc::new(LargeStringArray::from(vec![
4973
            Some("08:08:35.091323414"),
4974
            Some("Not a valid time"),
4975
            None,
4976
        ])) as ArrayRef;
4977
        for array in &[a0, a1, a2] {
4978
            let to_type = DataType::Time64(TimeUnit::Nanosecond);
4979
            let b = cast(array, &to_type).unwrap();
4980
            let c = b.as_primitive::<Time64NanosecondType>();
4981
            assert_eq!(29315091323414, c.value(0));
4982
            assert!(c.is_null(1));
4983
            assert!(c.is_null(2));
4984
4985
            let options = CastOptions {
4986
                safe: false,
4987
                format_options: FormatOptions::default(),
4988
            };
4989
            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4990
            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Nanosecond) type");
4991
        }
4992
    }
4993
4994
    #[test]
4995
    fn test_cast_string_to_date64() {
4996
        let a0 = Arc::new(StringViewArray::from(vec![
4997
            Some("2020-09-08T12:00:00"),
4998
            Some("Not a valid date"),
4999
            None,
5000
        ])) as ArrayRef;
5001
        let a1 = Arc::new(StringArray::from(vec![
5002
            Some("2020-09-08T12:00:00"),
5003
            Some("Not a valid date"),
5004
            None,
5005
        ])) as ArrayRef;
5006
        let a2 = Arc::new(LargeStringArray::from(vec![
5007
            Some("2020-09-08T12:00:00"),
5008
            Some("Not a valid date"),
5009
            None,
5010
        ])) as ArrayRef;
5011
        for array in &[a0, a1, a2] {
5012
            let to_type = DataType::Date64;
5013
            let b = cast(array, &to_type).unwrap();
5014
            let c = b.as_primitive::<Date64Type>();
5015
            assert_eq!(1599566400000, c.value(0));
5016
            assert!(c.is_null(1));
5017
            assert!(c.is_null(2));
5018
5019
            let options = CastOptions {
5020
                safe: false,
5021
                format_options: FormatOptions::default(),
5022
            };
5023
            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5024
            assert_eq!(
5025
                err.to_string(),
5026
                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5027
            );
5028
        }
5029
    }
5030
5031
    macro_rules! test_safe_string_to_interval {
5032
        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5033
            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5034
5035
            let options = CastOptions {
5036
                safe: true,
5037
                format_options: FormatOptions::default(),
5038
            };
5039
5040
            let target_interval_array = cast_with_options(
5041
                &source_string_array.clone(),
5042
                &DataType::Interval($interval_unit),
5043
                &options,
5044
            )
5045
            .unwrap()
5046
            .as_any()
5047
            .downcast_ref::<$array_ty>()
5048
            .unwrap()
5049
            .clone() as $array_ty;
5050
5051
            let target_string_array =
5052
                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5053
                    .unwrap()
5054
                    .as_any()
5055
                    .downcast_ref::<StringArray>()
5056
                    .unwrap()
5057
                    .clone();
5058
5059
            let expect_string_array = StringArray::from($expect_vec);
5060
5061
            assert_eq!(target_string_array, expect_string_array);
5062
5063
            let target_large_string_array =
5064
                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5065
                    .unwrap()
5066
                    .as_any()
5067
                    .downcast_ref::<LargeStringArray>()
5068
                    .unwrap()
5069
                    .clone();
5070
5071
            let expect_large_string_array = LargeStringArray::from($expect_vec);
5072
5073
            assert_eq!(target_large_string_array, expect_large_string_array);
5074
        };
5075
    }
5076
5077
    #[test]
5078
    fn test_cast_string_to_interval_year_month() {
5079
        test_safe_string_to_interval!(
5080
            vec![
5081
                Some("1 year 1 month"),
5082
                Some("1.5 years 13 month"),
5083
                Some("30 days"),
5084
                Some("31 days"),
5085
                Some("2 months 31 days"),
5086
                Some("2 months 31 days 1 second"),
5087
                Some("foobar"),
5088
            ],
5089
            IntervalUnit::YearMonth,
5090
            IntervalYearMonthArray,
5091
            vec![
5092
                Some("1 years 1 mons"),
5093
                Some("2 years 7 mons"),
5094
                None,
5095
                None,
5096
                None,
5097
                None,
5098
                None,
5099
            ]
5100
        );
5101
    }
5102
5103
    #[test]
5104
    fn test_cast_string_to_interval_day_time() {
5105
        test_safe_string_to_interval!(
5106
            vec![
5107
                Some("1 year 1 month"),
5108
                Some("1.5 years 13 month"),
5109
                Some("30 days"),
5110
                Some("1 day 2 second 3.5 milliseconds"),
5111
                Some("foobar"),
5112
            ],
5113
            IntervalUnit::DayTime,
5114
            IntervalDayTimeArray,
5115
            vec![
5116
                Some("390 days"),
5117
                Some("930 days"),
5118
                Some("30 days"),
5119
                None,
5120
                None,
5121
            ]
5122
        );
5123
    }
5124
5125
    #[test]
5126
    fn test_cast_string_to_interval_month_day_nano() {
5127
        test_safe_string_to_interval!(
5128
            vec![
5129
                Some("1 year 1 month 1 day"),
5130
                None,
5131
                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5132
                Some("3 days"),
5133
                Some("8 seconds"),
5134
                None,
5135
                Some("1 day 29800 milliseconds"),
5136
                Some("3 months 1 second"),
5137
                Some("6 minutes 120 second"),
5138
                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5139
                Some("foobar"),
5140
            ],
5141
            IntervalUnit::MonthDayNano,
5142
            IntervalMonthDayNanoArray,
5143
            vec![
5144
                Some("13 mons 1 days"),
5145
                None,
5146
                Some("31 mons 35 days 0.001400000 secs"),
5147
                Some("3 days"),
5148
                Some("8.000000000 secs"),
5149
                None,
5150
                Some("1 days 29.800000000 secs"),
5151
                Some("3 mons 1.000000000 secs"),
5152
                Some("8 mins"),
5153
                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5154
                None,
5155
            ]
5156
        );
5157
    }
5158
5159
    macro_rules! test_unsafe_string_to_interval_err {
5160
        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5161
            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5162
            let options = CastOptions {
5163
                safe: false,
5164
                format_options: FormatOptions::default(),
5165
            };
5166
            let arrow_err = cast_with_options(
5167
                &string_array.clone(),
5168
                &DataType::Interval($interval_unit),
5169
                &options,
5170
            )
5171
            .unwrap_err();
5172
            assert_eq!($error_msg, arrow_err.to_string());
5173
        };
5174
    }
5175
5176
    #[test]
5177
    fn test_cast_string_to_interval_err() {
5178
        test_unsafe_string_to_interval_err!(
5179
            vec![Some("foobar")],
5180
            IntervalUnit::YearMonth,
5181
            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5182
        );
5183
        test_unsafe_string_to_interval_err!(
5184
            vec![Some("foobar")],
5185
            IntervalUnit::DayTime,
5186
            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5187
        );
5188
        test_unsafe_string_to_interval_err!(
5189
            vec![Some("foobar")],
5190
            IntervalUnit::MonthDayNano,
5191
            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5192
        );
5193
        test_unsafe_string_to_interval_err!(
5194
            vec![Some("2 months 31 days 1 second")],
5195
            IntervalUnit::YearMonth,
5196
            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5197
        );
5198
        test_unsafe_string_to_interval_err!(
5199
            vec![Some("1 day 1.5 milliseconds")],
5200
            IntervalUnit::DayTime,
5201
            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5202
        );
5203
5204
        // overflow
5205
        test_unsafe_string_to_interval_err!(
5206
            vec![Some(format!(
5207
                "{} century {} year {} month",
5208
                i64::MAX - 2,
5209
                i64::MAX - 2,
5210
                i64::MAX - 2
5211
            ))],
5212
            IntervalUnit::DayTime,
5213
            format!(
5214
                "Arithmetic overflow: Overflow happened on: {} * 100",
5215
                i64::MAX - 2
5216
            )
5217
        );
5218
        test_unsafe_string_to_interval_err!(
5219
            vec![Some(format!(
5220
                "{} year {} month {} day",
5221
                i64::MAX - 2,
5222
                i64::MAX - 2,
5223
                i64::MAX - 2
5224
            ))],
5225
            IntervalUnit::MonthDayNano,
5226
            format!(
5227
                "Arithmetic overflow: Overflow happened on: {} * 12",
5228
                i64::MAX - 2
5229
            )
5230
        );
5231
    }
5232
5233
    #[test]
5234
    fn test_cast_binary_to_fixed_size_binary() {
5235
        let bytes_1 = "Hiiii".as_bytes();
5236
        let bytes_2 = "Hello".as_bytes();
5237
5238
        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5239
        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5240
        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5241
5242
        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5243
        let down_cast = array_ref
5244
            .as_any()
5245
            .downcast_ref::<FixedSizeBinaryArray>()
5246
            .unwrap();
5247
        assert_eq!(bytes_1, down_cast.value(0));
5248
        assert_eq!(bytes_2, down_cast.value(1));
5249
        assert!(down_cast.is_null(2));
5250
5251
        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5252
        let down_cast = array_ref
5253
            .as_any()
5254
            .downcast_ref::<FixedSizeBinaryArray>()
5255
            .unwrap();
5256
        assert_eq!(bytes_1, down_cast.value(0));
5257
        assert_eq!(bytes_2, down_cast.value(1));
5258
        assert!(down_cast.is_null(2));
5259
5260
        // test error cases when the length of binary are not same
5261
        let bytes_1 = "Hi".as_bytes();
5262
        let bytes_2 = "Hello".as_bytes();
5263
5264
        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5265
        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5266
        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5267
5268
        let array_ref = cast_with_options(
5269
            &a1,
5270
            &DataType::FixedSizeBinary(5),
5271
            &CastOptions {
5272
                safe: false,
5273
                format_options: FormatOptions::default(),
5274
            },
5275
        );
5276
        assert!(array_ref.is_err());
5277
5278
        let array_ref = cast_with_options(
5279
            &a2,
5280
            &DataType::FixedSizeBinary(5),
5281
            &CastOptions {
5282
                safe: false,
5283
                format_options: FormatOptions::default(),
5284
            },
5285
        );
5286
        assert!(array_ref.is_err());
5287
    }
5288
5289
    #[test]
5290
    fn test_fixed_size_binary_to_binary() {
5291
        let bytes_1 = "Hiiii".as_bytes();
5292
        let bytes_2 = "Hello".as_bytes();
5293
5294
        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5295
        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5296
5297
        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5298
        let down_cast = array_ref.as_binary::<i32>();
5299
        assert_eq!(bytes_1, down_cast.value(0));
5300
        assert_eq!(bytes_2, down_cast.value(1));
5301
        assert!(down_cast.is_null(2));
5302
5303
        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5304
        let down_cast = array_ref.as_binary::<i64>();
5305
        assert_eq!(bytes_1, down_cast.value(0));
5306
        assert_eq!(bytes_2, down_cast.value(1));
5307
        assert!(down_cast.is_null(2));
5308
5309
        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5310
        let down_cast = array_ref.as_binary_view();
5311
        assert_eq!(bytes_1, down_cast.value(0));
5312
        assert_eq!(bytes_2, down_cast.value(1));
5313
        assert!(down_cast.is_null(2));
5314
    }
5315
5316
    #[test]
5317
    fn test_fixed_size_binary_to_dictionary() {
5318
        let bytes_1 = "Hiiii".as_bytes();
5319
        let bytes_2 = "Hello".as_bytes();
5320
5321
        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5322
        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5323
5324
        let cast_type = DataType::Dictionary(
5325
            Box::new(DataType::Int8),
5326
            Box::new(DataType::FixedSizeBinary(5)),
5327
        );
5328
        let cast_array = cast(&a1, &cast_type).unwrap();
5329
        assert_eq!(cast_array.data_type(), &cast_type);
5330
        assert_eq!(
5331
            array_to_strings(&cast_array),
5332
            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5333
        );
5334
        // dictionary should only have two distinct values
5335
        let dict_array = cast_array
5336
            .as_any()
5337
            .downcast_ref::<DictionaryArray<Int8Type>>()
5338
            .unwrap();
5339
        assert_eq!(dict_array.values().len(), 2);
5340
    }
5341
5342
    #[test]
5343
    fn test_binary_to_dictionary() {
5344
        let mut builder = GenericBinaryBuilder::<i32>::new();
5345
        builder.append_value(b"hello");
5346
        builder.append_value(b"hiiii");
5347
        builder.append_value(b"hiiii"); // duplicate
5348
        builder.append_null();
5349
        builder.append_value(b"rustt");
5350
5351
        let a1 = builder.finish();
5352
5353
        let cast_type = DataType::Dictionary(
5354
            Box::new(DataType::Int8),
5355
            Box::new(DataType::FixedSizeBinary(5)),
5356
        );
5357
        let cast_array = cast(&a1, &cast_type).unwrap();
5358
        assert_eq!(cast_array.data_type(), &cast_type);
5359
        assert_eq!(
5360
            array_to_strings(&cast_array),
5361
            vec![
5362
                "68656c6c6f",
5363
                "6869696969",
5364
                "6869696969",
5365
                "null",
5366
                "7275737474"
5367
            ]
5368
        );
5369
        // dictionary should only have three distinct values
5370
        let dict_array = cast_array
5371
            .as_any()
5372
            .downcast_ref::<DictionaryArray<Int8Type>>()
5373
            .unwrap();
5374
        assert_eq!(dict_array.values().len(), 3);
5375
    }
5376
5377
    #[test]
5378
    fn test_numeric_to_binary() {
5379
        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5380
5381
        let array_ref = cast(&a, &DataType::Binary).unwrap();
5382
        let down_cast = array_ref.as_binary::<i32>();
5383
        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5384
        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5385
        assert!(down_cast.is_null(2));
5386
5387
        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5388
5389
        let array_ref = cast(&a, &DataType::Binary).unwrap();
5390
        let down_cast = array_ref.as_binary::<i32>();
5391
        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5392
        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5393
        assert!(down_cast.is_null(2));
5394
    }
5395
5396
    #[test]
5397
    fn test_numeric_to_large_binary() {
5398
        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5399
5400
        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5401
        let down_cast = array_ref.as_binary::<i64>();
5402
        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5403
        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5404
        assert!(down_cast.is_null(2));
5405
5406
        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5407
5408
        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5409
        let down_cast = array_ref.as_binary::<i64>();
5410
        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5411
        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5412
        assert!(down_cast.is_null(2));
5413
    }
5414
5415
    #[test]
5416
    fn test_cast_date32_to_int32() {
5417
        let array = Date32Array::from(vec![10000, 17890]);
5418
        let b = cast(&array, &DataType::Int32).unwrap();
5419
        let c = b.as_primitive::<Int32Type>();
5420
        assert_eq!(10000, c.value(0));
5421
        assert_eq!(17890, c.value(1));
5422
    }
5423
5424
    #[test]
5425
    fn test_cast_int32_to_date32() {
5426
        let array = Int32Array::from(vec![10000, 17890]);
5427
        let b = cast(&array, &DataType::Date32).unwrap();
5428
        let c = b.as_primitive::<Date32Type>();
5429
        assert_eq!(10000, c.value(0));
5430
        assert_eq!(17890, c.value(1));
5431
    }
5432
5433
    #[test]
5434
    fn test_cast_timestamp_to_date32() {
5435
        let array =
5436
            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5437
                .with_timezone("+00:00".to_string());
5438
        let b = cast(&array, &DataType::Date32).unwrap();
5439
        let c = b.as_primitive::<Date32Type>();
5440
        assert_eq!(10000, c.value(0));
5441
        assert_eq!(17890, c.value(1));
5442
        assert!(c.is_null(2));
5443
    }
5444
    #[test]
5445
    fn test_cast_timestamp_to_date32_zone() {
5446
        let strings = StringArray::from_iter([
5447
            Some("1970-01-01T00:00:01"),
5448
            Some("1970-01-01T23:59:59"),
5449
            None,
5450
            Some("2020-03-01T02:00:23+00:00"),
5451
        ]);
5452
        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
5453
        let timestamps = cast(&strings, &dt).unwrap();
5454
        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
5455
5456
        let c = dates.as_primitive::<Date32Type>();
5457
        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
5458
        assert_eq!(c.value_as_date(0).unwrap(), expected);
5459
        assert_eq!(c.value_as_date(1).unwrap(), expected);
5460
        assert!(c.is_null(2));
5461
        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
5462
        assert_eq!(c.value_as_date(3).unwrap(), expected);
5463
    }
5464
    #[test]
5465
    fn test_cast_timestamp_to_date64() {
5466
        let array =
5467
            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
5468
        let b = cast(&array, &DataType::Date64).unwrap();
5469
        let c = b.as_primitive::<Date64Type>();
5470
        assert_eq!(864000000005, c.value(0));
5471
        assert_eq!(1545696000001, c.value(1));
5472
        assert!(c.is_null(2));
5473
5474
        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
5475
        let b = cast(&array, &DataType::Date64).unwrap();
5476
        let c = b.as_primitive::<Date64Type>();
5477
        assert_eq!(864000000005000, c.value(0));
5478
        assert_eq!(1545696000001000, c.value(1));
5479
5480
        // test overflow, safe cast
5481
        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5482
        let b = cast(&array, &DataType::Date64).unwrap();
5483
        assert!(b.is_null(0));
5484
        // test overflow, unsafe cast
5485
        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5486
        let options = CastOptions {
5487
            safe: false,
5488
            format_options: FormatOptions::default(),
5489
        };
5490
        let b = cast_with_options(&array, &DataType::Date64, &options);
5491
        assert!(b.is_err());
5492
    }
5493
5494
    #[test]
5495
    fn test_cast_timestamp_to_time64() {
5496
        // test timestamp secs
5497
        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5498
            .with_timezone("+01:00".to_string());
5499
        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5500
        let c = b.as_primitive::<Time64MicrosecondType>();
5501
        assert_eq!(3605000000, c.value(0));
5502
        assert_eq!(3601000000, c.value(1));
5503
        assert!(c.is_null(2));
5504
        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5505
        let c = b.as_primitive::<Time64NanosecondType>();
5506
        assert_eq!(3605000000000, c.value(0));
5507
        assert_eq!(3601000000000, c.value(1));
5508
        assert!(c.is_null(2));
5509
5510
        // test timestamp milliseconds
5511
        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5512
            .with_timezone("+01:00".to_string());
5513
        let array = Arc::new(a) as ArrayRef;
5514
        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5515
        let c = b.as_primitive::<Time64MicrosecondType>();
5516
        assert_eq!(3605000000, c.value(0));
5517
        assert_eq!(3601000000, c.value(1));
5518
        assert!(c.is_null(2));
5519
        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5520
        let c = b.as_primitive::<Time64NanosecondType>();
5521
        assert_eq!(3605000000000, c.value(0));
5522
        assert_eq!(3601000000000, c.value(1));
5523
        assert!(c.is_null(2));
5524
5525
        // test timestamp microseconds
5526
        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5527
            .with_timezone("+01:00".to_string());
5528
        let array = Arc::new(a) as ArrayRef;
5529
        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5530
        let c = b.as_primitive::<Time64MicrosecondType>();
5531
        assert_eq!(3605000000, c.value(0));
5532
        assert_eq!(3601000000, c.value(1));
5533
        assert!(c.is_null(2));
5534
        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5535
        let c = b.as_primitive::<Time64NanosecondType>();
5536
        assert_eq!(3605000000000, c.value(0));
5537
        assert_eq!(3601000000000, c.value(1));
5538
        assert!(c.is_null(2));
5539
5540
        // test timestamp nanoseconds
5541
        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5542
            .with_timezone("+01:00".to_string());
5543
        let array = Arc::new(a) as ArrayRef;
5544
        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5545
        let c = b.as_primitive::<Time64MicrosecondType>();
5546
        assert_eq!(3605000000, c.value(0));
5547
        assert_eq!(3601000000, c.value(1));
5548
        assert!(c.is_null(2));
5549
        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5550
        let c = b.as_primitive::<Time64NanosecondType>();
5551
        assert_eq!(3605000000000, c.value(0));
5552
        assert_eq!(3601000000000, c.value(1));
5553
        assert!(c.is_null(2));
5554
5555
        // test overflow
5556
        let a =
5557
            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5558
        let array = Arc::new(a) as ArrayRef;
5559
        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5560
        assert!(b.is_err());
5561
        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5562
        assert!(b.is_err());
5563
        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5564
        assert!(b.is_err());
5565
    }
5566
5567
    #[test]
5568
    fn test_cast_timestamp_to_time32() {
5569
        // test timestamp secs
5570
        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5571
            .with_timezone("+01:00".to_string());
5572
        let array = Arc::new(a) as ArrayRef;
5573
        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5574
        let c = b.as_primitive::<Time32SecondType>();
5575
        assert_eq!(3605, c.value(0));
5576
        assert_eq!(3601, c.value(1));
5577
        assert!(c.is_null(2));
5578
        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5579
        let c = b.as_primitive::<Time32MillisecondType>();
5580
        assert_eq!(3605000, c.value(0));
5581
        assert_eq!(3601000, c.value(1));
5582
        assert!(c.is_null(2));
5583
5584
        // test timestamp milliseconds
5585
        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5586
            .with_timezone("+01:00".to_string());
5587
        let array = Arc::new(a) as ArrayRef;
5588
        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5589
        let c = b.as_primitive::<Time32SecondType>();
5590
        assert_eq!(3605, c.value(0));
5591
        assert_eq!(3601, c.value(1));
5592
        assert!(c.is_null(2));
5593
        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5594
        let c = b.as_primitive::<Time32MillisecondType>();
5595
        assert_eq!(3605000, c.value(0));
5596
        assert_eq!(3601000, c.value(1));
5597
        assert!(c.is_null(2));
5598
5599
        // test timestamp microseconds
5600
        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5601
            .with_timezone("+01:00".to_string());
5602
        let array = Arc::new(a) as ArrayRef;
5603
        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5604
        let c = b.as_primitive::<Time32SecondType>();
5605
        assert_eq!(3605, c.value(0));
5606
        assert_eq!(3601, c.value(1));
5607
        assert!(c.is_null(2));
5608
        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5609
        let c = b.as_primitive::<Time32MillisecondType>();
5610
        assert_eq!(3605000, c.value(0));
5611
        assert_eq!(3601000, c.value(1));
5612
        assert!(c.is_null(2));
5613
5614
        // test timestamp nanoseconds
5615
        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5616
            .with_timezone("+01:00".to_string());
5617
        let array = Arc::new(a) as ArrayRef;
5618
        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5619
        let c = b.as_primitive::<Time32SecondType>();
5620
        assert_eq!(3605, c.value(0));
5621
        assert_eq!(3601, c.value(1));
5622
        assert!(c.is_null(2));
5623
        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5624
        let c = b.as_primitive::<Time32MillisecondType>();
5625
        assert_eq!(3605000, c.value(0));
5626
        assert_eq!(3601000, c.value(1));
5627
        assert!(c.is_null(2));
5628
5629
        // test overflow
5630
        let a =
5631
            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5632
        let array = Arc::new(a) as ArrayRef;
5633
        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5634
        assert!(b.is_err());
5635
        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5636
        assert!(b.is_err());
5637
    }
5638
5639
    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
5640
    #[test]
5641
    fn test_cast_timestamp_with_timezone_1() {
5642
        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5643
            Some("2000-01-01T00:00:00.123456789"),
5644
            Some("2010-01-01T00:00:00.123456789"),
5645
            None,
5646
        ]));
5647
        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5648
        let timestamp_array = cast(&string_array, &to_type).unwrap();
5649
5650
        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5651
        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5652
5653
        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5654
        let result = string_array.as_string::<i32>();
5655
        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
5656
        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5657
        assert!(result.is_null(2));
5658
    }
5659
5660
    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
5661
    #[test]
5662
    fn test_cast_timestamp_with_timezone_2() {
5663
        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5664
            Some("2000-01-01T07:00:00.123456789"),
5665
            Some("2010-01-01T07:00:00.123456789"),
5666
            None,
5667
        ]));
5668
        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
5669
        let timestamp_array = cast(&string_array, &to_type).unwrap();
5670
5671
        // Check intermediate representation is correct
5672
        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5673
        let result = string_array.as_string::<i32>();
5674
        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
5675
        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5676
        assert!(result.is_null(2));
5677
5678
        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5679
        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5680
5681
        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5682
        let result = string_array.as_string::<i32>();
5683
        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
5684
        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5685
        assert!(result.is_null(2));
5686
    }
5687
5688
    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
5689
    #[test]
5690
    fn test_cast_timestamp_with_timezone_3() {
5691
        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5692
            Some("2000-01-01T07:00:00.123456789"),
5693
            Some("2010-01-01T07:00:00.123456789"),
5694
            None,
5695
        ]));
5696
        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5697
        let timestamp_array = cast(&string_array, &to_type).unwrap();
5698
5699
        // Check intermediate representation is correct
5700
        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5701
        let result = string_array.as_string::<i32>();
5702
        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5703
        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5704
        assert!(result.is_null(2));
5705
5706
        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5707
        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5708
5709
        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5710
        let result = string_array.as_string::<i32>();
5711
        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5712
        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5713
        assert!(result.is_null(2));
5714
    }
5715
5716
    #[test]
5717
    fn test_cast_date64_to_timestamp() {
5718
        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5719
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5720
        let c = b.as_primitive::<TimestampSecondType>();
5721
        assert_eq!(864000000, c.value(0));
5722
        assert_eq!(1545696000, c.value(1));
5723
        assert!(c.is_null(2));
5724
    }
5725
5726
    #[test]
5727
    fn test_cast_date64_to_timestamp_ms() {
5728
        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5729
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
5730
        let c = b
5731
            .as_any()
5732
            .downcast_ref::<TimestampMillisecondArray>()
5733
            .unwrap();
5734
        assert_eq!(864000000005, c.value(0));
5735
        assert_eq!(1545696000001, c.value(1));
5736
        assert!(c.is_null(2));
5737
    }
5738
5739
    #[test]
5740
    fn test_cast_date64_to_timestamp_us() {
5741
        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5742
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5743
        let c = b
5744
            .as_any()
5745
            .downcast_ref::<TimestampMicrosecondArray>()
5746
            .unwrap();
5747
        assert_eq!(864000000005000, c.value(0));
5748
        assert_eq!(1545696000001000, c.value(1));
5749
        assert!(c.is_null(2));
5750
    }
5751
5752
    #[test]
5753
    fn test_cast_date64_to_timestamp_ns() {
5754
        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5755
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
5756
        let c = b
5757
            .as_any()
5758
            .downcast_ref::<TimestampNanosecondArray>()
5759
            .unwrap();
5760
        assert_eq!(864000000005000000, c.value(0));
5761
        assert_eq!(1545696000001000000, c.value(1));
5762
        assert!(c.is_null(2));
5763
    }
5764
5765
    #[test]
5766
    fn test_cast_timestamp_to_i64() {
5767
        let array =
5768
            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5769
                .with_timezone("UTC".to_string());
5770
        let b = cast(&array, &DataType::Int64).unwrap();
5771
        let c = b.as_primitive::<Int64Type>();
5772
        assert_eq!(&DataType::Int64, c.data_type());
5773
        assert_eq!(864000000005, c.value(0));
5774
        assert_eq!(1545696000001, c.value(1));
5775
        assert!(c.is_null(2));
5776
    }
5777
5778
    #[test]
5779
    fn test_cast_date32_to_string() {
5780
        let array = Date32Array::from(vec![10000, 17890]);
5781
        let b = cast(&array, &DataType::Utf8).unwrap();
5782
        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5783
        assert_eq!(&DataType::Utf8, c.data_type());
5784
        assert_eq!("1997-05-19", c.value(0));
5785
        assert_eq!("2018-12-25", c.value(1));
5786
    }
5787
5788
    #[test]
5789
    fn test_cast_date64_to_string() {
5790
        let array = Date64Array::from(vec![10000 * 86400000, 17890 * 86400000]);
5791
        let b = cast(&array, &DataType::Utf8).unwrap();
5792
        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5793
        assert_eq!(&DataType::Utf8, c.data_type());
5794
        assert_eq!("1997-05-19T00:00:00", c.value(0));
5795
        assert_eq!("2018-12-25T00:00:00", c.value(1));
5796
    }
5797
5798
    macro_rules! assert_cast_timestamp_to_string {
5799
        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
5800
            let out = cast(&$array, &$datatype).unwrap();
5801
            let actual = out
5802
                .as_any()
5803
                .downcast_ref::<$output_array_type>()
5804
                .unwrap()
5805
                .into_iter()
5806
                .collect::<Vec<_>>();
5807
            assert_eq!(actual, $expected);
5808
        }};
5809
        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
5810
            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
5811
            let actual = out
5812
                .as_any()
5813
                .downcast_ref::<$output_array_type>()
5814
                .unwrap()
5815
                .into_iter()
5816
                .collect::<Vec<_>>();
5817
            assert_eq!(actual, $expected);
5818
        }};
5819
    }
5820
5821
    #[test]
5822
    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
5823
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5824
        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
5825
        let array = Arc::new(a) as ArrayRef;
5826
5827
        let b = cast(
5828
            &array,
5829
            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5830
        )
5831
        .unwrap();
5832
        let c = b.as_primitive::<TimestampSecondType>();
5833
        let string_array = cast(&c, &DataType::Utf8).unwrap();
5834
        let result = string_array.as_string::<i32>();
5835
        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5836
5837
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5838
        let c = b.as_primitive::<TimestampSecondType>();
5839
        let string_array = cast(&c, &DataType::Utf8).unwrap();
5840
        let result = string_array.as_string::<i32>();
5841
        assert_eq!("2021-01-01T00:00:00", result.value(0));
5842
    }
5843
5844
    #[test]
5845
    fn test_cast_date32_to_timestamp_with_timezone() {
5846
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5847
        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5848
        let array = Arc::new(a) as ArrayRef;
5849
        let b = cast(
5850
            &array,
5851
            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5852
        )
5853
        .unwrap();
5854
        let c = b.as_primitive::<TimestampSecondType>();
5855
        assert_eq!(1609438500, c.value(0));
5856
        assert_eq!(1640974500, c.value(1));
5857
        assert!(c.is_null(2));
5858
5859
        let string_array = cast(&c, &DataType::Utf8).unwrap();
5860
        let result = string_array.as_string::<i32>();
5861
        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5862
        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5863
    }
5864
5865
    #[test]
5866
    fn test_cast_date32_to_timestamp_with_timezone_ms() {
5867
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5868
        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5869
        let array = Arc::new(a) as ArrayRef;
5870
        let b = cast(
5871
            &array,
5872
            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5873
        )
5874
        .unwrap();
5875
        let c = b.as_primitive::<TimestampMillisecondType>();
5876
        assert_eq!(1609438500000, c.value(0));
5877
        assert_eq!(1640974500000, c.value(1));
5878
        assert!(c.is_null(2));
5879
5880
        let string_array = cast(&c, &DataType::Utf8).unwrap();
5881
        let result = string_array.as_string::<i32>();
5882
        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5883
        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5884
    }
5885
5886
    #[test]
5887
    fn test_cast_date32_to_timestamp_with_timezone_us() {
5888
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5889
        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5890
        let array = Arc::new(a) as ArrayRef;
5891
        let b = cast(
5892
            &array,
5893
            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5894
        )
5895
        .unwrap();
5896
        let c = b.as_primitive::<TimestampMicrosecondType>();
5897
        assert_eq!(1609438500000000, c.value(0));
5898
        assert_eq!(1640974500000000, c.value(1));
5899
        assert!(c.is_null(2));
5900
5901
        let string_array = cast(&c, &DataType::Utf8).unwrap();
5902
        let result = string_array.as_string::<i32>();
5903
        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5904
        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5905
    }
5906
5907
    #[test]
5908
    fn test_cast_date32_to_timestamp_with_timezone_ns() {
5909
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5910
        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5911
        let array = Arc::new(a) as ArrayRef;
5912
        let b = cast(
5913
            &array,
5914
            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5915
        )
5916
        .unwrap();
5917
        let c = b.as_primitive::<TimestampNanosecondType>();
5918
        assert_eq!(1609438500000000000, c.value(0));
5919
        assert_eq!(1640974500000000000, c.value(1));
5920
        assert!(c.is_null(2));
5921
5922
        let string_array = cast(&c, &DataType::Utf8).unwrap();
5923
        let result = string_array.as_string::<i32>();
5924
        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5925
        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5926
    }
5927
5928
    #[test]
5929
    fn test_cast_date64_to_timestamp_with_timezone() {
5930
        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5931
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5932
        let b = cast(
5933
            &array,
5934
            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5935
        )
5936
        .unwrap();
5937
5938
        let c = b.as_primitive::<TimestampSecondType>();
5939
        assert_eq!(863979300, c.value(0));
5940
        assert_eq!(1545675300, c.value(1));
5941
        assert!(c.is_null(2));
5942
5943
        let string_array = cast(&c, &DataType::Utf8).unwrap();
5944
        let result = string_array.as_string::<i32>();
5945
        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
5946
        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
5947
    }
5948
5949
    #[test]
5950
    fn test_cast_date64_to_timestamp_with_timezone_ms() {
5951
        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5952
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5953
        let b = cast(
5954
            &array,
5955
            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5956
        )
5957
        .unwrap();
5958
5959
        let c = b.as_primitive::<TimestampMillisecondType>();
5960
        assert_eq!(863979300005, c.value(0));
5961
        assert_eq!(1545675300001, c.value(1));
5962
        assert!(c.is_null(2));
5963
5964
        let string_array = cast(&c, &DataType::Utf8).unwrap();
5965
        let result = string_array.as_string::<i32>();
5966
        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5967
        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5968
    }
5969
5970
    #[test]
5971
    fn test_cast_date64_to_timestamp_with_timezone_us() {
5972
        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5973
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5974
        let b = cast(
5975
            &array,
5976
            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5977
        )
5978
        .unwrap();
5979
5980
        let c = b.as_primitive::<TimestampMicrosecondType>();
5981
        assert_eq!(863979300005000, c.value(0));
5982
        assert_eq!(1545675300001000, c.value(1));
5983
        assert!(c.is_null(2));
5984
5985
        let string_array = cast(&c, &DataType::Utf8).unwrap();
5986
        let result = string_array.as_string::<i32>();
5987
        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5988
        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5989
    }
5990
5991
    #[test]
5992
    fn test_cast_date64_to_timestamp_with_timezone_ns() {
5993
        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5994
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5995
        let b = cast(
5996
            &array,
5997
            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5998
        )
5999
        .unwrap();
6000
6001
        let c = b.as_primitive::<TimestampNanosecondType>();
6002
        assert_eq!(863979300005000000, c.value(0));
6003
        assert_eq!(1545675300001000000, c.value(1));
6004
        assert!(c.is_null(2));
6005
6006
        let string_array = cast(&c, &DataType::Utf8).unwrap();
6007
        let result = string_array.as_string::<i32>();
6008
        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6009
        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6010
    }
6011
6012
    #[test]
6013
    fn test_cast_timestamp_to_strings() {
6014
        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6015
        let array =
6016
            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6017
        let expected = vec![
6018
            Some("1997-05-19T00:00:03.005"),
6019
            Some("2018-12-25T00:00:02.001"),
6020
            None,
6021
        ];
6022
6023
        assert_cast_timestamp_to_string!(array, DataType::Utf8View, StringViewArray, expected);
6024
        assert_cast_timestamp_to_string!(array, DataType::Utf8, StringArray, expected);
6025
        assert_cast_timestamp_to_string!(array, DataType::LargeUtf8, LargeStringArray, expected);
6026
    }
6027
6028
    #[test]
6029
    fn test_cast_timestamp_to_strings_opt() {
6030
        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6031
        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6032
        let cast_options = CastOptions {
6033
            safe: true,
6034
            format_options: FormatOptions::default()
6035
                .with_timestamp_format(Some(ts_format))
6036
                .with_timestamp_tz_format(Some(ts_format)),
6037
        };
6038
6039
        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6040
        let array_without_tz =
6041
            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6042
        let expected = vec![
6043
            Some("1997-05-19 00:00:03.005000"),
6044
            Some("2018-12-25 00:00:02.001000"),
6045
            None,
6046
        ];
6047
        assert_cast_timestamp_to_string!(
6048
            array_without_tz,
6049
            DataType::Utf8View,
6050
            StringViewArray,
6051
            cast_options,
6052
            expected
6053
        );
6054
        assert_cast_timestamp_to_string!(
6055
            array_without_tz,
6056
            DataType::Utf8,
6057
            StringArray,
6058
            cast_options,
6059
            expected
6060
        );
6061
        assert_cast_timestamp_to_string!(
6062
            array_without_tz,
6063
            DataType::LargeUtf8,
6064
            LargeStringArray,
6065
            cast_options,
6066
            expected
6067
        );
6068
6069
        let array_with_tz =
6070
            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6071
                .with_timezone(tz.to_string());
6072
        let expected = vec![
6073
            Some("1997-05-19 05:45:03.005000"),
6074
            Some("2018-12-25 05:45:02.001000"),
6075
            None,
6076
        ];
6077
        assert_cast_timestamp_to_string!(
6078
            array_with_tz,
6079
            DataType::Utf8View,
6080
            StringViewArray,
6081
            cast_options,
6082
            expected
6083
        );
6084
        assert_cast_timestamp_to_string!(
6085
            array_with_tz,
6086
            DataType::Utf8,
6087
            StringArray,
6088
            cast_options,
6089
            expected
6090
        );
6091
        assert_cast_timestamp_to_string!(
6092
            array_with_tz,
6093
            DataType::LargeUtf8,
6094
            LargeStringArray,
6095
            cast_options,
6096
            expected
6097
        );
6098
    }
6099
6100
    #[test]
6101
    fn test_cast_between_timestamps() {
6102
        let array =
6103
            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6104
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6105
        let c = b.as_primitive::<TimestampSecondType>();
6106
        assert_eq!(864000003, c.value(0));
6107
        assert_eq!(1545696002, c.value(1));
6108
        assert!(c.is_null(2));
6109
    }
6110
6111
    #[test]
6112
    fn test_cast_duration_to_i64() {
6113
        let base = vec![5, 6, 7, 8, 100000000];
6114
6115
        let duration_arrays = vec![
6116
            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6117
            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6118
            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6119
            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6120
        ];
6121
6122
        for arr in duration_arrays {
6123
            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6124
            let result = cast(&arr, &DataType::Int64).unwrap();
6125
            let result = result.as_primitive::<Int64Type>();
6126
            assert_eq!(base.as_slice(), result.values());
6127
        }
6128
    }
6129
6130
    #[test]
6131
    fn test_cast_between_durations_and_numerics() {
6132
        fn test_cast_between_durations<FromType, ToType>()
6133
        where
6134
            FromType: ArrowPrimitiveType<Native = i64>,
6135
            ToType: ArrowPrimitiveType<Native = i64>,
6136
            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6137
        {
6138
            let from_unit = match FromType::DATA_TYPE {
6139
                DataType::Duration(unit) => unit,
6140
                _ => panic!("Expected a duration type"),
6141
            };
6142
            let to_unit = match ToType::DATA_TYPE {
6143
                DataType::Duration(unit) => unit,
6144
                _ => panic!("Expected a duration type"),
6145
            };
6146
            let from_size = time_unit_multiple(&from_unit);
6147
            let to_size = time_unit_multiple(&to_unit);
6148
6149
            let (v1_before, v2_before) = (8640003005, 1696002001);
6150
            let (v1_after, v2_after) = if from_size >= to_size {
6151
                (
6152
                    v1_before / (from_size / to_size),
6153
                    v2_before / (from_size / to_size),
6154
                )
6155
            } else {
6156
                (
6157
                    v1_before * (to_size / from_size),
6158
                    v2_before * (to_size / from_size),
6159
                )
6160
            };
6161
6162
            let array =
6163
                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6164
            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6165
            let c = b.as_primitive::<ToType>();
6166
            assert_eq!(v1_after, c.value(0));
6167
            assert_eq!(v2_after, c.value(1));
6168
            assert!(c.is_null(2));
6169
        }
6170
6171
        // between each individual duration type
6172
        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6173
        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6174
        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6175
        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6176
        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6177
        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6178
        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6179
        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6180
        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6181
        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6182
        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6183
        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6184
6185
        // cast failed
6186
        let array = DurationSecondArray::from(vec![
6187
            Some(i64::MAX),
6188
            Some(8640203410378005),
6189
            Some(10241096),
6190
            None,
6191
        ]);
6192
        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6193
        let c = b.as_primitive::<DurationNanosecondType>();
6194
        assert!(c.is_null(0));
6195
        assert!(c.is_null(1));
6196
        assert_eq!(10241096000000000, c.value(2));
6197
        assert!(c.is_null(3));
6198
6199
        // durations to numerics
6200
        let array = DurationSecondArray::from(vec![
6201
            Some(i64::MAX),
6202
            Some(8640203410378005),
6203
            Some(10241096),
6204
            None,
6205
        ]);
6206
        let b = cast(&array, &DataType::Int64).unwrap();
6207
        let c = b.as_primitive::<Int64Type>();
6208
        assert_eq!(i64::MAX, c.value(0));
6209
        assert_eq!(8640203410378005, c.value(1));
6210
        assert_eq!(10241096, c.value(2));
6211
        assert!(c.is_null(3));
6212
6213
        let b = cast(&array, &DataType::Int32).unwrap();
6214
        let c = b.as_primitive::<Int32Type>();
6215
        assert_eq!(0, c.value(0));
6216
        assert_eq!(0, c.value(1));
6217
        assert_eq!(10241096, c.value(2));
6218
        assert!(c.is_null(3));
6219
6220
        // numerics to durations
6221
        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6222
        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6223
        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6224
        assert_eq!(i32::MAX as i64, c.value(0));
6225
        assert_eq!(802034103, c.value(1));
6226
        assert_eq!(10241096, c.value(2));
6227
        assert!(c.is_null(3));
6228
    }
6229
6230
    #[test]
6231
    fn test_cast_to_strings() {
6232
        let a = Int32Array::from(vec![1, 2, 3]);
6233
        let out = cast(&a, &DataType::Utf8).unwrap();
6234
        let out = out
6235
            .as_any()
6236
            .downcast_ref::<StringArray>()
6237
            .unwrap()
6238
            .into_iter()
6239
            .collect::<Vec<_>>();
6240
        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6241
        let out = cast(&a, &DataType::LargeUtf8).unwrap();
6242
        let out = out
6243
            .as_any()
6244
            .downcast_ref::<LargeStringArray>()
6245
            .unwrap()
6246
            .into_iter()
6247
            .collect::<Vec<_>>();
6248
        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6249
    }
6250
6251
    #[test]
6252
    fn test_str_to_str_casts() {
6253
        for data in [
6254
            vec![Some("foo"), Some("bar"), Some("ham")],
6255
            vec![Some("foo"), None, Some("bar")],
6256
        ] {
6257
            let a = LargeStringArray::from(data.clone());
6258
            let to = cast(&a, &DataType::Utf8).unwrap();
6259
            let expect = a
6260
                .as_any()
6261
                .downcast_ref::<LargeStringArray>()
6262
                .unwrap()
6263
                .into_iter()
6264
                .collect::<Vec<_>>();
6265
            let out = to
6266
                .as_any()
6267
                .downcast_ref::<StringArray>()
6268
                .unwrap()
6269
                .into_iter()
6270
                .collect::<Vec<_>>();
6271
            assert_eq!(expect, out);
6272
6273
            let a = StringArray::from(data);
6274
            let to = cast(&a, &DataType::LargeUtf8).unwrap();
6275
            let expect = a
6276
                .as_any()
6277
                .downcast_ref::<StringArray>()
6278
                .unwrap()
6279
                .into_iter()
6280
                .collect::<Vec<_>>();
6281
            let out = to
6282
                .as_any()
6283
                .downcast_ref::<LargeStringArray>()
6284
                .unwrap()
6285
                .into_iter()
6286
                .collect::<Vec<_>>();
6287
            assert_eq!(expect, out);
6288
        }
6289
    }
6290
6291
    const VIEW_TEST_DATA: [Option<&str>; 5] = [
6292
        Some("hello"),
6293
        Some("repeated"),
6294
        None,
6295
        Some("large payload over 12 bytes"),
6296
        Some("repeated"),
6297
    ];
6298
6299
    #[test]
6300
    fn test_string_view_to_binary_view() {
6301
        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6302
6303
        assert!(can_cast_types(
6304
            string_view_array.data_type(),
6305
            &DataType::BinaryView
6306
        ));
6307
6308
        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
6309
        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6310
6311
        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6312
        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6313
    }
6314
6315
    #[test]
6316
    fn test_binary_view_to_string_view() {
6317
        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6318
6319
        assert!(can_cast_types(
6320
            binary_view_array.data_type(),
6321
            &DataType::Utf8View
6322
        ));
6323
6324
        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
6325
        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6326
6327
        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6328
        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6329
    }
6330
6331
    #[test]
6332
    fn test_string_to_view() {
6333
        _test_string_to_view::<i32>();
6334
        _test_string_to_view::<i64>();
6335
    }
6336
6337
    fn _test_string_to_view<O>()
6338
    where
6339
        O: OffsetSizeTrait,
6340
    {
6341
        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6342
6343
        assert!(can_cast_types(
6344
            string_array.data_type(),
6345
            &DataType::Utf8View
6346
        ));
6347
6348
        assert!(can_cast_types(
6349
            string_array.data_type(),
6350
            &DataType::BinaryView
6351
        ));
6352
6353
        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
6354
        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6355
6356
        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
6357
        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6358
6359
        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6360
        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6361
6362
        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6363
        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6364
    }
6365
6366
    #[test]
6367
    fn test_bianry_to_view() {
6368
        _test_binary_to_view::<i32>();
6369
        _test_binary_to_view::<i64>();
6370
    }
6371
6372
    fn _test_binary_to_view<O>()
6373
    where
6374
        O: OffsetSizeTrait,
6375
    {
6376
        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6377
6378
        assert!(can_cast_types(
6379
            binary_array.data_type(),
6380
            &DataType::Utf8View
6381
        ));
6382
6383
        assert!(can_cast_types(
6384
            binary_array.data_type(),
6385
            &DataType::BinaryView
6386
        ));
6387
6388
        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
6389
        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6390
6391
        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
6392
        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6393
6394
        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6395
        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6396
6397
        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6398
        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6399
    }
6400
6401
    #[test]
6402
    fn test_dict_to_view() {
6403
        let values = StringArray::from_iter(VIEW_TEST_DATA);
6404
        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
6405
        let string_dict_array =
6406
            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
6407
        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
6408
6409
        let string_view_array = {
6410
            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6411
            for v in typed_dict.into_iter() {
6412
                builder.append_option(v);
6413
            }
6414
            builder.finish()
6415
        };
6416
        let expected_string_array_type = string_view_array.data_type();
6417
        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
6418
        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
6419
        assert_eq!(casted_string_array.as_ref(), &string_view_array);
6420
6421
        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
6422
        let binary_dict_array =
6423
            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
6424
        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
6425
6426
        let binary_view_array = {
6427
            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6428
            for v in typed_binary_dict.into_iter() {
6429
                builder.append_option(v);
6430
            }
6431
            builder.finish()
6432
        };
6433
        let expected_binary_array_type = binary_view_array.data_type();
6434
        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
6435
        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
6436
        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
6437
    }
6438
6439
    #[test]
6440
    fn test_view_to_dict() {
6441
        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6442
        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
6443
        let casted_type = string_dict_array.data_type();
6444
        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
6445
        assert_eq!(casted_dict_array.data_type(), casted_type);
6446
        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
6447
6448
        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6449
        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
6450
        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
6451
        let binary_dict_array =
6452
            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
6453
        let casted_type = binary_dict_array.data_type();
6454
        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
6455
        assert_eq!(casted_binary_array.data_type(), casted_type);
6456
        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
6457
    }
6458
6459
    #[test]
6460
    fn test_view_to_string() {
6461
        _test_view_to_string::<i32>();
6462
        _test_view_to_string::<i64>();
6463
    }
6464
6465
    fn _test_view_to_string<O>()
6466
    where
6467
        O: OffsetSizeTrait,
6468
    {
6469
        let string_view_array = {
6470
            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6471
            for s in VIEW_TEST_DATA.iter() {
6472
                builder.append_option(*s);
6473
            }
6474
            builder.finish()
6475
        };
6476
6477
        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6478
6479
        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6480
        let expected_type = expected_string_array.data_type();
6481
6482
        assert!(can_cast_types(string_view_array.data_type(), expected_type));
6483
        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
6484
6485
        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
6486
        assert_eq!(string_view_casted_array.data_type(), expected_type);
6487
        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
6488
6489
        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
6490
        assert_eq!(binary_view_casted_array.data_type(), expected_type);
6491
        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
6492
    }
6493
6494
    #[test]
6495
    fn test_view_to_binary() {
6496
        _test_view_to_binary::<i32>();
6497
        _test_view_to_binary::<i64>();
6498
    }
6499
6500
    fn _test_view_to_binary<O>()
6501
    where
6502
        O: OffsetSizeTrait,
6503
    {
6504
        let view_array = {
6505
            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6506
            for s in VIEW_TEST_DATA.iter() {
6507
                builder.append_option(*s);
6508
            }
6509
            builder.finish()
6510
        };
6511
6512
        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6513
        let expected_type = expected_binary_array.data_type();
6514
6515
        assert!(can_cast_types(view_array.data_type(), expected_type));
6516
6517
        let binary_array = cast(&view_array, expected_type).unwrap();
6518
        assert_eq!(binary_array.data_type(), expected_type);
6519
6520
        assert_eq!(binary_array.as_ref(), &expected_binary_array);
6521
    }
6522
6523
    #[test]
6524
    fn test_cast_from_f64() {
6525
        let f64_values: Vec<f64> = vec![
6526
            i64::MIN as f64,
6527
            i32::MIN as f64,
6528
            i16::MIN as f64,
6529
            i8::MIN as f64,
6530
            0_f64,
6531
            u8::MAX as f64,
6532
            u16::MAX as f64,
6533
            u32::MAX as f64,
6534
            u64::MAX as f64,
6535
        ];
6536
        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6537
6538
        let f64_expected = vec![
6539
            -9223372036854776000.0,
6540
            -2147483648.0,
6541
            -32768.0,
6542
            -128.0,
6543
            0.0,
6544
            255.0,
6545
            65535.0,
6546
            4294967295.0,
6547
            18446744073709552000.0,
6548
        ];
6549
        assert_eq!(
6550
            f64_expected,
6551
            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6552
                .iter()
6553
                .map(|i| i.parse::<f64>().unwrap())
6554
                .collect::<Vec<f64>>()
6555
        );
6556
6557
        let f32_expected = vec![
6558
            -9223372000000000000.0,
6559
            -2147483600.0,
6560
            -32768.0,
6561
            -128.0,
6562
            0.0,
6563
            255.0,
6564
            65535.0,
6565
            4294967300.0,
6566
            18446744000000000000.0,
6567
        ];
6568
        assert_eq!(
6569
            f32_expected,
6570
            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6571
                .iter()
6572
                .map(|i| i.parse::<f32>().unwrap())
6573
                .collect::<Vec<f32>>()
6574
        );
6575
6576
        let f16_expected = vec![
6577
            f16::from_f64(-9223372000000000000.0),
6578
            f16::from_f64(-2147483600.0),
6579
            f16::from_f64(-32768.0),
6580
            f16::from_f64(-128.0),
6581
            f16::from_f64(0.0),
6582
            f16::from_f64(255.0),
6583
            f16::from_f64(65535.0),
6584
            f16::from_f64(4294967300.0),
6585
            f16::from_f64(18446744000000000000.0),
6586
        ];
6587
        assert_eq!(
6588
            f16_expected,
6589
            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6590
                .iter()
6591
                .map(|i| i.parse::<f16>().unwrap())
6592
                .collect::<Vec<f16>>()
6593
        );
6594
6595
        let i64_expected = vec![
6596
            "-9223372036854775808",
6597
            "-2147483648",
6598
            "-32768",
6599
            "-128",
6600
            "0",
6601
            "255",
6602
            "65535",
6603
            "4294967295",
6604
            "null",
6605
        ];
6606
        assert_eq!(
6607
            i64_expected,
6608
            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
6609
        );
6610
6611
        let i32_expected = vec![
6612
            "null",
6613
            "-2147483648",
6614
            "-32768",
6615
            "-128",
6616
            "0",
6617
            "255",
6618
            "65535",
6619
            "null",
6620
            "null",
6621
        ];
6622
        assert_eq!(
6623
            i32_expected,
6624
            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
6625
        );
6626
6627
        let i16_expected = vec![
6628
            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6629
        ];
6630
        assert_eq!(
6631
            i16_expected,
6632
            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
6633
        );
6634
6635
        let i8_expected = vec![
6636
            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6637
        ];
6638
        assert_eq!(
6639
            i8_expected,
6640
            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
6641
        );
6642
6643
        let u64_expected = vec![
6644
            "null",
6645
            "null",
6646
            "null",
6647
            "null",
6648
            "0",
6649
            "255",
6650
            "65535",
6651
            "4294967295",
6652
            "null",
6653
        ];
6654
        assert_eq!(
6655
            u64_expected,
6656
            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
6657
        );
6658
6659
        let u32_expected = vec![
6660
            "null",
6661
            "null",
6662
            "null",
6663
            "null",
6664
            "0",
6665
            "255",
6666
            "65535",
6667
            "4294967295",
6668
            "null",
6669
        ];
6670
        assert_eq!(
6671
            u32_expected,
6672
            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
6673
        );
6674
6675
        let u16_expected = vec![
6676
            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6677
        ];
6678
        assert_eq!(
6679
            u16_expected,
6680
            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
6681
        );
6682
6683
        let u8_expected = vec![
6684
            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6685
        ];
6686
        assert_eq!(
6687
            u8_expected,
6688
            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
6689
        );
6690
    }
6691
6692
    #[test]
6693
    fn test_cast_from_f32() {
6694
        let f32_values: Vec<f32> = vec![
6695
            i32::MIN as f32,
6696
            i32::MIN as f32,
6697
            i16::MIN as f32,
6698
            i8::MIN as f32,
6699
            0_f32,
6700
            u8::MAX as f32,
6701
            u16::MAX as f32,
6702
            u32::MAX as f32,
6703
            u32::MAX as f32,
6704
        ];
6705
        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
6706
6707
        let f64_expected = vec![
6708
            "-2147483648.0",
6709
            "-2147483648.0",
6710
            "-32768.0",
6711
            "-128.0",
6712
            "0.0",
6713
            "255.0",
6714
            "65535.0",
6715
            "4294967296.0",
6716
            "4294967296.0",
6717
        ];
6718
        assert_eq!(
6719
            f64_expected,
6720
            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
6721
        );
6722
6723
        let f32_expected = vec![
6724
            "-2147483600.0",
6725
            "-2147483600.0",
6726
            "-32768.0",
6727
            "-128.0",
6728
            "0.0",
6729
            "255.0",
6730
            "65535.0",
6731
            "4294967300.0",
6732
            "4294967300.0",
6733
        ];
6734
        assert_eq!(
6735
            f32_expected,
6736
            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
6737
        );
6738
6739
        let f16_expected = vec![
6740
            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
6741
        ];
6742
        assert_eq!(
6743
            f16_expected,
6744
            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
6745
        );
6746
6747
        let i64_expected = vec![
6748
            "-2147483648",
6749
            "-2147483648",
6750
            "-32768",
6751
            "-128",
6752
            "0",
6753
            "255",
6754
            "65535",
6755
            "4294967296",
6756
            "4294967296",
6757
        ];
6758
        assert_eq!(
6759
            i64_expected,
6760
            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
6761
        );
6762
6763
        let i32_expected = vec![
6764
            "-2147483648",
6765
            "-2147483648",
6766
            "-32768",
6767
            "-128",
6768
            "0",
6769
            "255",
6770
            "65535",
6771
            "null",
6772
            "null",
6773
        ];
6774
        assert_eq!(
6775
            i32_expected,
6776
            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
6777
        );
6778
6779
        let i16_expected = vec![
6780
            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6781
        ];
6782
        assert_eq!(
6783
            i16_expected,
6784
            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
6785
        );
6786
6787
        let i8_expected = vec![
6788
            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6789
        ];
6790
        assert_eq!(
6791
            i8_expected,
6792
            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
6793
        );
6794
6795
        let u64_expected = vec![
6796
            "null",
6797
            "null",
6798
            "null",
6799
            "null",
6800
            "0",
6801
            "255",
6802
            "65535",
6803
            "4294967296",
6804
            "4294967296",
6805
        ];
6806
        assert_eq!(
6807
            u64_expected,
6808
            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
6809
        );
6810
6811
        let u32_expected = vec![
6812
            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6813
        ];
6814
        assert_eq!(
6815
            u32_expected,
6816
            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
6817
        );
6818
6819
        let u16_expected = vec![
6820
            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6821
        ];
6822
        assert_eq!(
6823
            u16_expected,
6824
            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
6825
        );
6826
6827
        let u8_expected = vec![
6828
            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6829
        ];
6830
        assert_eq!(
6831
            u8_expected,
6832
            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
6833
        );
6834
    }
6835
6836
    #[test]
6837
    fn test_cast_from_uint64() {
6838
        let u64_values: Vec<u64> = vec![
6839
            0,
6840
            u8::MAX as u64,
6841
            u16::MAX as u64,
6842
            u32::MAX as u64,
6843
            u64::MAX,
6844
        ];
6845
        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
6846
6847
        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
6848
        assert_eq!(
6849
            f64_expected,
6850
            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
6851
                .iter()
6852
                .map(|i| i.parse::<f64>().unwrap())
6853
                .collect::<Vec<f64>>()
6854
        );
6855
6856
        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
6857
        assert_eq!(
6858
            f32_expected,
6859
            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
6860
                .iter()
6861
                .map(|i| i.parse::<f32>().unwrap())
6862
                .collect::<Vec<f32>>()
6863
        );
6864
6865
        let f16_expected = vec![
6866
            f16::from_f64(0.0),
6867
            f16::from_f64(255.0),
6868
            f16::from_f64(65535.0),
6869
            f16::from_f64(4294967300.0),
6870
            f16::from_f64(18446744000000000000.0),
6871
        ];
6872
        assert_eq!(
6873
            f16_expected,
6874
            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
6875
                .iter()
6876
                .map(|i| i.parse::<f16>().unwrap())
6877
                .collect::<Vec<f16>>()
6878
        );
6879
6880
        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
6881
        assert_eq!(
6882
            i64_expected,
6883
            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
6884
        );
6885
6886
        let i32_expected = vec!["0", "255", "65535", "null", "null"];
6887
        assert_eq!(
6888
            i32_expected,
6889
            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
6890
        );
6891
6892
        let i16_expected = vec!["0", "255", "null", "null", "null"];
6893
        assert_eq!(
6894
            i16_expected,
6895
            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
6896
        );
6897
6898
        let i8_expected = vec!["0", "null", "null", "null", "null"];
6899
        assert_eq!(
6900
            i8_expected,
6901
            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
6902
        );
6903
6904
        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
6905
        assert_eq!(
6906
            u64_expected,
6907
            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
6908
        );
6909
6910
        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
6911
        assert_eq!(
6912
            u32_expected,
6913
            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
6914
        );
6915
6916
        let u16_expected = vec!["0", "255", "65535", "null", "null"];
6917
        assert_eq!(
6918
            u16_expected,
6919
            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
6920
        );
6921
6922
        let u8_expected = vec!["0", "255", "null", "null", "null"];
6923
        assert_eq!(
6924
            u8_expected,
6925
            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
6926
        );
6927
    }
6928
6929
    #[test]
6930
    fn test_cast_from_uint32() {
6931
        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
6932
        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
6933
6934
        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
6935
        assert_eq!(
6936
            f64_expected,
6937
            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
6938
        );
6939
6940
        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
6941
        assert_eq!(
6942
            f32_expected,
6943
            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
6944
        );
6945
6946
        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
6947
        assert_eq!(
6948
            f16_expected,
6949
            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
6950
        );
6951
6952
        let i64_expected = vec!["0", "255", "65535", "4294967295"];
6953
        assert_eq!(
6954
            i64_expected,
6955
            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
6956
        );
6957
6958
        let i32_expected = vec!["0", "255", "65535", "null"];
6959
        assert_eq!(
6960
            i32_expected,
6961
            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
6962
        );
6963
6964
        let i16_expected = vec!["0", "255", "null", "null"];
6965
        assert_eq!(
6966
            i16_expected,
6967
            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
6968
        );
6969
6970
        let i8_expected = vec!["0", "null", "null", "null"];
6971
        assert_eq!(
6972
            i8_expected,
6973
            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
6974
        );
6975
6976
        let u64_expected = vec!["0", "255", "65535", "4294967295"];
6977
        assert_eq!(
6978
            u64_expected,
6979
            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
6980
        );
6981
6982
        let u32_expected = vec!["0", "255", "65535", "4294967295"];
6983
        assert_eq!(
6984
            u32_expected,
6985
            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
6986
        );
6987
6988
        let u16_expected = vec!["0", "255", "65535", "null"];
6989
        assert_eq!(
6990
            u16_expected,
6991
            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
6992
        );
6993
6994
        let u8_expected = vec!["0", "255", "null", "null"];
6995
        assert_eq!(
6996
            u8_expected,
6997
            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
6998
        );
6999
    }
7000
7001
    #[test]
7002
    fn test_cast_from_uint16() {
7003
        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7004
        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7005
7006
        let f64_expected = vec!["0.0", "255.0", "65535.0"];
7007
        assert_eq!(
7008
            f64_expected,
7009
            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7010
        );
7011
7012
        let f32_expected = vec!["0.0", "255.0", "65535.0"];
7013
        assert_eq!(
7014
            f32_expected,
7015
            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7016
        );
7017
7018
        let f16_expected = vec!["0.0", "255.0", "inf"];
7019
        assert_eq!(
7020
            f16_expected,
7021
            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7022
        );
7023
7024
        let i64_expected = vec!["0", "255", "65535"];
7025
        assert_eq!(
7026
            i64_expected,
7027
            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7028
        );
7029
7030
        let i32_expected = vec!["0", "255", "65535"];
7031
        assert_eq!(
7032
            i32_expected,
7033
            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7034
        );
7035
7036
        let i16_expected = vec!["0", "255", "null"];
7037
        assert_eq!(
7038
            i16_expected,
7039
            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7040
        );
7041
7042
        let i8_expected = vec!["0", "null", "null"];
7043
        assert_eq!(
7044
            i8_expected,
7045
            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7046
        );
7047
7048
        let u64_expected = vec!["0", "255", "65535"];
7049
        assert_eq!(
7050
            u64_expected,
7051
            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7052
        );
7053
7054
        let u32_expected = vec!["0", "255", "65535"];
7055
        assert_eq!(
7056
            u32_expected,
7057
            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7058
        );
7059
7060
        let u16_expected = vec!["0", "255", "65535"];
7061
        assert_eq!(
7062
            u16_expected,
7063
            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7064
        );
7065
7066
        let u8_expected = vec!["0", "255", "null"];
7067
        assert_eq!(
7068
            u8_expected,
7069
            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7070
        );
7071
    }
7072
7073
    #[test]
7074
    fn test_cast_from_uint8() {
7075
        let u8_values: Vec<u8> = vec![0, u8::MAX];
7076
        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7077
7078
        let f64_expected = vec!["0.0", "255.0"];
7079
        assert_eq!(
7080
            f64_expected,
7081
            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7082
        );
7083
7084
        let f32_expected = vec!["0.0", "255.0"];
7085
        assert_eq!(
7086
            f32_expected,
7087
            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7088
        );
7089
7090
        let f16_expected = vec!["0.0", "255.0"];
7091
        assert_eq!(
7092
            f16_expected,
7093
            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7094
        );
7095
7096
        let i64_expected = vec!["0", "255"];
7097
        assert_eq!(
7098
            i64_expected,
7099
            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7100
        );
7101
7102
        let i32_expected = vec!["0", "255"];
7103
        assert_eq!(
7104
            i32_expected,
7105
            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7106
        );
7107
7108
        let i16_expected = vec!["0", "255"];
7109
        assert_eq!(
7110
            i16_expected,
7111
            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7112
        );
7113
7114
        let i8_expected = vec!["0", "null"];
7115
        assert_eq!(
7116
            i8_expected,
7117
            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7118
        );
7119
7120
        let u64_expected = vec!["0", "255"];
7121
        assert_eq!(
7122
            u64_expected,
7123
            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7124
        );
7125
7126
        let u32_expected = vec!["0", "255"];
7127
        assert_eq!(
7128
            u32_expected,
7129
            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7130
        );
7131
7132
        let u16_expected = vec!["0", "255"];
7133
        assert_eq!(
7134
            u16_expected,
7135
            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7136
        );
7137
7138
        let u8_expected = vec!["0", "255"];
7139
        assert_eq!(
7140
            u8_expected,
7141
            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7142
        );
7143
    }
7144
7145
    #[test]
7146
    fn test_cast_from_int64() {
7147
        let i64_values: Vec<i64> = vec![
7148
            i64::MIN,
7149
            i32::MIN as i64,
7150
            i16::MIN as i64,
7151
            i8::MIN as i64,
7152
            0,
7153
            i8::MAX as i64,
7154
            i16::MAX as i64,
7155
            i32::MAX as i64,
7156
            i64::MAX,
7157
        ];
7158
        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7159
7160
        let f64_expected = vec![
7161
            -9223372036854776000.0,
7162
            -2147483648.0,
7163
            -32768.0,
7164
            -128.0,
7165
            0.0,
7166
            127.0,
7167
            32767.0,
7168
            2147483647.0,
7169
            9223372036854776000.0,
7170
        ];
7171
        assert_eq!(
7172
            f64_expected,
7173
            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7174
                .iter()
7175
                .map(|i| i.parse::<f64>().unwrap())
7176
                .collect::<Vec<f64>>()
7177
        );
7178
7179
        let f32_expected = vec![
7180
            -9223372000000000000.0,
7181
            -2147483600.0,
7182
            -32768.0,
7183
            -128.0,
7184
            0.0,
7185
            127.0,
7186
            32767.0,
7187
            2147483600.0,
7188
            9223372000000000000.0,
7189
        ];
7190
        assert_eq!(
7191
            f32_expected,
7192
            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7193
                .iter()
7194
                .map(|i| i.parse::<f32>().unwrap())
7195
                .collect::<Vec<f32>>()
7196
        );
7197
7198
        let f16_expected = vec![
7199
            f16::from_f64(-9223372000000000000.0),
7200
            f16::from_f64(-2147483600.0),
7201
            f16::from_f64(-32768.0),
7202
            f16::from_f64(-128.0),
7203
            f16::from_f64(0.0),
7204
            f16::from_f64(127.0),
7205
            f16::from_f64(32767.0),
7206
            f16::from_f64(2147483600.0),
7207
            f16::from_f64(9223372000000000000.0),
7208
        ];
7209
        assert_eq!(
7210
            f16_expected,
7211
            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7212
                .iter()
7213
                .map(|i| i.parse::<f16>().unwrap())
7214
                .collect::<Vec<f16>>()
7215
        );
7216
7217
        let i64_expected = vec![
7218
            "-9223372036854775808",
7219
            "-2147483648",
7220
            "-32768",
7221
            "-128",
7222
            "0",
7223
            "127",
7224
            "32767",
7225
            "2147483647",
7226
            "9223372036854775807",
7227
        ];
7228
        assert_eq!(
7229
            i64_expected,
7230
            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
7231
        );
7232
7233
        let i32_expected = vec![
7234
            "null",
7235
            "-2147483648",
7236
            "-32768",
7237
            "-128",
7238
            "0",
7239
            "127",
7240
            "32767",
7241
            "2147483647",
7242
            "null",
7243
        ];
7244
        assert_eq!(
7245
            i32_expected,
7246
            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
7247
        );
7248
7249
        assert_eq!(
7250
            i32_expected,
7251
            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
7252
        );
7253
7254
        let i16_expected = vec![
7255
            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
7256
        ];
7257
        assert_eq!(
7258
            i16_expected,
7259
            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
7260
        );
7261
7262
        let i8_expected = vec![
7263
            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
7264
        ];
7265
        assert_eq!(
7266
            i8_expected,
7267
            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
7268
        );
7269
7270
        let u64_expected = vec![
7271
            "null",
7272
            "null",
7273
            "null",
7274
            "null",
7275
            "0",
7276
            "127",
7277
            "32767",
7278
            "2147483647",
7279
            "9223372036854775807",
7280
        ];
7281
        assert_eq!(
7282
            u64_expected,
7283
            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
7284
        );
7285
7286
        let u32_expected = vec![
7287
            "null",
7288
            "null",
7289
            "null",
7290
            "null",
7291
            "0",
7292
            "127",
7293
            "32767",
7294
            "2147483647",
7295
            "null",
7296
        ];
7297
        assert_eq!(
7298
            u32_expected,
7299
            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
7300
        );
7301
7302
        let u16_expected = vec![
7303
            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
7304
        ];
7305
        assert_eq!(
7306
            u16_expected,
7307
            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
7308
        );
7309
7310
        let u8_expected = vec![
7311
            "null", "null", "null", "null", "0", "127", "null", "null", "null",
7312
        ];
7313
        assert_eq!(
7314
            u8_expected,
7315
            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
7316
        );
7317
    }
7318
7319
    #[test]
7320
    fn test_cast_from_int32() {
7321
        let i32_values: Vec<i32> = vec![
7322
            i32::MIN,
7323
            i16::MIN as i32,
7324
            i8::MIN as i32,
7325
            0,
7326
            i8::MAX as i32,
7327
            i16::MAX as i32,
7328
            i32::MAX,
7329
        ];
7330
        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
7331
7332
        let f64_expected = vec![
7333
            "-2147483648.0",
7334
            "-32768.0",
7335
            "-128.0",
7336
            "0.0",
7337
            "127.0",
7338
            "32767.0",
7339
            "2147483647.0",
7340
        ];
7341
        assert_eq!(
7342
            f64_expected,
7343
            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
7344
        );
7345
7346
        let f32_expected = vec![
7347
            "-2147483600.0",
7348
            "-32768.0",
7349
            "-128.0",
7350
            "0.0",
7351
            "127.0",
7352
            "32767.0",
7353
            "2147483600.0",
7354
        ];
7355
        assert_eq!(
7356
            f32_expected,
7357
            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
7358
        );
7359
7360
        let f16_expected = vec![
7361
            f16::from_f64(-2147483600.0),
7362
            f16::from_f64(-32768.0),
7363
            f16::from_f64(-128.0),
7364
            f16::from_f64(0.0),
7365
            f16::from_f64(127.0),
7366
            f16::from_f64(32767.0),
7367
            f16::from_f64(2147483600.0),
7368
        ];
7369
        assert_eq!(
7370
            f16_expected,
7371
            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
7372
                .iter()
7373
                .map(|i| i.parse::<f16>().unwrap())
7374
                .collect::<Vec<f16>>()
7375
        );
7376
7377
        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
7378
        assert_eq!(
7379
            i16_expected,
7380
            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
7381
        );
7382
7383
        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
7384
        assert_eq!(
7385
            i8_expected,
7386
            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
7387
        );
7388
7389
        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7390
        assert_eq!(
7391
            u64_expected,
7392
            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
7393
        );
7394
7395
        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7396
        assert_eq!(
7397
            u32_expected,
7398
            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
7399
        );
7400
7401
        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
7402
        assert_eq!(
7403
            u16_expected,
7404
            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
7405
        );
7406
7407
        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
7408
        assert_eq!(
7409
            u8_expected,
7410
            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
7411
        );
7412
7413
        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
7414
        let i64_expected = vec![
7415
            "-185542587187200000",
7416
            "-2831155200000",
7417
            "-11059200000",
7418
            "0",
7419
            "10972800000",
7420
            "2831068800000",
7421
            "185542587100800000",
7422
        ];
7423
        assert_eq!(
7424
            i64_expected,
7425
            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
7426
        );
7427
    }
7428
7429
    #[test]
7430
    fn test_cast_from_int16() {
7431
        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
7432
        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
7433
7434
        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7435
        assert_eq!(
7436
            f64_expected,
7437
            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
7438
        );
7439
7440
        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7441
        assert_eq!(
7442
            f32_expected,
7443
            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
7444
        );
7445
7446
        let f16_expected = vec![
7447
            f16::from_f64(-32768.0),
7448
            f16::from_f64(-128.0),
7449
            f16::from_f64(0.0),
7450
            f16::from_f64(127.0),
7451
            f16::from_f64(32767.0),
7452
        ];
7453
        assert_eq!(
7454
            f16_expected,
7455
            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
7456
                .iter()
7457
                .map(|i| i.parse::<f16>().unwrap())
7458
                .collect::<Vec<f16>>()
7459
        );
7460
7461
        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
7462
        assert_eq!(
7463
            i64_expected,
7464
            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
7465
        );
7466
7467
        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
7468
        assert_eq!(
7469
            i32_expected,
7470
            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
7471
        );
7472
7473
        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
7474
        assert_eq!(
7475
            i16_expected,
7476
            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
7477
        );
7478
7479
        let i8_expected = vec!["null", "-128", "0", "127", "null"];
7480
        assert_eq!(
7481
            i8_expected,
7482
            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
7483
        );
7484
7485
        let u64_expected = vec!["null", "null", "0", "127", "32767"];
7486
        assert_eq!(
7487
            u64_expected,
7488
            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
7489
        );
7490
7491
        let u32_expected = vec!["null", "null", "0", "127", "32767"];
7492
        assert_eq!(
7493
            u32_expected,
7494
            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
7495
        );
7496
7497
        let u16_expected = vec!["null", "null", "0", "127", "32767"];
7498
        assert_eq!(
7499
            u16_expected,
7500
            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
7501
        );
7502
7503
        let u8_expected = vec!["null", "null", "0", "127", "null"];
7504
        assert_eq!(
7505
            u8_expected,
7506
            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
7507
        );
7508
    }
7509
7510
    #[test]
7511
    fn test_cast_from_date32() {
7512
        let i32_values: Vec<i32> = vec![
7513
            i32::MIN,
7514
            i16::MIN as i32,
7515
            i8::MIN as i32,
7516
            0,
7517
            i8::MAX as i32,
7518
            i16::MAX as i32,
7519
            i32::MAX,
7520
        ];
7521
        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
7522
7523
        let i64_expected = vec![
7524
            "-2147483648",
7525
            "-32768",
7526
            "-128",
7527
            "0",
7528
            "127",
7529
            "32767",
7530
            "2147483647",
7531
        ];
7532
        assert_eq!(
7533
            i64_expected,
7534
            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7535
        );
7536
    }
7537
7538
    #[test]
7539
    fn test_cast_from_int8() {
7540
        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7541
        let i8_array = Int8Array::from(i8_values);
7542
7543
        let f64_expected = vec!["-128.0", "0.0", "127.0"];
7544
        assert_eq!(
7545
            f64_expected,
7546
            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7547
        );
7548
7549
        let f32_expected = vec!["-128.0", "0.0", "127.0"];
7550
        assert_eq!(
7551
            f32_expected,
7552
            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7553
        );
7554
7555
        let f16_expected = vec!["-128.0", "0.0", "127.0"];
7556
        assert_eq!(
7557
            f16_expected,
7558
            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7559
        );
7560
7561
        let i64_expected = vec!["-128", "0", "127"];
7562
        assert_eq!(
7563
            i64_expected,
7564
            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7565
        );
7566
7567
        let i32_expected = vec!["-128", "0", "127"];
7568
        assert_eq!(
7569
            i32_expected,
7570
            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7571
        );
7572
7573
        let i16_expected = vec!["-128", "0", "127"];
7574
        assert_eq!(
7575
            i16_expected,
7576
            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7577
        );
7578
7579
        let i8_expected = vec!["-128", "0", "127"];
7580
        assert_eq!(
7581
            i8_expected,
7582
            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7583
        );
7584
7585
        let u64_expected = vec!["null", "0", "127"];
7586
        assert_eq!(
7587
            u64_expected,
7588
            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7589
        );
7590
7591
        let u32_expected = vec!["null", "0", "127"];
7592
        assert_eq!(
7593
            u32_expected,
7594
            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7595
        );
7596
7597
        let u16_expected = vec!["null", "0", "127"];
7598
        assert_eq!(
7599
            u16_expected,
7600
            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7601
        );
7602
7603
        let u8_expected = vec!["null", "0", "127"];
7604
        assert_eq!(
7605
            u8_expected,
7606
            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
7607
        );
7608
    }
7609
7610
    /// Convert `array` into a vector of strings by casting to data type dt
7611
    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
7612
    where
7613
        T: ArrowPrimitiveType,
7614
    {
7615
        let c = cast(array, dt).unwrap();
7616
        let a = c.as_primitive::<T>();
7617
        let mut v: Vec<String> = vec![];
7618
        for i in 0..array.len() {
7619
            if a.is_null(i) {
7620
                v.push("null".to_string())
7621
            } else {
7622
                v.push(format!("{:?}", a.value(i)));
7623
            }
7624
        }
7625
        v
7626
    }
7627
7628
    #[test]
7629
    fn test_cast_utf8_dict() {
7630
        // FROM a dictionary with of Utf8 values
7631
        use DataType::*;
7632
7633
        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
7634
        builder.append("one").unwrap();
7635
        builder.append_null();
7636
        builder.append("three").unwrap();
7637
        let array: ArrayRef = Arc::new(builder.finish());
7638
7639
        let expected = vec!["one", "null", "three"];
7640
7641
        // Test casting TO StringArray
7642
        let cast_type = Utf8;
7643
        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
7644
        assert_eq!(cast_array.data_type(), &cast_type);
7645
        assert_eq!(array_to_strings(&cast_array), expected);
7646
7647
        // Test casting TO Dictionary (with different index sizes)
7648
7649
        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
7650
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7651
        assert_eq!(cast_array.data_type(), &cast_type);
7652
        assert_eq!(array_to_strings(&cast_array), expected);
7653
7654
        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
7655
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7656
        assert_eq!(cast_array.data_type(), &cast_type);
7657
        assert_eq!(array_to_strings(&cast_array), expected);
7658
7659
        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
7660
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7661
        assert_eq!(cast_array.data_type(), &cast_type);
7662
        assert_eq!(array_to_strings(&cast_array), expected);
7663
7664
        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7665
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7666
        assert_eq!(cast_array.data_type(), &cast_type);
7667
        assert_eq!(array_to_strings(&cast_array), expected);
7668
7669
        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
7670
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7671
        assert_eq!(cast_array.data_type(), &cast_type);
7672
        assert_eq!(array_to_strings(&cast_array), expected);
7673
7674
        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
7675
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7676
        assert_eq!(cast_array.data_type(), &cast_type);
7677
        assert_eq!(array_to_strings(&cast_array), expected);
7678
7679
        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
7680
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7681
        assert_eq!(cast_array.data_type(), &cast_type);
7682
        assert_eq!(array_to_strings(&cast_array), expected);
7683
    }
7684
7685
    #[test]
7686
    fn test_cast_dict_to_dict_bad_index_value_primitive() {
7687
        use DataType::*;
7688
        // test converting from an array that has indexes of a type
7689
        // that are out of bounds for a particular other kind of
7690
        // index.
7691
7692
        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
7693
7694
        // add 200 distinct values (which can be stored by a
7695
        // dictionary indexed by int32, but not a dictionary indexed
7696
        // with int8)
7697
        for i in 0..200 {
7698
            builder.append(i).unwrap();
7699
        }
7700
        let array: ArrayRef = Arc::new(builder.finish());
7701
7702
        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7703
        let res = cast(&array, &cast_type);
7704
        assert!(res.is_err());
7705
        let actual_error = format!("{res:?}");
7706
        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7707
        assert!(
7708
            actual_error.contains(expected_error),
7709
            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7710
        );
7711
    }
7712
7713
    #[test]
7714
    fn test_cast_dict_to_dict_bad_index_value_utf8() {
7715
        use DataType::*;
7716
        // Same test as test_cast_dict_to_dict_bad_index_value but use
7717
        // string values (and encode the expected behavior here);
7718
7719
        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
7720
7721
        // add 200 distinct values (which can be stored by a
7722
        // dictionary indexed by int32, but not a dictionary indexed
7723
        // with int8)
7724
        for i in 0..200 {
7725
            let val = format!("val{i}");
7726
            builder.append(&val).unwrap();
7727
        }
7728
        let array = builder.finish();
7729
7730
        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7731
        let res = cast(&array, &cast_type);
7732
        assert!(res.is_err());
7733
        let actual_error = format!("{res:?}");
7734
        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7735
        assert!(
7736
            actual_error.contains(expected_error),
7737
            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7738
        );
7739
    }
7740
7741
    #[test]
7742
    fn test_cast_primitive_dict() {
7743
        // FROM a dictionary with of INT32 values
7744
        use DataType::*;
7745
7746
        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
7747
        builder.append(1).unwrap();
7748
        builder.append_null();
7749
        builder.append(3).unwrap();
7750
        let array: ArrayRef = Arc::new(builder.finish());
7751
7752
        let expected = vec!["1", "null", "3"];
7753
7754
        // Test casting TO PrimitiveArray, different dictionary type
7755
        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
7756
        assert_eq!(array_to_strings(&cast_array), expected);
7757
        assert_eq!(cast_array.data_type(), &Utf8);
7758
7759
        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
7760
        assert_eq!(array_to_strings(&cast_array), expected);
7761
        assert_eq!(cast_array.data_type(), &Int64);
7762
    }
7763
7764
    #[test]
7765
    fn test_cast_primitive_array_to_dict() {
7766
        use DataType::*;
7767
7768
        let mut builder = PrimitiveBuilder::<Int32Type>::new();
7769
        builder.append_value(1);
7770
        builder.append_null();
7771
        builder.append_value(3);
7772
        let array: ArrayRef = Arc::new(builder.finish());
7773
7774
        let expected = vec!["1", "null", "3"];
7775
7776
        // Cast to a dictionary (same value type, Int32)
7777
        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
7778
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7779
        assert_eq!(cast_array.data_type(), &cast_type);
7780
        assert_eq!(array_to_strings(&cast_array), expected);
7781
7782
        // Cast to a dictionary (different value type, Int8)
7783
        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
7784
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7785
        assert_eq!(cast_array.data_type(), &cast_type);
7786
        assert_eq!(array_to_strings(&cast_array), expected);
7787
    }
7788
7789
    #[test]
7790
    fn test_cast_time_array_to_dict() {
7791
        use DataType::*;
7792
7793
        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
7794
7795
        let expected = vec!["1972-09-27", "null", "1975-06-24"];
7796
7797
        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
7798
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7799
        assert_eq!(cast_array.data_type(), &cast_type);
7800
        assert_eq!(array_to_strings(&cast_array), expected);
7801
    }
7802
7803
    #[test]
7804
    fn test_cast_timestamp_array_to_dict() {
7805
        use DataType::*;
7806
7807
        let array = Arc::new(
7808
            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
7809
        ) as ArrayRef;
7810
7811
        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
7812
7813
        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
7814
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7815
        assert_eq!(cast_array.data_type(), &cast_type);
7816
        assert_eq!(array_to_strings(&cast_array), expected);
7817
    }
7818
7819
    #[test]
7820
    fn test_cast_string_array_to_dict() {
7821
        use DataType::*;
7822
7823
        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
7824
7825
        let expected = vec!["one", "null", "three"];
7826
7827
        // Cast to a dictionary (same value type, Utf8)
7828
        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7829
        let cast_array = cast(&array, &cast_type).expect("cast failed");
7830
        assert_eq!(cast_array.data_type(), &cast_type);
7831
        assert_eq!(array_to_strings(&cast_array), expected);
7832
    }
7833
7834
    #[test]
7835
    fn test_cast_null_array_to_from_decimal_array() {
7836
        let data_type = DataType::Decimal128(12, 4);
7837
        let array = new_null_array(&DataType::Null, 4);
7838
        assert_eq!(array.data_type(), &DataType::Null);
7839
        let cast_array = cast(&array, &data_type).expect("cast failed");
7840
        assert_eq!(cast_array.data_type(), &data_type);
7841
        for i in 0..4 {
7842
            assert!(cast_array.is_null(i));
7843
        }
7844
7845
        let array = new_null_array(&data_type, 4);
7846
        assert_eq!(array.data_type(), &data_type);
7847
        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
7848
        assert_eq!(cast_array.data_type(), &DataType::Null);
7849
        assert_eq!(cast_array.len(), 4);
7850
        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
7851
    }
7852
7853
    #[test]
7854
    fn test_cast_null_array_from_and_to_primitive_array() {
7855
        macro_rules! typed_test {
7856
            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
7857
                {
7858
                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
7859
                    let expected = $ARR_TYPE::from(vec![None; 6]);
7860
                    let cast_type = DataType::$DATATYPE;
7861
                    let cast_array = cast(&array, &cast_type).expect("cast failed");
7862
                    let cast_array = cast_array.as_primitive::<$TYPE>();
7863
                    assert_eq!(cast_array.data_type(), &cast_type);
7864
                    assert_eq!(cast_array, &expected);
7865
                }
7866
            }};
7867
        }
7868
7869
        typed_test!(Int16Array, Int16, Int16Type);
7870
        typed_test!(Int32Array, Int32, Int32Type);
7871
        typed_test!(Int64Array, Int64, Int64Type);
7872
7873
        typed_test!(UInt16Array, UInt16, UInt16Type);
7874
        typed_test!(UInt32Array, UInt32, UInt32Type);
7875
        typed_test!(UInt64Array, UInt64, UInt64Type);
7876
7877
        typed_test!(Float32Array, Float32, Float32Type);
7878
        typed_test!(Float64Array, Float64, Float64Type);
7879
7880
        typed_test!(Date32Array, Date32, Date32Type);
7881
        typed_test!(Date64Array, Date64, Date64Type);
7882
    }
7883
7884
    fn cast_from_null_to_other(data_type: &DataType) {
7885
        // Cast from null to data_type
7886
        {
7887
            let array = new_null_array(&DataType::Null, 4);
7888
            assert_eq!(array.data_type(), &DataType::Null);
7889
            let cast_array = cast(&array, data_type).expect("cast failed");
7890
            assert_eq!(cast_array.data_type(), data_type);
7891
            for i in 0..4 {
7892
                assert!(cast_array.is_null(i));
7893
            }
7894
        }
7895
    }
7896
7897
    #[test]
7898
    fn test_cast_null_from_and_to_variable_sized() {
7899
        cast_from_null_to_other(&DataType::Utf8);
7900
        cast_from_null_to_other(&DataType::LargeUtf8);
7901
        cast_from_null_to_other(&DataType::Binary);
7902
        cast_from_null_to_other(&DataType::LargeBinary);
7903
    }
7904
7905
    #[test]
7906
    fn test_cast_null_from_and_to_nested_type() {
7907
        // Cast null from and to map
7908
        let data_type = DataType::Map(
7909
            Arc::new(Field::new_struct(
7910
                "entry",
7911
                vec![
7912
                    Field::new("key", DataType::Utf8, false),
7913
                    Field::new("value", DataType::Int32, true),
7914
                ],
7915
                false,
7916
            )),
7917
            false,
7918
        );
7919
        cast_from_null_to_other(&data_type);
7920
7921
        // Cast null from and to list
7922
        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
7923
        cast_from_null_to_other(&data_type);
7924
        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
7925
        cast_from_null_to_other(&data_type);
7926
        let data_type =
7927
            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
7928
        cast_from_null_to_other(&data_type);
7929
7930
        // Cast null from and to dictionary
7931
        let values = vec![None, None, None, None] as Vec<Option<&str>>;
7932
        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
7933
        let array = Arc::new(array) as ArrayRef;
7934
        let data_type = array.data_type().to_owned();
7935
        cast_from_null_to_other(&data_type);
7936
7937
        // Cast null from and to struct
7938
        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
7939
        cast_from_null_to_other(&data_type);
7940
    }
7941
7942
    /// Print the `DictionaryArray` `array` as a vector of strings
7943
    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
7944
        let options = FormatOptions::new().with_null("null");
7945
        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
7946
        (0..array.len())
7947
            .map(|i| formatter.value(i).to_string())
7948
            .collect()
7949
    }
7950
7951
    #[test]
7952
    fn test_cast_utf8_to_date32() {
7953
        use chrono::NaiveDate;
7954
        let from_ymd = chrono::NaiveDate::from_ymd_opt;
7955
        let since = chrono::NaiveDate::signed_duration_since;
7956
7957
        let a = StringArray::from(vec![
7958
            "2000-01-01",          // valid date with leading 0s
7959
            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
7960
            "2000-2-2",            // valid date without leading 0s
7961
            "2000-00-00",          // invalid month and day
7962
            "2000",                // just a year is invalid
7963
        ]);
7964
        let array = Arc::new(a) as ArrayRef;
7965
        let b = cast(&array, &DataType::Date32).unwrap();
7966
        let c = b.as_primitive::<Date32Type>();
7967
7968
        // test valid inputs
7969
        let date_value = since(
7970
            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
7971
            from_ymd(1970, 1, 1).unwrap(),
7972
        )
7973
        .num_days() as i32;
7974
        assert!(c.is_valid(0)); // "2000-01-01"
7975
        assert_eq!(date_value, c.value(0));
7976
7977
        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
7978
        assert_eq!(date_value, c.value(1));
7979
7980
        let date_value = since(
7981
            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
7982
            from_ymd(1970, 1, 1).unwrap(),
7983
        )
7984
        .num_days() as i32;
7985
        assert!(c.is_valid(2)); // "2000-2-2"
7986
        assert_eq!(date_value, c.value(2));
7987
7988
        // test invalid inputs
7989
        assert!(!c.is_valid(3)); // "2000-00-00"
7990
        assert!(!c.is_valid(4)); // "2000"
7991
    }
7992
7993
    #[test]
7994
    fn test_cast_utf8_to_date64() {
7995
        let a = StringArray::from(vec![
7996
            "2000-01-01T12:00:00", // date + time valid
7997
            "2020-12-15T12:34:56", // date + time valid
7998
            "2020-2-2T12:34:56",   // valid date time without leading 0s
7999
            "2000-00-00T12:00:00", // invalid month and day
8000
            "2000-01-01 12:00:00", // missing the 'T'
8001
            "2000-01-01",          // just a date is invalid
8002
        ]);
8003
        let array = Arc::new(a) as ArrayRef;
8004
        let b = cast(&array, &DataType::Date64).unwrap();
8005
        let c = b.as_primitive::<Date64Type>();
8006
8007
        // test valid inputs
8008
        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
8009
        assert_eq!(946728000000, c.value(0));
8010
        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
8011
        assert_eq!(1608035696000, c.value(1));
8012
        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
8013
8014
        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
8015
        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
8016
        assert_eq!(946728000000, c.value(4));
8017
        assert!(c.is_valid(5)); // "2000-01-01"
8018
        assert_eq!(946684800000, c.value(5));
8019
    }
8020
8021
    #[test]
8022
    fn test_can_cast_fsl_to_fsl() {
8023
        let from_array = Arc::new(
8024
            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8025
                [Some([Some(1.0), Some(2.0)]), None],
8026
                2,
8027
            ),
8028
        ) as ArrayRef;
8029
        let to_array = Arc::new(
8030
            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8031
                [
8032
                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8033
                    None,
8034
                ],
8035
                2,
8036
            ),
8037
        ) as ArrayRef;
8038
8039
        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8040
        let actual = cast(&from_array, to_array.data_type()).unwrap();
8041
        assert_eq!(actual.data_type(), to_array.data_type());
8042
8043
        let invalid_target =
8044
            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8045
        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8046
8047
        let invalid_size =
8048
            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8049
        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8050
    }
8051
8052
    #[test]
8053
    fn test_can_cast_types_fixed_size_list_to_list() {
8054
        // DataType::List
8055
        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
8056
        assert!(can_cast_types(
8057
            array1.data_type(),
8058
            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8059
        ));
8060
8061
        // DataType::LargeList
8062
        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
8063
        assert!(can_cast_types(
8064
            array2.data_type(),
8065
            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8066
        ));
8067
    }
8068
8069
    #[test]
8070
    fn test_cast_fixed_size_list_to_list() {
8071
        // Important cases:
8072
        // 1. With/without nulls
8073
        // 2. LargeList and List
8074
        // 3. With and without inner casts
8075
8076
        let cases = [
8077
            // fixed_size_list<i32, 2> => list<i32>
8078
            (
8079
                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8080
                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8081
                    2,
8082
                )) as ArrayRef,
8083
                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8084
                    Some([Some(1), Some(1)]),
8085
                    Some([Some(2), Some(2)]),
8086
                ])) as ArrayRef,
8087
            ),
8088
            // fixed_size_list<i32, 2> => list<i32> (nullable)
8089
            (
8090
                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8091
                    [None, Some([Some(2), Some(2)])],
8092
                    2,
8093
                )) as ArrayRef,
8094
                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8095
                    None,
8096
                    Some([Some(2), Some(2)]),
8097
                ])) as ArrayRef,
8098
            ),
8099
            // fixed_size_list<i32, 2> => large_list<i64>
8100
            (
8101
                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8102
                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8103
                    2,
8104
                )) as ArrayRef,
8105
                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8106
                    Some([Some(1), Some(1)]),
8107
                    Some([Some(2), Some(2)]),
8108
                ])) as ArrayRef,
8109
            ),
8110
            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
8111
            (
8112
                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8113
                    [None, Some([Some(2), Some(2)])],
8114
                    2,
8115
                )) as ArrayRef,
8116
                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8117
                    None,
8118
                    Some([Some(2), Some(2)]),
8119
                ])) as ArrayRef,
8120
            ),
8121
        ];
8122
8123
        for (array, expected) in cases {
8124
            let array = Arc::new(array) as ArrayRef;
8125
8126
            assert!(
8127
                can_cast_types(array.data_type(), expected.data_type()),
8128
                "can_cast_types claims we cannot cast {:?} to {:?}",
8129
                array.data_type(),
8130
                expected.data_type()
8131
            );
8132
8133
            let list_array = cast(&array, expected.data_type())
8134
                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8135
            assert_eq!(
8136
                list_array.as_ref(),
8137
                &expected,
8138
                "Incorrect result from casting {array:?} to {expected:?}",
8139
            );
8140
        }
8141
    }
8142
8143
    #[test]
8144
    fn test_cast_utf8_to_list() {
8145
        // DataType::List
8146
        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8147
        let field = Arc::new(Field::new("", DataType::Int32, false));
8148
        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8149
        let actual = list_array.as_list_opt::<i32>().unwrap();
8150
        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8151
        assert_eq!(&expect.value(0), &actual.value(0));
8152
8153
        // DataType::LargeList
8154
        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8155
        let actual = list_array.as_list_opt::<i64>().unwrap();
8156
        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8157
        assert_eq!(&expect.value(0), &actual.value(0));
8158
8159
        // DataType::FixedSizeList
8160
        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8161
        let actual = list_array.as_fixed_size_list_opt().unwrap();
8162
        let expect =
8163
            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
8164
        assert_eq!(&expect.value(0), &actual.value(0));
8165
    }
8166
8167
    #[test]
8168
    fn test_cast_single_element_fixed_size_list() {
8169
        // FixedSizeList<T>[1] => T
8170
        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8171
            [(Some([Some(5)]))],
8172
            1,
8173
        )) as ArrayRef;
8174
        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
8175
        let actual: &Int32Array = casted_array.as_primitive();
8176
        let expected = Int32Array::from(vec![Some(5)]);
8177
        assert_eq!(&expected, actual);
8178
8179
        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
8180
        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8181
            [(Some([Some(5)]))],
8182
            1,
8183
        )) as ArrayRef;
8184
        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
8185
        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8186
        let expected = Arc::new(FixedSizeListArray::new(
8187
            to_field.clone(),
8188
            1,
8189
            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8190
            None,
8191
        )) as ArrayRef;
8192
        assert_eq!(*expected, *actual);
8193
8194
        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
8195
        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8196
            [(Some([Some(5)]))],
8197
            1,
8198
        )) as ArrayRef;
8199
        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
8200
        let to_field = Arc::new(Field::new(
8201
            "dummy",
8202
            DataType::FixedSizeList(to_field_inner.clone(), 1),
8203
            false,
8204
        ));
8205
        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8206
        let expected = Arc::new(FixedSizeListArray::new(
8207
            to_field.clone(),
8208
            1,
8209
            Arc::new(FixedSizeListArray::new(
8210
                to_field_inner.clone(),
8211
                1,
8212
                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8213
                None,
8214
            )) as ArrayRef,
8215
            None,
8216
        )) as ArrayRef;
8217
        assert_eq!(*expected, *actual);
8218
8219
        // T => FixedSizeList<T>[1] (non-nullable)
8220
        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
8221
        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
8222
        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8223
        let actual = casted_array.as_fixed_size_list();
8224
        let expected = Arc::new(FixedSizeListArray::new(
8225
            field.clone(),
8226
            1,
8227
            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8228
            None,
8229
        )) as ArrayRef;
8230
        assert_eq!(expected.as_ref(), actual);
8231
8232
        // T => FixedSizeList<T>[1] (nullable)
8233
        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
8234
        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
8235
        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8236
        let actual = casted_array.as_fixed_size_list();
8237
        let expected = Arc::new(FixedSizeListArray::new(
8238
            field.clone(),
8239
            1,
8240
            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
8241
            None,
8242
        )) as ArrayRef;
8243
        assert_eq!(expected.as_ref(), actual);
8244
    }
8245
8246
    #[test]
8247
    fn test_cast_list_containers() {
8248
        // large-list to list
8249
        let array = Arc::new(make_large_list_array()) as ArrayRef;
8250
        let list_array = cast(
8251
            &array,
8252
            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
8253
        )
8254
        .unwrap();
8255
        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
8256
        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
8257
8258
        assert_eq!(&expected.value(0), &actual.value(0));
8259
        assert_eq!(&expected.value(1), &actual.value(1));
8260
        assert_eq!(&expected.value(2), &actual.value(2));
8261
8262
        // list to large-list
8263
        let array = Arc::new(make_list_array()) as ArrayRef;
8264
        let large_list_array = cast(
8265
            &array,
8266
            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
8267
        )
8268
        .unwrap();
8269
        let actual = large_list_array
8270
            .as_any()
8271
            .downcast_ref::<LargeListArray>()
8272
            .unwrap();
8273
        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
8274
8275
        assert_eq!(&expected.value(0), &actual.value(0));
8276
        assert_eq!(&expected.value(1), &actual.value(1));
8277
        assert_eq!(&expected.value(2), &actual.value(2));
8278
    }
8279
8280
    #[test]
8281
    fn test_cast_list_to_fsl() {
8282
        // There four noteworthy cases we should handle:
8283
        // 1. No nulls
8284
        // 2. Nulls that are always empty
8285
        // 3. Nulls that have varying lengths
8286
        // 4. Nulls that are correctly sized (same as target list size)
8287
8288
        // Non-null case
8289
        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8290
        let values = vec![
8291
            Some(vec![Some(1), Some(2), Some(3)]),
8292
            Some(vec![Some(4), Some(5), Some(6)]),
8293
        ];
8294
        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8295
            values.clone(),
8296
        )) as ArrayRef;
8297
        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8298
            values, 3,
8299
        )) as ArrayRef;
8300
        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8301
        assert_eq!(expected.as_ref(), actual.as_ref());
8302
8303
        // Null cases
8304
        // Array is [[1, 2, 3], null, [4, 5, 6], null]
8305
        let cases = [
8306
            (
8307
                // Zero-length nulls
8308
                vec![1, 2, 3, 4, 5, 6],
8309
                vec![3, 0, 3, 0],
8310
            ),
8311
            (
8312
                // Varying-length nulls
8313
                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
8314
                vec![3, 2, 3, 1],
8315
            ),
8316
            (
8317
                // Correctly-sized nulls
8318
                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
8319
                vec![3, 3, 3, 3],
8320
            ),
8321
            (
8322
                // Mixed nulls
8323
                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
8324
                vec![3, 0, 3, 3],
8325
            ),
8326
        ];
8327
        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
8328
8329
        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8330
            vec![
8331
                Some(vec![Some(1), Some(2), Some(3)]),
8332
                None,
8333
                Some(vec![Some(4), Some(5), Some(6)]),
8334
                None,
8335
            ],
8336
            3,
8337
        )) as ArrayRef;
8338
8339
        for (values, lengths) in cases.iter() {
8340
            let array = Arc::new(ListArray::new(
8341
                field.clone(),
8342
                OffsetBuffer::from_lengths(lengths.clone()),
8343
                Arc::new(Int32Array::from(values.clone())),
8344
                Some(null_buffer.clone()),
8345
            )) as ArrayRef;
8346
            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8347
            assert_eq!(expected.as_ref(), actual.as_ref());
8348
        }
8349
    }
8350
8351
    #[test]
8352
    fn test_cast_list_to_fsl_safety() {
8353
        let values = vec![
8354
            Some(vec![Some(1), Some(2), Some(3)]),
8355
            Some(vec![Some(4), Some(5)]),
8356
            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
8357
            Some(vec![Some(3), Some(4), Some(5)]),
8358
        ];
8359
        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8360
            values.clone(),
8361
        )) as ArrayRef;
8362
8363
        let res = cast_with_options(
8364
            array.as_ref(),
8365
            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8366
            &CastOptions {
8367
                safe: false,
8368
                ..Default::default()
8369
            },
8370
        );
8371
        assert!(res.is_err());
8372
        assert!(format!("{res:?}")
8373
            .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2"));
8374
8375
        // When safe=true (default), the cast will fill nulls for lists that are
8376
        // too short and truncate lists that are too long.
8377
        let res = cast(
8378
            array.as_ref(),
8379
            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8380
        )
8381
        .unwrap();
8382
        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8383
            vec![
8384
                Some(vec![Some(1), Some(2), Some(3)]),
8385
                None, // Too short -> replaced with null
8386
                None, // Too long -> replaced with null
8387
                Some(vec![Some(3), Some(4), Some(5)]),
8388
            ],
8389
            3,
8390
        )) as ArrayRef;
8391
        assert_eq!(expected.as_ref(), res.as_ref());
8392
8393
        // The safe option is false and the source array contains a null list.
8394
        // issue: https://github.com/apache/arrow-rs/issues/5642
8395
        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
8396
            Some(vec![Some(1), Some(2), Some(3)]),
8397
            None,
8398
        ])) as ArrayRef;
8399
        let res = cast_with_options(
8400
            array.as_ref(),
8401
            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8402
            &CastOptions {
8403
                safe: false,
8404
                ..Default::default()
8405
            },
8406
        )
8407
        .unwrap();
8408
        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8409
            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
8410
            3,
8411
        )) as ArrayRef;
8412
        assert_eq!(expected.as_ref(), res.as_ref());
8413
    }
8414
8415
    #[test]
8416
    fn test_cast_large_list_to_fsl() {
8417
        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
8418
        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8419
            values.clone(),
8420
        )) as ArrayRef;
8421
        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8422
            values, 2,
8423
        )) as ArrayRef;
8424
        let actual = cast(
8425
            array.as_ref(),
8426
            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
8427
        )
8428
        .unwrap();
8429
        assert_eq!(expected.as_ref(), actual.as_ref());
8430
    }
8431
8432
    #[test]
8433
    fn test_cast_list_to_fsl_subcast() {
8434
        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8435
            vec![
8436
                Some(vec![Some(1), Some(2)]),
8437
                Some(vec![Some(3), Some(i32::MAX)]),
8438
            ],
8439
        )) as ArrayRef;
8440
        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
8441
            vec![
8442
                Some(vec![Some(1), Some(2)]),
8443
                Some(vec![Some(3), Some(i32::MAX as i64)]),
8444
            ],
8445
            2,
8446
        )) as ArrayRef;
8447
        let actual = cast(
8448
            array.as_ref(),
8449
            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
8450
        )
8451
        .unwrap();
8452
        assert_eq!(expected.as_ref(), actual.as_ref());
8453
8454
        let res = cast_with_options(
8455
            array.as_ref(),
8456
            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
8457
            &CastOptions {
8458
                safe: false,
8459
                ..Default::default()
8460
            },
8461
        );
8462
        assert!(res.is_err());
8463
        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
8464
    }
8465
8466
    #[test]
8467
    fn test_cast_list_to_fsl_empty() {
8468
        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8469
        let array = new_empty_array(&DataType::List(field.clone()));
8470
8471
        let target_type = DataType::FixedSizeList(field.clone(), 3);
8472
        let expected = new_empty_array(&target_type);
8473
8474
        let actual = cast(array.as_ref(), &target_type).unwrap();
8475
        assert_eq!(expected.as_ref(), actual.as_ref());
8476
    }
8477
8478
    fn make_list_array() -> ListArray {
8479
        // Construct a value array
8480
        let value_data = ArrayData::builder(DataType::Int32)
8481
            .len(8)
8482
            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8483
            .build()
8484
            .unwrap();
8485
8486
        // Construct a buffer for value offsets, for the nested array:
8487
        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8488
        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8489
8490
        // Construct a list array from the above two
8491
        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8492
        let list_data = ArrayData::builder(list_data_type)
8493
            .len(3)
8494
            .add_buffer(value_offsets)
8495
            .add_child_data(value_data)
8496
            .build()
8497
            .unwrap();
8498
        ListArray::from(list_data)
8499
    }
8500
8501
    fn make_large_list_array() -> LargeListArray {
8502
        // Construct a value array
8503
        let value_data = ArrayData::builder(DataType::Int32)
8504
            .len(8)
8505
            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8506
            .build()
8507
            .unwrap();
8508
8509
        // Construct a buffer for value offsets, for the nested array:
8510
        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8511
        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
8512
8513
        // Construct a list array from the above two
8514
        let list_data_type =
8515
            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8516
        let list_data = ArrayData::builder(list_data_type)
8517
            .len(3)
8518
            .add_buffer(value_offsets)
8519
            .add_child_data(value_data)
8520
            .build()
8521
            .unwrap();
8522
        LargeListArray::from(list_data)
8523
    }
8524
8525
    fn make_fixed_size_list_array() -> FixedSizeListArray {
8526
        // Construct a value array
8527
        let value_data = ArrayData::builder(DataType::Int32)
8528
            .len(8)
8529
            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8530
            .build()
8531
            .unwrap();
8532
8533
        let list_data_type =
8534
            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8535
        let list_data = ArrayData::builder(list_data_type)
8536
            .len(2)
8537
            .add_child_data(value_data)
8538
            .build()
8539
            .unwrap();
8540
        FixedSizeListArray::from(list_data)
8541
    }
8542
8543
    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8544
        // Construct a value array
8545
        let value_data = ArrayData::builder(DataType::Int64)
8546
            .len(8)
8547
            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8548
            .build()
8549
            .unwrap();
8550
8551
        let list_data_type =
8552
            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8553
        let list_data = ArrayData::builder(list_data_type)
8554
            .len(2)
8555
            .add_child_data(value_data)
8556
            .build()
8557
            .unwrap();
8558
        FixedSizeListArray::from(list_data)
8559
    }
8560
8561
    #[test]
8562
    fn test_cast_map_dont_allow_change_of_order() {
8563
        let string_builder = StringBuilder::new();
8564
        let value_builder = StringBuilder::new();
8565
        let mut builder = MapBuilder::new(
8566
            Some(MapFieldNames {
8567
                entry: "entries".to_string(),
8568
                key: "key".to_string(),
8569
                value: "value".to_string(),
8570
            }),
8571
            string_builder,
8572
            value_builder,
8573
        );
8574
8575
        builder.keys().append_value("0");
8576
        builder.values().append_value("test_val_1");
8577
        builder.append(true).unwrap();
8578
        builder.keys().append_value("1");
8579
        builder.values().append_value("test_val_2");
8580
        builder.append(true).unwrap();
8581
8582
        // map builder returns unsorted map by default
8583
        let array = builder.finish();
8584
8585
        let new_ordered = true;
8586
        let new_type = DataType::Map(
8587
            Arc::new(Field::new(
8588
                "entries",
8589
                DataType::Struct(
8590
                    vec![
8591
                        Field::new("key", DataType::Utf8, false),
8592
                        Field::new("value", DataType::Utf8, false),
8593
                    ]
8594
                    .into(),
8595
                ),
8596
                false,
8597
            )),
8598
            new_ordered,
8599
        );
8600
8601
        let new_array_result = cast(&array, &new_type.clone());
8602
        assert!(!can_cast_types(array.data_type(), &new_type));
8603
        assert!(
8604
            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"#)
8605
        );
8606
    }
8607
8608
    #[test]
8609
    fn test_cast_map_dont_allow_when_container_cant_cast() {
8610
        let string_builder = StringBuilder::new();
8611
        let value_builder = IntervalDayTimeArray::builder(2);
8612
        let mut builder = MapBuilder::new(
8613
            Some(MapFieldNames {
8614
                entry: "entries".to_string(),
8615
                key: "key".to_string(),
8616
                value: "value".to_string(),
8617
            }),
8618
            string_builder,
8619
            value_builder,
8620
        );
8621
8622
        builder.keys().append_value("0");
8623
        builder.values().append_value(IntervalDayTime::new(1, 1));
8624
        builder.append(true).unwrap();
8625
        builder.keys().append_value("1");
8626
        builder.values().append_value(IntervalDayTime::new(2, 2));
8627
        builder.append(true).unwrap();
8628
8629
        // map builder returns unsorted map by default
8630
        let array = builder.finish();
8631
8632
        let new_ordered = true;
8633
        let new_type = DataType::Map(
8634
            Arc::new(Field::new(
8635
                "entries",
8636
                DataType::Struct(
8637
                    vec![
8638
                        Field::new("key", DataType::Utf8, false),
8639
                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
8640
                    ]
8641
                    .into(),
8642
                ),
8643
                false,
8644
            )),
8645
            new_ordered,
8646
        );
8647
8648
        let new_array_result = cast(&array, &new_type.clone());
8649
        assert!(!can_cast_types(array.data_type(), &new_type));
8650
        assert!(
8651
            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"#)
8652
        );
8653
    }
8654
8655
    #[test]
8656
    fn test_cast_map_field_names() {
8657
        let string_builder = StringBuilder::new();
8658
        let value_builder = StringBuilder::new();
8659
        let mut builder = MapBuilder::new(
8660
            Some(MapFieldNames {
8661
                entry: "entries".to_string(),
8662
                key: "key".to_string(),
8663
                value: "value".to_string(),
8664
            }),
8665
            string_builder,
8666
            value_builder,
8667
        );
8668
8669
        builder.keys().append_value("0");
8670
        builder.values().append_value("test_val_1");
8671
        builder.append(true).unwrap();
8672
        builder.keys().append_value("1");
8673
        builder.values().append_value("test_val_2");
8674
        builder.append(true).unwrap();
8675
        builder.append(false).unwrap();
8676
8677
        let array = builder.finish();
8678
8679
        let new_type = DataType::Map(
8680
            Arc::new(Field::new(
8681
                "entries_new",
8682
                DataType::Struct(
8683
                    vec![
8684
                        Field::new("key_new", DataType::Utf8, false),
8685
                        Field::new("value_values", DataType::Utf8, false),
8686
                    ]
8687
                    .into(),
8688
                ),
8689
                false,
8690
            )),
8691
            false,
8692
        );
8693
8694
        assert_ne!(new_type, array.data_type().clone());
8695
8696
        let new_array = cast(&array, &new_type.clone()).unwrap();
8697
        assert_eq!(new_type, new_array.data_type().clone());
8698
        let map_array = new_array.as_map();
8699
8700
        assert_ne!(new_type, array.data_type().clone());
8701
        assert_eq!(new_type, map_array.data_type().clone());
8702
8703
        let key_string = map_array
8704
            .keys()
8705
            .as_any()
8706
            .downcast_ref::<StringArray>()
8707
            .unwrap()
8708
            .into_iter()
8709
            .flatten()
8710
            .collect::<Vec<_>>();
8711
        assert_eq!(&key_string, &vec!["0", "1"]);
8712
8713
        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8714
        let values_string = values_string_array
8715
            .as_any()
8716
            .downcast_ref::<StringArray>()
8717
            .unwrap()
8718
            .into_iter()
8719
            .flatten()
8720
            .collect::<Vec<_>>();
8721
        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
8722
8723
        assert_eq!(
8724
            map_array.nulls(),
8725
            Some(&NullBuffer::from(vec![true, true, false]))
8726
        );
8727
    }
8728
8729
    #[test]
8730
    fn test_cast_map_contained_values() {
8731
        let string_builder = StringBuilder::new();
8732
        let value_builder = Int8Builder::new();
8733
        let mut builder = MapBuilder::new(
8734
            Some(MapFieldNames {
8735
                entry: "entries".to_string(),
8736
                key: "key".to_string(),
8737
                value: "value".to_string(),
8738
            }),
8739
            string_builder,
8740
            value_builder,
8741
        );
8742
8743
        builder.keys().append_value("0");
8744
        builder.values().append_value(44);
8745
        builder.append(true).unwrap();
8746
        builder.keys().append_value("1");
8747
        builder.values().append_value(22);
8748
        builder.append(true).unwrap();
8749
8750
        let array = builder.finish();
8751
8752
        let new_type = DataType::Map(
8753
            Arc::new(Field::new(
8754
                "entries",
8755
                DataType::Struct(
8756
                    vec![
8757
                        Field::new("key", DataType::Utf8, false),
8758
                        Field::new("value", DataType::Utf8, false),
8759
                    ]
8760
                    .into(),
8761
                ),
8762
                false,
8763
            )),
8764
            false,
8765
        );
8766
8767
        let new_array = cast(&array, &new_type.clone()).unwrap();
8768
        assert_eq!(new_type, new_array.data_type().clone());
8769
        let map_array = new_array.as_map();
8770
8771
        assert_ne!(new_type, array.data_type().clone());
8772
        assert_eq!(new_type, map_array.data_type().clone());
8773
8774
        let key_string = map_array
8775
            .keys()
8776
            .as_any()
8777
            .downcast_ref::<StringArray>()
8778
            .unwrap()
8779
            .into_iter()
8780
            .flatten()
8781
            .collect::<Vec<_>>();
8782
        assert_eq!(&key_string, &vec!["0", "1"]);
8783
8784
        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8785
        let values_string = values_string_array
8786
            .as_any()
8787
            .downcast_ref::<StringArray>()
8788
            .unwrap()
8789
            .into_iter()
8790
            .flatten()
8791
            .collect::<Vec<_>>();
8792
        assert_eq!(&values_string, &vec!["44", "22"]);
8793
    }
8794
8795
    #[test]
8796
    fn test_utf8_cast_offsets() {
8797
        // test if offset of the array is taken into account during cast
8798
        let str_array = StringArray::from(vec!["a", "b", "c"]);
8799
        let str_array = str_array.slice(1, 2);
8800
8801
        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
8802
8803
        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
8804
        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
8805
        assert_eq!(strs, &["b", "c"])
8806
    }
8807
8808
    #[test]
8809
    fn test_list_cast_offsets() {
8810
        // test if offset of the array is taken into account during cast
8811
        let array1 = make_list_array().slice(1, 2);
8812
        let array2 = Arc::new(make_list_array()) as ArrayRef;
8813
8814
        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8815
        let out1 = cast(&array1, &dt).unwrap();
8816
        let out2 = cast(&array2, &dt).unwrap();
8817
8818
        assert_eq!(&out1, &out2.slice(1, 2))
8819
    }
8820
8821
    #[test]
8822
    fn test_list_to_string() {
8823
        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
8824
        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8825
        let value_data = str_array.into_data();
8826
8827
        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
8828
        let list_data = ArrayData::builder(list_data_type)
8829
            .len(3)
8830
            .add_buffer(value_offsets)
8831
            .add_child_data(value_data)
8832
            .build()
8833
            .unwrap();
8834
        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
8835
8836
        let out = cast(&array, &DataType::Utf8).unwrap();
8837
        let out = out
8838
            .as_any()
8839
            .downcast_ref::<StringArray>()
8840
            .unwrap()
8841
            .into_iter()
8842
            .flatten()
8843
            .collect::<Vec<_>>();
8844
        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8845
8846
        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8847
        let out = out
8848
            .as_any()
8849
            .downcast_ref::<LargeStringArray>()
8850
            .unwrap()
8851
            .into_iter()
8852
            .flatten()
8853
            .collect::<Vec<_>>();
8854
        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8855
8856
        let array = Arc::new(make_list_array()) as ArrayRef;
8857
        let out = cast(&array, &DataType::Utf8).unwrap();
8858
        let out = out
8859
            .as_any()
8860
            .downcast_ref::<StringArray>()
8861
            .unwrap()
8862
            .into_iter()
8863
            .flatten()
8864
            .collect::<Vec<_>>();
8865
        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8866
8867
        let array = Arc::new(make_large_list_array()) as ArrayRef;
8868
        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8869
        let out = out
8870
            .as_any()
8871
            .downcast_ref::<LargeStringArray>()
8872
            .unwrap()
8873
            .into_iter()
8874
            .flatten()
8875
            .collect::<Vec<_>>();
8876
        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8877
    }
8878
8879
    #[test]
8880
    fn test_cast_f64_to_decimal128() {
8881
        // to reproduce https://github.com/apache/arrow-rs/issues/2997
8882
8883
        let decimal_type = DataType::Decimal128(18, 2);
8884
        let array = Float64Array::from(vec![
8885
            Some(0.0699999999),
8886
            Some(0.0659999999),
8887
            Some(0.0650000000),
8888
            Some(0.0649999999),
8889
        ]);
8890
        let array = Arc::new(array) as ArrayRef;
8891
        generate_cast_test_case!(
8892
            &array,
8893
            Decimal128Array,
8894
            &decimal_type,
8895
            vec![
8896
                Some(7_i128), // round up
8897
                Some(7_i128), // round up
8898
                Some(7_i128), // round up
8899
                Some(6_i128), // round down
8900
            ]
8901
        );
8902
8903
        let decimal_type = DataType::Decimal128(18, 3);
8904
        let array = Float64Array::from(vec![
8905
            Some(0.0699999999),
8906
            Some(0.0659999999),
8907
            Some(0.0650000000),
8908
            Some(0.0649999999),
8909
        ]);
8910
        let array = Arc::new(array) as ArrayRef;
8911
        generate_cast_test_case!(
8912
            &array,
8913
            Decimal128Array,
8914
            &decimal_type,
8915
            vec![
8916
                Some(70_i128), // round up
8917
                Some(66_i128), // round up
8918
                Some(65_i128), // round down
8919
                Some(65_i128), // round up
8920
            ]
8921
        );
8922
    }
8923
8924
    #[test]
8925
    fn test_cast_numeric_to_decimal128_overflow() {
8926
        let array = Int64Array::from(vec![i64::MAX]);
8927
        let array = Arc::new(array) as ArrayRef;
8928
        let casted_array = cast_with_options(
8929
            &array,
8930
            &DataType::Decimal128(38, 30),
8931
            &CastOptions {
8932
                safe: true,
8933
                format_options: FormatOptions::default(),
8934
            },
8935
        );
8936
        assert!(casted_array.is_ok());
8937
        assert!(casted_array.unwrap().is_null(0));
8938
8939
        let casted_array = cast_with_options(
8940
            &array,
8941
            &DataType::Decimal128(38, 30),
8942
            &CastOptions {
8943
                safe: false,
8944
                format_options: FormatOptions::default(),
8945
            },
8946
        );
8947
        assert!(casted_array.is_err());
8948
    }
8949
8950
    #[test]
8951
    fn test_cast_numeric_to_decimal256_overflow() {
8952
        let array = Int64Array::from(vec![i64::MAX]);
8953
        let array = Arc::new(array) as ArrayRef;
8954
        let casted_array = cast_with_options(
8955
            &array,
8956
            &DataType::Decimal256(76, 76),
8957
            &CastOptions {
8958
                safe: true,
8959
                format_options: FormatOptions::default(),
8960
            },
8961
        );
8962
        assert!(casted_array.is_ok());
8963
        assert!(casted_array.unwrap().is_null(0));
8964
8965
        let casted_array = cast_with_options(
8966
            &array,
8967
            &DataType::Decimal256(76, 76),
8968
            &CastOptions {
8969
                safe: false,
8970
                format_options: FormatOptions::default(),
8971
            },
8972
        );
8973
        assert!(casted_array.is_err());
8974
    }
8975
8976
    #[test]
8977
    fn test_cast_floating_point_to_decimal128_precision_overflow() {
8978
        let array = Float64Array::from(vec![1.1]);
8979
        let array = Arc::new(array) as ArrayRef;
8980
        let casted_array = cast_with_options(
8981
            &array,
8982
            &DataType::Decimal128(2, 2),
8983
            &CastOptions {
8984
                safe: true,
8985
                format_options: FormatOptions::default(),
8986
            },
8987
        );
8988
        assert!(casted_array.is_ok());
8989
        assert!(casted_array.unwrap().is_null(0));
8990
8991
        let casted_array = cast_with_options(
8992
            &array,
8993
            &DataType::Decimal128(2, 2),
8994
            &CastOptions {
8995
                safe: false,
8996
                format_options: FormatOptions::default(),
8997
            },
8998
        );
8999
        let err = casted_array.unwrap_err().to_string();
9000
        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal128 of precision 2. Max is 99";
9001
        assert!(
9002
            err.contains(expected_error),
9003
            "did not find expected error '{expected_error}' in actual error '{err}'"
9004
        );
9005
    }
9006
9007
    #[test]
9008
    fn test_cast_floating_point_to_decimal256_precision_overflow() {
9009
        let array = Float64Array::from(vec![1.1]);
9010
        let array = Arc::new(array) as ArrayRef;
9011
        let casted_array = cast_with_options(
9012
            &array,
9013
            &DataType::Decimal256(2, 2),
9014
            &CastOptions {
9015
                safe: true,
9016
                format_options: FormatOptions::default(),
9017
            },
9018
        );
9019
        assert!(casted_array.is_ok());
9020
        assert!(casted_array.unwrap().is_null(0));
9021
9022
        let casted_array = cast_with_options(
9023
            &array,
9024
            &DataType::Decimal256(2, 2),
9025
            &CastOptions {
9026
                safe: false,
9027
                format_options: FormatOptions::default(),
9028
            },
9029
        );
9030
        let err = casted_array.unwrap_err().to_string();
9031
        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal256 of precision 2. Max is 99";
9032
        assert!(
9033
            err.contains(expected_error),
9034
            "did not find expected error '{expected_error}' in actual error '{err}'"
9035
        );
9036
    }
9037
9038
    #[test]
9039
    fn test_cast_floating_point_to_decimal128_overflow() {
9040
        let array = Float64Array::from(vec![f64::MAX]);
9041
        let array = Arc::new(array) as ArrayRef;
9042
        let casted_array = cast_with_options(
9043
            &array,
9044
            &DataType::Decimal128(38, 30),
9045
            &CastOptions {
9046
                safe: true,
9047
                format_options: FormatOptions::default(),
9048
            },
9049
        );
9050
        assert!(casted_array.is_ok());
9051
        assert!(casted_array.unwrap().is_null(0));
9052
9053
        let casted_array = cast_with_options(
9054
            &array,
9055
            &DataType::Decimal128(38, 30),
9056
            &CastOptions {
9057
                safe: false,
9058
                format_options: FormatOptions::default(),
9059
            },
9060
        );
9061
        let err = casted_array.unwrap_err().to_string();
9062
        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
9063
        assert!(
9064
            err.contains(expected_error),
9065
            "did not find expected error '{expected_error}' in actual error '{err}'"
9066
        );
9067
    }
9068
9069
    #[test]
9070
    fn test_cast_floating_point_to_decimal256_overflow() {
9071
        let array = Float64Array::from(vec![f64::MAX]);
9072
        let array = Arc::new(array) as ArrayRef;
9073
        let casted_array = cast_with_options(
9074
            &array,
9075
            &DataType::Decimal256(76, 50),
9076
            &CastOptions {
9077
                safe: true,
9078
                format_options: FormatOptions::default(),
9079
            },
9080
        );
9081
        assert!(casted_array.is_ok());
9082
        assert!(casted_array.unwrap().is_null(0));
9083
9084
        let casted_array = cast_with_options(
9085
            &array,
9086
            &DataType::Decimal256(76, 50),
9087
            &CastOptions {
9088
                safe: false,
9089
                format_options: FormatOptions::default(),
9090
            },
9091
        );
9092
        let err = casted_array.unwrap_err().to_string();
9093
        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
9094
        assert!(
9095
            err.contains(expected_error),
9096
            "did not find expected error '{expected_error}' in actual error '{err}'"
9097
        );
9098
    }
9099
    #[test]
9100
    fn test_cast_decimal256_to_f64_no_overflow() {
9101
        // Test casting i256::MAX: should produce a large finite positive value
9102
        let array = vec![Some(i256::MAX)];
9103
        let array = create_decimal256_array(array, 76, 2).unwrap();
9104
        let array = Arc::new(array) as ArrayRef;
9105
9106
        let result = cast(&array, &DataType::Float64).unwrap();
9107
        let result = result.as_primitive::<Float64Type>();
9108
        assert!(result.value(0).is_finite());
9109
        assert!(result.value(0) > 0.0); // Positive result
9110
9111
        // Test casting i256::MIN: should produce a large finite negative value
9112
        let array = vec![Some(i256::MIN)];
9113
        let array = create_decimal256_array(array, 76, 2).unwrap();
9114
        let array = Arc::new(array) as ArrayRef;
9115
9116
        let result = cast(&array, &DataType::Float64).unwrap();
9117
        let result = result.as_primitive::<Float64Type>();
9118
        assert!(result.value(0).is_finite());
9119
        assert!(result.value(0) < 0.0); // Negative result
9120
    }
9121
9122
    #[test]
9123
    fn test_cast_decimal128_to_decimal128_negative_scale() {
9124
        let input_type = DataType::Decimal128(20, 0);
9125
        let output_type = DataType::Decimal128(20, -1);
9126
        assert!(can_cast_types(&input_type, &output_type));
9127
        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
9128
        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
9129
        let array = Arc::new(input_decimal_array) as ArrayRef;
9130
        generate_cast_test_case!(
9131
            &array,
9132
            Decimal128Array,
9133
            &output_type,
9134
            vec![
9135
                Some(112345_i128),
9136
                Some(212346_i128),
9137
                Some(312346_i128),
9138
                None
9139
            ]
9140
        );
9141
9142
        let casted_array = cast(&array, &output_type).unwrap();
9143
        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9144
9145
        assert_eq!("1123450", decimal_arr.value_as_string(0));
9146
        assert_eq!("2123460", decimal_arr.value_as_string(1));
9147
        assert_eq!("3123460", decimal_arr.value_as_string(2));
9148
    }
9149
9150
    #[test]
9151
    fn decimal128_min_max_to_f64() {
9152
        // Ensure Decimal128 i128::MIN/MAX round-trip cast
9153
        let min128 = i128::MIN;
9154
        let max128 = i128::MAX;
9155
        assert_eq!(min128 as f64, min128 as f64);
9156
        assert_eq!(max128 as f64, max128 as f64);
9157
    }
9158
9159
    #[test]
9160
    fn test_cast_numeric_to_decimal128_negative() {
9161
        let decimal_type = DataType::Decimal128(38, -1);
9162
        let array = Arc::new(Int32Array::from(vec![
9163
            Some(1123456),
9164
            Some(2123456),
9165
            Some(3123456),
9166
        ])) as ArrayRef;
9167
9168
        let casted_array = cast(&array, &decimal_type).unwrap();
9169
        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9170
9171
        assert_eq!("1123450", decimal_arr.value_as_string(0));
9172
        assert_eq!("2123450", decimal_arr.value_as_string(1));
9173
        assert_eq!("3123450", decimal_arr.value_as_string(2));
9174
9175
        let array = Arc::new(Float32Array::from(vec![
9176
            Some(1123.456),
9177
            Some(2123.456),
9178
            Some(3123.456),
9179
        ])) as ArrayRef;
9180
9181
        let casted_array = cast(&array, &decimal_type).unwrap();
9182
        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9183
9184
        assert_eq!("1120", decimal_arr.value_as_string(0));
9185
        assert_eq!("2120", decimal_arr.value_as_string(1));
9186
        assert_eq!("3120", decimal_arr.value_as_string(2));
9187
    }
9188
9189
    #[test]
9190
    fn test_cast_decimal128_to_decimal128_negative() {
9191
        let input_type = DataType::Decimal128(10, -1);
9192
        let output_type = DataType::Decimal128(10, -2);
9193
        assert!(can_cast_types(&input_type, &output_type));
9194
        let array = vec![Some(123)];
9195
        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9196
        let array = Arc::new(input_decimal_array) as ArrayRef;
9197
        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
9198
9199
        let casted_array = cast(&array, &output_type).unwrap();
9200
        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9201
9202
        assert_eq!("1200", decimal_arr.value_as_string(0));
9203
9204
        let array = vec![Some(125)];
9205
        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9206
        let array = Arc::new(input_decimal_array) as ArrayRef;
9207
        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
9208
9209
        let casted_array = cast(&array, &output_type).unwrap();
9210
        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9211
9212
        assert_eq!("1300", decimal_arr.value_as_string(0));
9213
    }
9214
9215
    #[test]
9216
    fn test_cast_decimal128_to_decimal256_negative() {
9217
        let input_type = DataType::Decimal128(10, 3);
9218
        let output_type = DataType::Decimal256(10, 5);
9219
        assert!(can_cast_types(&input_type, &output_type));
9220
        let array = vec![Some(123456), Some(-123456)];
9221
        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
9222
        let array = Arc::new(input_decimal_array) as ArrayRef;
9223
9224
        let hundred = i256::from_i128(100);
9225
        generate_cast_test_case!(
9226
            &array,
9227
            Decimal256Array,
9228
            &output_type,
9229
            vec![
9230
                Some(i256::from_i128(123456).mul_wrapping(hundred)),
9231
                Some(i256::from_i128(-123456).mul_wrapping(hundred))
9232
            ]
9233
        );
9234
    }
9235
9236
    #[test]
9237
    fn test_parse_string_to_decimal() {
9238
        assert_eq!(
9239
            Decimal128Type::format_decimal(
9240
                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
9241
                38,
9242
                2,
9243
            ),
9244
            "123.45"
9245
        );
9246
        assert_eq!(
9247
            Decimal128Type::format_decimal(
9248
                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
9249
                38,
9250
                2,
9251
            ),
9252
            "12345.00"
9253
        );
9254
        assert_eq!(
9255
            Decimal128Type::format_decimal(
9256
                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
9257
                38,
9258
                2,
9259
            ),
9260
            "0.12"
9261
        );
9262
        assert_eq!(
9263
            Decimal128Type::format_decimal(
9264
                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
9265
                38,
9266
                2,
9267
            ),
9268
            "0.12"
9269
        );
9270
        assert_eq!(
9271
            Decimal128Type::format_decimal(
9272
                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9273
                38,
9274
                2,
9275
            ),
9276
            "0.13"
9277
        );
9278
        assert_eq!(
9279
            Decimal128Type::format_decimal(
9280
                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9281
                38,
9282
                2,
9283
            ),
9284
            "0.13"
9285
        );
9286
9287
        assert_eq!(
9288
            Decimal256Type::format_decimal(
9289
                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
9290
                38,
9291
                3,
9292
            ),
9293
            "123.450"
9294
        );
9295
        assert_eq!(
9296
            Decimal256Type::format_decimal(
9297
                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
9298
                38,
9299
                3,
9300
            ),
9301
            "12345.000"
9302
        );
9303
        assert_eq!(
9304
            Decimal256Type::format_decimal(
9305
                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
9306
                38,
9307
                3,
9308
            ),
9309
            "0.123"
9310
        );
9311
        assert_eq!(
9312
            Decimal256Type::format_decimal(
9313
                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
9314
                38,
9315
                3,
9316
            ),
9317
            "0.123"
9318
        );
9319
        assert_eq!(
9320
            Decimal256Type::format_decimal(
9321
                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
9322
                38,
9323
                3,
9324
            ),
9325
            "0.127"
9326
        );
9327
    }
9328
9329
    fn test_cast_string_to_decimal(array: ArrayRef) {
9330
        // Decimal128
9331
        let output_type = DataType::Decimal128(38, 2);
9332
        assert!(can_cast_types(array.data_type(), &output_type));
9333
9334
        let casted_array = cast(&array, &output_type).unwrap();
9335
        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9336
9337
        assert_eq!("123.45", decimal_arr.value_as_string(0));
9338
        assert_eq!("1.23", decimal_arr.value_as_string(1));
9339
        assert_eq!("0.12", decimal_arr.value_as_string(2));
9340
        assert_eq!("0.13", decimal_arr.value_as_string(3));
9341
        assert_eq!("1.26", decimal_arr.value_as_string(4));
9342
        assert_eq!("12345.00", decimal_arr.value_as_string(5));
9343
        assert_eq!("12345.00", decimal_arr.value_as_string(6));
9344
        assert_eq!("0.12", decimal_arr.value_as_string(7));
9345
        assert_eq!("12.23", decimal_arr.value_as_string(8));
9346
        assert!(decimal_arr.is_null(9));
9347
        assert_eq!("0.00", decimal_arr.value_as_string(10));
9348
        assert_eq!("0.00", decimal_arr.value_as_string(11));
9349
        assert!(decimal_arr.is_null(12));
9350
        assert_eq!("-1.23", decimal_arr.value_as_string(13));
9351
        assert_eq!("-1.24", decimal_arr.value_as_string(14));
9352
        assert_eq!("0.00", decimal_arr.value_as_string(15));
9353
        assert_eq!("-123.00", decimal_arr.value_as_string(16));
9354
        assert_eq!("-123.23", decimal_arr.value_as_string(17));
9355
        assert_eq!("-0.12", decimal_arr.value_as_string(18));
9356
        assert_eq!("1.23", decimal_arr.value_as_string(19));
9357
        assert_eq!("1.24", decimal_arr.value_as_string(20));
9358
        assert_eq!("0.00", decimal_arr.value_as_string(21));
9359
        assert_eq!("123.00", decimal_arr.value_as_string(22));
9360
        assert_eq!("123.23", decimal_arr.value_as_string(23));
9361
        assert_eq!("0.12", decimal_arr.value_as_string(24));
9362
        assert!(decimal_arr.is_null(25));
9363
        assert!(decimal_arr.is_null(26));
9364
        assert!(decimal_arr.is_null(27));
9365
        assert_eq!("0.00", decimal_arr.value_as_string(28));
9366
        assert_eq!("0.00", decimal_arr.value_as_string(29));
9367
        assert_eq!("12345.00", decimal_arr.value_as_string(30));
9368
        assert_eq!(decimal_arr.len(), 31);
9369
9370
        // Decimal256
9371
        let output_type = DataType::Decimal256(76, 3);
9372
        assert!(can_cast_types(array.data_type(), &output_type));
9373
9374
        let casted_array = cast(&array, &output_type).unwrap();
9375
        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9376
9377
        assert_eq!("123.450", decimal_arr.value_as_string(0));
9378
        assert_eq!("1.235", decimal_arr.value_as_string(1));
9379
        assert_eq!("0.123", decimal_arr.value_as_string(2));
9380
        assert_eq!("0.127", decimal_arr.value_as_string(3));
9381
        assert_eq!("1.263", decimal_arr.value_as_string(4));
9382
        assert_eq!("12345.000", decimal_arr.value_as_string(5));
9383
        assert_eq!("12345.000", decimal_arr.value_as_string(6));
9384
        assert_eq!("0.123", decimal_arr.value_as_string(7));
9385
        assert_eq!("12.234", decimal_arr.value_as_string(8));
9386
        assert!(decimal_arr.is_null(9));
9387
        assert_eq!("0.000", decimal_arr.value_as_string(10));
9388
        assert_eq!("0.000", decimal_arr.value_as_string(11));
9389
        assert!(decimal_arr.is_null(12));
9390
        assert_eq!("-1.235", decimal_arr.value_as_string(13));
9391
        assert_eq!("-1.236", decimal_arr.value_as_string(14));
9392
        assert_eq!("0.000", decimal_arr.value_as_string(15));
9393
        assert_eq!("-123.000", decimal_arr.value_as_string(16));
9394
        assert_eq!("-123.234", decimal_arr.value_as_string(17));
9395
        assert_eq!("-0.123", decimal_arr.value_as_string(18));
9396
        assert_eq!("1.235", decimal_arr.value_as_string(19));
9397
        assert_eq!("1.236", decimal_arr.value_as_string(20));
9398
        assert_eq!("0.000", decimal_arr.value_as_string(21));
9399
        assert_eq!("123.000", decimal_arr.value_as_string(22));
9400
        assert_eq!("123.234", decimal_arr.value_as_string(23));
9401
        assert_eq!("0.123", decimal_arr.value_as_string(24));
9402
        assert!(decimal_arr.is_null(25));
9403
        assert!(decimal_arr.is_null(26));
9404
        assert!(decimal_arr.is_null(27));
9405
        assert_eq!("0.000", decimal_arr.value_as_string(28));
9406
        assert_eq!("0.000", decimal_arr.value_as_string(29));
9407
        assert_eq!("12345.000", decimal_arr.value_as_string(30));
9408
        assert_eq!(decimal_arr.len(), 31);
9409
    }
9410
9411
    #[test]
9412
    fn test_cast_utf8_to_decimal() {
9413
        let str_array = StringArray::from(vec![
9414
            Some("123.45"),
9415
            Some("1.2345"),
9416
            Some("0.12345"),
9417
            Some("0.1267"),
9418
            Some("1.263"),
9419
            Some("12345.0"),
9420
            Some("12345"),
9421
            Some("000.123"),
9422
            Some("12.234000"),
9423
            None,
9424
            Some(""),
9425
            Some(" "),
9426
            None,
9427
            Some("-1.23499999"),
9428
            Some("-1.23599999"),
9429
            Some("-0.00001"),
9430
            Some("-123"),
9431
            Some("-123.234000"),
9432
            Some("-000.123"),
9433
            Some("+1.23499999"),
9434
            Some("+1.23599999"),
9435
            Some("+0.00001"),
9436
            Some("+123"),
9437
            Some("+123.234000"),
9438
            Some("+000.123"),
9439
            Some("1.-23499999"),
9440
            Some("-1.-23499999"),
9441
            Some("--1.23499999"),
9442
            Some("0"),
9443
            Some("000.000"),
9444
            Some("0000000000000000012345.000"),
9445
        ]);
9446
        let array = Arc::new(str_array) as ArrayRef;
9447
9448
        test_cast_string_to_decimal(array);
9449
9450
        let test_cases = [
9451
            (None, None),
9452
            // (Some(""), None),
9453
            // (Some("   "), None),
9454
            (Some("0"), Some("0")),
9455
            (Some("000.000"), Some("0")),
9456
            (Some("12345"), Some("12345")),
9457
            (Some("000000000000000000000000000012345"), Some("12345")),
9458
            (Some("-123"), Some("-123")),
9459
            (Some("+123"), Some("123")),
9460
        ];
9461
        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9462
        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9463
9464
        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
9465
        test_cast_string_to_decimal_scale_zero(array, &expected);
9466
    }
9467
9468
    #[test]
9469
    fn test_cast_large_utf8_to_decimal() {
9470
        let str_array = LargeStringArray::from(vec![
9471
            Some("123.45"),
9472
            Some("1.2345"),
9473
            Some("0.12345"),
9474
            Some("0.1267"),
9475
            Some("1.263"),
9476
            Some("12345.0"),
9477
            Some("12345"),
9478
            Some("000.123"),
9479
            Some("12.234000"),
9480
            None,
9481
            Some(""),
9482
            Some(" "),
9483
            None,
9484
            Some("-1.23499999"),
9485
            Some("-1.23599999"),
9486
            Some("-0.00001"),
9487
            Some("-123"),
9488
            Some("-123.234000"),
9489
            Some("-000.123"),
9490
            Some("+1.23499999"),
9491
            Some("+1.23599999"),
9492
            Some("+0.00001"),
9493
            Some("+123"),
9494
            Some("+123.234000"),
9495
            Some("+000.123"),
9496
            Some("1.-23499999"),
9497
            Some("-1.-23499999"),
9498
            Some("--1.23499999"),
9499
            Some("0"),
9500
            Some("000.000"),
9501
            Some("0000000000000000012345.000"),
9502
        ]);
9503
        let array = Arc::new(str_array) as ArrayRef;
9504
9505
        test_cast_string_to_decimal(array);
9506
9507
        let test_cases = [
9508
            (None, None),
9509
            (Some(""), None),
9510
            (Some("   "), None),
9511
            (Some("0"), Some("0")),
9512
            (Some("000.000"), Some("0")),
9513
            (Some("12345"), Some("12345")),
9514
            (Some("000000000000000000000000000012345"), Some("12345")),
9515
            (Some("-123"), Some("-123")),
9516
            (Some("+123"), Some("123")),
9517
        ];
9518
        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9519
        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9520
9521
        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
9522
        test_cast_string_to_decimal_scale_zero(array, &expected);
9523
    }
9524
9525
    fn test_cast_string_to_decimal_scale_zero(
9526
        array: ArrayRef,
9527
        expected_as_string: &[Option<&str>],
9528
    ) {
9529
        // Decimal128
9530
        let output_type = DataType::Decimal128(38, 0);
9531
        assert!(can_cast_types(array.data_type(), &output_type));
9532
        let casted_array = cast(&array, &output_type).unwrap();
9533
        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9534
        assert_decimal_array_contents(decimal_arr, expected_as_string);
9535
9536
        // Decimal256
9537
        let output_type = DataType::Decimal256(76, 0);
9538
        assert!(can_cast_types(array.data_type(), &output_type));
9539
        let casted_array = cast(&array, &output_type).unwrap();
9540
        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9541
        assert_decimal_array_contents(decimal_arr, expected_as_string);
9542
    }
9543
9544
    fn assert_decimal_array_contents<T>(
9545
        array: &PrimitiveArray<T>,
9546
        expected_as_string: &[Option<&str>],
9547
    ) where
9548
        T: DecimalType + ArrowPrimitiveType,
9549
    {
9550
        assert_eq!(array.len(), expected_as_string.len());
9551
        for (i, expected) in expected_as_string.iter().enumerate() {
9552
            let actual = if array.is_null(i) {
9553
                None
9554
            } else {
9555
                Some(array.value_as_string(i))
9556
            };
9557
            let actual = actual.as_ref().map(|s| s.as_ref());
9558
            assert_eq!(*expected, actual, "Expected at position {i}");
9559
        }
9560
    }
9561
9562
    #[test]
9563
    fn test_cast_invalid_utf8_to_decimal() {
9564
        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9565
        let array = Arc::new(str_array) as ArrayRef;
9566
9567
        // Safe cast
9568
        let output_type = DataType::Decimal128(38, 2);
9569
        let casted_array = cast(&array, &output_type).unwrap();
9570
        assert!(casted_array.is_null(0));
9571
        assert!(casted_array.is_null(1));
9572
9573
        let output_type = DataType::Decimal256(76, 2);
9574
        let casted_array = cast(&array, &output_type).unwrap();
9575
        assert!(casted_array.is_null(0));
9576
        assert!(casted_array.is_null(1));
9577
9578
        // Non-safe cast
9579
        let output_type = DataType::Decimal128(38, 2);
9580
        let str_array = StringArray::from(vec!["4.4.5"]);
9581
        let array = Arc::new(str_array) as ArrayRef;
9582
        let option = CastOptions {
9583
            safe: false,
9584
            format_options: FormatOptions::default(),
9585
        };
9586
        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9587
        assert!(casted_err
9588
            .to_string()
9589
            .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type"));
9590
9591
        let str_array = StringArray::from(vec![". 0.123"]);
9592
        let array = Arc::new(str_array) as ArrayRef;
9593
        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9594
        assert!(casted_err
9595
            .to_string()
9596
            .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type"));
9597
    }
9598
9599
    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
9600
        let output_type = DataType::Decimal128(38, 2);
9601
        let casted_array = cast(&overflow_array, &output_type).unwrap();
9602
        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9603
9604
        assert!(decimal_arr.is_null(0));
9605
        assert!(decimal_arr.is_null(1));
9606
        assert!(decimal_arr.is_null(2));
9607
        assert_eq!(
9608
            "999999999999999999999999999999999999.99",
9609
            decimal_arr.value_as_string(3)
9610
        );
9611
        assert_eq!(
9612
            "100000000000000000000000000000000000.00",
9613
            decimal_arr.value_as_string(4)
9614
        );
9615
    }
9616
9617
    #[test]
9618
    fn test_cast_string_to_decimal128_precision_overflow() {
9619
        let array = StringArray::from(vec!["1000".to_string()]);
9620
        let array = Arc::new(array) as ArrayRef;
9621
        let casted_array = cast_with_options(
9622
            &array,
9623
            &DataType::Decimal128(10, 8),
9624
            &CastOptions {
9625
                safe: true,
9626
                format_options: FormatOptions::default(),
9627
            },
9628
        );
9629
        assert!(casted_array.is_ok());
9630
        assert!(casted_array.unwrap().is_null(0));
9631
9632
        let err = cast_with_options(
9633
            &array,
9634
            &DataType::Decimal128(10, 8),
9635
            &CastOptions {
9636
                safe: false,
9637
                format_options: FormatOptions::default(),
9638
            },
9639
        );
9640
        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());
9641
    }
9642
9643
    #[test]
9644
    fn test_cast_utf8_to_decimal128_overflow() {
9645
        let overflow_str_array = StringArray::from(vec![
9646
            i128::MAX.to_string(),
9647
            i128::MIN.to_string(),
9648
            "99999999999999999999999999999999999999".to_string(),
9649
            "999999999999999999999999999999999999.99".to_string(),
9650
            "99999999999999999999999999999999999.999".to_string(),
9651
        ]);
9652
        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9653
9654
        test_cast_string_to_decimal128_overflow(overflow_array);
9655
    }
9656
9657
    #[test]
9658
    fn test_cast_large_utf8_to_decimal128_overflow() {
9659
        let overflow_str_array = LargeStringArray::from(vec![
9660
            i128::MAX.to_string(),
9661
            i128::MIN.to_string(),
9662
            "99999999999999999999999999999999999999".to_string(),
9663
            "999999999999999999999999999999999999.99".to_string(),
9664
            "99999999999999999999999999999999999.999".to_string(),
9665
        ]);
9666
        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9667
9668
        test_cast_string_to_decimal128_overflow(overflow_array);
9669
    }
9670
9671
    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
9672
        let output_type = DataType::Decimal256(76, 2);
9673
        let casted_array = cast(&overflow_array, &output_type).unwrap();
9674
        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9675
9676
        assert_eq!(
9677
            "170141183460469231731687303715884105727.00",
9678
            decimal_arr.value_as_string(0)
9679
        );
9680
        assert_eq!(
9681
            "-170141183460469231731687303715884105728.00",
9682
            decimal_arr.value_as_string(1)
9683
        );
9684
        assert_eq!(
9685
            "99999999999999999999999999999999999999.00",
9686
            decimal_arr.value_as_string(2)
9687
        );
9688
        assert_eq!(
9689
            "999999999999999999999999999999999999.99",
9690
            decimal_arr.value_as_string(3)
9691
        );
9692
        assert_eq!(
9693
            "100000000000000000000000000000000000.00",
9694
            decimal_arr.value_as_string(4)
9695
        );
9696
        assert!(decimal_arr.is_null(5));
9697
        assert!(decimal_arr.is_null(6));
9698
    }
9699
9700
    #[test]
9701
    fn test_cast_string_to_decimal256_precision_overflow() {
9702
        let array = StringArray::from(vec!["1000".to_string()]);
9703
        let array = Arc::new(array) as ArrayRef;
9704
        let casted_array = cast_with_options(
9705
            &array,
9706
            &DataType::Decimal256(10, 8),
9707
            &CastOptions {
9708
                safe: true,
9709
                format_options: FormatOptions::default(),
9710
            },
9711
        );
9712
        assert!(casted_array.is_ok());
9713
        assert!(casted_array.unwrap().is_null(0));
9714
9715
        let err = cast_with_options(
9716
            &array,
9717
            &DataType::Decimal256(10, 8),
9718
            &CastOptions {
9719
                safe: false,
9720
                format_options: FormatOptions::default(),
9721
            },
9722
        );
9723
        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());
9724
    }
9725
9726
    #[test]
9727
    fn test_cast_utf8_to_decimal256_overflow() {
9728
        let overflow_str_array = StringArray::from(vec![
9729
            i128::MAX.to_string(),
9730
            i128::MIN.to_string(),
9731
            "99999999999999999999999999999999999999".to_string(),
9732
            "999999999999999999999999999999999999.99".to_string(),
9733
            "99999999999999999999999999999999999.999".to_string(),
9734
            i256::MAX.to_string(),
9735
            i256::MIN.to_string(),
9736
        ]);
9737
        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9738
9739
        test_cast_string_to_decimal256_overflow(overflow_array);
9740
    }
9741
9742
    #[test]
9743
    fn test_cast_large_utf8_to_decimal256_overflow() {
9744
        let overflow_str_array = LargeStringArray::from(vec![
9745
            i128::MAX.to_string(),
9746
            i128::MIN.to_string(),
9747
            "99999999999999999999999999999999999999".to_string(),
9748
            "999999999999999999999999999999999999.99".to_string(),
9749
            "99999999999999999999999999999999999.999".to_string(),
9750
            i256::MAX.to_string(),
9751
            i256::MIN.to_string(),
9752
        ]);
9753
        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9754
9755
        test_cast_string_to_decimal256_overflow(overflow_array);
9756
    }
9757
9758
    #[test]
9759
    fn test_cast_outside_supported_range_for_nanoseconds() {
9760
        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";
9761
9762
        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
9763
9764
        let cast_options = CastOptions {
9765
            safe: false,
9766
            format_options: FormatOptions::default(),
9767
        };
9768
9769
        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
9770
            &array,
9771
            &None::<Arc<str>>,
9772
            &cast_options,
9773
        );
9774
9775
        let err = result.unwrap_err();
9776
        assert_eq!(
9777
            err.to_string(),
9778
            format!(
9779
                "Cast error: Overflow converting {} to Nanosecond. {}",
9780
                array.value(0),
9781
                EXPECTED_ERROR_MESSAGE
9782
            )
9783
        );
9784
    }
9785
9786
    #[test]
9787
    fn test_cast_date32_to_timestamp() {
9788
        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9789
        let array = Arc::new(a) as ArrayRef;
9790
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
9791
        let c = b.as_primitive::<TimestampSecondType>();
9792
        assert_eq!(1609459200, c.value(0));
9793
        assert_eq!(1640995200, c.value(1));
9794
        assert!(c.is_null(2));
9795
    }
9796
9797
    #[test]
9798
    fn test_cast_date32_to_timestamp_ms() {
9799
        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9800
        let array = Arc::new(a) as ArrayRef;
9801
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
9802
        let c = b
9803
            .as_any()
9804
            .downcast_ref::<TimestampMillisecondArray>()
9805
            .unwrap();
9806
        assert_eq!(1609459200000, c.value(0));
9807
        assert_eq!(1640995200000, c.value(1));
9808
        assert!(c.is_null(2));
9809
    }
9810
9811
    #[test]
9812
    fn test_cast_date32_to_timestamp_us() {
9813
        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9814
        let array = Arc::new(a) as ArrayRef;
9815
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
9816
        let c = b
9817
            .as_any()
9818
            .downcast_ref::<TimestampMicrosecondArray>()
9819
            .unwrap();
9820
        assert_eq!(1609459200000000, c.value(0));
9821
        assert_eq!(1640995200000000, c.value(1));
9822
        assert!(c.is_null(2));
9823
    }
9824
9825
    #[test]
9826
    fn test_cast_date32_to_timestamp_ns() {
9827
        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9828
        let array = Arc::new(a) as ArrayRef;
9829
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9830
        let c = b
9831
            .as_any()
9832
            .downcast_ref::<TimestampNanosecondArray>()
9833
            .unwrap();
9834
        assert_eq!(1609459200000000000, c.value(0));
9835
        assert_eq!(1640995200000000000, c.value(1));
9836
        assert!(c.is_null(2));
9837
    }
9838
9839
    #[test]
9840
    fn test_timezone_cast() {
9841
        let a = StringArray::from(vec![
9842
            "2000-01-01T12:00:00", // date + time valid
9843
            "2020-12-15T12:34:56", // date + time valid
9844
        ]);
9845
        let array = Arc::new(a) as ArrayRef;
9846
        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9847
        let v = b.as_primitive::<TimestampNanosecondType>();
9848
9849
        assert_eq!(v.value(0), 946728000000000000);
9850
        assert_eq!(v.value(1), 1608035696000000000);
9851
9852
        let b = cast(
9853
            &b,
9854
            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9855
        )
9856
        .unwrap();
9857
        let v = b.as_primitive::<TimestampNanosecondType>();
9858
9859
        assert_eq!(v.value(0), 946728000000000000);
9860
        assert_eq!(v.value(1), 1608035696000000000);
9861
9862
        let b = cast(
9863
            &b,
9864
            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
9865
        )
9866
        .unwrap();
9867
        let v = b.as_primitive::<TimestampMillisecondType>();
9868
9869
        assert_eq!(v.value(0), 946728000000);
9870
        assert_eq!(v.value(1), 1608035696000);
9871
    }
9872
9873
    #[test]
9874
    fn test_cast_utf8_to_timestamp() {
9875
        fn test_tz(tz: Arc<str>) {
9876
            let valid = StringArray::from(vec![
9877
                "2023-01-01 04:05:06.789000-08:00",
9878
                "2023-01-01 04:05:06.789000-07:00",
9879
                "2023-01-01 04:05:06.789 -0800",
9880
                "2023-01-01 04:05:06.789 -08:00",
9881
                "2023-01-01 040506 +0730",
9882
                "2023-01-01 040506 +07:30",
9883
                "2023-01-01 04:05:06.789",
9884
                "2023-01-01 04:05:06",
9885
                "2023-01-01",
9886
            ]);
9887
9888
            let array = Arc::new(valid) as ArrayRef;
9889
            let b = cast_with_options(
9890
                &array,
9891
                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
9892
                &CastOptions {
9893
                    safe: false,
9894
                    format_options: FormatOptions::default(),
9895
                },
9896
            )
9897
            .unwrap();
9898
9899
            let tz = tz.as_ref().parse().unwrap();
9900
9901
            let as_tz =
9902
                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
9903
9904
            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
9905
            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
9906
9907
            let values = b.as_primitive::<TimestampNanosecondType>().values();
9908
            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
9909
            let local_results: Vec<_> = values.iter().map(as_local).collect();
9910
9911
            // Absolute timestamps should be parsed preserving the same UTC instant
9912
            assert_eq!(
9913
                &utc_results[..6],
9914
                &[
9915
                    "2023-01-01 12:05:06.789".to_string(),
9916
                    "2023-01-01 11:05:06.789".to_string(),
9917
                    "2023-01-01 12:05:06.789".to_string(),
9918
                    "2023-01-01 12:05:06.789".to_string(),
9919
                    "2022-12-31 20:35:06".to_string(),
9920
                    "2022-12-31 20:35:06".to_string(),
9921
                ]
9922
            );
9923
            // Non-absolute timestamps should be parsed preserving the same local instant
9924
            assert_eq!(
9925
                &local_results[6..],
9926
                &[
9927
                    "2023-01-01 04:05:06.789".to_string(),
9928
                    "2023-01-01 04:05:06".to_string(),
9929
                    "2023-01-01 00:00:00".to_string()
9930
                ]
9931
            )
9932
        }
9933
9934
        test_tz("+00:00".into());
9935
        test_tz("+02:00".into());
9936
    }
9937
9938
    #[test]
9939
    fn test_cast_invalid_utf8() {
9940
        let v1: &[u8] = b"\xFF invalid";
9941
        let v2: &[u8] = b"\x00 Foo";
9942
        let s = BinaryArray::from(vec![v1, v2]);
9943
        let options = CastOptions {
9944
            safe: true,
9945
            format_options: FormatOptions::default(),
9946
        };
9947
        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
9948
        let a = array.as_string::<i32>();
9949
        a.to_data().validate_full().unwrap();
9950
9951
        assert_eq!(a.null_count(), 1);
9952
        assert_eq!(a.len(), 2);
9953
        assert!(a.is_null(0));
9954
        assert_eq!(a.value(0), "");
9955
        assert_eq!(a.value(1), "\x00 Foo");
9956
    }
9957
9958
    #[test]
9959
    fn test_cast_utf8_to_timestamptz() {
9960
        let valid = StringArray::from(vec!["2023-01-01"]);
9961
9962
        let array = Arc::new(valid) as ArrayRef;
9963
        let b = cast(
9964
            &array,
9965
            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9966
        )
9967
        .unwrap();
9968
9969
        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
9970
9971
        assert_eq!(b.data_type(), &expect);
9972
        let c = b
9973
            .as_any()
9974
            .downcast_ref::<TimestampNanosecondArray>()
9975
            .unwrap();
9976
        assert_eq!(1672531200000000000, c.value(0));
9977
    }
9978
9979
    #[test]
9980
    fn test_cast_decimal_to_string() {
9981
        assert!(can_cast_types(
9982
            &DataType::Decimal32(9, 4),
9983
            &DataType::Utf8View
9984
        ));
9985
        assert!(can_cast_types(
9986
            &DataType::Decimal64(16, 4),
9987
            &DataType::Utf8View
9988
        ));
9989
        assert!(can_cast_types(
9990
            &DataType::Decimal128(10, 4),
9991
            &DataType::Utf8View
9992
        ));
9993
        assert!(can_cast_types(
9994
            &DataType::Decimal256(38, 10),
9995
            &DataType::Utf8View
9996
        ));
9997
9998
        macro_rules! assert_decimal_values {
9999
            ($array:expr) => {
10000
                let c = $array;
10001
                assert_eq!("1123.454", c.value(0));
10002
                assert_eq!("2123.456", c.value(1));
10003
                assert_eq!("-3123.453", c.value(2));
10004
                assert_eq!("-3123.456", c.value(3));
10005
                assert_eq!("0.000", c.value(4));
10006
                assert_eq!("0.123", c.value(5));
10007
                assert_eq!("1234.567", c.value(6));
10008
                assert_eq!("-1234.567", c.value(7));
10009
                assert!(c.is_null(8));
10010
            };
10011
        }
10012
10013
        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
10014
            output_type: DataType,
10015
            array: PrimitiveArray<IN>,
10016
        ) {
10017
            let b = cast(&array, &output_type).unwrap();
10018
10019
            assert_eq!(b.data_type(), &output_type);
10020
            match b.data_type() {
10021
                DataType::Utf8View => {
10022
                    let c = b.as_string_view();
10023
                    assert_decimal_values!(c);
10024
                }
10025
                DataType::Utf8 | DataType::LargeUtf8 => {
10026
                    let c = b.as_string::<OffsetSize>();
10027
                    assert_decimal_values!(c);
10028
                }
10029
                _ => (),
10030
            }
10031
        }
10032
10033
        let array32: Vec<Option<i32>> = vec![
10034
            Some(1123454),
10035
            Some(2123456),
10036
            Some(-3123453),
10037
            Some(-3123456),
10038
            Some(0),
10039
            Some(123),
10040
            Some(123456789),
10041
            Some(-123456789),
10042
            None,
10043
        ];
10044
        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
10045
        let array128: Vec<Option<i128>> =
10046
            array64.iter().map(|num| num.map(|x| x as i128)).collect();
10047
        let array256: Vec<Option<i256>> = array128
10048
            .iter()
10049
            .map(|num| num.map(i256::from_i128))
10050
            .collect();
10051
10052
        test_decimal_to_string::<Decimal32Type, i32>(
10053
            DataType::Utf8View,
10054
            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10055
        );
10056
        test_decimal_to_string::<Decimal32Type, i32>(
10057
            DataType::Utf8,
10058
            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10059
        );
10060
        test_decimal_to_string::<Decimal32Type, i64>(
10061
            DataType::LargeUtf8,
10062
            create_decimal32_array(array32, 7, 3).unwrap(),
10063
        );
10064
10065
        test_decimal_to_string::<Decimal64Type, i32>(
10066
            DataType::Utf8View,
10067
            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10068
        );
10069
        test_decimal_to_string::<Decimal64Type, i32>(
10070
            DataType::Utf8,
10071
            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10072
        );
10073
        test_decimal_to_string::<Decimal64Type, i64>(
10074
            DataType::LargeUtf8,
10075
            create_decimal64_array(array64, 7, 3).unwrap(),
10076
        );
10077
10078
        test_decimal_to_string::<Decimal128Type, i32>(
10079
            DataType::Utf8View,
10080
            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10081
        );
10082
        test_decimal_to_string::<Decimal128Type, i32>(
10083
            DataType::Utf8,
10084
            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10085
        );
10086
        test_decimal_to_string::<Decimal128Type, i64>(
10087
            DataType::LargeUtf8,
10088
            create_decimal128_array(array128, 7, 3).unwrap(),
10089
        );
10090
10091
        test_decimal_to_string::<Decimal256Type, i32>(
10092
            DataType::Utf8View,
10093
            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10094
        );
10095
        test_decimal_to_string::<Decimal256Type, i32>(
10096
            DataType::Utf8,
10097
            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10098
        );
10099
        test_decimal_to_string::<Decimal256Type, i64>(
10100
            DataType::LargeUtf8,
10101
            create_decimal256_array(array256, 7, 3).unwrap(),
10102
        );
10103
    }
10104
10105
    #[test]
10106
    fn test_cast_numeric_to_decimal128_precision_overflow() {
10107
        let array = Int64Array::from(vec![1234567]);
10108
        let array = Arc::new(array) as ArrayRef;
10109
        let casted_array = cast_with_options(
10110
            &array,
10111
            &DataType::Decimal128(7, 3),
10112
            &CastOptions {
10113
                safe: true,
10114
                format_options: FormatOptions::default(),
10115
            },
10116
        );
10117
        assert!(casted_array.is_ok());
10118
        assert!(casted_array.unwrap().is_null(0));
10119
10120
        let err = cast_with_options(
10121
            &array,
10122
            &DataType::Decimal128(7, 3),
10123
            &CastOptions {
10124
                safe: false,
10125
                format_options: FormatOptions::default(),
10126
            },
10127
        );
10128
        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());
10129
    }
10130
10131
    #[test]
10132
    fn test_cast_numeric_to_decimal256_precision_overflow() {
10133
        let array = Int64Array::from(vec![1234567]);
10134
        let array = Arc::new(array) as ArrayRef;
10135
        let casted_array = cast_with_options(
10136
            &array,
10137
            &DataType::Decimal256(7, 3),
10138
            &CastOptions {
10139
                safe: true,
10140
                format_options: FormatOptions::default(),
10141
            },
10142
        );
10143
        assert!(casted_array.is_ok());
10144
        assert!(casted_array.unwrap().is_null(0));
10145
10146
        let err = cast_with_options(
10147
            &array,
10148
            &DataType::Decimal256(7, 3),
10149
            &CastOptions {
10150
                safe: false,
10151
                format_options: FormatOptions::default(),
10152
            },
10153
        );
10154
        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());
10155
    }
10156
10157
    /// helper function to test casting from duration to interval
10158
    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
10159
        array: Vec<i64>,
10160
        cast_options: &CastOptions,
10161
    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10162
        let array = PrimitiveArray::<T>::new(array.into(), None);
10163
        let array = Arc::new(array) as ArrayRef;
10164
        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
10165
        let out = cast_with_options(&array, &interval, cast_options)?;
10166
        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
10167
        Ok(out)
10168
    }
10169
10170
    #[test]
10171
    fn test_cast_from_duration_to_interval() {
10172
        // from duration second to interval month day nano
10173
        let array = vec![1234567];
10174
        let casted_array =
10175
            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
10176
                .unwrap();
10177
        assert_eq!(
10178
            casted_array.data_type(),
10179
            &DataType::Interval(IntervalUnit::MonthDayNano)
10180
        );
10181
        assert_eq!(
10182
            casted_array.value(0),
10183
            IntervalMonthDayNano::new(0, 0, 1234567000000000)
10184
        );
10185
10186
        let array = vec![i64::MAX];
10187
        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10188
            array.clone(),
10189
            &CastOptions::default(),
10190
        )
10191
        .unwrap();
10192
        assert!(!casted_array.is_valid(0));
10193
10194
        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10195
            array,
10196
            &CastOptions {
10197
                safe: false,
10198
                format_options: FormatOptions::default(),
10199
            },
10200
        );
10201
        assert!(casted_array.is_err());
10202
10203
        // from duration millisecond to interval month day nano
10204
        let array = vec![1234567];
10205
        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10206
            array,
10207
            &CastOptions::default(),
10208
        )
10209
        .unwrap();
10210
        assert_eq!(
10211
            casted_array.data_type(),
10212
            &DataType::Interval(IntervalUnit::MonthDayNano)
10213
        );
10214
        assert_eq!(
10215
            casted_array.value(0),
10216
            IntervalMonthDayNano::new(0, 0, 1234567000000)
10217
        );
10218
10219
        let array = vec![i64::MAX];
10220
        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10221
            array.clone(),
10222
            &CastOptions::default(),
10223
        )
10224
        .unwrap();
10225
        assert!(!casted_array.is_valid(0));
10226
10227
        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10228
            array,
10229
            &CastOptions {
10230
                safe: false,
10231
                format_options: FormatOptions::default(),
10232
            },
10233
        );
10234
        assert!(casted_array.is_err());
10235
10236
        // from duration microsecond to interval month day nano
10237
        let array = vec![1234567];
10238
        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10239
            array,
10240
            &CastOptions::default(),
10241
        )
10242
        .unwrap();
10243
        assert_eq!(
10244
            casted_array.data_type(),
10245
            &DataType::Interval(IntervalUnit::MonthDayNano)
10246
        );
10247
        assert_eq!(
10248
            casted_array.value(0),
10249
            IntervalMonthDayNano::new(0, 0, 1234567000)
10250
        );
10251
10252
        let array = vec![i64::MAX];
10253
        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10254
            array.clone(),
10255
            &CastOptions::default(),
10256
        )
10257
        .unwrap();
10258
        assert!(!casted_array.is_valid(0));
10259
10260
        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10261
            array,
10262
            &CastOptions {
10263
                safe: false,
10264
                format_options: FormatOptions::default(),
10265
            },
10266
        );
10267
        assert!(casted_array.is_err());
10268
10269
        // from duration nanosecond to interval month day nano
10270
        let array = vec![1234567];
10271
        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10272
            array,
10273
            &CastOptions::default(),
10274
        )
10275
        .unwrap();
10276
        assert_eq!(
10277
            casted_array.data_type(),
10278
            &DataType::Interval(IntervalUnit::MonthDayNano)
10279
        );
10280
        assert_eq!(
10281
            casted_array.value(0),
10282
            IntervalMonthDayNano::new(0, 0, 1234567)
10283
        );
10284
10285
        let array = vec![i64::MAX];
10286
        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10287
            array,
10288
            &CastOptions {
10289
                safe: false,
10290
                format_options: FormatOptions::default(),
10291
            },
10292
        )
10293
        .unwrap();
10294
        assert_eq!(
10295
            casted_array.value(0),
10296
            IntervalMonthDayNano::new(0, 0, i64::MAX)
10297
        );
10298
    }
10299
10300
    /// helper function to test casting from interval to duration
10301
    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
10302
        array: &IntervalMonthDayNanoArray,
10303
        cast_options: &CastOptions,
10304
    ) -> Result<PrimitiveArray<T>, ArrowError> {
10305
        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
10306
        casted_array
10307
            .as_any()
10308
            .downcast_ref::<PrimitiveArray<T>>()
10309
            .ok_or_else(|| {
10310
                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
10311
            })
10312
            .cloned()
10313
    }
10314
10315
    #[test]
10316
    fn test_cast_from_interval_to_duration() {
10317
        let nullable = CastOptions::default();
10318
        let fallible = CastOptions {
10319
            safe: false,
10320
            format_options: FormatOptions::default(),
10321
        };
10322
        let v = IntervalMonthDayNano::new(0, 0, 1234567);
10323
10324
        // from interval month day nano to duration second
10325
        let array = vec![v].into();
10326
        let casted_array: DurationSecondArray =
10327
            cast_from_interval_to_duration(&array, &nullable).unwrap();
10328
        assert_eq!(casted_array.value(0), 0);
10329
10330
        let array = vec![IntervalMonthDayNano::MAX].into();
10331
        let casted_array: DurationSecondArray =
10332
            cast_from_interval_to_duration(&array, &nullable).unwrap();
10333
        assert!(!casted_array.is_valid(0));
10334
10335
        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
10336
        assert!(res.is_err());
10337
10338
        // from interval month day nano to duration millisecond
10339
        let array = vec![v].into();
10340
        let casted_array: DurationMillisecondArray =
10341
            cast_from_interval_to_duration(&array, &nullable).unwrap();
10342
        assert_eq!(casted_array.value(0), 1);
10343
10344
        let array = vec![IntervalMonthDayNano::MAX].into();
10345
        let casted_array: DurationMillisecondArray =
10346
            cast_from_interval_to_duration(&array, &nullable).unwrap();
10347
        assert!(!casted_array.is_valid(0));
10348
10349
        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
10350
        assert!(res.is_err());
10351
10352
        // from interval month day nano to duration microsecond
10353
        let array = vec![v].into();
10354
        let casted_array: DurationMicrosecondArray =
10355
            cast_from_interval_to_duration(&array, &nullable).unwrap();
10356
        assert_eq!(casted_array.value(0), 1234);
10357
10358
        let array = vec![IntervalMonthDayNano::MAX].into();
10359
        let casted_array =
10360
            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
10361
        assert!(!casted_array.is_valid(0));
10362
10363
        let casted_array =
10364
            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
10365
        assert!(casted_array.is_err());
10366
10367
        // from interval month day nano to duration nanosecond
10368
        let array = vec![v].into();
10369
        let casted_array: DurationNanosecondArray =
10370
            cast_from_interval_to_duration(&array, &nullable).unwrap();
10371
        assert_eq!(casted_array.value(0), 1234567);
10372
10373
        let array = vec![IntervalMonthDayNano::MAX].into();
10374
        let casted_array: DurationNanosecondArray =
10375
            cast_from_interval_to_duration(&array, &nullable).unwrap();
10376
        assert!(!casted_array.is_valid(0));
10377
10378
        let casted_array =
10379
            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
10380
        assert!(casted_array.is_err());
10381
10382
        let array = vec![
10383
            IntervalMonthDayNanoType::make_value(0, 1, 0),
10384
            IntervalMonthDayNanoType::make_value(-1, 0, 0),
10385
            IntervalMonthDayNanoType::make_value(1, 1, 0),
10386
            IntervalMonthDayNanoType::make_value(1, 0, 1),
10387
            IntervalMonthDayNanoType::make_value(0, 0, -1),
10388
        ]
10389
        .into();
10390
        let casted_array =
10391
            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
10392
        assert!(!casted_array.is_valid(0));
10393
        assert!(!casted_array.is_valid(1));
10394
        assert!(!casted_array.is_valid(2));
10395
        assert!(!casted_array.is_valid(3));
10396
        assert!(casted_array.is_valid(4));
10397
        assert_eq!(casted_array.value(4), -1);
10398
    }
10399
10400
    /// helper function to test casting from interval year month to interval month day nano
10401
    fn cast_from_interval_year_month_to_interval_month_day_nano(
10402
        array: Vec<i32>,
10403
        cast_options: &CastOptions,
10404
    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10405
        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
10406
        let array = Arc::new(array) as ArrayRef;
10407
        let casted_array = cast_with_options(
10408
            &array,
10409
            &DataType::Interval(IntervalUnit::MonthDayNano),
10410
            cast_options,
10411
        )?;
10412
        casted_array
10413
            .as_any()
10414
            .downcast_ref::<IntervalMonthDayNanoArray>()
10415
            .ok_or_else(|| {
10416
                ArrowError::ComputeError(
10417
                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
10418
                )
10419
            })
10420
            .cloned()
10421
    }
10422
10423
    #[test]
10424
    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
10425
        // from interval year month to interval month day nano
10426
        let array = vec![1234567];
10427
        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
10428
            array,
10429
            &CastOptions::default(),
10430
        )
10431
        .unwrap();
10432
        assert_eq!(
10433
            casted_array.data_type(),
10434
            &DataType::Interval(IntervalUnit::MonthDayNano)
10435
        );
10436
        assert_eq!(
10437
            casted_array.value(0),
10438
            IntervalMonthDayNano::new(1234567, 0, 0)
10439
        );
10440
    }
10441
10442
    /// helper function to test casting from interval day time to interval month day nano
10443
    fn cast_from_interval_day_time_to_interval_month_day_nano(
10444
        array: Vec<IntervalDayTime>,
10445
        cast_options: &CastOptions,
10446
    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10447
        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
10448
        let array = Arc::new(array) as ArrayRef;
10449
        let casted_array = cast_with_options(
10450
            &array,
10451
            &DataType::Interval(IntervalUnit::MonthDayNano),
10452
            cast_options,
10453
        )?;
10454
        Ok(casted_array
10455
            .as_primitive::<IntervalMonthDayNanoType>()
10456
            .clone())
10457
    }
10458
10459
    #[test]
10460
    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
10461
        // from interval day time to interval month day nano
10462
        let array = vec![IntervalDayTime::new(123, 0)];
10463
        let casted_array =
10464
            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
10465
                .unwrap();
10466
        assert_eq!(
10467
            casted_array.data_type(),
10468
            &DataType::Interval(IntervalUnit::MonthDayNano)
10469
        );
10470
        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
10471
    }
10472
10473
    #[test]
10474
    fn test_cast_below_unixtimestamp() {
10475
        let valid = StringArray::from(vec![
10476
            "1900-01-03 23:59:59",
10477
            "1969-12-31 00:00:01",
10478
            "1989-12-31 00:00:01",
10479
        ]);
10480
10481
        let array = Arc::new(valid) as ArrayRef;
10482
        let casted_array = cast_with_options(
10483
            &array,
10484
            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10485
            &CastOptions {
10486
                safe: false,
10487
                format_options: FormatOptions::default(),
10488
            },
10489
        )
10490
        .unwrap();
10491
10492
        let ts_array = casted_array
10493
            .as_primitive::<TimestampNanosecondType>()
10494
            .values()
10495
            .iter()
10496
            .map(|ts| ts / 1_000_000)
10497
            .collect::<Vec<_>>();
10498
10499
        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
10500
        let casted_array = cast(&array, &DataType::Date32).unwrap();
10501
        let date_array = casted_array.as_primitive::<Date32Type>();
10502
        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
10503
        let string_array = casted_array.as_string::<i32>();
10504
        assert_eq!("1900-01-03", string_array.value(0));
10505
        assert_eq!("1969-12-31", string_array.value(1));
10506
        assert_eq!("1989-12-31", string_array.value(2));
10507
    }
10508
10509
    #[test]
10510
    fn test_nested_list() {
10511
        let mut list = ListBuilder::new(Int32Builder::new());
10512
        list.append_value([Some(1), Some(2), Some(3)]);
10513
        list.append_value([Some(4), None, Some(6)]);
10514
        let list = list.finish();
10515
10516
        let to_field = Field::new("nested", list.data_type().clone(), false);
10517
        let to = DataType::List(Arc::new(to_field));
10518
        let out = cast(&list, &to).unwrap();
10519
        let opts = FormatOptions::default().with_null("null");
10520
        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
10521
10522
        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
10523
        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
10524
    }
10525
10526
    #[test]
10527
    fn test_nested_list_cast() {
10528
        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
10529
        builder.append_value([Some([Some(1), Some(2), None]), None]);
10530
        builder.append_value([None, Some([]), None]);
10531
        builder.append_null();
10532
        builder.append_value([Some([Some(2), Some(3)])]);
10533
        let start = builder.finish();
10534
10535
        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
10536
        builder.append_value([Some([Some(1), Some(2), None]), None]);
10537
        builder.append_value([None, Some([]), None]);
10538
        builder.append_null();
10539
        builder.append_value([Some([Some(2), Some(3)])]);
10540
        let expected = builder.finish();
10541
10542
        let actual = cast(&start, expected.data_type()).unwrap();
10543
        assert_eq!(actual.as_ref(), &expected);
10544
    }
10545
10546
    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
10547
        safe: true,
10548
        format_options: FormatOptions::new(),
10549
    };
10550
10551
    #[test]
10552
    #[allow(clippy::assertions_on_constants)]
10553
    fn test_const_options() {
10554
        assert!(CAST_OPTIONS.safe)
10555
    }
10556
10557
    #[test]
10558
    fn test_list_format_options() {
10559
        let options = CastOptions {
10560
            safe: false,
10561
            format_options: FormatOptions::default().with_null("null"),
10562
        };
10563
        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10564
            Some(vec![Some(0), Some(1), Some(2)]),
10565
            Some(vec![Some(0), None, Some(2)]),
10566
        ]);
10567
        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
10568
        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
10569
        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
10570
    }
10571
    #[test]
10572
    fn test_cast_string_to_timestamp_invalid_tz() {
10573
        // content after Z should be ignored
10574
        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
10575
        let array = StringArray::from(vec![Some(bad_timestamp)]);
10576
10577
        let data_types = [
10578
            DataType::Timestamp(TimeUnit::Second, None),
10579
            DataType::Timestamp(TimeUnit::Millisecond, None),
10580
            DataType::Timestamp(TimeUnit::Microsecond, None),
10581
            DataType::Timestamp(TimeUnit::Nanosecond, None),
10582
        ];
10583
10584
        let cast_options = CastOptions {
10585
            safe: false,
10586
            ..Default::default()
10587
        };
10588
10589
        for dt in data_types {
10590
            assert_eq!(
10591
                cast_with_options(&array, &dt, &cast_options)
10592
                    .unwrap_err()
10593
                    .to_string(),
10594
                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
10595
            );
10596
        }
10597
    }
10598
    #[test]
10599
    fn test_cast_struct_to_struct() {
10600
        let struct_type = DataType::Struct(
10601
            vec![
10602
                Field::new("a", DataType::Boolean, false),
10603
                Field::new("b", DataType::Int32, false),
10604
            ]
10605
            .into(),
10606
        );
10607
        let to_type = DataType::Struct(
10608
            vec![
10609
                Field::new("a", DataType::Utf8, false),
10610
                Field::new("b", DataType::Utf8, false),
10611
            ]
10612
            .into(),
10613
        );
10614
        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10615
        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
10616
        let struct_array = StructArray::from(vec![
10617
            (
10618
                Arc::new(Field::new("b", DataType::Boolean, false)),
10619
                boolean.clone() as ArrayRef,
10620
            ),
10621
            (
10622
                Arc::new(Field::new("c", DataType::Int32, false)),
10623
                int.clone() as ArrayRef,
10624
            ),
10625
        ]);
10626
        let casted_array = cast(&struct_array, &to_type).unwrap();
10627
        let casted_array = casted_array.as_struct();
10628
        assert_eq!(casted_array.data_type(), &to_type);
10629
        let casted_boolean_array = casted_array
10630
            .column(0)
10631
            .as_string::<i32>()
10632
            .into_iter()
10633
            .flatten()
10634
            .collect::<Vec<_>>();
10635
        let casted_int_array = casted_array
10636
            .column(1)
10637
            .as_string::<i32>()
10638
            .into_iter()
10639
            .flatten()
10640
            .collect::<Vec<_>>();
10641
        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
10642
        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
10643
10644
        // test for can't cast
10645
        let to_type = DataType::Struct(
10646
            vec![
10647
                Field::new("a", DataType::Date32, false),
10648
                Field::new("b", DataType::Utf8, false),
10649
            ]
10650
            .into(),
10651
        );
10652
        assert!(!can_cast_types(&struct_type, &to_type));
10653
        let result = cast(&struct_array, &to_type);
10654
        assert_eq!(
10655
            "Cast error: Casting from Boolean to Date32 not supported",
10656
            result.unwrap_err().to_string()
10657
        );
10658
    }
10659
10660
    #[test]
10661
    fn test_cast_struct_to_struct_nullability() {
10662
        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10663
        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
10664
        let struct_array = StructArray::from(vec![
10665
            (
10666
                Arc::new(Field::new("b", DataType::Boolean, false)),
10667
                boolean.clone() as ArrayRef,
10668
            ),
10669
            (
10670
                Arc::new(Field::new("c", DataType::Int32, true)),
10671
                int.clone() as ArrayRef,
10672
            ),
10673
        ]);
10674
10675
        // okay: nullable to nullable
10676
        let to_type = DataType::Struct(
10677
            vec![
10678
                Field::new("a", DataType::Utf8, false),
10679
                Field::new("b", DataType::Utf8, true),
10680
            ]
10681
            .into(),
10682
        );
10683
        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
10684
10685
        // error: nullable to non-nullable
10686
        let to_type = DataType::Struct(
10687
            vec![
10688
                Field::new("a", DataType::Utf8, false),
10689
                Field::new("b", DataType::Utf8, false),
10690
            ]
10691
            .into(),
10692
        );
10693
        cast(&struct_array, &to_type)
10694
            .expect_err("Cast nullable to non-nullable struct field should fail");
10695
10696
        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10697
        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
10698
        let struct_array = StructArray::from(vec![
10699
            (
10700
                Arc::new(Field::new("b", DataType::Boolean, false)),
10701
                boolean.clone() as ArrayRef,
10702
            ),
10703
            (
10704
                Arc::new(Field::new("c", DataType::Int32, false)),
10705
                int.clone() as ArrayRef,
10706
            ),
10707
        ]);
10708
10709
        // okay: non-nullable to non-nullable
10710
        let to_type = DataType::Struct(
10711
            vec![
10712
                Field::new("a", DataType::Utf8, false),
10713
                Field::new("b", DataType::Utf8, false),
10714
            ]
10715
            .into(),
10716
        );
10717
        cast(&struct_array, &to_type)
10718
            .expect("Cast non-nullable to non-nullable struct field should work");
10719
10720
        // err: non-nullable to non-nullable but overflowing return null during casting
10721
        let to_type = DataType::Struct(
10722
            vec![
10723
                Field::new("a", DataType::Utf8, false),
10724
                Field::new("b", DataType::Int8, false),
10725
            ]
10726
            .into(),
10727
        );
10728
        cast(&struct_array, &to_type).expect_err(
10729
            "Cast non-nullable to non-nullable struct field returning null should fail",
10730
        );
10731
    }
10732
10733
    #[test]
10734
    fn test_cast_struct_to_non_struct() {
10735
        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
10736
        let struct_array = StructArray::from(vec![(
10737
            Arc::new(Field::new("a", DataType::Boolean, false)),
10738
            boolean.clone() as ArrayRef,
10739
        )]);
10740
        let to_type = DataType::Utf8;
10741
        let result = cast(&struct_array, &to_type);
10742
        assert_eq!(
10743
            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"#,
10744
            result.unwrap_err().to_string()
10745
        );
10746
    }
10747
10748
    #[test]
10749
    fn test_cast_non_struct_to_struct() {
10750
        let array = StringArray::from(vec!["a", "b"]);
10751
        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
10752
        let result = cast(&array, &to_type);
10753
        assert_eq!(
10754
            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"#,
10755
            result.unwrap_err().to_string()
10756
        );
10757
    }
10758
10759
    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
10760
        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
10761
        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
10762
        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
10763
        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
10764
    }
10765
10766
    #[test]
10767
    fn test_decimal_to_decimal_coverage() {
10768
        let test_cases = [
10769
            // increase precision, increase scale, infallible
10770
            DecimalCastTestConfig {
10771
                input_prec: 5,
10772
                input_scale: 1,
10773
                input_repr: 99999, // 9999.9
10774
                output_prec: 10,
10775
                output_scale: 6,
10776
                expected_output_repr: Ok(9999900000), // 9999.900000
10777
            },
10778
            // increase precision, increase scale, fallible, safe
10779
            DecimalCastTestConfig {
10780
                input_prec: 5,
10781
                input_scale: 1,
10782
                input_repr: 99, // 9999.9
10783
                output_prec: 7,
10784
                output_scale: 6,
10785
                expected_output_repr: Ok(9900000), // 9.900000
10786
            },
10787
            // increase precision, increase scale, fallible, unsafe
10788
            DecimalCastTestConfig {
10789
                input_prec: 5,
10790
                input_scale: 1,
10791
                input_repr: 99999, // 9999.9
10792
                output_prec: 7,
10793
                output_scale: 6,
10794
                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
10795
            },
10796
            // increase precision, decrease scale, always infallible
10797
            DecimalCastTestConfig {
10798
                input_prec: 5,
10799
                input_scale: 3,
10800
                input_repr: 99999, // 99.999
10801
                output_prec: 10,
10802
                output_scale: 2,
10803
                expected_output_repr: Ok(10000), // 100.00
10804
            },
10805
            // increase precision, decrease scale, no rouding
10806
            DecimalCastTestConfig {
10807
                input_prec: 5,
10808
                input_scale: 3,
10809
                input_repr: 99994, // 99.994
10810
                output_prec: 10,
10811
                output_scale: 2,
10812
                expected_output_repr: Ok(9999), // 99.99
10813
            },
10814
            // increase precision, don't change scale, always infallible
10815
            DecimalCastTestConfig {
10816
                input_prec: 5,
10817
                input_scale: 3,
10818
                input_repr: 99999, // 99.999
10819
                output_prec: 10,
10820
                output_scale: 3,
10821
                expected_output_repr: Ok(99999), // 99.999
10822
            },
10823
            // decrease precision, increase scale, safe
10824
            DecimalCastTestConfig {
10825
                input_prec: 10,
10826
                input_scale: 5,
10827
                input_repr: 999999, // 9.99999
10828
                output_prec: 8,
10829
                output_scale: 7,
10830
                expected_output_repr: Ok(99999900), // 9.9999900
10831
            },
10832
            // decrease precision, increase scale, unsafe
10833
            DecimalCastTestConfig {
10834
                input_prec: 10,
10835
                input_scale: 5,
10836
                input_repr: 9999999, // 99.99999
10837
                output_prec: 8,
10838
                output_scale: 7,
10839
                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
10840
            },
10841
            // decrease precision, decrease scale, safe, infallible
10842
            DecimalCastTestConfig {
10843
                input_prec: 7,
10844
                input_scale: 4,
10845
                input_repr: 9999999, // 999.9999
10846
                output_prec: 6,
10847
                output_scale: 2,
10848
                expected_output_repr: Ok(100000),
10849
            },
10850
            // decrease precision, decrease scale, safe, fallible
10851
            DecimalCastTestConfig {
10852
                input_prec: 10,
10853
                input_scale: 5,
10854
                input_repr: 12345678, // 123.45678
10855
                output_prec: 8,
10856
                output_scale: 3,
10857
                expected_output_repr: Ok(123457), // 123.457
10858
            },
10859
            // decrease precision, decrease scale, unsafe
10860
            DecimalCastTestConfig {
10861
                input_prec: 10,
10862
                input_scale: 5,
10863
                input_repr: 9999999, // 99.99999
10864
                output_prec: 4,
10865
                output_scale: 3,
10866
                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
10867
            },
10868
            // decrease precision, same scale, safe
10869
            DecimalCastTestConfig {
10870
                input_prec: 10,
10871
                input_scale: 5,
10872
                input_repr: 999999, // 9.99999
10873
                output_prec: 6,
10874
                output_scale: 5,
10875
                expected_output_repr: Ok(999999), // 9.99999
10876
            },
10877
            // decrease precision, same scale, unsafe
10878
            DecimalCastTestConfig {
10879
                input_prec: 10,
10880
                input_scale: 5,
10881
                input_repr: 9999999, // 99.99999
10882
                output_prec: 6,
10883
                output_scale: 5,
10884
                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
10885
            },
10886
            // same precision, increase scale, safe
10887
            DecimalCastTestConfig {
10888
                input_prec: 7,
10889
                input_scale: 4,
10890
                input_repr: 12345, // 1.2345
10891
                output_prec: 7,
10892
                output_scale: 6,
10893
                expected_output_repr: Ok(1234500), // 1.234500
10894
            },
10895
            // same precision, increase scale, unsafe
10896
            DecimalCastTestConfig {
10897
                input_prec: 7,
10898
                input_scale: 4,
10899
                input_repr: 123456, // 12.3456
10900
                output_prec: 7,
10901
                output_scale: 6,
10902
                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
10903
            },
10904
            // same precision, decrease scale, infallible
10905
            DecimalCastTestConfig {
10906
                input_prec: 7,
10907
                input_scale: 5,
10908
                input_repr: 1234567, // 12.34567
10909
                output_prec: 7,
10910
                output_scale: 4,
10911
                expected_output_repr: Ok(123457), // 12.3457
10912
            },
10913
            // same precision, same scale, infallible
10914
            DecimalCastTestConfig {
10915
                input_prec: 7,
10916
                input_scale: 5,
10917
                input_repr: 9999999, // 99.99999
10918
                output_prec: 7,
10919
                output_scale: 5,
10920
                expected_output_repr: Ok(9999999), // 99.99999
10921
            },
10922
            // precision increase, input scale & output scale = 0, infallible
10923
            DecimalCastTestConfig {
10924
                input_prec: 7,
10925
                input_scale: 0,
10926
                input_repr: 1234567, // 1234567
10927
                output_prec: 8,
10928
                output_scale: 0,
10929
                expected_output_repr: Ok(1234567), // 1234567
10930
            },
10931
            // precision decrease, input scale & output scale = 0, failure
10932
            DecimalCastTestConfig {
10933
                input_prec: 7,
10934
                input_scale: 0,
10935
                input_repr: 1234567, // 1234567
10936
                output_prec: 6,
10937
                output_scale: 0,
10938
                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
10939
            },
10940
            // precision decrease, input scale & output scale = 0, success
10941
            DecimalCastTestConfig {
10942
                input_prec: 7,
10943
                input_scale: 0,
10944
                input_repr: 123456, // 123456
10945
                output_prec: 6,
10946
                output_scale: 0,
10947
                expected_output_repr: Ok(123456), // 123456
10948
            },
10949
        ];
10950
10951
        for t in test_cases {
10952
            run_decimal_cast_test_case_between_multiple_types(t);
10953
        }
10954
    }
10955
10956
    #[test]
10957
    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
10958
        let test_cases = [
10959
            DecimalCastTestConfig {
10960
                input_prec: 5,
10961
                input_scale: 0,
10962
                input_repr: 99999,
10963
                output_prec: 10,
10964
                output_scale: 5,
10965
                expected_output_repr: Ok(9999900000),
10966
            },
10967
            DecimalCastTestConfig {
10968
                input_prec: 5,
10969
                input_scale: 0,
10970
                input_repr: -99999,
10971
                output_prec: 10,
10972
                output_scale: 5,
10973
                expected_output_repr: Ok(-9999900000),
10974
            },
10975
            DecimalCastTestConfig {
10976
                input_prec: 5,
10977
                input_scale: 2,
10978
                input_repr: 99999,
10979
                output_prec: 10,
10980
                output_scale: 5,
10981
                expected_output_repr: Ok(99999000),
10982
            },
10983
            DecimalCastTestConfig {
10984
                input_prec: 5,
10985
                input_scale: -2,
10986
                input_repr: -99999,
10987
                output_prec: 10,
10988
                output_scale: 3,
10989
                expected_output_repr: Ok(-9999900000),
10990
            },
10991
            DecimalCastTestConfig {
10992
                input_prec: 5,
10993
                input_scale: 3,
10994
                input_repr: -12345,
10995
                output_prec: 6,
10996
                output_scale: 5,
10997
                expected_output_repr: Err("Invalid argument error: -1234500 is too small to store in a {} of precision 6. Min is -999999".to_string())
10998
            },
10999
        ];
11000
11001
        for t in test_cases {
11002
            run_decimal_cast_test_case_between_multiple_types(t);
11003
        }
11004
    }
11005
11006
    #[test]
11007
    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
11008
        let test_cases = [
11009
            DecimalCastTestConfig {
11010
                input_prec: 5,
11011
                input_scale: 0,
11012
                input_repr: 99999,
11013
                output_scale: -3,
11014
                output_prec: 3,
11015
                expected_output_repr: Ok(100),
11016
            },
11017
            DecimalCastTestConfig {
11018
                input_prec: 5,
11019
                input_scale: 0,
11020
                input_repr: -99999,
11021
                output_prec: 1,
11022
                output_scale: -5,
11023
                expected_output_repr: Ok(-1),
11024
            },
11025
            DecimalCastTestConfig {
11026
                input_prec: 10,
11027
                input_scale: 2,
11028
                input_repr: 123456789,
11029
                output_prec: 5,
11030
                output_scale: -2,
11031
                expected_output_repr: Ok(12346),
11032
            },
11033
            DecimalCastTestConfig {
11034
                input_prec: 10,
11035
                input_scale: 4,
11036
                input_repr: -9876543210,
11037
                output_prec: 7,
11038
                output_scale: 0,
11039
                expected_output_repr: Ok(-987654),
11040
            },
11041
            DecimalCastTestConfig {
11042
                input_prec: 7,
11043
                input_scale: 4,
11044
                input_repr: 9999999,
11045
                output_prec: 6,
11046
                output_scale: 3,
11047
                expected_output_repr:
11048
                    Err("Invalid argument error: 1000000 is too large to store in a {} of precision 6. Max is 999999".to_string()),
11049
            },
11050
        ];
11051
        for t in test_cases {
11052
            run_decimal_cast_test_case_between_multiple_types(t);
11053
        }
11054
    }
11055
11056
    #[test]
11057
    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
11058
        let array = vec![Some(123456789)];
11059
        let array = create_decimal128_array(array, 24, 2).unwrap();
11060
        let input_type = DataType::Decimal128(24, 2);
11061
        let output_type = DataType::Decimal128(6, 2);
11062
        assert!(can_cast_types(&input_type, &output_type));
11063
11064
        let options = CastOptions {
11065
            safe: false,
11066
            ..Default::default()
11067
        };
11068
        let result = cast_with_options(&array, &output_type, &options);
11069
        assert_eq!(result.unwrap_err().to_string(),
11070
                   "Invalid argument error: 123456789 is too large to store in a Decimal128 of precision 6. Max is 999999");
11071
    }
11072
11073
    #[test]
11074
    fn test_decimal_to_decimal_same_scale() {
11075
        let array = vec![Some(520)];
11076
        let array = create_decimal128_array(array, 4, 2).unwrap();
11077
        let input_type = DataType::Decimal128(4, 2);
11078
        let output_type = DataType::Decimal128(3, 2);
11079
        assert!(can_cast_types(&input_type, &output_type));
11080
11081
        let options = CastOptions {
11082
            safe: false,
11083
            ..Default::default()
11084
        };
11085
        let result = cast_with_options(&array, &output_type, &options);
11086
        assert_eq!(
11087
            result.unwrap().as_primitive::<Decimal128Type>().value(0),
11088
            520
11089
        );
11090
11091
        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
11092
        assert_eq!(
11093
            &cast(
11094
                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
11095
                &DataType::Decimal128(2, 0)
11096
            )
11097
            .unwrap(),
11098
            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
11099
        );
11100
    }
11101
11102
    #[test]
11103
    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
11104
        let array = vec![Some(123456789)];
11105
        let array = create_decimal128_array(array, 24, 4).unwrap();
11106
        let input_type = DataType::Decimal128(24, 4);
11107
        let output_type = DataType::Decimal128(6, 2);
11108
        assert!(can_cast_types(&input_type, &output_type));
11109
11110
        let options = CastOptions {
11111
            safe: false,
11112
            ..Default::default()
11113
        };
11114
        let result = cast_with_options(&array, &output_type, &options);
11115
        assert_eq!(result.unwrap_err().to_string(),
11116
                   "Invalid argument error: 1234568 is too large to store in a Decimal128 of precision 6. Max is 999999");
11117
    }
11118
11119
    #[test]
11120
    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
11121
        let array = vec![Some(123456789)];
11122
        let array = create_decimal128_array(array, 24, 2).unwrap();
11123
        let input_type = DataType::Decimal128(24, 2);
11124
        let output_type = DataType::Decimal128(6, 3);
11125
        assert!(can_cast_types(&input_type, &output_type));
11126
11127
        let options = CastOptions {
11128
            safe: false,
11129
            ..Default::default()
11130
        };
11131
        let result = cast_with_options(&array, &output_type, &options);
11132
        assert_eq!(result.unwrap_err().to_string(),
11133
                   "Invalid argument error: 1234567890 is too large to store in a Decimal128 of precision 6. Max is 999999");
11134
    }
11135
11136
    #[test]
11137
    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
11138
        let array = vec![Some(123456789)];
11139
        let array = create_decimal128_array(array, 24, 2).unwrap();
11140
        let input_type = DataType::Decimal128(24, 2);
11141
        let output_type = DataType::Decimal256(6, 2);
11142
        assert!(can_cast_types(&input_type, &output_type));
11143
11144
        let options = CastOptions {
11145
            safe: false,
11146
            ..Default::default()
11147
        };
11148
        let result = cast_with_options(&array, &output_type, &options);
11149
        assert_eq!(result.unwrap_err().to_string(),
11150
                   "Invalid argument error: 123456789 is too large to store in a Decimal256 of precision 6. Max is 999999");
11151
    }
11152
11153
    #[test]
11154
    fn test_first_none() {
11155
        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11156
            None,
11157
            Some(vec![Some(1), Some(2)]),
11158
        ])) as ArrayRef;
11159
        let data_type =
11160
            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11161
        let opt = CastOptions::default();
11162
        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11163
11164
        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11165
            vec![None, Some(vec![Some(1), Some(2)])],
11166
            2,
11167
        )) as ArrayRef;
11168
        assert_eq!(*fixed_array, *r);
11169
    }
11170
11171
    #[test]
11172
    fn test_first_last_none() {
11173
        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11174
            None,
11175
            Some(vec![Some(1), Some(2)]),
11176
            None,
11177
        ])) as ArrayRef;
11178
        let data_type =
11179
            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11180
        let opt = CastOptions::default();
11181
        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11182
11183
        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11184
            vec![None, Some(vec![Some(1), Some(2)]), None],
11185
            2,
11186
        )) as ArrayRef;
11187
        assert_eq!(*fixed_array, *r);
11188
    }
11189
}