1mod decimal;
41mod dictionary;
42mod list;
43mod map;
44mod string;
45use crate::cast::decimal::*;
46use crate::cast::dictionary::*;
47use crate::cast::list::*;
48use crate::cast::map::*;
49use crate::cast::string::*;
50
51use arrow_buffer::IntervalMonthDayNano;
52use arrow_data::ByteView;
53use chrono::{NaiveTime, Offset, TimeZone, Utc};
54use std::cmp::Ordering;
55use std::sync::Arc;
56
57use crate::display::{ArrayFormatter, FormatOptions};
58use crate::parse::{
59 parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
60 string_to_datetime, Parser,
61};
62use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
63use arrow_buffer::{i256, ArrowNativeType, OffsetBuffer};
64use arrow_data::transform::MutableArrayData;
65use arrow_data::ArrayData;
66use arrow_schema::*;
67use arrow_select::take::take;
68use num::cast::AsPrimitive;
69use num::{NumCast, ToPrimitive};
70
71#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub struct CastOptions<'a> {
74 pub safe: bool,
76 pub format_options: FormatOptions<'a>,
78}
79
80impl Default for CastOptions<'_> {
81 fn default() -> Self {
82 Self {
83 safe: true,
84 format_options: FormatOptions::default(),
85 }
86 }
87}
88
89pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
93 use self::DataType::*;
94 use self::IntervalUnit::*;
95 use self::TimeUnit::*;
96 if from_type == to_type {
97 return true;
98 }
99
100 match (from_type, to_type) {
101 (
102 Null,
103 Boolean
104 | Int8
105 | UInt8
106 | Int16
107 | UInt16
108 | Int32
109 | UInt32
110 | Float32
111 | Date32
112 | Time32(_)
113 | Int64
114 | UInt64
115 | Float64
116 | Date64
117 | Timestamp(_, _)
118 | Time64(_)
119 | Duration(_)
120 | Interval(_)
121 | FixedSizeBinary(_)
122 | Binary
123 | Utf8
124 | LargeBinary
125 | LargeUtf8
126 | BinaryView
127 | Utf8View
128 | List(_)
129 | LargeList(_)
130 | FixedSizeList(_, _)
131 | Struct(_)
132 | Map(_, _)
133 | Dictionary(_, _),
134 ) => true,
135 (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
137 can_cast_types(from_value_type, to_value_type)
138 }
139 (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
140 (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
141 (List(list_from) | LargeList(list_from), List(list_to) | LargeList(list_to)) => {
142 can_cast_types(list_from.data_type(), list_to.data_type())
143 }
144 (List(list_from) | LargeList(list_from), Utf8 | LargeUtf8) => {
145 can_cast_types(list_from.data_type(), to_type)
146 }
147 (List(list_from) | LargeList(list_from), FixedSizeList(list_to, _)) => {
148 can_cast_types(list_from.data_type(), list_to.data_type())
149 }
150 (List(_), _) => false,
151 (FixedSizeList(list_from, _), List(list_to))
152 | (FixedSizeList(list_from, _), LargeList(list_to)) => {
153 can_cast_types(list_from.data_type(), list_to.data_type())
154 }
155 (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
156 can_cast_types(inner.data_type(), inner_to.data_type())
157 }
158 (_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
159 (_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
160 (_, FixedSizeList(list_to, size)) if *size == 1 => {
161 can_cast_types(from_type, list_to.data_type())
162 }
163 (FixedSizeList(list_from, size), _) if *size == 1 => {
164 can_cast_types(list_from.data_type(), to_type)
165 }
166 (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
167 if ordered_from == ordered_to =>
168 {
169 match (
170 key_field(from_entries),
171 key_field(to_entries),
172 value_field(from_entries),
173 value_field(to_entries),
174 ) {
175 (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
176 can_cast_types(from_key.data_type(), to_key.data_type())
177 && can_cast_types(from_value.data_type(), to_value.data_type())
178 }
179 _ => false,
180 }
181 }
182 (
184 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
185 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
186 ) => true,
187 (
189 UInt8 | UInt16 | UInt32 | UInt64,
190 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
191 ) => true,
192 (
194 Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
195 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
196 ) => true,
197 (
199 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
200 UInt8 | UInt16 | UInt32 | UInt64,
201 ) => true,
202 (
204 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
205 Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
206 ) => true,
207 (
209 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
210 Utf8View | Utf8 | LargeUtf8,
211 ) => true,
212 (
214 Utf8View | Utf8 | LargeUtf8,
215 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
216 ) => true,
217 (Struct(from_fields), Struct(to_fields)) => {
218 from_fields.len() == to_fields.len()
219 && from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
220 can_cast_types(f1.data_type(), f2.data_type())
223 })
224 }
225 (Struct(_), _) => false,
226 (_, Struct(_)) => false,
227 (_, Boolean) => {
228 DataType::is_integer(from_type)
229 || DataType::is_floating(from_type)
230 || from_type == &Utf8View
231 || from_type == &Utf8
232 || from_type == &LargeUtf8
233 }
234 (Boolean, _) => {
235 DataType::is_integer(to_type)
236 || DataType::is_floating(to_type)
237 || to_type == &Utf8View
238 || to_type == &Utf8
239 || to_type == &LargeUtf8
240 }
241
242 (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
243 true
244 }
245 (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
246 true
247 }
248 (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 ) => true,
268 (Utf8 | LargeUtf8, Utf8View) => true,
269 (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
270 (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
271 (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
272
273 (_, Binary | LargeBinary) => from_type.is_integer(),
274
275 (
277 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
278 | Float64,
279 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
280 | Float64,
281 ) => true,
282 (Int32, Date32 | Date64 | Time32(_)) => true,
286 (Date32, Int32 | Int64) => true,
287 (Time32(_), Int32) => true,
288 (Int64, Date64 | Date32 | Time64(_)) => true,
289 (Date64, Int64 | Int32) => true,
290 (Time64(_), Int64) => true,
291 (Date32 | Date64, Date32 | Date64) => true,
292 (Time32(_), Time32(_)) => true,
294 (Time32(_), Time64(_)) => true,
295 (Time64(_), Time64(_)) => true,
296 (Time64(_), Time32(to_unit)) => {
297 matches!(to_unit, Second | Millisecond)
298 }
299 (Timestamp(_, _), _) if to_type.is_numeric() => true,
300 (_, Timestamp(_, _)) if from_type.is_numeric() => true,
301 (Date64, Timestamp(_, _)) => true,
302 (Date32, Timestamp(_, _)) => true,
303 (
304 Timestamp(_, _),
305 Timestamp(_, _)
306 | Date32
307 | Date64
308 | Time32(Second)
309 | Time32(Millisecond)
310 | Time64(Microsecond)
311 | Time64(Nanosecond),
312 ) => true,
313 (_, Duration(_)) if from_type.is_numeric() => true,
314 (Duration(_), _) if to_type.is_numeric() => true,
315 (Duration(_), Duration(_)) => true,
316 (Interval(from_type), Int64) => {
317 match from_type {
318 YearMonth => true,
319 DayTime => true,
320 MonthDayNano => false, }
322 }
323 (Int32, Interval(to_type)) => match to_type {
324 YearMonth => true,
325 DayTime => false,
326 MonthDayNano => false,
327 },
328 (Duration(_), Interval(MonthDayNano)) => true,
329 (Interval(MonthDayNano), Duration(_)) => true,
330 (Interval(YearMonth), Interval(MonthDayNano)) => true,
331 (Interval(DayTime), Interval(MonthDayNano)) => true,
332 (_, _) => false,
333 }
334}
335
336pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
340 cast_with_options(array, to_type, &CastOptions::default())
341}
342
343fn cast_integer_to_decimal<
344 T: ArrowPrimitiveType,
345 D: DecimalType + ArrowPrimitiveType<Native = M>,
346 M,
347>(
348 array: &PrimitiveArray<T>,
349 precision: u8,
350 scale: i8,
351 base: M,
352 cast_options: &CastOptions,
353) -> Result<ArrayRef, ArrowError>
354where
355 <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
356 M: ArrowNativeTypeOp,
357{
358 let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
359 ArrowError::CastError(format!(
360 "Cannot cast to {:?}({}, {}). The scale causes overflow.",
361 D::PREFIX,
362 precision,
363 scale,
364 ))
365 })?;
366
367 let array = if scale < 0 {
368 match cast_options.safe {
369 true => array.unary_opt::<_, D>(|v| {
370 v.as_()
371 .div_checked(scale_factor)
372 .ok()
373 .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
374 }),
375 false => array.try_unary::<_, D, _>(|v| {
376 v.as_()
377 .div_checked(scale_factor)
378 .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
379 })?,
380 }
381 } else {
382 match cast_options.safe {
383 true => array.unary_opt::<_, D>(|v| {
384 v.as_()
385 .mul_checked(scale_factor)
386 .ok()
387 .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
388 }),
389 false => array.try_unary::<_, D, _>(|v| {
390 v.as_()
391 .mul_checked(scale_factor)
392 .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
393 })?,
394 }
395 };
396
397 Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
398}
399
400fn cast_interval_year_month_to_interval_month_day_nano(
402 array: &dyn Array,
403 _cast_options: &CastOptions,
404) -> Result<ArrayRef, ArrowError> {
405 let array = array.as_primitive::<IntervalYearMonthType>();
406
407 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
408 let months = IntervalYearMonthType::to_months(v);
409 IntervalMonthDayNanoType::make_value(months, 0, 0)
410 })))
411}
412
413fn cast_interval_day_time_to_interval_month_day_nano(
415 array: &dyn Array,
416 _cast_options: &CastOptions,
417) -> Result<ArrayRef, ArrowError> {
418 let array = array.as_primitive::<IntervalDayTimeType>();
419 let mul = 1_000_000;
420
421 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
422 let (days, ms) = IntervalDayTimeType::to_parts(v);
423 IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
424 })))
425}
426
427fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
429 array: &dyn Array,
430 cast_options: &CastOptions,
431) -> Result<ArrayRef, ArrowError> {
432 let array = array.as_primitive::<IntervalMonthDayNanoType>();
433 let scale = match D::DATA_TYPE {
434 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
435 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
436 DataType::Duration(TimeUnit::Microsecond) => 1_000,
437 DataType::Duration(TimeUnit::Nanosecond) => 1,
438 _ => unreachable!(),
439 };
440
441 if cast_options.safe {
442 let iter = array.iter().map(|v| {
443 v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
444 });
445 Ok(Arc::new(unsafe {
446 PrimitiveArray::<D>::from_trusted_len_iter(iter)
447 }))
448 } else {
449 let vec = array
450 .iter()
451 .map(|v| {
452 v.map(|v| match v.days == 0 && v.months == 0 {
453 true => Ok((v.nanoseconds) / scale),
454 _ => Err(ArrowError::ComputeError(
455 "Cannot convert interval containing non-zero months or days to duration"
456 .to_string(),
457 )),
458 })
459 .transpose()
460 })
461 .collect::<Result<Vec<_>, _>>()?;
462 Ok(Arc::new(unsafe {
463 PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
464 }))
465 }
466}
467
468fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
470 array: &dyn Array,
471 cast_options: &CastOptions,
472) -> Result<ArrayRef, ArrowError> {
473 let array = array
474 .as_any()
475 .downcast_ref::<PrimitiveArray<D>>()
476 .ok_or_else(|| {
477 ArrowError::ComputeError(
478 "Internal Error: Cannot cast duration to DurationArray of expected type"
479 .to_string(),
480 )
481 })?;
482
483 let scale = match array.data_type() {
484 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
485 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
486 DataType::Duration(TimeUnit::Microsecond) => 1_000,
487 DataType::Duration(TimeUnit::Nanosecond) => 1,
488 _ => unreachable!(),
489 };
490
491 if cast_options.safe {
492 let iter = array.iter().map(|v| {
493 v.and_then(|v| {
494 v.checked_mul(scale)
495 .map(|v| IntervalMonthDayNano::new(0, 0, v))
496 })
497 });
498 Ok(Arc::new(unsafe {
499 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
500 }))
501 } else {
502 let vec = array
503 .iter()
504 .map(|v| {
505 v.map(|v| {
506 if let Ok(v) = v.mul_checked(scale) {
507 Ok(IntervalMonthDayNano::new(0, 0, v))
508 } else {
509 Err(ArrowError::ComputeError(format!(
510 "Cannot cast to {:?}. Overflowing on {:?}",
511 IntervalMonthDayNanoType::DATA_TYPE,
512 v
513 )))
514 }
515 })
516 .transpose()
517 })
518 .collect::<Result<Vec<_>, _>>()?;
519 Ok(Arc::new(unsafe {
520 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
521 }))
522 }
523}
524
525fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
527 array: &dyn Array,
528) -> Result<ArrayRef, ArrowError> {
529 Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
530}
531
532fn make_timestamp_array(
533 array: &PrimitiveArray<Int64Type>,
534 unit: TimeUnit,
535 tz: Option<Arc<str>>,
536) -> ArrayRef {
537 match unit {
538 TimeUnit::Second => Arc::new(
539 array
540 .reinterpret_cast::<TimestampSecondType>()
541 .with_timezone_opt(tz),
542 ),
543 TimeUnit::Millisecond => Arc::new(
544 array
545 .reinterpret_cast::<TimestampMillisecondType>()
546 .with_timezone_opt(tz),
547 ),
548 TimeUnit::Microsecond => Arc::new(
549 array
550 .reinterpret_cast::<TimestampMicrosecondType>()
551 .with_timezone_opt(tz),
552 ),
553 TimeUnit::Nanosecond => Arc::new(
554 array
555 .reinterpret_cast::<TimestampNanosecondType>()
556 .with_timezone_opt(tz),
557 ),
558 }
559}
560
561fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
562 match unit {
563 TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
564 TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
565 TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
566 TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
567 }
568}
569
570fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
571 v: i64,
572 tz: Option<Tz>,
573) -> Result<NaiveTime, ArrowError> {
574 let time = match tz {
575 Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
576 None => as_datetime::<T>(v).map(|d| d.time()),
577 };
578
579 time.ok_or_else(|| {
580 ArrowError::CastError(format!(
581 "Failed to create naive time with {} {}",
582 std::any::type_name::<T>(),
583 v
584 ))
585 })
586}
587
588fn timestamp_to_date32<T: ArrowTimestampType>(
589 array: &PrimitiveArray<T>,
590) -> Result<ArrayRef, ArrowError> {
591 let err = |x: i64| {
592 ArrowError::CastError(format!(
593 "Cannot convert {} {x} to datetime",
594 std::any::type_name::<T>()
595 ))
596 };
597
598 let array: Date32Array = match array.timezone() {
599 Some(tz) => {
600 let tz: Tz = tz.parse()?;
601 array.try_unary(|x| {
602 as_datetime_with_timezone::<T>(x, tz)
603 .ok_or_else(|| err(x))
604 .map(|d| Date32Type::from_naive_date(d.date_naive()))
605 })?
606 }
607 None => array.try_unary(|x| {
608 as_datetime::<T>(x)
609 .ok_or_else(|| err(x))
610 .map(|d| Date32Type::from_naive_date(d.date()))
611 })?,
612 };
613 Ok(Arc::new(array))
614}
615
616pub fn cast_with_options(
746 array: &dyn Array,
747 to_type: &DataType,
748 cast_options: &CastOptions,
749) -> Result<ArrayRef, ArrowError> {
750 use DataType::*;
751 let from_type = array.data_type();
752 if from_type == to_type {
754 return Ok(make_array(array.to_data()));
755 }
756 match (from_type, to_type) {
757 (
758 Null,
759 Boolean
760 | Int8
761 | UInt8
762 | Int16
763 | UInt16
764 | Int32
765 | UInt32
766 | Float32
767 | Date32
768 | Time32(_)
769 | Int64
770 | UInt64
771 | Float64
772 | Date64
773 | Timestamp(_, _)
774 | Time64(_)
775 | Duration(_)
776 | Interval(_)
777 | FixedSizeBinary(_)
778 | Binary
779 | Utf8
780 | LargeBinary
781 | LargeUtf8
782 | BinaryView
783 | Utf8View
784 | List(_)
785 | LargeList(_)
786 | FixedSizeList(_, _)
787 | Struct(_)
788 | Map(_, _)
789 | Dictionary(_, _),
790 ) => Ok(new_null_array(to_type, array.len())),
791 (Dictionary(index_type, _), _) => match **index_type {
792 Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
793 Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
794 Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
795 Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
796 UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
797 UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
798 UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
799 UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
800 _ => Err(ArrowError::CastError(format!(
801 "Casting from dictionary type {from_type:?} to {to_type:?} not supported",
802 ))),
803 },
804 (_, Dictionary(index_type, value_type)) => match **index_type {
805 Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
806 Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
807 Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
808 Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
809 UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
810 UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
811 UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
812 UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
813 _ => Err(ArrowError::CastError(format!(
814 "Casting from type {from_type:?} to dictionary type {to_type:?} not supported",
815 ))),
816 },
817 (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
818 (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
819 (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
820 (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
821 (List(_), FixedSizeList(field, size)) => {
822 let array = array.as_list::<i32>();
823 cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
824 }
825 (LargeList(_), FixedSizeList(field, size)) => {
826 let array = array.as_list::<i64>();
827 cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
828 }
829 (List(_) | LargeList(_), _) => match to_type {
830 Utf8 => value_to_string::<i32>(array, cast_options),
831 LargeUtf8 => value_to_string::<i64>(array, cast_options),
832 _ => Err(ArrowError::CastError(
833 "Cannot cast list to non-list data types".to_string(),
834 )),
835 },
836 (FixedSizeList(list_from, size), List(list_to)) => {
837 if list_to.data_type() != list_from.data_type() {
838 let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
840 let array = cast_with_options(array, &fsl_to, cast_options)?;
841 cast_fixed_size_list_to_list::<i32>(array.as_ref())
842 } else {
843 cast_fixed_size_list_to_list::<i32>(array)
844 }
845 }
846 (FixedSizeList(list_from, size), LargeList(list_to)) => {
847 if list_to.data_type() != list_from.data_type() {
848 let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
850 let array = cast_with_options(array, &fsl_to, cast_options)?;
851 cast_fixed_size_list_to_list::<i64>(array.as_ref())
852 } else {
853 cast_fixed_size_list_to_list::<i64>(array)
854 }
855 }
856 (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
857 if size_from != size_to {
858 return Err(ArrowError::CastError(
859 "cannot cast fixed-size-list to fixed-size-list with different size".into(),
860 ));
861 }
862 let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
863 let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
864 Ok(Arc::new(FixedSizeListArray::try_new(
865 list_to.clone(),
866 *size_from,
867 values,
868 array.nulls().cloned(),
869 )?))
870 }
871 (_, List(ref to)) => cast_values_to_list::<i32>(array, to, cast_options),
872 (_, LargeList(ref to)) => cast_values_to_list::<i64>(array, to, cast_options),
873 (_, FixedSizeList(ref to, size)) if *size == 1 => {
874 cast_values_to_fixed_size_list(array, to, *size, cast_options)
875 }
876 (FixedSizeList(_, size), _) if *size == 1 => {
877 cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
878 }
879 (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
880 cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
881 }
882 (Decimal32(p1, s1), Decimal32(p2, s2)) => {
884 cast_decimal_to_decimal_same_type::<Decimal32Type>(
885 array.as_primitive(),
886 *p1,
887 *s1,
888 *p2,
889 *s2,
890 cast_options,
891 )
892 }
893 (Decimal64(p1, s1), Decimal64(p2, s2)) => {
894 cast_decimal_to_decimal_same_type::<Decimal64Type>(
895 array.as_primitive(),
896 *p1,
897 *s1,
898 *p2,
899 *s2,
900 cast_options,
901 )
902 }
903 (Decimal128(p1, s1), Decimal128(p2, s2)) => {
904 cast_decimal_to_decimal_same_type::<Decimal128Type>(
905 array.as_primitive(),
906 *p1,
907 *s1,
908 *p2,
909 *s2,
910 cast_options,
911 )
912 }
913 (Decimal256(p1, s1), Decimal256(p2, s2)) => {
914 cast_decimal_to_decimal_same_type::<Decimal256Type>(
915 array.as_primitive(),
916 *p1,
917 *s1,
918 *p2,
919 *s2,
920 cast_options,
921 )
922 }
923 (Decimal32(p1, s1), Decimal64(p2, s2)) => {
925 cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
926 array.as_primitive(),
927 *p1,
928 *s1,
929 *p2,
930 *s2,
931 cast_options,
932 )
933 }
934 (Decimal32(p1, s1), Decimal128(p2, s2)) => {
935 cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
936 array.as_primitive(),
937 *p1,
938 *s1,
939 *p2,
940 *s2,
941 cast_options,
942 )
943 }
944 (Decimal32(p1, s1), Decimal256(p2, s2)) => {
945 cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
946 array.as_primitive(),
947 *p1,
948 *s1,
949 *p2,
950 *s2,
951 cast_options,
952 )
953 }
954 (Decimal64(p1, s1), Decimal32(p2, s2)) => {
955 cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
956 array.as_primitive(),
957 *p1,
958 *s1,
959 *p2,
960 *s2,
961 cast_options,
962 )
963 }
964 (Decimal64(p1, s1), Decimal128(p2, s2)) => {
965 cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
966 array.as_primitive(),
967 *p1,
968 *s1,
969 *p2,
970 *s2,
971 cast_options,
972 )
973 }
974 (Decimal64(p1, s1), Decimal256(p2, s2)) => {
975 cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
976 array.as_primitive(),
977 *p1,
978 *s1,
979 *p2,
980 *s2,
981 cast_options,
982 )
983 }
984 (Decimal128(p1, s1), Decimal32(p2, s2)) => {
985 cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
986 array.as_primitive(),
987 *p1,
988 *s1,
989 *p2,
990 *s2,
991 cast_options,
992 )
993 }
994 (Decimal128(p1, s1), Decimal64(p2, s2)) => {
995 cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
996 array.as_primitive(),
997 *p1,
998 *s1,
999 *p2,
1000 *s2,
1001 cast_options,
1002 )
1003 }
1004 (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1005 cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1006 array.as_primitive(),
1007 *p1,
1008 *s1,
1009 *p2,
1010 *s2,
1011 cast_options,
1012 )
1013 }
1014 (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1015 cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1016 array.as_primitive(),
1017 *p1,
1018 *s1,
1019 *p2,
1020 *s2,
1021 cast_options,
1022 )
1023 }
1024 (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1025 cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1026 array.as_primitive(),
1027 *p1,
1028 *s1,
1029 *p2,
1030 *s2,
1031 cast_options,
1032 )
1033 }
1034 (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1035 cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1036 array.as_primitive(),
1037 *p1,
1038 *s1,
1039 *p2,
1040 *s2,
1041 cast_options,
1042 )
1043 }
1044 (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1046 cast_from_decimal::<Decimal32Type, _>(
1047 array,
1048 10_i32,
1049 scale,
1050 from_type,
1051 to_type,
1052 |x: i32| x as f64,
1053 cast_options,
1054 )
1055 }
1056 (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1057 cast_from_decimal::<Decimal64Type, _>(
1058 array,
1059 10_i64,
1060 scale,
1061 from_type,
1062 to_type,
1063 |x: i64| x as f64,
1064 cast_options,
1065 )
1066 }
1067 (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1068 cast_from_decimal::<Decimal128Type, _>(
1069 array,
1070 10_i128,
1071 scale,
1072 from_type,
1073 to_type,
1074 |x: i128| x as f64,
1075 cast_options,
1076 )
1077 }
1078 (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1079 cast_from_decimal::<Decimal256Type, _>(
1080 array,
1081 i256::from_i128(10_i128),
1082 scale,
1083 from_type,
1084 to_type,
1085 |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1086 cast_options,
1087 )
1088 }
1089 (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1091 cast_to_decimal::<Decimal32Type, _>(
1092 array,
1093 10_i32,
1094 precision,
1095 scale,
1096 from_type,
1097 to_type,
1098 cast_options,
1099 )
1100 }
1101 (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1102 cast_to_decimal::<Decimal64Type, _>(
1103 array,
1104 10_i64,
1105 precision,
1106 scale,
1107 from_type,
1108 to_type,
1109 cast_options,
1110 )
1111 }
1112 (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1113 cast_to_decimal::<Decimal128Type, _>(
1114 array,
1115 10_i128,
1116 precision,
1117 scale,
1118 from_type,
1119 to_type,
1120 cast_options,
1121 )
1122 }
1123 (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1124 cast_to_decimal::<Decimal256Type, _>(
1125 array,
1126 i256::from_i128(10_i128),
1127 precision,
1128 scale,
1129 from_type,
1130 to_type,
1131 cast_options,
1132 )
1133 }
1134 (Struct(_), Struct(to_fields)) => {
1135 let array = array.as_struct();
1136 let fields = array
1137 .columns()
1138 .iter()
1139 .zip(to_fields.iter())
1140 .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
1141 .collect::<Result<Vec<ArrayRef>, ArrowError>>()?;
1142 let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
1143 Ok(Arc::new(array) as ArrayRef)
1144 }
1145 (Struct(_), _) => Err(ArrowError::CastError(format!(
1146 "Casting from {from_type:?} to {to_type:?} not supported"
1147 ))),
1148 (_, Struct(_)) => Err(ArrowError::CastError(format!(
1149 "Casting from {from_type:?} to {to_type:?} not supported"
1150 ))),
1151 (_, Boolean) => match from_type {
1152 UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1153 UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1154 UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1155 UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1156 Int8 => cast_numeric_to_bool::<Int8Type>(array),
1157 Int16 => cast_numeric_to_bool::<Int16Type>(array),
1158 Int32 => cast_numeric_to_bool::<Int32Type>(array),
1159 Int64 => cast_numeric_to_bool::<Int64Type>(array),
1160 Float16 => cast_numeric_to_bool::<Float16Type>(array),
1161 Float32 => cast_numeric_to_bool::<Float32Type>(array),
1162 Float64 => cast_numeric_to_bool::<Float64Type>(array),
1163 Utf8View => cast_utf8view_to_boolean(array, cast_options),
1164 Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1165 LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1166 _ => Err(ArrowError::CastError(format!(
1167 "Casting from {from_type:?} to {to_type:?} not supported",
1168 ))),
1169 },
1170 (Boolean, _) => match to_type {
1171 UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1172 UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1173 UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1174 UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1175 Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1176 Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1177 Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1178 Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1179 Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1180 Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1181 Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1182 Utf8View => value_to_string_view(array, cast_options),
1183 Utf8 => value_to_string::<i32>(array, cast_options),
1184 LargeUtf8 => value_to_string::<i64>(array, cast_options),
1185 _ => Err(ArrowError::CastError(format!(
1186 "Casting from {from_type:?} to {to_type:?} not supported",
1187 ))),
1188 },
1189 (Utf8, _) => match to_type {
1190 UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1191 UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1192 UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1193 UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1194 Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1195 Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1196 Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1197 Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1198 Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1199 Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1200 Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1201 Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1202 Binary => Ok(Arc::new(BinaryArray::from(
1203 array.as_string::<i32>().clone(),
1204 ))),
1205 LargeBinary => {
1206 let binary = BinaryArray::from(array.as_string::<i32>().clone());
1207 cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1208 }
1209 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1210 BinaryView => Ok(Arc::new(
1211 StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1212 )),
1213 LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1214 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1215 Time32(TimeUnit::Millisecond) => {
1216 parse_string::<Time32MillisecondType, i32>(array, cast_options)
1217 }
1218 Time64(TimeUnit::Microsecond) => {
1219 parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1220 }
1221 Time64(TimeUnit::Nanosecond) => {
1222 parse_string::<Time64NanosecondType, i32>(array, cast_options)
1223 }
1224 Timestamp(TimeUnit::Second, to_tz) => {
1225 cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1226 }
1227 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1228 i32,
1229 TimestampMillisecondType,
1230 >(array, to_tz, cast_options),
1231 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1232 i32,
1233 TimestampMicrosecondType,
1234 >(array, to_tz, cast_options),
1235 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1236 cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1237 }
1238 Interval(IntervalUnit::YearMonth) => {
1239 cast_string_to_year_month_interval::<i32>(array, cast_options)
1240 }
1241 Interval(IntervalUnit::DayTime) => {
1242 cast_string_to_day_time_interval::<i32>(array, cast_options)
1243 }
1244 Interval(IntervalUnit::MonthDayNano) => {
1245 cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1246 }
1247 _ => Err(ArrowError::CastError(format!(
1248 "Casting from {from_type:?} to {to_type:?} not supported",
1249 ))),
1250 },
1251 (Utf8View, _) => match to_type {
1252 UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1253 UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1254 UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1255 UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1256 Int8 => parse_string_view::<Int8Type>(array, cast_options),
1257 Int16 => parse_string_view::<Int16Type>(array, cast_options),
1258 Int32 => parse_string_view::<Int32Type>(array, cast_options),
1259 Int64 => parse_string_view::<Int64Type>(array, cast_options),
1260 Float32 => parse_string_view::<Float32Type>(array, cast_options),
1261 Float64 => parse_string_view::<Float64Type>(array, cast_options),
1262 Date32 => parse_string_view::<Date32Type>(array, cast_options),
1263 Date64 => parse_string_view::<Date64Type>(array, cast_options),
1264 Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1265 LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1266 BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1267 Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1268 LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1269 Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1270 Time32(TimeUnit::Millisecond) => {
1271 parse_string_view::<Time32MillisecondType>(array, cast_options)
1272 }
1273 Time64(TimeUnit::Microsecond) => {
1274 parse_string_view::<Time64MicrosecondType>(array, cast_options)
1275 }
1276 Time64(TimeUnit::Nanosecond) => {
1277 parse_string_view::<Time64NanosecondType>(array, cast_options)
1278 }
1279 Timestamp(TimeUnit::Second, to_tz) => {
1280 cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1281 }
1282 Timestamp(TimeUnit::Millisecond, to_tz) => {
1283 cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1284 }
1285 Timestamp(TimeUnit::Microsecond, to_tz) => {
1286 cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1287 }
1288 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1289 cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1290 }
1291 Interval(IntervalUnit::YearMonth) => {
1292 cast_view_to_year_month_interval(array, cast_options)
1293 }
1294 Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1295 Interval(IntervalUnit::MonthDayNano) => {
1296 cast_view_to_month_day_nano_interval(array, cast_options)
1297 }
1298 _ => Err(ArrowError::CastError(format!(
1299 "Casting from {from_type:?} to {to_type:?} not supported",
1300 ))),
1301 },
1302 (LargeUtf8, _) => match to_type {
1303 UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1304 UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1305 UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1306 UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1307 Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1308 Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1309 Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1310 Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1311 Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1312 Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1313 Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1314 Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1315 Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1316 Binary => {
1317 let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1318 cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1319 }
1320 LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1321 array.as_string::<i64>().clone(),
1322 ))),
1323 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1324 BinaryView => Ok(Arc::new(BinaryViewArray::from(
1325 array
1326 .as_string::<i64>()
1327 .into_iter()
1328 .map(|x| x.map(|x| x.as_bytes()))
1329 .collect::<Vec<_>>(),
1330 ))),
1331 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1332 Time32(TimeUnit::Millisecond) => {
1333 parse_string::<Time32MillisecondType, i64>(array, cast_options)
1334 }
1335 Time64(TimeUnit::Microsecond) => {
1336 parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1337 }
1338 Time64(TimeUnit::Nanosecond) => {
1339 parse_string::<Time64NanosecondType, i64>(array, cast_options)
1340 }
1341 Timestamp(TimeUnit::Second, to_tz) => {
1342 cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1343 }
1344 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1345 i64,
1346 TimestampMillisecondType,
1347 >(array, to_tz, cast_options),
1348 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1349 i64,
1350 TimestampMicrosecondType,
1351 >(array, to_tz, cast_options),
1352 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1353 cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1354 }
1355 Interval(IntervalUnit::YearMonth) => {
1356 cast_string_to_year_month_interval::<i64>(array, cast_options)
1357 }
1358 Interval(IntervalUnit::DayTime) => {
1359 cast_string_to_day_time_interval::<i64>(array, cast_options)
1360 }
1361 Interval(IntervalUnit::MonthDayNano) => {
1362 cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1363 }
1364 _ => Err(ArrowError::CastError(format!(
1365 "Casting from {from_type:?} to {to_type:?} not supported",
1366 ))),
1367 },
1368 (Binary, _) => match to_type {
1369 Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1370 LargeUtf8 => {
1371 let array = cast_binary_to_string::<i32>(array, cast_options)?;
1372 cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1373 }
1374 LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1375 FixedSizeBinary(size) => {
1376 cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1377 }
1378 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1379 Utf8View => Ok(Arc::new(StringViewArray::from(
1380 cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1381 ))),
1382 _ => Err(ArrowError::CastError(format!(
1383 "Casting from {from_type:?} to {to_type:?} not supported",
1384 ))),
1385 },
1386 (LargeBinary, _) => match to_type {
1387 Utf8 => {
1388 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1389 cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1390 }
1391 LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1392 Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1393 FixedSizeBinary(size) => {
1394 cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1395 }
1396 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1397 Utf8View => {
1398 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1399 Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1400 }
1401 _ => Err(ArrowError::CastError(format!(
1402 "Casting from {from_type:?} to {to_type:?} not supported",
1403 ))),
1404 },
1405 (FixedSizeBinary(size), _) => match to_type {
1406 Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1407 LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1408 BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1409 _ => Err(ArrowError::CastError(format!(
1410 "Casting from {from_type:?} to {to_type:?} not supported",
1411 ))),
1412 },
1413 (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1414 (BinaryView, LargeBinary) => {
1415 cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1416 }
1417 (BinaryView, Utf8) => {
1418 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1419 cast_binary_to_string::<i32>(&binary_arr, cast_options)
1420 }
1421 (BinaryView, LargeUtf8) => {
1422 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1423 cast_binary_to_string::<i64>(&binary_arr, cast_options)
1424 }
1425 (BinaryView, Utf8View) => {
1426 Ok(Arc::new(array.as_binary_view().clone().to_string_view()?) as ArrayRef)
1427 }
1428 (BinaryView, _) => Err(ArrowError::CastError(format!(
1429 "Casting from {from_type:?} to {to_type:?} not supported",
1430 ))),
1431 (from_type, Utf8View) if from_type.is_primitive() => {
1432 value_to_string_view(array, cast_options)
1433 }
1434 (from_type, LargeUtf8) if from_type.is_primitive() => {
1435 value_to_string::<i64>(array, cast_options)
1436 }
1437 (from_type, Utf8) if from_type.is_primitive() => {
1438 value_to_string::<i32>(array, cast_options)
1439 }
1440 (from_type, Binary) if from_type.is_integer() => match from_type {
1441 UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1442 UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1443 UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1444 UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1445 Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1446 Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1447 Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1448 Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1449 _ => unreachable!(),
1450 },
1451 (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1452 UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1453 UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1454 UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1455 UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1456 Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1457 Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1458 Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1459 Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1460 _ => unreachable!(),
1461 },
1462 (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1464 (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1465 (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1466 (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1467 (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1468 (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1469 (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1470 (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1471 (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1472 (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1473
1474 (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1475 (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1476 (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1477 (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1478 (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1479 (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1480 (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1481 (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1482 (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1483 (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1484
1485 (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1486 (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1487 (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1488 (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1489 (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1490 (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1491 (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1492 (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1493 (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1494 (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1495
1496 (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1497 (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1498 (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1499 (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1500 (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1501 (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1502 (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1503 (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1504 (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1505 (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1506
1507 (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1508 (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1509 (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1510 (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1511 (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1512 (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1513 (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1514 (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1515 (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1516 (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1517
1518 (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1519 (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1520 (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1521 (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1522 (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1523 (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1524 (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1525 (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1526 (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1527 (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1528
1529 (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1530 (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1531 (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1532 (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1533 (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1534 (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1535 (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1536 (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1537 (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1538 (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1539
1540 (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1541 (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1542 (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1543 (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1544 (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1545 (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1546 (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1547 (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1548 (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1549 (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1550
1551 (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1552 (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1553 (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1554 (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1555 (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1556 (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1557 (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1558 (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1559 (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1560 (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1561
1562 (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1563 (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1564 (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1565 (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1566 (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1567 (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1568 (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1569 (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1570 (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1571 (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1572
1573 (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1574 (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1575 (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1576 (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1577 (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1578 (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1579 (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1580 (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1581 (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1582 (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1583 (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1587 (Int32, Date64) => cast_with_options(
1588 &cast_with_options(array, &Date32, cast_options)?,
1589 &Date64,
1590 cast_options,
1591 ),
1592 (Int32, Time32(TimeUnit::Second)) => {
1593 cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1594 }
1595 (Int32, Time32(TimeUnit::Millisecond)) => {
1596 cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1597 }
1598 (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1600 (Date32, Int64) => cast_with_options(
1601 &cast_with_options(array, &Int32, cast_options)?,
1602 &Int64,
1603 cast_options,
1604 ),
1605 (Time32(TimeUnit::Second), Int32) => {
1606 cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1607 }
1608 (Time32(TimeUnit::Millisecond), Int32) => {
1609 cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1610 }
1611 (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1612 (Int64, Date32) => cast_with_options(
1613 &cast_with_options(array, &Int32, cast_options)?,
1614 &Date32,
1615 cast_options,
1616 ),
1617 (Int64, Time64(TimeUnit::Microsecond)) => {
1619 cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1620 }
1621 (Int64, Time64(TimeUnit::Nanosecond)) => {
1622 cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1623 }
1624
1625 (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1626 (Date64, Int32) => cast_with_options(
1627 &cast_with_options(array, &Int64, cast_options)?,
1628 &Int32,
1629 cast_options,
1630 ),
1631 (Time64(TimeUnit::Microsecond), Int64) => {
1632 cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1633 }
1634 (Time64(TimeUnit::Nanosecond), Int64) => {
1635 cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1636 }
1637 (Date32, Date64) => Ok(Arc::new(
1638 array
1639 .as_primitive::<Date32Type>()
1640 .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1641 )),
1642 (Date64, Date32) => Ok(Arc::new(
1643 array
1644 .as_primitive::<Date64Type>()
1645 .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1646 )),
1647
1648 (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1649 array
1650 .as_primitive::<Time32SecondType>()
1651 .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1652 )),
1653 (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1654 array
1655 .as_primitive::<Time32SecondType>()
1656 .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1657 )),
1658 (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1659 array
1660 .as_primitive::<Time32SecondType>()
1661 .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1662 )),
1663
1664 (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1665 array
1666 .as_primitive::<Time32MillisecondType>()
1667 .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1668 )),
1669 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1670 array
1671 .as_primitive::<Time32MillisecondType>()
1672 .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1673 )),
1674 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1675 array
1676 .as_primitive::<Time32MillisecondType>()
1677 .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1678 )),
1679
1680 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1681 array
1682 .as_primitive::<Time64MicrosecondType>()
1683 .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1684 )),
1685 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1686 array
1687 .as_primitive::<Time64MicrosecondType>()
1688 .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1689 )),
1690 (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1691 array
1692 .as_primitive::<Time64MicrosecondType>()
1693 .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1694 )),
1695
1696 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1697 array
1698 .as_primitive::<Time64NanosecondType>()
1699 .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1700 )),
1701 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1702 array
1703 .as_primitive::<Time64NanosecondType>()
1704 .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1705 )),
1706 (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1707 array
1708 .as_primitive::<Time64NanosecondType>()
1709 .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1710 )),
1711
1712 (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1714 let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1715 cast_with_options(&array, to_type, cast_options)
1716 }
1717 (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1718 let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1719 cast_with_options(&array, to_type, cast_options)
1720 }
1721 (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1722 let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1723 cast_with_options(&array, to_type, cast_options)
1724 }
1725 (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1726 let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1727 cast_with_options(&array, to_type, cast_options)
1728 }
1729
1730 (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1731 let array = cast_with_options(array, &Int64, cast_options)?;
1732 Ok(make_timestamp_array(
1733 array.as_primitive(),
1734 *unit,
1735 tz.clone(),
1736 ))
1737 }
1738
1739 (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1740 let array = cast_with_options(array, &Int64, cast_options)?;
1741 let time_array = array.as_primitive::<Int64Type>();
1742 let from_size = time_unit_multiple(from_unit);
1743 let to_size = time_unit_multiple(to_unit);
1744 let converted = match from_size.cmp(&to_size) {
1747 Ordering::Greater => {
1748 let divisor = from_size / to_size;
1749 time_array.unary::<_, Int64Type>(|o| o / divisor)
1750 }
1751 Ordering::Equal => time_array.clone(),
1752 Ordering::Less => {
1753 let mul = to_size / from_size;
1754 if cast_options.safe {
1755 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1756 } else {
1757 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1758 }
1759 }
1760 };
1761 let adjusted = match (from_tz, to_tz) {
1763 (None, Some(to_tz)) => {
1769 let to_tz: Tz = to_tz.parse()?;
1770 match to_unit {
1771 TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1772 converted,
1773 &to_tz,
1774 cast_options,
1775 )?,
1776 TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1777 TimestampMillisecondType,
1778 >(
1779 converted, &to_tz, cast_options
1780 )?,
1781 TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1782 TimestampMicrosecondType,
1783 >(
1784 converted, &to_tz, cast_options
1785 )?,
1786 TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1787 TimestampNanosecondType,
1788 >(
1789 converted, &to_tz, cast_options
1790 )?,
1791 }
1792 }
1793 _ => converted,
1794 };
1795 Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1796 }
1797 (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1798 timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1799 }
1800 (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1801 timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1802 }
1803 (Timestamp(TimeUnit::Second, _), Date32) => {
1804 timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1805 }
1806 (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1807 timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1808 }
1809 (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1810 true => {
1811 array
1813 .as_primitive::<TimestampSecondType>()
1814 .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1815 }
1816 false => array
1817 .as_primitive::<TimestampSecondType>()
1818 .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1819 })),
1820 (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1821 cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1822 }
1823 (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1824 array
1825 .as_primitive::<TimestampMicrosecondType>()
1826 .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1827 )),
1828 (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1829 array
1830 .as_primitive::<TimestampNanosecondType>()
1831 .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1832 )),
1833 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1834 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1835 Ok(Arc::new(
1836 array
1837 .as_primitive::<TimestampSecondType>()
1838 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1839 Ok(time_to_time64us(as_time_res_with_timezone::<
1840 TimestampSecondType,
1841 >(x, tz)?))
1842 })?,
1843 ))
1844 }
1845 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1846 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1847 Ok(Arc::new(
1848 array
1849 .as_primitive::<TimestampSecondType>()
1850 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1851 Ok(time_to_time64ns(as_time_res_with_timezone::<
1852 TimestampSecondType,
1853 >(x, tz)?))
1854 })?,
1855 ))
1856 }
1857 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1858 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1859 Ok(Arc::new(
1860 array
1861 .as_primitive::<TimestampMillisecondType>()
1862 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1863 Ok(time_to_time64us(as_time_res_with_timezone::<
1864 TimestampMillisecondType,
1865 >(x, tz)?))
1866 })?,
1867 ))
1868 }
1869 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1870 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1871 Ok(Arc::new(
1872 array
1873 .as_primitive::<TimestampMillisecondType>()
1874 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1875 Ok(time_to_time64ns(as_time_res_with_timezone::<
1876 TimestampMillisecondType,
1877 >(x, tz)?))
1878 })?,
1879 ))
1880 }
1881 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1882 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1883 Ok(Arc::new(
1884 array
1885 .as_primitive::<TimestampMicrosecondType>()
1886 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1887 Ok(time_to_time64us(as_time_res_with_timezone::<
1888 TimestampMicrosecondType,
1889 >(x, tz)?))
1890 })?,
1891 ))
1892 }
1893 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1894 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1895 Ok(Arc::new(
1896 array
1897 .as_primitive::<TimestampMicrosecondType>()
1898 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1899 Ok(time_to_time64ns(as_time_res_with_timezone::<
1900 TimestampMicrosecondType,
1901 >(x, tz)?))
1902 })?,
1903 ))
1904 }
1905 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1906 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1907 Ok(Arc::new(
1908 array
1909 .as_primitive::<TimestampNanosecondType>()
1910 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1911 Ok(time_to_time64us(as_time_res_with_timezone::<
1912 TimestampNanosecondType,
1913 >(x, tz)?))
1914 })?,
1915 ))
1916 }
1917 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1918 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1919 Ok(Arc::new(
1920 array
1921 .as_primitive::<TimestampNanosecondType>()
1922 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1923 Ok(time_to_time64ns(as_time_res_with_timezone::<
1924 TimestampNanosecondType,
1925 >(x, tz)?))
1926 })?,
1927 ))
1928 }
1929 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1930 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1931 Ok(Arc::new(
1932 array
1933 .as_primitive::<TimestampSecondType>()
1934 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1935 Ok(time_to_time32s(as_time_res_with_timezone::<
1936 TimestampSecondType,
1937 >(x, tz)?))
1938 })?,
1939 ))
1940 }
1941 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1942 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1943 Ok(Arc::new(
1944 array
1945 .as_primitive::<TimestampSecondType>()
1946 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1947 Ok(time_to_time32ms(as_time_res_with_timezone::<
1948 TimestampSecondType,
1949 >(x, tz)?))
1950 })?,
1951 ))
1952 }
1953 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1954 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1955 Ok(Arc::new(
1956 array
1957 .as_primitive::<TimestampMillisecondType>()
1958 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1959 Ok(time_to_time32s(as_time_res_with_timezone::<
1960 TimestampMillisecondType,
1961 >(x, tz)?))
1962 })?,
1963 ))
1964 }
1965 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
1966 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1967 Ok(Arc::new(
1968 array
1969 .as_primitive::<TimestampMillisecondType>()
1970 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1971 Ok(time_to_time32ms(as_time_res_with_timezone::<
1972 TimestampMillisecondType,
1973 >(x, tz)?))
1974 })?,
1975 ))
1976 }
1977 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
1978 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1979 Ok(Arc::new(
1980 array
1981 .as_primitive::<TimestampMicrosecondType>()
1982 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1983 Ok(time_to_time32s(as_time_res_with_timezone::<
1984 TimestampMicrosecondType,
1985 >(x, tz)?))
1986 })?,
1987 ))
1988 }
1989 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
1990 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1991 Ok(Arc::new(
1992 array
1993 .as_primitive::<TimestampMicrosecondType>()
1994 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1995 Ok(time_to_time32ms(as_time_res_with_timezone::<
1996 TimestampMicrosecondType,
1997 >(x, tz)?))
1998 })?,
1999 ))
2000 }
2001 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2002 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2003 Ok(Arc::new(
2004 array
2005 .as_primitive::<TimestampNanosecondType>()
2006 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2007 Ok(time_to_time32s(as_time_res_with_timezone::<
2008 TimestampNanosecondType,
2009 >(x, tz)?))
2010 })?,
2011 ))
2012 }
2013 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2014 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2015 Ok(Arc::new(
2016 array
2017 .as_primitive::<TimestampNanosecondType>()
2018 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2019 Ok(time_to_time32ms(as_time_res_with_timezone::<
2020 TimestampNanosecondType,
2021 >(x, tz)?))
2022 })?,
2023 ))
2024 }
2025 (Date64, Timestamp(TimeUnit::Second, _)) => {
2026 let array = array
2027 .as_primitive::<Date64Type>()
2028 .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2029
2030 cast_with_options(&array, to_type, cast_options)
2031 }
2032 (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2033 let array = array
2034 .as_primitive::<Date64Type>()
2035 .reinterpret_cast::<TimestampMillisecondType>();
2036
2037 cast_with_options(&array, to_type, cast_options)
2038 }
2039
2040 (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2041 let array = array
2042 .as_primitive::<Date64Type>()
2043 .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2044
2045 cast_with_options(&array, to_type, cast_options)
2046 }
2047 (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2048 let array = array
2049 .as_primitive::<Date64Type>()
2050 .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2051
2052 cast_with_options(&array, to_type, cast_options)
2053 }
2054 (Date32, Timestamp(TimeUnit::Second, _)) => {
2055 let array = array
2056 .as_primitive::<Date32Type>()
2057 .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2058
2059 cast_with_options(&array, to_type, cast_options)
2060 }
2061 (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2062 let array = array
2063 .as_primitive::<Date32Type>()
2064 .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2065
2066 cast_with_options(&array, to_type, cast_options)
2067 }
2068 (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2069 let array = array
2070 .as_primitive::<Date32Type>()
2071 .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2072
2073 cast_with_options(&array, to_type, cast_options)
2074 }
2075 (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2076 let array = array
2077 .as_primitive::<Date32Type>()
2078 .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2079
2080 cast_with_options(&array, to_type, cast_options)
2081 }
2082
2083 (_, Duration(unit)) if from_type.is_numeric() => {
2084 let array = cast_with_options(array, &Int64, cast_options)?;
2085 Ok(make_duration_array(array.as_primitive(), *unit))
2086 }
2087 (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2088 let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2089 cast_with_options(&array, to_type, cast_options)
2090 }
2091 (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2092 let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2093 cast_with_options(&array, to_type, cast_options)
2094 }
2095 (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2096 let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2097 cast_with_options(&array, to_type, cast_options)
2098 }
2099 (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2100 let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2101 cast_with_options(&array, to_type, cast_options)
2102 }
2103
2104 (Duration(from_unit), Duration(to_unit)) => {
2105 let array = cast_with_options(array, &Int64, cast_options)?;
2106 let time_array = array.as_primitive::<Int64Type>();
2107 let from_size = time_unit_multiple(from_unit);
2108 let to_size = time_unit_multiple(to_unit);
2109 let converted = match from_size.cmp(&to_size) {
2112 Ordering::Greater => {
2113 let divisor = from_size / to_size;
2114 time_array.unary::<_, Int64Type>(|o| o / divisor)
2115 }
2116 Ordering::Equal => time_array.clone(),
2117 Ordering::Less => {
2118 let mul = to_size / from_size;
2119 if cast_options.safe {
2120 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2121 } else {
2122 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2123 }
2124 }
2125 };
2126 Ok(make_duration_array(&converted, *to_unit))
2127 }
2128
2129 (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2130 cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2131 }
2132 (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2133 cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2134 }
2135 (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2136 cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2137 }
2138 (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2139 cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2140 }
2141 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2142 cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2143 }
2144 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2145 cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2146 }
2147 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2148 cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2149 }
2150 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2151 cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2152 }
2153 (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2154 cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2155 }
2156 (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2157 cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2158 }
2159 (Int32, Interval(IntervalUnit::YearMonth)) => {
2160 cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2161 }
2162 (_, _) => Err(ArrowError::CastError(format!(
2163 "Casting from {from_type:?} to {to_type:?} not supported",
2164 ))),
2165 }
2166}
2167
2168fn cast_from_decimal<D, F>(
2169 array: &dyn Array,
2170 base: D::Native,
2171 scale: &i8,
2172 from_type: &DataType,
2173 to_type: &DataType,
2174 as_float: F,
2175 cast_options: &CastOptions,
2176) -> Result<ArrayRef, ArrowError>
2177where
2178 D: DecimalType + ArrowPrimitiveType,
2179 <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2180 F: Fn(D::Native) -> f64,
2181{
2182 use DataType::*;
2183 match to_type {
2185 UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2186 UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2187 UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2188 UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2189 Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2190 Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2191 Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2192 Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2193 Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2194 (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2195 }),
2196 Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2197 as_float(x) / 10_f64.powi(*scale as i32)
2198 }),
2199 Utf8View => value_to_string_view(array, cast_options),
2200 Utf8 => value_to_string::<i32>(array, cast_options),
2201 LargeUtf8 => value_to_string::<i64>(array, cast_options),
2202 Null => Ok(new_null_array(to_type, array.len())),
2203 _ => Err(ArrowError::CastError(format!(
2204 "Casting from {from_type:?} to {to_type:?} not supported"
2205 ))),
2206 }
2207}
2208
2209fn cast_to_decimal<D, M>(
2210 array: &dyn Array,
2211 base: M,
2212 precision: &u8,
2213 scale: &i8,
2214 from_type: &DataType,
2215 to_type: &DataType,
2216 cast_options: &CastOptions,
2217) -> Result<ArrayRef, ArrowError>
2218where
2219 D: DecimalType + ArrowPrimitiveType<Native = M>,
2220 M: ArrowNativeTypeOp + DecimalCast,
2221 u8: num::traits::AsPrimitive<M>,
2222 u16: num::traits::AsPrimitive<M>,
2223 u32: num::traits::AsPrimitive<M>,
2224 u64: num::traits::AsPrimitive<M>,
2225 i8: num::traits::AsPrimitive<M>,
2226 i16: num::traits::AsPrimitive<M>,
2227 i32: num::traits::AsPrimitive<M>,
2228 i64: num::traits::AsPrimitive<M>,
2229{
2230 use DataType::*;
2231 match from_type {
2233 UInt8 => cast_integer_to_decimal::<_, D, M>(
2234 array.as_primitive::<UInt8Type>(),
2235 *precision,
2236 *scale,
2237 base,
2238 cast_options,
2239 ),
2240 UInt16 => cast_integer_to_decimal::<_, D, _>(
2241 array.as_primitive::<UInt16Type>(),
2242 *precision,
2243 *scale,
2244 base,
2245 cast_options,
2246 ),
2247 UInt32 => cast_integer_to_decimal::<_, D, _>(
2248 array.as_primitive::<UInt32Type>(),
2249 *precision,
2250 *scale,
2251 base,
2252 cast_options,
2253 ),
2254 UInt64 => cast_integer_to_decimal::<_, D, _>(
2255 array.as_primitive::<UInt64Type>(),
2256 *precision,
2257 *scale,
2258 base,
2259 cast_options,
2260 ),
2261 Int8 => cast_integer_to_decimal::<_, D, _>(
2262 array.as_primitive::<Int8Type>(),
2263 *precision,
2264 *scale,
2265 base,
2266 cast_options,
2267 ),
2268 Int16 => cast_integer_to_decimal::<_, D, _>(
2269 array.as_primitive::<Int16Type>(),
2270 *precision,
2271 *scale,
2272 base,
2273 cast_options,
2274 ),
2275 Int32 => cast_integer_to_decimal::<_, D, _>(
2276 array.as_primitive::<Int32Type>(),
2277 *precision,
2278 *scale,
2279 base,
2280 cast_options,
2281 ),
2282 Int64 => cast_integer_to_decimal::<_, D, _>(
2283 array.as_primitive::<Int64Type>(),
2284 *precision,
2285 *scale,
2286 base,
2287 cast_options,
2288 ),
2289 Float32 => cast_floating_point_to_decimal::<_, D>(
2290 array.as_primitive::<Float32Type>(),
2291 *precision,
2292 *scale,
2293 cast_options,
2294 ),
2295 Float64 => cast_floating_point_to_decimal::<_, D>(
2296 array.as_primitive::<Float64Type>(),
2297 *precision,
2298 *scale,
2299 cast_options,
2300 ),
2301 Utf8View | Utf8 => {
2302 cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2303 }
2304 LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2305 Null => Ok(new_null_array(to_type, array.len())),
2306 _ => Err(ArrowError::CastError(format!(
2307 "Casting from {from_type:?} to {to_type:?} not supported"
2308 ))),
2309 }
2310}
2311
2312const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2314 match unit {
2315 TimeUnit::Second => 1,
2316 TimeUnit::Millisecond => MILLISECONDS,
2317 TimeUnit::Microsecond => MICROSECONDS,
2318 TimeUnit::Nanosecond => NANOSECONDS,
2319 }
2320}
2321
2322fn cast_numeric_arrays<FROM, TO>(
2324 from: &dyn Array,
2325 cast_options: &CastOptions,
2326) -> Result<ArrayRef, ArrowError>
2327where
2328 FROM: ArrowPrimitiveType,
2329 TO: ArrowPrimitiveType,
2330 FROM::Native: NumCast,
2331 TO::Native: NumCast,
2332{
2333 if cast_options.safe {
2334 Ok(Arc::new(numeric_cast::<FROM, TO>(
2336 from.as_primitive::<FROM>(),
2337 )))
2338 } else {
2339 Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2341 from.as_primitive::<FROM>(),
2342 )?))
2343 }
2344}
2345
2346fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2349where
2350 T: ArrowPrimitiveType,
2351 R: ArrowPrimitiveType,
2352 T::Native: NumCast,
2353 R::Native: NumCast,
2354{
2355 from.try_unary(|value| {
2356 num::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2357 ArrowError::CastError(format!(
2358 "Can't cast value {:?} to type {}",
2359 value,
2360 R::DATA_TYPE
2361 ))
2362 })
2363 })
2364}
2365
2366fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2369where
2370 T: ArrowPrimitiveType,
2371 R: ArrowPrimitiveType,
2372 T::Native: NumCast,
2373 R::Native: NumCast,
2374{
2375 from.unary_opt::<_, R>(num::cast::cast::<T::Native, R::Native>)
2376}
2377
2378fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2379 array: &dyn Array,
2380) -> Result<ArrayRef, ArrowError> {
2381 let array = array.as_primitive::<FROM>();
2382 let size = std::mem::size_of::<FROM::Native>();
2383 let offsets = OffsetBuffer::from_lengths(std::iter::repeat_n(size, array.len()));
2384 Ok(Arc::new(GenericBinaryArray::<O>::new(
2385 offsets,
2386 array.values().inner().clone(),
2387 array.nulls().cloned(),
2388 )))
2389}
2390
2391fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2392 array: PrimitiveArray<Int64Type>,
2393 to_tz: &Tz,
2394 cast_options: &CastOptions,
2395) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2396 let adjust = |o| {
2397 let local = as_datetime::<T>(o)?;
2398 let offset = to_tz.offset_from_local_datetime(&local).single()?;
2399 T::make_value(local - offset.fix())
2400 };
2401 let adjusted = if cast_options.safe {
2402 array.unary_opt::<_, Int64Type>(adjust)
2403 } else {
2404 array.try_unary::<_, Int64Type, _>(|o| {
2405 adjust(o).ok_or_else(|| {
2406 ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2407 })
2408 })?
2409 };
2410 Ok(adjusted)
2411}
2412
2413fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2417where
2418 FROM: ArrowPrimitiveType,
2419{
2420 numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2421}
2422
2423fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2424where
2425 T: ArrowPrimitiveType + ArrowPrimitiveType,
2426{
2427 let mut b = BooleanBuilder::with_capacity(from.len());
2428
2429 for i in 0..from.len() {
2430 if from.is_null(i) {
2431 b.append_null();
2432 } else if from.value(i) != T::default_value() {
2433 b.append_value(true);
2434 } else {
2435 b.append_value(false);
2436 }
2437 }
2438
2439 Ok(b.finish())
2440}
2441
2442fn cast_bool_to_numeric<TO>(
2446 from: &dyn Array,
2447 cast_options: &CastOptions,
2448) -> Result<ArrayRef, ArrowError>
2449where
2450 TO: ArrowPrimitiveType,
2451 TO::Native: num::cast::NumCast,
2452{
2453 Ok(Arc::new(bool_to_numeric_cast::<TO>(
2454 from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2455 cast_options,
2456 )))
2457}
2458
2459fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2460where
2461 T: ArrowPrimitiveType,
2462 T::Native: num::NumCast,
2463{
2464 let iter = (0..from.len()).map(|i| {
2465 if from.is_null(i) {
2466 None
2467 } else if from.value(i) {
2468 num::cast::cast(1)
2470 } else {
2471 Some(T::default_value())
2472 }
2473 });
2474 unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2479}
2480
2481fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2483 array: &dyn Array,
2484 byte_width: i32,
2485 cast_options: &CastOptions,
2486) -> Result<ArrayRef, ArrowError> {
2487 let array = array.as_binary::<O>();
2488 let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2489
2490 for i in 0..array.len() {
2491 if array.is_null(i) {
2492 builder.append_null();
2493 } else {
2494 match builder.append_value(array.value(i)) {
2495 Ok(_) => {}
2496 Err(e) => match cast_options.safe {
2497 true => builder.append_null(),
2498 false => return Err(e),
2499 },
2500 }
2501 }
2502 }
2503
2504 Ok(Arc::new(builder.finish()))
2505}
2506
2507fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2510 array: &dyn Array,
2511 byte_width: i32,
2512) -> Result<ArrayRef, ArrowError> {
2513 let array = array
2514 .as_any()
2515 .downcast_ref::<FixedSizeBinaryArray>()
2516 .unwrap();
2517
2518 let offsets: i128 = byte_width as i128 * array.len() as i128;
2519
2520 let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2521 if is_binary && offsets > i32::MAX as i128 {
2522 return Err(ArrowError::ComputeError(
2523 "FixedSizeBinary array too large to cast to Binary array".to_string(),
2524 ));
2525 } else if !is_binary && offsets > i64::MAX as i128 {
2526 return Err(ArrowError::ComputeError(
2527 "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2528 ));
2529 }
2530
2531 let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2532
2533 for i in 0..array.len() {
2534 if array.is_null(i) {
2535 builder.append_null();
2536 } else {
2537 builder.append_value(array.value(i));
2538 }
2539 }
2540
2541 Ok(Arc::new(builder.finish()))
2542}
2543
2544fn cast_fixed_size_binary_to_binary_view(
2545 array: &dyn Array,
2546 _byte_width: i32,
2547) -> Result<ArrayRef, ArrowError> {
2548 let array = array
2549 .as_any()
2550 .downcast_ref::<FixedSizeBinaryArray>()
2551 .unwrap();
2552
2553 let mut builder = BinaryViewBuilder::with_capacity(array.len());
2554 for i in 0..array.len() {
2555 if array.is_null(i) {
2556 builder.append_null();
2557 } else {
2558 builder.append_value(array.value(i));
2559 }
2560 }
2561
2562 Ok(Arc::new(builder.finish()))
2563}
2564
2565fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2568where
2569 FROM: ByteArrayType,
2570 TO: ByteArrayType<Native = FROM::Native>,
2571 FROM::Offset: OffsetSizeTrait + ToPrimitive,
2572 TO::Offset: OffsetSizeTrait + NumCast,
2573{
2574 let data = array.to_data();
2575 assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2576 let str_values_buf = data.buffers()[1].clone();
2577 let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2578
2579 let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2580 offsets
2581 .iter()
2582 .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2583 let offset =
2584 <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2585 ArrowError::ComputeError(format!(
2586 "{}{} array too large to cast to {}{} array",
2587 FROM::Offset::PREFIX,
2588 FROM::PREFIX,
2589 TO::Offset::PREFIX,
2590 TO::PREFIX
2591 ))
2592 })?;
2593 offset_builder.append(offset);
2594 Ok(())
2595 })?;
2596
2597 let offset_buffer = offset_builder.finish();
2598
2599 let dtype = TO::DATA_TYPE;
2600
2601 let builder = ArrayData::builder(dtype)
2602 .offset(array.offset())
2603 .len(array.len())
2604 .add_buffer(offset_buffer)
2605 .add_buffer(str_values_buf)
2606 .nulls(data.nulls().cloned());
2607
2608 let array_data = unsafe { builder.build_unchecked() };
2609
2610 Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2611}
2612
2613fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2615where
2616 FROM: ByteViewType,
2617 TO: ByteArrayType,
2618 FROM::Native: AsRef<TO::Native>,
2619{
2620 let data = array.to_data();
2621 let view_array = GenericByteViewArray::<FROM>::from(data);
2622
2623 let len = view_array.len();
2624 let bytes = view_array
2625 .views()
2626 .iter()
2627 .map(|v| ByteView::from(*v).length as usize)
2628 .sum::<usize>();
2629
2630 let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2631
2632 for val in view_array.iter() {
2633 byte_array_builder.append_option(val);
2634 }
2635
2636 Ok(Arc::new(byte_array_builder.finish()))
2637}
2638
2639#[cfg(test)]
2640mod tests {
2641 use super::*;
2642 use arrow_buffer::i256;
2643 use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2644 use chrono::NaiveDate;
2645 use half::f16;
2646
2647 #[derive(Clone)]
2648 struct DecimalCastTestConfig {
2649 input_prec: u8,
2650 input_scale: i8,
2651 input_repr: i128,
2652 output_prec: u8,
2653 output_scale: i8,
2654 expected_output_repr: Result<i128, String>, }
2659
2660 macro_rules! generate_cast_test_case {
2661 ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2662 let output =
2663 $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2664
2665 let input_array_type = $INPUT_ARRAY.data_type();
2667 assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2668 let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2669 assert_eq!($OUTPUT_TYPE, result.data_type());
2670 assert_eq!(result.as_ref(), &output);
2671
2672 let cast_option = CastOptions {
2673 safe: false,
2674 format_options: FormatOptions::default(),
2675 };
2676 let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2677 assert_eq!($OUTPUT_TYPE, result.data_type());
2678 assert_eq!(result.as_ref(), &output);
2679 };
2680 }
2681
2682 fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2683 where
2684 I: DecimalType,
2685 O: DecimalType,
2686 I::Native: DecimalCast,
2687 O::Native: DecimalCast,
2688 {
2689 let array = vec![I::Native::from_decimal(t.input_repr)];
2690 let array = array
2691 .into_iter()
2692 .collect::<PrimitiveArray<I>>()
2693 .with_precision_and_scale(t.input_prec, t.input_scale)
2694 .unwrap();
2695 let input_type = array.data_type();
2696 let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2697 assert!(can_cast_types(input_type, &output_type));
2698
2699 let options = CastOptions {
2700 safe: false,
2701 ..Default::default()
2702 };
2703 let result = cast_with_options(&array, &output_type, &options);
2704
2705 match t.expected_output_repr {
2706 Ok(v) => {
2707 let expected_array = vec![O::Native::from_decimal(v)];
2708 let expected_array = expected_array
2709 .into_iter()
2710 .collect::<PrimitiveArray<O>>()
2711 .with_precision_and_scale(t.output_prec, t.output_scale)
2712 .unwrap();
2713 assert_eq!(*result.unwrap(), expected_array);
2714 }
2715 Err(expected_output_message_template) => {
2716 assert!(result.is_err());
2717 let expected_error_message =
2718 expected_output_message_template.replace("{}", O::PREFIX);
2719 assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2720 }
2721 }
2722 }
2723
2724 fn create_decimal32_array(
2725 array: Vec<Option<i32>>,
2726 precision: u8,
2727 scale: i8,
2728 ) -> Result<Decimal32Array, ArrowError> {
2729 array
2730 .into_iter()
2731 .collect::<Decimal32Array>()
2732 .with_precision_and_scale(precision, scale)
2733 }
2734
2735 fn create_decimal64_array(
2736 array: Vec<Option<i64>>,
2737 precision: u8,
2738 scale: i8,
2739 ) -> Result<Decimal64Array, ArrowError> {
2740 array
2741 .into_iter()
2742 .collect::<Decimal64Array>()
2743 .with_precision_and_scale(precision, scale)
2744 }
2745
2746 fn create_decimal128_array(
2747 array: Vec<Option<i128>>,
2748 precision: u8,
2749 scale: i8,
2750 ) -> Result<Decimal128Array, ArrowError> {
2751 array
2752 .into_iter()
2753 .collect::<Decimal128Array>()
2754 .with_precision_and_scale(precision, scale)
2755 }
2756
2757 fn create_decimal256_array(
2758 array: Vec<Option<i256>>,
2759 precision: u8,
2760 scale: i8,
2761 ) -> Result<Decimal256Array, ArrowError> {
2762 array
2763 .into_iter()
2764 .collect::<Decimal256Array>()
2765 .with_precision_and_scale(precision, scale)
2766 }
2767
2768 #[test]
2769 #[cfg(not(feature = "force_validate"))]
2770 #[should_panic(
2771 expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2772 )]
2773 fn test_cast_decimal_to_decimal_round_with_error() {
2774 let array = vec![
2776 Some(i256::from_i128(1123454)),
2777 Some(i256::from_i128(2123456)),
2778 Some(i256::from_i128(-3123453)),
2779 Some(i256::from_i128(-3123456)),
2780 None,
2781 Some(i256::MAX),
2782 Some(i256::MIN),
2783 ];
2784 let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2785 let array = Arc::new(input_decimal_array) as ArrayRef;
2786 let input_type = DataType::Decimal256(76, 4);
2787 let output_type = DataType::Decimal128(20, 3);
2788 assert!(can_cast_types(&input_type, &output_type));
2789 generate_cast_test_case!(
2790 &array,
2791 Decimal128Array,
2792 &output_type,
2793 vec![
2794 Some(112345_i128),
2795 Some(212346_i128),
2796 Some(-312345_i128),
2797 Some(-312346_i128),
2798 None,
2799 None,
2800 None,
2801 ]
2802 );
2803 }
2804
2805 #[test]
2806 #[cfg(not(feature = "force_validate"))]
2807 fn test_cast_decimal_to_decimal_round() {
2808 let array = vec![
2809 Some(1123454),
2810 Some(2123456),
2811 Some(-3123453),
2812 Some(-3123456),
2813 None,
2814 ];
2815 let array = create_decimal128_array(array, 20, 4).unwrap();
2816 let input_type = DataType::Decimal128(20, 4);
2818 let output_type = DataType::Decimal128(20, 3);
2819 assert!(can_cast_types(&input_type, &output_type));
2820 generate_cast_test_case!(
2821 &array,
2822 Decimal128Array,
2823 &output_type,
2824 vec![
2825 Some(112345_i128),
2826 Some(212346_i128),
2827 Some(-312345_i128),
2828 Some(-312346_i128),
2829 None
2830 ]
2831 );
2832
2833 let input_type = DataType::Decimal128(20, 4);
2835 let output_type = DataType::Decimal256(20, 3);
2836 assert!(can_cast_types(&input_type, &output_type));
2837 generate_cast_test_case!(
2838 &array,
2839 Decimal256Array,
2840 &output_type,
2841 vec![
2842 Some(i256::from_i128(112345_i128)),
2843 Some(i256::from_i128(212346_i128)),
2844 Some(i256::from_i128(-312345_i128)),
2845 Some(i256::from_i128(-312346_i128)),
2846 None
2847 ]
2848 );
2849
2850 let array = vec![
2852 Some(i256::from_i128(1123454)),
2853 Some(i256::from_i128(2123456)),
2854 Some(i256::from_i128(-3123453)),
2855 Some(i256::from_i128(-3123456)),
2856 None,
2857 ];
2858 let array = create_decimal256_array(array, 20, 4).unwrap();
2859
2860 let input_type = DataType::Decimal256(20, 4);
2862 let output_type = DataType::Decimal256(20, 3);
2863 assert!(can_cast_types(&input_type, &output_type));
2864 generate_cast_test_case!(
2865 &array,
2866 Decimal256Array,
2867 &output_type,
2868 vec![
2869 Some(i256::from_i128(112345_i128)),
2870 Some(i256::from_i128(212346_i128)),
2871 Some(i256::from_i128(-312345_i128)),
2872 Some(i256::from_i128(-312346_i128)),
2873 None
2874 ]
2875 );
2876 let input_type = DataType::Decimal256(20, 4);
2878 let output_type = DataType::Decimal128(20, 3);
2879 assert!(can_cast_types(&input_type, &output_type));
2880 generate_cast_test_case!(
2881 &array,
2882 Decimal128Array,
2883 &output_type,
2884 vec![
2885 Some(112345_i128),
2886 Some(212346_i128),
2887 Some(-312345_i128),
2888 Some(-312346_i128),
2889 None
2890 ]
2891 );
2892 }
2893
2894 #[test]
2895 fn test_cast_decimal32_to_decimal32() {
2896 let input_type = DataType::Decimal32(9, 3);
2898 let output_type = DataType::Decimal32(9, 4);
2899 assert!(can_cast_types(&input_type, &output_type));
2900 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2901 let array = create_decimal32_array(array, 9, 3).unwrap();
2902 generate_cast_test_case!(
2903 &array,
2904 Decimal32Array,
2905 &output_type,
2906 vec![
2907 Some(11234560_i32),
2908 Some(21234560_i32),
2909 Some(31234560_i32),
2910 None
2911 ]
2912 );
2913 let array = vec![Some(123456), None];
2915 let array = create_decimal32_array(array, 9, 0).unwrap();
2916 let result_safe = cast(&array, &DataType::Decimal32(2, 2));
2917 assert!(result_safe.is_ok());
2918 let options = CastOptions {
2919 safe: false,
2920 ..Default::default()
2921 };
2922
2923 let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
2924 assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal32 of precision 2. Max is 99",
2925 result_unsafe.unwrap_err().to_string());
2926 }
2927
2928 #[test]
2929 fn test_cast_decimal64_to_decimal64() {
2930 let input_type = DataType::Decimal64(17, 3);
2932 let output_type = DataType::Decimal64(17, 4);
2933 assert!(can_cast_types(&input_type, &output_type));
2934 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2935 let array = create_decimal64_array(array, 17, 3).unwrap();
2936 generate_cast_test_case!(
2937 &array,
2938 Decimal64Array,
2939 &output_type,
2940 vec![
2941 Some(11234560_i64),
2942 Some(21234560_i64),
2943 Some(31234560_i64),
2944 None
2945 ]
2946 );
2947 let array = vec![Some(123456), None];
2949 let array = create_decimal64_array(array, 9, 0).unwrap();
2950 let result_safe = cast(&array, &DataType::Decimal64(2, 2));
2951 assert!(result_safe.is_ok());
2952 let options = CastOptions {
2953 safe: false,
2954 ..Default::default()
2955 };
2956
2957 let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
2958 assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal64 of precision 2. Max is 99",
2959 result_unsafe.unwrap_err().to_string());
2960 }
2961
2962 #[test]
2963 fn test_cast_decimal128_to_decimal128() {
2964 let input_type = DataType::Decimal128(20, 3);
2966 let output_type = DataType::Decimal128(20, 4);
2967 assert!(can_cast_types(&input_type, &output_type));
2968 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2969 let array = create_decimal128_array(array, 20, 3).unwrap();
2970 generate_cast_test_case!(
2971 &array,
2972 Decimal128Array,
2973 &output_type,
2974 vec![
2975 Some(11234560_i128),
2976 Some(21234560_i128),
2977 Some(31234560_i128),
2978 None
2979 ]
2980 );
2981 let array = vec![Some(123456), None];
2983 let array = create_decimal128_array(array, 10, 0).unwrap();
2984 let result_safe = cast(&array, &DataType::Decimal128(2, 2));
2985 assert!(result_safe.is_ok());
2986 let options = CastOptions {
2987 safe: false,
2988 ..Default::default()
2989 };
2990
2991 let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
2992 assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal128 of precision 2. Max is 99",
2993 result_unsafe.unwrap_err().to_string());
2994 }
2995
2996 #[test]
2997 fn test_cast_decimal32_to_decimal32_dict() {
2998 let p = 9;
2999 let s = 3;
3000 let input_type = DataType::Decimal32(p, s);
3001 let output_type = DataType::Dictionary(
3002 Box::new(DataType::Int32),
3003 Box::new(DataType::Decimal32(p, s)),
3004 );
3005 assert!(can_cast_types(&input_type, &output_type));
3006 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3007 let array = create_decimal32_array(array, p, s).unwrap();
3008 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3009 assert_eq!(cast_array.data_type(), &output_type);
3010 }
3011
3012 #[test]
3013 fn test_cast_decimal64_to_decimal64_dict() {
3014 let p = 15;
3015 let s = 3;
3016 let input_type = DataType::Decimal64(p, s);
3017 let output_type = DataType::Dictionary(
3018 Box::new(DataType::Int32),
3019 Box::new(DataType::Decimal64(p, s)),
3020 );
3021 assert!(can_cast_types(&input_type, &output_type));
3022 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3023 let array = create_decimal64_array(array, p, s).unwrap();
3024 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3025 assert_eq!(cast_array.data_type(), &output_type);
3026 }
3027
3028 #[test]
3029 fn test_cast_decimal128_to_decimal128_dict() {
3030 let p = 20;
3031 let s = 3;
3032 let input_type = DataType::Decimal128(p, s);
3033 let output_type = DataType::Dictionary(
3034 Box::new(DataType::Int32),
3035 Box::new(DataType::Decimal128(p, s)),
3036 );
3037 assert!(can_cast_types(&input_type, &output_type));
3038 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3039 let array = create_decimal128_array(array, p, s).unwrap();
3040 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3041 assert_eq!(cast_array.data_type(), &output_type);
3042 }
3043
3044 #[test]
3045 fn test_cast_decimal256_to_decimal256_dict() {
3046 let p = 20;
3047 let s = 3;
3048 let input_type = DataType::Decimal256(p, s);
3049 let output_type = DataType::Dictionary(
3050 Box::new(DataType::Int32),
3051 Box::new(DataType::Decimal256(p, s)),
3052 );
3053 assert!(can_cast_types(&input_type, &output_type));
3054 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3055 let array = create_decimal128_array(array, p, s).unwrap();
3056 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3057 assert_eq!(cast_array.data_type(), &output_type);
3058 }
3059
3060 #[test]
3061 fn test_cast_decimal32_to_decimal32_overflow() {
3062 let input_type = DataType::Decimal32(9, 3);
3063 let output_type = DataType::Decimal32(9, 9);
3064 assert!(can_cast_types(&input_type, &output_type));
3065
3066 let array = vec![Some(i32::MAX)];
3067 let array = create_decimal32_array(array, 9, 3).unwrap();
3068 let result = cast_with_options(
3069 &array,
3070 &output_type,
3071 &CastOptions {
3072 safe: false,
3073 format_options: FormatOptions::default(),
3074 },
3075 );
3076 assert_eq!(
3077 "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3078 result.unwrap_err().to_string()
3079 );
3080 }
3081
3082 #[test]
3083 fn test_cast_decimal64_to_decimal64_overflow() {
3084 let input_type = DataType::Decimal64(18, 3);
3085 let output_type = DataType::Decimal64(18, 18);
3086 assert!(can_cast_types(&input_type, &output_type));
3087
3088 let array = vec![Some(i64::MAX)];
3089 let array = create_decimal64_array(array, 18, 3).unwrap();
3090 let result = cast_with_options(
3091 &array,
3092 &output_type,
3093 &CastOptions {
3094 safe: false,
3095 format_options: FormatOptions::default(),
3096 },
3097 );
3098 assert_eq!(
3099 "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3100 result.unwrap_err().to_string()
3101 );
3102 }
3103
3104 #[test]
3105 fn test_cast_floating_to_decimals() {
3106 for output_type in [
3107 DataType::Decimal32(9, 3),
3108 DataType::Decimal64(9, 3),
3109 DataType::Decimal128(9, 3),
3110 DataType::Decimal256(9, 3),
3111 ] {
3112 let input_type = DataType::Float64;
3113 assert!(can_cast_types(&input_type, &output_type));
3114
3115 let array = vec![Some(1.1_f64)];
3116 let array = PrimitiveArray::<Float64Type>::from_iter(array);
3117 let result = cast_with_options(
3118 &array,
3119 &output_type,
3120 &CastOptions {
3121 safe: false,
3122 format_options: FormatOptions::default(),
3123 },
3124 );
3125 assert!(
3126 result.is_ok(),
3127 "Failed to cast to {output_type} with: {}",
3128 result.unwrap_err()
3129 );
3130 }
3131 }
3132
3133 #[test]
3134 fn test_cast_decimal128_to_decimal128_overflow() {
3135 let input_type = DataType::Decimal128(38, 3);
3136 let output_type = DataType::Decimal128(38, 38);
3137 assert!(can_cast_types(&input_type, &output_type));
3138
3139 let array = vec![Some(i128::MAX)];
3140 let array = create_decimal128_array(array, 38, 3).unwrap();
3141 let result = cast_with_options(
3142 &array,
3143 &output_type,
3144 &CastOptions {
3145 safe: false,
3146 format_options: FormatOptions::default(),
3147 },
3148 );
3149 assert_eq!("Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3150 result.unwrap_err().to_string());
3151 }
3152
3153 #[test]
3154 fn test_cast_decimal128_to_decimal256_overflow() {
3155 let input_type = DataType::Decimal128(38, 3);
3156 let output_type = DataType::Decimal256(76, 76);
3157 assert!(can_cast_types(&input_type, &output_type));
3158
3159 let array = vec![Some(i128::MAX)];
3160 let array = create_decimal128_array(array, 38, 3).unwrap();
3161 let result = cast_with_options(
3162 &array,
3163 &output_type,
3164 &CastOptions {
3165 safe: false,
3166 format_options: FormatOptions::default(),
3167 },
3168 );
3169 assert_eq!("Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3170 result.unwrap_err().to_string());
3171 }
3172
3173 #[test]
3174 fn test_cast_decimal32_to_decimal256() {
3175 let input_type = DataType::Decimal32(8, 3);
3176 let output_type = DataType::Decimal256(20, 4);
3177 assert!(can_cast_types(&input_type, &output_type));
3178 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3179 let array = create_decimal32_array(array, 8, 3).unwrap();
3180 generate_cast_test_case!(
3181 &array,
3182 Decimal256Array,
3183 &output_type,
3184 vec![
3185 Some(i256::from_i128(11234560_i128)),
3186 Some(i256::from_i128(21234560_i128)),
3187 Some(i256::from_i128(31234560_i128)),
3188 None
3189 ]
3190 );
3191 }
3192 #[test]
3193 fn test_cast_decimal64_to_decimal256() {
3194 let input_type = DataType::Decimal64(12, 3);
3195 let output_type = DataType::Decimal256(20, 4);
3196 assert!(can_cast_types(&input_type, &output_type));
3197 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3198 let array = create_decimal64_array(array, 12, 3).unwrap();
3199 generate_cast_test_case!(
3200 &array,
3201 Decimal256Array,
3202 &output_type,
3203 vec![
3204 Some(i256::from_i128(11234560_i128)),
3205 Some(i256::from_i128(21234560_i128)),
3206 Some(i256::from_i128(31234560_i128)),
3207 None
3208 ]
3209 );
3210 }
3211 #[test]
3212 fn test_cast_decimal128_to_decimal256() {
3213 let input_type = DataType::Decimal128(20, 3);
3214 let output_type = DataType::Decimal256(20, 4);
3215 assert!(can_cast_types(&input_type, &output_type));
3216 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3217 let array = create_decimal128_array(array, 20, 3).unwrap();
3218 generate_cast_test_case!(
3219 &array,
3220 Decimal256Array,
3221 &output_type,
3222 vec![
3223 Some(i256::from_i128(11234560_i128)),
3224 Some(i256::from_i128(21234560_i128)),
3225 Some(i256::from_i128(31234560_i128)),
3226 None
3227 ]
3228 );
3229 }
3230
3231 #[test]
3232 fn test_cast_decimal256_to_decimal128_overflow() {
3233 let input_type = DataType::Decimal256(76, 5);
3234 let output_type = DataType::Decimal128(38, 7);
3235 assert!(can_cast_types(&input_type, &output_type));
3236 let array = vec![Some(i256::from_i128(i128::MAX))];
3237 let array = create_decimal256_array(array, 76, 5).unwrap();
3238 let result = cast_with_options(
3239 &array,
3240 &output_type,
3241 &CastOptions {
3242 safe: false,
3243 format_options: FormatOptions::default(),
3244 },
3245 );
3246 assert_eq!("Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3247 result.unwrap_err().to_string());
3248 }
3249
3250 #[test]
3251 fn test_cast_decimal256_to_decimal256_overflow() {
3252 let input_type = DataType::Decimal256(76, 5);
3253 let output_type = DataType::Decimal256(76, 55);
3254 assert!(can_cast_types(&input_type, &output_type));
3255 let array = vec![Some(i256::from_i128(i128::MAX))];
3256 let array = create_decimal256_array(array, 76, 5).unwrap();
3257 let result = cast_with_options(
3258 &array,
3259 &output_type,
3260 &CastOptions {
3261 safe: false,
3262 format_options: FormatOptions::default(),
3263 },
3264 );
3265 assert_eq!("Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3266 result.unwrap_err().to_string());
3267 }
3268
3269 #[test]
3270 fn test_cast_decimal256_to_decimal128() {
3271 let input_type = DataType::Decimal256(20, 3);
3272 let output_type = DataType::Decimal128(20, 4);
3273 assert!(can_cast_types(&input_type, &output_type));
3274 let array = vec![
3275 Some(i256::from_i128(1123456)),
3276 Some(i256::from_i128(2123456)),
3277 Some(i256::from_i128(3123456)),
3278 None,
3279 ];
3280 let array = create_decimal256_array(array, 20, 3).unwrap();
3281 generate_cast_test_case!(
3282 &array,
3283 Decimal128Array,
3284 &output_type,
3285 vec![
3286 Some(11234560_i128),
3287 Some(21234560_i128),
3288 Some(31234560_i128),
3289 None
3290 ]
3291 );
3292 }
3293
3294 #[test]
3295 fn test_cast_decimal256_to_decimal256() {
3296 let input_type = DataType::Decimal256(20, 3);
3297 let output_type = DataType::Decimal256(20, 4);
3298 assert!(can_cast_types(&input_type, &output_type));
3299 let array = vec![
3300 Some(i256::from_i128(1123456)),
3301 Some(i256::from_i128(2123456)),
3302 Some(i256::from_i128(3123456)),
3303 None,
3304 ];
3305 let array = create_decimal256_array(array, 20, 3).unwrap();
3306 generate_cast_test_case!(
3307 &array,
3308 Decimal256Array,
3309 &output_type,
3310 vec![
3311 Some(i256::from_i128(11234560_i128)),
3312 Some(i256::from_i128(21234560_i128)),
3313 Some(i256::from_i128(31234560_i128)),
3314 None
3315 ]
3316 );
3317 }
3318
3319 fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3320 where
3321 T: ArrowPrimitiveType + DecimalType,
3322 {
3323 generate_cast_test_case!(
3325 array,
3326 UInt8Array,
3327 &DataType::UInt8,
3328 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3329 );
3330 generate_cast_test_case!(
3332 array,
3333 UInt16Array,
3334 &DataType::UInt16,
3335 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3336 );
3337 generate_cast_test_case!(
3339 array,
3340 UInt32Array,
3341 &DataType::UInt32,
3342 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3343 );
3344 generate_cast_test_case!(
3346 array,
3347 UInt64Array,
3348 &DataType::UInt64,
3349 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3350 );
3351 generate_cast_test_case!(
3353 array,
3354 Int8Array,
3355 &DataType::Int8,
3356 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3357 );
3358 generate_cast_test_case!(
3360 array,
3361 Int16Array,
3362 &DataType::Int16,
3363 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3364 );
3365 generate_cast_test_case!(
3367 array,
3368 Int32Array,
3369 &DataType::Int32,
3370 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3371 );
3372 generate_cast_test_case!(
3374 array,
3375 Int64Array,
3376 &DataType::Int64,
3377 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3378 );
3379 generate_cast_test_case!(
3381 array,
3382 Float32Array,
3383 &DataType::Float32,
3384 vec![
3385 Some(1.25_f32),
3386 Some(2.25_f32),
3387 Some(3.25_f32),
3388 None,
3389 Some(5.25_f32)
3390 ]
3391 );
3392 generate_cast_test_case!(
3394 array,
3395 Float64Array,
3396 &DataType::Float64,
3397 vec![
3398 Some(1.25_f64),
3399 Some(2.25_f64),
3400 Some(3.25_f64),
3401 None,
3402 Some(5.25_f64)
3403 ]
3404 );
3405 }
3406
3407 #[test]
3408 fn test_cast_decimal32_to_numeric() {
3409 let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3410 let array = create_decimal32_array(value_array, 8, 2).unwrap();
3411
3412 generate_decimal_to_numeric_cast_test_case(&array);
3413 }
3414
3415 #[test]
3416 fn test_cast_decimal64_to_numeric() {
3417 let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3418 let array = create_decimal64_array(value_array, 8, 2).unwrap();
3419
3420 generate_decimal_to_numeric_cast_test_case(&array);
3421 }
3422
3423 #[test]
3424 fn test_cast_decimal128_to_numeric() {
3425 let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3426 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3427
3428 generate_decimal_to_numeric_cast_test_case(&array);
3429
3430 let value_array: Vec<Option<i128>> = vec![Some(51300)];
3432 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3433 let casted_array = cast_with_options(
3434 &array,
3435 &DataType::UInt8,
3436 &CastOptions {
3437 safe: false,
3438 format_options: FormatOptions::default(),
3439 },
3440 );
3441 assert_eq!(
3442 "Cast error: value of 513 is out of range UInt8".to_string(),
3443 casted_array.unwrap_err().to_string()
3444 );
3445
3446 let casted_array = cast_with_options(
3447 &array,
3448 &DataType::UInt8,
3449 &CastOptions {
3450 safe: true,
3451 format_options: FormatOptions::default(),
3452 },
3453 );
3454 assert!(casted_array.is_ok());
3455 assert!(casted_array.unwrap().is_null(0));
3456
3457 let value_array: Vec<Option<i128>> = vec![Some(24400)];
3459 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3460 let casted_array = cast_with_options(
3461 &array,
3462 &DataType::Int8,
3463 &CastOptions {
3464 safe: false,
3465 format_options: FormatOptions::default(),
3466 },
3467 );
3468 assert_eq!(
3469 "Cast error: value of 244 is out of range Int8".to_string(),
3470 casted_array.unwrap_err().to_string()
3471 );
3472
3473 let casted_array = cast_with_options(
3474 &array,
3475 &DataType::Int8,
3476 &CastOptions {
3477 safe: true,
3478 format_options: FormatOptions::default(),
3479 },
3480 );
3481 assert!(casted_array.is_ok());
3482 assert!(casted_array.unwrap().is_null(0));
3483
3484 let value_array: Vec<Option<i128>> = vec![
3488 Some(125),
3489 Some(225),
3490 Some(325),
3491 None,
3492 Some(525),
3493 Some(112345678),
3494 Some(112345679),
3495 ];
3496 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3497 generate_cast_test_case!(
3498 &array,
3499 Float32Array,
3500 &DataType::Float32,
3501 vec![
3502 Some(1.25_f32),
3503 Some(2.25_f32),
3504 Some(3.25_f32),
3505 None,
3506 Some(5.25_f32),
3507 Some(1_123_456.7_f32),
3508 Some(1_123_456.7_f32)
3509 ]
3510 );
3511
3512 let value_array: Vec<Option<i128>> = vec![
3515 Some(125),
3516 Some(225),
3517 Some(325),
3518 None,
3519 Some(525),
3520 Some(112345678901234568),
3521 Some(112345678901234560),
3522 ];
3523 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3524 generate_cast_test_case!(
3525 &array,
3526 Float64Array,
3527 &DataType::Float64,
3528 vec![
3529 Some(1.25_f64),
3530 Some(2.25_f64),
3531 Some(3.25_f64),
3532 None,
3533 Some(5.25_f64),
3534 Some(1_123_456_789_012_345.6_f64),
3535 Some(1_123_456_789_012_345.6_f64),
3536 ]
3537 );
3538 }
3539
3540 #[test]
3541 fn test_cast_decimal256_to_numeric() {
3542 let value_array: Vec<Option<i256>> = vec![
3543 Some(i256::from_i128(125)),
3544 Some(i256::from_i128(225)),
3545 Some(i256::from_i128(325)),
3546 None,
3547 Some(i256::from_i128(525)),
3548 ];
3549 let array = create_decimal256_array(value_array, 38, 2).unwrap();
3550 generate_cast_test_case!(
3552 &array,
3553 UInt8Array,
3554 &DataType::UInt8,
3555 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3556 );
3557 generate_cast_test_case!(
3559 &array,
3560 UInt16Array,
3561 &DataType::UInt16,
3562 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3563 );
3564 generate_cast_test_case!(
3566 &array,
3567 UInt32Array,
3568 &DataType::UInt32,
3569 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3570 );
3571 generate_cast_test_case!(
3573 &array,
3574 UInt64Array,
3575 &DataType::UInt64,
3576 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3577 );
3578 generate_cast_test_case!(
3580 &array,
3581 Int8Array,
3582 &DataType::Int8,
3583 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3584 );
3585 generate_cast_test_case!(
3587 &array,
3588 Int16Array,
3589 &DataType::Int16,
3590 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3591 );
3592 generate_cast_test_case!(
3594 &array,
3595 Int32Array,
3596 &DataType::Int32,
3597 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3598 );
3599 generate_cast_test_case!(
3601 &array,
3602 Int64Array,
3603 &DataType::Int64,
3604 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3605 );
3606 generate_cast_test_case!(
3608 &array,
3609 Float32Array,
3610 &DataType::Float32,
3611 vec![
3612 Some(1.25_f32),
3613 Some(2.25_f32),
3614 Some(3.25_f32),
3615 None,
3616 Some(5.25_f32)
3617 ]
3618 );
3619 generate_cast_test_case!(
3621 &array,
3622 Float64Array,
3623 &DataType::Float64,
3624 vec![
3625 Some(1.25_f64),
3626 Some(2.25_f64),
3627 Some(3.25_f64),
3628 None,
3629 Some(5.25_f64)
3630 ]
3631 );
3632
3633 let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3635 let array = create_decimal256_array(value_array, 38, 2).unwrap();
3636 let casted_array = cast_with_options(
3637 &array,
3638 &DataType::Int8,
3639 &CastOptions {
3640 safe: false,
3641 format_options: FormatOptions::default(),
3642 },
3643 );
3644 assert_eq!(
3645 "Cast error: value of 244 is out of range Int8".to_string(),
3646 casted_array.unwrap_err().to_string()
3647 );
3648
3649 let casted_array = cast_with_options(
3650 &array,
3651 &DataType::Int8,
3652 &CastOptions {
3653 safe: true,
3654 format_options: FormatOptions::default(),
3655 },
3656 );
3657 assert!(casted_array.is_ok());
3658 assert!(casted_array.unwrap().is_null(0));
3659
3660 let value_array: Vec<Option<i256>> = vec![
3664 Some(i256::from_i128(125)),
3665 Some(i256::from_i128(225)),
3666 Some(i256::from_i128(325)),
3667 None,
3668 Some(i256::from_i128(525)),
3669 Some(i256::from_i128(112345678)),
3670 Some(i256::from_i128(112345679)),
3671 ];
3672 let array = create_decimal256_array(value_array, 76, 2).unwrap();
3673 generate_cast_test_case!(
3674 &array,
3675 Float32Array,
3676 &DataType::Float32,
3677 vec![
3678 Some(1.25_f32),
3679 Some(2.25_f32),
3680 Some(3.25_f32),
3681 None,
3682 Some(5.25_f32),
3683 Some(1_123_456.7_f32),
3684 Some(1_123_456.7_f32)
3685 ]
3686 );
3687
3688 let value_array: Vec<Option<i256>> = vec![
3691 Some(i256::from_i128(125)),
3692 Some(i256::from_i128(225)),
3693 Some(i256::from_i128(325)),
3694 None,
3695 Some(i256::from_i128(525)),
3696 Some(i256::from_i128(112345678901234568)),
3697 Some(i256::from_i128(112345678901234560)),
3698 ];
3699 let array = create_decimal256_array(value_array, 76, 2).unwrap();
3700 generate_cast_test_case!(
3701 &array,
3702 Float64Array,
3703 &DataType::Float64,
3704 vec![
3705 Some(1.25_f64),
3706 Some(2.25_f64),
3707 Some(3.25_f64),
3708 None,
3709 Some(5.25_f64),
3710 Some(1_123_456_789_012_345.6_f64),
3711 Some(1_123_456_789_012_345.6_f64),
3712 ]
3713 );
3714 }
3715
3716 #[test]
3717 fn test_cast_numeric_to_decimal128() {
3718 let decimal_type = DataType::Decimal128(38, 6);
3719 let input_datas = vec![
3721 Arc::new(UInt8Array::from(vec![
3722 Some(1),
3723 Some(2),
3724 Some(3),
3725 None,
3726 Some(5),
3727 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
3729 Some(1),
3730 Some(2),
3731 Some(3),
3732 None,
3733 Some(5),
3734 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
3736 Some(1),
3737 Some(2),
3738 Some(3),
3739 None,
3740 Some(5),
3741 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
3743 Some(1),
3744 Some(2),
3745 Some(3),
3746 None,
3747 Some(5),
3748 ])) as ArrayRef, ];
3750
3751 for array in input_datas {
3752 generate_cast_test_case!(
3753 &array,
3754 Decimal128Array,
3755 &decimal_type,
3756 vec![
3757 Some(1000000_i128),
3758 Some(2000000_i128),
3759 Some(3000000_i128),
3760 None,
3761 Some(5000000_i128)
3762 ]
3763 );
3764 }
3765
3766 let input_datas = vec![
3768 Arc::new(Int8Array::from(vec![
3769 Some(1),
3770 Some(2),
3771 Some(3),
3772 None,
3773 Some(5),
3774 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
3776 Some(1),
3777 Some(2),
3778 Some(3),
3779 None,
3780 Some(5),
3781 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
3783 Some(1),
3784 Some(2),
3785 Some(3),
3786 None,
3787 Some(5),
3788 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
3790 Some(1),
3791 Some(2),
3792 Some(3),
3793 None,
3794 Some(5),
3795 ])) as ArrayRef, ];
3797 for array in input_datas {
3798 generate_cast_test_case!(
3799 &array,
3800 Decimal128Array,
3801 &decimal_type,
3802 vec![
3803 Some(1000000_i128),
3804 Some(2000000_i128),
3805 Some(3000000_i128),
3806 None,
3807 Some(5000000_i128)
3808 ]
3809 );
3810 }
3811
3812 let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
3815 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3816 assert!(casted_array.is_ok());
3817 let array = casted_array.unwrap();
3818 let array: &Decimal128Array = array.as_primitive();
3819 assert!(array.is_null(4));
3820
3821 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3824 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3825 assert!(casted_array.is_ok());
3826 let array = casted_array.unwrap();
3827 let array: &Decimal128Array = array.as_primitive();
3828 assert!(array.is_null(4));
3829
3830 let array = Float32Array::from(vec![
3832 Some(1.1),
3833 Some(2.2),
3834 Some(4.4),
3835 None,
3836 Some(1.123_456_4), Some(1.123_456_7), ]);
3839 let array = Arc::new(array) as ArrayRef;
3840 generate_cast_test_case!(
3841 &array,
3842 Decimal128Array,
3843 &decimal_type,
3844 vec![
3845 Some(1100000_i128),
3846 Some(2200000_i128),
3847 Some(4400000_i128),
3848 None,
3849 Some(1123456_i128), Some(1123457_i128), ]
3852 );
3853
3854 let array = Float64Array::from(vec![
3856 Some(1.1),
3857 Some(2.2),
3858 Some(4.4),
3859 None,
3860 Some(1.123_456_489_123_4), Some(1.123_456_789_123_4), Some(1.123_456_489_012_345_6), Some(1.123_456_789_012_345_6), ]);
3865 generate_cast_test_case!(
3866 &array,
3867 Decimal128Array,
3868 &decimal_type,
3869 vec![
3870 Some(1100000_i128),
3871 Some(2200000_i128),
3872 Some(4400000_i128),
3873 None,
3874 Some(1123456_i128), Some(1123457_i128), Some(1123456_i128), Some(1123457_i128), ]
3879 );
3880 }
3881
3882 #[test]
3883 fn test_cast_numeric_to_decimal256() {
3884 let decimal_type = DataType::Decimal256(76, 6);
3885 let input_datas = vec![
3887 Arc::new(UInt8Array::from(vec![
3888 Some(1),
3889 Some(2),
3890 Some(3),
3891 None,
3892 Some(5),
3893 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
3895 Some(1),
3896 Some(2),
3897 Some(3),
3898 None,
3899 Some(5),
3900 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
3902 Some(1),
3903 Some(2),
3904 Some(3),
3905 None,
3906 Some(5),
3907 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
3909 Some(1),
3910 Some(2),
3911 Some(3),
3912 None,
3913 Some(5),
3914 ])) as ArrayRef, ];
3916
3917 for array in input_datas {
3918 generate_cast_test_case!(
3919 &array,
3920 Decimal256Array,
3921 &decimal_type,
3922 vec![
3923 Some(i256::from_i128(1000000_i128)),
3924 Some(i256::from_i128(2000000_i128)),
3925 Some(i256::from_i128(3000000_i128)),
3926 None,
3927 Some(i256::from_i128(5000000_i128))
3928 ]
3929 );
3930 }
3931
3932 let input_datas = vec![
3934 Arc::new(Int8Array::from(vec![
3935 Some(1),
3936 Some(2),
3937 Some(3),
3938 None,
3939 Some(5),
3940 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
3942 Some(1),
3943 Some(2),
3944 Some(3),
3945 None,
3946 Some(5),
3947 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
3949 Some(1),
3950 Some(2),
3951 Some(3),
3952 None,
3953 Some(5),
3954 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
3956 Some(1),
3957 Some(2),
3958 Some(3),
3959 None,
3960 Some(5),
3961 ])) as ArrayRef, ];
3963 for array in input_datas {
3964 generate_cast_test_case!(
3965 &array,
3966 Decimal256Array,
3967 &decimal_type,
3968 vec![
3969 Some(i256::from_i128(1000000_i128)),
3970 Some(i256::from_i128(2000000_i128)),
3971 Some(i256::from_i128(3000000_i128)),
3972 None,
3973 Some(i256::from_i128(5000000_i128))
3974 ]
3975 );
3976 }
3977
3978 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3981 let array = Arc::new(array) as ArrayRef;
3982 let casted_array = cast(&array, &DataType::Decimal256(3, 1));
3983 assert!(casted_array.is_ok());
3984 let array = casted_array.unwrap();
3985 let array: &Decimal256Array = array.as_primitive();
3986 assert!(array.is_null(4));
3987
3988 let array = Float32Array::from(vec![
3990 Some(1.1),
3991 Some(2.2),
3992 Some(4.4),
3993 None,
3994 Some(1.123_456_4), Some(1.123_456_7), ]);
3997 generate_cast_test_case!(
3998 &array,
3999 Decimal256Array,
4000 &decimal_type,
4001 vec![
4002 Some(i256::from_i128(1100000_i128)),
4003 Some(i256::from_i128(2200000_i128)),
4004 Some(i256::from_i128(4400000_i128)),
4005 None,
4006 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4009 );
4010
4011 let array = Float64Array::from(vec![
4013 Some(1.1),
4014 Some(2.2),
4015 Some(4.4),
4016 None,
4017 Some(1.123_456_489_123_4), Some(1.123_456_789_123_4), Some(1.123_456_489_012_345_6), Some(1.123_456_789_012_345_6), ]);
4022 generate_cast_test_case!(
4023 &array,
4024 Decimal256Array,
4025 &decimal_type,
4026 vec![
4027 Some(i256::from_i128(1100000_i128)),
4028 Some(i256::from_i128(2200000_i128)),
4029 Some(i256::from_i128(4400000_i128)),
4030 None,
4031 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4036 );
4037 }
4038
4039 #[test]
4040 fn test_cast_i32_to_f64() {
4041 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4042 let b = cast(&array, &DataType::Float64).unwrap();
4043 let c = b.as_primitive::<Float64Type>();
4044 assert_eq!(5.0, c.value(0));
4045 assert_eq!(6.0, c.value(1));
4046 assert_eq!(7.0, c.value(2));
4047 assert_eq!(8.0, c.value(3));
4048 assert_eq!(9.0, c.value(4));
4049 }
4050
4051 #[test]
4052 fn test_cast_i32_to_u8() {
4053 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4054 let b = cast(&array, &DataType::UInt8).unwrap();
4055 let c = b.as_primitive::<UInt8Type>();
4056 assert!(!c.is_valid(0));
4057 assert_eq!(6, c.value(1));
4058 assert!(!c.is_valid(2));
4059 assert_eq!(8, c.value(3));
4060 assert!(!c.is_valid(4));
4062 }
4063
4064 #[test]
4065 #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4066 fn test_cast_int32_to_u8_with_error() {
4067 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4068 let cast_option = CastOptions {
4070 safe: false,
4071 format_options: FormatOptions::default(),
4072 };
4073 let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4074 assert!(result.is_err());
4075 result.unwrap();
4076 }
4077
4078 #[test]
4079 fn test_cast_i32_to_u8_sliced() {
4080 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4081 assert_eq!(0, array.offset());
4082 let array = array.slice(2, 3);
4083 let b = cast(&array, &DataType::UInt8).unwrap();
4084 assert_eq!(3, b.len());
4085 let c = b.as_primitive::<UInt8Type>();
4086 assert!(!c.is_valid(0));
4087 assert_eq!(8, c.value(1));
4088 assert!(!c.is_valid(2));
4090 }
4091
4092 #[test]
4093 fn test_cast_i32_to_i32() {
4094 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4095 let b = cast(&array, &DataType::Int32).unwrap();
4096 let c = b.as_primitive::<Int32Type>();
4097 assert_eq!(5, c.value(0));
4098 assert_eq!(6, c.value(1));
4099 assert_eq!(7, c.value(2));
4100 assert_eq!(8, c.value(3));
4101 assert_eq!(9, c.value(4));
4102 }
4103
4104 #[test]
4105 fn test_cast_i32_to_list_i32() {
4106 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4107 let b = cast(
4108 &array,
4109 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4110 )
4111 .unwrap();
4112 assert_eq!(5, b.len());
4113 let arr = b.as_list::<i32>();
4114 assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4115 assert_eq!(1, arr.value_length(0));
4116 assert_eq!(1, arr.value_length(1));
4117 assert_eq!(1, arr.value_length(2));
4118 assert_eq!(1, arr.value_length(3));
4119 assert_eq!(1, arr.value_length(4));
4120 let c = arr.values().as_primitive::<Int32Type>();
4121 assert_eq!(5, c.value(0));
4122 assert_eq!(6, c.value(1));
4123 assert_eq!(7, c.value(2));
4124 assert_eq!(8, c.value(3));
4125 assert_eq!(9, c.value(4));
4126 }
4127
4128 #[test]
4129 fn test_cast_i32_to_list_i32_nullable() {
4130 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4131 let b = cast(
4132 &array,
4133 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4134 )
4135 .unwrap();
4136 assert_eq!(5, b.len());
4137 assert_eq!(0, b.null_count());
4138 let arr = b.as_list::<i32>();
4139 assert_eq!(&[0, 1, 2, 3, 4, 5], 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 assert_eq!(1, arr.value_length(4));
4145
4146 let c = arr.values().as_primitive::<Int32Type>();
4147 assert_eq!(1, c.null_count());
4148 assert_eq!(5, c.value(0));
4149 assert!(!c.is_valid(1));
4150 assert_eq!(7, c.value(2));
4151 assert_eq!(8, c.value(3));
4152 assert_eq!(9, c.value(4));
4153 }
4154
4155 #[test]
4156 fn test_cast_i32_to_list_f64_nullable_sliced() {
4157 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4158 let array = array.slice(2, 4);
4159 let b = cast(
4160 &array,
4161 &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4162 )
4163 .unwrap();
4164 assert_eq!(4, b.len());
4165 assert_eq!(0, b.null_count());
4166 let arr = b.as_list::<i32>();
4167 assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4168 assert_eq!(1, arr.value_length(0));
4169 assert_eq!(1, arr.value_length(1));
4170 assert_eq!(1, arr.value_length(2));
4171 assert_eq!(1, arr.value_length(3));
4172 let c = arr.values().as_primitive::<Float64Type>();
4173 assert_eq!(1, c.null_count());
4174 assert_eq!(7.0, c.value(0));
4175 assert_eq!(8.0, c.value(1));
4176 assert!(!c.is_valid(2));
4177 assert_eq!(10.0, c.value(3));
4178 }
4179
4180 #[test]
4181 fn test_cast_int_to_utf8view() {
4182 let inputs = vec![
4183 Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4184 Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4185 Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4186 Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4187 Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4188 Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4189 Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4190 Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4191 ];
4192 let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4193 None,
4194 Some("8"),
4195 Some("9"),
4196 Some("10"),
4197 ]));
4198
4199 for array in inputs {
4200 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4201 let arr = cast(&array, &DataType::Utf8View).unwrap();
4202 assert_eq!(expected.as_ref(), arr.as_ref());
4203 }
4204 }
4205
4206 #[test]
4207 fn test_cast_float_to_utf8view() {
4208 let inputs = vec![
4209 Arc::new(Float16Array::from(vec![
4210 Some(f16::from_f64(1.5)),
4211 Some(f16::from_f64(2.5)),
4212 None,
4213 ])) as ArrayRef,
4214 Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4215 Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4216 ];
4217
4218 let expected: ArrayRef =
4219 Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4220
4221 for array in inputs {
4222 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4223 let arr = cast(&array, &DataType::Utf8View).unwrap();
4224 assert_eq!(expected.as_ref(), arr.as_ref());
4225 }
4226 }
4227
4228 #[test]
4229 fn test_cast_utf8_to_i32() {
4230 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4231 let b = cast(&array, &DataType::Int32).unwrap();
4232 let c = b.as_primitive::<Int32Type>();
4233 assert_eq!(5, c.value(0));
4234 assert_eq!(6, c.value(1));
4235 assert!(!c.is_valid(2));
4236 assert_eq!(8, c.value(3));
4237 assert!(!c.is_valid(4));
4238 }
4239
4240 #[test]
4241 fn test_cast_utf8view_to_i32() {
4242 let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4243 let b = cast(&array, &DataType::Int32).unwrap();
4244 let c = b.as_primitive::<Int32Type>();
4245 assert_eq!(5, c.value(0));
4246 assert_eq!(6, c.value(1));
4247 assert!(!c.is_valid(2));
4248 assert_eq!(8, c.value(3));
4249 assert!(!c.is_valid(4));
4250 }
4251
4252 #[test]
4253 fn test_cast_utf8view_to_f32() {
4254 let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4255 let b = cast(&array, &DataType::Float32).unwrap();
4256 let c = b.as_primitive::<Float32Type>();
4257 assert_eq!(3.0, c.value(0));
4258 assert_eq!(4.56, c.value(1));
4259 assert!(!c.is_valid(2));
4260 assert_eq!(8.9, c.value(3));
4261 }
4262
4263 #[test]
4264 fn test_cast_utf8view_to_decimal128() {
4265 let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4266 let arr = Arc::new(array) as ArrayRef;
4267 generate_cast_test_case!(
4268 &arr,
4269 Decimal128Array,
4270 &DataType::Decimal128(4, 2),
4271 vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4272 );
4273 }
4274
4275 #[test]
4276 fn test_cast_with_options_utf8_to_i32() {
4277 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4278 let result = cast_with_options(
4279 &array,
4280 &DataType::Int32,
4281 &CastOptions {
4282 safe: false,
4283 format_options: FormatOptions::default(),
4284 },
4285 );
4286 match result {
4287 Ok(_) => panic!("expected error"),
4288 Err(e) => {
4289 assert!(
4290 e.to_string()
4291 .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4292 "Error: {e}"
4293 )
4294 }
4295 }
4296 }
4297
4298 #[test]
4299 fn test_cast_utf8_to_bool() {
4300 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4301 let casted = cast(&strings, &DataType::Boolean).unwrap();
4302 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4303 assert_eq!(*as_boolean_array(&casted), expected);
4304 }
4305
4306 #[test]
4307 fn test_cast_utf8view_to_bool() {
4308 let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4309 let casted = cast(&strings, &DataType::Boolean).unwrap();
4310 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4311 assert_eq!(*as_boolean_array(&casted), expected);
4312 }
4313
4314 #[test]
4315 fn test_cast_with_options_utf8_to_bool() {
4316 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4317 let casted = cast_with_options(
4318 &strings,
4319 &DataType::Boolean,
4320 &CastOptions {
4321 safe: false,
4322 format_options: FormatOptions::default(),
4323 },
4324 );
4325 match casted {
4326 Ok(_) => panic!("expected error"),
4327 Err(e) => {
4328 assert!(e
4329 .to_string()
4330 .contains("Cast error: Cannot cast value 'invalid' to value of Boolean type"))
4331 }
4332 }
4333 }
4334
4335 #[test]
4336 fn test_cast_bool_to_i32() {
4337 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4338 let b = cast(&array, &DataType::Int32).unwrap();
4339 let c = b.as_primitive::<Int32Type>();
4340 assert_eq!(1, c.value(0));
4341 assert_eq!(0, c.value(1));
4342 assert!(!c.is_valid(2));
4343 }
4344
4345 #[test]
4346 fn test_cast_bool_to_utf8view() {
4347 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4348 let b = cast(&array, &DataType::Utf8View).unwrap();
4349 let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4350 assert_eq!("true", c.value(0));
4351 assert_eq!("false", c.value(1));
4352 assert!(!c.is_valid(2));
4353 }
4354
4355 #[test]
4356 fn test_cast_bool_to_utf8() {
4357 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4358 let b = cast(&array, &DataType::Utf8).unwrap();
4359 let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4360 assert_eq!("true", c.value(0));
4361 assert_eq!("false", c.value(1));
4362 assert!(!c.is_valid(2));
4363 }
4364
4365 #[test]
4366 fn test_cast_bool_to_large_utf8() {
4367 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4368 let b = cast(&array, &DataType::LargeUtf8).unwrap();
4369 let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4370 assert_eq!("true", c.value(0));
4371 assert_eq!("false", c.value(1));
4372 assert!(!c.is_valid(2));
4373 }
4374
4375 #[test]
4376 fn test_cast_bool_to_f64() {
4377 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4378 let b = cast(&array, &DataType::Float64).unwrap();
4379 let c = b.as_primitive::<Float64Type>();
4380 assert_eq!(1.0, c.value(0));
4381 assert_eq!(0.0, c.value(1));
4382 assert!(!c.is_valid(2));
4383 }
4384
4385 #[test]
4386 fn test_cast_integer_to_timestamp() {
4387 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4388 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4389
4390 let array = Int8Array::from(vec![Some(2), Some(10), None]);
4391 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4392
4393 assert_eq!(&actual, &expected);
4394
4395 let array = Int16Array::from(vec![Some(2), Some(10), None]);
4396 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4397
4398 assert_eq!(&actual, &expected);
4399
4400 let array = Int32Array::from(vec![Some(2), Some(10), None]);
4401 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4402
4403 assert_eq!(&actual, &expected);
4404
4405 let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4406 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4407
4408 assert_eq!(&actual, &expected);
4409
4410 let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4411 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4412
4413 assert_eq!(&actual, &expected);
4414
4415 let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4416 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4417
4418 assert_eq!(&actual, &expected);
4419
4420 let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4421 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4422
4423 assert_eq!(&actual, &expected);
4424 }
4425
4426 #[test]
4427 fn test_cast_timestamp_to_integer() {
4428 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4429 .with_timezone("UTC".to_string());
4430 let expected = cast(&array, &DataType::Int64).unwrap();
4431
4432 let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4433 assert_eq!(&actual, &expected);
4434
4435 let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4436 assert_eq!(&actual, &expected);
4437
4438 let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4439 assert_eq!(&actual, &expected);
4440
4441 let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4442 assert_eq!(&actual, &expected);
4443
4444 let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4445 assert_eq!(&actual, &expected);
4446
4447 let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4448 assert_eq!(&actual, &expected);
4449
4450 let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4451 assert_eq!(&actual, &expected);
4452 }
4453
4454 #[test]
4455 fn test_cast_floating_to_timestamp() {
4456 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4457 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4458
4459 let array = Float16Array::from(vec![
4460 Some(f16::from_f32(2.0)),
4461 Some(f16::from_f32(10.6)),
4462 None,
4463 ]);
4464 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4465
4466 assert_eq!(&actual, &expected);
4467
4468 let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4469 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4470
4471 assert_eq!(&actual, &expected);
4472
4473 let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4474 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4475
4476 assert_eq!(&actual, &expected);
4477 }
4478
4479 #[test]
4480 fn test_cast_timestamp_to_floating() {
4481 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4482 .with_timezone("UTC".to_string());
4483 let expected = cast(&array, &DataType::Int64).unwrap();
4484
4485 let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4486 assert_eq!(&actual, &expected);
4487
4488 let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4489 assert_eq!(&actual, &expected);
4490
4491 let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4492 assert_eq!(&actual, &expected);
4493 }
4494
4495 #[test]
4496 fn test_cast_decimal_to_timestamp() {
4497 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4498 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4499
4500 let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4501 .with_precision_and_scale(4, 2)
4502 .unwrap();
4503 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4504
4505 assert_eq!(&actual, &expected);
4506
4507 let array = Decimal256Array::from(vec![
4508 Some(i256::from_i128(2000)),
4509 Some(i256::from_i128(10000)),
4510 None,
4511 ])
4512 .with_precision_and_scale(5, 3)
4513 .unwrap();
4514 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4515
4516 assert_eq!(&actual, &expected);
4517 }
4518
4519 #[test]
4520 fn test_cast_timestamp_to_decimal() {
4521 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4522 .with_timezone("UTC".to_string());
4523 let expected = cast(&array, &DataType::Int64).unwrap();
4524
4525 let actual = cast(
4526 &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4527 &DataType::Int64,
4528 )
4529 .unwrap();
4530 assert_eq!(&actual, &expected);
4531
4532 let actual = cast(
4533 &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4534 &DataType::Int64,
4535 )
4536 .unwrap();
4537 assert_eq!(&actual, &expected);
4538 }
4539
4540 #[test]
4541 fn test_cast_list_i32_to_list_u16() {
4542 let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 100000000]).into_data();
4543
4544 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
4545
4546 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4549 let list_data = ArrayData::builder(list_data_type)
4550 .len(3)
4551 .add_buffer(value_offsets)
4552 .add_child_data(value_data)
4553 .build()
4554 .unwrap();
4555 let list_array = ListArray::from(list_data);
4556
4557 let cast_array = cast(
4558 &list_array,
4559 &DataType::List(Arc::new(Field::new_list_field(DataType::UInt16, true))),
4560 )
4561 .unwrap();
4562
4563 assert_eq!(0, cast_array.null_count());
4568
4569 let array = cast_array.as_list::<i32>();
4571 assert_eq!(list_array.value_offsets(), array.value_offsets());
4572
4573 assert_eq!(DataType::UInt16, array.value_type());
4574 assert_eq!(3, array.value_length(0));
4575 assert_eq!(3, array.value_length(1));
4576 assert_eq!(2, array.value_length(2));
4577
4578 let u16arr = array.values().as_primitive::<UInt16Type>();
4580 assert_eq!(4, u16arr.null_count());
4581
4582 let expected: UInt16Array =
4584 vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4585 .into_iter()
4586 .collect();
4587
4588 assert_eq!(u16arr, &expected);
4589 }
4590
4591 #[test]
4592 fn test_cast_list_i32_to_list_timestamp() {
4593 let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4595
4596 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4597
4598 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4600 let list_data = ArrayData::builder(list_data_type)
4601 .len(3)
4602 .add_buffer(value_offsets)
4603 .add_child_data(value_data)
4604 .build()
4605 .unwrap();
4606 let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4607
4608 let actual = cast(
4609 &list_array,
4610 &DataType::List(Arc::new(Field::new_list_field(
4611 DataType::Timestamp(TimeUnit::Microsecond, None),
4612 true,
4613 ))),
4614 )
4615 .unwrap();
4616
4617 let expected = cast(
4618 &cast(
4619 &list_array,
4620 &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4621 )
4622 .unwrap(),
4623 &DataType::List(Arc::new(Field::new_list_field(
4624 DataType::Timestamp(TimeUnit::Microsecond, None),
4625 true,
4626 ))),
4627 )
4628 .unwrap();
4629
4630 assert_eq!(&actual, &expected);
4631 }
4632
4633 #[test]
4634 fn test_cast_date32_to_date64() {
4635 let a = Date32Array::from(vec![10000, 17890]);
4636 let array = Arc::new(a) as ArrayRef;
4637 let b = cast(&array, &DataType::Date64).unwrap();
4638 let c = b.as_primitive::<Date64Type>();
4639 assert_eq!(864000000000, c.value(0));
4640 assert_eq!(1545696000000, c.value(1));
4641 }
4642
4643 #[test]
4644 fn test_cast_date64_to_date32() {
4645 let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4646 let array = Arc::new(a) as ArrayRef;
4647 let b = cast(&array, &DataType::Date32).unwrap();
4648 let c = b.as_primitive::<Date32Type>();
4649 assert_eq!(10000, c.value(0));
4650 assert_eq!(17890, c.value(1));
4651 assert!(c.is_null(2));
4652 }
4653
4654 #[test]
4655 fn test_cast_string_to_integral_overflow() {
4656 let str = Arc::new(StringArray::from(vec![
4657 Some("123"),
4658 Some("-123"),
4659 Some("86374"),
4660 None,
4661 ])) as ArrayRef;
4662
4663 let options = CastOptions {
4664 safe: true,
4665 format_options: FormatOptions::default(),
4666 };
4667 let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4668 let expected =
4669 Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4670 assert_eq!(&res, &expected);
4671 }
4672
4673 #[test]
4674 fn test_cast_string_to_timestamp() {
4675 let a0 = Arc::new(StringViewArray::from(vec![
4676 Some("2020-09-08T12:00:00.123456789+00:00"),
4677 Some("Not a valid date"),
4678 None,
4679 ])) as ArrayRef;
4680 let a1 = Arc::new(StringArray::from(vec![
4681 Some("2020-09-08T12:00:00.123456789+00:00"),
4682 Some("Not a valid date"),
4683 None,
4684 ])) as ArrayRef;
4685 let a2 = Arc::new(LargeStringArray::from(vec![
4686 Some("2020-09-08T12:00:00.123456789+00:00"),
4687 Some("Not a valid date"),
4688 None,
4689 ])) as ArrayRef;
4690 for array in &[a0, a1, a2] {
4691 for time_unit in &[
4692 TimeUnit::Second,
4693 TimeUnit::Millisecond,
4694 TimeUnit::Microsecond,
4695 TimeUnit::Nanosecond,
4696 ] {
4697 let to_type = DataType::Timestamp(*time_unit, None);
4698 let b = cast(array, &to_type).unwrap();
4699
4700 match time_unit {
4701 TimeUnit::Second => {
4702 let c = b.as_primitive::<TimestampSecondType>();
4703 assert_eq!(1599566400, c.value(0));
4704 assert!(c.is_null(1));
4705 assert!(c.is_null(2));
4706 }
4707 TimeUnit::Millisecond => {
4708 let c = b
4709 .as_any()
4710 .downcast_ref::<TimestampMillisecondArray>()
4711 .unwrap();
4712 assert_eq!(1599566400123, c.value(0));
4713 assert!(c.is_null(1));
4714 assert!(c.is_null(2));
4715 }
4716 TimeUnit::Microsecond => {
4717 let c = b
4718 .as_any()
4719 .downcast_ref::<TimestampMicrosecondArray>()
4720 .unwrap();
4721 assert_eq!(1599566400123456, c.value(0));
4722 assert!(c.is_null(1));
4723 assert!(c.is_null(2));
4724 }
4725 TimeUnit::Nanosecond => {
4726 let c = b
4727 .as_any()
4728 .downcast_ref::<TimestampNanosecondArray>()
4729 .unwrap();
4730 assert_eq!(1599566400123456789, c.value(0));
4731 assert!(c.is_null(1));
4732 assert!(c.is_null(2));
4733 }
4734 }
4735
4736 let options = CastOptions {
4737 safe: false,
4738 format_options: FormatOptions::default(),
4739 };
4740 let err = cast_with_options(array, &to_type, &options).unwrap_err();
4741 assert_eq!(
4742 err.to_string(),
4743 "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4744 );
4745 }
4746 }
4747 }
4748
4749 #[test]
4750 fn test_cast_string_to_timestamp_overflow() {
4751 let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4752 let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4753 let result = result.as_primitive::<TimestampSecondType>();
4754 assert_eq!(result.values(), &[247112596800]);
4755 }
4756
4757 #[test]
4758 fn test_cast_string_to_date32() {
4759 let a0 = Arc::new(StringViewArray::from(vec![
4760 Some("2018-12-25"),
4761 Some("Not a valid date"),
4762 None,
4763 ])) as ArrayRef;
4764 let a1 = Arc::new(StringArray::from(vec![
4765 Some("2018-12-25"),
4766 Some("Not a valid date"),
4767 None,
4768 ])) as ArrayRef;
4769 let a2 = Arc::new(LargeStringArray::from(vec![
4770 Some("2018-12-25"),
4771 Some("Not a valid date"),
4772 None,
4773 ])) as ArrayRef;
4774 for array in &[a0, a1, a2] {
4775 let to_type = DataType::Date32;
4776 let b = cast(array, &to_type).unwrap();
4777 let c = b.as_primitive::<Date32Type>();
4778 assert_eq!(17890, c.value(0));
4779 assert!(c.is_null(1));
4780 assert!(c.is_null(2));
4781
4782 let options = CastOptions {
4783 safe: false,
4784 format_options: FormatOptions::default(),
4785 };
4786 let err = cast_with_options(array, &to_type, &options).unwrap_err();
4787 assert_eq!(
4788 err.to_string(),
4789 "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
4790 );
4791 }
4792 }
4793
4794 #[test]
4795 fn test_cast_string_with_large_date_to_date32() {
4796 let array = Arc::new(StringArray::from(vec![
4797 Some("+10999-12-31"),
4798 Some("-0010-02-28"),
4799 Some("0010-02-28"),
4800 Some("0000-01-01"),
4801 Some("-0000-01-01"),
4802 Some("-0001-01-01"),
4803 ])) as ArrayRef;
4804 let to_type = DataType::Date32;
4805 let options = CastOptions {
4806 safe: false,
4807 format_options: FormatOptions::default(),
4808 };
4809 let b = cast_with_options(&array, &to_type, &options).unwrap();
4810 let c = b.as_primitive::<Date32Type>();
4811 assert_eq!(3298139, c.value(0)); assert_eq!(-723122, c.value(1)); assert_eq!(-715817, c.value(2)); assert_eq!(c.value(3), c.value(4)); assert_eq!(-719528, c.value(3)); assert_eq!(-719528, c.value(4)); assert_eq!(-719893, c.value(5)); }
4819
4820 #[test]
4821 fn test_cast_invalid_string_with_large_date_to_date32() {
4822 let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
4824 let to_type = DataType::Date32;
4825 let options = CastOptions {
4826 safe: false,
4827 format_options: FormatOptions::default(),
4828 };
4829 let err = cast_with_options(&array, &to_type, &options).unwrap_err();
4830 assert_eq!(
4831 err.to_string(),
4832 "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
4833 );
4834 }
4835
4836 #[test]
4837 fn test_cast_string_format_yyyymmdd_to_date32() {
4838 let a0 = Arc::new(StringViewArray::from(vec![
4839 Some("2020-12-25"),
4840 Some("20201117"),
4841 ])) as ArrayRef;
4842 let a1 = Arc::new(StringArray::from(vec![
4843 Some("2020-12-25"),
4844 Some("20201117"),
4845 ])) as ArrayRef;
4846 let a2 = Arc::new(LargeStringArray::from(vec![
4847 Some("2020-12-25"),
4848 Some("20201117"),
4849 ])) as ArrayRef;
4850
4851 for array in &[a0, a1, a2] {
4852 let to_type = DataType::Date32;
4853 let options = CastOptions {
4854 safe: false,
4855 format_options: FormatOptions::default(),
4856 };
4857 let result = cast_with_options(&array, &to_type, &options).unwrap();
4858 let c = result.as_primitive::<Date32Type>();
4859 assert_eq!(
4860 chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
4861 c.value_as_date(0)
4862 );
4863 assert_eq!(
4864 chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
4865 c.value_as_date(1)
4866 );
4867 }
4868 }
4869
4870 #[test]
4871 fn test_cast_string_to_time32second() {
4872 let a0 = Arc::new(StringViewArray::from(vec![
4873 Some("08:08:35.091323414"),
4874 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
4877 None,
4878 ])) as ArrayRef;
4879 let a1 = Arc::new(StringArray::from(vec![
4880 Some("08:08:35.091323414"),
4881 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
4884 None,
4885 ])) as ArrayRef;
4886 let a2 = Arc::new(LargeStringArray::from(vec![
4887 Some("08:08:35.091323414"),
4888 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
4891 None,
4892 ])) as ArrayRef;
4893 for array in &[a0, a1, a2] {
4894 let to_type = DataType::Time32(TimeUnit::Second);
4895 let b = cast(array, &to_type).unwrap();
4896 let c = b.as_primitive::<Time32SecondType>();
4897 assert_eq!(29315, c.value(0));
4898 assert_eq!(29340, c.value(1));
4899 assert!(c.is_null(2));
4900 assert!(c.is_null(3));
4901 assert!(c.is_null(4));
4902
4903 let options = CastOptions {
4904 safe: false,
4905 format_options: FormatOptions::default(),
4906 };
4907 let err = cast_with_options(array, &to_type, &options).unwrap_err();
4908 assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Second) type");
4909 }
4910 }
4911
4912 #[test]
4913 fn test_cast_string_to_time32millisecond() {
4914 let a0 = Arc::new(StringViewArray::from(vec![
4915 Some("08:08:35.091323414"),
4916 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
4919 None,
4920 ])) as ArrayRef;
4921 let a1 = Arc::new(StringArray::from(vec![
4922 Some("08:08:35.091323414"),
4923 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
4926 None,
4927 ])) as ArrayRef;
4928 let a2 = Arc::new(LargeStringArray::from(vec![
4929 Some("08:08:35.091323414"),
4930 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
4933 None,
4934 ])) as ArrayRef;
4935 for array in &[a0, a1, a2] {
4936 let to_type = DataType::Time32(TimeUnit::Millisecond);
4937 let b = cast(array, &to_type).unwrap();
4938 let c = b.as_primitive::<Time32MillisecondType>();
4939 assert_eq!(29315091, c.value(0));
4940 assert_eq!(29340091, c.value(1));
4941 assert!(c.is_null(2));
4942 assert!(c.is_null(3));
4943 assert!(c.is_null(4));
4944
4945 let options = CastOptions {
4946 safe: false,
4947 format_options: FormatOptions::default(),
4948 };
4949 let err = cast_with_options(array, &to_type, &options).unwrap_err();
4950 assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Millisecond) type");
4951 }
4952 }
4953
4954 #[test]
4955 fn test_cast_string_to_time64microsecond() {
4956 let a0 = Arc::new(StringViewArray::from(vec![
4957 Some("08:08:35.091323414"),
4958 Some("Not a valid time"),
4959 None,
4960 ])) as ArrayRef;
4961 let a1 = Arc::new(StringArray::from(vec![
4962 Some("08:08:35.091323414"),
4963 Some("Not a valid time"),
4964 None,
4965 ])) as ArrayRef;
4966 let a2 = Arc::new(LargeStringArray::from(vec![
4967 Some("08:08:35.091323414"),
4968 Some("Not a valid time"),
4969 None,
4970 ])) as ArrayRef;
4971 for array in &[a0, a1, a2] {
4972 let to_type = DataType::Time64(TimeUnit::Microsecond);
4973 let b = cast(array, &to_type).unwrap();
4974 let c = b.as_primitive::<Time64MicrosecondType>();
4975 assert_eq!(29315091323, c.value(0));
4976 assert!(c.is_null(1));
4977 assert!(c.is_null(2));
4978
4979 let options = CastOptions {
4980 safe: false,
4981 format_options: FormatOptions::default(),
4982 };
4983 let err = cast_with_options(array, &to_type, &options).unwrap_err();
4984 assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Microsecond) type");
4985 }
4986 }
4987
4988 #[test]
4989 fn test_cast_string_to_time64nanosecond() {
4990 let a0 = Arc::new(StringViewArray::from(vec![
4991 Some("08:08:35.091323414"),
4992 Some("Not a valid time"),
4993 None,
4994 ])) as ArrayRef;
4995 let a1 = Arc::new(StringArray::from(vec![
4996 Some("08:08:35.091323414"),
4997 Some("Not a valid time"),
4998 None,
4999 ])) as ArrayRef;
5000 let a2 = Arc::new(LargeStringArray::from(vec![
5001 Some("08:08:35.091323414"),
5002 Some("Not a valid time"),
5003 None,
5004 ])) as ArrayRef;
5005 for array in &[a0, a1, a2] {
5006 let to_type = DataType::Time64(TimeUnit::Nanosecond);
5007 let b = cast(array, &to_type).unwrap();
5008 let c = b.as_primitive::<Time64NanosecondType>();
5009 assert_eq!(29315091323414, c.value(0));
5010 assert!(c.is_null(1));
5011 assert!(c.is_null(2));
5012
5013 let options = CastOptions {
5014 safe: false,
5015 format_options: FormatOptions::default(),
5016 };
5017 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5018 assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Nanosecond) type");
5019 }
5020 }
5021
5022 #[test]
5023 fn test_cast_string_to_date64() {
5024 let a0 = Arc::new(StringViewArray::from(vec![
5025 Some("2020-09-08T12:00:00"),
5026 Some("Not a valid date"),
5027 None,
5028 ])) as ArrayRef;
5029 let a1 = Arc::new(StringArray::from(vec![
5030 Some("2020-09-08T12:00:00"),
5031 Some("Not a valid date"),
5032 None,
5033 ])) as ArrayRef;
5034 let a2 = Arc::new(LargeStringArray::from(vec![
5035 Some("2020-09-08T12:00:00"),
5036 Some("Not a valid date"),
5037 None,
5038 ])) as ArrayRef;
5039 for array in &[a0, a1, a2] {
5040 let to_type = DataType::Date64;
5041 let b = cast(array, &to_type).unwrap();
5042 let c = b.as_primitive::<Date64Type>();
5043 assert_eq!(1599566400000, c.value(0));
5044 assert!(c.is_null(1));
5045 assert!(c.is_null(2));
5046
5047 let options = CastOptions {
5048 safe: false,
5049 format_options: FormatOptions::default(),
5050 };
5051 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5052 assert_eq!(
5053 err.to_string(),
5054 "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5055 );
5056 }
5057 }
5058
5059 macro_rules! test_safe_string_to_interval {
5060 ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5061 let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5062
5063 let options = CastOptions {
5064 safe: true,
5065 format_options: FormatOptions::default(),
5066 };
5067
5068 let target_interval_array = cast_with_options(
5069 &source_string_array.clone(),
5070 &DataType::Interval($interval_unit),
5071 &options,
5072 )
5073 .unwrap()
5074 .as_any()
5075 .downcast_ref::<$array_ty>()
5076 .unwrap()
5077 .clone() as $array_ty;
5078
5079 let target_string_array =
5080 cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5081 .unwrap()
5082 .as_any()
5083 .downcast_ref::<StringArray>()
5084 .unwrap()
5085 .clone();
5086
5087 let expect_string_array = StringArray::from($expect_vec);
5088
5089 assert_eq!(target_string_array, expect_string_array);
5090
5091 let target_large_string_array =
5092 cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5093 .unwrap()
5094 .as_any()
5095 .downcast_ref::<LargeStringArray>()
5096 .unwrap()
5097 .clone();
5098
5099 let expect_large_string_array = LargeStringArray::from($expect_vec);
5100
5101 assert_eq!(target_large_string_array, expect_large_string_array);
5102 };
5103 }
5104
5105 #[test]
5106 fn test_cast_string_to_interval_year_month() {
5107 test_safe_string_to_interval!(
5108 vec![
5109 Some("1 year 1 month"),
5110 Some("1.5 years 13 month"),
5111 Some("30 days"),
5112 Some("31 days"),
5113 Some("2 months 31 days"),
5114 Some("2 months 31 days 1 second"),
5115 Some("foobar"),
5116 ],
5117 IntervalUnit::YearMonth,
5118 IntervalYearMonthArray,
5119 vec![
5120 Some("1 years 1 mons"),
5121 Some("2 years 7 mons"),
5122 None,
5123 None,
5124 None,
5125 None,
5126 None,
5127 ]
5128 );
5129 }
5130
5131 #[test]
5132 fn test_cast_string_to_interval_day_time() {
5133 test_safe_string_to_interval!(
5134 vec![
5135 Some("1 year 1 month"),
5136 Some("1.5 years 13 month"),
5137 Some("30 days"),
5138 Some("1 day 2 second 3.5 milliseconds"),
5139 Some("foobar"),
5140 ],
5141 IntervalUnit::DayTime,
5142 IntervalDayTimeArray,
5143 vec![
5144 Some("390 days"),
5145 Some("930 days"),
5146 Some("30 days"),
5147 None,
5148 None,
5149 ]
5150 );
5151 }
5152
5153 #[test]
5154 fn test_cast_string_to_interval_month_day_nano() {
5155 test_safe_string_to_interval!(
5156 vec![
5157 Some("1 year 1 month 1 day"),
5158 None,
5159 Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5160 Some("3 days"),
5161 Some("8 seconds"),
5162 None,
5163 Some("1 day 29800 milliseconds"),
5164 Some("3 months 1 second"),
5165 Some("6 minutes 120 second"),
5166 Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5167 Some("foobar"),
5168 ],
5169 IntervalUnit::MonthDayNano,
5170 IntervalMonthDayNanoArray,
5171 vec![
5172 Some("13 mons 1 days"),
5173 None,
5174 Some("31 mons 35 days 0.001400000 secs"),
5175 Some("3 days"),
5176 Some("8.000000000 secs"),
5177 None,
5178 Some("1 days 29.800000000 secs"),
5179 Some("3 mons 1.000000000 secs"),
5180 Some("8 mins"),
5181 Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5182 None,
5183 ]
5184 );
5185 }
5186
5187 macro_rules! test_unsafe_string_to_interval_err {
5188 ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5189 let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5190 let options = CastOptions {
5191 safe: false,
5192 format_options: FormatOptions::default(),
5193 };
5194 let arrow_err = cast_with_options(
5195 &string_array.clone(),
5196 &DataType::Interval($interval_unit),
5197 &options,
5198 )
5199 .unwrap_err();
5200 assert_eq!($error_msg, arrow_err.to_string());
5201 };
5202 }
5203
5204 #[test]
5205 fn test_cast_string_to_interval_err() {
5206 test_unsafe_string_to_interval_err!(
5207 vec![Some("foobar")],
5208 IntervalUnit::YearMonth,
5209 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5210 );
5211 test_unsafe_string_to_interval_err!(
5212 vec![Some("foobar")],
5213 IntervalUnit::DayTime,
5214 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5215 );
5216 test_unsafe_string_to_interval_err!(
5217 vec![Some("foobar")],
5218 IntervalUnit::MonthDayNano,
5219 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5220 );
5221 test_unsafe_string_to_interval_err!(
5222 vec![Some("2 months 31 days 1 second")],
5223 IntervalUnit::YearMonth,
5224 r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5225 );
5226 test_unsafe_string_to_interval_err!(
5227 vec![Some("1 day 1.5 milliseconds")],
5228 IntervalUnit::DayTime,
5229 r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5230 );
5231
5232 test_unsafe_string_to_interval_err!(
5234 vec![Some(format!(
5235 "{} century {} year {} month",
5236 i64::MAX - 2,
5237 i64::MAX - 2,
5238 i64::MAX - 2
5239 ))],
5240 IntervalUnit::DayTime,
5241 format!(
5242 "Arithmetic overflow: Overflow happened on: {} * 100",
5243 i64::MAX - 2
5244 )
5245 );
5246 test_unsafe_string_to_interval_err!(
5247 vec![Some(format!(
5248 "{} year {} month {} day",
5249 i64::MAX - 2,
5250 i64::MAX - 2,
5251 i64::MAX - 2
5252 ))],
5253 IntervalUnit::MonthDayNano,
5254 format!(
5255 "Arithmetic overflow: Overflow happened on: {} * 12",
5256 i64::MAX - 2
5257 )
5258 );
5259 }
5260
5261 #[test]
5262 fn test_cast_binary_to_fixed_size_binary() {
5263 let bytes_1 = "Hiiii".as_bytes();
5264 let bytes_2 = "Hello".as_bytes();
5265
5266 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5267 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5268 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5269
5270 let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5271 let down_cast = array_ref
5272 .as_any()
5273 .downcast_ref::<FixedSizeBinaryArray>()
5274 .unwrap();
5275 assert_eq!(bytes_1, down_cast.value(0));
5276 assert_eq!(bytes_2, down_cast.value(1));
5277 assert!(down_cast.is_null(2));
5278
5279 let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5280 let down_cast = array_ref
5281 .as_any()
5282 .downcast_ref::<FixedSizeBinaryArray>()
5283 .unwrap();
5284 assert_eq!(bytes_1, down_cast.value(0));
5285 assert_eq!(bytes_2, down_cast.value(1));
5286 assert!(down_cast.is_null(2));
5287
5288 let bytes_1 = "Hi".as_bytes();
5290 let bytes_2 = "Hello".as_bytes();
5291
5292 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5293 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5294 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5295
5296 let array_ref = cast_with_options(
5297 &a1,
5298 &DataType::FixedSizeBinary(5),
5299 &CastOptions {
5300 safe: false,
5301 format_options: FormatOptions::default(),
5302 },
5303 );
5304 assert!(array_ref.is_err());
5305
5306 let array_ref = cast_with_options(
5307 &a2,
5308 &DataType::FixedSizeBinary(5),
5309 &CastOptions {
5310 safe: false,
5311 format_options: FormatOptions::default(),
5312 },
5313 );
5314 assert!(array_ref.is_err());
5315 }
5316
5317 #[test]
5318 fn test_fixed_size_binary_to_binary() {
5319 let bytes_1 = "Hiiii".as_bytes();
5320 let bytes_2 = "Hello".as_bytes();
5321
5322 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5323 let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5324
5325 let array_ref = cast(&a1, &DataType::Binary).unwrap();
5326 let down_cast = array_ref.as_binary::<i32>();
5327 assert_eq!(bytes_1, down_cast.value(0));
5328 assert_eq!(bytes_2, down_cast.value(1));
5329 assert!(down_cast.is_null(2));
5330
5331 let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5332 let down_cast = array_ref.as_binary::<i64>();
5333 assert_eq!(bytes_1, down_cast.value(0));
5334 assert_eq!(bytes_2, down_cast.value(1));
5335 assert!(down_cast.is_null(2));
5336
5337 let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5338 let down_cast = array_ref.as_binary_view();
5339 assert_eq!(bytes_1, down_cast.value(0));
5340 assert_eq!(bytes_2, down_cast.value(1));
5341 assert!(down_cast.is_null(2));
5342 }
5343
5344 #[test]
5345 fn test_fixed_size_binary_to_dictionary() {
5346 let bytes_1 = "Hiiii".as_bytes();
5347 let bytes_2 = "Hello".as_bytes();
5348
5349 let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5350 let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5351
5352 let cast_type = DataType::Dictionary(
5353 Box::new(DataType::Int8),
5354 Box::new(DataType::FixedSizeBinary(5)),
5355 );
5356 let cast_array = cast(&a1, &cast_type).unwrap();
5357 assert_eq!(cast_array.data_type(), &cast_type);
5358 assert_eq!(
5359 array_to_strings(&cast_array),
5360 vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5361 );
5362 let dict_array = cast_array
5364 .as_any()
5365 .downcast_ref::<DictionaryArray<Int8Type>>()
5366 .unwrap();
5367 assert_eq!(dict_array.values().len(), 2);
5368 }
5369
5370 #[test]
5371 fn test_binary_to_dictionary() {
5372 let mut builder = GenericBinaryBuilder::<i32>::new();
5373 builder.append_value(b"hello");
5374 builder.append_value(b"hiiii");
5375 builder.append_value(b"hiiii"); builder.append_null();
5377 builder.append_value(b"rustt");
5378
5379 let a1 = builder.finish();
5380
5381 let cast_type = DataType::Dictionary(
5382 Box::new(DataType::Int8),
5383 Box::new(DataType::FixedSizeBinary(5)),
5384 );
5385 let cast_array = cast(&a1, &cast_type).unwrap();
5386 assert_eq!(cast_array.data_type(), &cast_type);
5387 assert_eq!(
5388 array_to_strings(&cast_array),
5389 vec![
5390 "68656c6c6f",
5391 "6869696969",
5392 "6869696969",
5393 "null",
5394 "7275737474"
5395 ]
5396 );
5397 let dict_array = cast_array
5399 .as_any()
5400 .downcast_ref::<DictionaryArray<Int8Type>>()
5401 .unwrap();
5402 assert_eq!(dict_array.values().len(), 3);
5403 }
5404
5405 #[test]
5406 fn test_numeric_to_binary() {
5407 let a = Int16Array::from(vec![Some(1), Some(511), None]);
5408
5409 let array_ref = cast(&a, &DataType::Binary).unwrap();
5410 let down_cast = array_ref.as_binary::<i32>();
5411 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5412 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5413 assert!(down_cast.is_null(2));
5414
5415 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5416
5417 let array_ref = cast(&a, &DataType::Binary).unwrap();
5418 let down_cast = array_ref.as_binary::<i32>();
5419 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5420 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5421 assert!(down_cast.is_null(2));
5422 }
5423
5424 #[test]
5425 fn test_numeric_to_large_binary() {
5426 let a = Int16Array::from(vec![Some(1), Some(511), None]);
5427
5428 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5429 let down_cast = array_ref.as_binary::<i64>();
5430 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5431 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5432 assert!(down_cast.is_null(2));
5433
5434 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5435
5436 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5437 let down_cast = array_ref.as_binary::<i64>();
5438 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5439 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5440 assert!(down_cast.is_null(2));
5441 }
5442
5443 #[test]
5444 fn test_cast_date32_to_int32() {
5445 let array = Date32Array::from(vec![10000, 17890]);
5446 let b = cast(&array, &DataType::Int32).unwrap();
5447 let c = b.as_primitive::<Int32Type>();
5448 assert_eq!(10000, c.value(0));
5449 assert_eq!(17890, c.value(1));
5450 }
5451
5452 #[test]
5453 fn test_cast_int32_to_date32() {
5454 let array = Int32Array::from(vec![10000, 17890]);
5455 let b = cast(&array, &DataType::Date32).unwrap();
5456 let c = b.as_primitive::<Date32Type>();
5457 assert_eq!(10000, c.value(0));
5458 assert_eq!(17890, c.value(1));
5459 }
5460
5461 #[test]
5462 fn test_cast_timestamp_to_date32() {
5463 let array =
5464 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5465 .with_timezone("+00:00".to_string());
5466 let b = cast(&array, &DataType::Date32).unwrap();
5467 let c = b.as_primitive::<Date32Type>();
5468 assert_eq!(10000, c.value(0));
5469 assert_eq!(17890, c.value(1));
5470 assert!(c.is_null(2));
5471 }
5472 #[test]
5473 fn test_cast_timestamp_to_date32_zone() {
5474 let strings = StringArray::from_iter([
5475 Some("1970-01-01T00:00:01"),
5476 Some("1970-01-01T23:59:59"),
5477 None,
5478 Some("2020-03-01T02:00:23+00:00"),
5479 ]);
5480 let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
5481 let timestamps = cast(&strings, &dt).unwrap();
5482 let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
5483
5484 let c = dates.as_primitive::<Date32Type>();
5485 let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
5486 assert_eq!(c.value_as_date(0).unwrap(), expected);
5487 assert_eq!(c.value_as_date(1).unwrap(), expected);
5488 assert!(c.is_null(2));
5489 let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
5490 assert_eq!(c.value_as_date(3).unwrap(), expected);
5491 }
5492 #[test]
5493 fn test_cast_timestamp_to_date64() {
5494 let array =
5495 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
5496 let b = cast(&array, &DataType::Date64).unwrap();
5497 let c = b.as_primitive::<Date64Type>();
5498 assert_eq!(864000000005, c.value(0));
5499 assert_eq!(1545696000001, c.value(1));
5500 assert!(c.is_null(2));
5501
5502 let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
5503 let b = cast(&array, &DataType::Date64).unwrap();
5504 let c = b.as_primitive::<Date64Type>();
5505 assert_eq!(864000000005000, c.value(0));
5506 assert_eq!(1545696000001000, c.value(1));
5507
5508 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5510 let b = cast(&array, &DataType::Date64).unwrap();
5511 assert!(b.is_null(0));
5512 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5514 let options = CastOptions {
5515 safe: false,
5516 format_options: FormatOptions::default(),
5517 };
5518 let b = cast_with_options(&array, &DataType::Date64, &options);
5519 assert!(b.is_err());
5520 }
5521
5522 #[test]
5523 fn test_cast_timestamp_to_time64() {
5524 let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5526 .with_timezone("+01:00".to_string());
5527 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5528 let c = b.as_primitive::<Time64MicrosecondType>();
5529 assert_eq!(3605000000, c.value(0));
5530 assert_eq!(3601000000, c.value(1));
5531 assert!(c.is_null(2));
5532 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5533 let c = b.as_primitive::<Time64NanosecondType>();
5534 assert_eq!(3605000000000, c.value(0));
5535 assert_eq!(3601000000000, c.value(1));
5536 assert!(c.is_null(2));
5537
5538 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5540 .with_timezone("+01:00".to_string());
5541 let array = Arc::new(a) as ArrayRef;
5542 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5543 let c = b.as_primitive::<Time64MicrosecondType>();
5544 assert_eq!(3605000000, c.value(0));
5545 assert_eq!(3601000000, c.value(1));
5546 assert!(c.is_null(2));
5547 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5548 let c = b.as_primitive::<Time64NanosecondType>();
5549 assert_eq!(3605000000000, c.value(0));
5550 assert_eq!(3601000000000, c.value(1));
5551 assert!(c.is_null(2));
5552
5553 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5555 .with_timezone("+01:00".to_string());
5556 let array = Arc::new(a) as ArrayRef;
5557 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5558 let c = b.as_primitive::<Time64MicrosecondType>();
5559 assert_eq!(3605000000, c.value(0));
5560 assert_eq!(3601000000, c.value(1));
5561 assert!(c.is_null(2));
5562 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5563 let c = b.as_primitive::<Time64NanosecondType>();
5564 assert_eq!(3605000000000, c.value(0));
5565 assert_eq!(3601000000000, c.value(1));
5566 assert!(c.is_null(2));
5567
5568 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5570 .with_timezone("+01:00".to_string());
5571 let array = Arc::new(a) as ArrayRef;
5572 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5573 let c = b.as_primitive::<Time64MicrosecondType>();
5574 assert_eq!(3605000000, c.value(0));
5575 assert_eq!(3601000000, c.value(1));
5576 assert!(c.is_null(2));
5577 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5578 let c = b.as_primitive::<Time64NanosecondType>();
5579 assert_eq!(3605000000000, c.value(0));
5580 assert_eq!(3601000000000, c.value(1));
5581 assert!(c.is_null(2));
5582
5583 let a =
5585 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5586 let array = Arc::new(a) as ArrayRef;
5587 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5588 assert!(b.is_err());
5589 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5590 assert!(b.is_err());
5591 let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5592 assert!(b.is_err());
5593 }
5594
5595 #[test]
5596 fn test_cast_timestamp_to_time32() {
5597 let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5599 .with_timezone("+01:00".to_string());
5600 let array = Arc::new(a) as ArrayRef;
5601 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5602 let c = b.as_primitive::<Time32SecondType>();
5603 assert_eq!(3605, c.value(0));
5604 assert_eq!(3601, c.value(1));
5605 assert!(c.is_null(2));
5606 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5607 let c = b.as_primitive::<Time32MillisecondType>();
5608 assert_eq!(3605000, c.value(0));
5609 assert_eq!(3601000, c.value(1));
5610 assert!(c.is_null(2));
5611
5612 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5614 .with_timezone("+01:00".to_string());
5615 let array = Arc::new(a) as ArrayRef;
5616 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5617 let c = b.as_primitive::<Time32SecondType>();
5618 assert_eq!(3605, c.value(0));
5619 assert_eq!(3601, c.value(1));
5620 assert!(c.is_null(2));
5621 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5622 let c = b.as_primitive::<Time32MillisecondType>();
5623 assert_eq!(3605000, c.value(0));
5624 assert_eq!(3601000, c.value(1));
5625 assert!(c.is_null(2));
5626
5627 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5629 .with_timezone("+01:00".to_string());
5630 let array = Arc::new(a) as ArrayRef;
5631 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5632 let c = b.as_primitive::<Time32SecondType>();
5633 assert_eq!(3605, c.value(0));
5634 assert_eq!(3601, c.value(1));
5635 assert!(c.is_null(2));
5636 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5637 let c = b.as_primitive::<Time32MillisecondType>();
5638 assert_eq!(3605000, c.value(0));
5639 assert_eq!(3601000, c.value(1));
5640 assert!(c.is_null(2));
5641
5642 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5644 .with_timezone("+01:00".to_string());
5645 let array = Arc::new(a) as ArrayRef;
5646 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5647 let c = b.as_primitive::<Time32SecondType>();
5648 assert_eq!(3605, c.value(0));
5649 assert_eq!(3601, c.value(1));
5650 assert!(c.is_null(2));
5651 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5652 let c = b.as_primitive::<Time32MillisecondType>();
5653 assert_eq!(3605000, c.value(0));
5654 assert_eq!(3601000, c.value(1));
5655 assert!(c.is_null(2));
5656
5657 let a =
5659 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5660 let array = Arc::new(a) as ArrayRef;
5661 let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5662 assert!(b.is_err());
5663 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5664 assert!(b.is_err());
5665 }
5666
5667 #[test]
5669 fn test_cast_timestamp_with_timezone_1() {
5670 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5671 Some("2000-01-01T00:00:00.123456789"),
5672 Some("2010-01-01T00:00:00.123456789"),
5673 None,
5674 ]));
5675 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5676 let timestamp_array = cast(&string_array, &to_type).unwrap();
5677
5678 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5679 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
5680
5681 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5682 let result = string_array.as_string::<i32>();
5683 assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
5684 assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5685 assert!(result.is_null(2));
5686 }
5687
5688 #[test]
5690 fn test_cast_timestamp_with_timezone_2() {
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::Millisecond, Some("+0700".into()));
5697 let timestamp_array = cast(&string_array, &to_type).unwrap();
5698
5699 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5701 let result = string_array.as_string::<i32>();
5702 assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
5703 assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5704 assert!(result.is_null(2));
5705
5706 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5707 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
5708
5709 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5710 let result = string_array.as_string::<i32>();
5711 assert_eq!("2000-01-01T00:00:00.123", result.value(0));
5712 assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5713 assert!(result.is_null(2));
5714 }
5715
5716 #[test]
5718 fn test_cast_timestamp_with_timezone_3() {
5719 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5720 Some("2000-01-01T07:00:00.123456789"),
5721 Some("2010-01-01T07:00:00.123456789"),
5722 None,
5723 ]));
5724 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5725 let timestamp_array = cast(&string_array, &to_type).unwrap();
5726
5727 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5729 let result = string_array.as_string::<i32>();
5730 assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5731 assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5732 assert!(result.is_null(2));
5733
5734 let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5735 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
5736
5737 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5738 let result = string_array.as_string::<i32>();
5739 assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5740 assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5741 assert!(result.is_null(2));
5742 }
5743
5744 #[test]
5745 fn test_cast_date64_to_timestamp() {
5746 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5747 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5748 let c = b.as_primitive::<TimestampSecondType>();
5749 assert_eq!(864000000, c.value(0));
5750 assert_eq!(1545696000, c.value(1));
5751 assert!(c.is_null(2));
5752 }
5753
5754 #[test]
5755 fn test_cast_date64_to_timestamp_ms() {
5756 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5757 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
5758 let c = b
5759 .as_any()
5760 .downcast_ref::<TimestampMillisecondArray>()
5761 .unwrap();
5762 assert_eq!(864000000005, c.value(0));
5763 assert_eq!(1545696000001, c.value(1));
5764 assert!(c.is_null(2));
5765 }
5766
5767 #[test]
5768 fn test_cast_date64_to_timestamp_us() {
5769 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5770 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5771 let c = b
5772 .as_any()
5773 .downcast_ref::<TimestampMicrosecondArray>()
5774 .unwrap();
5775 assert_eq!(864000000005000, c.value(0));
5776 assert_eq!(1545696000001000, c.value(1));
5777 assert!(c.is_null(2));
5778 }
5779
5780 #[test]
5781 fn test_cast_date64_to_timestamp_ns() {
5782 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5783 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
5784 let c = b
5785 .as_any()
5786 .downcast_ref::<TimestampNanosecondArray>()
5787 .unwrap();
5788 assert_eq!(864000000005000000, c.value(0));
5789 assert_eq!(1545696000001000000, c.value(1));
5790 assert!(c.is_null(2));
5791 }
5792
5793 #[test]
5794 fn test_cast_timestamp_to_i64() {
5795 let array =
5796 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5797 .with_timezone("UTC".to_string());
5798 let b = cast(&array, &DataType::Int64).unwrap();
5799 let c = b.as_primitive::<Int64Type>();
5800 assert_eq!(&DataType::Int64, c.data_type());
5801 assert_eq!(864000000005, c.value(0));
5802 assert_eq!(1545696000001, c.value(1));
5803 assert!(c.is_null(2));
5804 }
5805
5806 macro_rules! assert_cast {
5807 ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
5808 assert!(can_cast_types($array.data_type(), &$datatype));
5809 let out = cast(&$array, &$datatype).unwrap();
5810 let actual = out
5811 .as_any()
5812 .downcast_ref::<$output_array_type>()
5813 .unwrap()
5814 .into_iter()
5815 .collect::<Vec<_>>();
5816 assert_eq!(actual, $expected);
5817 }};
5818 ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
5819 assert!(can_cast_types($array.data_type(), &$datatype));
5820 let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
5821 let actual = out
5822 .as_any()
5823 .downcast_ref::<$output_array_type>()
5824 .unwrap()
5825 .into_iter()
5826 .collect::<Vec<_>>();
5827 assert_eq!(actual, $expected);
5828 }};
5829 }
5830
5831 #[test]
5832 fn test_cast_date32_to_string() {
5833 let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
5834 let expected = vec![
5835 Some("1970-01-01"),
5836 Some("1997-05-19"),
5837 Some("2005-09-10"),
5838 Some("2018-12-25"),
5839 None,
5840 ];
5841
5842 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
5843 assert_cast!(array, DataType::Utf8, StringArray, expected);
5844 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
5845 }
5846
5847 #[test]
5848 fn test_cast_date64_to_string() {
5849 let array = Date64Array::from(vec![
5850 Some(0),
5851 Some(10000 * 86400000),
5852 Some(13036 * 86400000),
5853 Some(17890 * 86400000),
5854 None,
5855 ]);
5856 let expected = vec![
5857 Some("1970-01-01T00:00:00"),
5858 Some("1997-05-19T00:00:00"),
5859 Some("2005-09-10T00:00:00"),
5860 Some("2018-12-25T00:00:00"),
5861 None,
5862 ];
5863
5864 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
5865 assert_cast!(array, DataType::Utf8, StringArray, expected);
5866 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
5867 }
5868
5869 #[test]
5870 fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
5871 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), None, None]); let array = Arc::new(a) as ArrayRef;
5874
5875 let b = cast(
5876 &array,
5877 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5878 )
5879 .unwrap();
5880 let c = b.as_primitive::<TimestampSecondType>();
5881 let string_array = cast(&c, &DataType::Utf8).unwrap();
5882 let result = string_array.as_string::<i32>();
5883 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5884
5885 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5886 let c = b.as_primitive::<TimestampSecondType>();
5887 let string_array = cast(&c, &DataType::Utf8).unwrap();
5888 let result = string_array.as_string::<i32>();
5889 assert_eq!("2021-01-01T00:00:00", result.value(0));
5890 }
5891
5892 #[test]
5893 fn test_cast_date32_to_timestamp_with_timezone() {
5894 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
5897 let b = cast(
5898 &array,
5899 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5900 )
5901 .unwrap();
5902 let c = b.as_primitive::<TimestampSecondType>();
5903 assert_eq!(1609438500, c.value(0));
5904 assert_eq!(1640974500, c.value(1));
5905 assert!(c.is_null(2));
5906
5907 let string_array = cast(&c, &DataType::Utf8).unwrap();
5908 let result = string_array.as_string::<i32>();
5909 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5910 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5911 }
5912
5913 #[test]
5914 fn test_cast_date32_to_timestamp_with_timezone_ms() {
5915 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
5918 let b = cast(
5919 &array,
5920 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5921 )
5922 .unwrap();
5923 let c = b.as_primitive::<TimestampMillisecondType>();
5924 assert_eq!(1609438500000, c.value(0));
5925 assert_eq!(1640974500000, c.value(1));
5926 assert!(c.is_null(2));
5927
5928 let string_array = cast(&c, &DataType::Utf8).unwrap();
5929 let result = string_array.as_string::<i32>();
5930 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5931 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5932 }
5933
5934 #[test]
5935 fn test_cast_date32_to_timestamp_with_timezone_us() {
5936 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
5939 let b = cast(
5940 &array,
5941 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5942 )
5943 .unwrap();
5944 let c = b.as_primitive::<TimestampMicrosecondType>();
5945 assert_eq!(1609438500000000, c.value(0));
5946 assert_eq!(1640974500000000, c.value(1));
5947 assert!(c.is_null(2));
5948
5949 let string_array = cast(&c, &DataType::Utf8).unwrap();
5950 let result = string_array.as_string::<i32>();
5951 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5952 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5953 }
5954
5955 #[test]
5956 fn test_cast_date32_to_timestamp_with_timezone_ns() {
5957 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
5960 let b = cast(
5961 &array,
5962 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5963 )
5964 .unwrap();
5965 let c = b.as_primitive::<TimestampNanosecondType>();
5966 assert_eq!(1609438500000000000, c.value(0));
5967 assert_eq!(1640974500000000000, c.value(1));
5968 assert!(c.is_null(2));
5969
5970 let string_array = cast(&c, &DataType::Utf8).unwrap();
5971 let result = string_array.as_string::<i32>();
5972 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5973 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5974 }
5975
5976 #[test]
5977 fn test_cast_date64_to_timestamp_with_timezone() {
5978 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5979 let tz = "+0545"; let b = cast(
5981 &array,
5982 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5983 )
5984 .unwrap();
5985
5986 let c = b.as_primitive::<TimestampSecondType>();
5987 assert_eq!(863979300, c.value(0));
5988 assert_eq!(1545675300, c.value(1));
5989 assert!(c.is_null(2));
5990
5991 let string_array = cast(&c, &DataType::Utf8).unwrap();
5992 let result = string_array.as_string::<i32>();
5993 assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
5994 assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
5995 }
5996
5997 #[test]
5998 fn test_cast_date64_to_timestamp_with_timezone_ms() {
5999 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6000 let tz = "+0545"; let b = cast(
6002 &array,
6003 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6004 )
6005 .unwrap();
6006
6007 let c = b.as_primitive::<TimestampMillisecondType>();
6008 assert_eq!(863979300005, c.value(0));
6009 assert_eq!(1545675300001, c.value(1));
6010 assert!(c.is_null(2));
6011
6012 let string_array = cast(&c, &DataType::Utf8).unwrap();
6013 let result = string_array.as_string::<i32>();
6014 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6015 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6016 }
6017
6018 #[test]
6019 fn test_cast_date64_to_timestamp_with_timezone_us() {
6020 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6021 let tz = "+0545"; let b = cast(
6023 &array,
6024 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6025 )
6026 .unwrap();
6027
6028 let c = b.as_primitive::<TimestampMicrosecondType>();
6029 assert_eq!(863979300005000, c.value(0));
6030 assert_eq!(1545675300001000, c.value(1));
6031 assert!(c.is_null(2));
6032
6033 let string_array = cast(&c, &DataType::Utf8).unwrap();
6034 let result = string_array.as_string::<i32>();
6035 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6036 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6037 }
6038
6039 #[test]
6040 fn test_cast_date64_to_timestamp_with_timezone_ns() {
6041 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6042 let tz = "+0545"; let b = cast(
6044 &array,
6045 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6046 )
6047 .unwrap();
6048
6049 let c = b.as_primitive::<TimestampNanosecondType>();
6050 assert_eq!(863979300005000000, c.value(0));
6051 assert_eq!(1545675300001000000, c.value(1));
6052 assert!(c.is_null(2));
6053
6054 let string_array = cast(&c, &DataType::Utf8).unwrap();
6055 let result = string_array.as_string::<i32>();
6056 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6057 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6058 }
6059
6060 #[test]
6061 fn test_cast_timestamp_to_strings() {
6062 let array =
6064 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6065 let expected = vec![
6066 Some("1997-05-19T00:00:03.005"),
6067 Some("2018-12-25T00:00:02.001"),
6068 None,
6069 ];
6070
6071 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6072 assert_cast!(array, DataType::Utf8, StringArray, expected);
6073 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6074 }
6075
6076 #[test]
6077 fn test_cast_timestamp_to_strings_opt() {
6078 let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6079 let tz = "+0545"; let cast_options = CastOptions {
6081 safe: true,
6082 format_options: FormatOptions::default()
6083 .with_timestamp_format(Some(ts_format))
6084 .with_timestamp_tz_format(Some(ts_format)),
6085 };
6086
6087 let array_without_tz =
6089 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6090 let expected = vec![
6091 Some("1997-05-19 00:00:03.005000"),
6092 Some("2018-12-25 00:00:02.001000"),
6093 None,
6094 ];
6095 assert_cast!(
6096 array_without_tz,
6097 DataType::Utf8View,
6098 StringViewArray,
6099 cast_options,
6100 expected
6101 );
6102 assert_cast!(
6103 array_without_tz,
6104 DataType::Utf8,
6105 StringArray,
6106 cast_options,
6107 expected
6108 );
6109 assert_cast!(
6110 array_without_tz,
6111 DataType::LargeUtf8,
6112 LargeStringArray,
6113 cast_options,
6114 expected
6115 );
6116
6117 let array_with_tz =
6118 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6119 .with_timezone(tz.to_string());
6120 let expected = vec![
6121 Some("1997-05-19 05:45:03.005000"),
6122 Some("2018-12-25 05:45:02.001000"),
6123 None,
6124 ];
6125 assert_cast!(
6126 array_with_tz,
6127 DataType::Utf8View,
6128 StringViewArray,
6129 cast_options,
6130 expected
6131 );
6132 assert_cast!(
6133 array_with_tz,
6134 DataType::Utf8,
6135 StringArray,
6136 cast_options,
6137 expected
6138 );
6139 assert_cast!(
6140 array_with_tz,
6141 DataType::LargeUtf8,
6142 LargeStringArray,
6143 cast_options,
6144 expected
6145 );
6146 }
6147
6148 #[test]
6149 fn test_cast_between_timestamps() {
6150 let array =
6151 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6152 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6153 let c = b.as_primitive::<TimestampSecondType>();
6154 assert_eq!(864000003, c.value(0));
6155 assert_eq!(1545696002, c.value(1));
6156 assert!(c.is_null(2));
6157 }
6158
6159 #[test]
6160 fn test_cast_duration_to_i64() {
6161 let base = vec![5, 6, 7, 8, 100000000];
6162
6163 let duration_arrays = vec![
6164 Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6165 Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6166 Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6167 Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6168 ];
6169
6170 for arr in duration_arrays {
6171 assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6172 let result = cast(&arr, &DataType::Int64).unwrap();
6173 let result = result.as_primitive::<Int64Type>();
6174 assert_eq!(base.as_slice(), result.values());
6175 }
6176 }
6177
6178 #[test]
6179 fn test_cast_between_durations_and_numerics() {
6180 fn test_cast_between_durations<FromType, ToType>()
6181 where
6182 FromType: ArrowPrimitiveType<Native = i64>,
6183 ToType: ArrowPrimitiveType<Native = i64>,
6184 PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6185 {
6186 let from_unit = match FromType::DATA_TYPE {
6187 DataType::Duration(unit) => unit,
6188 _ => panic!("Expected a duration type"),
6189 };
6190 let to_unit = match ToType::DATA_TYPE {
6191 DataType::Duration(unit) => unit,
6192 _ => panic!("Expected a duration type"),
6193 };
6194 let from_size = time_unit_multiple(&from_unit);
6195 let to_size = time_unit_multiple(&to_unit);
6196
6197 let (v1_before, v2_before) = (8640003005, 1696002001);
6198 let (v1_after, v2_after) = if from_size >= to_size {
6199 (
6200 v1_before / (from_size / to_size),
6201 v2_before / (from_size / to_size),
6202 )
6203 } else {
6204 (
6205 v1_before * (to_size / from_size),
6206 v2_before * (to_size / from_size),
6207 )
6208 };
6209
6210 let array =
6211 PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6212 let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6213 let c = b.as_primitive::<ToType>();
6214 assert_eq!(v1_after, c.value(0));
6215 assert_eq!(v2_after, c.value(1));
6216 assert!(c.is_null(2));
6217 }
6218
6219 test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6221 test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6222 test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6223 test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6224 test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6225 test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6226 test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6227 test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6228 test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6229 test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6230 test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6231 test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6232
6233 let array = DurationSecondArray::from(vec![
6235 Some(i64::MAX),
6236 Some(8640203410378005),
6237 Some(10241096),
6238 None,
6239 ]);
6240 let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6241 let c = b.as_primitive::<DurationNanosecondType>();
6242 assert!(c.is_null(0));
6243 assert!(c.is_null(1));
6244 assert_eq!(10241096000000000, c.value(2));
6245 assert!(c.is_null(3));
6246
6247 let array = DurationSecondArray::from(vec![
6249 Some(i64::MAX),
6250 Some(8640203410378005),
6251 Some(10241096),
6252 None,
6253 ]);
6254 let b = cast(&array, &DataType::Int64).unwrap();
6255 let c = b.as_primitive::<Int64Type>();
6256 assert_eq!(i64::MAX, c.value(0));
6257 assert_eq!(8640203410378005, c.value(1));
6258 assert_eq!(10241096, c.value(2));
6259 assert!(c.is_null(3));
6260
6261 let b = cast(&array, &DataType::Int32).unwrap();
6262 let c = b.as_primitive::<Int32Type>();
6263 assert_eq!(0, c.value(0));
6264 assert_eq!(0, c.value(1));
6265 assert_eq!(10241096, c.value(2));
6266 assert!(c.is_null(3));
6267
6268 let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6270 let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6271 let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6272 assert_eq!(i32::MAX as i64, c.value(0));
6273 assert_eq!(802034103, c.value(1));
6274 assert_eq!(10241096, c.value(2));
6275 assert!(c.is_null(3));
6276 }
6277
6278 #[test]
6279 fn test_cast_to_strings() {
6280 let a = Int32Array::from(vec![1, 2, 3]);
6281 let out = cast(&a, &DataType::Utf8).unwrap();
6282 let out = out
6283 .as_any()
6284 .downcast_ref::<StringArray>()
6285 .unwrap()
6286 .into_iter()
6287 .collect::<Vec<_>>();
6288 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6289 let out = cast(&a, &DataType::LargeUtf8).unwrap();
6290 let out = out
6291 .as_any()
6292 .downcast_ref::<LargeStringArray>()
6293 .unwrap()
6294 .into_iter()
6295 .collect::<Vec<_>>();
6296 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6297 }
6298
6299 #[test]
6300 fn test_str_to_str_casts() {
6301 for data in [
6302 vec![Some("foo"), Some("bar"), Some("ham")],
6303 vec![Some("foo"), None, Some("bar")],
6304 ] {
6305 let a = LargeStringArray::from(data.clone());
6306 let to = cast(&a, &DataType::Utf8).unwrap();
6307 let expect = a
6308 .as_any()
6309 .downcast_ref::<LargeStringArray>()
6310 .unwrap()
6311 .into_iter()
6312 .collect::<Vec<_>>();
6313 let out = to
6314 .as_any()
6315 .downcast_ref::<StringArray>()
6316 .unwrap()
6317 .into_iter()
6318 .collect::<Vec<_>>();
6319 assert_eq!(expect, out);
6320
6321 let a = StringArray::from(data);
6322 let to = cast(&a, &DataType::LargeUtf8).unwrap();
6323 let expect = a
6324 .as_any()
6325 .downcast_ref::<StringArray>()
6326 .unwrap()
6327 .into_iter()
6328 .collect::<Vec<_>>();
6329 let out = to
6330 .as_any()
6331 .downcast_ref::<LargeStringArray>()
6332 .unwrap()
6333 .into_iter()
6334 .collect::<Vec<_>>();
6335 assert_eq!(expect, out);
6336 }
6337 }
6338
6339 const VIEW_TEST_DATA: [Option<&str>; 5] = [
6340 Some("hello"),
6341 Some("repeated"),
6342 None,
6343 Some("large payload over 12 bytes"),
6344 Some("repeated"),
6345 ];
6346
6347 #[test]
6348 fn test_string_view_to_binary_view() {
6349 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6350
6351 assert!(can_cast_types(
6352 string_view_array.data_type(),
6353 &DataType::BinaryView
6354 ));
6355
6356 let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
6357 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6358
6359 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6360 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6361 }
6362
6363 #[test]
6364 fn test_binary_view_to_string_view() {
6365 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6366
6367 assert!(can_cast_types(
6368 binary_view_array.data_type(),
6369 &DataType::Utf8View
6370 ));
6371
6372 let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
6373 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6374
6375 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6376 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6377 }
6378
6379 #[test]
6380 fn test_string_to_view() {
6381 _test_string_to_view::<i32>();
6382 _test_string_to_view::<i64>();
6383 }
6384
6385 fn _test_string_to_view<O>()
6386 where
6387 O: OffsetSizeTrait,
6388 {
6389 let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6390
6391 assert!(can_cast_types(
6392 string_array.data_type(),
6393 &DataType::Utf8View
6394 ));
6395
6396 assert!(can_cast_types(
6397 string_array.data_type(),
6398 &DataType::BinaryView
6399 ));
6400
6401 let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
6402 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6403
6404 let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
6405 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6406
6407 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6408 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6409
6410 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6411 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6412 }
6413
6414 #[test]
6415 fn test_bianry_to_view() {
6416 _test_binary_to_view::<i32>();
6417 _test_binary_to_view::<i64>();
6418 }
6419
6420 fn _test_binary_to_view<O>()
6421 where
6422 O: OffsetSizeTrait,
6423 {
6424 let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6425
6426 assert!(can_cast_types(
6427 binary_array.data_type(),
6428 &DataType::Utf8View
6429 ));
6430
6431 assert!(can_cast_types(
6432 binary_array.data_type(),
6433 &DataType::BinaryView
6434 ));
6435
6436 let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
6437 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6438
6439 let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
6440 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6441
6442 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6443 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6444
6445 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6446 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6447 }
6448
6449 #[test]
6450 fn test_dict_to_view() {
6451 let values = StringArray::from_iter(VIEW_TEST_DATA);
6452 let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
6453 let string_dict_array =
6454 DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
6455 let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
6456
6457 let string_view_array = {
6458 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for v in typed_dict.into_iter() {
6460 builder.append_option(v);
6461 }
6462 builder.finish()
6463 };
6464 let expected_string_array_type = string_view_array.data_type();
6465 let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
6466 assert_eq!(casted_string_array.data_type(), expected_string_array_type);
6467 assert_eq!(casted_string_array.as_ref(), &string_view_array);
6468
6469 let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
6470 let binary_dict_array =
6471 DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
6472 let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
6473
6474 let binary_view_array = {
6475 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for v in typed_binary_dict.into_iter() {
6477 builder.append_option(v);
6478 }
6479 builder.finish()
6480 };
6481 let expected_binary_array_type = binary_view_array.data_type();
6482 let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
6483 assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
6484 assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
6485 }
6486
6487 #[test]
6488 fn test_view_to_dict() {
6489 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6490 let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
6491 let casted_type = string_dict_array.data_type();
6492 let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
6493 assert_eq!(casted_dict_array.data_type(), casted_type);
6494 assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
6495
6496 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6497 let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
6498 let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
6499 let binary_dict_array =
6500 DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
6501 let casted_type = binary_dict_array.data_type();
6502 let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
6503 assert_eq!(casted_binary_array.data_type(), casted_type);
6504 assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
6505 }
6506
6507 #[test]
6508 fn test_view_to_string() {
6509 _test_view_to_string::<i32>();
6510 _test_view_to_string::<i64>();
6511 }
6512
6513 fn _test_view_to_string<O>()
6514 where
6515 O: OffsetSizeTrait,
6516 {
6517 let string_view_array = {
6518 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for s in VIEW_TEST_DATA.iter() {
6520 builder.append_option(*s);
6521 }
6522 builder.finish()
6523 };
6524
6525 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6526
6527 let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6528 let expected_type = expected_string_array.data_type();
6529
6530 assert!(can_cast_types(string_view_array.data_type(), expected_type));
6531 assert!(can_cast_types(binary_view_array.data_type(), expected_type));
6532
6533 let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
6534 assert_eq!(string_view_casted_array.data_type(), expected_type);
6535 assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
6536
6537 let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
6538 assert_eq!(binary_view_casted_array.data_type(), expected_type);
6539 assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
6540 }
6541
6542 #[test]
6543 fn test_view_to_binary() {
6544 _test_view_to_binary::<i32>();
6545 _test_view_to_binary::<i64>();
6546 }
6547
6548 fn _test_view_to_binary<O>()
6549 where
6550 O: OffsetSizeTrait,
6551 {
6552 let view_array = {
6553 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for s in VIEW_TEST_DATA.iter() {
6555 builder.append_option(*s);
6556 }
6557 builder.finish()
6558 };
6559
6560 let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6561 let expected_type = expected_binary_array.data_type();
6562
6563 assert!(can_cast_types(view_array.data_type(), expected_type));
6564
6565 let binary_array = cast(&view_array, expected_type).unwrap();
6566 assert_eq!(binary_array.data_type(), expected_type);
6567
6568 assert_eq!(binary_array.as_ref(), &expected_binary_array);
6569 }
6570
6571 #[test]
6572 fn test_cast_from_f64() {
6573 let f64_values: Vec<f64> = vec![
6574 i64::MIN as f64,
6575 i32::MIN as f64,
6576 i16::MIN as f64,
6577 i8::MIN as f64,
6578 0_f64,
6579 u8::MAX as f64,
6580 u16::MAX as f64,
6581 u32::MAX as f64,
6582 u64::MAX as f64,
6583 ];
6584 let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6585
6586 let f64_expected = vec![
6587 -9223372036854776000.0,
6588 -2147483648.0,
6589 -32768.0,
6590 -128.0,
6591 0.0,
6592 255.0,
6593 65535.0,
6594 4294967295.0,
6595 18446744073709552000.0,
6596 ];
6597 assert_eq!(
6598 f64_expected,
6599 get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6600 .iter()
6601 .map(|i| i.parse::<f64>().unwrap())
6602 .collect::<Vec<f64>>()
6603 );
6604
6605 let f32_expected = vec![
6606 -9223372000000000000.0,
6607 -2147483600.0,
6608 -32768.0,
6609 -128.0,
6610 0.0,
6611 255.0,
6612 65535.0,
6613 4294967300.0,
6614 18446744000000000000.0,
6615 ];
6616 assert_eq!(
6617 f32_expected,
6618 get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6619 .iter()
6620 .map(|i| i.parse::<f32>().unwrap())
6621 .collect::<Vec<f32>>()
6622 );
6623
6624 let f16_expected = vec![
6625 f16::from_f64(-9223372000000000000.0),
6626 f16::from_f64(-2147483600.0),
6627 f16::from_f64(-32768.0),
6628 f16::from_f64(-128.0),
6629 f16::from_f64(0.0),
6630 f16::from_f64(255.0),
6631 f16::from_f64(65535.0),
6632 f16::from_f64(4294967300.0),
6633 f16::from_f64(18446744000000000000.0),
6634 ];
6635 assert_eq!(
6636 f16_expected,
6637 get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6638 .iter()
6639 .map(|i| i.parse::<f16>().unwrap())
6640 .collect::<Vec<f16>>()
6641 );
6642
6643 let i64_expected = vec![
6644 "-9223372036854775808",
6645 "-2147483648",
6646 "-32768",
6647 "-128",
6648 "0",
6649 "255",
6650 "65535",
6651 "4294967295",
6652 "null",
6653 ];
6654 assert_eq!(
6655 i64_expected,
6656 get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
6657 );
6658
6659 let i32_expected = vec![
6660 "null",
6661 "-2147483648",
6662 "-32768",
6663 "-128",
6664 "0",
6665 "255",
6666 "65535",
6667 "null",
6668 "null",
6669 ];
6670 assert_eq!(
6671 i32_expected,
6672 get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
6673 );
6674
6675 let i16_expected = vec![
6676 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6677 ];
6678 assert_eq!(
6679 i16_expected,
6680 get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
6681 );
6682
6683 let i8_expected = vec![
6684 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6685 ];
6686 assert_eq!(
6687 i8_expected,
6688 get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
6689 );
6690
6691 let u64_expected = vec![
6692 "null",
6693 "null",
6694 "null",
6695 "null",
6696 "0",
6697 "255",
6698 "65535",
6699 "4294967295",
6700 "null",
6701 ];
6702 assert_eq!(
6703 u64_expected,
6704 get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
6705 );
6706
6707 let u32_expected = vec![
6708 "null",
6709 "null",
6710 "null",
6711 "null",
6712 "0",
6713 "255",
6714 "65535",
6715 "4294967295",
6716 "null",
6717 ];
6718 assert_eq!(
6719 u32_expected,
6720 get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
6721 );
6722
6723 let u16_expected = vec![
6724 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6725 ];
6726 assert_eq!(
6727 u16_expected,
6728 get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
6729 );
6730
6731 let u8_expected = vec![
6732 "null", "null", "null", "null", "0", "255", "null", "null", "null",
6733 ];
6734 assert_eq!(
6735 u8_expected,
6736 get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
6737 );
6738 }
6739
6740 #[test]
6741 fn test_cast_from_f32() {
6742 let f32_values: Vec<f32> = vec![
6743 i32::MIN as f32,
6744 i32::MIN as f32,
6745 i16::MIN as f32,
6746 i8::MIN as f32,
6747 0_f32,
6748 u8::MAX as f32,
6749 u16::MAX as f32,
6750 u32::MAX as f32,
6751 u32::MAX as f32,
6752 ];
6753 let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
6754
6755 let f64_expected = vec![
6756 "-2147483648.0",
6757 "-2147483648.0",
6758 "-32768.0",
6759 "-128.0",
6760 "0.0",
6761 "255.0",
6762 "65535.0",
6763 "4294967296.0",
6764 "4294967296.0",
6765 ];
6766 assert_eq!(
6767 f64_expected,
6768 get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
6769 );
6770
6771 let f32_expected = vec![
6772 "-2147483600.0",
6773 "-2147483600.0",
6774 "-32768.0",
6775 "-128.0",
6776 "0.0",
6777 "255.0",
6778 "65535.0",
6779 "4294967300.0",
6780 "4294967300.0",
6781 ];
6782 assert_eq!(
6783 f32_expected,
6784 get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
6785 );
6786
6787 let f16_expected = vec![
6788 "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
6789 ];
6790 assert_eq!(
6791 f16_expected,
6792 get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
6793 );
6794
6795 let i64_expected = vec![
6796 "-2147483648",
6797 "-2147483648",
6798 "-32768",
6799 "-128",
6800 "0",
6801 "255",
6802 "65535",
6803 "4294967296",
6804 "4294967296",
6805 ];
6806 assert_eq!(
6807 i64_expected,
6808 get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
6809 );
6810
6811 let i32_expected = vec![
6812 "-2147483648",
6813 "-2147483648",
6814 "-32768",
6815 "-128",
6816 "0",
6817 "255",
6818 "65535",
6819 "null",
6820 "null",
6821 ];
6822 assert_eq!(
6823 i32_expected,
6824 get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
6825 );
6826
6827 let i16_expected = vec![
6828 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6829 ];
6830 assert_eq!(
6831 i16_expected,
6832 get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
6833 );
6834
6835 let i8_expected = vec![
6836 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6837 ];
6838 assert_eq!(
6839 i8_expected,
6840 get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
6841 );
6842
6843 let u64_expected = vec![
6844 "null",
6845 "null",
6846 "null",
6847 "null",
6848 "0",
6849 "255",
6850 "65535",
6851 "4294967296",
6852 "4294967296",
6853 ];
6854 assert_eq!(
6855 u64_expected,
6856 get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
6857 );
6858
6859 let u32_expected = vec![
6860 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6861 ];
6862 assert_eq!(
6863 u32_expected,
6864 get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
6865 );
6866
6867 let u16_expected = vec![
6868 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6869 ];
6870 assert_eq!(
6871 u16_expected,
6872 get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
6873 );
6874
6875 let u8_expected = vec![
6876 "null", "null", "null", "null", "0", "255", "null", "null", "null",
6877 ];
6878 assert_eq!(
6879 u8_expected,
6880 get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
6881 );
6882 }
6883
6884 #[test]
6885 fn test_cast_from_uint64() {
6886 let u64_values: Vec<u64> = vec![
6887 0,
6888 u8::MAX as u64,
6889 u16::MAX as u64,
6890 u32::MAX as u64,
6891 u64::MAX,
6892 ];
6893 let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
6894
6895 let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
6896 assert_eq!(
6897 f64_expected,
6898 get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
6899 .iter()
6900 .map(|i| i.parse::<f64>().unwrap())
6901 .collect::<Vec<f64>>()
6902 );
6903
6904 let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
6905 assert_eq!(
6906 f32_expected,
6907 get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
6908 .iter()
6909 .map(|i| i.parse::<f32>().unwrap())
6910 .collect::<Vec<f32>>()
6911 );
6912
6913 let f16_expected = vec![
6914 f16::from_f64(0.0),
6915 f16::from_f64(255.0),
6916 f16::from_f64(65535.0),
6917 f16::from_f64(4294967300.0),
6918 f16::from_f64(18446744000000000000.0),
6919 ];
6920 assert_eq!(
6921 f16_expected,
6922 get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
6923 .iter()
6924 .map(|i| i.parse::<f16>().unwrap())
6925 .collect::<Vec<f16>>()
6926 );
6927
6928 let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
6929 assert_eq!(
6930 i64_expected,
6931 get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
6932 );
6933
6934 let i32_expected = vec!["0", "255", "65535", "null", "null"];
6935 assert_eq!(
6936 i32_expected,
6937 get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
6938 );
6939
6940 let i16_expected = vec!["0", "255", "null", "null", "null"];
6941 assert_eq!(
6942 i16_expected,
6943 get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
6944 );
6945
6946 let i8_expected = vec!["0", "null", "null", "null", "null"];
6947 assert_eq!(
6948 i8_expected,
6949 get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
6950 );
6951
6952 let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
6953 assert_eq!(
6954 u64_expected,
6955 get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
6956 );
6957
6958 let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
6959 assert_eq!(
6960 u32_expected,
6961 get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
6962 );
6963
6964 let u16_expected = vec!["0", "255", "65535", "null", "null"];
6965 assert_eq!(
6966 u16_expected,
6967 get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
6968 );
6969
6970 let u8_expected = vec!["0", "255", "null", "null", "null"];
6971 assert_eq!(
6972 u8_expected,
6973 get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
6974 );
6975 }
6976
6977 #[test]
6978 fn test_cast_from_uint32() {
6979 let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
6980 let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
6981
6982 let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
6983 assert_eq!(
6984 f64_expected,
6985 get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
6986 );
6987
6988 let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
6989 assert_eq!(
6990 f32_expected,
6991 get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
6992 );
6993
6994 let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
6995 assert_eq!(
6996 f16_expected,
6997 get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
6998 );
6999
7000 let i64_expected = vec!["0", "255", "65535", "4294967295"];
7001 assert_eq!(
7002 i64_expected,
7003 get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7004 );
7005
7006 let i32_expected = vec!["0", "255", "65535", "null"];
7007 assert_eq!(
7008 i32_expected,
7009 get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7010 );
7011
7012 let i16_expected = vec!["0", "255", "null", "null"];
7013 assert_eq!(
7014 i16_expected,
7015 get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7016 );
7017
7018 let i8_expected = vec!["0", "null", "null", "null"];
7019 assert_eq!(
7020 i8_expected,
7021 get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7022 );
7023
7024 let u64_expected = vec!["0", "255", "65535", "4294967295"];
7025 assert_eq!(
7026 u64_expected,
7027 get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7028 );
7029
7030 let u32_expected = vec!["0", "255", "65535", "4294967295"];
7031 assert_eq!(
7032 u32_expected,
7033 get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7034 );
7035
7036 let u16_expected = vec!["0", "255", "65535", "null"];
7037 assert_eq!(
7038 u16_expected,
7039 get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7040 );
7041
7042 let u8_expected = vec!["0", "255", "null", "null"];
7043 assert_eq!(
7044 u8_expected,
7045 get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7046 );
7047 }
7048
7049 #[test]
7050 fn test_cast_from_uint16() {
7051 let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7052 let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7053
7054 let f64_expected = vec!["0.0", "255.0", "65535.0"];
7055 assert_eq!(
7056 f64_expected,
7057 get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7058 );
7059
7060 let f32_expected = vec!["0.0", "255.0", "65535.0"];
7061 assert_eq!(
7062 f32_expected,
7063 get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7064 );
7065
7066 let f16_expected = vec!["0.0", "255.0", "inf"];
7067 assert_eq!(
7068 f16_expected,
7069 get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7070 );
7071
7072 let i64_expected = vec!["0", "255", "65535"];
7073 assert_eq!(
7074 i64_expected,
7075 get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7076 );
7077
7078 let i32_expected = vec!["0", "255", "65535"];
7079 assert_eq!(
7080 i32_expected,
7081 get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7082 );
7083
7084 let i16_expected = vec!["0", "255", "null"];
7085 assert_eq!(
7086 i16_expected,
7087 get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7088 );
7089
7090 let i8_expected = vec!["0", "null", "null"];
7091 assert_eq!(
7092 i8_expected,
7093 get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7094 );
7095
7096 let u64_expected = vec!["0", "255", "65535"];
7097 assert_eq!(
7098 u64_expected,
7099 get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7100 );
7101
7102 let u32_expected = vec!["0", "255", "65535"];
7103 assert_eq!(
7104 u32_expected,
7105 get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7106 );
7107
7108 let u16_expected = vec!["0", "255", "65535"];
7109 assert_eq!(
7110 u16_expected,
7111 get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7112 );
7113
7114 let u8_expected = vec!["0", "255", "null"];
7115 assert_eq!(
7116 u8_expected,
7117 get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7118 );
7119 }
7120
7121 #[test]
7122 fn test_cast_from_uint8() {
7123 let u8_values: Vec<u8> = vec![0, u8::MAX];
7124 let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7125
7126 let f64_expected = vec!["0.0", "255.0"];
7127 assert_eq!(
7128 f64_expected,
7129 get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7130 );
7131
7132 let f32_expected = vec!["0.0", "255.0"];
7133 assert_eq!(
7134 f32_expected,
7135 get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7136 );
7137
7138 let f16_expected = vec!["0.0", "255.0"];
7139 assert_eq!(
7140 f16_expected,
7141 get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7142 );
7143
7144 let i64_expected = vec!["0", "255"];
7145 assert_eq!(
7146 i64_expected,
7147 get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7148 );
7149
7150 let i32_expected = vec!["0", "255"];
7151 assert_eq!(
7152 i32_expected,
7153 get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7154 );
7155
7156 let i16_expected = vec!["0", "255"];
7157 assert_eq!(
7158 i16_expected,
7159 get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7160 );
7161
7162 let i8_expected = vec!["0", "null"];
7163 assert_eq!(
7164 i8_expected,
7165 get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7166 );
7167
7168 let u64_expected = vec!["0", "255"];
7169 assert_eq!(
7170 u64_expected,
7171 get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7172 );
7173
7174 let u32_expected = vec!["0", "255"];
7175 assert_eq!(
7176 u32_expected,
7177 get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7178 );
7179
7180 let u16_expected = vec!["0", "255"];
7181 assert_eq!(
7182 u16_expected,
7183 get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7184 );
7185
7186 let u8_expected = vec!["0", "255"];
7187 assert_eq!(
7188 u8_expected,
7189 get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7190 );
7191 }
7192
7193 #[test]
7194 fn test_cast_from_int64() {
7195 let i64_values: Vec<i64> = vec![
7196 i64::MIN,
7197 i32::MIN as i64,
7198 i16::MIN as i64,
7199 i8::MIN as i64,
7200 0,
7201 i8::MAX as i64,
7202 i16::MAX as i64,
7203 i32::MAX as i64,
7204 i64::MAX,
7205 ];
7206 let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7207
7208 let f64_expected = vec![
7209 -9223372036854776000.0,
7210 -2147483648.0,
7211 -32768.0,
7212 -128.0,
7213 0.0,
7214 127.0,
7215 32767.0,
7216 2147483647.0,
7217 9223372036854776000.0,
7218 ];
7219 assert_eq!(
7220 f64_expected,
7221 get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7222 .iter()
7223 .map(|i| i.parse::<f64>().unwrap())
7224 .collect::<Vec<f64>>()
7225 );
7226
7227 let f32_expected = vec![
7228 -9223372000000000000.0,
7229 -2147483600.0,
7230 -32768.0,
7231 -128.0,
7232 0.0,
7233 127.0,
7234 32767.0,
7235 2147483600.0,
7236 9223372000000000000.0,
7237 ];
7238 assert_eq!(
7239 f32_expected,
7240 get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7241 .iter()
7242 .map(|i| i.parse::<f32>().unwrap())
7243 .collect::<Vec<f32>>()
7244 );
7245
7246 let f16_expected = vec![
7247 f16::from_f64(-9223372000000000000.0),
7248 f16::from_f64(-2147483600.0),
7249 f16::from_f64(-32768.0),
7250 f16::from_f64(-128.0),
7251 f16::from_f64(0.0),
7252 f16::from_f64(127.0),
7253 f16::from_f64(32767.0),
7254 f16::from_f64(2147483600.0),
7255 f16::from_f64(9223372000000000000.0),
7256 ];
7257 assert_eq!(
7258 f16_expected,
7259 get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7260 .iter()
7261 .map(|i| i.parse::<f16>().unwrap())
7262 .collect::<Vec<f16>>()
7263 );
7264
7265 let i64_expected = vec![
7266 "-9223372036854775808",
7267 "-2147483648",
7268 "-32768",
7269 "-128",
7270 "0",
7271 "127",
7272 "32767",
7273 "2147483647",
7274 "9223372036854775807",
7275 ];
7276 assert_eq!(
7277 i64_expected,
7278 get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
7279 );
7280
7281 let i32_expected = vec![
7282 "null",
7283 "-2147483648",
7284 "-32768",
7285 "-128",
7286 "0",
7287 "127",
7288 "32767",
7289 "2147483647",
7290 "null",
7291 ];
7292 assert_eq!(
7293 i32_expected,
7294 get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
7295 );
7296
7297 assert_eq!(
7298 i32_expected,
7299 get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
7300 );
7301
7302 let i16_expected = vec![
7303 "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
7304 ];
7305 assert_eq!(
7306 i16_expected,
7307 get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
7308 );
7309
7310 let i8_expected = vec![
7311 "null", "null", "null", "-128", "0", "127", "null", "null", "null",
7312 ];
7313 assert_eq!(
7314 i8_expected,
7315 get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
7316 );
7317
7318 let u64_expected = vec![
7319 "null",
7320 "null",
7321 "null",
7322 "null",
7323 "0",
7324 "127",
7325 "32767",
7326 "2147483647",
7327 "9223372036854775807",
7328 ];
7329 assert_eq!(
7330 u64_expected,
7331 get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
7332 );
7333
7334 let u32_expected = vec![
7335 "null",
7336 "null",
7337 "null",
7338 "null",
7339 "0",
7340 "127",
7341 "32767",
7342 "2147483647",
7343 "null",
7344 ];
7345 assert_eq!(
7346 u32_expected,
7347 get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
7348 );
7349
7350 let u16_expected = vec![
7351 "null", "null", "null", "null", "0", "127", "32767", "null", "null",
7352 ];
7353 assert_eq!(
7354 u16_expected,
7355 get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
7356 );
7357
7358 let u8_expected = vec![
7359 "null", "null", "null", "null", "0", "127", "null", "null", "null",
7360 ];
7361 assert_eq!(
7362 u8_expected,
7363 get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
7364 );
7365 }
7366
7367 #[test]
7368 fn test_cast_from_int32() {
7369 let i32_values: Vec<i32> = vec![
7370 i32::MIN,
7371 i16::MIN as i32,
7372 i8::MIN as i32,
7373 0,
7374 i8::MAX as i32,
7375 i16::MAX as i32,
7376 i32::MAX,
7377 ];
7378 let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
7379
7380 let f64_expected = vec![
7381 "-2147483648.0",
7382 "-32768.0",
7383 "-128.0",
7384 "0.0",
7385 "127.0",
7386 "32767.0",
7387 "2147483647.0",
7388 ];
7389 assert_eq!(
7390 f64_expected,
7391 get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
7392 );
7393
7394 let f32_expected = vec![
7395 "-2147483600.0",
7396 "-32768.0",
7397 "-128.0",
7398 "0.0",
7399 "127.0",
7400 "32767.0",
7401 "2147483600.0",
7402 ];
7403 assert_eq!(
7404 f32_expected,
7405 get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
7406 );
7407
7408 let f16_expected = vec![
7409 f16::from_f64(-2147483600.0),
7410 f16::from_f64(-32768.0),
7411 f16::from_f64(-128.0),
7412 f16::from_f64(0.0),
7413 f16::from_f64(127.0),
7414 f16::from_f64(32767.0),
7415 f16::from_f64(2147483600.0),
7416 ];
7417 assert_eq!(
7418 f16_expected,
7419 get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
7420 .iter()
7421 .map(|i| i.parse::<f16>().unwrap())
7422 .collect::<Vec<f16>>()
7423 );
7424
7425 let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
7426 assert_eq!(
7427 i16_expected,
7428 get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
7429 );
7430
7431 let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
7432 assert_eq!(
7433 i8_expected,
7434 get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
7435 );
7436
7437 let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7438 assert_eq!(
7439 u64_expected,
7440 get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
7441 );
7442
7443 let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7444 assert_eq!(
7445 u32_expected,
7446 get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
7447 );
7448
7449 let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
7450 assert_eq!(
7451 u16_expected,
7452 get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
7453 );
7454
7455 let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
7456 assert_eq!(
7457 u8_expected,
7458 get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
7459 );
7460
7461 let i64_expected = vec![
7463 "-185542587187200000",
7464 "-2831155200000",
7465 "-11059200000",
7466 "0",
7467 "10972800000",
7468 "2831068800000",
7469 "185542587100800000",
7470 ];
7471 assert_eq!(
7472 i64_expected,
7473 get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
7474 );
7475 }
7476
7477 #[test]
7478 fn test_cast_from_int16() {
7479 let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
7480 let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
7481
7482 let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7483 assert_eq!(
7484 f64_expected,
7485 get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
7486 );
7487
7488 let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7489 assert_eq!(
7490 f32_expected,
7491 get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
7492 );
7493
7494 let f16_expected = vec![
7495 f16::from_f64(-32768.0),
7496 f16::from_f64(-128.0),
7497 f16::from_f64(0.0),
7498 f16::from_f64(127.0),
7499 f16::from_f64(32767.0),
7500 ];
7501 assert_eq!(
7502 f16_expected,
7503 get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
7504 .iter()
7505 .map(|i| i.parse::<f16>().unwrap())
7506 .collect::<Vec<f16>>()
7507 );
7508
7509 let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
7510 assert_eq!(
7511 i64_expected,
7512 get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
7513 );
7514
7515 let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
7516 assert_eq!(
7517 i32_expected,
7518 get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
7519 );
7520
7521 let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
7522 assert_eq!(
7523 i16_expected,
7524 get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
7525 );
7526
7527 let i8_expected = vec!["null", "-128", "0", "127", "null"];
7528 assert_eq!(
7529 i8_expected,
7530 get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
7531 );
7532
7533 let u64_expected = vec!["null", "null", "0", "127", "32767"];
7534 assert_eq!(
7535 u64_expected,
7536 get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
7537 );
7538
7539 let u32_expected = vec!["null", "null", "0", "127", "32767"];
7540 assert_eq!(
7541 u32_expected,
7542 get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
7543 );
7544
7545 let u16_expected = vec!["null", "null", "0", "127", "32767"];
7546 assert_eq!(
7547 u16_expected,
7548 get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
7549 );
7550
7551 let u8_expected = vec!["null", "null", "0", "127", "null"];
7552 assert_eq!(
7553 u8_expected,
7554 get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
7555 );
7556 }
7557
7558 #[test]
7559 fn test_cast_from_date32() {
7560 let i32_values: Vec<i32> = vec![
7561 i32::MIN,
7562 i16::MIN as i32,
7563 i8::MIN as i32,
7564 0,
7565 i8::MAX as i32,
7566 i16::MAX as i32,
7567 i32::MAX,
7568 ];
7569 let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
7570
7571 let i64_expected = vec![
7572 "-2147483648",
7573 "-32768",
7574 "-128",
7575 "0",
7576 "127",
7577 "32767",
7578 "2147483647",
7579 ];
7580 assert_eq!(
7581 i64_expected,
7582 get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7583 );
7584 }
7585
7586 #[test]
7587 fn test_cast_from_int8() {
7588 let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7589 let i8_array = Int8Array::from(i8_values);
7590
7591 let f64_expected = vec!["-128.0", "0.0", "127.0"];
7592 assert_eq!(
7593 f64_expected,
7594 get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7595 );
7596
7597 let f32_expected = vec!["-128.0", "0.0", "127.0"];
7598 assert_eq!(
7599 f32_expected,
7600 get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7601 );
7602
7603 let f16_expected = vec!["-128.0", "0.0", "127.0"];
7604 assert_eq!(
7605 f16_expected,
7606 get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7607 );
7608
7609 let i64_expected = vec!["-128", "0", "127"];
7610 assert_eq!(
7611 i64_expected,
7612 get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7613 );
7614
7615 let i32_expected = vec!["-128", "0", "127"];
7616 assert_eq!(
7617 i32_expected,
7618 get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7619 );
7620
7621 let i16_expected = vec!["-128", "0", "127"];
7622 assert_eq!(
7623 i16_expected,
7624 get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7625 );
7626
7627 let i8_expected = vec!["-128", "0", "127"];
7628 assert_eq!(
7629 i8_expected,
7630 get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7631 );
7632
7633 let u64_expected = vec!["null", "0", "127"];
7634 assert_eq!(
7635 u64_expected,
7636 get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7637 );
7638
7639 let u32_expected = vec!["null", "0", "127"];
7640 assert_eq!(
7641 u32_expected,
7642 get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7643 );
7644
7645 let u16_expected = vec!["null", "0", "127"];
7646 assert_eq!(
7647 u16_expected,
7648 get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7649 );
7650
7651 let u8_expected = vec!["null", "0", "127"];
7652 assert_eq!(
7653 u8_expected,
7654 get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
7655 );
7656 }
7657
7658 fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
7660 where
7661 T: ArrowPrimitiveType,
7662 {
7663 let c = cast(array, dt).unwrap();
7664 let a = c.as_primitive::<T>();
7665 let mut v: Vec<String> = vec![];
7666 for i in 0..array.len() {
7667 if a.is_null(i) {
7668 v.push("null".to_string())
7669 } else {
7670 v.push(format!("{:?}", a.value(i)));
7671 }
7672 }
7673 v
7674 }
7675
7676 #[test]
7677 fn test_cast_utf8_dict() {
7678 use DataType::*;
7680
7681 let mut builder = StringDictionaryBuilder::<Int8Type>::new();
7682 builder.append("one").unwrap();
7683 builder.append_null();
7684 builder.append("three").unwrap();
7685 let array: ArrayRef = Arc::new(builder.finish());
7686
7687 let expected = vec!["one", "null", "three"];
7688
7689 let cast_type = Utf8;
7691 let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
7692 assert_eq!(cast_array.data_type(), &cast_type);
7693 assert_eq!(array_to_strings(&cast_array), expected);
7694
7695 let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
7698 let cast_array = cast(&array, &cast_type).expect("cast failed");
7699 assert_eq!(cast_array.data_type(), &cast_type);
7700 assert_eq!(array_to_strings(&cast_array), expected);
7701
7702 let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
7703 let cast_array = cast(&array, &cast_type).expect("cast failed");
7704 assert_eq!(cast_array.data_type(), &cast_type);
7705 assert_eq!(array_to_strings(&cast_array), expected);
7706
7707 let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
7708 let cast_array = cast(&array, &cast_type).expect("cast failed");
7709 assert_eq!(cast_array.data_type(), &cast_type);
7710 assert_eq!(array_to_strings(&cast_array), expected);
7711
7712 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7713 let cast_array = cast(&array, &cast_type).expect("cast failed");
7714 assert_eq!(cast_array.data_type(), &cast_type);
7715 assert_eq!(array_to_strings(&cast_array), expected);
7716
7717 let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
7718 let cast_array = cast(&array, &cast_type).expect("cast failed");
7719 assert_eq!(cast_array.data_type(), &cast_type);
7720 assert_eq!(array_to_strings(&cast_array), expected);
7721
7722 let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
7723 let cast_array = cast(&array, &cast_type).expect("cast failed");
7724 assert_eq!(cast_array.data_type(), &cast_type);
7725 assert_eq!(array_to_strings(&cast_array), expected);
7726
7727 let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
7728 let cast_array = cast(&array, &cast_type).expect("cast failed");
7729 assert_eq!(cast_array.data_type(), &cast_type);
7730 assert_eq!(array_to_strings(&cast_array), expected);
7731 }
7732
7733 #[test]
7734 fn test_cast_dict_to_dict_bad_index_value_primitive() {
7735 use DataType::*;
7736 let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
7741
7742 for i in 0..200 {
7746 builder.append(i).unwrap();
7747 }
7748 let array: ArrayRef = Arc::new(builder.finish());
7749
7750 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7751 let res = cast(&array, &cast_type);
7752 assert!(res.is_err());
7753 let actual_error = format!("{res:?}");
7754 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7755 assert!(
7756 actual_error.contains(expected_error),
7757 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7758 );
7759 }
7760
7761 #[test]
7762 fn test_cast_dict_to_dict_bad_index_value_utf8() {
7763 use DataType::*;
7764 let mut builder = StringDictionaryBuilder::<Int32Type>::new();
7768
7769 for i in 0..200 {
7773 let val = format!("val{i}");
7774 builder.append(&val).unwrap();
7775 }
7776 let array = builder.finish();
7777
7778 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7779 let res = cast(&array, &cast_type);
7780 assert!(res.is_err());
7781 let actual_error = format!("{res:?}");
7782 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7783 assert!(
7784 actual_error.contains(expected_error),
7785 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7786 );
7787 }
7788
7789 #[test]
7790 fn test_cast_primitive_dict() {
7791 use DataType::*;
7793
7794 let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
7795 builder.append(1).unwrap();
7796 builder.append_null();
7797 builder.append(3).unwrap();
7798 let array: ArrayRef = Arc::new(builder.finish());
7799
7800 let expected = vec!["1", "null", "3"];
7801
7802 let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
7804 assert_eq!(array_to_strings(&cast_array), expected);
7805 assert_eq!(cast_array.data_type(), &Utf8);
7806
7807 let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
7808 assert_eq!(array_to_strings(&cast_array), expected);
7809 assert_eq!(cast_array.data_type(), &Int64);
7810 }
7811
7812 #[test]
7813 fn test_cast_primitive_array_to_dict() {
7814 use DataType::*;
7815
7816 let mut builder = PrimitiveBuilder::<Int32Type>::new();
7817 builder.append_value(1);
7818 builder.append_null();
7819 builder.append_value(3);
7820 let array: ArrayRef = Arc::new(builder.finish());
7821
7822 let expected = vec!["1", "null", "3"];
7823
7824 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
7826 let cast_array = cast(&array, &cast_type).expect("cast failed");
7827 assert_eq!(cast_array.data_type(), &cast_type);
7828 assert_eq!(array_to_strings(&cast_array), expected);
7829
7830 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
7832 let cast_array = cast(&array, &cast_type).expect("cast failed");
7833 assert_eq!(cast_array.data_type(), &cast_type);
7834 assert_eq!(array_to_strings(&cast_array), expected);
7835 }
7836
7837 #[test]
7838 fn test_cast_time_array_to_dict() {
7839 use DataType::*;
7840
7841 let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
7842
7843 let expected = vec!["1972-09-27", "null", "1975-06-24"];
7844
7845 let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
7846 let cast_array = cast(&array, &cast_type).expect("cast failed");
7847 assert_eq!(cast_array.data_type(), &cast_type);
7848 assert_eq!(array_to_strings(&cast_array), expected);
7849 }
7850
7851 #[test]
7852 fn test_cast_timestamp_array_to_dict() {
7853 use DataType::*;
7854
7855 let array = Arc::new(
7856 TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
7857 ) as ArrayRef;
7858
7859 let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
7860
7861 let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
7862 let cast_array = cast(&array, &cast_type).expect("cast failed");
7863 assert_eq!(cast_array.data_type(), &cast_type);
7864 assert_eq!(array_to_strings(&cast_array), expected);
7865 }
7866
7867 #[test]
7868 fn test_cast_string_array_to_dict() {
7869 use DataType::*;
7870
7871 let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
7872
7873 let expected = vec!["one", "null", "three"];
7874
7875 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7877 let cast_array = cast(&array, &cast_type).expect("cast failed");
7878 assert_eq!(cast_array.data_type(), &cast_type);
7879 assert_eq!(array_to_strings(&cast_array), expected);
7880 }
7881
7882 #[test]
7883 fn test_cast_null_array_to_from_decimal_array() {
7884 let data_type = DataType::Decimal128(12, 4);
7885 let array = new_null_array(&DataType::Null, 4);
7886 assert_eq!(array.data_type(), &DataType::Null);
7887 let cast_array = cast(&array, &data_type).expect("cast failed");
7888 assert_eq!(cast_array.data_type(), &data_type);
7889 for i in 0..4 {
7890 assert!(cast_array.is_null(i));
7891 }
7892
7893 let array = new_null_array(&data_type, 4);
7894 assert_eq!(array.data_type(), &data_type);
7895 let cast_array = cast(&array, &DataType::Null).expect("cast failed");
7896 assert_eq!(cast_array.data_type(), &DataType::Null);
7897 assert_eq!(cast_array.len(), 4);
7898 assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
7899 }
7900
7901 #[test]
7902 fn test_cast_null_array_from_and_to_primitive_array() {
7903 macro_rules! typed_test {
7904 ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
7905 {
7906 let array = Arc::new(NullArray::new(6)) as ArrayRef;
7907 let expected = $ARR_TYPE::from(vec![None; 6]);
7908 let cast_type = DataType::$DATATYPE;
7909 let cast_array = cast(&array, &cast_type).expect("cast failed");
7910 let cast_array = cast_array.as_primitive::<$TYPE>();
7911 assert_eq!(cast_array.data_type(), &cast_type);
7912 assert_eq!(cast_array, &expected);
7913 }
7914 }};
7915 }
7916
7917 typed_test!(Int16Array, Int16, Int16Type);
7918 typed_test!(Int32Array, Int32, Int32Type);
7919 typed_test!(Int64Array, Int64, Int64Type);
7920
7921 typed_test!(UInt16Array, UInt16, UInt16Type);
7922 typed_test!(UInt32Array, UInt32, UInt32Type);
7923 typed_test!(UInt64Array, UInt64, UInt64Type);
7924
7925 typed_test!(Float32Array, Float32, Float32Type);
7926 typed_test!(Float64Array, Float64, Float64Type);
7927
7928 typed_test!(Date32Array, Date32, Date32Type);
7929 typed_test!(Date64Array, Date64, Date64Type);
7930 }
7931
7932 fn cast_from_null_to_other(data_type: &DataType) {
7933 {
7935 let array = new_null_array(&DataType::Null, 4);
7936 assert_eq!(array.data_type(), &DataType::Null);
7937 let cast_array = cast(&array, data_type).expect("cast failed");
7938 assert_eq!(cast_array.data_type(), data_type);
7939 for i in 0..4 {
7940 assert!(cast_array.is_null(i));
7941 }
7942 }
7943 }
7944
7945 #[test]
7946 fn test_cast_null_from_and_to_variable_sized() {
7947 cast_from_null_to_other(&DataType::Utf8);
7948 cast_from_null_to_other(&DataType::LargeUtf8);
7949 cast_from_null_to_other(&DataType::Binary);
7950 cast_from_null_to_other(&DataType::LargeBinary);
7951 }
7952
7953 #[test]
7954 fn test_cast_null_from_and_to_nested_type() {
7955 let data_type = DataType::Map(
7957 Arc::new(Field::new_struct(
7958 "entry",
7959 vec![
7960 Field::new("key", DataType::Utf8, false),
7961 Field::new("value", DataType::Int32, true),
7962 ],
7963 false,
7964 )),
7965 false,
7966 );
7967 cast_from_null_to_other(&data_type);
7968
7969 let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
7971 cast_from_null_to_other(&data_type);
7972 let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
7973 cast_from_null_to_other(&data_type);
7974 let data_type =
7975 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
7976 cast_from_null_to_other(&data_type);
7977
7978 let values = vec![None, None, None, None] as Vec<Option<&str>>;
7980 let array: DictionaryArray<Int8Type> = values.into_iter().collect();
7981 let array = Arc::new(array) as ArrayRef;
7982 let data_type = array.data_type().to_owned();
7983 cast_from_null_to_other(&data_type);
7984
7985 let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
7987 cast_from_null_to_other(&data_type);
7988 }
7989
7990 fn array_to_strings(array: &ArrayRef) -> Vec<String> {
7992 let options = FormatOptions::new().with_null("null");
7993 let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
7994 (0..array.len())
7995 .map(|i| formatter.value(i).to_string())
7996 .collect()
7997 }
7998
7999 #[test]
8000 fn test_cast_utf8_to_date32() {
8001 use chrono::NaiveDate;
8002 let from_ymd = chrono::NaiveDate::from_ymd_opt;
8003 let since = chrono::NaiveDate::signed_duration_since;
8004
8005 let a = StringArray::from(vec![
8006 "2000-01-01", "2000-01-01T12:00:00", "2000-2-2", "2000-00-00", "2000", ]);
8012 let array = Arc::new(a) as ArrayRef;
8013 let b = cast(&array, &DataType::Date32).unwrap();
8014 let c = b.as_primitive::<Date32Type>();
8015
8016 let date_value = since(
8018 NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8019 from_ymd(1970, 1, 1).unwrap(),
8020 )
8021 .num_days() as i32;
8022 assert!(c.is_valid(0)); assert_eq!(date_value, c.value(0));
8024
8025 assert!(c.is_valid(1)); assert_eq!(date_value, c.value(1));
8027
8028 let date_value = since(
8029 NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8030 from_ymd(1970, 1, 1).unwrap(),
8031 )
8032 .num_days() as i32;
8033 assert!(c.is_valid(2)); assert_eq!(date_value, c.value(2));
8035
8036 assert!(!c.is_valid(3)); assert!(!c.is_valid(4)); }
8040
8041 #[test]
8042 fn test_cast_utf8_to_date64() {
8043 let a = StringArray::from(vec![
8044 "2000-01-01T12:00:00", "2020-12-15T12:34:56", "2020-2-2T12:34:56", "2000-00-00T12:00:00", "2000-01-01 12:00:00", "2000-01-01", ]);
8051 let array = Arc::new(a) as ArrayRef;
8052 let b = cast(&array, &DataType::Date64).unwrap();
8053 let c = b.as_primitive::<Date64Type>();
8054
8055 assert!(c.is_valid(0)); assert_eq!(946728000000, c.value(0));
8058 assert!(c.is_valid(1)); assert_eq!(1608035696000, c.value(1));
8060 assert!(!c.is_valid(2)); assert!(!c.is_valid(3)); assert!(c.is_valid(4)); assert_eq!(946728000000, c.value(4));
8065 assert!(c.is_valid(5)); assert_eq!(946684800000, c.value(5));
8067 }
8068
8069 #[test]
8070 fn test_can_cast_fsl_to_fsl() {
8071 let from_array = Arc::new(
8072 FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8073 [Some([Some(1.0), Some(2.0)]), None],
8074 2,
8075 ),
8076 ) as ArrayRef;
8077 let to_array = Arc::new(
8078 FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8079 [
8080 Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8081 None,
8082 ],
8083 2,
8084 ),
8085 ) as ArrayRef;
8086
8087 assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8088 let actual = cast(&from_array, to_array.data_type()).unwrap();
8089 assert_eq!(actual.data_type(), to_array.data_type());
8090
8091 let invalid_target =
8092 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8093 assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8094
8095 let invalid_size =
8096 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8097 assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8098 }
8099
8100 #[test]
8101 fn test_can_cast_types_fixed_size_list_to_list() {
8102 let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
8104 assert!(can_cast_types(
8105 array1.data_type(),
8106 &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8107 ));
8108
8109 let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
8111 assert!(can_cast_types(
8112 array2.data_type(),
8113 &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8114 ));
8115 }
8116
8117 #[test]
8118 fn test_cast_fixed_size_list_to_list() {
8119 let cases = [
8125 (
8127 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8128 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8129 2,
8130 )) as ArrayRef,
8131 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8132 Some([Some(1), Some(1)]),
8133 Some([Some(2), Some(2)]),
8134 ])) as ArrayRef,
8135 ),
8136 (
8138 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8139 [None, Some([Some(2), Some(2)])],
8140 2,
8141 )) as ArrayRef,
8142 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8143 None,
8144 Some([Some(2), Some(2)]),
8145 ])) as ArrayRef,
8146 ),
8147 (
8149 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8150 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8151 2,
8152 )) as ArrayRef,
8153 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8154 Some([Some(1), Some(1)]),
8155 Some([Some(2), Some(2)]),
8156 ])) as ArrayRef,
8157 ),
8158 (
8160 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8161 [None, Some([Some(2), Some(2)])],
8162 2,
8163 )) as ArrayRef,
8164 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8165 None,
8166 Some([Some(2), Some(2)]),
8167 ])) as ArrayRef,
8168 ),
8169 ];
8170
8171 for (array, expected) in cases {
8172 let array = Arc::new(array) as ArrayRef;
8173
8174 assert!(
8175 can_cast_types(array.data_type(), expected.data_type()),
8176 "can_cast_types claims we cannot cast {:?} to {:?}",
8177 array.data_type(),
8178 expected.data_type()
8179 );
8180
8181 let list_array = cast(&array, expected.data_type())
8182 .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8183 assert_eq!(
8184 list_array.as_ref(),
8185 &expected,
8186 "Incorrect result from casting {array:?} to {expected:?}",
8187 );
8188 }
8189 }
8190
8191 #[test]
8192 fn test_cast_utf8_to_list() {
8193 let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8195 let field = Arc::new(Field::new("", DataType::Int32, false));
8196 let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8197 let actual = list_array.as_list_opt::<i32>().unwrap();
8198 let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8199 assert_eq!(&expect.value(0), &actual.value(0));
8200
8201 let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8203 let actual = list_array.as_list_opt::<i64>().unwrap();
8204 let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8205 assert_eq!(&expect.value(0), &actual.value(0));
8206
8207 let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8209 let actual = list_array.as_fixed_size_list_opt().unwrap();
8210 let expect =
8211 FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
8212 assert_eq!(&expect.value(0), &actual.value(0));
8213 }
8214
8215 #[test]
8216 fn test_cast_single_element_fixed_size_list() {
8217 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8219 [(Some([Some(5)]))],
8220 1,
8221 )) as ArrayRef;
8222 let casted_array = cast(&from_array, &DataType::Int32).unwrap();
8223 let actual: &Int32Array = casted_array.as_primitive();
8224 let expected = Int32Array::from(vec![Some(5)]);
8225 assert_eq!(&expected, actual);
8226
8227 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8229 [(Some([Some(5)]))],
8230 1,
8231 )) as ArrayRef;
8232 let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
8233 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8234 let expected = Arc::new(FixedSizeListArray::new(
8235 to_field.clone(),
8236 1,
8237 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8238 None,
8239 )) as ArrayRef;
8240 assert_eq!(*expected, *actual);
8241
8242 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8244 [(Some([Some(5)]))],
8245 1,
8246 )) as ArrayRef;
8247 let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
8248 let to_field = Arc::new(Field::new(
8249 "dummy",
8250 DataType::FixedSizeList(to_field_inner.clone(), 1),
8251 false,
8252 ));
8253 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8254 let expected = Arc::new(FixedSizeListArray::new(
8255 to_field.clone(),
8256 1,
8257 Arc::new(FixedSizeListArray::new(
8258 to_field_inner.clone(),
8259 1,
8260 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8261 None,
8262 )) as ArrayRef,
8263 None,
8264 )) as ArrayRef;
8265 assert_eq!(*expected, *actual);
8266
8267 let field = Arc::new(Field::new("dummy", DataType::Float32, false));
8269 let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
8270 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8271 let actual = casted_array.as_fixed_size_list();
8272 let expected = Arc::new(FixedSizeListArray::new(
8273 field.clone(),
8274 1,
8275 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8276 None,
8277 )) as ArrayRef;
8278 assert_eq!(expected.as_ref(), actual);
8279
8280 let field = Arc::new(Field::new("nullable", DataType::Float32, true));
8282 let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
8283 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8284 let actual = casted_array.as_fixed_size_list();
8285 let expected = Arc::new(FixedSizeListArray::new(
8286 field.clone(),
8287 1,
8288 Arc::new(Float32Array::from(vec![None])) as ArrayRef,
8289 None,
8290 )) as ArrayRef;
8291 assert_eq!(expected.as_ref(), actual);
8292 }
8293
8294 #[test]
8295 fn test_cast_list_containers() {
8296 let array = Arc::new(make_large_list_array()) as ArrayRef;
8298 let list_array = cast(
8299 &array,
8300 &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
8301 )
8302 .unwrap();
8303 let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
8304 let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
8305
8306 assert_eq!(&expected.value(0), &actual.value(0));
8307 assert_eq!(&expected.value(1), &actual.value(1));
8308 assert_eq!(&expected.value(2), &actual.value(2));
8309
8310 let array = Arc::new(make_list_array()) as ArrayRef;
8312 let large_list_array = cast(
8313 &array,
8314 &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
8315 )
8316 .unwrap();
8317 let actual = large_list_array
8318 .as_any()
8319 .downcast_ref::<LargeListArray>()
8320 .unwrap();
8321 let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
8322
8323 assert_eq!(&expected.value(0), &actual.value(0));
8324 assert_eq!(&expected.value(1), &actual.value(1));
8325 assert_eq!(&expected.value(2), &actual.value(2));
8326 }
8327
8328 #[test]
8329 fn test_cast_list_to_fsl() {
8330 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8338 let values = vec![
8339 Some(vec![Some(1), Some(2), Some(3)]),
8340 Some(vec![Some(4), Some(5), Some(6)]),
8341 ];
8342 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8343 values.clone(),
8344 )) as ArrayRef;
8345 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8346 values, 3,
8347 )) as ArrayRef;
8348 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8349 assert_eq!(expected.as_ref(), actual.as_ref());
8350
8351 let cases = [
8354 (
8355 vec![1, 2, 3, 4, 5, 6],
8357 vec![3, 0, 3, 0],
8358 ),
8359 (
8360 vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
8362 vec![3, 2, 3, 1],
8363 ),
8364 (
8365 vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
8367 vec![3, 3, 3, 3],
8368 ),
8369 (
8370 vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
8372 vec![3, 0, 3, 3],
8373 ),
8374 ];
8375 let null_buffer = NullBuffer::from(vec![true, false, true, false]);
8376
8377 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8378 vec![
8379 Some(vec![Some(1), Some(2), Some(3)]),
8380 None,
8381 Some(vec![Some(4), Some(5), Some(6)]),
8382 None,
8383 ],
8384 3,
8385 )) as ArrayRef;
8386
8387 for (values, lengths) in cases.iter() {
8388 let array = Arc::new(ListArray::new(
8389 field.clone(),
8390 OffsetBuffer::from_lengths(lengths.clone()),
8391 Arc::new(Int32Array::from(values.clone())),
8392 Some(null_buffer.clone()),
8393 )) as ArrayRef;
8394 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8395 assert_eq!(expected.as_ref(), actual.as_ref());
8396 }
8397 }
8398
8399 #[test]
8400 fn test_cast_list_to_fsl_safety() {
8401 let values = vec![
8402 Some(vec![Some(1), Some(2), Some(3)]),
8403 Some(vec![Some(4), Some(5)]),
8404 Some(vec![Some(6), Some(7), Some(8), Some(9)]),
8405 Some(vec![Some(3), Some(4), Some(5)]),
8406 ];
8407 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8408 values.clone(),
8409 )) as ArrayRef;
8410
8411 let res = cast_with_options(
8412 array.as_ref(),
8413 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8414 &CastOptions {
8415 safe: false,
8416 ..Default::default()
8417 },
8418 );
8419 assert!(res.is_err());
8420 assert!(format!("{res:?}")
8421 .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2"));
8422
8423 let res = cast(
8426 array.as_ref(),
8427 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8428 )
8429 .unwrap();
8430 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8431 vec![
8432 Some(vec![Some(1), Some(2), Some(3)]),
8433 None, None, Some(vec![Some(3), Some(4), Some(5)]),
8436 ],
8437 3,
8438 )) as ArrayRef;
8439 assert_eq!(expected.as_ref(), res.as_ref());
8440
8441 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
8444 Some(vec![Some(1), Some(2), Some(3)]),
8445 None,
8446 ])) as ArrayRef;
8447 let res = cast_with_options(
8448 array.as_ref(),
8449 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8450 &CastOptions {
8451 safe: false,
8452 ..Default::default()
8453 },
8454 )
8455 .unwrap();
8456 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8457 vec![Some(vec![Some(1), Some(2), Some(3)]), None],
8458 3,
8459 )) as ArrayRef;
8460 assert_eq!(expected.as_ref(), res.as_ref());
8461 }
8462
8463 #[test]
8464 fn test_cast_large_list_to_fsl() {
8465 let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
8466 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8467 values.clone(),
8468 )) as ArrayRef;
8469 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8470 values, 2,
8471 )) as ArrayRef;
8472 let actual = cast(
8473 array.as_ref(),
8474 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
8475 )
8476 .unwrap();
8477 assert_eq!(expected.as_ref(), actual.as_ref());
8478 }
8479
8480 #[test]
8481 fn test_cast_list_to_fsl_subcast() {
8482 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8483 vec![
8484 Some(vec![Some(1), Some(2)]),
8485 Some(vec![Some(3), Some(i32::MAX)]),
8486 ],
8487 )) as ArrayRef;
8488 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
8489 vec![
8490 Some(vec![Some(1), Some(2)]),
8491 Some(vec![Some(3), Some(i32::MAX as i64)]),
8492 ],
8493 2,
8494 )) as ArrayRef;
8495 let actual = cast(
8496 array.as_ref(),
8497 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
8498 )
8499 .unwrap();
8500 assert_eq!(expected.as_ref(), actual.as_ref());
8501
8502 let res = cast_with_options(
8503 array.as_ref(),
8504 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
8505 &CastOptions {
8506 safe: false,
8507 ..Default::default()
8508 },
8509 );
8510 assert!(res.is_err());
8511 assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
8512 }
8513
8514 #[test]
8515 fn test_cast_list_to_fsl_empty() {
8516 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8517 let array = new_empty_array(&DataType::List(field.clone()));
8518
8519 let target_type = DataType::FixedSizeList(field.clone(), 3);
8520 let expected = new_empty_array(&target_type);
8521
8522 let actual = cast(array.as_ref(), &target_type).unwrap();
8523 assert_eq!(expected.as_ref(), actual.as_ref());
8524 }
8525
8526 fn make_list_array() -> ListArray {
8527 let value_data = ArrayData::builder(DataType::Int32)
8529 .len(8)
8530 .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8531 .build()
8532 .unwrap();
8533
8534 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8537
8538 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8540 let list_data = ArrayData::builder(list_data_type)
8541 .len(3)
8542 .add_buffer(value_offsets)
8543 .add_child_data(value_data)
8544 .build()
8545 .unwrap();
8546 ListArray::from(list_data)
8547 }
8548
8549 fn make_large_list_array() -> LargeListArray {
8550 let value_data = ArrayData::builder(DataType::Int32)
8552 .len(8)
8553 .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8554 .build()
8555 .unwrap();
8556
8557 let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
8560
8561 let list_data_type =
8563 DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8564 let list_data = ArrayData::builder(list_data_type)
8565 .len(3)
8566 .add_buffer(value_offsets)
8567 .add_child_data(value_data)
8568 .build()
8569 .unwrap();
8570 LargeListArray::from(list_data)
8571 }
8572
8573 fn make_fixed_size_list_array() -> FixedSizeListArray {
8574 let value_data = ArrayData::builder(DataType::Int32)
8576 .len(8)
8577 .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8578 .build()
8579 .unwrap();
8580
8581 let list_data_type =
8582 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8583 let list_data = ArrayData::builder(list_data_type)
8584 .len(2)
8585 .add_child_data(value_data)
8586 .build()
8587 .unwrap();
8588 FixedSizeListArray::from(list_data)
8589 }
8590
8591 fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8592 let value_data = ArrayData::builder(DataType::Int64)
8594 .len(8)
8595 .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8596 .build()
8597 .unwrap();
8598
8599 let list_data_type =
8600 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8601 let list_data = ArrayData::builder(list_data_type)
8602 .len(2)
8603 .add_child_data(value_data)
8604 .build()
8605 .unwrap();
8606 FixedSizeListArray::from(list_data)
8607 }
8608
8609 #[test]
8610 fn test_cast_map_dont_allow_change_of_order() {
8611 let string_builder = StringBuilder::new();
8612 let value_builder = StringBuilder::new();
8613 let mut builder = MapBuilder::new(
8614 Some(MapFieldNames {
8615 entry: "entries".to_string(),
8616 key: "key".to_string(),
8617 value: "value".to_string(),
8618 }),
8619 string_builder,
8620 value_builder,
8621 );
8622
8623 builder.keys().append_value("0");
8624 builder.values().append_value("test_val_1");
8625 builder.append(true).unwrap();
8626 builder.keys().append_value("1");
8627 builder.values().append_value("test_val_2");
8628 builder.append(true).unwrap();
8629
8630 let array = builder.finish();
8632
8633 let new_ordered = true;
8634 let new_type = DataType::Map(
8635 Arc::new(Field::new(
8636 "entries",
8637 DataType::Struct(
8638 vec![
8639 Field::new("key", DataType::Utf8, false),
8640 Field::new("value", DataType::Utf8, false),
8641 ]
8642 .into(),
8643 ),
8644 false,
8645 )),
8646 new_ordered,
8647 );
8648
8649 let new_array_result = cast(&array, &new_type.clone());
8650 assert!(!can_cast_types(array.data_type(), &new_type));
8651 assert!(
8652 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"#)
8653 );
8654 }
8655
8656 #[test]
8657 fn test_cast_map_dont_allow_when_container_cant_cast() {
8658 let string_builder = StringBuilder::new();
8659 let value_builder = IntervalDayTimeArray::builder(2);
8660 let mut builder = MapBuilder::new(
8661 Some(MapFieldNames {
8662 entry: "entries".to_string(),
8663 key: "key".to_string(),
8664 value: "value".to_string(),
8665 }),
8666 string_builder,
8667 value_builder,
8668 );
8669
8670 builder.keys().append_value("0");
8671 builder.values().append_value(IntervalDayTime::new(1, 1));
8672 builder.append(true).unwrap();
8673 builder.keys().append_value("1");
8674 builder.values().append_value(IntervalDayTime::new(2, 2));
8675 builder.append(true).unwrap();
8676
8677 let array = builder.finish();
8679
8680 let new_ordered = true;
8681 let new_type = DataType::Map(
8682 Arc::new(Field::new(
8683 "entries",
8684 DataType::Struct(
8685 vec![
8686 Field::new("key", DataType::Utf8, false),
8687 Field::new("value", DataType::Duration(TimeUnit::Second), false),
8688 ]
8689 .into(),
8690 ),
8691 false,
8692 )),
8693 new_ordered,
8694 );
8695
8696 let new_array_result = cast(&array, &new_type.clone());
8697 assert!(!can_cast_types(array.data_type(), &new_type));
8698 assert!(
8699 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"#)
8700 );
8701 }
8702
8703 #[test]
8704 fn test_cast_map_field_names() {
8705 let string_builder = StringBuilder::new();
8706 let value_builder = StringBuilder::new();
8707 let mut builder = MapBuilder::new(
8708 Some(MapFieldNames {
8709 entry: "entries".to_string(),
8710 key: "key".to_string(),
8711 value: "value".to_string(),
8712 }),
8713 string_builder,
8714 value_builder,
8715 );
8716
8717 builder.keys().append_value("0");
8718 builder.values().append_value("test_val_1");
8719 builder.append(true).unwrap();
8720 builder.keys().append_value("1");
8721 builder.values().append_value("test_val_2");
8722 builder.append(true).unwrap();
8723 builder.append(false).unwrap();
8724
8725 let array = builder.finish();
8726
8727 let new_type = DataType::Map(
8728 Arc::new(Field::new(
8729 "entries_new",
8730 DataType::Struct(
8731 vec![
8732 Field::new("key_new", DataType::Utf8, false),
8733 Field::new("value_values", DataType::Utf8, false),
8734 ]
8735 .into(),
8736 ),
8737 false,
8738 )),
8739 false,
8740 );
8741
8742 assert_ne!(new_type, array.data_type().clone());
8743
8744 let new_array = cast(&array, &new_type.clone()).unwrap();
8745 assert_eq!(new_type, new_array.data_type().clone());
8746 let map_array = new_array.as_map();
8747
8748 assert_ne!(new_type, array.data_type().clone());
8749 assert_eq!(new_type, map_array.data_type().clone());
8750
8751 let key_string = map_array
8752 .keys()
8753 .as_any()
8754 .downcast_ref::<StringArray>()
8755 .unwrap()
8756 .into_iter()
8757 .flatten()
8758 .collect::<Vec<_>>();
8759 assert_eq!(&key_string, &vec!["0", "1"]);
8760
8761 let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8762 let values_string = values_string_array
8763 .as_any()
8764 .downcast_ref::<StringArray>()
8765 .unwrap()
8766 .into_iter()
8767 .flatten()
8768 .collect::<Vec<_>>();
8769 assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
8770
8771 assert_eq!(
8772 map_array.nulls(),
8773 Some(&NullBuffer::from(vec![true, true, false]))
8774 );
8775 }
8776
8777 #[test]
8778 fn test_cast_map_contained_values() {
8779 let string_builder = StringBuilder::new();
8780 let value_builder = Int8Builder::new();
8781 let mut builder = MapBuilder::new(
8782 Some(MapFieldNames {
8783 entry: "entries".to_string(),
8784 key: "key".to_string(),
8785 value: "value".to_string(),
8786 }),
8787 string_builder,
8788 value_builder,
8789 );
8790
8791 builder.keys().append_value("0");
8792 builder.values().append_value(44);
8793 builder.append(true).unwrap();
8794 builder.keys().append_value("1");
8795 builder.values().append_value(22);
8796 builder.append(true).unwrap();
8797
8798 let array = builder.finish();
8799
8800 let new_type = DataType::Map(
8801 Arc::new(Field::new(
8802 "entries",
8803 DataType::Struct(
8804 vec![
8805 Field::new("key", DataType::Utf8, false),
8806 Field::new("value", DataType::Utf8, false),
8807 ]
8808 .into(),
8809 ),
8810 false,
8811 )),
8812 false,
8813 );
8814
8815 let new_array = cast(&array, &new_type.clone()).unwrap();
8816 assert_eq!(new_type, new_array.data_type().clone());
8817 let map_array = new_array.as_map();
8818
8819 assert_ne!(new_type, array.data_type().clone());
8820 assert_eq!(new_type, map_array.data_type().clone());
8821
8822 let key_string = map_array
8823 .keys()
8824 .as_any()
8825 .downcast_ref::<StringArray>()
8826 .unwrap()
8827 .into_iter()
8828 .flatten()
8829 .collect::<Vec<_>>();
8830 assert_eq!(&key_string, &vec!["0", "1"]);
8831
8832 let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8833 let values_string = values_string_array
8834 .as_any()
8835 .downcast_ref::<StringArray>()
8836 .unwrap()
8837 .into_iter()
8838 .flatten()
8839 .collect::<Vec<_>>();
8840 assert_eq!(&values_string, &vec!["44", "22"]);
8841 }
8842
8843 #[test]
8844 fn test_utf8_cast_offsets() {
8845 let str_array = StringArray::from(vec!["a", "b", "c"]);
8847 let str_array = str_array.slice(1, 2);
8848
8849 let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
8850
8851 let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
8852 let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
8853 assert_eq!(strs, &["b", "c"])
8854 }
8855
8856 #[test]
8857 fn test_list_cast_offsets() {
8858 let array1 = make_list_array().slice(1, 2);
8860 let array2 = Arc::new(make_list_array()) as ArrayRef;
8861
8862 let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8863 let out1 = cast(&array1, &dt).unwrap();
8864 let out2 = cast(&array2, &dt).unwrap();
8865
8866 assert_eq!(&out1, &out2.slice(1, 2))
8867 }
8868
8869 #[test]
8870 fn test_list_to_string() {
8871 let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
8872 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8873 let value_data = str_array.into_data();
8874
8875 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
8876 let list_data = ArrayData::builder(list_data_type)
8877 .len(3)
8878 .add_buffer(value_offsets)
8879 .add_child_data(value_data)
8880 .build()
8881 .unwrap();
8882 let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
8883
8884 let out = cast(&array, &DataType::Utf8).unwrap();
8885 let out = out
8886 .as_any()
8887 .downcast_ref::<StringArray>()
8888 .unwrap()
8889 .into_iter()
8890 .flatten()
8891 .collect::<Vec<_>>();
8892 assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8893
8894 let out = cast(&array, &DataType::LargeUtf8).unwrap();
8895 let out = out
8896 .as_any()
8897 .downcast_ref::<LargeStringArray>()
8898 .unwrap()
8899 .into_iter()
8900 .flatten()
8901 .collect::<Vec<_>>();
8902 assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8903
8904 let array = Arc::new(make_list_array()) as ArrayRef;
8905 let out = cast(&array, &DataType::Utf8).unwrap();
8906 let out = out
8907 .as_any()
8908 .downcast_ref::<StringArray>()
8909 .unwrap()
8910 .into_iter()
8911 .flatten()
8912 .collect::<Vec<_>>();
8913 assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8914
8915 let array = Arc::new(make_large_list_array()) as ArrayRef;
8916 let out = cast(&array, &DataType::LargeUtf8).unwrap();
8917 let out = out
8918 .as_any()
8919 .downcast_ref::<LargeStringArray>()
8920 .unwrap()
8921 .into_iter()
8922 .flatten()
8923 .collect::<Vec<_>>();
8924 assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8925 }
8926
8927 #[test]
8928 fn test_cast_f64_to_decimal128() {
8929 let decimal_type = DataType::Decimal128(18, 2);
8932 let array = Float64Array::from(vec![
8933 Some(0.0699999999),
8934 Some(0.0659999999),
8935 Some(0.0650000000),
8936 Some(0.0649999999),
8937 ]);
8938 let array = Arc::new(array) as ArrayRef;
8939 generate_cast_test_case!(
8940 &array,
8941 Decimal128Array,
8942 &decimal_type,
8943 vec![
8944 Some(7_i128), Some(7_i128), Some(7_i128), Some(6_i128), ]
8949 );
8950
8951 let decimal_type = DataType::Decimal128(18, 3);
8952 let array = Float64Array::from(vec![
8953 Some(0.0699999999),
8954 Some(0.0659999999),
8955 Some(0.0650000000),
8956 Some(0.0649999999),
8957 ]);
8958 let array = Arc::new(array) as ArrayRef;
8959 generate_cast_test_case!(
8960 &array,
8961 Decimal128Array,
8962 &decimal_type,
8963 vec![
8964 Some(70_i128), Some(66_i128), Some(65_i128), Some(65_i128), ]
8969 );
8970 }
8971
8972 #[test]
8973 fn test_cast_numeric_to_decimal128_overflow() {
8974 let array = Int64Array::from(vec![i64::MAX]);
8975 let array = Arc::new(array) as ArrayRef;
8976 let casted_array = cast_with_options(
8977 &array,
8978 &DataType::Decimal128(38, 30),
8979 &CastOptions {
8980 safe: true,
8981 format_options: FormatOptions::default(),
8982 },
8983 );
8984 assert!(casted_array.is_ok());
8985 assert!(casted_array.unwrap().is_null(0));
8986
8987 let casted_array = cast_with_options(
8988 &array,
8989 &DataType::Decimal128(38, 30),
8990 &CastOptions {
8991 safe: false,
8992 format_options: FormatOptions::default(),
8993 },
8994 );
8995 assert!(casted_array.is_err());
8996 }
8997
8998 #[test]
8999 fn test_cast_numeric_to_decimal256_overflow() {
9000 let array = Int64Array::from(vec![i64::MAX]);
9001 let array = Arc::new(array) as ArrayRef;
9002 let casted_array = cast_with_options(
9003 &array,
9004 &DataType::Decimal256(76, 76),
9005 &CastOptions {
9006 safe: true,
9007 format_options: FormatOptions::default(),
9008 },
9009 );
9010 assert!(casted_array.is_ok());
9011 assert!(casted_array.unwrap().is_null(0));
9012
9013 let casted_array = cast_with_options(
9014 &array,
9015 &DataType::Decimal256(76, 76),
9016 &CastOptions {
9017 safe: false,
9018 format_options: FormatOptions::default(),
9019 },
9020 );
9021 assert!(casted_array.is_err());
9022 }
9023
9024 #[test]
9025 fn test_cast_floating_point_to_decimal128_precision_overflow() {
9026 let array = Float64Array::from(vec![1.1]);
9027 let array = Arc::new(array) as ArrayRef;
9028 let casted_array = cast_with_options(
9029 &array,
9030 &DataType::Decimal128(2, 2),
9031 &CastOptions {
9032 safe: true,
9033 format_options: FormatOptions::default(),
9034 },
9035 );
9036 assert!(casted_array.is_ok());
9037 assert!(casted_array.unwrap().is_null(0));
9038
9039 let casted_array = cast_with_options(
9040 &array,
9041 &DataType::Decimal128(2, 2),
9042 &CastOptions {
9043 safe: false,
9044 format_options: FormatOptions::default(),
9045 },
9046 );
9047 let err = casted_array.unwrap_err().to_string();
9048 let expected_error = "Invalid argument error: 110 is too large to store in a Decimal128 of precision 2. Max is 99";
9049 assert!(
9050 err.contains(expected_error),
9051 "did not find expected error '{expected_error}' in actual error '{err}'"
9052 );
9053 }
9054
9055 #[test]
9056 fn test_cast_floating_point_to_decimal256_precision_overflow() {
9057 let array = Float64Array::from(vec![1.1]);
9058 let array = Arc::new(array) as ArrayRef;
9059 let casted_array = cast_with_options(
9060 &array,
9061 &DataType::Decimal256(2, 2),
9062 &CastOptions {
9063 safe: true,
9064 format_options: FormatOptions::default(),
9065 },
9066 );
9067 assert!(casted_array.is_ok());
9068 assert!(casted_array.unwrap().is_null(0));
9069
9070 let casted_array = cast_with_options(
9071 &array,
9072 &DataType::Decimal256(2, 2),
9073 &CastOptions {
9074 safe: false,
9075 format_options: FormatOptions::default(),
9076 },
9077 );
9078 let err = casted_array.unwrap_err().to_string();
9079 let expected_error = "Invalid argument error: 110 is too large to store in a Decimal256 of precision 2. Max is 99";
9080 assert!(
9081 err.contains(expected_error),
9082 "did not find expected error '{expected_error}' in actual error '{err}'"
9083 );
9084 }
9085
9086 #[test]
9087 fn test_cast_floating_point_to_decimal128_overflow() {
9088 let array = Float64Array::from(vec![f64::MAX]);
9089 let array = Arc::new(array) as ArrayRef;
9090 let casted_array = cast_with_options(
9091 &array,
9092 &DataType::Decimal128(38, 30),
9093 &CastOptions {
9094 safe: true,
9095 format_options: FormatOptions::default(),
9096 },
9097 );
9098 assert!(casted_array.is_ok());
9099 assert!(casted_array.unwrap().is_null(0));
9100
9101 let casted_array = cast_with_options(
9102 &array,
9103 &DataType::Decimal128(38, 30),
9104 &CastOptions {
9105 safe: false,
9106 format_options: FormatOptions::default(),
9107 },
9108 );
9109 let err = casted_array.unwrap_err().to_string();
9110 let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
9111 assert!(
9112 err.contains(expected_error),
9113 "did not find expected error '{expected_error}' in actual error '{err}'"
9114 );
9115 }
9116
9117 #[test]
9118 fn test_cast_floating_point_to_decimal256_overflow() {
9119 let array = Float64Array::from(vec![f64::MAX]);
9120 let array = Arc::new(array) as ArrayRef;
9121 let casted_array = cast_with_options(
9122 &array,
9123 &DataType::Decimal256(76, 50),
9124 &CastOptions {
9125 safe: true,
9126 format_options: FormatOptions::default(),
9127 },
9128 );
9129 assert!(casted_array.is_ok());
9130 assert!(casted_array.unwrap().is_null(0));
9131
9132 let casted_array = cast_with_options(
9133 &array,
9134 &DataType::Decimal256(76, 50),
9135 &CastOptions {
9136 safe: false,
9137 format_options: FormatOptions::default(),
9138 },
9139 );
9140 let err = casted_array.unwrap_err().to_string();
9141 let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
9142 assert!(
9143 err.contains(expected_error),
9144 "did not find expected error '{expected_error}' in actual error '{err}'"
9145 );
9146 }
9147 #[test]
9148 fn test_cast_decimal256_to_f64_no_overflow() {
9149 let array = vec![Some(i256::MAX)];
9151 let array = create_decimal256_array(array, 76, 2).unwrap();
9152 let array = Arc::new(array) as ArrayRef;
9153
9154 let result = cast(&array, &DataType::Float64).unwrap();
9155 let result = result.as_primitive::<Float64Type>();
9156 assert!(result.value(0).is_finite());
9157 assert!(result.value(0) > 0.0); let array = vec![Some(i256::MIN)];
9161 let array = create_decimal256_array(array, 76, 2).unwrap();
9162 let array = Arc::new(array) as ArrayRef;
9163
9164 let result = cast(&array, &DataType::Float64).unwrap();
9165 let result = result.as_primitive::<Float64Type>();
9166 assert!(result.value(0).is_finite());
9167 assert!(result.value(0) < 0.0); }
9169
9170 #[test]
9171 fn test_cast_decimal128_to_decimal128_negative_scale() {
9172 let input_type = DataType::Decimal128(20, 0);
9173 let output_type = DataType::Decimal128(20, -1);
9174 assert!(can_cast_types(&input_type, &output_type));
9175 let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
9176 let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
9177 let array = Arc::new(input_decimal_array) as ArrayRef;
9178 generate_cast_test_case!(
9179 &array,
9180 Decimal128Array,
9181 &output_type,
9182 vec![
9183 Some(112345_i128),
9184 Some(212346_i128),
9185 Some(312346_i128),
9186 None
9187 ]
9188 );
9189
9190 let casted_array = cast(&array, &output_type).unwrap();
9191 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9192
9193 assert_eq!("1123450", decimal_arr.value_as_string(0));
9194 assert_eq!("2123460", decimal_arr.value_as_string(1));
9195 assert_eq!("3123460", decimal_arr.value_as_string(2));
9196 }
9197
9198 #[test]
9199 fn decimal128_min_max_to_f64() {
9200 let min128 = i128::MIN;
9202 let max128 = i128::MAX;
9203 assert_eq!(min128 as f64, min128 as f64);
9204 assert_eq!(max128 as f64, max128 as f64);
9205 }
9206
9207 #[test]
9208 fn test_cast_numeric_to_decimal128_negative() {
9209 let decimal_type = DataType::Decimal128(38, -1);
9210 let array = Arc::new(Int32Array::from(vec![
9211 Some(1123456),
9212 Some(2123456),
9213 Some(3123456),
9214 ])) as ArrayRef;
9215
9216 let casted_array = cast(&array, &decimal_type).unwrap();
9217 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9218
9219 assert_eq!("1123450", decimal_arr.value_as_string(0));
9220 assert_eq!("2123450", decimal_arr.value_as_string(1));
9221 assert_eq!("3123450", decimal_arr.value_as_string(2));
9222
9223 let array = Arc::new(Float32Array::from(vec![
9224 Some(1123.456),
9225 Some(2123.456),
9226 Some(3123.456),
9227 ])) as ArrayRef;
9228
9229 let casted_array = cast(&array, &decimal_type).unwrap();
9230 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9231
9232 assert_eq!("1120", decimal_arr.value_as_string(0));
9233 assert_eq!("2120", decimal_arr.value_as_string(1));
9234 assert_eq!("3120", decimal_arr.value_as_string(2));
9235 }
9236
9237 #[test]
9238 fn test_cast_decimal128_to_decimal128_negative() {
9239 let input_type = DataType::Decimal128(10, -1);
9240 let output_type = DataType::Decimal128(10, -2);
9241 assert!(can_cast_types(&input_type, &output_type));
9242 let array = vec![Some(123)];
9243 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9244 let array = Arc::new(input_decimal_array) as ArrayRef;
9245 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
9246
9247 let casted_array = cast(&array, &output_type).unwrap();
9248 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9249
9250 assert_eq!("1200", decimal_arr.value_as_string(0));
9251
9252 let array = vec![Some(125)];
9253 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9254 let array = Arc::new(input_decimal_array) as ArrayRef;
9255 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
9256
9257 let casted_array = cast(&array, &output_type).unwrap();
9258 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9259
9260 assert_eq!("1300", decimal_arr.value_as_string(0));
9261 }
9262
9263 #[test]
9264 fn test_cast_decimal128_to_decimal256_negative() {
9265 let input_type = DataType::Decimal128(10, 3);
9266 let output_type = DataType::Decimal256(10, 5);
9267 assert!(can_cast_types(&input_type, &output_type));
9268 let array = vec![Some(123456), Some(-123456)];
9269 let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
9270 let array = Arc::new(input_decimal_array) as ArrayRef;
9271
9272 let hundred = i256::from_i128(100);
9273 generate_cast_test_case!(
9274 &array,
9275 Decimal256Array,
9276 &output_type,
9277 vec![
9278 Some(i256::from_i128(123456).mul_wrapping(hundred)),
9279 Some(i256::from_i128(-123456).mul_wrapping(hundred))
9280 ]
9281 );
9282 }
9283
9284 #[test]
9285 fn test_parse_string_to_decimal() {
9286 assert_eq!(
9287 Decimal128Type::format_decimal(
9288 parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
9289 38,
9290 2,
9291 ),
9292 "123.45"
9293 );
9294 assert_eq!(
9295 Decimal128Type::format_decimal(
9296 parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
9297 38,
9298 2,
9299 ),
9300 "12345.00"
9301 );
9302 assert_eq!(
9303 Decimal128Type::format_decimal(
9304 parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
9305 38,
9306 2,
9307 ),
9308 "0.12"
9309 );
9310 assert_eq!(
9311 Decimal128Type::format_decimal(
9312 parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
9313 38,
9314 2,
9315 ),
9316 "0.12"
9317 );
9318 assert_eq!(
9319 Decimal128Type::format_decimal(
9320 parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9321 38,
9322 2,
9323 ),
9324 "0.13"
9325 );
9326 assert_eq!(
9327 Decimal128Type::format_decimal(
9328 parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9329 38,
9330 2,
9331 ),
9332 "0.13"
9333 );
9334
9335 assert_eq!(
9336 Decimal256Type::format_decimal(
9337 parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
9338 38,
9339 3,
9340 ),
9341 "123.450"
9342 );
9343 assert_eq!(
9344 Decimal256Type::format_decimal(
9345 parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
9346 38,
9347 3,
9348 ),
9349 "12345.000"
9350 );
9351 assert_eq!(
9352 Decimal256Type::format_decimal(
9353 parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
9354 38,
9355 3,
9356 ),
9357 "0.123"
9358 );
9359 assert_eq!(
9360 Decimal256Type::format_decimal(
9361 parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
9362 38,
9363 3,
9364 ),
9365 "0.123"
9366 );
9367 assert_eq!(
9368 Decimal256Type::format_decimal(
9369 parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
9370 38,
9371 3,
9372 ),
9373 "0.127"
9374 );
9375 }
9376
9377 fn test_cast_string_to_decimal(array: ArrayRef) {
9378 let output_type = DataType::Decimal128(38, 2);
9380 assert!(can_cast_types(array.data_type(), &output_type));
9381
9382 let casted_array = cast(&array, &output_type).unwrap();
9383 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9384
9385 assert_eq!("123.45", decimal_arr.value_as_string(0));
9386 assert_eq!("1.23", decimal_arr.value_as_string(1));
9387 assert_eq!("0.12", decimal_arr.value_as_string(2));
9388 assert_eq!("0.13", decimal_arr.value_as_string(3));
9389 assert_eq!("1.26", decimal_arr.value_as_string(4));
9390 assert_eq!("12345.00", decimal_arr.value_as_string(5));
9391 assert_eq!("12345.00", decimal_arr.value_as_string(6));
9392 assert_eq!("0.12", decimal_arr.value_as_string(7));
9393 assert_eq!("12.23", decimal_arr.value_as_string(8));
9394 assert!(decimal_arr.is_null(9));
9395 assert_eq!("0.00", decimal_arr.value_as_string(10));
9396 assert_eq!("0.00", decimal_arr.value_as_string(11));
9397 assert!(decimal_arr.is_null(12));
9398 assert_eq!("-1.23", decimal_arr.value_as_string(13));
9399 assert_eq!("-1.24", decimal_arr.value_as_string(14));
9400 assert_eq!("0.00", decimal_arr.value_as_string(15));
9401 assert_eq!("-123.00", decimal_arr.value_as_string(16));
9402 assert_eq!("-123.23", decimal_arr.value_as_string(17));
9403 assert_eq!("-0.12", decimal_arr.value_as_string(18));
9404 assert_eq!("1.23", decimal_arr.value_as_string(19));
9405 assert_eq!("1.24", decimal_arr.value_as_string(20));
9406 assert_eq!("0.00", decimal_arr.value_as_string(21));
9407 assert_eq!("123.00", decimal_arr.value_as_string(22));
9408 assert_eq!("123.23", decimal_arr.value_as_string(23));
9409 assert_eq!("0.12", decimal_arr.value_as_string(24));
9410 assert!(decimal_arr.is_null(25));
9411 assert!(decimal_arr.is_null(26));
9412 assert!(decimal_arr.is_null(27));
9413 assert_eq!("0.00", decimal_arr.value_as_string(28));
9414 assert_eq!("0.00", decimal_arr.value_as_string(29));
9415 assert_eq!("12345.00", decimal_arr.value_as_string(30));
9416 assert_eq!(decimal_arr.len(), 31);
9417
9418 let output_type = DataType::Decimal256(76, 3);
9420 assert!(can_cast_types(array.data_type(), &output_type));
9421
9422 let casted_array = cast(&array, &output_type).unwrap();
9423 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9424
9425 assert_eq!("123.450", decimal_arr.value_as_string(0));
9426 assert_eq!("1.235", decimal_arr.value_as_string(1));
9427 assert_eq!("0.123", decimal_arr.value_as_string(2));
9428 assert_eq!("0.127", decimal_arr.value_as_string(3));
9429 assert_eq!("1.263", decimal_arr.value_as_string(4));
9430 assert_eq!("12345.000", decimal_arr.value_as_string(5));
9431 assert_eq!("12345.000", decimal_arr.value_as_string(6));
9432 assert_eq!("0.123", decimal_arr.value_as_string(7));
9433 assert_eq!("12.234", decimal_arr.value_as_string(8));
9434 assert!(decimal_arr.is_null(9));
9435 assert_eq!("0.000", decimal_arr.value_as_string(10));
9436 assert_eq!("0.000", decimal_arr.value_as_string(11));
9437 assert!(decimal_arr.is_null(12));
9438 assert_eq!("-1.235", decimal_arr.value_as_string(13));
9439 assert_eq!("-1.236", decimal_arr.value_as_string(14));
9440 assert_eq!("0.000", decimal_arr.value_as_string(15));
9441 assert_eq!("-123.000", decimal_arr.value_as_string(16));
9442 assert_eq!("-123.234", decimal_arr.value_as_string(17));
9443 assert_eq!("-0.123", decimal_arr.value_as_string(18));
9444 assert_eq!("1.235", decimal_arr.value_as_string(19));
9445 assert_eq!("1.236", decimal_arr.value_as_string(20));
9446 assert_eq!("0.000", decimal_arr.value_as_string(21));
9447 assert_eq!("123.000", decimal_arr.value_as_string(22));
9448 assert_eq!("123.234", decimal_arr.value_as_string(23));
9449 assert_eq!("0.123", decimal_arr.value_as_string(24));
9450 assert!(decimal_arr.is_null(25));
9451 assert!(decimal_arr.is_null(26));
9452 assert!(decimal_arr.is_null(27));
9453 assert_eq!("0.000", decimal_arr.value_as_string(28));
9454 assert_eq!("0.000", decimal_arr.value_as_string(29));
9455 assert_eq!("12345.000", decimal_arr.value_as_string(30));
9456 assert_eq!(decimal_arr.len(), 31);
9457 }
9458
9459 #[test]
9460 fn test_cast_utf8_to_decimal() {
9461 let str_array = StringArray::from(vec![
9462 Some("123.45"),
9463 Some("1.2345"),
9464 Some("0.12345"),
9465 Some("0.1267"),
9466 Some("1.263"),
9467 Some("12345.0"),
9468 Some("12345"),
9469 Some("000.123"),
9470 Some("12.234000"),
9471 None,
9472 Some(""),
9473 Some(" "),
9474 None,
9475 Some("-1.23499999"),
9476 Some("-1.23599999"),
9477 Some("-0.00001"),
9478 Some("-123"),
9479 Some("-123.234000"),
9480 Some("-000.123"),
9481 Some("+1.23499999"),
9482 Some("+1.23599999"),
9483 Some("+0.00001"),
9484 Some("+123"),
9485 Some("+123.234000"),
9486 Some("+000.123"),
9487 Some("1.-23499999"),
9488 Some("-1.-23499999"),
9489 Some("--1.23499999"),
9490 Some("0"),
9491 Some("000.000"),
9492 Some("0000000000000000012345.000"),
9493 ]);
9494 let array = Arc::new(str_array) as ArrayRef;
9495
9496 test_cast_string_to_decimal(array);
9497
9498 let test_cases = [
9499 (None, None),
9500 (Some("0"), Some("0")),
9503 (Some("000.000"), Some("0")),
9504 (Some("12345"), Some("12345")),
9505 (Some("000000000000000000000000000012345"), Some("12345")),
9506 (Some("-123"), Some("-123")),
9507 (Some("+123"), Some("123")),
9508 ];
9509 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9510 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9511
9512 let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
9513 test_cast_string_to_decimal_scale_zero(array, &expected);
9514 }
9515
9516 #[test]
9517 fn test_cast_large_utf8_to_decimal() {
9518 let str_array = LargeStringArray::from(vec![
9519 Some("123.45"),
9520 Some("1.2345"),
9521 Some("0.12345"),
9522 Some("0.1267"),
9523 Some("1.263"),
9524 Some("12345.0"),
9525 Some("12345"),
9526 Some("000.123"),
9527 Some("12.234000"),
9528 None,
9529 Some(""),
9530 Some(" "),
9531 None,
9532 Some("-1.23499999"),
9533 Some("-1.23599999"),
9534 Some("-0.00001"),
9535 Some("-123"),
9536 Some("-123.234000"),
9537 Some("-000.123"),
9538 Some("+1.23499999"),
9539 Some("+1.23599999"),
9540 Some("+0.00001"),
9541 Some("+123"),
9542 Some("+123.234000"),
9543 Some("+000.123"),
9544 Some("1.-23499999"),
9545 Some("-1.-23499999"),
9546 Some("--1.23499999"),
9547 Some("0"),
9548 Some("000.000"),
9549 Some("0000000000000000012345.000"),
9550 ]);
9551 let array = Arc::new(str_array) as ArrayRef;
9552
9553 test_cast_string_to_decimal(array);
9554
9555 let test_cases = [
9556 (None, None),
9557 (Some(""), None),
9558 (Some(" "), None),
9559 (Some("0"), Some("0")),
9560 (Some("000.000"), Some("0")),
9561 (Some("12345"), Some("12345")),
9562 (Some("000000000000000000000000000012345"), Some("12345")),
9563 (Some("-123"), Some("-123")),
9564 (Some("+123"), Some("123")),
9565 ];
9566 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9567 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9568
9569 let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
9570 test_cast_string_to_decimal_scale_zero(array, &expected);
9571 }
9572
9573 fn test_cast_string_to_decimal_scale_zero(
9574 array: ArrayRef,
9575 expected_as_string: &[Option<&str>],
9576 ) {
9577 let output_type = DataType::Decimal128(38, 0);
9579 assert!(can_cast_types(array.data_type(), &output_type));
9580 let casted_array = cast(&array, &output_type).unwrap();
9581 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9582 assert_decimal_array_contents(decimal_arr, expected_as_string);
9583
9584 let output_type = DataType::Decimal256(76, 0);
9586 assert!(can_cast_types(array.data_type(), &output_type));
9587 let casted_array = cast(&array, &output_type).unwrap();
9588 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9589 assert_decimal_array_contents(decimal_arr, expected_as_string);
9590 }
9591
9592 fn assert_decimal_array_contents<T>(
9593 array: &PrimitiveArray<T>,
9594 expected_as_string: &[Option<&str>],
9595 ) where
9596 T: DecimalType + ArrowPrimitiveType,
9597 {
9598 assert_eq!(array.len(), expected_as_string.len());
9599 for (i, expected) in expected_as_string.iter().enumerate() {
9600 let actual = if array.is_null(i) {
9601 None
9602 } else {
9603 Some(array.value_as_string(i))
9604 };
9605 let actual = actual.as_ref().map(|s| s.as_ref());
9606 assert_eq!(*expected, actual, "Expected at position {i}");
9607 }
9608 }
9609
9610 #[test]
9611 fn test_cast_invalid_utf8_to_decimal() {
9612 let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9613 let array = Arc::new(str_array) as ArrayRef;
9614
9615 let output_type = DataType::Decimal128(38, 2);
9617 let casted_array = cast(&array, &output_type).unwrap();
9618 assert!(casted_array.is_null(0));
9619 assert!(casted_array.is_null(1));
9620
9621 let output_type = DataType::Decimal256(76, 2);
9622 let casted_array = cast(&array, &output_type).unwrap();
9623 assert!(casted_array.is_null(0));
9624 assert!(casted_array.is_null(1));
9625
9626 let output_type = DataType::Decimal128(38, 2);
9628 let str_array = StringArray::from(vec!["4.4.5"]);
9629 let array = Arc::new(str_array) as ArrayRef;
9630 let option = CastOptions {
9631 safe: false,
9632 format_options: FormatOptions::default(),
9633 };
9634 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9635 assert!(casted_err
9636 .to_string()
9637 .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type"));
9638
9639 let str_array = StringArray::from(vec![". 0.123"]);
9640 let array = Arc::new(str_array) as ArrayRef;
9641 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9642 assert!(casted_err
9643 .to_string()
9644 .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type"));
9645 }
9646
9647 fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
9648 let output_type = DataType::Decimal128(38, 2);
9649 let casted_array = cast(&overflow_array, &output_type).unwrap();
9650 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9651
9652 assert!(decimal_arr.is_null(0));
9653 assert!(decimal_arr.is_null(1));
9654 assert!(decimal_arr.is_null(2));
9655 assert_eq!(
9656 "999999999999999999999999999999999999.99",
9657 decimal_arr.value_as_string(3)
9658 );
9659 assert_eq!(
9660 "100000000000000000000000000000000000.00",
9661 decimal_arr.value_as_string(4)
9662 );
9663 }
9664
9665 #[test]
9666 fn test_cast_string_to_decimal128_precision_overflow() {
9667 let array = StringArray::from(vec!["1000".to_string()]);
9668 let array = Arc::new(array) as ArrayRef;
9669 let casted_array = cast_with_options(
9670 &array,
9671 &DataType::Decimal128(10, 8),
9672 &CastOptions {
9673 safe: true,
9674 format_options: FormatOptions::default(),
9675 },
9676 );
9677 assert!(casted_array.is_ok());
9678 assert!(casted_array.unwrap().is_null(0));
9679
9680 let err = cast_with_options(
9681 &array,
9682 &DataType::Decimal128(10, 8),
9683 &CastOptions {
9684 safe: false,
9685 format_options: FormatOptions::default(),
9686 },
9687 );
9688 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());
9689 }
9690
9691 #[test]
9692 fn test_cast_utf8_to_decimal128_overflow() {
9693 let overflow_str_array = StringArray::from(vec![
9694 i128::MAX.to_string(),
9695 i128::MIN.to_string(),
9696 "99999999999999999999999999999999999999".to_string(),
9697 "999999999999999999999999999999999999.99".to_string(),
9698 "99999999999999999999999999999999999.999".to_string(),
9699 ]);
9700 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9701
9702 test_cast_string_to_decimal128_overflow(overflow_array);
9703 }
9704
9705 #[test]
9706 fn test_cast_large_utf8_to_decimal128_overflow() {
9707 let overflow_str_array = LargeStringArray::from(vec![
9708 i128::MAX.to_string(),
9709 i128::MIN.to_string(),
9710 "99999999999999999999999999999999999999".to_string(),
9711 "999999999999999999999999999999999999.99".to_string(),
9712 "99999999999999999999999999999999999.999".to_string(),
9713 ]);
9714 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9715
9716 test_cast_string_to_decimal128_overflow(overflow_array);
9717 }
9718
9719 fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
9720 let output_type = DataType::Decimal256(76, 2);
9721 let casted_array = cast(&overflow_array, &output_type).unwrap();
9722 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9723
9724 assert_eq!(
9725 "170141183460469231731687303715884105727.00",
9726 decimal_arr.value_as_string(0)
9727 );
9728 assert_eq!(
9729 "-170141183460469231731687303715884105728.00",
9730 decimal_arr.value_as_string(1)
9731 );
9732 assert_eq!(
9733 "99999999999999999999999999999999999999.00",
9734 decimal_arr.value_as_string(2)
9735 );
9736 assert_eq!(
9737 "999999999999999999999999999999999999.99",
9738 decimal_arr.value_as_string(3)
9739 );
9740 assert_eq!(
9741 "100000000000000000000000000000000000.00",
9742 decimal_arr.value_as_string(4)
9743 );
9744 assert!(decimal_arr.is_null(5));
9745 assert!(decimal_arr.is_null(6));
9746 }
9747
9748 #[test]
9749 fn test_cast_string_to_decimal256_precision_overflow() {
9750 let array = StringArray::from(vec!["1000".to_string()]);
9751 let array = Arc::new(array) as ArrayRef;
9752 let casted_array = cast_with_options(
9753 &array,
9754 &DataType::Decimal256(10, 8),
9755 &CastOptions {
9756 safe: true,
9757 format_options: FormatOptions::default(),
9758 },
9759 );
9760 assert!(casted_array.is_ok());
9761 assert!(casted_array.unwrap().is_null(0));
9762
9763 let err = cast_with_options(
9764 &array,
9765 &DataType::Decimal256(10, 8),
9766 &CastOptions {
9767 safe: false,
9768 format_options: FormatOptions::default(),
9769 },
9770 );
9771 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());
9772 }
9773
9774 #[test]
9775 fn test_cast_utf8_to_decimal256_overflow() {
9776 let overflow_str_array = StringArray::from(vec![
9777 i128::MAX.to_string(),
9778 i128::MIN.to_string(),
9779 "99999999999999999999999999999999999999".to_string(),
9780 "999999999999999999999999999999999999.99".to_string(),
9781 "99999999999999999999999999999999999.999".to_string(),
9782 i256::MAX.to_string(),
9783 i256::MIN.to_string(),
9784 ]);
9785 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9786
9787 test_cast_string_to_decimal256_overflow(overflow_array);
9788 }
9789
9790 #[test]
9791 fn test_cast_large_utf8_to_decimal256_overflow() {
9792 let overflow_str_array = LargeStringArray::from(vec![
9793 i128::MAX.to_string(),
9794 i128::MIN.to_string(),
9795 "99999999999999999999999999999999999999".to_string(),
9796 "999999999999999999999999999999999999.99".to_string(),
9797 "99999999999999999999999999999999999.999".to_string(),
9798 i256::MAX.to_string(),
9799 i256::MIN.to_string(),
9800 ]);
9801 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9802
9803 test_cast_string_to_decimal256_overflow(overflow_array);
9804 }
9805
9806 #[test]
9807 fn test_cast_outside_supported_range_for_nanoseconds() {
9808 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";
9809
9810 let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
9811
9812 let cast_options = CastOptions {
9813 safe: false,
9814 format_options: FormatOptions::default(),
9815 };
9816
9817 let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
9818 &array,
9819 &None::<Arc<str>>,
9820 &cast_options,
9821 );
9822
9823 let err = result.unwrap_err();
9824 assert_eq!(
9825 err.to_string(),
9826 format!(
9827 "Cast error: Overflow converting {} to Nanosecond. {}",
9828 array.value(0),
9829 EXPECTED_ERROR_MESSAGE
9830 )
9831 );
9832 }
9833
9834 #[test]
9835 fn test_cast_date32_to_timestamp() {
9836 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
9838 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
9839 let c = b.as_primitive::<TimestampSecondType>();
9840 assert_eq!(1609459200, c.value(0));
9841 assert_eq!(1640995200, c.value(1));
9842 assert!(c.is_null(2));
9843 }
9844
9845 #[test]
9846 fn test_cast_date32_to_timestamp_ms() {
9847 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
9849 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
9850 let c = b
9851 .as_any()
9852 .downcast_ref::<TimestampMillisecondArray>()
9853 .unwrap();
9854 assert_eq!(1609459200000, c.value(0));
9855 assert_eq!(1640995200000, c.value(1));
9856 assert!(c.is_null(2));
9857 }
9858
9859 #[test]
9860 fn test_cast_date32_to_timestamp_us() {
9861 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
9863 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
9864 let c = b
9865 .as_any()
9866 .downcast_ref::<TimestampMicrosecondArray>()
9867 .unwrap();
9868 assert_eq!(1609459200000000, c.value(0));
9869 assert_eq!(1640995200000000, c.value(1));
9870 assert!(c.is_null(2));
9871 }
9872
9873 #[test]
9874 fn test_cast_date32_to_timestamp_ns() {
9875 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
9877 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9878 let c = b
9879 .as_any()
9880 .downcast_ref::<TimestampNanosecondArray>()
9881 .unwrap();
9882 assert_eq!(1609459200000000000, c.value(0));
9883 assert_eq!(1640995200000000000, c.value(1));
9884 assert!(c.is_null(2));
9885 }
9886
9887 #[test]
9888 fn test_timezone_cast() {
9889 let a = StringArray::from(vec![
9890 "2000-01-01T12:00:00", "2020-12-15T12:34:56", ]);
9893 let array = Arc::new(a) as ArrayRef;
9894 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9895 let v = b.as_primitive::<TimestampNanosecondType>();
9896
9897 assert_eq!(v.value(0), 946728000000000000);
9898 assert_eq!(v.value(1), 1608035696000000000);
9899
9900 let b = cast(
9901 &b,
9902 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9903 )
9904 .unwrap();
9905 let v = b.as_primitive::<TimestampNanosecondType>();
9906
9907 assert_eq!(v.value(0), 946728000000000000);
9908 assert_eq!(v.value(1), 1608035696000000000);
9909
9910 let b = cast(
9911 &b,
9912 &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
9913 )
9914 .unwrap();
9915 let v = b.as_primitive::<TimestampMillisecondType>();
9916
9917 assert_eq!(v.value(0), 946728000000);
9918 assert_eq!(v.value(1), 1608035696000);
9919 }
9920
9921 #[test]
9922 fn test_cast_utf8_to_timestamp() {
9923 fn test_tz(tz: Arc<str>) {
9924 let valid = StringArray::from(vec![
9925 "2023-01-01 04:05:06.789000-08:00",
9926 "2023-01-01 04:05:06.789000-07:00",
9927 "2023-01-01 04:05:06.789 -0800",
9928 "2023-01-01 04:05:06.789 -08:00",
9929 "2023-01-01 040506 +0730",
9930 "2023-01-01 040506 +07:30",
9931 "2023-01-01 04:05:06.789",
9932 "2023-01-01 04:05:06",
9933 "2023-01-01",
9934 ]);
9935
9936 let array = Arc::new(valid) as ArrayRef;
9937 let b = cast_with_options(
9938 &array,
9939 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
9940 &CastOptions {
9941 safe: false,
9942 format_options: FormatOptions::default(),
9943 },
9944 )
9945 .unwrap();
9946
9947 let tz = tz.as_ref().parse().unwrap();
9948
9949 let as_tz =
9950 |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
9951
9952 let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
9953 let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
9954
9955 let values = b.as_primitive::<TimestampNanosecondType>().values();
9956 let utc_results: Vec<_> = values.iter().map(as_utc).collect();
9957 let local_results: Vec<_> = values.iter().map(as_local).collect();
9958
9959 assert_eq!(
9961 &utc_results[..6],
9962 &[
9963 "2023-01-01 12:05:06.789".to_string(),
9964 "2023-01-01 11:05:06.789".to_string(),
9965 "2023-01-01 12:05:06.789".to_string(),
9966 "2023-01-01 12:05:06.789".to_string(),
9967 "2022-12-31 20:35:06".to_string(),
9968 "2022-12-31 20:35:06".to_string(),
9969 ]
9970 );
9971 assert_eq!(
9973 &local_results[6..],
9974 &[
9975 "2023-01-01 04:05:06.789".to_string(),
9976 "2023-01-01 04:05:06".to_string(),
9977 "2023-01-01 00:00:00".to_string()
9978 ]
9979 )
9980 }
9981
9982 test_tz("+00:00".into());
9983 test_tz("+02:00".into());
9984 }
9985
9986 #[test]
9987 fn test_cast_invalid_utf8() {
9988 let v1: &[u8] = b"\xFF invalid";
9989 let v2: &[u8] = b"\x00 Foo";
9990 let s = BinaryArray::from(vec![v1, v2]);
9991 let options = CastOptions {
9992 safe: true,
9993 format_options: FormatOptions::default(),
9994 };
9995 let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
9996 let a = array.as_string::<i32>();
9997 a.to_data().validate_full().unwrap();
9998
9999 assert_eq!(a.null_count(), 1);
10000 assert_eq!(a.len(), 2);
10001 assert!(a.is_null(0));
10002 assert_eq!(a.value(0), "");
10003 assert_eq!(a.value(1), "\x00 Foo");
10004 }
10005
10006 #[test]
10007 fn test_cast_utf8_to_timestamptz() {
10008 let valid = StringArray::from(vec!["2023-01-01"]);
10009
10010 let array = Arc::new(valid) as ArrayRef;
10011 let b = cast(
10012 &array,
10013 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10014 )
10015 .unwrap();
10016
10017 let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
10018
10019 assert_eq!(b.data_type(), &expect);
10020 let c = b
10021 .as_any()
10022 .downcast_ref::<TimestampNanosecondArray>()
10023 .unwrap();
10024 assert_eq!(1672531200000000000, c.value(0));
10025 }
10026
10027 #[test]
10028 fn test_cast_decimal_to_string() {
10029 assert!(can_cast_types(
10030 &DataType::Decimal32(9, 4),
10031 &DataType::Utf8View
10032 ));
10033 assert!(can_cast_types(
10034 &DataType::Decimal64(16, 4),
10035 &DataType::Utf8View
10036 ));
10037 assert!(can_cast_types(
10038 &DataType::Decimal128(10, 4),
10039 &DataType::Utf8View
10040 ));
10041 assert!(can_cast_types(
10042 &DataType::Decimal256(38, 10),
10043 &DataType::Utf8View
10044 ));
10045
10046 macro_rules! assert_decimal_values {
10047 ($array:expr) => {
10048 let c = $array;
10049 assert_eq!("1123.454", c.value(0));
10050 assert_eq!("2123.456", c.value(1));
10051 assert_eq!("-3123.453", c.value(2));
10052 assert_eq!("-3123.456", c.value(3));
10053 assert_eq!("0.000", c.value(4));
10054 assert_eq!("0.123", c.value(5));
10055 assert_eq!("1234.567", c.value(6));
10056 assert_eq!("-1234.567", c.value(7));
10057 assert!(c.is_null(8));
10058 };
10059 }
10060
10061 fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
10062 output_type: DataType,
10063 array: PrimitiveArray<IN>,
10064 ) {
10065 let b = cast(&array, &output_type).unwrap();
10066
10067 assert_eq!(b.data_type(), &output_type);
10068 match b.data_type() {
10069 DataType::Utf8View => {
10070 let c = b.as_string_view();
10071 assert_decimal_values!(c);
10072 }
10073 DataType::Utf8 | DataType::LargeUtf8 => {
10074 let c = b.as_string::<OffsetSize>();
10075 assert_decimal_values!(c);
10076 }
10077 _ => (),
10078 }
10079 }
10080
10081 let array32: Vec<Option<i32>> = vec![
10082 Some(1123454),
10083 Some(2123456),
10084 Some(-3123453),
10085 Some(-3123456),
10086 Some(0),
10087 Some(123),
10088 Some(123456789),
10089 Some(-123456789),
10090 None,
10091 ];
10092 let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
10093 let array128: Vec<Option<i128>> =
10094 array64.iter().map(|num| num.map(|x| x as i128)).collect();
10095 let array256: Vec<Option<i256>> = array128
10096 .iter()
10097 .map(|num| num.map(i256::from_i128))
10098 .collect();
10099
10100 test_decimal_to_string::<Decimal32Type, i32>(
10101 DataType::Utf8View,
10102 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10103 );
10104 test_decimal_to_string::<Decimal32Type, i32>(
10105 DataType::Utf8,
10106 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10107 );
10108 test_decimal_to_string::<Decimal32Type, i64>(
10109 DataType::LargeUtf8,
10110 create_decimal32_array(array32, 7, 3).unwrap(),
10111 );
10112
10113 test_decimal_to_string::<Decimal64Type, i32>(
10114 DataType::Utf8View,
10115 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10116 );
10117 test_decimal_to_string::<Decimal64Type, i32>(
10118 DataType::Utf8,
10119 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10120 );
10121 test_decimal_to_string::<Decimal64Type, i64>(
10122 DataType::LargeUtf8,
10123 create_decimal64_array(array64, 7, 3).unwrap(),
10124 );
10125
10126 test_decimal_to_string::<Decimal128Type, i32>(
10127 DataType::Utf8View,
10128 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10129 );
10130 test_decimal_to_string::<Decimal128Type, i32>(
10131 DataType::Utf8,
10132 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10133 );
10134 test_decimal_to_string::<Decimal128Type, i64>(
10135 DataType::LargeUtf8,
10136 create_decimal128_array(array128, 7, 3).unwrap(),
10137 );
10138
10139 test_decimal_to_string::<Decimal256Type, i32>(
10140 DataType::Utf8View,
10141 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10142 );
10143 test_decimal_to_string::<Decimal256Type, i32>(
10144 DataType::Utf8,
10145 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10146 );
10147 test_decimal_to_string::<Decimal256Type, i64>(
10148 DataType::LargeUtf8,
10149 create_decimal256_array(array256, 7, 3).unwrap(),
10150 );
10151 }
10152
10153 #[test]
10154 fn test_cast_numeric_to_decimal128_precision_overflow() {
10155 let array = Int64Array::from(vec![1234567]);
10156 let array = Arc::new(array) as ArrayRef;
10157 let casted_array = cast_with_options(
10158 &array,
10159 &DataType::Decimal128(7, 3),
10160 &CastOptions {
10161 safe: true,
10162 format_options: FormatOptions::default(),
10163 },
10164 );
10165 assert!(casted_array.is_ok());
10166 assert!(casted_array.unwrap().is_null(0));
10167
10168 let err = cast_with_options(
10169 &array,
10170 &DataType::Decimal128(7, 3),
10171 &CastOptions {
10172 safe: false,
10173 format_options: FormatOptions::default(),
10174 },
10175 );
10176 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());
10177 }
10178
10179 #[test]
10180 fn test_cast_numeric_to_decimal256_precision_overflow() {
10181 let array = Int64Array::from(vec![1234567]);
10182 let array = Arc::new(array) as ArrayRef;
10183 let casted_array = cast_with_options(
10184 &array,
10185 &DataType::Decimal256(7, 3),
10186 &CastOptions {
10187 safe: true,
10188 format_options: FormatOptions::default(),
10189 },
10190 );
10191 assert!(casted_array.is_ok());
10192 assert!(casted_array.unwrap().is_null(0));
10193
10194 let err = cast_with_options(
10195 &array,
10196 &DataType::Decimal256(7, 3),
10197 &CastOptions {
10198 safe: false,
10199 format_options: FormatOptions::default(),
10200 },
10201 );
10202 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());
10203 }
10204
10205 fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
10207 array: Vec<i64>,
10208 cast_options: &CastOptions,
10209 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10210 let array = PrimitiveArray::<T>::new(array.into(), None);
10211 let array = Arc::new(array) as ArrayRef;
10212 let interval = DataType::Interval(IntervalUnit::MonthDayNano);
10213 let out = cast_with_options(&array, &interval, cast_options)?;
10214 let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
10215 Ok(out)
10216 }
10217
10218 #[test]
10219 fn test_cast_from_duration_to_interval() {
10220 let array = vec![1234567];
10222 let casted_array =
10223 cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
10224 .unwrap();
10225 assert_eq!(
10226 casted_array.data_type(),
10227 &DataType::Interval(IntervalUnit::MonthDayNano)
10228 );
10229 assert_eq!(
10230 casted_array.value(0),
10231 IntervalMonthDayNano::new(0, 0, 1234567000000000)
10232 );
10233
10234 let array = vec![i64::MAX];
10235 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10236 array.clone(),
10237 &CastOptions::default(),
10238 )
10239 .unwrap();
10240 assert!(!casted_array.is_valid(0));
10241
10242 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10243 array,
10244 &CastOptions {
10245 safe: false,
10246 format_options: FormatOptions::default(),
10247 },
10248 );
10249 assert!(casted_array.is_err());
10250
10251 let array = vec![1234567];
10253 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10254 array,
10255 &CastOptions::default(),
10256 )
10257 .unwrap();
10258 assert_eq!(
10259 casted_array.data_type(),
10260 &DataType::Interval(IntervalUnit::MonthDayNano)
10261 );
10262 assert_eq!(
10263 casted_array.value(0),
10264 IntervalMonthDayNano::new(0, 0, 1234567000000)
10265 );
10266
10267 let array = vec![i64::MAX];
10268 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10269 array.clone(),
10270 &CastOptions::default(),
10271 )
10272 .unwrap();
10273 assert!(!casted_array.is_valid(0));
10274
10275 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10276 array,
10277 &CastOptions {
10278 safe: false,
10279 format_options: FormatOptions::default(),
10280 },
10281 );
10282 assert!(casted_array.is_err());
10283
10284 let array = vec![1234567];
10286 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10287 array,
10288 &CastOptions::default(),
10289 )
10290 .unwrap();
10291 assert_eq!(
10292 casted_array.data_type(),
10293 &DataType::Interval(IntervalUnit::MonthDayNano)
10294 );
10295 assert_eq!(
10296 casted_array.value(0),
10297 IntervalMonthDayNano::new(0, 0, 1234567000)
10298 );
10299
10300 let array = vec![i64::MAX];
10301 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10302 array.clone(),
10303 &CastOptions::default(),
10304 )
10305 .unwrap();
10306 assert!(!casted_array.is_valid(0));
10307
10308 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10309 array,
10310 &CastOptions {
10311 safe: false,
10312 format_options: FormatOptions::default(),
10313 },
10314 );
10315 assert!(casted_array.is_err());
10316
10317 let array = vec![1234567];
10319 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10320 array,
10321 &CastOptions::default(),
10322 )
10323 .unwrap();
10324 assert_eq!(
10325 casted_array.data_type(),
10326 &DataType::Interval(IntervalUnit::MonthDayNano)
10327 );
10328 assert_eq!(
10329 casted_array.value(0),
10330 IntervalMonthDayNano::new(0, 0, 1234567)
10331 );
10332
10333 let array = vec![i64::MAX];
10334 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10335 array,
10336 &CastOptions {
10337 safe: false,
10338 format_options: FormatOptions::default(),
10339 },
10340 )
10341 .unwrap();
10342 assert_eq!(
10343 casted_array.value(0),
10344 IntervalMonthDayNano::new(0, 0, i64::MAX)
10345 );
10346 }
10347
10348 fn cast_from_interval_to_duration<T: ArrowTemporalType>(
10350 array: &IntervalMonthDayNanoArray,
10351 cast_options: &CastOptions,
10352 ) -> Result<PrimitiveArray<T>, ArrowError> {
10353 let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
10354 casted_array
10355 .as_any()
10356 .downcast_ref::<PrimitiveArray<T>>()
10357 .ok_or_else(|| {
10358 ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
10359 })
10360 .cloned()
10361 }
10362
10363 #[test]
10364 fn test_cast_from_interval_to_duration() {
10365 let nullable = CastOptions::default();
10366 let fallible = CastOptions {
10367 safe: false,
10368 format_options: FormatOptions::default(),
10369 };
10370 let v = IntervalMonthDayNano::new(0, 0, 1234567);
10371
10372 let array = vec![v].into();
10374 let casted_array: DurationSecondArray =
10375 cast_from_interval_to_duration(&array, &nullable).unwrap();
10376 assert_eq!(casted_array.value(0), 0);
10377
10378 let array = vec![IntervalMonthDayNano::MAX].into();
10379 let casted_array: DurationSecondArray =
10380 cast_from_interval_to_duration(&array, &nullable).unwrap();
10381 assert!(!casted_array.is_valid(0));
10382
10383 let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
10384 assert!(res.is_err());
10385
10386 let array = vec![v].into();
10388 let casted_array: DurationMillisecondArray =
10389 cast_from_interval_to_duration(&array, &nullable).unwrap();
10390 assert_eq!(casted_array.value(0), 1);
10391
10392 let array = vec![IntervalMonthDayNano::MAX].into();
10393 let casted_array: DurationMillisecondArray =
10394 cast_from_interval_to_duration(&array, &nullable).unwrap();
10395 assert!(!casted_array.is_valid(0));
10396
10397 let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
10398 assert!(res.is_err());
10399
10400 let array = vec![v].into();
10402 let casted_array: DurationMicrosecondArray =
10403 cast_from_interval_to_duration(&array, &nullable).unwrap();
10404 assert_eq!(casted_array.value(0), 1234);
10405
10406 let array = vec![IntervalMonthDayNano::MAX].into();
10407 let casted_array =
10408 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
10409 assert!(!casted_array.is_valid(0));
10410
10411 let casted_array =
10412 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
10413 assert!(casted_array.is_err());
10414
10415 let array = vec![v].into();
10417 let casted_array: DurationNanosecondArray =
10418 cast_from_interval_to_duration(&array, &nullable).unwrap();
10419 assert_eq!(casted_array.value(0), 1234567);
10420
10421 let array = vec![IntervalMonthDayNano::MAX].into();
10422 let casted_array: DurationNanosecondArray =
10423 cast_from_interval_to_duration(&array, &nullable).unwrap();
10424 assert!(!casted_array.is_valid(0));
10425
10426 let casted_array =
10427 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
10428 assert!(casted_array.is_err());
10429
10430 let array = vec![
10431 IntervalMonthDayNanoType::make_value(0, 1, 0),
10432 IntervalMonthDayNanoType::make_value(-1, 0, 0),
10433 IntervalMonthDayNanoType::make_value(1, 1, 0),
10434 IntervalMonthDayNanoType::make_value(1, 0, 1),
10435 IntervalMonthDayNanoType::make_value(0, 0, -1),
10436 ]
10437 .into();
10438 let casted_array =
10439 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
10440 assert!(!casted_array.is_valid(0));
10441 assert!(!casted_array.is_valid(1));
10442 assert!(!casted_array.is_valid(2));
10443 assert!(!casted_array.is_valid(3));
10444 assert!(casted_array.is_valid(4));
10445 assert_eq!(casted_array.value(4), -1);
10446 }
10447
10448 fn cast_from_interval_year_month_to_interval_month_day_nano(
10450 array: Vec<i32>,
10451 cast_options: &CastOptions,
10452 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10453 let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
10454 let array = Arc::new(array) as ArrayRef;
10455 let casted_array = cast_with_options(
10456 &array,
10457 &DataType::Interval(IntervalUnit::MonthDayNano),
10458 cast_options,
10459 )?;
10460 casted_array
10461 .as_any()
10462 .downcast_ref::<IntervalMonthDayNanoArray>()
10463 .ok_or_else(|| {
10464 ArrowError::ComputeError(
10465 "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
10466 )
10467 })
10468 .cloned()
10469 }
10470
10471 #[test]
10472 fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
10473 let array = vec![1234567];
10475 let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
10476 array,
10477 &CastOptions::default(),
10478 )
10479 .unwrap();
10480 assert_eq!(
10481 casted_array.data_type(),
10482 &DataType::Interval(IntervalUnit::MonthDayNano)
10483 );
10484 assert_eq!(
10485 casted_array.value(0),
10486 IntervalMonthDayNano::new(1234567, 0, 0)
10487 );
10488 }
10489
10490 fn cast_from_interval_day_time_to_interval_month_day_nano(
10492 array: Vec<IntervalDayTime>,
10493 cast_options: &CastOptions,
10494 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10495 let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
10496 let array = Arc::new(array) as ArrayRef;
10497 let casted_array = cast_with_options(
10498 &array,
10499 &DataType::Interval(IntervalUnit::MonthDayNano),
10500 cast_options,
10501 )?;
10502 Ok(casted_array
10503 .as_primitive::<IntervalMonthDayNanoType>()
10504 .clone())
10505 }
10506
10507 #[test]
10508 fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
10509 let array = vec![IntervalDayTime::new(123, 0)];
10511 let casted_array =
10512 cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
10513 .unwrap();
10514 assert_eq!(
10515 casted_array.data_type(),
10516 &DataType::Interval(IntervalUnit::MonthDayNano)
10517 );
10518 assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
10519 }
10520
10521 #[test]
10522 fn test_cast_below_unixtimestamp() {
10523 let valid = StringArray::from(vec![
10524 "1900-01-03 23:59:59",
10525 "1969-12-31 00:00:01",
10526 "1989-12-31 00:00:01",
10527 ]);
10528
10529 let array = Arc::new(valid) as ArrayRef;
10530 let casted_array = cast_with_options(
10531 &array,
10532 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10533 &CastOptions {
10534 safe: false,
10535 format_options: FormatOptions::default(),
10536 },
10537 )
10538 .unwrap();
10539
10540 let ts_array = casted_array
10541 .as_primitive::<TimestampNanosecondType>()
10542 .values()
10543 .iter()
10544 .map(|ts| ts / 1_000_000)
10545 .collect::<Vec<_>>();
10546
10547 let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
10548 let casted_array = cast(&array, &DataType::Date32).unwrap();
10549 let date_array = casted_array.as_primitive::<Date32Type>();
10550 let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
10551 let string_array = casted_array.as_string::<i32>();
10552 assert_eq!("1900-01-03", string_array.value(0));
10553 assert_eq!("1969-12-31", string_array.value(1));
10554 assert_eq!("1989-12-31", string_array.value(2));
10555 }
10556
10557 #[test]
10558 fn test_nested_list() {
10559 let mut list = ListBuilder::new(Int32Builder::new());
10560 list.append_value([Some(1), Some(2), Some(3)]);
10561 list.append_value([Some(4), None, Some(6)]);
10562 let list = list.finish();
10563
10564 let to_field = Field::new("nested", list.data_type().clone(), false);
10565 let to = DataType::List(Arc::new(to_field));
10566 let out = cast(&list, &to).unwrap();
10567 let opts = FormatOptions::default().with_null("null");
10568 let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
10569
10570 assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
10571 assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
10572 }
10573
10574 #[test]
10575 fn test_nested_list_cast() {
10576 let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
10577 builder.append_value([Some([Some(1), Some(2), None]), None]);
10578 builder.append_value([None, Some([]), None]);
10579 builder.append_null();
10580 builder.append_value([Some([Some(2), Some(3)])]);
10581 let start = builder.finish();
10582
10583 let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
10584 builder.append_value([Some([Some(1), Some(2), None]), None]);
10585 builder.append_value([None, Some([]), None]);
10586 builder.append_null();
10587 builder.append_value([Some([Some(2), Some(3)])]);
10588 let expected = builder.finish();
10589
10590 let actual = cast(&start, expected.data_type()).unwrap();
10591 assert_eq!(actual.as_ref(), &expected);
10592 }
10593
10594 const CAST_OPTIONS: CastOptions<'static> = CastOptions {
10595 safe: true,
10596 format_options: FormatOptions::new(),
10597 };
10598
10599 #[test]
10600 #[allow(clippy::assertions_on_constants)]
10601 fn test_const_options() {
10602 assert!(CAST_OPTIONS.safe)
10603 }
10604
10605 #[test]
10606 fn test_list_format_options() {
10607 let options = CastOptions {
10608 safe: false,
10609 format_options: FormatOptions::default().with_null("null"),
10610 };
10611 let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10612 Some(vec![Some(0), Some(1), Some(2)]),
10613 Some(vec![Some(0), None, Some(2)]),
10614 ]);
10615 let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
10616 let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
10617 assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
10618 }
10619 #[test]
10620 fn test_cast_string_to_timestamp_invalid_tz() {
10621 let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
10623 let array = StringArray::from(vec![Some(bad_timestamp)]);
10624
10625 let data_types = [
10626 DataType::Timestamp(TimeUnit::Second, None),
10627 DataType::Timestamp(TimeUnit::Millisecond, None),
10628 DataType::Timestamp(TimeUnit::Microsecond, None),
10629 DataType::Timestamp(TimeUnit::Nanosecond, None),
10630 ];
10631
10632 let cast_options = CastOptions {
10633 safe: false,
10634 ..Default::default()
10635 };
10636
10637 for dt in data_types {
10638 assert_eq!(
10639 cast_with_options(&array, &dt, &cast_options)
10640 .unwrap_err()
10641 .to_string(),
10642 "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
10643 );
10644 }
10645 }
10646 #[test]
10647 fn test_cast_struct_to_struct() {
10648 let struct_type = DataType::Struct(
10649 vec![
10650 Field::new("a", DataType::Boolean, false),
10651 Field::new("b", DataType::Int32, false),
10652 ]
10653 .into(),
10654 );
10655 let to_type = DataType::Struct(
10656 vec![
10657 Field::new("a", DataType::Utf8, false),
10658 Field::new("b", DataType::Utf8, false),
10659 ]
10660 .into(),
10661 );
10662 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10663 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
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, false)),
10671 int.clone() as ArrayRef,
10672 ),
10673 ]);
10674 let casted_array = cast(&struct_array, &to_type).unwrap();
10675 let casted_array = casted_array.as_struct();
10676 assert_eq!(casted_array.data_type(), &to_type);
10677 let casted_boolean_array = casted_array
10678 .column(0)
10679 .as_string::<i32>()
10680 .into_iter()
10681 .flatten()
10682 .collect::<Vec<_>>();
10683 let casted_int_array = casted_array
10684 .column(1)
10685 .as_string::<i32>()
10686 .into_iter()
10687 .flatten()
10688 .collect::<Vec<_>>();
10689 assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
10690 assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
10691
10692 let to_type = DataType::Struct(
10694 vec![
10695 Field::new("a", DataType::Date32, false),
10696 Field::new("b", DataType::Utf8, false),
10697 ]
10698 .into(),
10699 );
10700 assert!(!can_cast_types(&struct_type, &to_type));
10701 let result = cast(&struct_array, &to_type);
10702 assert_eq!(
10703 "Cast error: Casting from Boolean to Date32 not supported",
10704 result.unwrap_err().to_string()
10705 );
10706 }
10707
10708 #[test]
10709 fn test_cast_struct_to_struct_nullability() {
10710 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10711 let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
10712 let struct_array = StructArray::from(vec![
10713 (
10714 Arc::new(Field::new("b", DataType::Boolean, false)),
10715 boolean.clone() as ArrayRef,
10716 ),
10717 (
10718 Arc::new(Field::new("c", DataType::Int32, true)),
10719 int.clone() as ArrayRef,
10720 ),
10721 ]);
10722
10723 let to_type = DataType::Struct(
10725 vec![
10726 Field::new("a", DataType::Utf8, false),
10727 Field::new("b", DataType::Utf8, true),
10728 ]
10729 .into(),
10730 );
10731 cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
10732
10733 let to_type = DataType::Struct(
10735 vec![
10736 Field::new("a", DataType::Utf8, false),
10737 Field::new("b", DataType::Utf8, false),
10738 ]
10739 .into(),
10740 );
10741 cast(&struct_array, &to_type)
10742 .expect_err("Cast nullable to non-nullable struct field should fail");
10743
10744 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10745 let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
10746 let struct_array = StructArray::from(vec![
10747 (
10748 Arc::new(Field::new("b", DataType::Boolean, false)),
10749 boolean.clone() as ArrayRef,
10750 ),
10751 (
10752 Arc::new(Field::new("c", DataType::Int32, false)),
10753 int.clone() as ArrayRef,
10754 ),
10755 ]);
10756
10757 let to_type = DataType::Struct(
10759 vec![
10760 Field::new("a", DataType::Utf8, false),
10761 Field::new("b", DataType::Utf8, false),
10762 ]
10763 .into(),
10764 );
10765 cast(&struct_array, &to_type)
10766 .expect("Cast non-nullable to non-nullable struct field should work");
10767
10768 let to_type = DataType::Struct(
10770 vec![
10771 Field::new("a", DataType::Utf8, false),
10772 Field::new("b", DataType::Int8, false),
10773 ]
10774 .into(),
10775 );
10776 cast(&struct_array, &to_type).expect_err(
10777 "Cast non-nullable to non-nullable struct field returning null should fail",
10778 );
10779 }
10780
10781 #[test]
10782 fn test_cast_struct_to_non_struct() {
10783 let boolean = Arc::new(BooleanArray::from(vec![true, false]));
10784 let struct_array = StructArray::from(vec![(
10785 Arc::new(Field::new("a", DataType::Boolean, false)),
10786 boolean.clone() as ArrayRef,
10787 )]);
10788 let to_type = DataType::Utf8;
10789 let result = cast(&struct_array, &to_type);
10790 assert_eq!(
10791 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"#,
10792 result.unwrap_err().to_string()
10793 );
10794 }
10795
10796 #[test]
10797 fn test_cast_non_struct_to_struct() {
10798 let array = StringArray::from(vec!["a", "b"]);
10799 let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
10800 let result = cast(&array, &to_type);
10801 assert_eq!(
10802 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"#,
10803 result.unwrap_err().to_string()
10804 );
10805 }
10806
10807 fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
10808 run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
10809 run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
10810 run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
10811 run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
10812 }
10813
10814 #[test]
10815 fn test_decimal_to_decimal_coverage() {
10816 let test_cases = [
10817 DecimalCastTestConfig {
10819 input_prec: 5,
10820 input_scale: 1,
10821 input_repr: 99999, output_prec: 10,
10823 output_scale: 6,
10824 expected_output_repr: Ok(9999900000), },
10826 DecimalCastTestConfig {
10828 input_prec: 5,
10829 input_scale: 1,
10830 input_repr: 99, output_prec: 7,
10832 output_scale: 6,
10833 expected_output_repr: Ok(9900000), },
10835 DecimalCastTestConfig {
10837 input_prec: 5,
10838 input_scale: 1,
10839 input_repr: 99999, output_prec: 7,
10841 output_scale: 6,
10842 expected_output_repr: Err("Invalid argument error: 9999900000 is too large to store in a {} of precision 7. Max is 9999999".to_string()) },
10844 DecimalCastTestConfig {
10846 input_prec: 5,
10847 input_scale: 3,
10848 input_repr: 99999, output_prec: 10,
10850 output_scale: 2,
10851 expected_output_repr: Ok(10000), },
10853 DecimalCastTestConfig {
10855 input_prec: 5,
10856 input_scale: 3,
10857 input_repr: 99994, output_prec: 10,
10859 output_scale: 2,
10860 expected_output_repr: Ok(9999), },
10862 DecimalCastTestConfig {
10864 input_prec: 5,
10865 input_scale: 3,
10866 input_repr: 99999, output_prec: 10,
10868 output_scale: 3,
10869 expected_output_repr: Ok(99999), },
10871 DecimalCastTestConfig {
10873 input_prec: 10,
10874 input_scale: 5,
10875 input_repr: 999999, output_prec: 8,
10877 output_scale: 7,
10878 expected_output_repr: Ok(99999900), },
10880 DecimalCastTestConfig {
10882 input_prec: 10,
10883 input_scale: 5,
10884 input_repr: 9999999, output_prec: 8,
10886 output_scale: 7,
10887 expected_output_repr: Err("Invalid argument error: 999999900 is too large to store in a {} of precision 8. Max is 99999999".to_string()) },
10889 DecimalCastTestConfig {
10891 input_prec: 7,
10892 input_scale: 4,
10893 input_repr: 9999999, output_prec: 6,
10895 output_scale: 2,
10896 expected_output_repr: Ok(100000),
10897 },
10898 DecimalCastTestConfig {
10900 input_prec: 10,
10901 input_scale: 5,
10902 input_repr: 12345678, output_prec: 8,
10904 output_scale: 3,
10905 expected_output_repr: Ok(123457), },
10907 DecimalCastTestConfig {
10909 input_prec: 10,
10910 input_scale: 5,
10911 input_repr: 9999999, output_prec: 4,
10913 output_scale: 3,
10914 expected_output_repr: Err("Invalid argument error: 100000 is too large to store in a {} of precision 4. Max is 9999".to_string()) },
10916 DecimalCastTestConfig {
10918 input_prec: 10,
10919 input_scale: 5,
10920 input_repr: 999999, output_prec: 6,
10922 output_scale: 5,
10923 expected_output_repr: Ok(999999), },
10925 DecimalCastTestConfig {
10927 input_prec: 10,
10928 input_scale: 5,
10929 input_repr: 9999999, output_prec: 6,
10931 output_scale: 5,
10932 expected_output_repr: Err("Invalid argument error: 9999999 is too large to store in a {} of precision 6. Max is 999999".to_string()) },
10934 DecimalCastTestConfig {
10936 input_prec: 7,
10937 input_scale: 4,
10938 input_repr: 12345, output_prec: 7,
10940 output_scale: 6,
10941 expected_output_repr: Ok(1234500), },
10943 DecimalCastTestConfig {
10945 input_prec: 7,
10946 input_scale: 4,
10947 input_repr: 123456, output_prec: 7,
10949 output_scale: 6,
10950 expected_output_repr: Err("Invalid argument error: 12345600 is too large to store in a {} of precision 7. Max is 9999999".to_string()) },
10952 DecimalCastTestConfig {
10954 input_prec: 7,
10955 input_scale: 5,
10956 input_repr: 1234567, output_prec: 7,
10958 output_scale: 4,
10959 expected_output_repr: Ok(123457), },
10961 DecimalCastTestConfig {
10963 input_prec: 7,
10964 input_scale: 5,
10965 input_repr: 9999999, output_prec: 7,
10967 output_scale: 5,
10968 expected_output_repr: Ok(9999999), },
10970 DecimalCastTestConfig {
10972 input_prec: 7,
10973 input_scale: 0,
10974 input_repr: 1234567, output_prec: 8,
10976 output_scale: 0,
10977 expected_output_repr: Ok(1234567), },
10979 DecimalCastTestConfig {
10981 input_prec: 7,
10982 input_scale: 0,
10983 input_repr: 1234567, output_prec: 6,
10985 output_scale: 0,
10986 expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
10987 },
10988 DecimalCastTestConfig {
10990 input_prec: 7,
10991 input_scale: 0,
10992 input_repr: 123456, output_prec: 6,
10994 output_scale: 0,
10995 expected_output_repr: Ok(123456), },
10997 ];
10998
10999 for t in test_cases {
11000 run_decimal_cast_test_case_between_multiple_types(t);
11001 }
11002 }
11003
11004 #[test]
11005 fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
11006 let test_cases = [
11007 DecimalCastTestConfig {
11008 input_prec: 5,
11009 input_scale: 0,
11010 input_repr: 99999,
11011 output_prec: 10,
11012 output_scale: 5,
11013 expected_output_repr: Ok(9999900000),
11014 },
11015 DecimalCastTestConfig {
11016 input_prec: 5,
11017 input_scale: 0,
11018 input_repr: -99999,
11019 output_prec: 10,
11020 output_scale: 5,
11021 expected_output_repr: Ok(-9999900000),
11022 },
11023 DecimalCastTestConfig {
11024 input_prec: 5,
11025 input_scale: 2,
11026 input_repr: 99999,
11027 output_prec: 10,
11028 output_scale: 5,
11029 expected_output_repr: Ok(99999000),
11030 },
11031 DecimalCastTestConfig {
11032 input_prec: 5,
11033 input_scale: -2,
11034 input_repr: -99999,
11035 output_prec: 10,
11036 output_scale: 3,
11037 expected_output_repr: Ok(-9999900000),
11038 },
11039 DecimalCastTestConfig {
11040 input_prec: 5,
11041 input_scale: 3,
11042 input_repr: -12345,
11043 output_prec: 6,
11044 output_scale: 5,
11045 expected_output_repr: Err("Invalid argument error: -1234500 is too small to store in a {} of precision 6. Min is -999999".to_string())
11046 },
11047 ];
11048
11049 for t in test_cases {
11050 run_decimal_cast_test_case_between_multiple_types(t);
11051 }
11052 }
11053
11054 #[test]
11055 fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
11056 let test_cases = [
11057 DecimalCastTestConfig {
11058 input_prec: 5,
11059 input_scale: 0,
11060 input_repr: 99999,
11061 output_scale: -3,
11062 output_prec: 3,
11063 expected_output_repr: Ok(100),
11064 },
11065 DecimalCastTestConfig {
11066 input_prec: 5,
11067 input_scale: 0,
11068 input_repr: -99999,
11069 output_prec: 1,
11070 output_scale: -5,
11071 expected_output_repr: Ok(-1),
11072 },
11073 DecimalCastTestConfig {
11074 input_prec: 10,
11075 input_scale: 2,
11076 input_repr: 123456789,
11077 output_prec: 5,
11078 output_scale: -2,
11079 expected_output_repr: Ok(12346),
11080 },
11081 DecimalCastTestConfig {
11082 input_prec: 10,
11083 input_scale: 4,
11084 input_repr: -9876543210,
11085 output_prec: 7,
11086 output_scale: 0,
11087 expected_output_repr: Ok(-987654),
11088 },
11089 DecimalCastTestConfig {
11090 input_prec: 7,
11091 input_scale: 4,
11092 input_repr: 9999999,
11093 output_prec: 6,
11094 output_scale: 3,
11095 expected_output_repr:
11096 Err("Invalid argument error: 1000000 is too large to store in a {} of precision 6. Max is 999999".to_string()),
11097 },
11098 ];
11099 for t in test_cases {
11100 run_decimal_cast_test_case_between_multiple_types(t);
11101 }
11102 }
11103
11104 #[test]
11105 fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
11106 let array = vec![Some(123456789)];
11107 let array = create_decimal128_array(array, 24, 2).unwrap();
11108 let input_type = DataType::Decimal128(24, 2);
11109 let output_type = DataType::Decimal128(6, 2);
11110 assert!(can_cast_types(&input_type, &output_type));
11111
11112 let options = CastOptions {
11113 safe: false,
11114 ..Default::default()
11115 };
11116 let result = cast_with_options(&array, &output_type, &options);
11117 assert_eq!(result.unwrap_err().to_string(),
11118 "Invalid argument error: 123456789 is too large to store in a Decimal128 of precision 6. Max is 999999");
11119 }
11120
11121 #[test]
11122 fn test_decimal_to_decimal_same_scale() {
11123 let array = vec![Some(520)];
11124 let array = create_decimal128_array(array, 4, 2).unwrap();
11125 let input_type = DataType::Decimal128(4, 2);
11126 let output_type = DataType::Decimal128(3, 2);
11127 assert!(can_cast_types(&input_type, &output_type));
11128
11129 let options = CastOptions {
11130 safe: false,
11131 ..Default::default()
11132 };
11133 let result = cast_with_options(&array, &output_type, &options);
11134 assert_eq!(
11135 result.unwrap().as_primitive::<Decimal128Type>().value(0),
11136 520
11137 );
11138
11139 assert_eq!(
11141 &cast(
11142 &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
11143 &DataType::Decimal128(2, 0)
11144 )
11145 .unwrap(),
11146 &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
11147 );
11148 }
11149
11150 #[test]
11151 fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
11152 let array = vec![Some(123456789)];
11153 let array = create_decimal128_array(array, 24, 4).unwrap();
11154 let input_type = DataType::Decimal128(24, 4);
11155 let output_type = DataType::Decimal128(6, 2);
11156 assert!(can_cast_types(&input_type, &output_type));
11157
11158 let options = CastOptions {
11159 safe: false,
11160 ..Default::default()
11161 };
11162 let result = cast_with_options(&array, &output_type, &options);
11163 assert_eq!(result.unwrap_err().to_string(),
11164 "Invalid argument error: 1234568 is too large to store in a Decimal128 of precision 6. Max is 999999");
11165 }
11166
11167 #[test]
11168 fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
11169 let array = vec![Some(123456789)];
11170 let array = create_decimal128_array(array, 24, 2).unwrap();
11171 let input_type = DataType::Decimal128(24, 2);
11172 let output_type = DataType::Decimal128(6, 3);
11173 assert!(can_cast_types(&input_type, &output_type));
11174
11175 let options = CastOptions {
11176 safe: false,
11177 ..Default::default()
11178 };
11179 let result = cast_with_options(&array, &output_type, &options);
11180 assert_eq!(result.unwrap_err().to_string(),
11181 "Invalid argument error: 1234567890 is too large to store in a Decimal128 of precision 6. Max is 999999");
11182 }
11183
11184 #[test]
11185 fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
11186 let array = vec![Some(123456789)];
11187 let array = create_decimal128_array(array, 24, 2).unwrap();
11188 let input_type = DataType::Decimal128(24, 2);
11189 let output_type = DataType::Decimal256(6, 2);
11190 assert!(can_cast_types(&input_type, &output_type));
11191
11192 let options = CastOptions {
11193 safe: false,
11194 ..Default::default()
11195 };
11196 let result = cast_with_options(&array, &output_type, &options);
11197 assert_eq!(result.unwrap_err().to_string(),
11198 "Invalid argument error: 123456789 is too large to store in a Decimal256 of precision 6. Max is 999999");
11199 }
11200
11201 #[test]
11202 fn test_first_none() {
11203 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11204 None,
11205 Some(vec![Some(1), Some(2)]),
11206 ])) as ArrayRef;
11207 let data_type =
11208 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11209 let opt = CastOptions::default();
11210 let r = cast_with_options(&array, &data_type, &opt).unwrap();
11211
11212 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11213 vec![None, Some(vec![Some(1), Some(2)])],
11214 2,
11215 )) as ArrayRef;
11216 assert_eq!(*fixed_array, *r);
11217 }
11218
11219 #[test]
11220 fn test_first_last_none() {
11221 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11222 None,
11223 Some(vec![Some(1), Some(2)]),
11224 None,
11225 ])) as ArrayRef;
11226 let data_type =
11227 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11228 let opt = CastOptions::default();
11229 let r = cast_with_options(&array, &data_type, &opt).unwrap();
11230
11231 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11232 vec![None, Some(vec![Some(1), Some(2)]), None],
11233 2,
11234 )) as ArrayRef;
11235 assert_eq!(*fixed_array, *r);
11236 }
11237}