1mod decimal;
41mod dictionary;
42mod list;
43mod list_view;
44mod map;
45mod run_array;
46mod string;
47
48use crate::cast::decimal::*;
49use crate::cast::dictionary::*;
50use crate::cast::list::*;
51use crate::cast::map::*;
52use crate::cast::run_array::*;
53use crate::cast::string::*;
54
55use arrow_buffer::IntervalMonthDayNano;
56use arrow_data::ByteView;
57use chrono::{NaiveTime, Offset, TimeZone, Utc};
58use std::cmp::Ordering;
59use std::sync::Arc;
60
61use crate::display::{ArrayFormatter, FormatOptions};
62use crate::parse::{
63 Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
64 string_to_datetime,
65};
66use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
67use arrow_buffer::{ArrowNativeType, OffsetBuffer, i256};
68use arrow_data::ArrayData;
69use arrow_data::transform::MutableArrayData;
70use arrow_schema::*;
71use arrow_select::take::take;
72use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
73
74use crate::cast::list_view::{cast_list_to_list_view, cast_list_view, cast_list_view_to_list};
75pub use decimal::{DecimalCast, rescale_decimal};
76
77#[derive(Debug, Clone, PartialEq, Eq, Hash)]
79pub struct CastOptions<'a> {
80 pub safe: bool,
82 pub format_options: FormatOptions<'a>,
84}
85
86impl Default for CastOptions<'_> {
87 fn default() -> Self {
88 Self {
89 safe: true,
90 format_options: FormatOptions::default(),
91 }
92 }
93}
94
95pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
99 use self::DataType::*;
100 use self::IntervalUnit::*;
101 use self::TimeUnit::*;
102 if from_type == to_type {
103 return true;
104 }
105
106 match (from_type, to_type) {
107 (
108 Null,
109 Boolean
110 | Int8
111 | UInt8
112 | Int16
113 | UInt16
114 | Float16
115 | Int32
116 | UInt32
117 | Float32
118 | Date32
119 | Time32(_)
120 | Int64
121 | UInt64
122 | Float64
123 | Date64
124 | Timestamp(_, _)
125 | Time64(_)
126 | Duration(_)
127 | Interval(_)
128 | FixedSizeBinary(_)
129 | Binary
130 | Utf8
131 | LargeBinary
132 | LargeUtf8
133 | BinaryView
134 | Utf8View
135 | List(_)
136 | LargeList(_)
137 | FixedSizeList(_, _)
138 | Struct(_)
139 | Map(_, _)
140 | Dictionary(_, _),
141 ) => true,
142 (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
144 can_cast_types(from_value_type, to_value_type)
145 }
146 (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
147 (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
148 (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
149 (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
150 (List(list_from) | LargeList(list_from), List(list_to) | LargeList(list_to)) => {
151 can_cast_types(list_from.data_type(), list_to.data_type())
152 }
153 (List(list_from) | LargeList(list_from), Utf8 | LargeUtf8) => {
154 can_cast_types(list_from.data_type(), to_type)
155 }
156 (List(list_from) | LargeList(list_from), FixedSizeList(list_to, _)) => {
157 can_cast_types(list_from.data_type(), list_to.data_type())
158 }
159 (List(list_from) | LargeList(list_from), ListView(list_to) | LargeListView(list_to)) => {
160 can_cast_types(list_from.data_type(), list_to.data_type())
161 }
162 (List(_), _) => false,
163 (ListView(list_from) | LargeListView(list_from), List(list_to) | LargeList(list_to)) => {
164 can_cast_types(list_from.data_type(), list_to.data_type())
165 }
166 (ListView(list_from), LargeListView(list_to)) => {
167 can_cast_types(list_from.data_type(), list_to.data_type())
168 }
169 (LargeListView(list_from), ListView(list_to)) => {
170 can_cast_types(list_from.data_type(), list_to.data_type())
171 }
172 (FixedSizeList(list_from, _), List(list_to))
173 | (FixedSizeList(list_from, _), LargeList(list_to)) => {
174 can_cast_types(list_from.data_type(), list_to.data_type())
175 }
176 (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
177 can_cast_types(inner.data_type(), inner_to.data_type())
178 }
179 (_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
180 (_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
181 (_, FixedSizeList(list_to, size)) if *size == 1 => {
182 can_cast_types(from_type, list_to.data_type())
183 }
184 (FixedSizeList(list_from, size), _) if *size == 1 => {
185 can_cast_types(list_from.data_type(), to_type)
186 }
187 (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
188 if ordered_from == ordered_to =>
189 {
190 match (
191 key_field(from_entries),
192 key_field(to_entries),
193 value_field(from_entries),
194 value_field(to_entries),
195 ) {
196 (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
197 can_cast_types(from_key.data_type(), to_key.data_type())
198 && can_cast_types(from_value.data_type(), to_value.data_type())
199 }
200 _ => false,
201 }
202 }
203 (
205 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
206 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
207 ) => true,
208 (
210 UInt8 | UInt16 | UInt32 | UInt64,
211 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
212 ) => true,
213 (
215 Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
216 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
217 ) => true,
218 (
220 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
221 UInt8 | UInt16 | UInt32 | UInt64,
222 ) => true,
223 (
225 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
226 Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
227 ) => true,
228 (
230 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
231 Utf8View | Utf8 | LargeUtf8,
232 ) => true,
233 (
235 Utf8View | Utf8 | LargeUtf8,
236 Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
237 ) => true,
238 (Struct(from_fields), Struct(to_fields)) => {
239 if from_fields.len() != to_fields.len() {
240 return false;
241 }
242
243 if from_fields
245 .iter()
246 .zip(to_fields.iter())
247 .all(|(f1, f2)| f1.name() == f2.name())
248 {
249 return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
250 can_cast_types(f1.data_type(), f2.data_type())
253 });
254 }
255
256 if to_fields.iter().all(|to_field| {
258 from_fields
259 .iter()
260 .find(|from_field| from_field.name() == to_field.name())
261 .is_some_and(|from_field| {
262 can_cast_types(from_field.data_type(), to_field.data_type())
265 })
266 }) {
267 return true;
268 }
269
270 from_fields
272 .iter()
273 .zip(to_fields.iter())
274 .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
275 }
276 (Struct(_), _) => false,
277 (_, Struct(_)) => false,
278 (_, Boolean) => {
279 DataType::is_integer(from_type)
280 || DataType::is_floating(from_type)
281 || from_type == &Utf8View
282 || from_type == &Utf8
283 || from_type == &LargeUtf8
284 }
285 (Boolean, _) => {
286 DataType::is_integer(to_type)
287 || DataType::is_floating(to_type)
288 || to_type == &Utf8View
289 || to_type == &Utf8
290 || to_type == &LargeUtf8
291 }
292
293 (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
294 true
295 }
296 (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
297 true
298 }
299 (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
300 (
301 Utf8 | LargeUtf8 | Utf8View,
302 Binary
303 | LargeBinary
304 | Utf8
305 | LargeUtf8
306 | Date32
307 | Date64
308 | Time32(Second)
309 | Time32(Millisecond)
310 | Time64(Microsecond)
311 | Time64(Nanosecond)
312 | Timestamp(Second, _)
313 | Timestamp(Millisecond, _)
314 | Timestamp(Microsecond, _)
315 | Timestamp(Nanosecond, _)
316 | Interval(_)
317 | BinaryView,
318 ) => true,
319 (Utf8 | LargeUtf8, Utf8View) => true,
320 (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
321 (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
322 (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
323
324 (_, Binary | LargeBinary) => from_type.is_integer(),
325
326 (
328 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
329 | Float64,
330 UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
331 | Float64,
332 ) => true,
333 (Int32, Date32 | Date64 | Time32(_)) => true,
337 (Date32, Int32 | Int64) => true,
338 (Time32(_), Int32 | Int64) => true,
339 (Int64, Date64 | Date32 | Time64(_)) => true,
340 (Date64, Int64 | Int32) => true,
341 (Time64(_), Int64) => true,
342 (Date32 | Date64, Date32 | Date64) => true,
343 (Time32(_), Time32(_)) => true,
345 (Time32(_), Time64(_)) => true,
346 (Time64(_), Time64(_)) => true,
347 (Time64(_), Time32(to_unit)) => {
348 matches!(to_unit, Second | Millisecond)
349 }
350 (Timestamp(_, _), _) if to_type.is_numeric() => true,
351 (_, Timestamp(_, _)) if from_type.is_numeric() => true,
352 (Date64, Timestamp(_, _)) => true,
353 (Date32, Timestamp(_, _)) => true,
354 (
355 Timestamp(_, _),
356 Timestamp(_, _)
357 | Date32
358 | Date64
359 | Time32(Second)
360 | Time32(Millisecond)
361 | Time64(Microsecond)
362 | Time64(Nanosecond),
363 ) => true,
364 (_, Duration(_)) if from_type.is_numeric() => true,
365 (Duration(_), _) if to_type.is_numeric() => true,
366 (Duration(_), Duration(_)) => true,
367 (Interval(from_type), Int64) => {
368 match from_type {
369 YearMonth => true,
370 DayTime => true,
371 MonthDayNano => false, }
373 }
374 (Int32, Interval(to_type)) => match to_type {
375 YearMonth => true,
376 DayTime => false,
377 MonthDayNano => false,
378 },
379 (Duration(_), Interval(MonthDayNano)) => true,
380 (Interval(MonthDayNano), Duration(_)) => true,
381 (Interval(YearMonth), Interval(MonthDayNano)) => true,
382 (Interval(DayTime), Interval(MonthDayNano)) => true,
383 (_, _) => false,
384 }
385}
386
387pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
391 cast_with_options(array, to_type, &CastOptions::default())
392}
393
394fn cast_integer_to_decimal<
395 T: ArrowPrimitiveType,
396 D: DecimalType + ArrowPrimitiveType<Native = M>,
397 M,
398>(
399 array: &PrimitiveArray<T>,
400 precision: u8,
401 scale: i8,
402 base: M,
403 cast_options: &CastOptions,
404) -> Result<ArrayRef, ArrowError>
405where
406 <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
407 M: ArrowNativeTypeOp,
408{
409 let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
410 ArrowError::CastError(format!(
411 "Cannot cast to {:?}({}, {}). The scale causes overflow.",
412 D::PREFIX,
413 precision,
414 scale,
415 ))
416 })?;
417
418 let array = if scale < 0 {
419 match cast_options.safe {
420 true => array.unary_opt::<_, D>(|v| {
421 v.as_()
422 .div_checked(scale_factor)
423 .ok()
424 .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
425 }),
426 false => array.try_unary::<_, D, _>(|v| {
427 v.as_()
428 .div_checked(scale_factor)
429 .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
430 })?,
431 }
432 } else {
433 match cast_options.safe {
434 true => array.unary_opt::<_, D>(|v| {
435 v.as_()
436 .mul_checked(scale_factor)
437 .ok()
438 .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
439 }),
440 false => array.try_unary::<_, D, _>(|v| {
441 v.as_()
442 .mul_checked(scale_factor)
443 .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
444 })?,
445 }
446 };
447
448 Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
449}
450
451fn cast_interval_year_month_to_interval_month_day_nano(
453 array: &dyn Array,
454 _cast_options: &CastOptions,
455) -> Result<ArrayRef, ArrowError> {
456 let array = array.as_primitive::<IntervalYearMonthType>();
457
458 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
459 let months = IntervalYearMonthType::to_months(v);
460 IntervalMonthDayNanoType::make_value(months, 0, 0)
461 })))
462}
463
464fn cast_interval_day_time_to_interval_month_day_nano(
466 array: &dyn Array,
467 _cast_options: &CastOptions,
468) -> Result<ArrayRef, ArrowError> {
469 let array = array.as_primitive::<IntervalDayTimeType>();
470 let mul = 1_000_000;
471
472 Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
473 let (days, ms) = IntervalDayTimeType::to_parts(v);
474 IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
475 })))
476}
477
478fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
480 array: &dyn Array,
481 cast_options: &CastOptions,
482) -> Result<ArrayRef, ArrowError> {
483 let array = array.as_primitive::<IntervalMonthDayNanoType>();
484 let scale = match D::DATA_TYPE {
485 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
486 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
487 DataType::Duration(TimeUnit::Microsecond) => 1_000,
488 DataType::Duration(TimeUnit::Nanosecond) => 1,
489 _ => unreachable!(),
490 };
491
492 if cast_options.safe {
493 let iter = array.iter().map(|v| {
494 v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
495 });
496 Ok(Arc::new(unsafe {
497 PrimitiveArray::<D>::from_trusted_len_iter(iter)
498 }))
499 } else {
500 let vec = array
501 .iter()
502 .map(|v| {
503 v.map(|v| match v.days == 0 && v.months == 0 {
504 true => Ok((v.nanoseconds) / scale),
505 _ => Err(ArrowError::ComputeError(
506 "Cannot convert interval containing non-zero months or days to duration"
507 .to_string(),
508 )),
509 })
510 .transpose()
511 })
512 .collect::<Result<Vec<_>, _>>()?;
513 Ok(Arc::new(unsafe {
514 PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
515 }))
516 }
517}
518
519fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
521 array: &dyn Array,
522 cast_options: &CastOptions,
523) -> Result<ArrayRef, ArrowError> {
524 let array = array
525 .as_any()
526 .downcast_ref::<PrimitiveArray<D>>()
527 .ok_or_else(|| {
528 ArrowError::ComputeError(
529 "Internal Error: Cannot cast duration to DurationArray of expected type"
530 .to_string(),
531 )
532 })?;
533
534 let scale = match array.data_type() {
535 DataType::Duration(TimeUnit::Second) => 1_000_000_000,
536 DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
537 DataType::Duration(TimeUnit::Microsecond) => 1_000,
538 DataType::Duration(TimeUnit::Nanosecond) => 1,
539 _ => unreachable!(),
540 };
541
542 if cast_options.safe {
543 let iter = array.iter().map(|v| {
544 v.and_then(|v| {
545 v.checked_mul(scale)
546 .map(|v| IntervalMonthDayNano::new(0, 0, v))
547 })
548 });
549 Ok(Arc::new(unsafe {
550 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
551 }))
552 } else {
553 let vec = array
554 .iter()
555 .map(|v| {
556 v.map(|v| {
557 if let Ok(v) = v.mul_checked(scale) {
558 Ok(IntervalMonthDayNano::new(0, 0, v))
559 } else {
560 Err(ArrowError::ComputeError(format!(
561 "Cannot cast to {:?}. Overflowing on {:?}",
562 IntervalMonthDayNanoType::DATA_TYPE,
563 v
564 )))
565 }
566 })
567 .transpose()
568 })
569 .collect::<Result<Vec<_>, _>>()?;
570 Ok(Arc::new(unsafe {
571 PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
572 }))
573 }
574}
575
576fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
578 array: &dyn Array,
579) -> Result<ArrayRef, ArrowError> {
580 Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
581}
582
583fn make_timestamp_array(
584 array: &PrimitiveArray<Int64Type>,
585 unit: TimeUnit,
586 tz: Option<Arc<str>>,
587) -> ArrayRef {
588 match unit {
589 TimeUnit::Second => Arc::new(
590 array
591 .reinterpret_cast::<TimestampSecondType>()
592 .with_timezone_opt(tz),
593 ),
594 TimeUnit::Millisecond => Arc::new(
595 array
596 .reinterpret_cast::<TimestampMillisecondType>()
597 .with_timezone_opt(tz),
598 ),
599 TimeUnit::Microsecond => Arc::new(
600 array
601 .reinterpret_cast::<TimestampMicrosecondType>()
602 .with_timezone_opt(tz),
603 ),
604 TimeUnit::Nanosecond => Arc::new(
605 array
606 .reinterpret_cast::<TimestampNanosecondType>()
607 .with_timezone_opt(tz),
608 ),
609 }
610}
611
612fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
613 match unit {
614 TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
615 TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
616 TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
617 TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
618 }
619}
620
621fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
622 v: i64,
623 tz: Option<Tz>,
624) -> Result<NaiveTime, ArrowError> {
625 let time = match tz {
626 Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
627 None => as_datetime::<T>(v).map(|d| d.time()),
628 };
629
630 time.ok_or_else(|| {
631 ArrowError::CastError(format!(
632 "Failed to create naive time with {} {}",
633 std::any::type_name::<T>(),
634 v
635 ))
636 })
637}
638
639fn timestamp_to_date32<T: ArrowTimestampType>(
640 array: &PrimitiveArray<T>,
641) -> Result<ArrayRef, ArrowError> {
642 let err = |x: i64| {
643 ArrowError::CastError(format!(
644 "Cannot convert {} {x} to datetime",
645 std::any::type_name::<T>()
646 ))
647 };
648
649 let array: Date32Array = match array.timezone() {
650 Some(tz) => {
651 let tz: Tz = tz.parse()?;
652 array.try_unary(|x| {
653 as_datetime_with_timezone::<T>(x, tz)
654 .ok_or_else(|| err(x))
655 .map(|d| Date32Type::from_naive_date(d.date_naive()))
656 })?
657 }
658 None => array.try_unary(|x| {
659 as_datetime::<T>(x)
660 .ok_or_else(|| err(x))
661 .map(|d| Date32Type::from_naive_date(d.date()))
662 })?,
663 };
664 Ok(Arc::new(array))
665}
666
667pub fn cast_with_options(
797 array: &dyn Array,
798 to_type: &DataType,
799 cast_options: &CastOptions,
800) -> Result<ArrayRef, ArrowError> {
801 use DataType::*;
802 let from_type = array.data_type();
803 if from_type == to_type {
805 return Ok(make_array(array.to_data()));
806 }
807 match (from_type, to_type) {
808 (
809 Null,
810 Boolean
811 | Int8
812 | UInt8
813 | Int16
814 | UInt16
815 | Float16
816 | Int32
817 | UInt32
818 | Float32
819 | Date32
820 | Time32(_)
821 | Int64
822 | UInt64
823 | Float64
824 | Date64
825 | Timestamp(_, _)
826 | Time64(_)
827 | Duration(_)
828 | Interval(_)
829 | FixedSizeBinary(_)
830 | Binary
831 | Utf8
832 | LargeBinary
833 | LargeUtf8
834 | BinaryView
835 | Utf8View
836 | List(_)
837 | LargeList(_)
838 | FixedSizeList(_, _)
839 | Struct(_)
840 | Map(_, _)
841 | Dictionary(_, _),
842 ) => Ok(new_null_array(to_type, array.len())),
843 (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
844 Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
845 Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
846 Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
847 _ => Err(ArrowError::CastError(format!(
848 "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
849 ))),
850 },
851 (_, RunEndEncoded(index_type, value_type)) => {
852 let array_ref = make_array(array.to_data());
853 match index_type.data_type() {
854 Int16 => cast_to_run_end_encoded::<Int16Type>(
855 &array_ref,
856 value_type.data_type(),
857 cast_options,
858 ),
859 Int32 => cast_to_run_end_encoded::<Int32Type>(
860 &array_ref,
861 value_type.data_type(),
862 cast_options,
863 ),
864 Int64 => cast_to_run_end_encoded::<Int64Type>(
865 &array_ref,
866 value_type.data_type(),
867 cast_options,
868 ),
869 _ => Err(ArrowError::CastError(format!(
870 "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
871 ))),
872 }
873 }
874 (Dictionary(index_type, _), _) => match **index_type {
875 Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
876 Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
877 Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
878 Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
879 UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
880 UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
881 UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
882 UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
883 _ => Err(ArrowError::CastError(format!(
884 "Casting from dictionary type {from_type} to {to_type} not supported",
885 ))),
886 },
887 (_, Dictionary(index_type, value_type)) => match **index_type {
888 Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
889 Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
890 Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
891 Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
892 UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
893 UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
894 UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
895 UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
896 _ => Err(ArrowError::CastError(format!(
897 "Casting from type {from_type} to dictionary type {to_type} not supported",
898 ))),
899 },
900 (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
901 (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
902 (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
903 (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
904 (List(_), FixedSizeList(field, size)) => {
905 let array = array.as_list::<i32>();
906 cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
907 }
908 (LargeList(_), FixedSizeList(field, size)) => {
909 let array = array.as_list::<i64>();
910 cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
911 }
912 (ListView(_), List(list_to)) => cast_list_view_to_list::<i32>(array, list_to, cast_options),
913 (LargeListView(_), LargeList(list_to)) => {
914 cast_list_view_to_list::<i64>(array, list_to, cast_options)
915 }
916 (ListView(_), LargeListView(list_to)) => {
917 cast_list_view::<i32, i64>(array, list_to, cast_options)
918 }
919 (LargeListView(_), ListView(list_to)) => {
920 cast_list_view::<i64, i32>(array, list_to, cast_options)
921 }
922 (List(_), ListView(_)) => cast_list_to_list_view::<i32>(array),
923 (LargeList(_), LargeListView(_)) => cast_list_to_list_view::<i64>(array),
924 (List(_) | LargeList(_), _) => match to_type {
925 Utf8 => value_to_string::<i32>(array, cast_options),
926 LargeUtf8 => value_to_string::<i64>(array, cast_options),
927 _ => Err(ArrowError::CastError(
928 "Cannot cast list to non-list data types".to_string(),
929 )),
930 },
931 (FixedSizeList(list_from, size), List(list_to)) => {
932 if list_to.data_type() != list_from.data_type() {
933 let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
935 let array = cast_with_options(array, &fsl_to, cast_options)?;
936 cast_fixed_size_list_to_list::<i32>(array.as_ref())
937 } else {
938 cast_fixed_size_list_to_list::<i32>(array)
939 }
940 }
941 (FixedSizeList(list_from, size), LargeList(list_to)) => {
942 if list_to.data_type() != list_from.data_type() {
943 let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
945 let array = cast_with_options(array, &fsl_to, cast_options)?;
946 cast_fixed_size_list_to_list::<i64>(array.as_ref())
947 } else {
948 cast_fixed_size_list_to_list::<i64>(array)
949 }
950 }
951 (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
952 if size_from != size_to {
953 return Err(ArrowError::CastError(
954 "cannot cast fixed-size-list to fixed-size-list with different size".into(),
955 ));
956 }
957 let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
958 let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
959 Ok(Arc::new(FixedSizeListArray::try_new(
960 list_to.clone(),
961 *size_from,
962 values,
963 array.nulls().cloned(),
964 )?))
965 }
966 (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
967 (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
968 (_, FixedSizeList(to, size)) if *size == 1 => {
969 cast_values_to_fixed_size_list(array, to, *size, cast_options)
970 }
971 (FixedSizeList(_, size), _) if *size == 1 => {
972 cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
973 }
974 (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
975 cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
976 }
977 (Decimal32(p1, s1), Decimal32(p2, s2)) => {
979 cast_decimal_to_decimal_same_type::<Decimal32Type>(
980 array.as_primitive(),
981 *p1,
982 *s1,
983 *p2,
984 *s2,
985 cast_options,
986 )
987 }
988 (Decimal64(p1, s1), Decimal64(p2, s2)) => {
989 cast_decimal_to_decimal_same_type::<Decimal64Type>(
990 array.as_primitive(),
991 *p1,
992 *s1,
993 *p2,
994 *s2,
995 cast_options,
996 )
997 }
998 (Decimal128(p1, s1), Decimal128(p2, s2)) => {
999 cast_decimal_to_decimal_same_type::<Decimal128Type>(
1000 array.as_primitive(),
1001 *p1,
1002 *s1,
1003 *p2,
1004 *s2,
1005 cast_options,
1006 )
1007 }
1008 (Decimal256(p1, s1), Decimal256(p2, s2)) => {
1009 cast_decimal_to_decimal_same_type::<Decimal256Type>(
1010 array.as_primitive(),
1011 *p1,
1012 *s1,
1013 *p2,
1014 *s2,
1015 cast_options,
1016 )
1017 }
1018 (Decimal32(p1, s1), Decimal64(p2, s2)) => {
1020 cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
1021 array.as_primitive(),
1022 *p1,
1023 *s1,
1024 *p2,
1025 *s2,
1026 cast_options,
1027 )
1028 }
1029 (Decimal32(p1, s1), Decimal128(p2, s2)) => {
1030 cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
1031 array.as_primitive(),
1032 *p1,
1033 *s1,
1034 *p2,
1035 *s2,
1036 cast_options,
1037 )
1038 }
1039 (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1040 cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1041 array.as_primitive(),
1042 *p1,
1043 *s1,
1044 *p2,
1045 *s2,
1046 cast_options,
1047 )
1048 }
1049 (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1050 cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1051 array.as_primitive(),
1052 *p1,
1053 *s1,
1054 *p2,
1055 *s2,
1056 cast_options,
1057 )
1058 }
1059 (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1060 cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1061 array.as_primitive(),
1062 *p1,
1063 *s1,
1064 *p2,
1065 *s2,
1066 cast_options,
1067 )
1068 }
1069 (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1070 cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1071 array.as_primitive(),
1072 *p1,
1073 *s1,
1074 *p2,
1075 *s2,
1076 cast_options,
1077 )
1078 }
1079 (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1080 cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1081 array.as_primitive(),
1082 *p1,
1083 *s1,
1084 *p2,
1085 *s2,
1086 cast_options,
1087 )
1088 }
1089 (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1090 cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1091 array.as_primitive(),
1092 *p1,
1093 *s1,
1094 *p2,
1095 *s2,
1096 cast_options,
1097 )
1098 }
1099 (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1100 cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1101 array.as_primitive(),
1102 *p1,
1103 *s1,
1104 *p2,
1105 *s2,
1106 cast_options,
1107 )
1108 }
1109 (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1110 cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1111 array.as_primitive(),
1112 *p1,
1113 *s1,
1114 *p2,
1115 *s2,
1116 cast_options,
1117 )
1118 }
1119 (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1120 cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1121 array.as_primitive(),
1122 *p1,
1123 *s1,
1124 *p2,
1125 *s2,
1126 cast_options,
1127 )
1128 }
1129 (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1130 cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1131 array.as_primitive(),
1132 *p1,
1133 *s1,
1134 *p2,
1135 *s2,
1136 cast_options,
1137 )
1138 }
1139 (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1141 cast_from_decimal::<Decimal32Type, _>(
1142 array,
1143 10_i32,
1144 scale,
1145 from_type,
1146 to_type,
1147 |x: i32| x as f64,
1148 cast_options,
1149 )
1150 }
1151 (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1152 cast_from_decimal::<Decimal64Type, _>(
1153 array,
1154 10_i64,
1155 scale,
1156 from_type,
1157 to_type,
1158 |x: i64| x as f64,
1159 cast_options,
1160 )
1161 }
1162 (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1163 cast_from_decimal::<Decimal128Type, _>(
1164 array,
1165 10_i128,
1166 scale,
1167 from_type,
1168 to_type,
1169 |x: i128| x as f64,
1170 cast_options,
1171 )
1172 }
1173 (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1174 cast_from_decimal::<Decimal256Type, _>(
1175 array,
1176 i256::from_i128(10_i128),
1177 scale,
1178 from_type,
1179 to_type,
1180 |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1181 cast_options,
1182 )
1183 }
1184 (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1186 cast_to_decimal::<Decimal32Type, _>(
1187 array,
1188 10_i32,
1189 precision,
1190 scale,
1191 from_type,
1192 to_type,
1193 cast_options,
1194 )
1195 }
1196 (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1197 cast_to_decimal::<Decimal64Type, _>(
1198 array,
1199 10_i64,
1200 precision,
1201 scale,
1202 from_type,
1203 to_type,
1204 cast_options,
1205 )
1206 }
1207 (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1208 cast_to_decimal::<Decimal128Type, _>(
1209 array,
1210 10_i128,
1211 precision,
1212 scale,
1213 from_type,
1214 to_type,
1215 cast_options,
1216 )
1217 }
1218 (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1219 cast_to_decimal::<Decimal256Type, _>(
1220 array,
1221 i256::from_i128(10_i128),
1222 precision,
1223 scale,
1224 from_type,
1225 to_type,
1226 cast_options,
1227 )
1228 }
1229 (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1230 array.as_struct(),
1231 from_fields.clone(),
1232 to_fields.clone(),
1233 cast_options,
1234 ),
1235 (Struct(_), _) => Err(ArrowError::CastError(format!(
1236 "Casting from {from_type} to {to_type} not supported"
1237 ))),
1238 (_, Struct(_)) => Err(ArrowError::CastError(format!(
1239 "Casting from {from_type} to {to_type} not supported"
1240 ))),
1241 (_, Boolean) => match from_type {
1242 UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1243 UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1244 UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1245 UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1246 Int8 => cast_numeric_to_bool::<Int8Type>(array),
1247 Int16 => cast_numeric_to_bool::<Int16Type>(array),
1248 Int32 => cast_numeric_to_bool::<Int32Type>(array),
1249 Int64 => cast_numeric_to_bool::<Int64Type>(array),
1250 Float16 => cast_numeric_to_bool::<Float16Type>(array),
1251 Float32 => cast_numeric_to_bool::<Float32Type>(array),
1252 Float64 => cast_numeric_to_bool::<Float64Type>(array),
1253 Utf8View => cast_utf8view_to_boolean(array, cast_options),
1254 Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1255 LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1256 _ => Err(ArrowError::CastError(format!(
1257 "Casting from {from_type} to {to_type} not supported",
1258 ))),
1259 },
1260 (Boolean, _) => match to_type {
1261 UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1262 UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1263 UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1264 UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1265 Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1266 Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1267 Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1268 Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1269 Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1270 Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1271 Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1272 Utf8View => value_to_string_view(array, cast_options),
1273 Utf8 => value_to_string::<i32>(array, cast_options),
1274 LargeUtf8 => value_to_string::<i64>(array, cast_options),
1275 _ => Err(ArrowError::CastError(format!(
1276 "Casting from {from_type} to {to_type} not supported",
1277 ))),
1278 },
1279 (Utf8, _) => match to_type {
1280 UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1281 UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1282 UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1283 UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1284 Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1285 Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1286 Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1287 Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1288 Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1289 Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1290 Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1291 Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1292 Binary => Ok(Arc::new(BinaryArray::from(
1293 array.as_string::<i32>().clone(),
1294 ))),
1295 LargeBinary => {
1296 let binary = BinaryArray::from(array.as_string::<i32>().clone());
1297 cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1298 }
1299 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1300 BinaryView => Ok(Arc::new(
1301 StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1302 )),
1303 LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1304 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1305 Time32(TimeUnit::Millisecond) => {
1306 parse_string::<Time32MillisecondType, i32>(array, cast_options)
1307 }
1308 Time64(TimeUnit::Microsecond) => {
1309 parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1310 }
1311 Time64(TimeUnit::Nanosecond) => {
1312 parse_string::<Time64NanosecondType, i32>(array, cast_options)
1313 }
1314 Timestamp(TimeUnit::Second, to_tz) => {
1315 cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1316 }
1317 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1318 i32,
1319 TimestampMillisecondType,
1320 >(array, to_tz, cast_options),
1321 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1322 i32,
1323 TimestampMicrosecondType,
1324 >(array, to_tz, cast_options),
1325 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1326 cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1327 }
1328 Interval(IntervalUnit::YearMonth) => {
1329 cast_string_to_year_month_interval::<i32>(array, cast_options)
1330 }
1331 Interval(IntervalUnit::DayTime) => {
1332 cast_string_to_day_time_interval::<i32>(array, cast_options)
1333 }
1334 Interval(IntervalUnit::MonthDayNano) => {
1335 cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1336 }
1337 _ => Err(ArrowError::CastError(format!(
1338 "Casting from {from_type} to {to_type} not supported",
1339 ))),
1340 },
1341 (Utf8View, _) => match to_type {
1342 UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1343 UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1344 UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1345 UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1346 Int8 => parse_string_view::<Int8Type>(array, cast_options),
1347 Int16 => parse_string_view::<Int16Type>(array, cast_options),
1348 Int32 => parse_string_view::<Int32Type>(array, cast_options),
1349 Int64 => parse_string_view::<Int64Type>(array, cast_options),
1350 Float32 => parse_string_view::<Float32Type>(array, cast_options),
1351 Float64 => parse_string_view::<Float64Type>(array, cast_options),
1352 Date32 => parse_string_view::<Date32Type>(array, cast_options),
1353 Date64 => parse_string_view::<Date64Type>(array, cast_options),
1354 Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1355 LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1356 BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1357 Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1358 LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1359 Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1360 Time32(TimeUnit::Millisecond) => {
1361 parse_string_view::<Time32MillisecondType>(array, cast_options)
1362 }
1363 Time64(TimeUnit::Microsecond) => {
1364 parse_string_view::<Time64MicrosecondType>(array, cast_options)
1365 }
1366 Time64(TimeUnit::Nanosecond) => {
1367 parse_string_view::<Time64NanosecondType>(array, cast_options)
1368 }
1369 Timestamp(TimeUnit::Second, to_tz) => {
1370 cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1371 }
1372 Timestamp(TimeUnit::Millisecond, to_tz) => {
1373 cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1374 }
1375 Timestamp(TimeUnit::Microsecond, to_tz) => {
1376 cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1377 }
1378 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1379 cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1380 }
1381 Interval(IntervalUnit::YearMonth) => {
1382 cast_view_to_year_month_interval(array, cast_options)
1383 }
1384 Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1385 Interval(IntervalUnit::MonthDayNano) => {
1386 cast_view_to_month_day_nano_interval(array, cast_options)
1387 }
1388 _ => Err(ArrowError::CastError(format!(
1389 "Casting from {from_type} to {to_type} not supported",
1390 ))),
1391 },
1392 (LargeUtf8, _) => match to_type {
1393 UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1394 UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1395 UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1396 UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1397 Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1398 Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1399 Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1400 Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1401 Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1402 Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1403 Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1404 Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1405 Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1406 Binary => {
1407 let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1408 cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1409 }
1410 LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1411 array.as_string::<i64>().clone(),
1412 ))),
1413 Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1414 BinaryView => Ok(Arc::new(BinaryViewArray::from(
1415 array
1416 .as_string::<i64>()
1417 .into_iter()
1418 .map(|x| x.map(|x| x.as_bytes()))
1419 .collect::<Vec<_>>(),
1420 ))),
1421 Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1422 Time32(TimeUnit::Millisecond) => {
1423 parse_string::<Time32MillisecondType, i64>(array, cast_options)
1424 }
1425 Time64(TimeUnit::Microsecond) => {
1426 parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1427 }
1428 Time64(TimeUnit::Nanosecond) => {
1429 parse_string::<Time64NanosecondType, i64>(array, cast_options)
1430 }
1431 Timestamp(TimeUnit::Second, to_tz) => {
1432 cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1433 }
1434 Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1435 i64,
1436 TimestampMillisecondType,
1437 >(array, to_tz, cast_options),
1438 Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1439 i64,
1440 TimestampMicrosecondType,
1441 >(array, to_tz, cast_options),
1442 Timestamp(TimeUnit::Nanosecond, to_tz) => {
1443 cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1444 }
1445 Interval(IntervalUnit::YearMonth) => {
1446 cast_string_to_year_month_interval::<i64>(array, cast_options)
1447 }
1448 Interval(IntervalUnit::DayTime) => {
1449 cast_string_to_day_time_interval::<i64>(array, cast_options)
1450 }
1451 Interval(IntervalUnit::MonthDayNano) => {
1452 cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1453 }
1454 _ => Err(ArrowError::CastError(format!(
1455 "Casting from {from_type} to {to_type} not supported",
1456 ))),
1457 },
1458 (Binary, _) => match to_type {
1459 Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1460 LargeUtf8 => {
1461 let array = cast_binary_to_string::<i32>(array, cast_options)?;
1462 cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1463 }
1464 LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1465 FixedSizeBinary(size) => {
1466 cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1467 }
1468 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1469 Utf8View => Ok(Arc::new(StringViewArray::from(
1470 cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1471 ))),
1472 _ => Err(ArrowError::CastError(format!(
1473 "Casting from {from_type} to {to_type} not supported",
1474 ))),
1475 },
1476 (LargeBinary, _) => match to_type {
1477 Utf8 => {
1478 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1479 cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1480 }
1481 LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1482 Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1483 FixedSizeBinary(size) => {
1484 cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1485 }
1486 BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1487 Utf8View => {
1488 let array = cast_binary_to_string::<i64>(array, cast_options)?;
1489 Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1490 }
1491 _ => Err(ArrowError::CastError(format!(
1492 "Casting from {from_type} to {to_type} not supported",
1493 ))),
1494 },
1495 (FixedSizeBinary(size), _) => match to_type {
1496 Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1497 LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1498 BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1499 _ => Err(ArrowError::CastError(format!(
1500 "Casting from {from_type} to {to_type} not supported",
1501 ))),
1502 },
1503 (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1504 (BinaryView, LargeBinary) => {
1505 cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1506 }
1507 (BinaryView, Utf8) => {
1508 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1509 cast_binary_to_string::<i32>(&binary_arr, cast_options)
1510 }
1511 (BinaryView, LargeUtf8) => {
1512 let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1513 cast_binary_to_string::<i64>(&binary_arr, cast_options)
1514 }
1515 (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1516 (BinaryView, _) => Err(ArrowError::CastError(format!(
1517 "Casting from {from_type} to {to_type} not supported",
1518 ))),
1519 (from_type, Utf8View) if from_type.is_primitive() => {
1520 value_to_string_view(array, cast_options)
1521 }
1522 (from_type, LargeUtf8) if from_type.is_primitive() => {
1523 value_to_string::<i64>(array, cast_options)
1524 }
1525 (from_type, Utf8) if from_type.is_primitive() => {
1526 value_to_string::<i32>(array, cast_options)
1527 }
1528 (from_type, Binary) if from_type.is_integer() => match from_type {
1529 UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1530 UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1531 UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1532 UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1533 Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1534 Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1535 Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1536 Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1537 _ => unreachable!(),
1538 },
1539 (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1540 UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1541 UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1542 UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1543 UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1544 Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1545 Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1546 Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1547 Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1548 _ => unreachable!(),
1549 },
1550 (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1552 (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1553 (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1554 (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1555 (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1556 (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1557 (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1558 (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1559 (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1560 (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1561
1562 (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1563 (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1564 (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1565 (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1566 (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1567 (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1568 (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1569 (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1570 (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1571 (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1572
1573 (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1574 (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1575 (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1576 (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1577 (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1578 (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1579 (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1580 (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1581 (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1582 (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1583
1584 (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1585 (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1586 (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1587 (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1588 (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1589 (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1590 (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1591 (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1592 (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1593 (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1594
1595 (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1596 (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1597 (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1598 (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1599 (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1600 (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1601 (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1602 (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1603 (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1604 (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1605
1606 (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1607 (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1608 (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1609 (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1610 (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1611 (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1612 (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1613 (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1614 (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1615 (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1616
1617 (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1618 (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1619 (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1620 (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1621 (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1622 (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1623 (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1624 (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1625 (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1626 (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1627
1628 (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1629 (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1630 (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1631 (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1632 (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1633 (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1634 (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1635 (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1636 (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1637 (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1638
1639 (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1640 (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1641 (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1642 (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1643 (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1644 (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1645 (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1646 (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1647 (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1648 (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1649
1650 (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1651 (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1652 (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1653 (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1654 (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1655 (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1656 (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1657 (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1658 (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1659 (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1660
1661 (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1662 (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1663 (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1664 (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1665 (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1666 (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1667 (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1668 (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1669 (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1670 (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1671 (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1675 (Int32, Date64) => cast_with_options(
1676 &cast_with_options(array, &Date32, cast_options)?,
1677 &Date64,
1678 cast_options,
1679 ),
1680 (Int32, Time32(TimeUnit::Second)) => {
1681 cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1682 }
1683 (Int32, Time32(TimeUnit::Millisecond)) => {
1684 cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1685 }
1686 (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1688 (Date32, Int64) => cast_with_options(
1689 &cast_with_options(array, &Int32, cast_options)?,
1690 &Int64,
1691 cast_options,
1692 ),
1693 (Time32(TimeUnit::Second), Int32) => {
1694 cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1695 }
1696 (Time32(TimeUnit::Millisecond), Int32) => {
1697 cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1698 }
1699 (Time32(TimeUnit::Second), Int64) => cast_with_options(
1700 &cast_with_options(array, &Int32, cast_options)?,
1701 &Int64,
1702 cast_options,
1703 ),
1704 (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1705 &cast_with_options(array, &Int32, cast_options)?,
1706 &Int64,
1707 cast_options,
1708 ),
1709 (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1710 (Int64, Date32) => cast_with_options(
1711 &cast_with_options(array, &Int32, cast_options)?,
1712 &Date32,
1713 cast_options,
1714 ),
1715 (Int64, Time64(TimeUnit::Microsecond)) => {
1717 cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1718 }
1719 (Int64, Time64(TimeUnit::Nanosecond)) => {
1720 cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1721 }
1722
1723 (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1724 (Date64, Int32) => cast_with_options(
1725 &cast_with_options(array, &Int64, cast_options)?,
1726 &Int32,
1727 cast_options,
1728 ),
1729 (Time64(TimeUnit::Microsecond), Int64) => {
1730 cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1731 }
1732 (Time64(TimeUnit::Nanosecond), Int64) => {
1733 cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1734 }
1735 (Date32, Date64) => Ok(Arc::new(
1736 array
1737 .as_primitive::<Date32Type>()
1738 .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1739 )),
1740 (Date64, Date32) => Ok(Arc::new(
1741 array
1742 .as_primitive::<Date64Type>()
1743 .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1744 )),
1745
1746 (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1747 array
1748 .as_primitive::<Time32SecondType>()
1749 .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1750 )),
1751 (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1752 array
1753 .as_primitive::<Time32SecondType>()
1754 .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1755 )),
1756 (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1757 array
1758 .as_primitive::<Time32SecondType>()
1759 .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1760 )),
1761
1762 (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1763 array
1764 .as_primitive::<Time32MillisecondType>()
1765 .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1766 )),
1767 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1768 array
1769 .as_primitive::<Time32MillisecondType>()
1770 .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1771 )),
1772 (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1773 array
1774 .as_primitive::<Time32MillisecondType>()
1775 .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1776 )),
1777
1778 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1779 array
1780 .as_primitive::<Time64MicrosecondType>()
1781 .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1782 )),
1783 (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1784 array
1785 .as_primitive::<Time64MicrosecondType>()
1786 .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1787 )),
1788 (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1789 array
1790 .as_primitive::<Time64MicrosecondType>()
1791 .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1792 )),
1793
1794 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1795 array
1796 .as_primitive::<Time64NanosecondType>()
1797 .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1798 )),
1799 (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1800 array
1801 .as_primitive::<Time64NanosecondType>()
1802 .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1803 )),
1804 (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1805 array
1806 .as_primitive::<Time64NanosecondType>()
1807 .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1808 )),
1809
1810 (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1812 let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1813 cast_with_options(&array, to_type, cast_options)
1814 }
1815 (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1816 let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1817 cast_with_options(&array, to_type, cast_options)
1818 }
1819 (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1820 let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1821 cast_with_options(&array, to_type, cast_options)
1822 }
1823 (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1824 let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1825 cast_with_options(&array, to_type, cast_options)
1826 }
1827
1828 (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1829 let array = cast_with_options(array, &Int64, cast_options)?;
1830 Ok(make_timestamp_array(
1831 array.as_primitive(),
1832 *unit,
1833 tz.clone(),
1834 ))
1835 }
1836
1837 (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1838 let array = cast_with_options(array, &Int64, cast_options)?;
1839 let time_array = array.as_primitive::<Int64Type>();
1840 let from_size = time_unit_multiple(from_unit);
1841 let to_size = time_unit_multiple(to_unit);
1842 let converted = match from_size.cmp(&to_size) {
1845 Ordering::Greater => {
1846 let divisor = from_size / to_size;
1847 time_array.unary::<_, Int64Type>(|o| o / divisor)
1848 }
1849 Ordering::Equal => time_array.clone(),
1850 Ordering::Less => {
1851 let mul = to_size / from_size;
1852 if cast_options.safe {
1853 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1854 } else {
1855 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1856 }
1857 }
1858 };
1859 let adjusted = match (from_tz, to_tz) {
1861 (None, Some(to_tz)) => {
1867 let to_tz: Tz = to_tz.parse()?;
1868 match to_unit {
1869 TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1870 converted,
1871 &to_tz,
1872 cast_options,
1873 )?,
1874 TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1875 TimestampMillisecondType,
1876 >(
1877 converted, &to_tz, cast_options
1878 )?,
1879 TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1880 TimestampMicrosecondType,
1881 >(
1882 converted, &to_tz, cast_options
1883 )?,
1884 TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1885 TimestampNanosecondType,
1886 >(
1887 converted, &to_tz, cast_options
1888 )?,
1889 }
1890 }
1891 _ => converted,
1892 };
1893 Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1894 }
1895 (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1896 timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1897 }
1898 (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1899 timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1900 }
1901 (Timestamp(TimeUnit::Second, _), Date32) => {
1902 timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1903 }
1904 (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1905 timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1906 }
1907 (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1908 true => {
1909 array
1911 .as_primitive::<TimestampSecondType>()
1912 .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1913 }
1914 false => array
1915 .as_primitive::<TimestampSecondType>()
1916 .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1917 })),
1918 (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1919 cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1920 }
1921 (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1922 array
1923 .as_primitive::<TimestampMicrosecondType>()
1924 .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1925 )),
1926 (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1927 array
1928 .as_primitive::<TimestampNanosecondType>()
1929 .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1930 )),
1931 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1932 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1933 Ok(Arc::new(
1934 array
1935 .as_primitive::<TimestampSecondType>()
1936 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1937 Ok(time_to_time64us(as_time_res_with_timezone::<
1938 TimestampSecondType,
1939 >(x, tz)?))
1940 })?,
1941 ))
1942 }
1943 (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1944 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1945 Ok(Arc::new(
1946 array
1947 .as_primitive::<TimestampSecondType>()
1948 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1949 Ok(time_to_time64ns(as_time_res_with_timezone::<
1950 TimestampSecondType,
1951 >(x, tz)?))
1952 })?,
1953 ))
1954 }
1955 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1956 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1957 Ok(Arc::new(
1958 array
1959 .as_primitive::<TimestampMillisecondType>()
1960 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1961 Ok(time_to_time64us(as_time_res_with_timezone::<
1962 TimestampMillisecondType,
1963 >(x, tz)?))
1964 })?,
1965 ))
1966 }
1967 (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1968 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1969 Ok(Arc::new(
1970 array
1971 .as_primitive::<TimestampMillisecondType>()
1972 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1973 Ok(time_to_time64ns(as_time_res_with_timezone::<
1974 TimestampMillisecondType,
1975 >(x, tz)?))
1976 })?,
1977 ))
1978 }
1979 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1980 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1981 Ok(Arc::new(
1982 array
1983 .as_primitive::<TimestampMicrosecondType>()
1984 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1985 Ok(time_to_time64us(as_time_res_with_timezone::<
1986 TimestampMicrosecondType,
1987 >(x, tz)?))
1988 })?,
1989 ))
1990 }
1991 (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1992 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1993 Ok(Arc::new(
1994 array
1995 .as_primitive::<TimestampMicrosecondType>()
1996 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1997 Ok(time_to_time64ns(as_time_res_with_timezone::<
1998 TimestampMicrosecondType,
1999 >(x, tz)?))
2000 })?,
2001 ))
2002 }
2003 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
2004 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2005 Ok(Arc::new(
2006 array
2007 .as_primitive::<TimestampNanosecondType>()
2008 .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2009 Ok(time_to_time64us(as_time_res_with_timezone::<
2010 TimestampNanosecondType,
2011 >(x, tz)?))
2012 })?,
2013 ))
2014 }
2015 (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
2016 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2017 Ok(Arc::new(
2018 array
2019 .as_primitive::<TimestampNanosecondType>()
2020 .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2021 Ok(time_to_time64ns(as_time_res_with_timezone::<
2022 TimestampNanosecondType,
2023 >(x, tz)?))
2024 })?,
2025 ))
2026 }
2027 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2028 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2029 Ok(Arc::new(
2030 array
2031 .as_primitive::<TimestampSecondType>()
2032 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2033 Ok(time_to_time32s(as_time_res_with_timezone::<
2034 TimestampSecondType,
2035 >(x, tz)?))
2036 })?,
2037 ))
2038 }
2039 (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2040 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2041 Ok(Arc::new(
2042 array
2043 .as_primitive::<TimestampSecondType>()
2044 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2045 Ok(time_to_time32ms(as_time_res_with_timezone::<
2046 TimestampSecondType,
2047 >(x, tz)?))
2048 })?,
2049 ))
2050 }
2051 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2052 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2053 Ok(Arc::new(
2054 array
2055 .as_primitive::<TimestampMillisecondType>()
2056 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2057 Ok(time_to_time32s(as_time_res_with_timezone::<
2058 TimestampMillisecondType,
2059 >(x, tz)?))
2060 })?,
2061 ))
2062 }
2063 (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2064 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2065 Ok(Arc::new(
2066 array
2067 .as_primitive::<TimestampMillisecondType>()
2068 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2069 Ok(time_to_time32ms(as_time_res_with_timezone::<
2070 TimestampMillisecondType,
2071 >(x, tz)?))
2072 })?,
2073 ))
2074 }
2075 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2076 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2077 Ok(Arc::new(
2078 array
2079 .as_primitive::<TimestampMicrosecondType>()
2080 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2081 Ok(time_to_time32s(as_time_res_with_timezone::<
2082 TimestampMicrosecondType,
2083 >(x, tz)?))
2084 })?,
2085 ))
2086 }
2087 (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2088 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2089 Ok(Arc::new(
2090 array
2091 .as_primitive::<TimestampMicrosecondType>()
2092 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2093 Ok(time_to_time32ms(as_time_res_with_timezone::<
2094 TimestampMicrosecondType,
2095 >(x, tz)?))
2096 })?,
2097 ))
2098 }
2099 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2100 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2101 Ok(Arc::new(
2102 array
2103 .as_primitive::<TimestampNanosecondType>()
2104 .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2105 Ok(time_to_time32s(as_time_res_with_timezone::<
2106 TimestampNanosecondType,
2107 >(x, tz)?))
2108 })?,
2109 ))
2110 }
2111 (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2112 let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2113 Ok(Arc::new(
2114 array
2115 .as_primitive::<TimestampNanosecondType>()
2116 .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2117 Ok(time_to_time32ms(as_time_res_with_timezone::<
2118 TimestampNanosecondType,
2119 >(x, tz)?))
2120 })?,
2121 ))
2122 }
2123 (Date64, Timestamp(TimeUnit::Second, _)) => {
2124 let array = array
2125 .as_primitive::<Date64Type>()
2126 .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2127
2128 cast_with_options(&array, to_type, cast_options)
2129 }
2130 (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2131 let array = array
2132 .as_primitive::<Date64Type>()
2133 .reinterpret_cast::<TimestampMillisecondType>();
2134
2135 cast_with_options(&array, to_type, cast_options)
2136 }
2137
2138 (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2139 let array = array
2140 .as_primitive::<Date64Type>()
2141 .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2142
2143 cast_with_options(&array, to_type, cast_options)
2144 }
2145 (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2146 let array = array
2147 .as_primitive::<Date64Type>()
2148 .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2149
2150 cast_with_options(&array, to_type, cast_options)
2151 }
2152 (Date32, Timestamp(TimeUnit::Second, _)) => {
2153 let array = array
2154 .as_primitive::<Date32Type>()
2155 .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2156
2157 cast_with_options(&array, to_type, cast_options)
2158 }
2159 (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2160 let array = array
2161 .as_primitive::<Date32Type>()
2162 .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2163
2164 cast_with_options(&array, to_type, cast_options)
2165 }
2166 (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2167 let array = array
2168 .as_primitive::<Date32Type>()
2169 .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2170
2171 cast_with_options(&array, to_type, cast_options)
2172 }
2173 (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2174 let array = array
2175 .as_primitive::<Date32Type>()
2176 .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2177
2178 cast_with_options(&array, to_type, cast_options)
2179 }
2180
2181 (_, Duration(unit)) if from_type.is_numeric() => {
2182 let array = cast_with_options(array, &Int64, cast_options)?;
2183 Ok(make_duration_array(array.as_primitive(), *unit))
2184 }
2185 (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2186 let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2187 cast_with_options(&array, to_type, cast_options)
2188 }
2189 (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2190 let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2191 cast_with_options(&array, to_type, cast_options)
2192 }
2193 (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2194 let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2195 cast_with_options(&array, to_type, cast_options)
2196 }
2197 (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2198 let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2199 cast_with_options(&array, to_type, cast_options)
2200 }
2201
2202 (Duration(from_unit), Duration(to_unit)) => {
2203 let array = cast_with_options(array, &Int64, cast_options)?;
2204 let time_array = array.as_primitive::<Int64Type>();
2205 let from_size = time_unit_multiple(from_unit);
2206 let to_size = time_unit_multiple(to_unit);
2207 let converted = match from_size.cmp(&to_size) {
2210 Ordering::Greater => {
2211 let divisor = from_size / to_size;
2212 time_array.unary::<_, Int64Type>(|o| o / divisor)
2213 }
2214 Ordering::Equal => time_array.clone(),
2215 Ordering::Less => {
2216 let mul = to_size / from_size;
2217 if cast_options.safe {
2218 time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2219 } else {
2220 time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2221 }
2222 }
2223 };
2224 Ok(make_duration_array(&converted, *to_unit))
2225 }
2226
2227 (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2228 cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2229 }
2230 (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2231 cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2232 }
2233 (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2234 cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2235 }
2236 (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2237 cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2238 }
2239 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2240 cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2241 }
2242 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2243 cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2244 }
2245 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2246 cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2247 }
2248 (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2249 cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2250 }
2251 (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2252 cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2253 }
2254 (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2255 cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2256 }
2257 (Int32, Interval(IntervalUnit::YearMonth)) => {
2258 cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2259 }
2260 (_, _) => Err(ArrowError::CastError(format!(
2261 "Casting from {from_type} to {to_type} not supported",
2262 ))),
2263 }
2264}
2265
2266fn cast_struct_to_struct(
2267 array: &StructArray,
2268 from_fields: Fields,
2269 to_fields: Fields,
2270 cast_options: &CastOptions,
2271) -> Result<ArrayRef, ArrowError> {
2272 let fields_match_order = from_fields.len() == to_fields.len()
2274 && from_fields
2275 .iter()
2276 .zip(to_fields.iter())
2277 .all(|(f1, f2)| f1.name() == f2.name());
2278
2279 let fields = if fields_match_order {
2280 cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2282 } else {
2283 let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2284 from_fields
2285 .iter()
2286 .any(|from_field| from_field.name() == to_field.name())
2287 });
2288
2289 if all_fields_match_by_name {
2290 cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2292 } else {
2293 cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2295 }
2296 };
2297
2298 let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2299 Ok(Arc::new(array) as ArrayRef)
2300}
2301
2302fn cast_struct_fields_by_name(
2303 array: &StructArray,
2304 from_fields: Fields,
2305 to_fields: Fields,
2306 cast_options: &CastOptions,
2307) -> Result<Vec<ArrayRef>, ArrowError> {
2308 to_fields
2309 .iter()
2310 .map(|to_field| {
2311 let from_field_idx = from_fields
2312 .iter()
2313 .position(|from_field| from_field.name() == to_field.name())
2314 .unwrap(); let column = array.column(from_field_idx);
2316 cast_with_options(column, to_field.data_type(), cast_options)
2317 })
2318 .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2319}
2320
2321fn cast_struct_fields_in_order(
2322 array: &StructArray,
2323 to_fields: Fields,
2324 cast_options: &CastOptions,
2325) -> Result<Vec<ArrayRef>, ArrowError> {
2326 array
2327 .columns()
2328 .iter()
2329 .zip(to_fields.iter())
2330 .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2331 .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2332}
2333
2334fn cast_from_decimal<D, F>(
2335 array: &dyn Array,
2336 base: D::Native,
2337 scale: &i8,
2338 from_type: &DataType,
2339 to_type: &DataType,
2340 as_float: F,
2341 cast_options: &CastOptions,
2342) -> Result<ArrayRef, ArrowError>
2343where
2344 D: DecimalType + ArrowPrimitiveType,
2345 <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2346 F: Fn(D::Native) -> f64,
2347{
2348 use DataType::*;
2349 match to_type {
2351 UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2352 UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2353 UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2354 UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2355 Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2356 Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2357 Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2358 Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2359 Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2360 (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2361 }),
2362 Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2363 as_float(x) / 10_f64.powi(*scale as i32)
2364 }),
2365 Utf8View => value_to_string_view(array, cast_options),
2366 Utf8 => value_to_string::<i32>(array, cast_options),
2367 LargeUtf8 => value_to_string::<i64>(array, cast_options),
2368 Null => Ok(new_null_array(to_type, array.len())),
2369 _ => Err(ArrowError::CastError(format!(
2370 "Casting from {from_type} to {to_type} not supported"
2371 ))),
2372 }
2373}
2374
2375fn cast_to_decimal<D, M>(
2376 array: &dyn Array,
2377 base: M,
2378 precision: &u8,
2379 scale: &i8,
2380 from_type: &DataType,
2381 to_type: &DataType,
2382 cast_options: &CastOptions,
2383) -> Result<ArrayRef, ArrowError>
2384where
2385 D: DecimalType + ArrowPrimitiveType<Native = M>,
2386 M: ArrowNativeTypeOp + DecimalCast,
2387 u8: num_traits::AsPrimitive<M>,
2388 u16: num_traits::AsPrimitive<M>,
2389 u32: num_traits::AsPrimitive<M>,
2390 u64: num_traits::AsPrimitive<M>,
2391 i8: num_traits::AsPrimitive<M>,
2392 i16: num_traits::AsPrimitive<M>,
2393 i32: num_traits::AsPrimitive<M>,
2394 i64: num_traits::AsPrimitive<M>,
2395{
2396 use DataType::*;
2397 match from_type {
2399 UInt8 => cast_integer_to_decimal::<_, D, M>(
2400 array.as_primitive::<UInt8Type>(),
2401 *precision,
2402 *scale,
2403 base,
2404 cast_options,
2405 ),
2406 UInt16 => cast_integer_to_decimal::<_, D, _>(
2407 array.as_primitive::<UInt16Type>(),
2408 *precision,
2409 *scale,
2410 base,
2411 cast_options,
2412 ),
2413 UInt32 => cast_integer_to_decimal::<_, D, _>(
2414 array.as_primitive::<UInt32Type>(),
2415 *precision,
2416 *scale,
2417 base,
2418 cast_options,
2419 ),
2420 UInt64 => cast_integer_to_decimal::<_, D, _>(
2421 array.as_primitive::<UInt64Type>(),
2422 *precision,
2423 *scale,
2424 base,
2425 cast_options,
2426 ),
2427 Int8 => cast_integer_to_decimal::<_, D, _>(
2428 array.as_primitive::<Int8Type>(),
2429 *precision,
2430 *scale,
2431 base,
2432 cast_options,
2433 ),
2434 Int16 => cast_integer_to_decimal::<_, D, _>(
2435 array.as_primitive::<Int16Type>(),
2436 *precision,
2437 *scale,
2438 base,
2439 cast_options,
2440 ),
2441 Int32 => cast_integer_to_decimal::<_, D, _>(
2442 array.as_primitive::<Int32Type>(),
2443 *precision,
2444 *scale,
2445 base,
2446 cast_options,
2447 ),
2448 Int64 => cast_integer_to_decimal::<_, D, _>(
2449 array.as_primitive::<Int64Type>(),
2450 *precision,
2451 *scale,
2452 base,
2453 cast_options,
2454 ),
2455 Float32 => cast_floating_point_to_decimal::<_, D>(
2456 array.as_primitive::<Float32Type>(),
2457 *precision,
2458 *scale,
2459 cast_options,
2460 ),
2461 Float64 => cast_floating_point_to_decimal::<_, D>(
2462 array.as_primitive::<Float64Type>(),
2463 *precision,
2464 *scale,
2465 cast_options,
2466 ),
2467 Utf8View | Utf8 => {
2468 cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2469 }
2470 LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2471 Null => Ok(new_null_array(to_type, array.len())),
2472 _ => Err(ArrowError::CastError(format!(
2473 "Casting from {from_type} to {to_type} not supported"
2474 ))),
2475 }
2476}
2477
2478const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2480 match unit {
2481 TimeUnit::Second => 1,
2482 TimeUnit::Millisecond => MILLISECONDS,
2483 TimeUnit::Microsecond => MICROSECONDS,
2484 TimeUnit::Nanosecond => NANOSECONDS,
2485 }
2486}
2487
2488fn cast_numeric_arrays<FROM, TO>(
2490 from: &dyn Array,
2491 cast_options: &CastOptions,
2492) -> Result<ArrayRef, ArrowError>
2493where
2494 FROM: ArrowPrimitiveType,
2495 TO: ArrowPrimitiveType,
2496 FROM::Native: NumCast,
2497 TO::Native: NumCast,
2498{
2499 if cast_options.safe {
2500 Ok(Arc::new(numeric_cast::<FROM, TO>(
2502 from.as_primitive::<FROM>(),
2503 )))
2504 } else {
2505 Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2507 from.as_primitive::<FROM>(),
2508 )?))
2509 }
2510}
2511
2512fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2515where
2516 T: ArrowPrimitiveType,
2517 R: ArrowPrimitiveType,
2518 T::Native: NumCast,
2519 R::Native: NumCast,
2520{
2521 from.try_unary(|value| {
2522 num_traits::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2523 ArrowError::CastError(format!(
2524 "Can't cast value {:?} to type {}",
2525 value,
2526 R::DATA_TYPE
2527 ))
2528 })
2529 })
2530}
2531
2532fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2535where
2536 T: ArrowPrimitiveType,
2537 R: ArrowPrimitiveType,
2538 T::Native: NumCast,
2539 R::Native: NumCast,
2540{
2541 from.unary_opt::<_, R>(num_traits::cast::cast::<T::Native, R::Native>)
2542}
2543
2544fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2545 array: &dyn Array,
2546) -> Result<ArrayRef, ArrowError> {
2547 let array = array.as_primitive::<FROM>();
2548 let size = std::mem::size_of::<FROM::Native>();
2549 let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2550 Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2551 offsets,
2552 array.values().inner().clone(),
2553 array.nulls().cloned(),
2554 )?))
2555}
2556
2557fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2558 array: PrimitiveArray<Int64Type>,
2559 to_tz: &Tz,
2560 cast_options: &CastOptions,
2561) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2562 let adjust = |o| {
2563 let local = as_datetime::<T>(o)?;
2564 let offset = to_tz.offset_from_local_datetime(&local).single()?;
2565 T::make_value(local - offset.fix())
2566 };
2567 let adjusted = if cast_options.safe {
2568 array.unary_opt::<_, Int64Type>(adjust)
2569 } else {
2570 array.try_unary::<_, Int64Type, _>(|o| {
2571 adjust(o).ok_or_else(|| {
2572 ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2573 })
2574 })?
2575 };
2576 Ok(adjusted)
2577}
2578
2579fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2583where
2584 FROM: ArrowPrimitiveType,
2585{
2586 numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2587}
2588
2589fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2590where
2591 T: ArrowPrimitiveType + ArrowPrimitiveType,
2592{
2593 let mut b = BooleanBuilder::with_capacity(from.len());
2594
2595 for i in 0..from.len() {
2596 if from.is_null(i) {
2597 b.append_null();
2598 } else if from.value(i) != T::default_value() {
2599 b.append_value(true);
2600 } else {
2601 b.append_value(false);
2602 }
2603 }
2604
2605 Ok(b.finish())
2606}
2607
2608fn cast_bool_to_numeric<TO>(
2612 from: &dyn Array,
2613 cast_options: &CastOptions,
2614) -> Result<ArrayRef, ArrowError>
2615where
2616 TO: ArrowPrimitiveType,
2617 TO::Native: num_traits::cast::NumCast,
2618{
2619 Ok(Arc::new(bool_to_numeric_cast::<TO>(
2620 from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2621 cast_options,
2622 )))
2623}
2624
2625fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2626where
2627 T: ArrowPrimitiveType,
2628 T::Native: num_traits::NumCast,
2629{
2630 let iter = (0..from.len()).map(|i| {
2631 if from.is_null(i) {
2632 None
2633 } else if from.value(i) {
2634 num_traits::cast::cast(1)
2636 } else {
2637 Some(T::default_value())
2638 }
2639 });
2640 unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2645}
2646
2647fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2649 array: &dyn Array,
2650 byte_width: i32,
2651 cast_options: &CastOptions,
2652) -> Result<ArrayRef, ArrowError> {
2653 let array = array.as_binary::<O>();
2654 let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2655
2656 for i in 0..array.len() {
2657 if array.is_null(i) {
2658 builder.append_null();
2659 } else {
2660 match builder.append_value(array.value(i)) {
2661 Ok(_) => {}
2662 Err(e) => match cast_options.safe {
2663 true => builder.append_null(),
2664 false => return Err(e),
2665 },
2666 }
2667 }
2668 }
2669
2670 Ok(Arc::new(builder.finish()))
2671}
2672
2673fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2676 array: &dyn Array,
2677 byte_width: i32,
2678) -> Result<ArrayRef, ArrowError> {
2679 let array = array
2680 .as_any()
2681 .downcast_ref::<FixedSizeBinaryArray>()
2682 .unwrap();
2683
2684 let offsets: i128 = byte_width as i128 * array.len() as i128;
2685
2686 let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2687 if is_binary && offsets > i32::MAX as i128 {
2688 return Err(ArrowError::ComputeError(
2689 "FixedSizeBinary array too large to cast to Binary array".to_string(),
2690 ));
2691 } else if !is_binary && offsets > i64::MAX as i128 {
2692 return Err(ArrowError::ComputeError(
2693 "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2694 ));
2695 }
2696
2697 let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2698
2699 for i in 0..array.len() {
2700 if array.is_null(i) {
2701 builder.append_null();
2702 } else {
2703 builder.append_value(array.value(i));
2704 }
2705 }
2706
2707 Ok(Arc::new(builder.finish()))
2708}
2709
2710fn cast_fixed_size_binary_to_binary_view(
2711 array: &dyn Array,
2712 _byte_width: i32,
2713) -> Result<ArrayRef, ArrowError> {
2714 let array = array
2715 .as_any()
2716 .downcast_ref::<FixedSizeBinaryArray>()
2717 .unwrap();
2718
2719 let mut builder = BinaryViewBuilder::with_capacity(array.len());
2720 for i in 0..array.len() {
2721 if array.is_null(i) {
2722 builder.append_null();
2723 } else {
2724 builder.append_value(array.value(i));
2725 }
2726 }
2727
2728 Ok(Arc::new(builder.finish()))
2729}
2730
2731fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2734where
2735 FROM: ByteArrayType,
2736 TO: ByteArrayType<Native = FROM::Native>,
2737 FROM::Offset: OffsetSizeTrait + ToPrimitive,
2738 TO::Offset: OffsetSizeTrait + NumCast,
2739{
2740 let data = array.to_data();
2741 assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2742 let str_values_buf = data.buffers()[1].clone();
2743 let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2744
2745 let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2746 offsets
2747 .iter()
2748 .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2749 let offset =
2750 <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2751 ArrowError::ComputeError(format!(
2752 "{}{} array too large to cast to {}{} array",
2753 FROM::Offset::PREFIX,
2754 FROM::PREFIX,
2755 TO::Offset::PREFIX,
2756 TO::PREFIX
2757 ))
2758 })?;
2759 offset_builder.append(offset);
2760 Ok(())
2761 })?;
2762
2763 let offset_buffer = offset_builder.finish();
2764
2765 let dtype = TO::DATA_TYPE;
2766
2767 let builder = ArrayData::builder(dtype)
2768 .offset(array.offset())
2769 .len(array.len())
2770 .add_buffer(offset_buffer)
2771 .add_buffer(str_values_buf)
2772 .nulls(data.nulls().cloned());
2773
2774 let array_data = unsafe { builder.build_unchecked() };
2775
2776 Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2777}
2778
2779fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2781where
2782 FROM: ByteViewType,
2783 TO: ByteArrayType,
2784 FROM::Native: AsRef<TO::Native>,
2785{
2786 let data = array.to_data();
2787 let view_array = GenericByteViewArray::<FROM>::from(data);
2788
2789 let len = view_array.len();
2790 let bytes = view_array
2791 .views()
2792 .iter()
2793 .map(|v| ByteView::from(*v).length as usize)
2794 .sum::<usize>();
2795
2796 let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2797
2798 for val in view_array.iter() {
2799 byte_array_builder.append_option(val);
2800 }
2801
2802 Ok(Arc::new(byte_array_builder.finish()))
2803}
2804
2805#[cfg(test)]
2806mod tests {
2807 use super::*;
2808 use DataType::*;
2809 use arrow_array::{Int64Array, RunArray, StringArray};
2810 use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2811 use arrow_buffer::{ScalarBuffer, i256};
2812 use arrow_schema::{DataType, Field};
2813 use chrono::NaiveDate;
2814 use half::f16;
2815 use std::sync::Arc;
2816
2817 #[derive(Clone)]
2818 struct DecimalCastTestConfig {
2819 input_prec: u8,
2820 input_scale: i8,
2821 input_repr: i128,
2822 output_prec: u8,
2823 output_scale: i8,
2824 expected_output_repr: Result<i128, String>, }
2829
2830 macro_rules! generate_cast_test_case {
2831 ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2832 let output =
2833 $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2834
2835 let input_array_type = $INPUT_ARRAY.data_type();
2837 assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2838 let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2839 assert_eq!($OUTPUT_TYPE, result.data_type());
2840 assert_eq!(result.as_ref(), &output);
2841
2842 let cast_option = CastOptions {
2843 safe: false,
2844 format_options: FormatOptions::default(),
2845 };
2846 let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2847 assert_eq!($OUTPUT_TYPE, result.data_type());
2848 assert_eq!(result.as_ref(), &output);
2849 };
2850 }
2851
2852 fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2853 where
2854 I: DecimalType,
2855 O: DecimalType,
2856 I::Native: DecimalCast,
2857 O::Native: DecimalCast,
2858 {
2859 let array = vec![I::Native::from_decimal(t.input_repr)];
2860 let array = array
2861 .into_iter()
2862 .collect::<PrimitiveArray<I>>()
2863 .with_precision_and_scale(t.input_prec, t.input_scale)
2864 .unwrap();
2865 let input_type = array.data_type();
2866 let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2867 assert!(can_cast_types(input_type, &output_type));
2868
2869 let options = CastOptions {
2870 safe: false,
2871 ..Default::default()
2872 };
2873 let result = cast_with_options(&array, &output_type, &options);
2874
2875 match t.expected_output_repr {
2876 Ok(v) => {
2877 let expected_array = vec![O::Native::from_decimal(v)];
2878 let expected_array = expected_array
2879 .into_iter()
2880 .collect::<PrimitiveArray<O>>()
2881 .with_precision_and_scale(t.output_prec, t.output_scale)
2882 .unwrap();
2883 assert_eq!(*result.unwrap(), expected_array);
2884 }
2885 Err(expected_output_message_template) => {
2886 assert!(result.is_err());
2887 let expected_error_message =
2888 expected_output_message_template.replace("{}", O::PREFIX);
2889 assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2890 }
2891 }
2892 }
2893
2894 fn create_decimal32_array(
2895 array: Vec<Option<i32>>,
2896 precision: u8,
2897 scale: i8,
2898 ) -> Result<Decimal32Array, ArrowError> {
2899 array
2900 .into_iter()
2901 .collect::<Decimal32Array>()
2902 .with_precision_and_scale(precision, scale)
2903 }
2904
2905 fn create_decimal64_array(
2906 array: Vec<Option<i64>>,
2907 precision: u8,
2908 scale: i8,
2909 ) -> Result<Decimal64Array, ArrowError> {
2910 array
2911 .into_iter()
2912 .collect::<Decimal64Array>()
2913 .with_precision_and_scale(precision, scale)
2914 }
2915
2916 fn create_decimal128_array(
2917 array: Vec<Option<i128>>,
2918 precision: u8,
2919 scale: i8,
2920 ) -> Result<Decimal128Array, ArrowError> {
2921 array
2922 .into_iter()
2923 .collect::<Decimal128Array>()
2924 .with_precision_and_scale(precision, scale)
2925 }
2926
2927 fn create_decimal256_array(
2928 array: Vec<Option<i256>>,
2929 precision: u8,
2930 scale: i8,
2931 ) -> Result<Decimal256Array, ArrowError> {
2932 array
2933 .into_iter()
2934 .collect::<Decimal256Array>()
2935 .with_precision_and_scale(precision, scale)
2936 }
2937
2938 #[test]
2939 #[cfg(not(feature = "force_validate"))]
2940 #[should_panic(
2941 expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2942 )]
2943 fn test_cast_decimal_to_decimal_round_with_error() {
2944 let array = vec![
2946 Some(i256::from_i128(1123454)),
2947 Some(i256::from_i128(2123456)),
2948 Some(i256::from_i128(-3123453)),
2949 Some(i256::from_i128(-3123456)),
2950 None,
2951 Some(i256::MAX),
2952 Some(i256::MIN),
2953 ];
2954 let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2955 let array = Arc::new(input_decimal_array) as ArrayRef;
2956 let input_type = DataType::Decimal256(76, 4);
2957 let output_type = DataType::Decimal128(20, 3);
2958 assert!(can_cast_types(&input_type, &output_type));
2959 generate_cast_test_case!(
2960 &array,
2961 Decimal128Array,
2962 &output_type,
2963 vec![
2964 Some(112345_i128),
2965 Some(212346_i128),
2966 Some(-312345_i128),
2967 Some(-312346_i128),
2968 None,
2969 None,
2970 None,
2971 ]
2972 );
2973 }
2974
2975 #[test]
2976 #[cfg(not(feature = "force_validate"))]
2977 fn test_cast_decimal_to_decimal_round() {
2978 let array = vec![
2979 Some(1123454),
2980 Some(2123456),
2981 Some(-3123453),
2982 Some(-3123456),
2983 None,
2984 ];
2985 let array = create_decimal128_array(array, 20, 4).unwrap();
2986 let input_type = DataType::Decimal128(20, 4);
2988 let output_type = DataType::Decimal128(20, 3);
2989 assert!(can_cast_types(&input_type, &output_type));
2990 generate_cast_test_case!(
2991 &array,
2992 Decimal128Array,
2993 &output_type,
2994 vec![
2995 Some(112345_i128),
2996 Some(212346_i128),
2997 Some(-312345_i128),
2998 Some(-312346_i128),
2999 None
3000 ]
3001 );
3002
3003 let input_type = DataType::Decimal128(20, 4);
3005 let output_type = DataType::Decimal256(20, 3);
3006 assert!(can_cast_types(&input_type, &output_type));
3007 generate_cast_test_case!(
3008 &array,
3009 Decimal256Array,
3010 &output_type,
3011 vec![
3012 Some(i256::from_i128(112345_i128)),
3013 Some(i256::from_i128(212346_i128)),
3014 Some(i256::from_i128(-312345_i128)),
3015 Some(i256::from_i128(-312346_i128)),
3016 None
3017 ]
3018 );
3019
3020 let array = vec![
3022 Some(i256::from_i128(1123454)),
3023 Some(i256::from_i128(2123456)),
3024 Some(i256::from_i128(-3123453)),
3025 Some(i256::from_i128(-3123456)),
3026 None,
3027 ];
3028 let array = create_decimal256_array(array, 20, 4).unwrap();
3029
3030 let input_type = DataType::Decimal256(20, 4);
3032 let output_type = DataType::Decimal256(20, 3);
3033 assert!(can_cast_types(&input_type, &output_type));
3034 generate_cast_test_case!(
3035 &array,
3036 Decimal256Array,
3037 &output_type,
3038 vec![
3039 Some(i256::from_i128(112345_i128)),
3040 Some(i256::from_i128(212346_i128)),
3041 Some(i256::from_i128(-312345_i128)),
3042 Some(i256::from_i128(-312346_i128)),
3043 None
3044 ]
3045 );
3046 let input_type = DataType::Decimal256(20, 4);
3048 let output_type = DataType::Decimal128(20, 3);
3049 assert!(can_cast_types(&input_type, &output_type));
3050 generate_cast_test_case!(
3051 &array,
3052 Decimal128Array,
3053 &output_type,
3054 vec![
3055 Some(112345_i128),
3056 Some(212346_i128),
3057 Some(-312345_i128),
3058 Some(-312346_i128),
3059 None
3060 ]
3061 );
3062 }
3063
3064 #[test]
3065 fn test_cast_decimal32_to_decimal32() {
3066 let input_type = DataType::Decimal32(9, 3);
3068 let output_type = DataType::Decimal32(9, 4);
3069 assert!(can_cast_types(&input_type, &output_type));
3070 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3071 let array = create_decimal32_array(array, 9, 3).unwrap();
3072 generate_cast_test_case!(
3073 &array,
3074 Decimal32Array,
3075 &output_type,
3076 vec![
3077 Some(11234560_i32),
3078 Some(21234560_i32),
3079 Some(31234560_i32),
3080 None
3081 ]
3082 );
3083 let array = vec![Some(123456), None];
3085 let array = create_decimal32_array(array, 9, 0).unwrap();
3086 let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3087 assert!(result_safe.is_ok());
3088 let options = CastOptions {
3089 safe: false,
3090 ..Default::default()
3091 };
3092
3093 let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3094 assert_eq!(
3095 "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3096 result_unsafe.unwrap_err().to_string()
3097 );
3098 }
3099
3100 #[test]
3101 fn test_cast_decimal64_to_decimal64() {
3102 let input_type = DataType::Decimal64(17, 3);
3104 let output_type = DataType::Decimal64(17, 4);
3105 assert!(can_cast_types(&input_type, &output_type));
3106 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3107 let array = create_decimal64_array(array, 17, 3).unwrap();
3108 generate_cast_test_case!(
3109 &array,
3110 Decimal64Array,
3111 &output_type,
3112 vec![
3113 Some(11234560_i64),
3114 Some(21234560_i64),
3115 Some(31234560_i64),
3116 None
3117 ]
3118 );
3119 let array = vec![Some(123456), None];
3121 let array = create_decimal64_array(array, 9, 0).unwrap();
3122 let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3123 assert!(result_safe.is_ok());
3124 let options = CastOptions {
3125 safe: false,
3126 ..Default::default()
3127 };
3128
3129 let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3130 assert_eq!(
3131 "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3132 result_unsafe.unwrap_err().to_string()
3133 );
3134 }
3135
3136 #[test]
3137 fn test_cast_decimal128_to_decimal128() {
3138 let input_type = DataType::Decimal128(20, 3);
3140 let output_type = DataType::Decimal128(20, 4);
3141 assert!(can_cast_types(&input_type, &output_type));
3142 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3143 let array = create_decimal128_array(array, 20, 3).unwrap();
3144 generate_cast_test_case!(
3145 &array,
3146 Decimal128Array,
3147 &output_type,
3148 vec![
3149 Some(11234560_i128),
3150 Some(21234560_i128),
3151 Some(31234560_i128),
3152 None
3153 ]
3154 );
3155 let array = vec![Some(123456), None];
3157 let array = create_decimal128_array(array, 10, 0).unwrap();
3158 let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3159 assert!(result_safe.is_ok());
3160 let options = CastOptions {
3161 safe: false,
3162 ..Default::default()
3163 };
3164
3165 let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3166 assert_eq!(
3167 "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3168 result_unsafe.unwrap_err().to_string()
3169 );
3170 }
3171
3172 #[test]
3173 fn test_cast_decimal32_to_decimal32_dict() {
3174 let p = 9;
3175 let s = 3;
3176 let input_type = DataType::Decimal32(p, s);
3177 let output_type = DataType::Dictionary(
3178 Box::new(DataType::Int32),
3179 Box::new(DataType::Decimal32(p, s)),
3180 );
3181 assert!(can_cast_types(&input_type, &output_type));
3182 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3183 let array = create_decimal32_array(array, p, s).unwrap();
3184 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3185 assert_eq!(cast_array.data_type(), &output_type);
3186 }
3187
3188 #[test]
3189 fn test_cast_decimal64_to_decimal64_dict() {
3190 let p = 15;
3191 let s = 3;
3192 let input_type = DataType::Decimal64(p, s);
3193 let output_type = DataType::Dictionary(
3194 Box::new(DataType::Int32),
3195 Box::new(DataType::Decimal64(p, s)),
3196 );
3197 assert!(can_cast_types(&input_type, &output_type));
3198 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3199 let array = create_decimal64_array(array, p, s).unwrap();
3200 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3201 assert_eq!(cast_array.data_type(), &output_type);
3202 }
3203
3204 #[test]
3205 fn test_cast_decimal128_to_decimal128_dict() {
3206 let p = 20;
3207 let s = 3;
3208 let input_type = DataType::Decimal128(p, s);
3209 let output_type = DataType::Dictionary(
3210 Box::new(DataType::Int32),
3211 Box::new(DataType::Decimal128(p, s)),
3212 );
3213 assert!(can_cast_types(&input_type, &output_type));
3214 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3215 let array = create_decimal128_array(array, p, s).unwrap();
3216 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3217 assert_eq!(cast_array.data_type(), &output_type);
3218 }
3219
3220 #[test]
3221 fn test_cast_decimal256_to_decimal256_dict() {
3222 let p = 20;
3223 let s = 3;
3224 let input_type = DataType::Decimal256(p, s);
3225 let output_type = DataType::Dictionary(
3226 Box::new(DataType::Int32),
3227 Box::new(DataType::Decimal256(p, s)),
3228 );
3229 assert!(can_cast_types(&input_type, &output_type));
3230 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3231 let array = create_decimal128_array(array, p, s).unwrap();
3232 let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3233 assert_eq!(cast_array.data_type(), &output_type);
3234 }
3235
3236 #[test]
3237 fn test_cast_decimal32_to_decimal32_overflow() {
3238 let input_type = DataType::Decimal32(9, 3);
3239 let output_type = DataType::Decimal32(9, 9);
3240 assert!(can_cast_types(&input_type, &output_type));
3241
3242 let array = vec![Some(i32::MAX)];
3243 let array = create_decimal32_array(array, 9, 3).unwrap();
3244 let result = cast_with_options(
3245 &array,
3246 &output_type,
3247 &CastOptions {
3248 safe: false,
3249 format_options: FormatOptions::default(),
3250 },
3251 );
3252 assert_eq!(
3253 "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3254 result.unwrap_err().to_string()
3255 );
3256 }
3257
3258 #[test]
3259 fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3260 let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3261 let array = create_decimal32_array(array, 9, 3).unwrap();
3262
3263 let output_type = DataType::Decimal32(9, -6);
3265 assert!(can_cast_types(array.data_type(), &output_type));
3266 generate_cast_test_case!(
3267 &array,
3268 Decimal32Array,
3269 &output_type,
3270 vec![Some(-1), Some(0), Some(1), None]
3271 );
3272
3273 let output_type = DataType::Decimal32(9, -7);
3275 assert!(can_cast_types(array.data_type(), &output_type));
3276 generate_cast_test_case!(
3277 &array,
3278 Decimal32Array,
3279 &output_type,
3280 vec![Some(0), Some(0), Some(0), None]
3281 );
3282 }
3283
3284 #[test]
3285 fn test_cast_decimal64_to_decimal64_overflow() {
3286 let input_type = DataType::Decimal64(18, 3);
3287 let output_type = DataType::Decimal64(18, 18);
3288 assert!(can_cast_types(&input_type, &output_type));
3289
3290 let array = vec![Some(i64::MAX)];
3291 let array = create_decimal64_array(array, 18, 3).unwrap();
3292 let result = cast_with_options(
3293 &array,
3294 &output_type,
3295 &CastOptions {
3296 safe: false,
3297 format_options: FormatOptions::default(),
3298 },
3299 );
3300 assert_eq!(
3301 "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3302 result.unwrap_err().to_string()
3303 );
3304 }
3305
3306 #[test]
3307 fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3308 let array = vec![
3309 Some(-999999999999999999),
3310 Some(0),
3311 Some(999999999999999999),
3312 None,
3313 ];
3314 let array = create_decimal64_array(array, 18, 3).unwrap();
3315
3316 let output_type = DataType::Decimal64(18, -15);
3318 assert!(can_cast_types(array.data_type(), &output_type));
3319 generate_cast_test_case!(
3320 &array,
3321 Decimal64Array,
3322 &output_type,
3323 vec![Some(-1), Some(0), Some(1), None]
3324 );
3325
3326 let output_type = DataType::Decimal64(18, -16);
3328 assert!(can_cast_types(array.data_type(), &output_type));
3329 generate_cast_test_case!(
3330 &array,
3331 Decimal64Array,
3332 &output_type,
3333 vec![Some(0), Some(0), Some(0), None]
3334 );
3335 }
3336
3337 #[test]
3338 fn test_cast_floating_to_decimals() {
3339 for output_type in [
3340 DataType::Decimal32(9, 3),
3341 DataType::Decimal64(9, 3),
3342 DataType::Decimal128(9, 3),
3343 DataType::Decimal256(9, 3),
3344 ] {
3345 let input_type = DataType::Float64;
3346 assert!(can_cast_types(&input_type, &output_type));
3347
3348 let array = vec![Some(1.1_f64)];
3349 let array = PrimitiveArray::<Float64Type>::from_iter(array);
3350 let result = cast_with_options(
3351 &array,
3352 &output_type,
3353 &CastOptions {
3354 safe: false,
3355 format_options: FormatOptions::default(),
3356 },
3357 );
3358 assert!(
3359 result.is_ok(),
3360 "Failed to cast to {output_type} with: {}",
3361 result.unwrap_err()
3362 );
3363 }
3364 }
3365
3366 #[test]
3367 fn test_cast_decimal128_to_decimal128_overflow() {
3368 let input_type = DataType::Decimal128(38, 3);
3369 let output_type = DataType::Decimal128(38, 38);
3370 assert!(can_cast_types(&input_type, &output_type));
3371
3372 let array = vec![Some(i128::MAX)];
3373 let array = create_decimal128_array(array, 38, 3).unwrap();
3374 let result = cast_with_options(
3375 &array,
3376 &output_type,
3377 &CastOptions {
3378 safe: false,
3379 format_options: FormatOptions::default(),
3380 },
3381 );
3382 assert_eq!(
3383 "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3384 result.unwrap_err().to_string()
3385 );
3386 }
3387
3388 #[test]
3389 fn test_cast_decimal128_to_decimal256_overflow() {
3390 let input_type = DataType::Decimal128(38, 3);
3391 let output_type = DataType::Decimal256(76, 76);
3392 assert!(can_cast_types(&input_type, &output_type));
3393
3394 let array = vec![Some(i128::MAX)];
3395 let array = create_decimal128_array(array, 38, 3).unwrap();
3396 let result = cast_with_options(
3397 &array,
3398 &output_type,
3399 &CastOptions {
3400 safe: false,
3401 format_options: FormatOptions::default(),
3402 },
3403 );
3404 assert_eq!(
3405 "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3406 result.unwrap_err().to_string()
3407 );
3408 }
3409
3410 #[test]
3411 fn test_cast_decimal32_to_decimal256() {
3412 let input_type = DataType::Decimal32(8, 3);
3413 let output_type = DataType::Decimal256(20, 4);
3414 assert!(can_cast_types(&input_type, &output_type));
3415 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3416 let array = create_decimal32_array(array, 8, 3).unwrap();
3417 generate_cast_test_case!(
3418 &array,
3419 Decimal256Array,
3420 &output_type,
3421 vec![
3422 Some(i256::from_i128(11234560_i128)),
3423 Some(i256::from_i128(21234560_i128)),
3424 Some(i256::from_i128(31234560_i128)),
3425 None
3426 ]
3427 );
3428 }
3429 #[test]
3430 fn test_cast_decimal64_to_decimal256() {
3431 let input_type = DataType::Decimal64(12, 3);
3432 let output_type = DataType::Decimal256(20, 4);
3433 assert!(can_cast_types(&input_type, &output_type));
3434 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3435 let array = create_decimal64_array(array, 12, 3).unwrap();
3436 generate_cast_test_case!(
3437 &array,
3438 Decimal256Array,
3439 &output_type,
3440 vec![
3441 Some(i256::from_i128(11234560_i128)),
3442 Some(i256::from_i128(21234560_i128)),
3443 Some(i256::from_i128(31234560_i128)),
3444 None
3445 ]
3446 );
3447 }
3448 #[test]
3449 fn test_cast_decimal128_to_decimal256() {
3450 let input_type = DataType::Decimal128(20, 3);
3451 let output_type = DataType::Decimal256(20, 4);
3452 assert!(can_cast_types(&input_type, &output_type));
3453 let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3454 let array = create_decimal128_array(array, 20, 3).unwrap();
3455 generate_cast_test_case!(
3456 &array,
3457 Decimal256Array,
3458 &output_type,
3459 vec![
3460 Some(i256::from_i128(11234560_i128)),
3461 Some(i256::from_i128(21234560_i128)),
3462 Some(i256::from_i128(31234560_i128)),
3463 None
3464 ]
3465 );
3466 }
3467
3468 #[test]
3469 fn test_cast_decimal256_to_decimal128_overflow() {
3470 let input_type = DataType::Decimal256(76, 5);
3471 let output_type = DataType::Decimal128(38, 7);
3472 assert!(can_cast_types(&input_type, &output_type));
3473 let array = vec![Some(i256::from_i128(i128::MAX))];
3474 let array = create_decimal256_array(array, 76, 5).unwrap();
3475 let result = cast_with_options(
3476 &array,
3477 &output_type,
3478 &CastOptions {
3479 safe: false,
3480 format_options: FormatOptions::default(),
3481 },
3482 );
3483 assert_eq!(
3484 "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3485 result.unwrap_err().to_string()
3486 );
3487 }
3488
3489 #[test]
3490 fn test_cast_decimal256_to_decimal256_overflow() {
3491 let input_type = DataType::Decimal256(76, 5);
3492 let output_type = DataType::Decimal256(76, 55);
3493 assert!(can_cast_types(&input_type, &output_type));
3494 let array = vec![Some(i256::from_i128(i128::MAX))];
3495 let array = create_decimal256_array(array, 76, 5).unwrap();
3496 let result = cast_with_options(
3497 &array,
3498 &output_type,
3499 &CastOptions {
3500 safe: false,
3501 format_options: FormatOptions::default(),
3502 },
3503 );
3504 assert_eq!(
3505 "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3506 result.unwrap_err().to_string()
3507 );
3508 }
3509
3510 #[test]
3511 fn test_cast_decimal256_to_decimal128() {
3512 let input_type = DataType::Decimal256(20, 3);
3513 let output_type = DataType::Decimal128(20, 4);
3514 assert!(can_cast_types(&input_type, &output_type));
3515 let array = vec![
3516 Some(i256::from_i128(1123456)),
3517 Some(i256::from_i128(2123456)),
3518 Some(i256::from_i128(3123456)),
3519 None,
3520 ];
3521 let array = create_decimal256_array(array, 20, 3).unwrap();
3522 generate_cast_test_case!(
3523 &array,
3524 Decimal128Array,
3525 &output_type,
3526 vec![
3527 Some(11234560_i128),
3528 Some(21234560_i128),
3529 Some(31234560_i128),
3530 None
3531 ]
3532 );
3533 }
3534
3535 #[test]
3536 fn test_cast_decimal256_to_decimal256() {
3537 let input_type = DataType::Decimal256(20, 3);
3538 let output_type = DataType::Decimal256(20, 4);
3539 assert!(can_cast_types(&input_type, &output_type));
3540 let array = vec![
3541 Some(i256::from_i128(1123456)),
3542 Some(i256::from_i128(2123456)),
3543 Some(i256::from_i128(3123456)),
3544 None,
3545 ];
3546 let array = create_decimal256_array(array, 20, 3).unwrap();
3547 generate_cast_test_case!(
3548 &array,
3549 Decimal256Array,
3550 &output_type,
3551 vec![
3552 Some(i256::from_i128(11234560_i128)),
3553 Some(i256::from_i128(21234560_i128)),
3554 Some(i256::from_i128(31234560_i128)),
3555 None
3556 ]
3557 );
3558 }
3559
3560 fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3561 where
3562 T: ArrowPrimitiveType + DecimalType,
3563 {
3564 generate_cast_test_case!(
3566 array,
3567 UInt8Array,
3568 &DataType::UInt8,
3569 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3570 );
3571 generate_cast_test_case!(
3573 array,
3574 UInt16Array,
3575 &DataType::UInt16,
3576 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3577 );
3578 generate_cast_test_case!(
3580 array,
3581 UInt32Array,
3582 &DataType::UInt32,
3583 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3584 );
3585 generate_cast_test_case!(
3587 array,
3588 UInt64Array,
3589 &DataType::UInt64,
3590 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3591 );
3592 generate_cast_test_case!(
3594 array,
3595 Int8Array,
3596 &DataType::Int8,
3597 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3598 );
3599 generate_cast_test_case!(
3601 array,
3602 Int16Array,
3603 &DataType::Int16,
3604 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3605 );
3606 generate_cast_test_case!(
3608 array,
3609 Int32Array,
3610 &DataType::Int32,
3611 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3612 );
3613 generate_cast_test_case!(
3615 array,
3616 Int64Array,
3617 &DataType::Int64,
3618 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3619 );
3620 generate_cast_test_case!(
3622 array,
3623 Float32Array,
3624 &DataType::Float32,
3625 vec![
3626 Some(1.25_f32),
3627 Some(2.25_f32),
3628 Some(3.25_f32),
3629 None,
3630 Some(5.25_f32)
3631 ]
3632 );
3633 generate_cast_test_case!(
3635 array,
3636 Float64Array,
3637 &DataType::Float64,
3638 vec![
3639 Some(1.25_f64),
3640 Some(2.25_f64),
3641 Some(3.25_f64),
3642 None,
3643 Some(5.25_f64)
3644 ]
3645 );
3646 }
3647
3648 #[test]
3649 fn test_cast_decimal32_to_numeric() {
3650 let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3651 let array = create_decimal32_array(value_array, 8, 2).unwrap();
3652
3653 generate_decimal_to_numeric_cast_test_case(&array);
3654 }
3655
3656 #[test]
3657 fn test_cast_decimal64_to_numeric() {
3658 let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3659 let array = create_decimal64_array(value_array, 8, 2).unwrap();
3660
3661 generate_decimal_to_numeric_cast_test_case(&array);
3662 }
3663
3664 #[test]
3665 fn test_cast_decimal128_to_numeric() {
3666 let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3667 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3668
3669 generate_decimal_to_numeric_cast_test_case(&array);
3670
3671 let value_array: Vec<Option<i128>> = vec![Some(51300)];
3673 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3674 let casted_array = cast_with_options(
3675 &array,
3676 &DataType::UInt8,
3677 &CastOptions {
3678 safe: false,
3679 format_options: FormatOptions::default(),
3680 },
3681 );
3682 assert_eq!(
3683 "Cast error: value of 513 is out of range UInt8".to_string(),
3684 casted_array.unwrap_err().to_string()
3685 );
3686
3687 let casted_array = cast_with_options(
3688 &array,
3689 &DataType::UInt8,
3690 &CastOptions {
3691 safe: true,
3692 format_options: FormatOptions::default(),
3693 },
3694 );
3695 assert!(casted_array.is_ok());
3696 assert!(casted_array.unwrap().is_null(0));
3697
3698 let value_array: Vec<Option<i128>> = vec![Some(24400)];
3700 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3701 let casted_array = cast_with_options(
3702 &array,
3703 &DataType::Int8,
3704 &CastOptions {
3705 safe: false,
3706 format_options: FormatOptions::default(),
3707 },
3708 );
3709 assert_eq!(
3710 "Cast error: value of 244 is out of range Int8".to_string(),
3711 casted_array.unwrap_err().to_string()
3712 );
3713
3714 let casted_array = cast_with_options(
3715 &array,
3716 &DataType::Int8,
3717 &CastOptions {
3718 safe: true,
3719 format_options: FormatOptions::default(),
3720 },
3721 );
3722 assert!(casted_array.is_ok());
3723 assert!(casted_array.unwrap().is_null(0));
3724
3725 let value_array: Vec<Option<i128>> = vec![
3729 Some(125),
3730 Some(225),
3731 Some(325),
3732 None,
3733 Some(525),
3734 Some(112345678),
3735 Some(112345679),
3736 ];
3737 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3738 generate_cast_test_case!(
3739 &array,
3740 Float32Array,
3741 &DataType::Float32,
3742 vec![
3743 Some(1.25_f32),
3744 Some(2.25_f32),
3745 Some(3.25_f32),
3746 None,
3747 Some(5.25_f32),
3748 Some(1_123_456.7_f32),
3749 Some(1_123_456.7_f32)
3750 ]
3751 );
3752
3753 let value_array: Vec<Option<i128>> = vec![
3756 Some(125),
3757 Some(225),
3758 Some(325),
3759 None,
3760 Some(525),
3761 Some(112345678901234568),
3762 Some(112345678901234560),
3763 ];
3764 let array = create_decimal128_array(value_array, 38, 2).unwrap();
3765 generate_cast_test_case!(
3766 &array,
3767 Float64Array,
3768 &DataType::Float64,
3769 vec![
3770 Some(1.25_f64),
3771 Some(2.25_f64),
3772 Some(3.25_f64),
3773 None,
3774 Some(5.25_f64),
3775 Some(1_123_456_789_012_345.6_f64),
3776 Some(1_123_456_789_012_345.6_f64),
3777 ]
3778 );
3779 }
3780
3781 #[test]
3782 fn test_cast_decimal256_to_numeric() {
3783 let value_array: Vec<Option<i256>> = vec![
3784 Some(i256::from_i128(125)),
3785 Some(i256::from_i128(225)),
3786 Some(i256::from_i128(325)),
3787 None,
3788 Some(i256::from_i128(525)),
3789 ];
3790 let array = create_decimal256_array(value_array, 38, 2).unwrap();
3791 generate_cast_test_case!(
3793 &array,
3794 UInt8Array,
3795 &DataType::UInt8,
3796 vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3797 );
3798 generate_cast_test_case!(
3800 &array,
3801 UInt16Array,
3802 &DataType::UInt16,
3803 vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3804 );
3805 generate_cast_test_case!(
3807 &array,
3808 UInt32Array,
3809 &DataType::UInt32,
3810 vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3811 );
3812 generate_cast_test_case!(
3814 &array,
3815 UInt64Array,
3816 &DataType::UInt64,
3817 vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3818 );
3819 generate_cast_test_case!(
3821 &array,
3822 Int8Array,
3823 &DataType::Int8,
3824 vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3825 );
3826 generate_cast_test_case!(
3828 &array,
3829 Int16Array,
3830 &DataType::Int16,
3831 vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3832 );
3833 generate_cast_test_case!(
3835 &array,
3836 Int32Array,
3837 &DataType::Int32,
3838 vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3839 );
3840 generate_cast_test_case!(
3842 &array,
3843 Int64Array,
3844 &DataType::Int64,
3845 vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3846 );
3847 generate_cast_test_case!(
3849 &array,
3850 Float32Array,
3851 &DataType::Float32,
3852 vec![
3853 Some(1.25_f32),
3854 Some(2.25_f32),
3855 Some(3.25_f32),
3856 None,
3857 Some(5.25_f32)
3858 ]
3859 );
3860 generate_cast_test_case!(
3862 &array,
3863 Float64Array,
3864 &DataType::Float64,
3865 vec![
3866 Some(1.25_f64),
3867 Some(2.25_f64),
3868 Some(3.25_f64),
3869 None,
3870 Some(5.25_f64)
3871 ]
3872 );
3873
3874 let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3876 let array = create_decimal256_array(value_array, 38, 2).unwrap();
3877 let casted_array = cast_with_options(
3878 &array,
3879 &DataType::Int8,
3880 &CastOptions {
3881 safe: false,
3882 format_options: FormatOptions::default(),
3883 },
3884 );
3885 assert_eq!(
3886 "Cast error: value of 244 is out of range Int8".to_string(),
3887 casted_array.unwrap_err().to_string()
3888 );
3889
3890 let casted_array = cast_with_options(
3891 &array,
3892 &DataType::Int8,
3893 &CastOptions {
3894 safe: true,
3895 format_options: FormatOptions::default(),
3896 },
3897 );
3898 assert!(casted_array.is_ok());
3899 assert!(casted_array.unwrap().is_null(0));
3900
3901 let value_array: Vec<Option<i256>> = vec![
3905 Some(i256::from_i128(125)),
3906 Some(i256::from_i128(225)),
3907 Some(i256::from_i128(325)),
3908 None,
3909 Some(i256::from_i128(525)),
3910 Some(i256::from_i128(112345678)),
3911 Some(i256::from_i128(112345679)),
3912 ];
3913 let array = create_decimal256_array(value_array, 76, 2).unwrap();
3914 generate_cast_test_case!(
3915 &array,
3916 Float32Array,
3917 &DataType::Float32,
3918 vec![
3919 Some(1.25_f32),
3920 Some(2.25_f32),
3921 Some(3.25_f32),
3922 None,
3923 Some(5.25_f32),
3924 Some(1_123_456.7_f32),
3925 Some(1_123_456.7_f32)
3926 ]
3927 );
3928
3929 let value_array: Vec<Option<i256>> = vec![
3932 Some(i256::from_i128(125)),
3933 Some(i256::from_i128(225)),
3934 Some(i256::from_i128(325)),
3935 None,
3936 Some(i256::from_i128(525)),
3937 Some(i256::from_i128(112345678901234568)),
3938 Some(i256::from_i128(112345678901234560)),
3939 ];
3940 let array = create_decimal256_array(value_array, 76, 2).unwrap();
3941 generate_cast_test_case!(
3942 &array,
3943 Float64Array,
3944 &DataType::Float64,
3945 vec![
3946 Some(1.25_f64),
3947 Some(2.25_f64),
3948 Some(3.25_f64),
3949 None,
3950 Some(5.25_f64),
3951 Some(1_123_456_789_012_345.6_f64),
3952 Some(1_123_456_789_012_345.6_f64),
3953 ]
3954 );
3955 }
3956
3957 #[test]
3958 fn test_cast_numeric_to_decimal128() {
3959 let decimal_type = DataType::Decimal128(38, 6);
3960 let input_datas = vec![
3962 Arc::new(UInt8Array::from(vec![
3963 Some(1),
3964 Some(2),
3965 Some(3),
3966 None,
3967 Some(5),
3968 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
3970 Some(1),
3971 Some(2),
3972 Some(3),
3973 None,
3974 Some(5),
3975 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
3977 Some(1),
3978 Some(2),
3979 Some(3),
3980 None,
3981 Some(5),
3982 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
3984 Some(1),
3985 Some(2),
3986 Some(3),
3987 None,
3988 Some(5),
3989 ])) as ArrayRef, ];
3991
3992 for array in input_datas {
3993 generate_cast_test_case!(
3994 &array,
3995 Decimal128Array,
3996 &decimal_type,
3997 vec![
3998 Some(1000000_i128),
3999 Some(2000000_i128),
4000 Some(3000000_i128),
4001 None,
4002 Some(5000000_i128)
4003 ]
4004 );
4005 }
4006
4007 let input_datas = vec![
4009 Arc::new(Int8Array::from(vec![
4010 Some(1),
4011 Some(2),
4012 Some(3),
4013 None,
4014 Some(5),
4015 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
4017 Some(1),
4018 Some(2),
4019 Some(3),
4020 None,
4021 Some(5),
4022 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
4024 Some(1),
4025 Some(2),
4026 Some(3),
4027 None,
4028 Some(5),
4029 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
4031 Some(1),
4032 Some(2),
4033 Some(3),
4034 None,
4035 Some(5),
4036 ])) as ArrayRef, ];
4038 for array in input_datas {
4039 generate_cast_test_case!(
4040 &array,
4041 Decimal128Array,
4042 &decimal_type,
4043 vec![
4044 Some(1000000_i128),
4045 Some(2000000_i128),
4046 Some(3000000_i128),
4047 None,
4048 Some(5000000_i128)
4049 ]
4050 );
4051 }
4052
4053 let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4056 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4057 assert!(casted_array.is_ok());
4058 let array = casted_array.unwrap();
4059 let array: &Decimal128Array = array.as_primitive();
4060 assert!(array.is_null(4));
4061
4062 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4065 let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4066 assert!(casted_array.is_ok());
4067 let array = casted_array.unwrap();
4068 let array: &Decimal128Array = array.as_primitive();
4069 assert!(array.is_null(4));
4070
4071 let array = Float32Array::from(vec![
4073 Some(1.1),
4074 Some(2.2),
4075 Some(4.4),
4076 None,
4077 Some(1.123_456_4), Some(1.123_456_7), ]);
4080 let array = Arc::new(array) as ArrayRef;
4081 generate_cast_test_case!(
4082 &array,
4083 Decimal128Array,
4084 &decimal_type,
4085 vec![
4086 Some(1100000_i128),
4087 Some(2200000_i128),
4088 Some(4400000_i128),
4089 None,
4090 Some(1123456_i128), Some(1123457_i128), ]
4093 );
4094
4095 let array = Float64Array::from(vec![
4097 Some(1.1),
4098 Some(2.2),
4099 Some(4.4),
4100 None,
4101 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), ]);
4106 generate_cast_test_case!(
4107 &array,
4108 Decimal128Array,
4109 &decimal_type,
4110 vec![
4111 Some(1100000_i128),
4112 Some(2200000_i128),
4113 Some(4400000_i128),
4114 None,
4115 Some(1123456_i128), Some(1123457_i128), Some(1123456_i128), Some(1123457_i128), ]
4120 );
4121 }
4122
4123 #[test]
4124 fn test_cast_numeric_to_decimal256() {
4125 let decimal_type = DataType::Decimal256(76, 6);
4126 let input_datas = vec![
4128 Arc::new(UInt8Array::from(vec![
4129 Some(1),
4130 Some(2),
4131 Some(3),
4132 None,
4133 Some(5),
4134 ])) as ArrayRef, Arc::new(UInt16Array::from(vec![
4136 Some(1),
4137 Some(2),
4138 Some(3),
4139 None,
4140 Some(5),
4141 ])) as ArrayRef, Arc::new(UInt32Array::from(vec![
4143 Some(1),
4144 Some(2),
4145 Some(3),
4146 None,
4147 Some(5),
4148 ])) as ArrayRef, Arc::new(UInt64Array::from(vec![
4150 Some(1),
4151 Some(2),
4152 Some(3),
4153 None,
4154 Some(5),
4155 ])) as ArrayRef, ];
4157
4158 for array in input_datas {
4159 generate_cast_test_case!(
4160 &array,
4161 Decimal256Array,
4162 &decimal_type,
4163 vec![
4164 Some(i256::from_i128(1000000_i128)),
4165 Some(i256::from_i128(2000000_i128)),
4166 Some(i256::from_i128(3000000_i128)),
4167 None,
4168 Some(i256::from_i128(5000000_i128))
4169 ]
4170 );
4171 }
4172
4173 let input_datas = vec![
4175 Arc::new(Int8Array::from(vec![
4176 Some(1),
4177 Some(2),
4178 Some(3),
4179 None,
4180 Some(5),
4181 ])) as ArrayRef, Arc::new(Int16Array::from(vec![
4183 Some(1),
4184 Some(2),
4185 Some(3),
4186 None,
4187 Some(5),
4188 ])) as ArrayRef, Arc::new(Int32Array::from(vec![
4190 Some(1),
4191 Some(2),
4192 Some(3),
4193 None,
4194 Some(5),
4195 ])) as ArrayRef, Arc::new(Int64Array::from(vec![
4197 Some(1),
4198 Some(2),
4199 Some(3),
4200 None,
4201 Some(5),
4202 ])) as ArrayRef, ];
4204 for array in input_datas {
4205 generate_cast_test_case!(
4206 &array,
4207 Decimal256Array,
4208 &decimal_type,
4209 vec![
4210 Some(i256::from_i128(1000000_i128)),
4211 Some(i256::from_i128(2000000_i128)),
4212 Some(i256::from_i128(3000000_i128)),
4213 None,
4214 Some(i256::from_i128(5000000_i128))
4215 ]
4216 );
4217 }
4218
4219 let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4222 let array = Arc::new(array) as ArrayRef;
4223 let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4224 assert!(casted_array.is_ok());
4225 let array = casted_array.unwrap();
4226 let array: &Decimal256Array = array.as_primitive();
4227 assert!(array.is_null(4));
4228
4229 let array = Float32Array::from(vec![
4231 Some(1.1),
4232 Some(2.2),
4233 Some(4.4),
4234 None,
4235 Some(1.123_456_4), Some(1.123_456_7), ]);
4238 generate_cast_test_case!(
4239 &array,
4240 Decimal256Array,
4241 &decimal_type,
4242 vec![
4243 Some(i256::from_i128(1100000_i128)),
4244 Some(i256::from_i128(2200000_i128)),
4245 Some(i256::from_i128(4400000_i128)),
4246 None,
4247 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4250 );
4251
4252 let array = Float64Array::from(vec![
4254 Some(1.1),
4255 Some(2.2),
4256 Some(4.4),
4257 None,
4258 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), ]);
4263 generate_cast_test_case!(
4264 &array,
4265 Decimal256Array,
4266 &decimal_type,
4267 vec![
4268 Some(i256::from_i128(1100000_i128)),
4269 Some(i256::from_i128(2200000_i128)),
4270 Some(i256::from_i128(4400000_i128)),
4271 None,
4272 Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), Some(i256::from_i128(1123456_i128)), Some(i256::from_i128(1123457_i128)), ]
4277 );
4278 }
4279
4280 #[test]
4281 fn test_cast_i32_to_f64() {
4282 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4283 let b = cast(&array, &DataType::Float64).unwrap();
4284 let c = b.as_primitive::<Float64Type>();
4285 assert_eq!(5.0, c.value(0));
4286 assert_eq!(6.0, c.value(1));
4287 assert_eq!(7.0, c.value(2));
4288 assert_eq!(8.0, c.value(3));
4289 assert_eq!(9.0, c.value(4));
4290 }
4291
4292 #[test]
4293 fn test_cast_i32_to_u8() {
4294 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4295 let b = cast(&array, &DataType::UInt8).unwrap();
4296 let c = b.as_primitive::<UInt8Type>();
4297 assert!(!c.is_valid(0));
4298 assert_eq!(6, c.value(1));
4299 assert!(!c.is_valid(2));
4300 assert_eq!(8, c.value(3));
4301 assert!(!c.is_valid(4));
4303 }
4304
4305 #[test]
4306 #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4307 fn test_cast_int32_to_u8_with_error() {
4308 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4309 let cast_option = CastOptions {
4311 safe: false,
4312 format_options: FormatOptions::default(),
4313 };
4314 let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4315 assert!(result.is_err());
4316 result.unwrap();
4317 }
4318
4319 #[test]
4320 fn test_cast_i32_to_u8_sliced() {
4321 let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4322 assert_eq!(0, array.offset());
4323 let array = array.slice(2, 3);
4324 let b = cast(&array, &DataType::UInt8).unwrap();
4325 assert_eq!(3, b.len());
4326 let c = b.as_primitive::<UInt8Type>();
4327 assert!(!c.is_valid(0));
4328 assert_eq!(8, c.value(1));
4329 assert!(!c.is_valid(2));
4331 }
4332
4333 #[test]
4334 fn test_cast_i32_to_i32() {
4335 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4336 let b = cast(&array, &DataType::Int32).unwrap();
4337 let c = b.as_primitive::<Int32Type>();
4338 assert_eq!(5, c.value(0));
4339 assert_eq!(6, c.value(1));
4340 assert_eq!(7, c.value(2));
4341 assert_eq!(8, c.value(3));
4342 assert_eq!(9, c.value(4));
4343 }
4344
4345 #[test]
4346 fn test_cast_i32_to_list_i32() {
4347 let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4348 let b = cast(
4349 &array,
4350 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4351 )
4352 .unwrap();
4353 assert_eq!(5, b.len());
4354 let arr = b.as_list::<i32>();
4355 assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4356 assert_eq!(1, arr.value_length(0));
4357 assert_eq!(1, arr.value_length(1));
4358 assert_eq!(1, arr.value_length(2));
4359 assert_eq!(1, arr.value_length(3));
4360 assert_eq!(1, arr.value_length(4));
4361 let c = arr.values().as_primitive::<Int32Type>();
4362 assert_eq!(5, c.value(0));
4363 assert_eq!(6, c.value(1));
4364 assert_eq!(7, c.value(2));
4365 assert_eq!(8, c.value(3));
4366 assert_eq!(9, c.value(4));
4367 }
4368
4369 #[test]
4370 fn test_cast_i32_to_list_i32_nullable() {
4371 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4372 let b = cast(
4373 &array,
4374 &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4375 )
4376 .unwrap();
4377 assert_eq!(5, b.len());
4378 assert_eq!(0, b.null_count());
4379 let arr = b.as_list::<i32>();
4380 assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4381 assert_eq!(1, arr.value_length(0));
4382 assert_eq!(1, arr.value_length(1));
4383 assert_eq!(1, arr.value_length(2));
4384 assert_eq!(1, arr.value_length(3));
4385 assert_eq!(1, arr.value_length(4));
4386
4387 let c = arr.values().as_primitive::<Int32Type>();
4388 assert_eq!(1, c.null_count());
4389 assert_eq!(5, c.value(0));
4390 assert!(!c.is_valid(1));
4391 assert_eq!(7, c.value(2));
4392 assert_eq!(8, c.value(3));
4393 assert_eq!(9, c.value(4));
4394 }
4395
4396 #[test]
4397 fn test_cast_i32_to_list_f64_nullable_sliced() {
4398 let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4399 let array = array.slice(2, 4);
4400 let b = cast(
4401 &array,
4402 &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4403 )
4404 .unwrap();
4405 assert_eq!(4, b.len());
4406 assert_eq!(0, b.null_count());
4407 let arr = b.as_list::<i32>();
4408 assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4409 assert_eq!(1, arr.value_length(0));
4410 assert_eq!(1, arr.value_length(1));
4411 assert_eq!(1, arr.value_length(2));
4412 assert_eq!(1, arr.value_length(3));
4413 let c = arr.values().as_primitive::<Float64Type>();
4414 assert_eq!(1, c.null_count());
4415 assert_eq!(7.0, c.value(0));
4416 assert_eq!(8.0, c.value(1));
4417 assert!(!c.is_valid(2));
4418 assert_eq!(10.0, c.value(3));
4419 }
4420
4421 #[test]
4422 fn test_cast_int_to_utf8view() {
4423 let inputs = vec![
4424 Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4425 Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4426 Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4427 Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4428 Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4429 Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4430 Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4431 Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4432 ];
4433 let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4434 None,
4435 Some("8"),
4436 Some("9"),
4437 Some("10"),
4438 ]));
4439
4440 for array in inputs {
4441 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4442 let arr = cast(&array, &DataType::Utf8View).unwrap();
4443 assert_eq!(expected.as_ref(), arr.as_ref());
4444 }
4445 }
4446
4447 #[test]
4448 fn test_cast_float_to_utf8view() {
4449 let inputs = vec![
4450 Arc::new(Float16Array::from(vec![
4451 Some(f16::from_f64(1.5)),
4452 Some(f16::from_f64(2.5)),
4453 None,
4454 ])) as ArrayRef,
4455 Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4456 Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4457 ];
4458
4459 let expected: ArrayRef =
4460 Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4461
4462 for array in inputs {
4463 assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4464 let arr = cast(&array, &DataType::Utf8View).unwrap();
4465 assert_eq!(expected.as_ref(), arr.as_ref());
4466 }
4467 }
4468
4469 #[test]
4470 fn test_cast_utf8_to_i32() {
4471 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4472 let b = cast(&array, &DataType::Int32).unwrap();
4473 let c = b.as_primitive::<Int32Type>();
4474 assert_eq!(5, c.value(0));
4475 assert_eq!(6, c.value(1));
4476 assert!(!c.is_valid(2));
4477 assert_eq!(8, c.value(3));
4478 assert!(!c.is_valid(4));
4479 }
4480
4481 #[test]
4482 fn test_cast_utf8view_to_i32() {
4483 let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4484 let b = cast(&array, &DataType::Int32).unwrap();
4485 let c = b.as_primitive::<Int32Type>();
4486 assert_eq!(5, c.value(0));
4487 assert_eq!(6, c.value(1));
4488 assert!(!c.is_valid(2));
4489 assert_eq!(8, c.value(3));
4490 assert!(!c.is_valid(4));
4491 }
4492
4493 #[test]
4494 fn test_cast_utf8view_to_f32() {
4495 let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4496 let b = cast(&array, &DataType::Float32).unwrap();
4497 let c = b.as_primitive::<Float32Type>();
4498 assert_eq!(3.0, c.value(0));
4499 assert_eq!(4.56, c.value(1));
4500 assert!(!c.is_valid(2));
4501 assert_eq!(8.9, c.value(3));
4502 }
4503
4504 #[test]
4505 fn test_cast_utf8view_to_decimal128() {
4506 let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4507 let arr = Arc::new(array) as ArrayRef;
4508 generate_cast_test_case!(
4509 &arr,
4510 Decimal128Array,
4511 &DataType::Decimal128(4, 2),
4512 vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4513 );
4514 }
4515
4516 #[test]
4517 fn test_cast_with_options_utf8_to_i32() {
4518 let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4519 let result = cast_with_options(
4520 &array,
4521 &DataType::Int32,
4522 &CastOptions {
4523 safe: false,
4524 format_options: FormatOptions::default(),
4525 },
4526 );
4527 match result {
4528 Ok(_) => panic!("expected error"),
4529 Err(e) => {
4530 assert!(
4531 e.to_string()
4532 .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4533 "Error: {e}"
4534 )
4535 }
4536 }
4537 }
4538
4539 #[test]
4540 fn test_cast_utf8_to_bool() {
4541 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4542 let casted = cast(&strings, &DataType::Boolean).unwrap();
4543 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4544 assert_eq!(*as_boolean_array(&casted), expected);
4545 }
4546
4547 #[test]
4548 fn test_cast_utf8view_to_bool() {
4549 let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4550 let casted = cast(&strings, &DataType::Boolean).unwrap();
4551 let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4552 assert_eq!(*as_boolean_array(&casted), expected);
4553 }
4554
4555 #[test]
4556 fn test_cast_with_options_utf8_to_bool() {
4557 let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4558 let casted = cast_with_options(
4559 &strings,
4560 &DataType::Boolean,
4561 &CastOptions {
4562 safe: false,
4563 format_options: FormatOptions::default(),
4564 },
4565 );
4566 match casted {
4567 Ok(_) => panic!("expected error"),
4568 Err(e) => {
4569 assert!(
4570 e.to_string().contains(
4571 "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4572 )
4573 )
4574 }
4575 }
4576 }
4577
4578 #[test]
4579 fn test_cast_bool_to_i32() {
4580 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4581 let b = cast(&array, &DataType::Int32).unwrap();
4582 let c = b.as_primitive::<Int32Type>();
4583 assert_eq!(1, c.value(0));
4584 assert_eq!(0, c.value(1));
4585 assert!(!c.is_valid(2));
4586 }
4587
4588 #[test]
4589 fn test_cast_bool_to_utf8view() {
4590 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4591 let b = cast(&array, &DataType::Utf8View).unwrap();
4592 let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4593 assert_eq!("true", c.value(0));
4594 assert_eq!("false", c.value(1));
4595 assert!(!c.is_valid(2));
4596 }
4597
4598 #[test]
4599 fn test_cast_bool_to_utf8() {
4600 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4601 let b = cast(&array, &DataType::Utf8).unwrap();
4602 let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4603 assert_eq!("true", c.value(0));
4604 assert_eq!("false", c.value(1));
4605 assert!(!c.is_valid(2));
4606 }
4607
4608 #[test]
4609 fn test_cast_bool_to_large_utf8() {
4610 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4611 let b = cast(&array, &DataType::LargeUtf8).unwrap();
4612 let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4613 assert_eq!("true", c.value(0));
4614 assert_eq!("false", c.value(1));
4615 assert!(!c.is_valid(2));
4616 }
4617
4618 #[test]
4619 fn test_cast_bool_to_f64() {
4620 let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4621 let b = cast(&array, &DataType::Float64).unwrap();
4622 let c = b.as_primitive::<Float64Type>();
4623 assert_eq!(1.0, c.value(0));
4624 assert_eq!(0.0, c.value(1));
4625 assert!(!c.is_valid(2));
4626 }
4627
4628 #[test]
4629 fn test_cast_integer_to_timestamp() {
4630 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4631 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4632
4633 let array = Int8Array::from(vec![Some(2), Some(10), None]);
4634 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4635
4636 assert_eq!(&actual, &expected);
4637
4638 let array = Int16Array::from(vec![Some(2), Some(10), None]);
4639 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4640
4641 assert_eq!(&actual, &expected);
4642
4643 let array = Int32Array::from(vec![Some(2), Some(10), None]);
4644 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4645
4646 assert_eq!(&actual, &expected);
4647
4648 let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4649 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4650
4651 assert_eq!(&actual, &expected);
4652
4653 let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4654 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4655
4656 assert_eq!(&actual, &expected);
4657
4658 let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4659 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4660
4661 assert_eq!(&actual, &expected);
4662
4663 let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4664 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4665
4666 assert_eq!(&actual, &expected);
4667 }
4668
4669 #[test]
4670 fn test_cast_timestamp_to_integer() {
4671 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4672 .with_timezone("UTC".to_string());
4673 let expected = cast(&array, &DataType::Int64).unwrap();
4674
4675 let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4676 assert_eq!(&actual, &expected);
4677
4678 let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4679 assert_eq!(&actual, &expected);
4680
4681 let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4682 assert_eq!(&actual, &expected);
4683
4684 let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4685 assert_eq!(&actual, &expected);
4686
4687 let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4688 assert_eq!(&actual, &expected);
4689
4690 let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4691 assert_eq!(&actual, &expected);
4692
4693 let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4694 assert_eq!(&actual, &expected);
4695 }
4696
4697 #[test]
4698 fn test_cast_floating_to_timestamp() {
4699 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4700 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4701
4702 let array = Float16Array::from(vec![
4703 Some(f16::from_f32(2.0)),
4704 Some(f16::from_f32(10.6)),
4705 None,
4706 ]);
4707 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4708
4709 assert_eq!(&actual, &expected);
4710
4711 let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4712 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4713
4714 assert_eq!(&actual, &expected);
4715
4716 let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4717 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4718
4719 assert_eq!(&actual, &expected);
4720 }
4721
4722 #[test]
4723 fn test_cast_timestamp_to_floating() {
4724 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4725 .with_timezone("UTC".to_string());
4726 let expected = cast(&array, &DataType::Int64).unwrap();
4727
4728 let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4729 assert_eq!(&actual, &expected);
4730
4731 let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4732 assert_eq!(&actual, &expected);
4733
4734 let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4735 assert_eq!(&actual, &expected);
4736 }
4737
4738 #[test]
4739 fn test_cast_decimal_to_timestamp() {
4740 let array = Int64Array::from(vec![Some(2), Some(10), None]);
4741 let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4742
4743 let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4744 .with_precision_and_scale(4, 2)
4745 .unwrap();
4746 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4747
4748 assert_eq!(&actual, &expected);
4749
4750 let array = Decimal256Array::from(vec![
4751 Some(i256::from_i128(2000)),
4752 Some(i256::from_i128(10000)),
4753 None,
4754 ])
4755 .with_precision_and_scale(5, 3)
4756 .unwrap();
4757 let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4758
4759 assert_eq!(&actual, &expected);
4760 }
4761
4762 #[test]
4763 fn test_cast_timestamp_to_decimal() {
4764 let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4765 .with_timezone("UTC".to_string());
4766 let expected = cast(&array, &DataType::Int64).unwrap();
4767
4768 let actual = cast(
4769 &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4770 &DataType::Int64,
4771 )
4772 .unwrap();
4773 assert_eq!(&actual, &expected);
4774
4775 let actual = cast(
4776 &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4777 &DataType::Int64,
4778 )
4779 .unwrap();
4780 assert_eq!(&actual, &expected);
4781 }
4782
4783 #[test]
4784 fn test_cast_list_i32_to_list_u16() {
4785 let values = vec![
4786 Some(vec![Some(0), Some(0), Some(0)]),
4787 Some(vec![Some(-1), Some(-2), Some(-1)]),
4788 Some(vec![Some(2), Some(100000000)]),
4789 ];
4790 let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
4791
4792 let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
4793 assert!(can_cast_types(list_array.data_type(), &target_type));
4794 let cast_array = cast(&list_array, &target_type).unwrap();
4795
4796 assert_eq!(0, cast_array.null_count());
4801
4802 let array = cast_array.as_list::<i32>();
4804 assert_eq!(list_array.value_offsets(), array.value_offsets());
4805
4806 assert_eq!(DataType::UInt16, array.value_type());
4807 assert_eq!(3, array.value_length(0));
4808 assert_eq!(3, array.value_length(1));
4809 assert_eq!(2, array.value_length(2));
4810
4811 let u16arr = array.values().as_primitive::<UInt16Type>();
4813 assert_eq!(4, u16arr.null_count());
4814
4815 let expected: UInt16Array =
4817 vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4818 .into_iter()
4819 .collect();
4820
4821 assert_eq!(u16arr, &expected);
4822 }
4823
4824 #[test]
4825 fn test_cast_list_i32_to_list_timestamp() {
4826 let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4828
4829 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4830
4831 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4833 let list_data = ArrayData::builder(list_data_type)
4834 .len(3)
4835 .add_buffer(value_offsets)
4836 .add_child_data(value_data)
4837 .build()
4838 .unwrap();
4839 let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4840
4841 let actual = cast(
4842 &list_array,
4843 &DataType::List(Arc::new(Field::new_list_field(
4844 DataType::Timestamp(TimeUnit::Microsecond, None),
4845 true,
4846 ))),
4847 )
4848 .unwrap();
4849
4850 let expected = cast(
4851 &cast(
4852 &list_array,
4853 &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4854 )
4855 .unwrap(),
4856 &DataType::List(Arc::new(Field::new_list_field(
4857 DataType::Timestamp(TimeUnit::Microsecond, None),
4858 true,
4859 ))),
4860 )
4861 .unwrap();
4862
4863 assert_eq!(&actual, &expected);
4864 }
4865
4866 #[test]
4867 fn test_cast_date32_to_date64() {
4868 let a = Date32Array::from(vec![10000, 17890]);
4869 let array = Arc::new(a) as ArrayRef;
4870 let b = cast(&array, &DataType::Date64).unwrap();
4871 let c = b.as_primitive::<Date64Type>();
4872 assert_eq!(864000000000, c.value(0));
4873 assert_eq!(1545696000000, c.value(1));
4874 }
4875
4876 #[test]
4877 fn test_cast_date64_to_date32() {
4878 let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4879 let array = Arc::new(a) as ArrayRef;
4880 let b = cast(&array, &DataType::Date32).unwrap();
4881 let c = b.as_primitive::<Date32Type>();
4882 assert_eq!(10000, c.value(0));
4883 assert_eq!(17890, c.value(1));
4884 assert!(c.is_null(2));
4885 }
4886
4887 #[test]
4888 fn test_cast_string_to_integral_overflow() {
4889 let str = Arc::new(StringArray::from(vec![
4890 Some("123"),
4891 Some("-123"),
4892 Some("86374"),
4893 None,
4894 ])) as ArrayRef;
4895
4896 let options = CastOptions {
4897 safe: true,
4898 format_options: FormatOptions::default(),
4899 };
4900 let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4901 let expected =
4902 Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4903 assert_eq!(&res, &expected);
4904 }
4905
4906 #[test]
4907 fn test_cast_string_to_timestamp() {
4908 let a0 = Arc::new(StringViewArray::from(vec![
4909 Some("2020-09-08T12:00:00.123456789+00:00"),
4910 Some("Not a valid date"),
4911 None,
4912 ])) as ArrayRef;
4913 let a1 = Arc::new(StringArray::from(vec![
4914 Some("2020-09-08T12:00:00.123456789+00:00"),
4915 Some("Not a valid date"),
4916 None,
4917 ])) as ArrayRef;
4918 let a2 = Arc::new(LargeStringArray::from(vec![
4919 Some("2020-09-08T12:00:00.123456789+00:00"),
4920 Some("Not a valid date"),
4921 None,
4922 ])) as ArrayRef;
4923 for array in &[a0, a1, a2] {
4924 for time_unit in &[
4925 TimeUnit::Second,
4926 TimeUnit::Millisecond,
4927 TimeUnit::Microsecond,
4928 TimeUnit::Nanosecond,
4929 ] {
4930 let to_type = DataType::Timestamp(*time_unit, None);
4931 let b = cast(array, &to_type).unwrap();
4932
4933 match time_unit {
4934 TimeUnit::Second => {
4935 let c = b.as_primitive::<TimestampSecondType>();
4936 assert_eq!(1599566400, c.value(0));
4937 assert!(c.is_null(1));
4938 assert!(c.is_null(2));
4939 }
4940 TimeUnit::Millisecond => {
4941 let c = b
4942 .as_any()
4943 .downcast_ref::<TimestampMillisecondArray>()
4944 .unwrap();
4945 assert_eq!(1599566400123, c.value(0));
4946 assert!(c.is_null(1));
4947 assert!(c.is_null(2));
4948 }
4949 TimeUnit::Microsecond => {
4950 let c = b
4951 .as_any()
4952 .downcast_ref::<TimestampMicrosecondArray>()
4953 .unwrap();
4954 assert_eq!(1599566400123456, c.value(0));
4955 assert!(c.is_null(1));
4956 assert!(c.is_null(2));
4957 }
4958 TimeUnit::Nanosecond => {
4959 let c = b
4960 .as_any()
4961 .downcast_ref::<TimestampNanosecondArray>()
4962 .unwrap();
4963 assert_eq!(1599566400123456789, c.value(0));
4964 assert!(c.is_null(1));
4965 assert!(c.is_null(2));
4966 }
4967 }
4968
4969 let options = CastOptions {
4970 safe: false,
4971 format_options: FormatOptions::default(),
4972 };
4973 let err = cast_with_options(array, &to_type, &options).unwrap_err();
4974 assert_eq!(
4975 err.to_string(),
4976 "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4977 );
4978 }
4979 }
4980 }
4981
4982 #[test]
4983 fn test_cast_string_to_timestamp_overflow() {
4984 let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4985 let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4986 let result = result.as_primitive::<TimestampSecondType>();
4987 assert_eq!(result.values(), &[247112596800]);
4988 }
4989
4990 #[test]
4991 fn test_cast_string_to_date32() {
4992 let a0 = Arc::new(StringViewArray::from(vec![
4993 Some("2018-12-25"),
4994 Some("Not a valid date"),
4995 None,
4996 ])) as ArrayRef;
4997 let a1 = Arc::new(StringArray::from(vec![
4998 Some("2018-12-25"),
4999 Some("Not a valid date"),
5000 None,
5001 ])) as ArrayRef;
5002 let a2 = Arc::new(LargeStringArray::from(vec![
5003 Some("2018-12-25"),
5004 Some("Not a valid date"),
5005 None,
5006 ])) as ArrayRef;
5007 for array in &[a0, a1, a2] {
5008 let to_type = DataType::Date32;
5009 let b = cast(array, &to_type).unwrap();
5010 let c = b.as_primitive::<Date32Type>();
5011 assert_eq!(17890, c.value(0));
5012 assert!(c.is_null(1));
5013 assert!(c.is_null(2));
5014
5015 let options = CastOptions {
5016 safe: false,
5017 format_options: FormatOptions::default(),
5018 };
5019 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5020 assert_eq!(
5021 err.to_string(),
5022 "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5023 );
5024 }
5025 }
5026
5027 #[test]
5028 fn test_cast_string_with_large_date_to_date32() {
5029 let array = Arc::new(StringArray::from(vec![
5030 Some("+10999-12-31"),
5031 Some("-0010-02-28"),
5032 Some("0010-02-28"),
5033 Some("0000-01-01"),
5034 Some("-0000-01-01"),
5035 Some("-0001-01-01"),
5036 ])) as ArrayRef;
5037 let to_type = DataType::Date32;
5038 let options = CastOptions {
5039 safe: false,
5040 format_options: FormatOptions::default(),
5041 };
5042 let b = cast_with_options(&array, &to_type, &options).unwrap();
5043 let c = b.as_primitive::<Date32Type>();
5044 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)); }
5052
5053 #[test]
5054 fn test_cast_invalid_string_with_large_date_to_date32() {
5055 let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5057 let to_type = DataType::Date32;
5058 let options = CastOptions {
5059 safe: false,
5060 format_options: FormatOptions::default(),
5061 };
5062 let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5063 assert_eq!(
5064 err.to_string(),
5065 "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5066 );
5067 }
5068
5069 #[test]
5070 fn test_cast_string_format_yyyymmdd_to_date32() {
5071 let a0 = Arc::new(StringViewArray::from(vec![
5072 Some("2020-12-25"),
5073 Some("20201117"),
5074 ])) as ArrayRef;
5075 let a1 = Arc::new(StringArray::from(vec![
5076 Some("2020-12-25"),
5077 Some("20201117"),
5078 ])) as ArrayRef;
5079 let a2 = Arc::new(LargeStringArray::from(vec![
5080 Some("2020-12-25"),
5081 Some("20201117"),
5082 ])) as ArrayRef;
5083
5084 for array in &[a0, a1, a2] {
5085 let to_type = DataType::Date32;
5086 let options = CastOptions {
5087 safe: false,
5088 format_options: FormatOptions::default(),
5089 };
5090 let result = cast_with_options(&array, &to_type, &options).unwrap();
5091 let c = result.as_primitive::<Date32Type>();
5092 assert_eq!(
5093 chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5094 c.value_as_date(0)
5095 );
5096 assert_eq!(
5097 chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5098 c.value_as_date(1)
5099 );
5100 }
5101 }
5102
5103 #[test]
5104 fn test_cast_string_to_time32second() {
5105 let a0 = Arc::new(StringViewArray::from(vec![
5106 Some("08:08:35.091323414"),
5107 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5110 None,
5111 ])) as ArrayRef;
5112 let a1 = Arc::new(StringArray::from(vec![
5113 Some("08:08:35.091323414"),
5114 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5117 None,
5118 ])) as ArrayRef;
5119 let a2 = Arc::new(LargeStringArray::from(vec![
5120 Some("08:08:35.091323414"),
5121 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5124 None,
5125 ])) as ArrayRef;
5126 for array in &[a0, a1, a2] {
5127 let to_type = DataType::Time32(TimeUnit::Second);
5128 let b = cast(array, &to_type).unwrap();
5129 let c = b.as_primitive::<Time32SecondType>();
5130 assert_eq!(29315, c.value(0));
5131 assert_eq!(29340, c.value(1));
5132 assert!(c.is_null(2));
5133 assert!(c.is_null(3));
5134 assert!(c.is_null(4));
5135
5136 let options = CastOptions {
5137 safe: false,
5138 format_options: FormatOptions::default(),
5139 };
5140 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5141 assert_eq!(
5142 err.to_string(),
5143 "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5144 );
5145 }
5146 }
5147
5148 #[test]
5149 fn test_cast_string_to_time32millisecond() {
5150 let a0 = Arc::new(StringViewArray::from(vec![
5151 Some("08:08:35.091323414"),
5152 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5155 None,
5156 ])) as ArrayRef;
5157 let a1 = Arc::new(StringArray::from(vec![
5158 Some("08:08:35.091323414"),
5159 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5162 None,
5163 ])) as ArrayRef;
5164 let a2 = Arc::new(LargeStringArray::from(vec![
5165 Some("08:08:35.091323414"),
5166 Some("08:08:60.091323414"), Some("08:08:61.091323414"), Some("Not a valid time"),
5169 None,
5170 ])) as ArrayRef;
5171 for array in &[a0, a1, a2] {
5172 let to_type = DataType::Time32(TimeUnit::Millisecond);
5173 let b = cast(array, &to_type).unwrap();
5174 let c = b.as_primitive::<Time32MillisecondType>();
5175 assert_eq!(29315091, c.value(0));
5176 assert_eq!(29340091, c.value(1));
5177 assert!(c.is_null(2));
5178 assert!(c.is_null(3));
5179 assert!(c.is_null(4));
5180
5181 let options = CastOptions {
5182 safe: false,
5183 format_options: FormatOptions::default(),
5184 };
5185 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5186 assert_eq!(
5187 err.to_string(),
5188 "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5189 );
5190 }
5191 }
5192
5193 #[test]
5194 fn test_cast_string_to_time64microsecond() {
5195 let a0 = Arc::new(StringViewArray::from(vec![
5196 Some("08:08:35.091323414"),
5197 Some("Not a valid time"),
5198 None,
5199 ])) as ArrayRef;
5200 let a1 = Arc::new(StringArray::from(vec![
5201 Some("08:08:35.091323414"),
5202 Some("Not a valid time"),
5203 None,
5204 ])) as ArrayRef;
5205 let a2 = Arc::new(LargeStringArray::from(vec![
5206 Some("08:08:35.091323414"),
5207 Some("Not a valid time"),
5208 None,
5209 ])) as ArrayRef;
5210 for array in &[a0, a1, a2] {
5211 let to_type = DataType::Time64(TimeUnit::Microsecond);
5212 let b = cast(array, &to_type).unwrap();
5213 let c = b.as_primitive::<Time64MicrosecondType>();
5214 assert_eq!(29315091323, c.value(0));
5215 assert!(c.is_null(1));
5216 assert!(c.is_null(2));
5217
5218 let options = CastOptions {
5219 safe: false,
5220 format_options: FormatOptions::default(),
5221 };
5222 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5223 assert_eq!(
5224 err.to_string(),
5225 "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5226 );
5227 }
5228 }
5229
5230 #[test]
5231 fn test_cast_string_to_time64nanosecond() {
5232 let a0 = Arc::new(StringViewArray::from(vec![
5233 Some("08:08:35.091323414"),
5234 Some("Not a valid time"),
5235 None,
5236 ])) as ArrayRef;
5237 let a1 = Arc::new(StringArray::from(vec![
5238 Some("08:08:35.091323414"),
5239 Some("Not a valid time"),
5240 None,
5241 ])) as ArrayRef;
5242 let a2 = Arc::new(LargeStringArray::from(vec![
5243 Some("08:08:35.091323414"),
5244 Some("Not a valid time"),
5245 None,
5246 ])) as ArrayRef;
5247 for array in &[a0, a1, a2] {
5248 let to_type = DataType::Time64(TimeUnit::Nanosecond);
5249 let b = cast(array, &to_type).unwrap();
5250 let c = b.as_primitive::<Time64NanosecondType>();
5251 assert_eq!(29315091323414, c.value(0));
5252 assert!(c.is_null(1));
5253 assert!(c.is_null(2));
5254
5255 let options = CastOptions {
5256 safe: false,
5257 format_options: FormatOptions::default(),
5258 };
5259 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5260 assert_eq!(
5261 err.to_string(),
5262 "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5263 );
5264 }
5265 }
5266
5267 #[test]
5268 fn test_cast_string_to_date64() {
5269 let a0 = Arc::new(StringViewArray::from(vec![
5270 Some("2020-09-08T12:00:00"),
5271 Some("Not a valid date"),
5272 None,
5273 ])) as ArrayRef;
5274 let a1 = Arc::new(StringArray::from(vec![
5275 Some("2020-09-08T12:00:00"),
5276 Some("Not a valid date"),
5277 None,
5278 ])) as ArrayRef;
5279 let a2 = Arc::new(LargeStringArray::from(vec![
5280 Some("2020-09-08T12:00:00"),
5281 Some("Not a valid date"),
5282 None,
5283 ])) as ArrayRef;
5284 for array in &[a0, a1, a2] {
5285 let to_type = DataType::Date64;
5286 let b = cast(array, &to_type).unwrap();
5287 let c = b.as_primitive::<Date64Type>();
5288 assert_eq!(1599566400000, c.value(0));
5289 assert!(c.is_null(1));
5290 assert!(c.is_null(2));
5291
5292 let options = CastOptions {
5293 safe: false,
5294 format_options: FormatOptions::default(),
5295 };
5296 let err = cast_with_options(array, &to_type, &options).unwrap_err();
5297 assert_eq!(
5298 err.to_string(),
5299 "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5300 );
5301 }
5302 }
5303
5304 macro_rules! test_safe_string_to_interval {
5305 ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5306 let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5307
5308 let options = CastOptions {
5309 safe: true,
5310 format_options: FormatOptions::default(),
5311 };
5312
5313 let target_interval_array = cast_with_options(
5314 &source_string_array.clone(),
5315 &DataType::Interval($interval_unit),
5316 &options,
5317 )
5318 .unwrap()
5319 .as_any()
5320 .downcast_ref::<$array_ty>()
5321 .unwrap()
5322 .clone() as $array_ty;
5323
5324 let target_string_array =
5325 cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5326 .unwrap()
5327 .as_any()
5328 .downcast_ref::<StringArray>()
5329 .unwrap()
5330 .clone();
5331
5332 let expect_string_array = StringArray::from($expect_vec);
5333
5334 assert_eq!(target_string_array, expect_string_array);
5335
5336 let target_large_string_array =
5337 cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5338 .unwrap()
5339 .as_any()
5340 .downcast_ref::<LargeStringArray>()
5341 .unwrap()
5342 .clone();
5343
5344 let expect_large_string_array = LargeStringArray::from($expect_vec);
5345
5346 assert_eq!(target_large_string_array, expect_large_string_array);
5347 };
5348 }
5349
5350 #[test]
5351 fn test_cast_string_to_interval_year_month() {
5352 test_safe_string_to_interval!(
5353 vec![
5354 Some("1 year 1 month"),
5355 Some("1.5 years 13 month"),
5356 Some("30 days"),
5357 Some("31 days"),
5358 Some("2 months 31 days"),
5359 Some("2 months 31 days 1 second"),
5360 Some("foobar"),
5361 ],
5362 IntervalUnit::YearMonth,
5363 IntervalYearMonthArray,
5364 vec![
5365 Some("1 years 1 mons"),
5366 Some("2 years 7 mons"),
5367 None,
5368 None,
5369 None,
5370 None,
5371 None,
5372 ]
5373 );
5374 }
5375
5376 #[test]
5377 fn test_cast_string_to_interval_day_time() {
5378 test_safe_string_to_interval!(
5379 vec![
5380 Some("1 year 1 month"),
5381 Some("1.5 years 13 month"),
5382 Some("30 days"),
5383 Some("1 day 2 second 3.5 milliseconds"),
5384 Some("foobar"),
5385 ],
5386 IntervalUnit::DayTime,
5387 IntervalDayTimeArray,
5388 vec![
5389 Some("390 days"),
5390 Some("930 days"),
5391 Some("30 days"),
5392 None,
5393 None,
5394 ]
5395 );
5396 }
5397
5398 #[test]
5399 fn test_cast_string_to_interval_month_day_nano() {
5400 test_safe_string_to_interval!(
5401 vec![
5402 Some("1 year 1 month 1 day"),
5403 None,
5404 Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5405 Some("3 days"),
5406 Some("8 seconds"),
5407 None,
5408 Some("1 day 29800 milliseconds"),
5409 Some("3 months 1 second"),
5410 Some("6 minutes 120 second"),
5411 Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5412 Some("foobar"),
5413 ],
5414 IntervalUnit::MonthDayNano,
5415 IntervalMonthDayNanoArray,
5416 vec![
5417 Some("13 mons 1 days"),
5418 None,
5419 Some("31 mons 35 days 0.001400000 secs"),
5420 Some("3 days"),
5421 Some("8.000000000 secs"),
5422 None,
5423 Some("1 days 29.800000000 secs"),
5424 Some("3 mons 1.000000000 secs"),
5425 Some("8 mins"),
5426 Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5427 None,
5428 ]
5429 );
5430 }
5431
5432 macro_rules! test_unsafe_string_to_interval_err {
5433 ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5434 let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5435 let options = CastOptions {
5436 safe: false,
5437 format_options: FormatOptions::default(),
5438 };
5439 let arrow_err = cast_with_options(
5440 &string_array.clone(),
5441 &DataType::Interval($interval_unit),
5442 &options,
5443 )
5444 .unwrap_err();
5445 assert_eq!($error_msg, arrow_err.to_string());
5446 };
5447 }
5448
5449 #[test]
5450 fn test_cast_string_to_interval_err() {
5451 test_unsafe_string_to_interval_err!(
5452 vec![Some("foobar")],
5453 IntervalUnit::YearMonth,
5454 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5455 );
5456 test_unsafe_string_to_interval_err!(
5457 vec![Some("foobar")],
5458 IntervalUnit::DayTime,
5459 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5460 );
5461 test_unsafe_string_to_interval_err!(
5462 vec![Some("foobar")],
5463 IntervalUnit::MonthDayNano,
5464 r#"Parser error: Invalid input syntax for type interval: "foobar""#
5465 );
5466 test_unsafe_string_to_interval_err!(
5467 vec![Some("2 months 31 days 1 second")],
5468 IntervalUnit::YearMonth,
5469 r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5470 );
5471 test_unsafe_string_to_interval_err!(
5472 vec![Some("1 day 1.5 milliseconds")],
5473 IntervalUnit::DayTime,
5474 r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5475 );
5476
5477 test_unsafe_string_to_interval_err!(
5479 vec![Some(format!(
5480 "{} century {} year {} month",
5481 i64::MAX - 2,
5482 i64::MAX - 2,
5483 i64::MAX - 2
5484 ))],
5485 IntervalUnit::DayTime,
5486 format!(
5487 "Arithmetic overflow: Overflow happened on: {} * 100",
5488 i64::MAX - 2
5489 )
5490 );
5491 test_unsafe_string_to_interval_err!(
5492 vec![Some(format!(
5493 "{} year {} month {} day",
5494 i64::MAX - 2,
5495 i64::MAX - 2,
5496 i64::MAX - 2
5497 ))],
5498 IntervalUnit::MonthDayNano,
5499 format!(
5500 "Arithmetic overflow: Overflow happened on: {} * 12",
5501 i64::MAX - 2
5502 )
5503 );
5504 }
5505
5506 #[test]
5507 fn test_cast_binary_to_fixed_size_binary() {
5508 let bytes_1 = "Hiiii".as_bytes();
5509 let bytes_2 = "Hello".as_bytes();
5510
5511 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5512 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5513 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5514
5515 let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5516 let down_cast = array_ref
5517 .as_any()
5518 .downcast_ref::<FixedSizeBinaryArray>()
5519 .unwrap();
5520 assert_eq!(bytes_1, down_cast.value(0));
5521 assert_eq!(bytes_2, down_cast.value(1));
5522 assert!(down_cast.is_null(2));
5523
5524 let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5525 let down_cast = array_ref
5526 .as_any()
5527 .downcast_ref::<FixedSizeBinaryArray>()
5528 .unwrap();
5529 assert_eq!(bytes_1, down_cast.value(0));
5530 assert_eq!(bytes_2, down_cast.value(1));
5531 assert!(down_cast.is_null(2));
5532
5533 let bytes_1 = "Hi".as_bytes();
5535 let bytes_2 = "Hello".as_bytes();
5536
5537 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5538 let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5539 let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5540
5541 let array_ref = cast_with_options(
5542 &a1,
5543 &DataType::FixedSizeBinary(5),
5544 &CastOptions {
5545 safe: false,
5546 format_options: FormatOptions::default(),
5547 },
5548 );
5549 assert!(array_ref.is_err());
5550
5551 let array_ref = cast_with_options(
5552 &a2,
5553 &DataType::FixedSizeBinary(5),
5554 &CastOptions {
5555 safe: false,
5556 format_options: FormatOptions::default(),
5557 },
5558 );
5559 assert!(array_ref.is_err());
5560 }
5561
5562 #[test]
5563 fn test_fixed_size_binary_to_binary() {
5564 let bytes_1 = "Hiiii".as_bytes();
5565 let bytes_2 = "Hello".as_bytes();
5566
5567 let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5568 let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5569
5570 let array_ref = cast(&a1, &DataType::Binary).unwrap();
5571 let down_cast = array_ref.as_binary::<i32>();
5572 assert_eq!(bytes_1, down_cast.value(0));
5573 assert_eq!(bytes_2, down_cast.value(1));
5574 assert!(down_cast.is_null(2));
5575
5576 let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5577 let down_cast = array_ref.as_binary::<i64>();
5578 assert_eq!(bytes_1, down_cast.value(0));
5579 assert_eq!(bytes_2, down_cast.value(1));
5580 assert!(down_cast.is_null(2));
5581
5582 let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5583 let down_cast = array_ref.as_binary_view();
5584 assert_eq!(bytes_1, down_cast.value(0));
5585 assert_eq!(bytes_2, down_cast.value(1));
5586 assert!(down_cast.is_null(2));
5587 }
5588
5589 #[test]
5590 fn test_fixed_size_binary_to_dictionary() {
5591 let bytes_1 = "Hiiii".as_bytes();
5592 let bytes_2 = "Hello".as_bytes();
5593
5594 let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5595 let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5596
5597 let cast_type = DataType::Dictionary(
5598 Box::new(DataType::Int8),
5599 Box::new(DataType::FixedSizeBinary(5)),
5600 );
5601 let cast_array = cast(&a1, &cast_type).unwrap();
5602 assert_eq!(cast_array.data_type(), &cast_type);
5603 assert_eq!(
5604 array_to_strings(&cast_array),
5605 vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5606 );
5607 let dict_array = cast_array
5609 .as_any()
5610 .downcast_ref::<DictionaryArray<Int8Type>>()
5611 .unwrap();
5612 assert_eq!(dict_array.values().len(), 2);
5613 }
5614
5615 #[test]
5616 fn test_binary_to_dictionary() {
5617 let mut builder = GenericBinaryBuilder::<i32>::new();
5618 builder.append_value(b"hello");
5619 builder.append_value(b"hiiii");
5620 builder.append_value(b"hiiii"); builder.append_null();
5622 builder.append_value(b"rustt");
5623
5624 let a1 = builder.finish();
5625
5626 let cast_type = DataType::Dictionary(
5627 Box::new(DataType::Int8),
5628 Box::new(DataType::FixedSizeBinary(5)),
5629 );
5630 let cast_array = cast(&a1, &cast_type).unwrap();
5631 assert_eq!(cast_array.data_type(), &cast_type);
5632 assert_eq!(
5633 array_to_strings(&cast_array),
5634 vec![
5635 "68656c6c6f",
5636 "6869696969",
5637 "6869696969",
5638 "null",
5639 "7275737474"
5640 ]
5641 );
5642 let dict_array = cast_array
5644 .as_any()
5645 .downcast_ref::<DictionaryArray<Int8Type>>()
5646 .unwrap();
5647 assert_eq!(dict_array.values().len(), 3);
5648 }
5649
5650 #[test]
5651 fn test_numeric_to_binary() {
5652 let a = Int16Array::from(vec![Some(1), Some(511), None]);
5653
5654 let array_ref = cast(&a, &DataType::Binary).unwrap();
5655 let down_cast = array_ref.as_binary::<i32>();
5656 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5657 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5658 assert!(down_cast.is_null(2));
5659
5660 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5661
5662 let array_ref = cast(&a, &DataType::Binary).unwrap();
5663 let down_cast = array_ref.as_binary::<i32>();
5664 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5665 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5666 assert!(down_cast.is_null(2));
5667 }
5668
5669 #[test]
5670 fn test_numeric_to_large_binary() {
5671 let a = Int16Array::from(vec![Some(1), Some(511), None]);
5672
5673 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5674 let down_cast = array_ref.as_binary::<i64>();
5675 assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5676 assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5677 assert!(down_cast.is_null(2));
5678
5679 let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5680
5681 let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5682 let down_cast = array_ref.as_binary::<i64>();
5683 assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5684 assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5685 assert!(down_cast.is_null(2));
5686 }
5687
5688 #[test]
5689 fn test_cast_date32_to_int32() {
5690 let array = Date32Array::from(vec![10000, 17890]);
5691 let b = cast(&array, &DataType::Int32).unwrap();
5692 let c = b.as_primitive::<Int32Type>();
5693 assert_eq!(10000, c.value(0));
5694 assert_eq!(17890, c.value(1));
5695 }
5696
5697 #[test]
5698 fn test_cast_int32_to_date32() {
5699 let array = Int32Array::from(vec![10000, 17890]);
5700 let b = cast(&array, &DataType::Date32).unwrap();
5701 let c = b.as_primitive::<Date32Type>();
5702 assert_eq!(10000, c.value(0));
5703 assert_eq!(17890, c.value(1));
5704 }
5705
5706 #[test]
5707 fn test_cast_timestamp_to_date32() {
5708 let array =
5709 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5710 .with_timezone("+00:00".to_string());
5711 let b = cast(&array, &DataType::Date32).unwrap();
5712 let c = b.as_primitive::<Date32Type>();
5713 assert_eq!(10000, c.value(0));
5714 assert_eq!(17890, c.value(1));
5715 assert!(c.is_null(2));
5716 }
5717 #[test]
5718 fn test_cast_timestamp_to_date32_zone() {
5719 let strings = StringArray::from_iter([
5720 Some("1970-01-01T00:00:01"),
5721 Some("1970-01-01T23:59:59"),
5722 None,
5723 Some("2020-03-01T02:00:23+00:00"),
5724 ]);
5725 let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
5726 let timestamps = cast(&strings, &dt).unwrap();
5727 let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
5728
5729 let c = dates.as_primitive::<Date32Type>();
5730 let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
5731 assert_eq!(c.value_as_date(0).unwrap(), expected);
5732 assert_eq!(c.value_as_date(1).unwrap(), expected);
5733 assert!(c.is_null(2));
5734 let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
5735 assert_eq!(c.value_as_date(3).unwrap(), expected);
5736 }
5737 #[test]
5738 fn test_cast_timestamp_to_date64() {
5739 let array =
5740 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
5741 let b = cast(&array, &DataType::Date64).unwrap();
5742 let c = b.as_primitive::<Date64Type>();
5743 assert_eq!(864000000005, c.value(0));
5744 assert_eq!(1545696000001, c.value(1));
5745 assert!(c.is_null(2));
5746
5747 let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
5748 let b = cast(&array, &DataType::Date64).unwrap();
5749 let c = b.as_primitive::<Date64Type>();
5750 assert_eq!(864000000005000, c.value(0));
5751 assert_eq!(1545696000001000, c.value(1));
5752
5753 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5755 let b = cast(&array, &DataType::Date64).unwrap();
5756 assert!(b.is_null(0));
5757 let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5759 let options = CastOptions {
5760 safe: false,
5761 format_options: FormatOptions::default(),
5762 };
5763 let b = cast_with_options(&array, &DataType::Date64, &options);
5764 assert!(b.is_err());
5765 }
5766
5767 #[test]
5768 fn test_cast_timestamp_to_time64() {
5769 let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5771 .with_timezone("+01:00".to_string());
5772 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5773 let c = b.as_primitive::<Time64MicrosecondType>();
5774 assert_eq!(3605000000, c.value(0));
5775 assert_eq!(3601000000, c.value(1));
5776 assert!(c.is_null(2));
5777 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5778 let c = b.as_primitive::<Time64NanosecondType>();
5779 assert_eq!(3605000000000, c.value(0));
5780 assert_eq!(3601000000000, c.value(1));
5781 assert!(c.is_null(2));
5782
5783 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5785 .with_timezone("+01:00".to_string());
5786 let array = Arc::new(a) as ArrayRef;
5787 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5788 let c = b.as_primitive::<Time64MicrosecondType>();
5789 assert_eq!(3605000000, c.value(0));
5790 assert_eq!(3601000000, c.value(1));
5791 assert!(c.is_null(2));
5792 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5793 let c = b.as_primitive::<Time64NanosecondType>();
5794 assert_eq!(3605000000000, c.value(0));
5795 assert_eq!(3601000000000, c.value(1));
5796 assert!(c.is_null(2));
5797
5798 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5800 .with_timezone("+01:00".to_string());
5801 let array = Arc::new(a) as ArrayRef;
5802 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5803 let c = b.as_primitive::<Time64MicrosecondType>();
5804 assert_eq!(3605000000, c.value(0));
5805 assert_eq!(3601000000, c.value(1));
5806 assert!(c.is_null(2));
5807 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5808 let c = b.as_primitive::<Time64NanosecondType>();
5809 assert_eq!(3605000000000, c.value(0));
5810 assert_eq!(3601000000000, c.value(1));
5811 assert!(c.is_null(2));
5812
5813 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5815 .with_timezone("+01:00".to_string());
5816 let array = Arc::new(a) as ArrayRef;
5817 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5818 let c = b.as_primitive::<Time64MicrosecondType>();
5819 assert_eq!(3605000000, c.value(0));
5820 assert_eq!(3601000000, c.value(1));
5821 assert!(c.is_null(2));
5822 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5823 let c = b.as_primitive::<Time64NanosecondType>();
5824 assert_eq!(3605000000000, c.value(0));
5825 assert_eq!(3601000000000, c.value(1));
5826 assert!(c.is_null(2));
5827
5828 let a =
5830 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5831 let array = Arc::new(a) as ArrayRef;
5832 let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5833 assert!(b.is_err());
5834 let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5835 assert!(b.is_err());
5836 let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5837 assert!(b.is_err());
5838 }
5839
5840 #[test]
5841 fn test_cast_timestamp_to_time32() {
5842 let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5844 .with_timezone("+01:00".to_string());
5845 let array = Arc::new(a) as ArrayRef;
5846 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5847 let c = b.as_primitive::<Time32SecondType>();
5848 assert_eq!(3605, c.value(0));
5849 assert_eq!(3601, c.value(1));
5850 assert!(c.is_null(2));
5851 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5852 let c = b.as_primitive::<Time32MillisecondType>();
5853 assert_eq!(3605000, c.value(0));
5854 assert_eq!(3601000, c.value(1));
5855 assert!(c.is_null(2));
5856
5857 let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5859 .with_timezone("+01:00".to_string());
5860 let array = Arc::new(a) as ArrayRef;
5861 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5862 let c = b.as_primitive::<Time32SecondType>();
5863 assert_eq!(3605, c.value(0));
5864 assert_eq!(3601, c.value(1));
5865 assert!(c.is_null(2));
5866 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5867 let c = b.as_primitive::<Time32MillisecondType>();
5868 assert_eq!(3605000, c.value(0));
5869 assert_eq!(3601000, c.value(1));
5870 assert!(c.is_null(2));
5871
5872 let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5874 .with_timezone("+01:00".to_string());
5875 let array = Arc::new(a) as ArrayRef;
5876 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5877 let c = b.as_primitive::<Time32SecondType>();
5878 assert_eq!(3605, c.value(0));
5879 assert_eq!(3601, c.value(1));
5880 assert!(c.is_null(2));
5881 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5882 let c = b.as_primitive::<Time32MillisecondType>();
5883 assert_eq!(3605000, c.value(0));
5884 assert_eq!(3601000, c.value(1));
5885 assert!(c.is_null(2));
5886
5887 let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5889 .with_timezone("+01:00".to_string());
5890 let array = Arc::new(a) as ArrayRef;
5891 let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5892 let c = b.as_primitive::<Time32SecondType>();
5893 assert_eq!(3605, c.value(0));
5894 assert_eq!(3601, c.value(1));
5895 assert!(c.is_null(2));
5896 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5897 let c = b.as_primitive::<Time32MillisecondType>();
5898 assert_eq!(3605000, c.value(0));
5899 assert_eq!(3601000, c.value(1));
5900 assert!(c.is_null(2));
5901
5902 let a =
5904 TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5905 let array = Arc::new(a) as ArrayRef;
5906 let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5907 assert!(b.is_err());
5908 let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5909 assert!(b.is_err());
5910 }
5911
5912 #[test]
5914 fn test_cast_timestamp_with_timezone_1() {
5915 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5916 Some("2000-01-01T00:00:00.123456789"),
5917 Some("2010-01-01T00:00:00.123456789"),
5918 None,
5919 ]));
5920 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5921 let timestamp_array = cast(&string_array, &to_type).unwrap();
5922
5923 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5924 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
5925
5926 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5927 let result = string_array.as_string::<i32>();
5928 assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
5929 assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5930 assert!(result.is_null(2));
5931 }
5932
5933 #[test]
5935 fn test_cast_timestamp_with_timezone_2() {
5936 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5937 Some("2000-01-01T07:00:00.123456789"),
5938 Some("2010-01-01T07:00:00.123456789"),
5939 None,
5940 ]));
5941 let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
5942 let timestamp_array = cast(&string_array, &to_type).unwrap();
5943
5944 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5946 let result = string_array.as_string::<i32>();
5947 assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
5948 assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5949 assert!(result.is_null(2));
5950
5951 let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5952 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
5953
5954 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5955 let result = string_array.as_string::<i32>();
5956 assert_eq!("2000-01-01T00:00:00.123", result.value(0));
5957 assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5958 assert!(result.is_null(2));
5959 }
5960
5961 #[test]
5963 fn test_cast_timestamp_with_timezone_3() {
5964 let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5965 Some("2000-01-01T07:00:00.123456789"),
5966 Some("2010-01-01T07:00:00.123456789"),
5967 None,
5968 ]));
5969 let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5970 let timestamp_array = cast(&string_array, &to_type).unwrap();
5971
5972 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5974 let result = string_array.as_string::<i32>();
5975 assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5976 assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5977 assert!(result.is_null(2));
5978
5979 let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5980 let timestamp_array = cast(×tamp_array, &to_type).unwrap();
5981
5982 let string_array = cast(×tamp_array, &DataType::Utf8).unwrap();
5983 let result = string_array.as_string::<i32>();
5984 assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5985 assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5986 assert!(result.is_null(2));
5987 }
5988
5989 #[test]
5990 fn test_cast_date64_to_timestamp() {
5991 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5992 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5993 let c = b.as_primitive::<TimestampSecondType>();
5994 assert_eq!(864000000, c.value(0));
5995 assert_eq!(1545696000, c.value(1));
5996 assert!(c.is_null(2));
5997 }
5998
5999 #[test]
6000 fn test_cast_date64_to_timestamp_ms() {
6001 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6002 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6003 let c = b
6004 .as_any()
6005 .downcast_ref::<TimestampMillisecondArray>()
6006 .unwrap();
6007 assert_eq!(864000000005, c.value(0));
6008 assert_eq!(1545696000001, c.value(1));
6009 assert!(c.is_null(2));
6010 }
6011
6012 #[test]
6013 fn test_cast_date64_to_timestamp_us() {
6014 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6015 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6016 let c = b
6017 .as_any()
6018 .downcast_ref::<TimestampMicrosecondArray>()
6019 .unwrap();
6020 assert_eq!(864000000005000, c.value(0));
6021 assert_eq!(1545696000001000, c.value(1));
6022 assert!(c.is_null(2));
6023 }
6024
6025 #[test]
6026 fn test_cast_date64_to_timestamp_ns() {
6027 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6028 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6029 let c = b
6030 .as_any()
6031 .downcast_ref::<TimestampNanosecondArray>()
6032 .unwrap();
6033 assert_eq!(864000000005000000, c.value(0));
6034 assert_eq!(1545696000001000000, c.value(1));
6035 assert!(c.is_null(2));
6036 }
6037
6038 #[test]
6039 fn test_cast_timestamp_to_i64() {
6040 let array =
6041 TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6042 .with_timezone("UTC".to_string());
6043 let b = cast(&array, &DataType::Int64).unwrap();
6044 let c = b.as_primitive::<Int64Type>();
6045 assert_eq!(&DataType::Int64, c.data_type());
6046 assert_eq!(864000000005, c.value(0));
6047 assert_eq!(1545696000001, c.value(1));
6048 assert!(c.is_null(2));
6049 }
6050
6051 macro_rules! assert_cast {
6052 ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6053 assert!(can_cast_types($array.data_type(), &$datatype));
6054 let out = cast(&$array, &$datatype).unwrap();
6055 let actual = out
6056 .as_any()
6057 .downcast_ref::<$output_array_type>()
6058 .unwrap()
6059 .into_iter()
6060 .collect::<Vec<_>>();
6061 assert_eq!(actual, $expected);
6062 }};
6063 ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6064 assert!(can_cast_types($array.data_type(), &$datatype));
6065 let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6066 let actual = out
6067 .as_any()
6068 .downcast_ref::<$output_array_type>()
6069 .unwrap()
6070 .into_iter()
6071 .collect::<Vec<_>>();
6072 assert_eq!(actual, $expected);
6073 }};
6074 }
6075
6076 #[test]
6077 fn test_cast_date32_to_string() {
6078 let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6079 let expected = vec![
6080 Some("1970-01-01"),
6081 Some("1997-05-19"),
6082 Some("2005-09-10"),
6083 Some("2018-12-25"),
6084 None,
6085 ];
6086
6087 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6088 assert_cast!(array, DataType::Utf8, StringArray, expected);
6089 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6090 }
6091
6092 #[test]
6093 fn test_cast_date64_to_string() {
6094 let array = Date64Array::from(vec![
6095 Some(0),
6096 Some(10000 * 86400000),
6097 Some(13036 * 86400000),
6098 Some(17890 * 86400000),
6099 None,
6100 ]);
6101 let expected = vec![
6102 Some("1970-01-01T00:00:00"),
6103 Some("1997-05-19T00:00:00"),
6104 Some("2005-09-10T00:00:00"),
6105 Some("2018-12-25T00:00:00"),
6106 None,
6107 ];
6108
6109 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6110 assert_cast!(array, DataType::Utf8, StringArray, expected);
6111 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6112 }
6113
6114 #[test]
6115 fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6116 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), None, None]); let array = Arc::new(a) as ArrayRef;
6119
6120 let b = cast(
6121 &array,
6122 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6123 )
6124 .unwrap();
6125 let c = b.as_primitive::<TimestampSecondType>();
6126 let string_array = cast(&c, &DataType::Utf8).unwrap();
6127 let result = string_array.as_string::<i32>();
6128 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6129
6130 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6131 let c = b.as_primitive::<TimestampSecondType>();
6132 let string_array = cast(&c, &DataType::Utf8).unwrap();
6133 let result = string_array.as_string::<i32>();
6134 assert_eq!("2021-01-01T00:00:00", result.value(0));
6135 }
6136
6137 #[test]
6138 fn test_cast_date32_to_timestamp_with_timezone() {
6139 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6142 let b = cast(
6143 &array,
6144 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6145 )
6146 .unwrap();
6147 let c = b.as_primitive::<TimestampSecondType>();
6148 assert_eq!(1609438500, c.value(0));
6149 assert_eq!(1640974500, c.value(1));
6150 assert!(c.is_null(2));
6151
6152 let string_array = cast(&c, &DataType::Utf8).unwrap();
6153 let result = string_array.as_string::<i32>();
6154 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6155 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6156 }
6157
6158 #[test]
6159 fn test_cast_date32_to_timestamp_with_timezone_ms() {
6160 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6163 let b = cast(
6164 &array,
6165 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6166 )
6167 .unwrap();
6168 let c = b.as_primitive::<TimestampMillisecondType>();
6169 assert_eq!(1609438500000, c.value(0));
6170 assert_eq!(1640974500000, c.value(1));
6171 assert!(c.is_null(2));
6172
6173 let string_array = cast(&c, &DataType::Utf8).unwrap();
6174 let result = string_array.as_string::<i32>();
6175 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6176 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6177 }
6178
6179 #[test]
6180 fn test_cast_date32_to_timestamp_with_timezone_us() {
6181 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6184 let b = cast(
6185 &array,
6186 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6187 )
6188 .unwrap();
6189 let c = b.as_primitive::<TimestampMicrosecondType>();
6190 assert_eq!(1609438500000000, c.value(0));
6191 assert_eq!(1640974500000000, c.value(1));
6192 assert!(c.is_null(2));
6193
6194 let string_array = cast(&c, &DataType::Utf8).unwrap();
6195 let result = string_array.as_string::<i32>();
6196 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6197 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6198 }
6199
6200 #[test]
6201 fn test_cast_date32_to_timestamp_with_timezone_ns() {
6202 let tz = "+0545"; let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
6205 let b = cast(
6206 &array,
6207 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6208 )
6209 .unwrap();
6210 let c = b.as_primitive::<TimestampNanosecondType>();
6211 assert_eq!(1609438500000000000, c.value(0));
6212 assert_eq!(1640974500000000000, c.value(1));
6213 assert!(c.is_null(2));
6214
6215 let string_array = cast(&c, &DataType::Utf8).unwrap();
6216 let result = string_array.as_string::<i32>();
6217 assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6218 assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6219 }
6220
6221 #[test]
6222 fn test_cast_date64_to_timestamp_with_timezone() {
6223 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6224 let tz = "+0545"; let b = cast(
6226 &array,
6227 &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6228 )
6229 .unwrap();
6230
6231 let c = b.as_primitive::<TimestampSecondType>();
6232 assert_eq!(863979300, c.value(0));
6233 assert_eq!(1545675300, c.value(1));
6234 assert!(c.is_null(2));
6235
6236 let string_array = cast(&c, &DataType::Utf8).unwrap();
6237 let result = string_array.as_string::<i32>();
6238 assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
6239 assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6240 }
6241
6242 #[test]
6243 fn test_cast_date64_to_timestamp_with_timezone_ms() {
6244 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6245 let tz = "+0545"; let b = cast(
6247 &array,
6248 &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6249 )
6250 .unwrap();
6251
6252 let c = b.as_primitive::<TimestampMillisecondType>();
6253 assert_eq!(863979300005, c.value(0));
6254 assert_eq!(1545675300001, c.value(1));
6255 assert!(c.is_null(2));
6256
6257 let string_array = cast(&c, &DataType::Utf8).unwrap();
6258 let result = string_array.as_string::<i32>();
6259 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6260 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6261 }
6262
6263 #[test]
6264 fn test_cast_date64_to_timestamp_with_timezone_us() {
6265 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6266 let tz = "+0545"; let b = cast(
6268 &array,
6269 &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6270 )
6271 .unwrap();
6272
6273 let c = b.as_primitive::<TimestampMicrosecondType>();
6274 assert_eq!(863979300005000, c.value(0));
6275 assert_eq!(1545675300001000, c.value(1));
6276 assert!(c.is_null(2));
6277
6278 let string_array = cast(&c, &DataType::Utf8).unwrap();
6279 let result = string_array.as_string::<i32>();
6280 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6281 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6282 }
6283
6284 #[test]
6285 fn test_cast_date64_to_timestamp_with_timezone_ns() {
6286 let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6287 let tz = "+0545"; let b = cast(
6289 &array,
6290 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6291 )
6292 .unwrap();
6293
6294 let c = b.as_primitive::<TimestampNanosecondType>();
6295 assert_eq!(863979300005000000, c.value(0));
6296 assert_eq!(1545675300001000000, c.value(1));
6297 assert!(c.is_null(2));
6298
6299 let string_array = cast(&c, &DataType::Utf8).unwrap();
6300 let result = string_array.as_string::<i32>();
6301 assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6302 assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6303 }
6304
6305 #[test]
6306 fn test_cast_timestamp_to_strings() {
6307 let array =
6309 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6310 let expected = vec![
6311 Some("1997-05-19T00:00:03.005"),
6312 Some("2018-12-25T00:00:02.001"),
6313 None,
6314 ];
6315
6316 assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6317 assert_cast!(array, DataType::Utf8, StringArray, expected);
6318 assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6319 }
6320
6321 #[test]
6322 fn test_cast_timestamp_to_strings_opt() {
6323 let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6324 let tz = "+0545"; let cast_options = CastOptions {
6326 safe: true,
6327 format_options: FormatOptions::default()
6328 .with_timestamp_format(Some(ts_format))
6329 .with_timestamp_tz_format(Some(ts_format)),
6330 };
6331
6332 let array_without_tz =
6334 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6335 let expected = vec![
6336 Some("1997-05-19 00:00:03.005000"),
6337 Some("2018-12-25 00:00:02.001000"),
6338 None,
6339 ];
6340 assert_cast!(
6341 array_without_tz,
6342 DataType::Utf8View,
6343 StringViewArray,
6344 cast_options,
6345 expected
6346 );
6347 assert_cast!(
6348 array_without_tz,
6349 DataType::Utf8,
6350 StringArray,
6351 cast_options,
6352 expected
6353 );
6354 assert_cast!(
6355 array_without_tz,
6356 DataType::LargeUtf8,
6357 LargeStringArray,
6358 cast_options,
6359 expected
6360 );
6361
6362 let array_with_tz =
6363 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6364 .with_timezone(tz.to_string());
6365 let expected = vec![
6366 Some("1997-05-19 05:45:03.005000"),
6367 Some("2018-12-25 05:45:02.001000"),
6368 None,
6369 ];
6370 assert_cast!(
6371 array_with_tz,
6372 DataType::Utf8View,
6373 StringViewArray,
6374 cast_options,
6375 expected
6376 );
6377 assert_cast!(
6378 array_with_tz,
6379 DataType::Utf8,
6380 StringArray,
6381 cast_options,
6382 expected
6383 );
6384 assert_cast!(
6385 array_with_tz,
6386 DataType::LargeUtf8,
6387 LargeStringArray,
6388 cast_options,
6389 expected
6390 );
6391 }
6392
6393 #[test]
6394 fn test_cast_between_timestamps() {
6395 let array =
6396 TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6397 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6398 let c = b.as_primitive::<TimestampSecondType>();
6399 assert_eq!(864000003, c.value(0));
6400 assert_eq!(1545696002, c.value(1));
6401 assert!(c.is_null(2));
6402 }
6403
6404 #[test]
6405 fn test_cast_duration_to_i64() {
6406 let base = vec![5, 6, 7, 8, 100000000];
6407
6408 let duration_arrays = vec![
6409 Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6410 Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6411 Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6412 Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6413 ];
6414
6415 for arr in duration_arrays {
6416 assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6417 let result = cast(&arr, &DataType::Int64).unwrap();
6418 let result = result.as_primitive::<Int64Type>();
6419 assert_eq!(base.as_slice(), result.values());
6420 }
6421 }
6422
6423 #[test]
6424 fn test_cast_between_durations_and_numerics() {
6425 fn test_cast_between_durations<FromType, ToType>()
6426 where
6427 FromType: ArrowPrimitiveType<Native = i64>,
6428 ToType: ArrowPrimitiveType<Native = i64>,
6429 PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6430 {
6431 let from_unit = match FromType::DATA_TYPE {
6432 DataType::Duration(unit) => unit,
6433 _ => panic!("Expected a duration type"),
6434 };
6435 let to_unit = match ToType::DATA_TYPE {
6436 DataType::Duration(unit) => unit,
6437 _ => panic!("Expected a duration type"),
6438 };
6439 let from_size = time_unit_multiple(&from_unit);
6440 let to_size = time_unit_multiple(&to_unit);
6441
6442 let (v1_before, v2_before) = (8640003005, 1696002001);
6443 let (v1_after, v2_after) = if from_size >= to_size {
6444 (
6445 v1_before / (from_size / to_size),
6446 v2_before / (from_size / to_size),
6447 )
6448 } else {
6449 (
6450 v1_before * (to_size / from_size),
6451 v2_before * (to_size / from_size),
6452 )
6453 };
6454
6455 let array =
6456 PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6457 let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6458 let c = b.as_primitive::<ToType>();
6459 assert_eq!(v1_after, c.value(0));
6460 assert_eq!(v2_after, c.value(1));
6461 assert!(c.is_null(2));
6462 }
6463
6464 test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6466 test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6467 test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6468 test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6469 test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6470 test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6471 test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6472 test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6473 test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6474 test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6475 test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6476 test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6477
6478 let array = DurationSecondArray::from(vec![
6480 Some(i64::MAX),
6481 Some(8640203410378005),
6482 Some(10241096),
6483 None,
6484 ]);
6485 let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6486 let c = b.as_primitive::<DurationNanosecondType>();
6487 assert!(c.is_null(0));
6488 assert!(c.is_null(1));
6489 assert_eq!(10241096000000000, c.value(2));
6490 assert!(c.is_null(3));
6491
6492 let array = DurationSecondArray::from(vec![
6494 Some(i64::MAX),
6495 Some(8640203410378005),
6496 Some(10241096),
6497 None,
6498 ]);
6499 let b = cast(&array, &DataType::Int64).unwrap();
6500 let c = b.as_primitive::<Int64Type>();
6501 assert_eq!(i64::MAX, c.value(0));
6502 assert_eq!(8640203410378005, c.value(1));
6503 assert_eq!(10241096, c.value(2));
6504 assert!(c.is_null(3));
6505
6506 let b = cast(&array, &DataType::Int32).unwrap();
6507 let c = b.as_primitive::<Int32Type>();
6508 assert_eq!(0, c.value(0));
6509 assert_eq!(0, c.value(1));
6510 assert_eq!(10241096, c.value(2));
6511 assert!(c.is_null(3));
6512
6513 let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6515 let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6516 let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6517 assert_eq!(i32::MAX as i64, c.value(0));
6518 assert_eq!(802034103, c.value(1));
6519 assert_eq!(10241096, c.value(2));
6520 assert!(c.is_null(3));
6521 }
6522
6523 #[test]
6524 fn test_cast_to_strings() {
6525 let a = Int32Array::from(vec![1, 2, 3]);
6526 let out = cast(&a, &DataType::Utf8).unwrap();
6527 let out = out
6528 .as_any()
6529 .downcast_ref::<StringArray>()
6530 .unwrap()
6531 .into_iter()
6532 .collect::<Vec<_>>();
6533 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6534 let out = cast(&a, &DataType::LargeUtf8).unwrap();
6535 let out = out
6536 .as_any()
6537 .downcast_ref::<LargeStringArray>()
6538 .unwrap()
6539 .into_iter()
6540 .collect::<Vec<_>>();
6541 assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6542 }
6543
6544 #[test]
6545 fn test_str_to_str_casts() {
6546 for data in [
6547 vec![Some("foo"), Some("bar"), Some("ham")],
6548 vec![Some("foo"), None, Some("bar")],
6549 ] {
6550 let a = LargeStringArray::from(data.clone());
6551 let to = cast(&a, &DataType::Utf8).unwrap();
6552 let expect = a
6553 .as_any()
6554 .downcast_ref::<LargeStringArray>()
6555 .unwrap()
6556 .into_iter()
6557 .collect::<Vec<_>>();
6558 let out = to
6559 .as_any()
6560 .downcast_ref::<StringArray>()
6561 .unwrap()
6562 .into_iter()
6563 .collect::<Vec<_>>();
6564 assert_eq!(expect, out);
6565
6566 let a = StringArray::from(data);
6567 let to = cast(&a, &DataType::LargeUtf8).unwrap();
6568 let expect = a
6569 .as_any()
6570 .downcast_ref::<StringArray>()
6571 .unwrap()
6572 .into_iter()
6573 .collect::<Vec<_>>();
6574 let out = to
6575 .as_any()
6576 .downcast_ref::<LargeStringArray>()
6577 .unwrap()
6578 .into_iter()
6579 .collect::<Vec<_>>();
6580 assert_eq!(expect, out);
6581 }
6582 }
6583
6584 const VIEW_TEST_DATA: [Option<&str>; 5] = [
6585 Some("hello"),
6586 Some("repeated"),
6587 None,
6588 Some("large payload over 12 bytes"),
6589 Some("repeated"),
6590 ];
6591
6592 #[test]
6593 fn test_string_view_to_binary_view() {
6594 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6595
6596 assert!(can_cast_types(
6597 string_view_array.data_type(),
6598 &DataType::BinaryView
6599 ));
6600
6601 let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
6602 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6603
6604 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6605 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6606 }
6607
6608 #[test]
6609 fn test_binary_view_to_string_view() {
6610 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6611
6612 assert!(can_cast_types(
6613 binary_view_array.data_type(),
6614 &DataType::Utf8View
6615 ));
6616
6617 let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
6618 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6619
6620 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6621 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6622 }
6623
6624 #[test]
6625 fn test_binary_view_to_string_view_with_invalid_utf8() {
6626 let binary_view_array = BinaryViewArray::from_iter(vec![
6627 Some("valid".as_bytes()),
6628 Some(&[0xff]),
6629 Some("utf8".as_bytes()),
6630 None,
6631 ]);
6632
6633 let strict_options = CastOptions {
6634 safe: false,
6635 ..Default::default()
6636 };
6637
6638 assert!(
6639 cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
6640 );
6641
6642 let safe_options = CastOptions {
6643 safe: true,
6644 ..Default::default()
6645 };
6646
6647 let string_view_array =
6648 cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
6649 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6650
6651 let values: Vec<_> = string_view_array.as_string_view().iter().collect();
6652
6653 assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
6654 }
6655
6656 #[test]
6657 fn test_string_to_view() {
6658 _test_string_to_view::<i32>();
6659 _test_string_to_view::<i64>();
6660 }
6661
6662 fn _test_string_to_view<O>()
6663 where
6664 O: OffsetSizeTrait,
6665 {
6666 let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6667
6668 assert!(can_cast_types(
6669 string_array.data_type(),
6670 &DataType::Utf8View
6671 ));
6672
6673 assert!(can_cast_types(
6674 string_array.data_type(),
6675 &DataType::BinaryView
6676 ));
6677
6678 let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
6679 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6680
6681 let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
6682 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6683
6684 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6685 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6686
6687 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6688 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6689 }
6690
6691 #[test]
6692 fn test_bianry_to_view() {
6693 _test_binary_to_view::<i32>();
6694 _test_binary_to_view::<i64>();
6695 }
6696
6697 fn _test_binary_to_view<O>()
6698 where
6699 O: OffsetSizeTrait,
6700 {
6701 let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6702
6703 assert!(can_cast_types(
6704 binary_array.data_type(),
6705 &DataType::Utf8View
6706 ));
6707
6708 assert!(can_cast_types(
6709 binary_array.data_type(),
6710 &DataType::BinaryView
6711 ));
6712
6713 let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
6714 assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6715
6716 let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
6717 assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6718
6719 let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6720 assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6721
6722 let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6723 assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6724 }
6725
6726 #[test]
6727 fn test_dict_to_view() {
6728 let values = StringArray::from_iter(VIEW_TEST_DATA);
6729 let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
6730 let string_dict_array =
6731 DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
6732 let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
6733
6734 let string_view_array = {
6735 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for v in typed_dict.into_iter() {
6737 builder.append_option(v);
6738 }
6739 builder.finish()
6740 };
6741 let expected_string_array_type = string_view_array.data_type();
6742 let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
6743 assert_eq!(casted_string_array.data_type(), expected_string_array_type);
6744 assert_eq!(casted_string_array.as_ref(), &string_view_array);
6745
6746 let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
6747 let binary_dict_array =
6748 DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
6749 let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
6750
6751 let binary_view_array = {
6752 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for v in typed_binary_dict.into_iter() {
6754 builder.append_option(v);
6755 }
6756 builder.finish()
6757 };
6758 let expected_binary_array_type = binary_view_array.data_type();
6759 let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
6760 assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
6761 assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
6762 }
6763
6764 #[test]
6765 fn test_view_to_dict() {
6766 let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6767 let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
6768 let casted_type = string_dict_array.data_type();
6769 let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
6770 assert_eq!(casted_dict_array.data_type(), casted_type);
6771 assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
6772
6773 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6774 let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
6775 let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
6776 let binary_dict_array =
6777 DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
6778 let casted_type = binary_dict_array.data_type();
6779 let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
6780 assert_eq!(casted_binary_array.data_type(), casted_type);
6781 assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
6782 }
6783
6784 #[test]
6785 fn test_view_to_string() {
6786 _test_view_to_string::<i32>();
6787 _test_view_to_string::<i64>();
6788 }
6789
6790 fn _test_view_to_string<O>()
6791 where
6792 O: OffsetSizeTrait,
6793 {
6794 let string_view_array = {
6795 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); for s in VIEW_TEST_DATA.iter() {
6797 builder.append_option(*s);
6798 }
6799 builder.finish()
6800 };
6801
6802 let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6803
6804 let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6805 let expected_type = expected_string_array.data_type();
6806
6807 assert!(can_cast_types(string_view_array.data_type(), expected_type));
6808 assert!(can_cast_types(binary_view_array.data_type(), expected_type));
6809
6810 let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
6811 assert_eq!(string_view_casted_array.data_type(), expected_type);
6812 assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
6813
6814 let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
6815 assert_eq!(binary_view_casted_array.data_type(), expected_type);
6816 assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
6817 }
6818
6819 #[test]
6820 fn test_view_to_binary() {
6821 _test_view_to_binary::<i32>();
6822 _test_view_to_binary::<i64>();
6823 }
6824
6825 fn _test_view_to_binary<O>()
6826 where
6827 O: OffsetSizeTrait,
6828 {
6829 let view_array = {
6830 let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); for s in VIEW_TEST_DATA.iter() {
6832 builder.append_option(*s);
6833 }
6834 builder.finish()
6835 };
6836
6837 let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6838 let expected_type = expected_binary_array.data_type();
6839
6840 assert!(can_cast_types(view_array.data_type(), expected_type));
6841
6842 let binary_array = cast(&view_array, expected_type).unwrap();
6843 assert_eq!(binary_array.data_type(), expected_type);
6844
6845 assert_eq!(binary_array.as_ref(), &expected_binary_array);
6846 }
6847
6848 #[test]
6849 fn test_cast_from_f64() {
6850 let f64_values: Vec<f64> = vec![
6851 i64::MIN as f64,
6852 i32::MIN as f64,
6853 i16::MIN as f64,
6854 i8::MIN as f64,
6855 0_f64,
6856 u8::MAX as f64,
6857 u16::MAX as f64,
6858 u32::MAX as f64,
6859 u64::MAX as f64,
6860 ];
6861 let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6862
6863 let f64_expected = vec![
6864 -9223372036854776000.0,
6865 -2147483648.0,
6866 -32768.0,
6867 -128.0,
6868 0.0,
6869 255.0,
6870 65535.0,
6871 4294967295.0,
6872 18446744073709552000.0,
6873 ];
6874 assert_eq!(
6875 f64_expected,
6876 get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6877 .iter()
6878 .map(|i| i.parse::<f64>().unwrap())
6879 .collect::<Vec<f64>>()
6880 );
6881
6882 let f32_expected = vec![
6883 -9223372000000000000.0,
6884 -2147483600.0,
6885 -32768.0,
6886 -128.0,
6887 0.0,
6888 255.0,
6889 65535.0,
6890 4294967300.0,
6891 18446744000000000000.0,
6892 ];
6893 assert_eq!(
6894 f32_expected,
6895 get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6896 .iter()
6897 .map(|i| i.parse::<f32>().unwrap())
6898 .collect::<Vec<f32>>()
6899 );
6900
6901 let f16_expected = vec![
6902 f16::from_f64(-9223372000000000000.0),
6903 f16::from_f64(-2147483600.0),
6904 f16::from_f64(-32768.0),
6905 f16::from_f64(-128.0),
6906 f16::from_f64(0.0),
6907 f16::from_f64(255.0),
6908 f16::from_f64(65535.0),
6909 f16::from_f64(4294967300.0),
6910 f16::from_f64(18446744000000000000.0),
6911 ];
6912 assert_eq!(
6913 f16_expected,
6914 get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6915 .iter()
6916 .map(|i| i.parse::<f16>().unwrap())
6917 .collect::<Vec<f16>>()
6918 );
6919
6920 let i64_expected = vec![
6921 "-9223372036854775808",
6922 "-2147483648",
6923 "-32768",
6924 "-128",
6925 "0",
6926 "255",
6927 "65535",
6928 "4294967295",
6929 "null",
6930 ];
6931 assert_eq!(
6932 i64_expected,
6933 get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
6934 );
6935
6936 let i32_expected = vec![
6937 "null",
6938 "-2147483648",
6939 "-32768",
6940 "-128",
6941 "0",
6942 "255",
6943 "65535",
6944 "null",
6945 "null",
6946 ];
6947 assert_eq!(
6948 i32_expected,
6949 get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
6950 );
6951
6952 let i16_expected = vec![
6953 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6954 ];
6955 assert_eq!(
6956 i16_expected,
6957 get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
6958 );
6959
6960 let i8_expected = vec![
6961 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6962 ];
6963 assert_eq!(
6964 i8_expected,
6965 get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
6966 );
6967
6968 let u64_expected = vec![
6969 "null",
6970 "null",
6971 "null",
6972 "null",
6973 "0",
6974 "255",
6975 "65535",
6976 "4294967295",
6977 "null",
6978 ];
6979 assert_eq!(
6980 u64_expected,
6981 get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
6982 );
6983
6984 let u32_expected = vec![
6985 "null",
6986 "null",
6987 "null",
6988 "null",
6989 "0",
6990 "255",
6991 "65535",
6992 "4294967295",
6993 "null",
6994 ];
6995 assert_eq!(
6996 u32_expected,
6997 get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
6998 );
6999
7000 let u16_expected = vec![
7001 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7002 ];
7003 assert_eq!(
7004 u16_expected,
7005 get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7006 );
7007
7008 let u8_expected = vec![
7009 "null", "null", "null", "null", "0", "255", "null", "null", "null",
7010 ];
7011 assert_eq!(
7012 u8_expected,
7013 get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7014 );
7015 }
7016
7017 #[test]
7018 fn test_cast_from_f32() {
7019 let f32_values: Vec<f32> = vec![
7020 i32::MIN as f32,
7021 i32::MIN as f32,
7022 i16::MIN as f32,
7023 i8::MIN as f32,
7024 0_f32,
7025 u8::MAX as f32,
7026 u16::MAX as f32,
7027 u32::MAX as f32,
7028 u32::MAX as f32,
7029 ];
7030 let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7031
7032 let f64_expected = vec![
7033 "-2147483648.0",
7034 "-2147483648.0",
7035 "-32768.0",
7036 "-128.0",
7037 "0.0",
7038 "255.0",
7039 "65535.0",
7040 "4294967296.0",
7041 "4294967296.0",
7042 ];
7043 assert_eq!(
7044 f64_expected,
7045 get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7046 );
7047
7048 let f32_expected = vec![
7049 "-2147483600.0",
7050 "-2147483600.0",
7051 "-32768.0",
7052 "-128.0",
7053 "0.0",
7054 "255.0",
7055 "65535.0",
7056 "4294967300.0",
7057 "4294967300.0",
7058 ];
7059 assert_eq!(
7060 f32_expected,
7061 get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7062 );
7063
7064 let f16_expected = vec![
7065 "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7066 ];
7067 assert_eq!(
7068 f16_expected,
7069 get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7070 );
7071
7072 let i64_expected = vec![
7073 "-2147483648",
7074 "-2147483648",
7075 "-32768",
7076 "-128",
7077 "0",
7078 "255",
7079 "65535",
7080 "4294967296",
7081 "4294967296",
7082 ];
7083 assert_eq!(
7084 i64_expected,
7085 get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7086 );
7087
7088 let i32_expected = vec![
7089 "-2147483648",
7090 "-2147483648",
7091 "-32768",
7092 "-128",
7093 "0",
7094 "255",
7095 "65535",
7096 "null",
7097 "null",
7098 ];
7099 assert_eq!(
7100 i32_expected,
7101 get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7102 );
7103
7104 let i16_expected = vec![
7105 "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7106 ];
7107 assert_eq!(
7108 i16_expected,
7109 get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7110 );
7111
7112 let i8_expected = vec![
7113 "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7114 ];
7115 assert_eq!(
7116 i8_expected,
7117 get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7118 );
7119
7120 let u64_expected = vec![
7121 "null",
7122 "null",
7123 "null",
7124 "null",
7125 "0",
7126 "255",
7127 "65535",
7128 "4294967296",
7129 "4294967296",
7130 ];
7131 assert_eq!(
7132 u64_expected,
7133 get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7134 );
7135
7136 let u32_expected = vec![
7137 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7138 ];
7139 assert_eq!(
7140 u32_expected,
7141 get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7142 );
7143
7144 let u16_expected = vec![
7145 "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7146 ];
7147 assert_eq!(
7148 u16_expected,
7149 get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7150 );
7151
7152 let u8_expected = vec![
7153 "null", "null", "null", "null", "0", "255", "null", "null", "null",
7154 ];
7155 assert_eq!(
7156 u8_expected,
7157 get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7158 );
7159 }
7160
7161 #[test]
7162 fn test_cast_from_uint64() {
7163 let u64_values: Vec<u64> = vec![
7164 0,
7165 u8::MAX as u64,
7166 u16::MAX as u64,
7167 u32::MAX as u64,
7168 u64::MAX,
7169 ];
7170 let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7171
7172 let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7173 assert_eq!(
7174 f64_expected,
7175 get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7176 .iter()
7177 .map(|i| i.parse::<f64>().unwrap())
7178 .collect::<Vec<f64>>()
7179 );
7180
7181 let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7182 assert_eq!(
7183 f32_expected,
7184 get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7185 .iter()
7186 .map(|i| i.parse::<f32>().unwrap())
7187 .collect::<Vec<f32>>()
7188 );
7189
7190 let f16_expected = vec![
7191 f16::from_f64(0.0),
7192 f16::from_f64(255.0),
7193 f16::from_f64(65535.0),
7194 f16::from_f64(4294967300.0),
7195 f16::from_f64(18446744000000000000.0),
7196 ];
7197 assert_eq!(
7198 f16_expected,
7199 get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7200 .iter()
7201 .map(|i| i.parse::<f16>().unwrap())
7202 .collect::<Vec<f16>>()
7203 );
7204
7205 let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7206 assert_eq!(
7207 i64_expected,
7208 get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7209 );
7210
7211 let i32_expected = vec!["0", "255", "65535", "null", "null"];
7212 assert_eq!(
7213 i32_expected,
7214 get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7215 );
7216
7217 let i16_expected = vec!["0", "255", "null", "null", "null"];
7218 assert_eq!(
7219 i16_expected,
7220 get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7221 );
7222
7223 let i8_expected = vec!["0", "null", "null", "null", "null"];
7224 assert_eq!(
7225 i8_expected,
7226 get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7227 );
7228
7229 let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7230 assert_eq!(
7231 u64_expected,
7232 get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7233 );
7234
7235 let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7236 assert_eq!(
7237 u32_expected,
7238 get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7239 );
7240
7241 let u16_expected = vec!["0", "255", "65535", "null", "null"];
7242 assert_eq!(
7243 u16_expected,
7244 get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7245 );
7246
7247 let u8_expected = vec!["0", "255", "null", "null", "null"];
7248 assert_eq!(
7249 u8_expected,
7250 get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7251 );
7252 }
7253
7254 #[test]
7255 fn test_cast_from_uint32() {
7256 let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7257 let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7258
7259 let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7260 assert_eq!(
7261 f64_expected,
7262 get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7263 );
7264
7265 let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7266 assert_eq!(
7267 f32_expected,
7268 get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7269 );
7270
7271 let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7272 assert_eq!(
7273 f16_expected,
7274 get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7275 );
7276
7277 let i64_expected = vec!["0", "255", "65535", "4294967295"];
7278 assert_eq!(
7279 i64_expected,
7280 get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7281 );
7282
7283 let i32_expected = vec!["0", "255", "65535", "null"];
7284 assert_eq!(
7285 i32_expected,
7286 get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7287 );
7288
7289 let i16_expected = vec!["0", "255", "null", "null"];
7290 assert_eq!(
7291 i16_expected,
7292 get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7293 );
7294
7295 let i8_expected = vec!["0", "null", "null", "null"];
7296 assert_eq!(
7297 i8_expected,
7298 get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7299 );
7300
7301 let u64_expected = vec!["0", "255", "65535", "4294967295"];
7302 assert_eq!(
7303 u64_expected,
7304 get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7305 );
7306
7307 let u32_expected = vec!["0", "255", "65535", "4294967295"];
7308 assert_eq!(
7309 u32_expected,
7310 get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7311 );
7312
7313 let u16_expected = vec!["0", "255", "65535", "null"];
7314 assert_eq!(
7315 u16_expected,
7316 get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7317 );
7318
7319 let u8_expected = vec!["0", "255", "null", "null"];
7320 assert_eq!(
7321 u8_expected,
7322 get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7323 );
7324 }
7325
7326 #[test]
7327 fn test_cast_from_uint16() {
7328 let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7329 let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7330
7331 let f64_expected = vec!["0.0", "255.0", "65535.0"];
7332 assert_eq!(
7333 f64_expected,
7334 get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7335 );
7336
7337 let f32_expected = vec!["0.0", "255.0", "65535.0"];
7338 assert_eq!(
7339 f32_expected,
7340 get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7341 );
7342
7343 let f16_expected = vec!["0.0", "255.0", "inf"];
7344 assert_eq!(
7345 f16_expected,
7346 get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7347 );
7348
7349 let i64_expected = vec!["0", "255", "65535"];
7350 assert_eq!(
7351 i64_expected,
7352 get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7353 );
7354
7355 let i32_expected = vec!["0", "255", "65535"];
7356 assert_eq!(
7357 i32_expected,
7358 get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7359 );
7360
7361 let i16_expected = vec!["0", "255", "null"];
7362 assert_eq!(
7363 i16_expected,
7364 get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7365 );
7366
7367 let i8_expected = vec!["0", "null", "null"];
7368 assert_eq!(
7369 i8_expected,
7370 get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7371 );
7372
7373 let u64_expected = vec!["0", "255", "65535"];
7374 assert_eq!(
7375 u64_expected,
7376 get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7377 );
7378
7379 let u32_expected = vec!["0", "255", "65535"];
7380 assert_eq!(
7381 u32_expected,
7382 get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7383 );
7384
7385 let u16_expected = vec!["0", "255", "65535"];
7386 assert_eq!(
7387 u16_expected,
7388 get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7389 );
7390
7391 let u8_expected = vec!["0", "255", "null"];
7392 assert_eq!(
7393 u8_expected,
7394 get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7395 );
7396 }
7397
7398 #[test]
7399 fn test_cast_from_uint8() {
7400 let u8_values: Vec<u8> = vec![0, u8::MAX];
7401 let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7402
7403 let f64_expected = vec!["0.0", "255.0"];
7404 assert_eq!(
7405 f64_expected,
7406 get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7407 );
7408
7409 let f32_expected = vec!["0.0", "255.0"];
7410 assert_eq!(
7411 f32_expected,
7412 get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7413 );
7414
7415 let f16_expected = vec!["0.0", "255.0"];
7416 assert_eq!(
7417 f16_expected,
7418 get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7419 );
7420
7421 let i64_expected = vec!["0", "255"];
7422 assert_eq!(
7423 i64_expected,
7424 get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7425 );
7426
7427 let i32_expected = vec!["0", "255"];
7428 assert_eq!(
7429 i32_expected,
7430 get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7431 );
7432
7433 let i16_expected = vec!["0", "255"];
7434 assert_eq!(
7435 i16_expected,
7436 get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7437 );
7438
7439 let i8_expected = vec!["0", "null"];
7440 assert_eq!(
7441 i8_expected,
7442 get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7443 );
7444
7445 let u64_expected = vec!["0", "255"];
7446 assert_eq!(
7447 u64_expected,
7448 get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7449 );
7450
7451 let u32_expected = vec!["0", "255"];
7452 assert_eq!(
7453 u32_expected,
7454 get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7455 );
7456
7457 let u16_expected = vec!["0", "255"];
7458 assert_eq!(
7459 u16_expected,
7460 get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7461 );
7462
7463 let u8_expected = vec!["0", "255"];
7464 assert_eq!(
7465 u8_expected,
7466 get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7467 );
7468 }
7469
7470 #[test]
7471 fn test_cast_from_int64() {
7472 let i64_values: Vec<i64> = vec![
7473 i64::MIN,
7474 i32::MIN as i64,
7475 i16::MIN as i64,
7476 i8::MIN as i64,
7477 0,
7478 i8::MAX as i64,
7479 i16::MAX as i64,
7480 i32::MAX as i64,
7481 i64::MAX,
7482 ];
7483 let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7484
7485 let f64_expected = vec![
7486 -9223372036854776000.0,
7487 -2147483648.0,
7488 -32768.0,
7489 -128.0,
7490 0.0,
7491 127.0,
7492 32767.0,
7493 2147483647.0,
7494 9223372036854776000.0,
7495 ];
7496 assert_eq!(
7497 f64_expected,
7498 get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7499 .iter()
7500 .map(|i| i.parse::<f64>().unwrap())
7501 .collect::<Vec<f64>>()
7502 );
7503
7504 let f32_expected = vec![
7505 -9223372000000000000.0,
7506 -2147483600.0,
7507 -32768.0,
7508 -128.0,
7509 0.0,
7510 127.0,
7511 32767.0,
7512 2147483600.0,
7513 9223372000000000000.0,
7514 ];
7515 assert_eq!(
7516 f32_expected,
7517 get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7518 .iter()
7519 .map(|i| i.parse::<f32>().unwrap())
7520 .collect::<Vec<f32>>()
7521 );
7522
7523 let f16_expected = vec![
7524 f16::from_f64(-9223372000000000000.0),
7525 f16::from_f64(-2147483600.0),
7526 f16::from_f64(-32768.0),
7527 f16::from_f64(-128.0),
7528 f16::from_f64(0.0),
7529 f16::from_f64(127.0),
7530 f16::from_f64(32767.0),
7531 f16::from_f64(2147483600.0),
7532 f16::from_f64(9223372000000000000.0),
7533 ];
7534 assert_eq!(
7535 f16_expected,
7536 get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7537 .iter()
7538 .map(|i| i.parse::<f16>().unwrap())
7539 .collect::<Vec<f16>>()
7540 );
7541
7542 let i64_expected = vec![
7543 "-9223372036854775808",
7544 "-2147483648",
7545 "-32768",
7546 "-128",
7547 "0",
7548 "127",
7549 "32767",
7550 "2147483647",
7551 "9223372036854775807",
7552 ];
7553 assert_eq!(
7554 i64_expected,
7555 get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
7556 );
7557
7558 let i32_expected = vec![
7559 "null",
7560 "-2147483648",
7561 "-32768",
7562 "-128",
7563 "0",
7564 "127",
7565 "32767",
7566 "2147483647",
7567 "null",
7568 ];
7569 assert_eq!(
7570 i32_expected,
7571 get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
7572 );
7573
7574 assert_eq!(
7575 i32_expected,
7576 get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
7577 );
7578
7579 let i16_expected = vec![
7580 "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
7581 ];
7582 assert_eq!(
7583 i16_expected,
7584 get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
7585 );
7586
7587 let i8_expected = vec![
7588 "null", "null", "null", "-128", "0", "127", "null", "null", "null",
7589 ];
7590 assert_eq!(
7591 i8_expected,
7592 get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
7593 );
7594
7595 let u64_expected = vec![
7596 "null",
7597 "null",
7598 "null",
7599 "null",
7600 "0",
7601 "127",
7602 "32767",
7603 "2147483647",
7604 "9223372036854775807",
7605 ];
7606 assert_eq!(
7607 u64_expected,
7608 get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
7609 );
7610
7611 let u32_expected = vec![
7612 "null",
7613 "null",
7614 "null",
7615 "null",
7616 "0",
7617 "127",
7618 "32767",
7619 "2147483647",
7620 "null",
7621 ];
7622 assert_eq!(
7623 u32_expected,
7624 get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
7625 );
7626
7627 let u16_expected = vec![
7628 "null", "null", "null", "null", "0", "127", "32767", "null", "null",
7629 ];
7630 assert_eq!(
7631 u16_expected,
7632 get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
7633 );
7634
7635 let u8_expected = vec![
7636 "null", "null", "null", "null", "0", "127", "null", "null", "null",
7637 ];
7638 assert_eq!(
7639 u8_expected,
7640 get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
7641 );
7642 }
7643
7644 #[test]
7645 fn test_cast_from_int32() {
7646 let i32_values: Vec<i32> = vec![
7647 i32::MIN,
7648 i16::MIN as i32,
7649 i8::MIN as i32,
7650 0,
7651 i8::MAX as i32,
7652 i16::MAX as i32,
7653 i32::MAX,
7654 ];
7655 let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
7656
7657 let f64_expected = vec![
7658 "-2147483648.0",
7659 "-32768.0",
7660 "-128.0",
7661 "0.0",
7662 "127.0",
7663 "32767.0",
7664 "2147483647.0",
7665 ];
7666 assert_eq!(
7667 f64_expected,
7668 get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
7669 );
7670
7671 let f32_expected = vec![
7672 "-2147483600.0",
7673 "-32768.0",
7674 "-128.0",
7675 "0.0",
7676 "127.0",
7677 "32767.0",
7678 "2147483600.0",
7679 ];
7680 assert_eq!(
7681 f32_expected,
7682 get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
7683 );
7684
7685 let f16_expected = vec![
7686 f16::from_f64(-2147483600.0),
7687 f16::from_f64(-32768.0),
7688 f16::from_f64(-128.0),
7689 f16::from_f64(0.0),
7690 f16::from_f64(127.0),
7691 f16::from_f64(32767.0),
7692 f16::from_f64(2147483600.0),
7693 ];
7694 assert_eq!(
7695 f16_expected,
7696 get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
7697 .iter()
7698 .map(|i| i.parse::<f16>().unwrap())
7699 .collect::<Vec<f16>>()
7700 );
7701
7702 let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
7703 assert_eq!(
7704 i16_expected,
7705 get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
7706 );
7707
7708 let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
7709 assert_eq!(
7710 i8_expected,
7711 get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
7712 );
7713
7714 let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7715 assert_eq!(
7716 u64_expected,
7717 get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
7718 );
7719
7720 let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7721 assert_eq!(
7722 u32_expected,
7723 get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
7724 );
7725
7726 let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
7727 assert_eq!(
7728 u16_expected,
7729 get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
7730 );
7731
7732 let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
7733 assert_eq!(
7734 u8_expected,
7735 get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
7736 );
7737
7738 let i64_expected = vec![
7740 "-185542587187200000",
7741 "-2831155200000",
7742 "-11059200000",
7743 "0",
7744 "10972800000",
7745 "2831068800000",
7746 "185542587100800000",
7747 ];
7748 assert_eq!(
7749 i64_expected,
7750 get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
7751 );
7752 }
7753
7754 #[test]
7755 fn test_cast_from_int16() {
7756 let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
7757 let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
7758
7759 let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7760 assert_eq!(
7761 f64_expected,
7762 get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
7763 );
7764
7765 let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7766 assert_eq!(
7767 f32_expected,
7768 get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
7769 );
7770
7771 let f16_expected = vec![
7772 f16::from_f64(-32768.0),
7773 f16::from_f64(-128.0),
7774 f16::from_f64(0.0),
7775 f16::from_f64(127.0),
7776 f16::from_f64(32767.0),
7777 ];
7778 assert_eq!(
7779 f16_expected,
7780 get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
7781 .iter()
7782 .map(|i| i.parse::<f16>().unwrap())
7783 .collect::<Vec<f16>>()
7784 );
7785
7786 let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
7787 assert_eq!(
7788 i64_expected,
7789 get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
7790 );
7791
7792 let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
7793 assert_eq!(
7794 i32_expected,
7795 get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
7796 );
7797
7798 let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
7799 assert_eq!(
7800 i16_expected,
7801 get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
7802 );
7803
7804 let i8_expected = vec!["null", "-128", "0", "127", "null"];
7805 assert_eq!(
7806 i8_expected,
7807 get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
7808 );
7809
7810 let u64_expected = vec!["null", "null", "0", "127", "32767"];
7811 assert_eq!(
7812 u64_expected,
7813 get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
7814 );
7815
7816 let u32_expected = vec!["null", "null", "0", "127", "32767"];
7817 assert_eq!(
7818 u32_expected,
7819 get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
7820 );
7821
7822 let u16_expected = vec!["null", "null", "0", "127", "32767"];
7823 assert_eq!(
7824 u16_expected,
7825 get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
7826 );
7827
7828 let u8_expected = vec!["null", "null", "0", "127", "null"];
7829 assert_eq!(
7830 u8_expected,
7831 get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
7832 );
7833 }
7834
7835 #[test]
7836 fn test_cast_from_date32() {
7837 let i32_values: Vec<i32> = vec![
7838 i32::MIN,
7839 i16::MIN as i32,
7840 i8::MIN as i32,
7841 0,
7842 i8::MAX as i32,
7843 i16::MAX as i32,
7844 i32::MAX,
7845 ];
7846 let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
7847
7848 let i64_expected = vec![
7849 "-2147483648",
7850 "-32768",
7851 "-128",
7852 "0",
7853 "127",
7854 "32767",
7855 "2147483647",
7856 ];
7857 assert_eq!(
7858 i64_expected,
7859 get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7860 );
7861 }
7862
7863 #[test]
7864 fn test_cast_from_int8() {
7865 let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7866 let i8_array = Int8Array::from(i8_values);
7867
7868 let f64_expected = vec!["-128.0", "0.0", "127.0"];
7869 assert_eq!(
7870 f64_expected,
7871 get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7872 );
7873
7874 let f32_expected = vec!["-128.0", "0.0", "127.0"];
7875 assert_eq!(
7876 f32_expected,
7877 get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7878 );
7879
7880 let f16_expected = vec!["-128.0", "0.0", "127.0"];
7881 assert_eq!(
7882 f16_expected,
7883 get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7884 );
7885
7886 let i64_expected = vec!["-128", "0", "127"];
7887 assert_eq!(
7888 i64_expected,
7889 get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7890 );
7891
7892 let i32_expected = vec!["-128", "0", "127"];
7893 assert_eq!(
7894 i32_expected,
7895 get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7896 );
7897
7898 let i16_expected = vec!["-128", "0", "127"];
7899 assert_eq!(
7900 i16_expected,
7901 get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7902 );
7903
7904 let i8_expected = vec!["-128", "0", "127"];
7905 assert_eq!(
7906 i8_expected,
7907 get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7908 );
7909
7910 let u64_expected = vec!["null", "0", "127"];
7911 assert_eq!(
7912 u64_expected,
7913 get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7914 );
7915
7916 let u32_expected = vec!["null", "0", "127"];
7917 assert_eq!(
7918 u32_expected,
7919 get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7920 );
7921
7922 let u16_expected = vec!["null", "0", "127"];
7923 assert_eq!(
7924 u16_expected,
7925 get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7926 );
7927
7928 let u8_expected = vec!["null", "0", "127"];
7929 assert_eq!(
7930 u8_expected,
7931 get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
7932 );
7933 }
7934
7935 fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
7937 where
7938 T: ArrowPrimitiveType,
7939 {
7940 let c = cast(array, dt).unwrap();
7941 let a = c.as_primitive::<T>();
7942 let mut v: Vec<String> = vec![];
7943 for i in 0..array.len() {
7944 if a.is_null(i) {
7945 v.push("null".to_string())
7946 } else {
7947 v.push(format!("{:?}", a.value(i)));
7948 }
7949 }
7950 v
7951 }
7952
7953 #[test]
7954 fn test_cast_utf8_dict() {
7955 let mut builder = StringDictionaryBuilder::<Int8Type>::new();
7957 builder.append("one").unwrap();
7958 builder.append_null();
7959 builder.append("three").unwrap();
7960 let array: ArrayRef = Arc::new(builder.finish());
7961
7962 let expected = vec!["one", "null", "three"];
7963
7964 let cast_type = Utf8;
7966 let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
7967 assert_eq!(cast_array.data_type(), &cast_type);
7968 assert_eq!(array_to_strings(&cast_array), expected);
7969
7970 let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
7973 let cast_array = cast(&array, &cast_type).expect("cast failed");
7974 assert_eq!(cast_array.data_type(), &cast_type);
7975 assert_eq!(array_to_strings(&cast_array), expected);
7976
7977 let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
7978 let cast_array = cast(&array, &cast_type).expect("cast failed");
7979 assert_eq!(cast_array.data_type(), &cast_type);
7980 assert_eq!(array_to_strings(&cast_array), expected);
7981
7982 let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
7983 let cast_array = cast(&array, &cast_type).expect("cast failed");
7984 assert_eq!(cast_array.data_type(), &cast_type);
7985 assert_eq!(array_to_strings(&cast_array), expected);
7986
7987 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7988 let cast_array = cast(&array, &cast_type).expect("cast failed");
7989 assert_eq!(cast_array.data_type(), &cast_type);
7990 assert_eq!(array_to_strings(&cast_array), expected);
7991
7992 let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
7993 let cast_array = cast(&array, &cast_type).expect("cast failed");
7994 assert_eq!(cast_array.data_type(), &cast_type);
7995 assert_eq!(array_to_strings(&cast_array), expected);
7996
7997 let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
7998 let cast_array = cast(&array, &cast_type).expect("cast failed");
7999 assert_eq!(cast_array.data_type(), &cast_type);
8000 assert_eq!(array_to_strings(&cast_array), expected);
8001
8002 let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8003 let cast_array = cast(&array, &cast_type).expect("cast failed");
8004 assert_eq!(cast_array.data_type(), &cast_type);
8005 assert_eq!(array_to_strings(&cast_array), expected);
8006 }
8007
8008 #[test]
8009 fn test_cast_dict_to_dict_bad_index_value_primitive() {
8010 let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8015
8016 for i in 0..200 {
8020 builder.append(i).unwrap();
8021 }
8022 let array: ArrayRef = Arc::new(builder.finish());
8023
8024 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8025 let res = cast(&array, &cast_type);
8026 assert!(res.is_err());
8027 let actual_error = format!("{res:?}");
8028 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8029 assert!(
8030 actual_error.contains(expected_error),
8031 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8032 );
8033 }
8034
8035 #[test]
8036 fn test_cast_dict_to_dict_bad_index_value_utf8() {
8037 let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8041
8042 for i in 0..200 {
8046 let val = format!("val{i}");
8047 builder.append(&val).unwrap();
8048 }
8049 let array = builder.finish();
8050
8051 let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8052 let res = cast(&array, &cast_type);
8053 assert!(res.is_err());
8054 let actual_error = format!("{res:?}");
8055 let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8056 assert!(
8057 actual_error.contains(expected_error),
8058 "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8059 );
8060 }
8061
8062 #[test]
8063 fn test_cast_primitive_dict() {
8064 let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8066 builder.append(1).unwrap();
8067 builder.append_null();
8068 builder.append(3).unwrap();
8069 let array: ArrayRef = Arc::new(builder.finish());
8070
8071 let expected = vec!["1", "null", "3"];
8072
8073 let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8075 assert_eq!(array_to_strings(&cast_array), expected);
8076 assert_eq!(cast_array.data_type(), &Utf8);
8077
8078 let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8079 assert_eq!(array_to_strings(&cast_array), expected);
8080 assert_eq!(cast_array.data_type(), &Int64);
8081 }
8082
8083 #[test]
8084 fn test_cast_primitive_array_to_dict() {
8085 let mut builder = PrimitiveBuilder::<Int32Type>::new();
8086 builder.append_value(1);
8087 builder.append_null();
8088 builder.append_value(3);
8089 let array: ArrayRef = Arc::new(builder.finish());
8090
8091 let expected = vec!["1", "null", "3"];
8092
8093 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8095 let cast_array = cast(&array, &cast_type).expect("cast failed");
8096 assert_eq!(cast_array.data_type(), &cast_type);
8097 assert_eq!(array_to_strings(&cast_array), expected);
8098
8099 let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8101 let cast_array = cast(&array, &cast_type).expect("cast failed");
8102 assert_eq!(cast_array.data_type(), &cast_type);
8103 assert_eq!(array_to_strings(&cast_array), expected);
8104 }
8105
8106 #[test]
8107 fn test_cast_time_array_to_dict() {
8108 use DataType::*;
8109
8110 let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8111
8112 let expected = vec!["1972-09-27", "null", "1975-06-24"];
8113
8114 let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8115 let cast_array = cast(&array, &cast_type).expect("cast failed");
8116 assert_eq!(cast_array.data_type(), &cast_type);
8117 assert_eq!(array_to_strings(&cast_array), expected);
8118 }
8119
8120 #[test]
8121 fn test_cast_timestamp_array_to_dict() {
8122 use DataType::*;
8123
8124 let array = Arc::new(
8125 TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8126 ) as ArrayRef;
8127
8128 let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8129
8130 let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8131 let cast_array = cast(&array, &cast_type).expect("cast failed");
8132 assert_eq!(cast_array.data_type(), &cast_type);
8133 assert_eq!(array_to_strings(&cast_array), expected);
8134 }
8135
8136 #[test]
8137 fn test_cast_string_array_to_dict() {
8138 use DataType::*;
8139
8140 let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8141
8142 let expected = vec!["one", "null", "three"];
8143
8144 let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8146 let cast_array = cast(&array, &cast_type).expect("cast failed");
8147 assert_eq!(cast_array.data_type(), &cast_type);
8148 assert_eq!(array_to_strings(&cast_array), expected);
8149 }
8150
8151 #[test]
8152 fn test_cast_null_array_to_from_decimal_array() {
8153 let data_type = DataType::Decimal128(12, 4);
8154 let array = new_null_array(&DataType::Null, 4);
8155 assert_eq!(array.data_type(), &DataType::Null);
8156 let cast_array = cast(&array, &data_type).expect("cast failed");
8157 assert_eq!(cast_array.data_type(), &data_type);
8158 for i in 0..4 {
8159 assert!(cast_array.is_null(i));
8160 }
8161
8162 let array = new_null_array(&data_type, 4);
8163 assert_eq!(array.data_type(), &data_type);
8164 let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8165 assert_eq!(cast_array.data_type(), &DataType::Null);
8166 assert_eq!(cast_array.len(), 4);
8167 assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8168 }
8169
8170 #[test]
8171 fn test_cast_null_array_from_and_to_primitive_array() {
8172 macro_rules! typed_test {
8173 ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8174 {
8175 let array = Arc::new(NullArray::new(6)) as ArrayRef;
8176 let expected = $ARR_TYPE::from(vec![None; 6]);
8177 let cast_type = DataType::$DATATYPE;
8178 let cast_array = cast(&array, &cast_type).expect("cast failed");
8179 let cast_array = cast_array.as_primitive::<$TYPE>();
8180 assert_eq!(cast_array.data_type(), &cast_type);
8181 assert_eq!(cast_array, &expected);
8182 }
8183 }};
8184 }
8185
8186 typed_test!(Int16Array, Int16, Int16Type);
8187 typed_test!(Int32Array, Int32, Int32Type);
8188 typed_test!(Int64Array, Int64, Int64Type);
8189
8190 typed_test!(UInt16Array, UInt16, UInt16Type);
8191 typed_test!(UInt32Array, UInt32, UInt32Type);
8192 typed_test!(UInt64Array, UInt64, UInt64Type);
8193
8194 typed_test!(Float16Array, Float16, Float16Type);
8195 typed_test!(Float32Array, Float32, Float32Type);
8196 typed_test!(Float64Array, Float64, Float64Type);
8197
8198 typed_test!(Date32Array, Date32, Date32Type);
8199 typed_test!(Date64Array, Date64, Date64Type);
8200 }
8201
8202 fn cast_from_null_to_other(data_type: &DataType) {
8203 {
8205 let array = new_null_array(&DataType::Null, 4);
8206 assert_eq!(array.data_type(), &DataType::Null);
8207 let cast_array = cast(&array, data_type).expect("cast failed");
8208 assert_eq!(cast_array.data_type(), data_type);
8209 for i in 0..4 {
8210 assert!(cast_array.is_null(i));
8211 }
8212 }
8213 }
8214
8215 #[test]
8216 fn test_cast_null_from_and_to_variable_sized() {
8217 cast_from_null_to_other(&DataType::Utf8);
8218 cast_from_null_to_other(&DataType::LargeUtf8);
8219 cast_from_null_to_other(&DataType::Binary);
8220 cast_from_null_to_other(&DataType::LargeBinary);
8221 }
8222
8223 #[test]
8224 fn test_cast_null_from_and_to_nested_type() {
8225 let data_type = DataType::Map(
8227 Arc::new(Field::new_struct(
8228 "entry",
8229 vec![
8230 Field::new("key", DataType::Utf8, false),
8231 Field::new("value", DataType::Int32, true),
8232 ],
8233 false,
8234 )),
8235 false,
8236 );
8237 cast_from_null_to_other(&data_type);
8238
8239 let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8241 cast_from_null_to_other(&data_type);
8242 let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8243 cast_from_null_to_other(&data_type);
8244 let data_type =
8245 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8246 cast_from_null_to_other(&data_type);
8247
8248 let values = vec![None, None, None, None] as Vec<Option<&str>>;
8250 let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8251 let array = Arc::new(array) as ArrayRef;
8252 let data_type = array.data_type().to_owned();
8253 cast_from_null_to_other(&data_type);
8254
8255 let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8257 cast_from_null_to_other(&data_type);
8258 }
8259
8260 fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8262 let options = FormatOptions::new().with_null("null");
8263 let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8264 (0..array.len())
8265 .map(|i| formatter.value(i).to_string())
8266 .collect()
8267 }
8268
8269 #[test]
8270 fn test_cast_utf8_to_date32() {
8271 use chrono::NaiveDate;
8272 let from_ymd = chrono::NaiveDate::from_ymd_opt;
8273 let since = chrono::NaiveDate::signed_duration_since;
8274
8275 let a = StringArray::from(vec![
8276 "2000-01-01", "2000-01-01T12:00:00", "2000-2-2", "2000-00-00", "2000", ]);
8282 let array = Arc::new(a) as ArrayRef;
8283 let b = cast(&array, &DataType::Date32).unwrap();
8284 let c = b.as_primitive::<Date32Type>();
8285
8286 let date_value = since(
8288 NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8289 from_ymd(1970, 1, 1).unwrap(),
8290 )
8291 .num_days() as i32;
8292 assert!(c.is_valid(0)); assert_eq!(date_value, c.value(0));
8294
8295 assert!(c.is_valid(1)); assert_eq!(date_value, c.value(1));
8297
8298 let date_value = since(
8299 NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8300 from_ymd(1970, 1, 1).unwrap(),
8301 )
8302 .num_days() as i32;
8303 assert!(c.is_valid(2)); assert_eq!(date_value, c.value(2));
8305
8306 assert!(!c.is_valid(3)); assert!(!c.is_valid(4)); }
8310
8311 #[test]
8312 fn test_cast_utf8_to_date64() {
8313 let a = StringArray::from(vec![
8314 "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", ]);
8321 let array = Arc::new(a) as ArrayRef;
8322 let b = cast(&array, &DataType::Date64).unwrap();
8323 let c = b.as_primitive::<Date64Type>();
8324
8325 assert!(c.is_valid(0)); assert_eq!(946728000000, c.value(0));
8328 assert!(c.is_valid(1)); assert_eq!(1608035696000, c.value(1));
8330 assert!(!c.is_valid(2)); assert!(!c.is_valid(3)); assert!(c.is_valid(4)); assert_eq!(946728000000, c.value(4));
8335 assert!(c.is_valid(5)); assert_eq!(946684800000, c.value(5));
8337 }
8338
8339 #[test]
8340 fn test_can_cast_fsl_to_fsl() {
8341 let from_array = Arc::new(
8342 FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8343 [Some([Some(1.0), Some(2.0)]), None],
8344 2,
8345 ),
8346 ) as ArrayRef;
8347 let to_array = Arc::new(
8348 FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8349 [
8350 Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8351 None,
8352 ],
8353 2,
8354 ),
8355 ) as ArrayRef;
8356
8357 assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8358 let actual = cast(&from_array, to_array.data_type()).unwrap();
8359 assert_eq!(actual.data_type(), to_array.data_type());
8360
8361 let invalid_target =
8362 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8363 assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8364
8365 let invalid_size =
8366 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8367 assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8368 }
8369
8370 #[test]
8371 fn test_can_cast_types_fixed_size_list_to_list() {
8372 let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
8374 assert!(can_cast_types(
8375 array1.data_type(),
8376 &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8377 ));
8378
8379 let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
8381 assert!(can_cast_types(
8382 array2.data_type(),
8383 &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8384 ));
8385 }
8386
8387 #[test]
8388 fn test_cast_fixed_size_list_to_list() {
8389 let cases = [
8395 (
8397 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8398 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8399 2,
8400 )) as ArrayRef,
8401 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8402 Some([Some(1), Some(1)]),
8403 Some([Some(2), Some(2)]),
8404 ])) as ArrayRef,
8405 ),
8406 (
8408 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8409 [None, Some([Some(2), Some(2)])],
8410 2,
8411 )) as ArrayRef,
8412 Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8413 None,
8414 Some([Some(2), Some(2)]),
8415 ])) as ArrayRef,
8416 ),
8417 (
8419 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8420 [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8421 2,
8422 )) as ArrayRef,
8423 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8424 Some([Some(1), Some(1)]),
8425 Some([Some(2), Some(2)]),
8426 ])) as ArrayRef,
8427 ),
8428 (
8430 Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8431 [None, Some([Some(2), Some(2)])],
8432 2,
8433 )) as ArrayRef,
8434 Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8435 None,
8436 Some([Some(2), Some(2)]),
8437 ])) as ArrayRef,
8438 ),
8439 ];
8440
8441 for (array, expected) in cases {
8442 let array = Arc::new(array) as ArrayRef;
8443
8444 assert!(
8445 can_cast_types(array.data_type(), expected.data_type()),
8446 "can_cast_types claims we cannot cast {:?} to {:?}",
8447 array.data_type(),
8448 expected.data_type()
8449 );
8450
8451 let list_array = cast(&array, expected.data_type())
8452 .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8453 assert_eq!(
8454 list_array.as_ref(),
8455 &expected,
8456 "Incorrect result from casting {array:?} to {expected:?}",
8457 );
8458 }
8459 }
8460
8461 #[test]
8462 fn test_cast_utf8_to_list() {
8463 let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8465 let field = Arc::new(Field::new("", DataType::Int32, false));
8466 let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8467 let actual = list_array.as_list_opt::<i32>().unwrap();
8468 let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8469 assert_eq!(&expect.value(0), &actual.value(0));
8470
8471 let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8473 let actual = list_array.as_list_opt::<i64>().unwrap();
8474 let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8475 assert_eq!(&expect.value(0), &actual.value(0));
8476
8477 let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8479 let actual = list_array.as_fixed_size_list_opt().unwrap();
8480 let expect =
8481 FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
8482 assert_eq!(&expect.value(0), &actual.value(0));
8483 }
8484
8485 #[test]
8486 fn test_cast_single_element_fixed_size_list() {
8487 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8489 [(Some([Some(5)]))],
8490 1,
8491 )) as ArrayRef;
8492 let casted_array = cast(&from_array, &DataType::Int32).unwrap();
8493 let actual: &Int32Array = casted_array.as_primitive();
8494 let expected = Int32Array::from(vec![Some(5)]);
8495 assert_eq!(&expected, actual);
8496
8497 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8499 [(Some([Some(5)]))],
8500 1,
8501 )) as ArrayRef;
8502 let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
8503 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8504 let expected = Arc::new(FixedSizeListArray::new(
8505 to_field.clone(),
8506 1,
8507 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8508 None,
8509 )) as ArrayRef;
8510 assert_eq!(*expected, *actual);
8511
8512 let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8514 [(Some([Some(5)]))],
8515 1,
8516 )) as ArrayRef;
8517 let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
8518 let to_field = Arc::new(Field::new(
8519 "dummy",
8520 DataType::FixedSizeList(to_field_inner.clone(), 1),
8521 false,
8522 ));
8523 let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8524 let expected = Arc::new(FixedSizeListArray::new(
8525 to_field.clone(),
8526 1,
8527 Arc::new(FixedSizeListArray::new(
8528 to_field_inner.clone(),
8529 1,
8530 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8531 None,
8532 )) as ArrayRef,
8533 None,
8534 )) as ArrayRef;
8535 assert_eq!(*expected, *actual);
8536
8537 let field = Arc::new(Field::new("dummy", DataType::Float32, false));
8539 let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
8540 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8541 let actual = casted_array.as_fixed_size_list();
8542 let expected = Arc::new(FixedSizeListArray::new(
8543 field.clone(),
8544 1,
8545 Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8546 None,
8547 )) as ArrayRef;
8548 assert_eq!(expected.as_ref(), actual);
8549
8550 let field = Arc::new(Field::new("nullable", DataType::Float32, true));
8552 let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
8553 let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8554 let actual = casted_array.as_fixed_size_list();
8555 let expected = Arc::new(FixedSizeListArray::new(
8556 field.clone(),
8557 1,
8558 Arc::new(Float32Array::from(vec![None])) as ArrayRef,
8559 None,
8560 )) as ArrayRef;
8561 assert_eq!(expected.as_ref(), actual);
8562 }
8563
8564 #[test]
8565 fn test_cast_list_containers() {
8566 let array = Arc::new(make_large_list_array()) as ArrayRef;
8568 let list_array = cast(
8569 &array,
8570 &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
8571 )
8572 .unwrap();
8573 let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
8574 let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
8575
8576 assert_eq!(&expected.value(0), &actual.value(0));
8577 assert_eq!(&expected.value(1), &actual.value(1));
8578 assert_eq!(&expected.value(2), &actual.value(2));
8579
8580 let array = Arc::new(make_list_array()) as ArrayRef;
8582 let large_list_array = cast(
8583 &array,
8584 &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
8585 )
8586 .unwrap();
8587 let actual = large_list_array
8588 .as_any()
8589 .downcast_ref::<LargeListArray>()
8590 .unwrap();
8591 let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
8592
8593 assert_eq!(&expected.value(0), &actual.value(0));
8594 assert_eq!(&expected.value(1), &actual.value(1));
8595 assert_eq!(&expected.value(2), &actual.value(2));
8596 }
8597
8598 #[test]
8599 fn test_cast_list_to_fsl() {
8600 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8608 let values = vec![
8609 Some(vec![Some(1), Some(2), Some(3)]),
8610 Some(vec![Some(4), Some(5), Some(6)]),
8611 ];
8612 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8613 values.clone(),
8614 )) as ArrayRef;
8615 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8616 values, 3,
8617 )) as ArrayRef;
8618 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8619 assert_eq!(expected.as_ref(), actual.as_ref());
8620
8621 let cases = [
8624 (
8625 vec![1, 2, 3, 4, 5, 6],
8627 vec![3, 0, 3, 0],
8628 ),
8629 (
8630 vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
8632 vec![3, 2, 3, 1],
8633 ),
8634 (
8635 vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
8637 vec![3, 3, 3, 3],
8638 ),
8639 (
8640 vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
8642 vec![3, 0, 3, 3],
8643 ),
8644 ];
8645 let null_buffer = NullBuffer::from(vec![true, false, true, false]);
8646
8647 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8648 vec![
8649 Some(vec![Some(1), Some(2), Some(3)]),
8650 None,
8651 Some(vec![Some(4), Some(5), Some(6)]),
8652 None,
8653 ],
8654 3,
8655 )) as ArrayRef;
8656
8657 for (values, lengths) in cases.iter() {
8658 let array = Arc::new(ListArray::new(
8659 field.clone(),
8660 OffsetBuffer::from_lengths(lengths.clone()),
8661 Arc::new(Int32Array::from(values.clone())),
8662 Some(null_buffer.clone()),
8663 )) as ArrayRef;
8664 let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8665 assert_eq!(expected.as_ref(), actual.as_ref());
8666 }
8667 }
8668
8669 #[test]
8670 fn test_cast_list_to_fsl_safety() {
8671 let values = vec![
8672 Some(vec![Some(1), Some(2), Some(3)]),
8673 Some(vec![Some(4), Some(5)]),
8674 Some(vec![Some(6), Some(7), Some(8), Some(9)]),
8675 Some(vec![Some(3), Some(4), Some(5)]),
8676 ];
8677 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8678 values.clone(),
8679 )) as ArrayRef;
8680
8681 let res = cast_with_options(
8682 array.as_ref(),
8683 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8684 &CastOptions {
8685 safe: false,
8686 ..Default::default()
8687 },
8688 );
8689 assert!(res.is_err());
8690 assert!(
8691 format!("{res:?}")
8692 .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
8693 );
8694
8695 let res = cast(
8698 array.as_ref(),
8699 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8700 )
8701 .unwrap();
8702 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8703 vec![
8704 Some(vec![Some(1), Some(2), Some(3)]),
8705 None, None, Some(vec![Some(3), Some(4), Some(5)]),
8708 ],
8709 3,
8710 )) as ArrayRef;
8711 assert_eq!(expected.as_ref(), res.as_ref());
8712
8713 let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
8716 Some(vec![Some(1), Some(2), Some(3)]),
8717 None,
8718 ])) as ArrayRef;
8719 let res = cast_with_options(
8720 array.as_ref(),
8721 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8722 &CastOptions {
8723 safe: false,
8724 ..Default::default()
8725 },
8726 )
8727 .unwrap();
8728 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8729 vec![Some(vec![Some(1), Some(2), Some(3)]), None],
8730 3,
8731 )) as ArrayRef;
8732 assert_eq!(expected.as_ref(), res.as_ref());
8733 }
8734
8735 #[test]
8736 fn test_cast_large_list_to_fsl() {
8737 let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
8738 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8739 values.clone(),
8740 )) as ArrayRef;
8741 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8742 values, 2,
8743 )) as ArrayRef;
8744 let actual = cast(
8745 array.as_ref(),
8746 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
8747 )
8748 .unwrap();
8749 assert_eq!(expected.as_ref(), actual.as_ref());
8750 }
8751
8752 #[test]
8753 fn test_cast_list_to_fsl_subcast() {
8754 let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8755 vec![
8756 Some(vec![Some(1), Some(2)]),
8757 Some(vec![Some(3), Some(i32::MAX)]),
8758 ],
8759 )) as ArrayRef;
8760 let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
8761 vec![
8762 Some(vec![Some(1), Some(2)]),
8763 Some(vec![Some(3), Some(i32::MAX as i64)]),
8764 ],
8765 2,
8766 )) as ArrayRef;
8767 let actual = cast(
8768 array.as_ref(),
8769 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
8770 )
8771 .unwrap();
8772 assert_eq!(expected.as_ref(), actual.as_ref());
8773
8774 let res = cast_with_options(
8775 array.as_ref(),
8776 &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
8777 &CastOptions {
8778 safe: false,
8779 ..Default::default()
8780 },
8781 );
8782 assert!(res.is_err());
8783 assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
8784 }
8785
8786 #[test]
8787 fn test_cast_list_to_fsl_empty() {
8788 let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8789 let array = new_empty_array(&DataType::List(field.clone()));
8790
8791 let target_type = DataType::FixedSizeList(field.clone(), 3);
8792 let expected = new_empty_array(&target_type);
8793
8794 let actual = cast(array.as_ref(), &target_type).unwrap();
8795 assert_eq!(expected.as_ref(), actual.as_ref());
8796 }
8797
8798 fn make_list_array() -> ListArray {
8799 let value_data = ArrayData::builder(DataType::Int32)
8801 .len(8)
8802 .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8803 .build()
8804 .unwrap();
8805
8806 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8809
8810 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8812 let list_data = ArrayData::builder(list_data_type)
8813 .len(3)
8814 .add_buffer(value_offsets)
8815 .add_child_data(value_data)
8816 .build()
8817 .unwrap();
8818 ListArray::from(list_data)
8819 }
8820
8821 fn make_large_list_array() -> LargeListArray {
8822 let value_data = ArrayData::builder(DataType::Int32)
8824 .len(8)
8825 .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8826 .build()
8827 .unwrap();
8828
8829 let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
8832
8833 let list_data_type =
8835 DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8836 let list_data = ArrayData::builder(list_data_type)
8837 .len(3)
8838 .add_buffer(value_offsets)
8839 .add_child_data(value_data)
8840 .build()
8841 .unwrap();
8842 LargeListArray::from(list_data)
8843 }
8844
8845 fn make_fixed_size_list_array() -> FixedSizeListArray {
8846 let value_data = ArrayData::builder(DataType::Int32)
8848 .len(8)
8849 .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8850 .build()
8851 .unwrap();
8852
8853 let list_data_type =
8854 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8855 let list_data = ArrayData::builder(list_data_type)
8856 .len(2)
8857 .add_child_data(value_data)
8858 .build()
8859 .unwrap();
8860 FixedSizeListArray::from(list_data)
8861 }
8862
8863 fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8864 let value_data = ArrayData::builder(DataType::Int64)
8866 .len(8)
8867 .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8868 .build()
8869 .unwrap();
8870
8871 let list_data_type =
8872 DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8873 let list_data = ArrayData::builder(list_data_type)
8874 .len(2)
8875 .add_child_data(value_data)
8876 .build()
8877 .unwrap();
8878 FixedSizeListArray::from(list_data)
8879 }
8880
8881 #[test]
8882 fn test_cast_map_dont_allow_change_of_order() {
8883 let string_builder = StringBuilder::new();
8884 let value_builder = StringBuilder::new();
8885 let mut builder = MapBuilder::new(
8886 Some(MapFieldNames {
8887 entry: "entries".to_string(),
8888 key: "key".to_string(),
8889 value: "value".to_string(),
8890 }),
8891 string_builder,
8892 value_builder,
8893 );
8894
8895 builder.keys().append_value("0");
8896 builder.values().append_value("test_val_1");
8897 builder.append(true).unwrap();
8898 builder.keys().append_value("1");
8899 builder.values().append_value("test_val_2");
8900 builder.append(true).unwrap();
8901
8902 let array = builder.finish();
8904
8905 let new_ordered = true;
8906 let new_type = DataType::Map(
8907 Arc::new(Field::new(
8908 "entries",
8909 DataType::Struct(
8910 vec![
8911 Field::new("key", DataType::Utf8, false),
8912 Field::new("value", DataType::Utf8, false),
8913 ]
8914 .into(),
8915 ),
8916 false,
8917 )),
8918 new_ordered,
8919 );
8920
8921 let new_array_result = cast(&array, &new_type.clone());
8922 assert!(!can_cast_types(array.data_type(), &new_type));
8923 let Err(ArrowError::CastError(t)) = new_array_result else {
8924 panic!();
8925 };
8926 assert_eq!(
8927 t,
8928 r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Utf8), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Utf8), sorted) not supported"#
8929 );
8930 }
8931
8932 #[test]
8933 fn test_cast_map_dont_allow_when_container_cant_cast() {
8934 let string_builder = StringBuilder::new();
8935 let value_builder = IntervalDayTimeArray::builder(2);
8936 let mut builder = MapBuilder::new(
8937 Some(MapFieldNames {
8938 entry: "entries".to_string(),
8939 key: "key".to_string(),
8940 value: "value".to_string(),
8941 }),
8942 string_builder,
8943 value_builder,
8944 );
8945
8946 builder.keys().append_value("0");
8947 builder.values().append_value(IntervalDayTime::new(1, 1));
8948 builder.append(true).unwrap();
8949 builder.keys().append_value("1");
8950 builder.values().append_value(IntervalDayTime::new(2, 2));
8951 builder.append(true).unwrap();
8952
8953 let array = builder.finish();
8955
8956 let new_ordered = true;
8957 let new_type = DataType::Map(
8958 Arc::new(Field::new(
8959 "entries",
8960 DataType::Struct(
8961 vec![
8962 Field::new("key", DataType::Utf8, false),
8963 Field::new("value", DataType::Duration(TimeUnit::Second), false),
8964 ]
8965 .into(),
8966 ),
8967 false,
8968 )),
8969 new_ordered,
8970 );
8971
8972 let new_array_result = cast(&array, &new_type.clone());
8973 assert!(!can_cast_types(array.data_type(), &new_type));
8974 let Err(ArrowError::CastError(t)) = new_array_result else {
8975 panic!();
8976 };
8977 assert_eq!(
8978 t,
8979 r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Interval(DayTime)), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Duration(s)), sorted) not supported"#
8980 );
8981 }
8982
8983 #[test]
8984 fn test_cast_map_field_names() {
8985 let string_builder = StringBuilder::new();
8986 let value_builder = StringBuilder::new();
8987 let mut builder = MapBuilder::new(
8988 Some(MapFieldNames {
8989 entry: "entries".to_string(),
8990 key: "key".to_string(),
8991 value: "value".to_string(),
8992 }),
8993 string_builder,
8994 value_builder,
8995 );
8996
8997 builder.keys().append_value("0");
8998 builder.values().append_value("test_val_1");
8999 builder.append(true).unwrap();
9000 builder.keys().append_value("1");
9001 builder.values().append_value("test_val_2");
9002 builder.append(true).unwrap();
9003 builder.append(false).unwrap();
9004
9005 let array = builder.finish();
9006
9007 let new_type = DataType::Map(
9008 Arc::new(Field::new(
9009 "entries_new",
9010 DataType::Struct(
9011 vec![
9012 Field::new("key_new", DataType::Utf8, false),
9013 Field::new("value_values", DataType::Utf8, false),
9014 ]
9015 .into(),
9016 ),
9017 false,
9018 )),
9019 false,
9020 );
9021
9022 assert_ne!(new_type, array.data_type().clone());
9023
9024 let new_array = cast(&array, &new_type.clone()).unwrap();
9025 assert_eq!(new_type, new_array.data_type().clone());
9026 let map_array = new_array.as_map();
9027
9028 assert_ne!(new_type, array.data_type().clone());
9029 assert_eq!(new_type, map_array.data_type().clone());
9030
9031 let key_string = map_array
9032 .keys()
9033 .as_any()
9034 .downcast_ref::<StringArray>()
9035 .unwrap()
9036 .into_iter()
9037 .flatten()
9038 .collect::<Vec<_>>();
9039 assert_eq!(&key_string, &vec!["0", "1"]);
9040
9041 let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9042 let values_string = values_string_array
9043 .as_any()
9044 .downcast_ref::<StringArray>()
9045 .unwrap()
9046 .into_iter()
9047 .flatten()
9048 .collect::<Vec<_>>();
9049 assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
9050
9051 assert_eq!(
9052 map_array.nulls(),
9053 Some(&NullBuffer::from(vec![true, true, false]))
9054 );
9055 }
9056
9057 #[test]
9058 fn test_cast_map_contained_values() {
9059 let string_builder = StringBuilder::new();
9060 let value_builder = Int8Builder::new();
9061 let mut builder = MapBuilder::new(
9062 Some(MapFieldNames {
9063 entry: "entries".to_string(),
9064 key: "key".to_string(),
9065 value: "value".to_string(),
9066 }),
9067 string_builder,
9068 value_builder,
9069 );
9070
9071 builder.keys().append_value("0");
9072 builder.values().append_value(44);
9073 builder.append(true).unwrap();
9074 builder.keys().append_value("1");
9075 builder.values().append_value(22);
9076 builder.append(true).unwrap();
9077
9078 let array = builder.finish();
9079
9080 let new_type = DataType::Map(
9081 Arc::new(Field::new(
9082 "entries",
9083 DataType::Struct(
9084 vec![
9085 Field::new("key", DataType::Utf8, false),
9086 Field::new("value", DataType::Utf8, false),
9087 ]
9088 .into(),
9089 ),
9090 false,
9091 )),
9092 false,
9093 );
9094
9095 let new_array = cast(&array, &new_type.clone()).unwrap();
9096 assert_eq!(new_type, new_array.data_type().clone());
9097 let map_array = new_array.as_map();
9098
9099 assert_ne!(new_type, array.data_type().clone());
9100 assert_eq!(new_type, map_array.data_type().clone());
9101
9102 let key_string = map_array
9103 .keys()
9104 .as_any()
9105 .downcast_ref::<StringArray>()
9106 .unwrap()
9107 .into_iter()
9108 .flatten()
9109 .collect::<Vec<_>>();
9110 assert_eq!(&key_string, &vec!["0", "1"]);
9111
9112 let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9113 let values_string = values_string_array
9114 .as_any()
9115 .downcast_ref::<StringArray>()
9116 .unwrap()
9117 .into_iter()
9118 .flatten()
9119 .collect::<Vec<_>>();
9120 assert_eq!(&values_string, &vec!["44", "22"]);
9121 }
9122
9123 #[test]
9124 fn test_utf8_cast_offsets() {
9125 let str_array = StringArray::from(vec!["a", "b", "c"]);
9127 let str_array = str_array.slice(1, 2);
9128
9129 let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9130
9131 let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9132 let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9133 assert_eq!(strs, &["b", "c"])
9134 }
9135
9136 #[test]
9137 fn test_list_cast_offsets() {
9138 let array1 = make_list_array().slice(1, 2);
9140 let array2 = Arc::new(make_list_array()) as ArrayRef;
9141
9142 let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9143 let out1 = cast(&array1, &dt).unwrap();
9144 let out2 = cast(&array2, &dt).unwrap();
9145
9146 assert_eq!(&out1, &out2.slice(1, 2))
9147 }
9148
9149 #[test]
9150 fn test_list_to_string() {
9151 let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
9152 let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
9153 let value_data = str_array.into_data();
9154
9155 let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
9156 let list_data = ArrayData::builder(list_data_type)
9157 .len(3)
9158 .add_buffer(value_offsets)
9159 .add_child_data(value_data)
9160 .build()
9161 .unwrap();
9162 let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
9163
9164 let out = cast(&array, &DataType::Utf8).unwrap();
9165 let out = out
9166 .as_any()
9167 .downcast_ref::<StringArray>()
9168 .unwrap()
9169 .into_iter()
9170 .flatten()
9171 .collect::<Vec<_>>();
9172 assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9173
9174 let out = cast(&array, &DataType::LargeUtf8).unwrap();
9175 let out = out
9176 .as_any()
9177 .downcast_ref::<LargeStringArray>()
9178 .unwrap()
9179 .into_iter()
9180 .flatten()
9181 .collect::<Vec<_>>();
9182 assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9183
9184 let array = Arc::new(make_list_array()) as ArrayRef;
9185 let out = cast(&array, &DataType::Utf8).unwrap();
9186 let out = out
9187 .as_any()
9188 .downcast_ref::<StringArray>()
9189 .unwrap()
9190 .into_iter()
9191 .flatten()
9192 .collect::<Vec<_>>();
9193 assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9194
9195 let array = Arc::new(make_large_list_array()) as ArrayRef;
9196 let out = cast(&array, &DataType::LargeUtf8).unwrap();
9197 let out = out
9198 .as_any()
9199 .downcast_ref::<LargeStringArray>()
9200 .unwrap()
9201 .into_iter()
9202 .flatten()
9203 .collect::<Vec<_>>();
9204 assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9205 }
9206
9207 #[test]
9208 fn test_cast_f64_to_decimal128() {
9209 let decimal_type = DataType::Decimal128(18, 2);
9212 let array = Float64Array::from(vec![
9213 Some(0.0699999999),
9214 Some(0.0659999999),
9215 Some(0.0650000000),
9216 Some(0.0649999999),
9217 ]);
9218 let array = Arc::new(array) as ArrayRef;
9219 generate_cast_test_case!(
9220 &array,
9221 Decimal128Array,
9222 &decimal_type,
9223 vec![
9224 Some(7_i128), Some(7_i128), Some(7_i128), Some(6_i128), ]
9229 );
9230
9231 let decimal_type = DataType::Decimal128(18, 3);
9232 let array = Float64Array::from(vec![
9233 Some(0.0699999999),
9234 Some(0.0659999999),
9235 Some(0.0650000000),
9236 Some(0.0649999999),
9237 ]);
9238 let array = Arc::new(array) as ArrayRef;
9239 generate_cast_test_case!(
9240 &array,
9241 Decimal128Array,
9242 &decimal_type,
9243 vec![
9244 Some(70_i128), Some(66_i128), Some(65_i128), Some(65_i128), ]
9249 );
9250 }
9251
9252 #[test]
9253 fn test_cast_numeric_to_decimal128_overflow() {
9254 let array = Int64Array::from(vec![i64::MAX]);
9255 let array = Arc::new(array) as ArrayRef;
9256 let casted_array = cast_with_options(
9257 &array,
9258 &DataType::Decimal128(38, 30),
9259 &CastOptions {
9260 safe: true,
9261 format_options: FormatOptions::default(),
9262 },
9263 );
9264 assert!(casted_array.is_ok());
9265 assert!(casted_array.unwrap().is_null(0));
9266
9267 let casted_array = cast_with_options(
9268 &array,
9269 &DataType::Decimal128(38, 30),
9270 &CastOptions {
9271 safe: false,
9272 format_options: FormatOptions::default(),
9273 },
9274 );
9275 assert!(casted_array.is_err());
9276 }
9277
9278 #[test]
9279 fn test_cast_numeric_to_decimal256_overflow() {
9280 let array = Int64Array::from(vec![i64::MAX]);
9281 let array = Arc::new(array) as ArrayRef;
9282 let casted_array = cast_with_options(
9283 &array,
9284 &DataType::Decimal256(76, 76),
9285 &CastOptions {
9286 safe: true,
9287 format_options: FormatOptions::default(),
9288 },
9289 );
9290 assert!(casted_array.is_ok());
9291 assert!(casted_array.unwrap().is_null(0));
9292
9293 let casted_array = cast_with_options(
9294 &array,
9295 &DataType::Decimal256(76, 76),
9296 &CastOptions {
9297 safe: false,
9298 format_options: FormatOptions::default(),
9299 },
9300 );
9301 assert!(casted_array.is_err());
9302 }
9303
9304 #[test]
9305 fn test_cast_floating_point_to_decimal128_precision_overflow() {
9306 let array = Float64Array::from(vec![1.1]);
9307 let array = Arc::new(array) as ArrayRef;
9308 let casted_array = cast_with_options(
9309 &array,
9310 &DataType::Decimal128(2, 2),
9311 &CastOptions {
9312 safe: true,
9313 format_options: FormatOptions::default(),
9314 },
9315 );
9316 assert!(casted_array.is_ok());
9317 assert!(casted_array.unwrap().is_null(0));
9318
9319 let casted_array = cast_with_options(
9320 &array,
9321 &DataType::Decimal128(2, 2),
9322 &CastOptions {
9323 safe: false,
9324 format_options: FormatOptions::default(),
9325 },
9326 );
9327 let err = casted_array.unwrap_err().to_string();
9328 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
9329 assert!(
9330 err.contains(expected_error),
9331 "did not find expected error '{expected_error}' in actual error '{err}'"
9332 );
9333 }
9334
9335 #[test]
9336 fn test_cast_floating_point_to_decimal256_precision_overflow() {
9337 let array = Float64Array::from(vec![1.1]);
9338 let array = Arc::new(array) as ArrayRef;
9339 let casted_array = cast_with_options(
9340 &array,
9341 &DataType::Decimal256(2, 2),
9342 &CastOptions {
9343 safe: true,
9344 format_options: FormatOptions::default(),
9345 },
9346 );
9347 assert!(casted_array.is_ok());
9348 assert!(casted_array.unwrap().is_null(0));
9349
9350 let casted_array = cast_with_options(
9351 &array,
9352 &DataType::Decimal256(2, 2),
9353 &CastOptions {
9354 safe: false,
9355 format_options: FormatOptions::default(),
9356 },
9357 );
9358 let err = casted_array.unwrap_err().to_string();
9359 let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
9360 assert_eq!(err, expected_error);
9361 }
9362
9363 #[test]
9364 fn test_cast_floating_point_to_decimal128_overflow() {
9365 let array = Float64Array::from(vec![f64::MAX]);
9366 let array = Arc::new(array) as ArrayRef;
9367 let casted_array = cast_with_options(
9368 &array,
9369 &DataType::Decimal128(38, 30),
9370 &CastOptions {
9371 safe: true,
9372 format_options: FormatOptions::default(),
9373 },
9374 );
9375 assert!(casted_array.is_ok());
9376 assert!(casted_array.unwrap().is_null(0));
9377
9378 let casted_array = cast_with_options(
9379 &array,
9380 &DataType::Decimal128(38, 30),
9381 &CastOptions {
9382 safe: false,
9383 format_options: FormatOptions::default(),
9384 },
9385 );
9386 let err = casted_array.unwrap_err().to_string();
9387 let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
9388 assert!(
9389 err.contains(expected_error),
9390 "did not find expected error '{expected_error}' in actual error '{err}'"
9391 );
9392 }
9393
9394 #[test]
9395 fn test_cast_floating_point_to_decimal256_overflow() {
9396 let array = Float64Array::from(vec![f64::MAX]);
9397 let array = Arc::new(array) as ArrayRef;
9398 let casted_array = cast_with_options(
9399 &array,
9400 &DataType::Decimal256(76, 50),
9401 &CastOptions {
9402 safe: true,
9403 format_options: FormatOptions::default(),
9404 },
9405 );
9406 assert!(casted_array.is_ok());
9407 assert!(casted_array.unwrap().is_null(0));
9408
9409 let casted_array = cast_with_options(
9410 &array,
9411 &DataType::Decimal256(76, 50),
9412 &CastOptions {
9413 safe: false,
9414 format_options: FormatOptions::default(),
9415 },
9416 );
9417 let err = casted_array.unwrap_err().to_string();
9418 let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
9419 assert!(
9420 err.contains(expected_error),
9421 "did not find expected error '{expected_error}' in actual error '{err}'"
9422 );
9423 }
9424 #[test]
9425 fn test_cast_decimal256_to_f64_no_overflow() {
9426 let array = vec![Some(i256::MAX)];
9428 let array = create_decimal256_array(array, 76, 2).unwrap();
9429 let array = Arc::new(array) as ArrayRef;
9430
9431 let result = cast(&array, &DataType::Float64).unwrap();
9432 let result = result.as_primitive::<Float64Type>();
9433 assert!(result.value(0).is_finite());
9434 assert!(result.value(0) > 0.0); let array = vec![Some(i256::MIN)];
9438 let array = create_decimal256_array(array, 76, 2).unwrap();
9439 let array = Arc::new(array) as ArrayRef;
9440
9441 let result = cast(&array, &DataType::Float64).unwrap();
9442 let result = result.as_primitive::<Float64Type>();
9443 assert!(result.value(0).is_finite());
9444 assert!(result.value(0) < 0.0); }
9446
9447 #[test]
9448 fn test_cast_decimal128_to_decimal128_negative_scale() {
9449 let input_type = DataType::Decimal128(20, 0);
9450 let output_type = DataType::Decimal128(20, -1);
9451 assert!(can_cast_types(&input_type, &output_type));
9452 let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
9453 let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
9454 let array = Arc::new(input_decimal_array) as ArrayRef;
9455 generate_cast_test_case!(
9456 &array,
9457 Decimal128Array,
9458 &output_type,
9459 vec![
9460 Some(112345_i128),
9461 Some(212346_i128),
9462 Some(312346_i128),
9463 None
9464 ]
9465 );
9466
9467 let casted_array = cast(&array, &output_type).unwrap();
9468 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9469
9470 assert_eq!("1123450", decimal_arr.value_as_string(0));
9471 assert_eq!("2123460", decimal_arr.value_as_string(1));
9472 assert_eq!("3123460", decimal_arr.value_as_string(2));
9473 }
9474
9475 #[test]
9476 fn decimal128_min_max_to_f64() {
9477 let min128 = i128::MIN;
9479 let max128 = i128::MAX;
9480 assert_eq!(min128 as f64, min128 as f64);
9481 assert_eq!(max128 as f64, max128 as f64);
9482 }
9483
9484 #[test]
9485 fn test_cast_numeric_to_decimal128_negative() {
9486 let decimal_type = DataType::Decimal128(38, -1);
9487 let array = Arc::new(Int32Array::from(vec![
9488 Some(1123456),
9489 Some(2123456),
9490 Some(3123456),
9491 ])) as ArrayRef;
9492
9493 let casted_array = cast(&array, &decimal_type).unwrap();
9494 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9495
9496 assert_eq!("1123450", decimal_arr.value_as_string(0));
9497 assert_eq!("2123450", decimal_arr.value_as_string(1));
9498 assert_eq!("3123450", decimal_arr.value_as_string(2));
9499
9500 let array = Arc::new(Float32Array::from(vec![
9501 Some(1123.456),
9502 Some(2123.456),
9503 Some(3123.456),
9504 ])) as ArrayRef;
9505
9506 let casted_array = cast(&array, &decimal_type).unwrap();
9507 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9508
9509 assert_eq!("1120", decimal_arr.value_as_string(0));
9510 assert_eq!("2120", decimal_arr.value_as_string(1));
9511 assert_eq!("3120", decimal_arr.value_as_string(2));
9512 }
9513
9514 #[test]
9515 fn test_cast_decimal128_to_decimal128_negative() {
9516 let input_type = DataType::Decimal128(10, -1);
9517 let output_type = DataType::Decimal128(10, -2);
9518 assert!(can_cast_types(&input_type, &output_type));
9519 let array = vec![Some(123)];
9520 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9521 let array = Arc::new(input_decimal_array) as ArrayRef;
9522 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
9523
9524 let casted_array = cast(&array, &output_type).unwrap();
9525 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9526
9527 assert_eq!("1200", decimal_arr.value_as_string(0));
9528
9529 let array = vec![Some(125)];
9530 let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9531 let array = Arc::new(input_decimal_array) as ArrayRef;
9532 generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
9533
9534 let casted_array = cast(&array, &output_type).unwrap();
9535 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9536
9537 assert_eq!("1300", decimal_arr.value_as_string(0));
9538 }
9539
9540 #[test]
9541 fn test_cast_decimal128_to_decimal256_negative() {
9542 let input_type = DataType::Decimal128(10, 3);
9543 let output_type = DataType::Decimal256(10, 5);
9544 assert!(can_cast_types(&input_type, &output_type));
9545 let array = vec![Some(123456), Some(-123456)];
9546 let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
9547 let array = Arc::new(input_decimal_array) as ArrayRef;
9548
9549 let hundred = i256::from_i128(100);
9550 generate_cast_test_case!(
9551 &array,
9552 Decimal256Array,
9553 &output_type,
9554 vec![
9555 Some(i256::from_i128(123456).mul_wrapping(hundred)),
9556 Some(i256::from_i128(-123456).mul_wrapping(hundred))
9557 ]
9558 );
9559 }
9560
9561 #[test]
9562 fn test_parse_string_to_decimal() {
9563 assert_eq!(
9564 Decimal128Type::format_decimal(
9565 parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
9566 38,
9567 2,
9568 ),
9569 "123.45"
9570 );
9571 assert_eq!(
9572 Decimal128Type::format_decimal(
9573 parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
9574 38,
9575 2,
9576 ),
9577 "12345.00"
9578 );
9579 assert_eq!(
9580 Decimal128Type::format_decimal(
9581 parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
9582 38,
9583 2,
9584 ),
9585 "0.12"
9586 );
9587 assert_eq!(
9588 Decimal128Type::format_decimal(
9589 parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
9590 38,
9591 2,
9592 ),
9593 "0.12"
9594 );
9595 assert_eq!(
9596 Decimal128Type::format_decimal(
9597 parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9598 38,
9599 2,
9600 ),
9601 "0.13"
9602 );
9603 assert_eq!(
9604 Decimal128Type::format_decimal(
9605 parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9606 38,
9607 2,
9608 ),
9609 "0.13"
9610 );
9611
9612 assert_eq!(
9613 Decimal256Type::format_decimal(
9614 parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
9615 38,
9616 3,
9617 ),
9618 "123.450"
9619 );
9620 assert_eq!(
9621 Decimal256Type::format_decimal(
9622 parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
9623 38,
9624 3,
9625 ),
9626 "12345.000"
9627 );
9628 assert_eq!(
9629 Decimal256Type::format_decimal(
9630 parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
9631 38,
9632 3,
9633 ),
9634 "0.123"
9635 );
9636 assert_eq!(
9637 Decimal256Type::format_decimal(
9638 parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
9639 38,
9640 3,
9641 ),
9642 "0.123"
9643 );
9644 assert_eq!(
9645 Decimal256Type::format_decimal(
9646 parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
9647 38,
9648 3,
9649 ),
9650 "0.127"
9651 );
9652 }
9653
9654 fn test_cast_string_to_decimal(array: ArrayRef) {
9655 let output_type = DataType::Decimal128(38, 2);
9657 assert!(can_cast_types(array.data_type(), &output_type));
9658
9659 let casted_array = cast(&array, &output_type).unwrap();
9660 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9661
9662 assert_eq!("123.45", decimal_arr.value_as_string(0));
9663 assert_eq!("1.23", decimal_arr.value_as_string(1));
9664 assert_eq!("0.12", decimal_arr.value_as_string(2));
9665 assert_eq!("0.13", decimal_arr.value_as_string(3));
9666 assert_eq!("1.26", decimal_arr.value_as_string(4));
9667 assert_eq!("12345.00", decimal_arr.value_as_string(5));
9668 assert_eq!("12345.00", decimal_arr.value_as_string(6));
9669 assert_eq!("0.12", decimal_arr.value_as_string(7));
9670 assert_eq!("12.23", decimal_arr.value_as_string(8));
9671 assert!(decimal_arr.is_null(9));
9672 assert_eq!("0.00", decimal_arr.value_as_string(10));
9673 assert_eq!("0.00", decimal_arr.value_as_string(11));
9674 assert!(decimal_arr.is_null(12));
9675 assert_eq!("-1.23", decimal_arr.value_as_string(13));
9676 assert_eq!("-1.24", decimal_arr.value_as_string(14));
9677 assert_eq!("0.00", decimal_arr.value_as_string(15));
9678 assert_eq!("-123.00", decimal_arr.value_as_string(16));
9679 assert_eq!("-123.23", decimal_arr.value_as_string(17));
9680 assert_eq!("-0.12", decimal_arr.value_as_string(18));
9681 assert_eq!("1.23", decimal_arr.value_as_string(19));
9682 assert_eq!("1.24", decimal_arr.value_as_string(20));
9683 assert_eq!("0.00", decimal_arr.value_as_string(21));
9684 assert_eq!("123.00", decimal_arr.value_as_string(22));
9685 assert_eq!("123.23", decimal_arr.value_as_string(23));
9686 assert_eq!("0.12", decimal_arr.value_as_string(24));
9687 assert!(decimal_arr.is_null(25));
9688 assert!(decimal_arr.is_null(26));
9689 assert!(decimal_arr.is_null(27));
9690 assert_eq!("0.00", decimal_arr.value_as_string(28));
9691 assert_eq!("0.00", decimal_arr.value_as_string(29));
9692 assert_eq!("12345.00", decimal_arr.value_as_string(30));
9693 assert_eq!(decimal_arr.len(), 31);
9694
9695 let output_type = DataType::Decimal256(76, 3);
9697 assert!(can_cast_types(array.data_type(), &output_type));
9698
9699 let casted_array = cast(&array, &output_type).unwrap();
9700 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9701
9702 assert_eq!("123.450", decimal_arr.value_as_string(0));
9703 assert_eq!("1.235", decimal_arr.value_as_string(1));
9704 assert_eq!("0.123", decimal_arr.value_as_string(2));
9705 assert_eq!("0.127", decimal_arr.value_as_string(3));
9706 assert_eq!("1.263", decimal_arr.value_as_string(4));
9707 assert_eq!("12345.000", decimal_arr.value_as_string(5));
9708 assert_eq!("12345.000", decimal_arr.value_as_string(6));
9709 assert_eq!("0.123", decimal_arr.value_as_string(7));
9710 assert_eq!("12.234", decimal_arr.value_as_string(8));
9711 assert!(decimal_arr.is_null(9));
9712 assert_eq!("0.000", decimal_arr.value_as_string(10));
9713 assert_eq!("0.000", decimal_arr.value_as_string(11));
9714 assert!(decimal_arr.is_null(12));
9715 assert_eq!("-1.235", decimal_arr.value_as_string(13));
9716 assert_eq!("-1.236", decimal_arr.value_as_string(14));
9717 assert_eq!("0.000", decimal_arr.value_as_string(15));
9718 assert_eq!("-123.000", decimal_arr.value_as_string(16));
9719 assert_eq!("-123.234", decimal_arr.value_as_string(17));
9720 assert_eq!("-0.123", decimal_arr.value_as_string(18));
9721 assert_eq!("1.235", decimal_arr.value_as_string(19));
9722 assert_eq!("1.236", decimal_arr.value_as_string(20));
9723 assert_eq!("0.000", decimal_arr.value_as_string(21));
9724 assert_eq!("123.000", decimal_arr.value_as_string(22));
9725 assert_eq!("123.234", decimal_arr.value_as_string(23));
9726 assert_eq!("0.123", decimal_arr.value_as_string(24));
9727 assert!(decimal_arr.is_null(25));
9728 assert!(decimal_arr.is_null(26));
9729 assert!(decimal_arr.is_null(27));
9730 assert_eq!("0.000", decimal_arr.value_as_string(28));
9731 assert_eq!("0.000", decimal_arr.value_as_string(29));
9732 assert_eq!("12345.000", decimal_arr.value_as_string(30));
9733 assert_eq!(decimal_arr.len(), 31);
9734 }
9735
9736 #[test]
9737 fn test_cast_utf8_to_decimal() {
9738 let str_array = StringArray::from(vec![
9739 Some("123.45"),
9740 Some("1.2345"),
9741 Some("0.12345"),
9742 Some("0.1267"),
9743 Some("1.263"),
9744 Some("12345.0"),
9745 Some("12345"),
9746 Some("000.123"),
9747 Some("12.234000"),
9748 None,
9749 Some(""),
9750 Some(" "),
9751 None,
9752 Some("-1.23499999"),
9753 Some("-1.23599999"),
9754 Some("-0.00001"),
9755 Some("-123"),
9756 Some("-123.234000"),
9757 Some("-000.123"),
9758 Some("+1.23499999"),
9759 Some("+1.23599999"),
9760 Some("+0.00001"),
9761 Some("+123"),
9762 Some("+123.234000"),
9763 Some("+000.123"),
9764 Some("1.-23499999"),
9765 Some("-1.-23499999"),
9766 Some("--1.23499999"),
9767 Some("0"),
9768 Some("000.000"),
9769 Some("0000000000000000012345.000"),
9770 ]);
9771 let array = Arc::new(str_array) as ArrayRef;
9772
9773 test_cast_string_to_decimal(array);
9774
9775 let test_cases = [
9776 (None, None),
9777 (Some("0"), Some("0")),
9780 (Some("000.000"), Some("0")),
9781 (Some("12345"), Some("12345")),
9782 (Some("000000000000000000000000000012345"), Some("12345")),
9783 (Some("-123"), Some("-123")),
9784 (Some("+123"), Some("123")),
9785 ];
9786 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9787 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9788
9789 let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
9790 test_cast_string_to_decimal_scale_zero(array, &expected);
9791 }
9792
9793 #[test]
9794 fn test_cast_large_utf8_to_decimal() {
9795 let str_array = LargeStringArray::from(vec![
9796 Some("123.45"),
9797 Some("1.2345"),
9798 Some("0.12345"),
9799 Some("0.1267"),
9800 Some("1.263"),
9801 Some("12345.0"),
9802 Some("12345"),
9803 Some("000.123"),
9804 Some("12.234000"),
9805 None,
9806 Some(""),
9807 Some(" "),
9808 None,
9809 Some("-1.23499999"),
9810 Some("-1.23599999"),
9811 Some("-0.00001"),
9812 Some("-123"),
9813 Some("-123.234000"),
9814 Some("-000.123"),
9815 Some("+1.23499999"),
9816 Some("+1.23599999"),
9817 Some("+0.00001"),
9818 Some("+123"),
9819 Some("+123.234000"),
9820 Some("+000.123"),
9821 Some("1.-23499999"),
9822 Some("-1.-23499999"),
9823 Some("--1.23499999"),
9824 Some("0"),
9825 Some("000.000"),
9826 Some("0000000000000000012345.000"),
9827 ]);
9828 let array = Arc::new(str_array) as ArrayRef;
9829
9830 test_cast_string_to_decimal(array);
9831
9832 let test_cases = [
9833 (None, None),
9834 (Some(""), None),
9835 (Some(" "), None),
9836 (Some("0"), Some("0")),
9837 (Some("000.000"), Some("0")),
9838 (Some("12345"), Some("12345")),
9839 (Some("000000000000000000000000000012345"), Some("12345")),
9840 (Some("-123"), Some("-123")),
9841 (Some("+123"), Some("123")),
9842 ];
9843 let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9844 let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9845
9846 let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
9847 test_cast_string_to_decimal_scale_zero(array, &expected);
9848 }
9849
9850 fn test_cast_string_to_decimal_scale_zero(
9851 array: ArrayRef,
9852 expected_as_string: &[Option<&str>],
9853 ) {
9854 let output_type = DataType::Decimal128(38, 0);
9856 assert!(can_cast_types(array.data_type(), &output_type));
9857 let casted_array = cast(&array, &output_type).unwrap();
9858 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9859 assert_decimal_array_contents(decimal_arr, expected_as_string);
9860
9861 let output_type = DataType::Decimal256(76, 0);
9863 assert!(can_cast_types(array.data_type(), &output_type));
9864 let casted_array = cast(&array, &output_type).unwrap();
9865 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9866 assert_decimal_array_contents(decimal_arr, expected_as_string);
9867 }
9868
9869 fn assert_decimal_array_contents<T>(
9870 array: &PrimitiveArray<T>,
9871 expected_as_string: &[Option<&str>],
9872 ) where
9873 T: DecimalType + ArrowPrimitiveType,
9874 {
9875 assert_eq!(array.len(), expected_as_string.len());
9876 for (i, expected) in expected_as_string.iter().enumerate() {
9877 let actual = if array.is_null(i) {
9878 None
9879 } else {
9880 Some(array.value_as_string(i))
9881 };
9882 let actual = actual.as_ref().map(|s| s.as_ref());
9883 assert_eq!(*expected, actual, "Expected at position {i}");
9884 }
9885 }
9886
9887 #[test]
9888 fn test_cast_invalid_utf8_to_decimal() {
9889 let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9890 let array = Arc::new(str_array) as ArrayRef;
9891
9892 let output_type = DataType::Decimal128(38, 2);
9894 let casted_array = cast(&array, &output_type).unwrap();
9895 assert!(casted_array.is_null(0));
9896 assert!(casted_array.is_null(1));
9897
9898 let output_type = DataType::Decimal256(76, 2);
9899 let casted_array = cast(&array, &output_type).unwrap();
9900 assert!(casted_array.is_null(0));
9901 assert!(casted_array.is_null(1));
9902
9903 let output_type = DataType::Decimal128(38, 2);
9905 let str_array = StringArray::from(vec!["4.4.5"]);
9906 let array = Arc::new(str_array) as ArrayRef;
9907 let option = CastOptions {
9908 safe: false,
9909 format_options: FormatOptions::default(),
9910 };
9911 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9912 assert!(
9913 casted_err
9914 .to_string()
9915 .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
9916 );
9917
9918 let str_array = StringArray::from(vec![". 0.123"]);
9919 let array = Arc::new(str_array) as ArrayRef;
9920 let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9921 assert!(
9922 casted_err
9923 .to_string()
9924 .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
9925 );
9926 }
9927
9928 fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
9929 let output_type = DataType::Decimal128(38, 2);
9930 let casted_array = cast(&overflow_array, &output_type).unwrap();
9931 let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9932
9933 assert!(decimal_arr.is_null(0));
9934 assert!(decimal_arr.is_null(1));
9935 assert!(decimal_arr.is_null(2));
9936 assert_eq!(
9937 "999999999999999999999999999999999999.99",
9938 decimal_arr.value_as_string(3)
9939 );
9940 assert_eq!(
9941 "100000000000000000000000000000000000.00",
9942 decimal_arr.value_as_string(4)
9943 );
9944 }
9945
9946 #[test]
9947 fn test_cast_string_to_decimal128_precision_overflow() {
9948 let array = StringArray::from(vec!["1000".to_string()]);
9949 let array = Arc::new(array) as ArrayRef;
9950 let casted_array = cast_with_options(
9951 &array,
9952 &DataType::Decimal128(10, 8),
9953 &CastOptions {
9954 safe: true,
9955 format_options: FormatOptions::default(),
9956 },
9957 );
9958 assert!(casted_array.is_ok());
9959 assert!(casted_array.unwrap().is_null(0));
9960
9961 let err = cast_with_options(
9962 &array,
9963 &DataType::Decimal128(10, 8),
9964 &CastOptions {
9965 safe: false,
9966 format_options: FormatOptions::default(),
9967 },
9968 );
9969 assert_eq!(
9970 "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
9971 err.unwrap_err().to_string()
9972 );
9973 }
9974
9975 #[test]
9976 fn test_cast_utf8_to_decimal128_overflow() {
9977 let overflow_str_array = StringArray::from(vec![
9978 i128::MAX.to_string(),
9979 i128::MIN.to_string(),
9980 "99999999999999999999999999999999999999".to_string(),
9981 "999999999999999999999999999999999999.99".to_string(),
9982 "99999999999999999999999999999999999.999".to_string(),
9983 ]);
9984 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9985
9986 test_cast_string_to_decimal128_overflow(overflow_array);
9987 }
9988
9989 #[test]
9990 fn test_cast_large_utf8_to_decimal128_overflow() {
9991 let overflow_str_array = LargeStringArray::from(vec![
9992 i128::MAX.to_string(),
9993 i128::MIN.to_string(),
9994 "99999999999999999999999999999999999999".to_string(),
9995 "999999999999999999999999999999999999.99".to_string(),
9996 "99999999999999999999999999999999999.999".to_string(),
9997 ]);
9998 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9999
10000 test_cast_string_to_decimal128_overflow(overflow_array);
10001 }
10002
10003 fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
10004 let output_type = DataType::Decimal256(76, 2);
10005 let casted_array = cast(&overflow_array, &output_type).unwrap();
10006 let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10007
10008 assert_eq!(
10009 "170141183460469231731687303715884105727.00",
10010 decimal_arr.value_as_string(0)
10011 );
10012 assert_eq!(
10013 "-170141183460469231731687303715884105728.00",
10014 decimal_arr.value_as_string(1)
10015 );
10016 assert_eq!(
10017 "99999999999999999999999999999999999999.00",
10018 decimal_arr.value_as_string(2)
10019 );
10020 assert_eq!(
10021 "999999999999999999999999999999999999.99",
10022 decimal_arr.value_as_string(3)
10023 );
10024 assert_eq!(
10025 "100000000000000000000000000000000000.00",
10026 decimal_arr.value_as_string(4)
10027 );
10028 assert!(decimal_arr.is_null(5));
10029 assert!(decimal_arr.is_null(6));
10030 }
10031
10032 #[test]
10033 fn test_cast_string_to_decimal256_precision_overflow() {
10034 let array = StringArray::from(vec!["1000".to_string()]);
10035 let array = Arc::new(array) as ArrayRef;
10036 let casted_array = cast_with_options(
10037 &array,
10038 &DataType::Decimal256(10, 8),
10039 &CastOptions {
10040 safe: true,
10041 format_options: FormatOptions::default(),
10042 },
10043 );
10044 assert!(casted_array.is_ok());
10045 assert!(casted_array.unwrap().is_null(0));
10046
10047 let err = cast_with_options(
10048 &array,
10049 &DataType::Decimal256(10, 8),
10050 &CastOptions {
10051 safe: false,
10052 format_options: FormatOptions::default(),
10053 },
10054 );
10055 assert_eq!(
10056 "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
10057 err.unwrap_err().to_string()
10058 );
10059 }
10060
10061 #[test]
10062 fn test_cast_utf8_to_decimal256_overflow() {
10063 let overflow_str_array = StringArray::from(vec![
10064 i128::MAX.to_string(),
10065 i128::MIN.to_string(),
10066 "99999999999999999999999999999999999999".to_string(),
10067 "999999999999999999999999999999999999.99".to_string(),
10068 "99999999999999999999999999999999999.999".to_string(),
10069 i256::MAX.to_string(),
10070 i256::MIN.to_string(),
10071 ]);
10072 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10073
10074 test_cast_string_to_decimal256_overflow(overflow_array);
10075 }
10076
10077 #[test]
10078 fn test_cast_large_utf8_to_decimal256_overflow() {
10079 let overflow_str_array = LargeStringArray::from(vec![
10080 i128::MAX.to_string(),
10081 i128::MIN.to_string(),
10082 "99999999999999999999999999999999999999".to_string(),
10083 "999999999999999999999999999999999999.99".to_string(),
10084 "99999999999999999999999999999999999.999".to_string(),
10085 i256::MAX.to_string(),
10086 i256::MIN.to_string(),
10087 ]);
10088 let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10089
10090 test_cast_string_to_decimal256_overflow(overflow_array);
10091 }
10092
10093 #[test]
10094 fn test_cast_outside_supported_range_for_nanoseconds() {
10095 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";
10096
10097 let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
10098
10099 let cast_options = CastOptions {
10100 safe: false,
10101 format_options: FormatOptions::default(),
10102 };
10103
10104 let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
10105 &array,
10106 &None::<Arc<str>>,
10107 &cast_options,
10108 );
10109
10110 let err = result.unwrap_err();
10111 assert_eq!(
10112 err.to_string(),
10113 format!(
10114 "Cast error: Overflow converting {} to Nanosecond. {}",
10115 array.value(0),
10116 EXPECTED_ERROR_MESSAGE
10117 )
10118 );
10119 }
10120
10121 #[test]
10122 fn test_cast_date32_to_timestamp() {
10123 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
10125 let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10126 let c = b.as_primitive::<TimestampSecondType>();
10127 assert_eq!(1609459200, c.value(0));
10128 assert_eq!(1640995200, c.value(1));
10129 assert!(c.is_null(2));
10130 }
10131
10132 #[test]
10133 fn test_cast_date32_to_timestamp_ms() {
10134 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
10136 let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10137 let c = b
10138 .as_any()
10139 .downcast_ref::<TimestampMillisecondArray>()
10140 .unwrap();
10141 assert_eq!(1609459200000, c.value(0));
10142 assert_eq!(1640995200000, c.value(1));
10143 assert!(c.is_null(2));
10144 }
10145
10146 #[test]
10147 fn test_cast_date32_to_timestamp_us() {
10148 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
10150 let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10151 let c = b
10152 .as_any()
10153 .downcast_ref::<TimestampMicrosecondArray>()
10154 .unwrap();
10155 assert_eq!(1609459200000000, c.value(0));
10156 assert_eq!(1640995200000000, c.value(1));
10157 assert!(c.is_null(2));
10158 }
10159
10160 #[test]
10161 fn test_cast_date32_to_timestamp_ns() {
10162 let a = Date32Array::from(vec![Some(18628), Some(18993), None]); let array = Arc::new(a) as ArrayRef;
10164 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10165 let c = b
10166 .as_any()
10167 .downcast_ref::<TimestampNanosecondArray>()
10168 .unwrap();
10169 assert_eq!(1609459200000000000, c.value(0));
10170 assert_eq!(1640995200000000000, c.value(1));
10171 assert!(c.is_null(2));
10172 }
10173
10174 #[test]
10175 fn test_timezone_cast() {
10176 let a = StringArray::from(vec![
10177 "2000-01-01T12:00:00", "2020-12-15T12:34:56", ]);
10180 let array = Arc::new(a) as ArrayRef;
10181 let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10182 let v = b.as_primitive::<TimestampNanosecondType>();
10183
10184 assert_eq!(v.value(0), 946728000000000000);
10185 assert_eq!(v.value(1), 1608035696000000000);
10186
10187 let b = cast(
10188 &b,
10189 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10190 )
10191 .unwrap();
10192 let v = b.as_primitive::<TimestampNanosecondType>();
10193
10194 assert_eq!(v.value(0), 946728000000000000);
10195 assert_eq!(v.value(1), 1608035696000000000);
10196
10197 let b = cast(
10198 &b,
10199 &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10200 )
10201 .unwrap();
10202 let v = b.as_primitive::<TimestampMillisecondType>();
10203
10204 assert_eq!(v.value(0), 946728000000);
10205 assert_eq!(v.value(1), 1608035696000);
10206 }
10207
10208 #[test]
10209 fn test_cast_utf8_to_timestamp() {
10210 fn test_tz(tz: Arc<str>) {
10211 let valid = StringArray::from(vec![
10212 "2023-01-01 04:05:06.789000-08:00",
10213 "2023-01-01 04:05:06.789000-07:00",
10214 "2023-01-01 04:05:06.789 -0800",
10215 "2023-01-01 04:05:06.789 -08:00",
10216 "2023-01-01 040506 +0730",
10217 "2023-01-01 040506 +07:30",
10218 "2023-01-01 04:05:06.789",
10219 "2023-01-01 04:05:06",
10220 "2023-01-01",
10221 ]);
10222
10223 let array = Arc::new(valid) as ArrayRef;
10224 let b = cast_with_options(
10225 &array,
10226 &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
10227 &CastOptions {
10228 safe: false,
10229 format_options: FormatOptions::default(),
10230 },
10231 )
10232 .unwrap();
10233
10234 let tz = tz.as_ref().parse().unwrap();
10235
10236 let as_tz =
10237 |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
10238
10239 let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
10240 let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
10241
10242 let values = b.as_primitive::<TimestampNanosecondType>().values();
10243 let utc_results: Vec<_> = values.iter().map(as_utc).collect();
10244 let local_results: Vec<_> = values.iter().map(as_local).collect();
10245
10246 assert_eq!(
10248 &utc_results[..6],
10249 &[
10250 "2023-01-01 12:05:06.789".to_string(),
10251 "2023-01-01 11:05:06.789".to_string(),
10252 "2023-01-01 12:05:06.789".to_string(),
10253 "2023-01-01 12:05:06.789".to_string(),
10254 "2022-12-31 20:35:06".to_string(),
10255 "2022-12-31 20:35:06".to_string(),
10256 ]
10257 );
10258 assert_eq!(
10260 &local_results[6..],
10261 &[
10262 "2023-01-01 04:05:06.789".to_string(),
10263 "2023-01-01 04:05:06".to_string(),
10264 "2023-01-01 00:00:00".to_string()
10265 ]
10266 )
10267 }
10268
10269 test_tz("+00:00".into());
10270 test_tz("+02:00".into());
10271 }
10272
10273 #[test]
10274 fn test_cast_invalid_utf8() {
10275 let v1: &[u8] = b"\xFF invalid";
10276 let v2: &[u8] = b"\x00 Foo";
10277 let s = BinaryArray::from(vec![v1, v2]);
10278 let options = CastOptions {
10279 safe: true,
10280 format_options: FormatOptions::default(),
10281 };
10282 let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
10283 let a = array.as_string::<i32>();
10284 a.to_data().validate_full().unwrap();
10285
10286 assert_eq!(a.null_count(), 1);
10287 assert_eq!(a.len(), 2);
10288 assert!(a.is_null(0));
10289 assert_eq!(a.value(0), "");
10290 assert_eq!(a.value(1), "\x00 Foo");
10291 }
10292
10293 #[test]
10294 fn test_cast_utf8_to_timestamptz() {
10295 let valid = StringArray::from(vec!["2023-01-01"]);
10296
10297 let array = Arc::new(valid) as ArrayRef;
10298 let b = cast(
10299 &array,
10300 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10301 )
10302 .unwrap();
10303
10304 let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
10305
10306 assert_eq!(b.data_type(), &expect);
10307 let c = b
10308 .as_any()
10309 .downcast_ref::<TimestampNanosecondArray>()
10310 .unwrap();
10311 assert_eq!(1672531200000000000, c.value(0));
10312 }
10313
10314 #[test]
10315 fn test_cast_decimal_to_string() {
10316 assert!(can_cast_types(
10317 &DataType::Decimal32(9, 4),
10318 &DataType::Utf8View
10319 ));
10320 assert!(can_cast_types(
10321 &DataType::Decimal64(16, 4),
10322 &DataType::Utf8View
10323 ));
10324 assert!(can_cast_types(
10325 &DataType::Decimal128(10, 4),
10326 &DataType::Utf8View
10327 ));
10328 assert!(can_cast_types(
10329 &DataType::Decimal256(38, 10),
10330 &DataType::Utf8View
10331 ));
10332
10333 macro_rules! assert_decimal_values {
10334 ($array:expr) => {
10335 let c = $array;
10336 assert_eq!("1123.454", c.value(0));
10337 assert_eq!("2123.456", c.value(1));
10338 assert_eq!("-3123.453", c.value(2));
10339 assert_eq!("-3123.456", c.value(3));
10340 assert_eq!("0.000", c.value(4));
10341 assert_eq!("0.123", c.value(5));
10342 assert_eq!("1234.567", c.value(6));
10343 assert_eq!("-1234.567", c.value(7));
10344 assert!(c.is_null(8));
10345 };
10346 }
10347
10348 fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
10349 output_type: DataType,
10350 array: PrimitiveArray<IN>,
10351 ) {
10352 let b = cast(&array, &output_type).unwrap();
10353
10354 assert_eq!(b.data_type(), &output_type);
10355 match b.data_type() {
10356 DataType::Utf8View => {
10357 let c = b.as_string_view();
10358 assert_decimal_values!(c);
10359 }
10360 DataType::Utf8 | DataType::LargeUtf8 => {
10361 let c = b.as_string::<OffsetSize>();
10362 assert_decimal_values!(c);
10363 }
10364 _ => (),
10365 }
10366 }
10367
10368 let array32: Vec<Option<i32>> = vec![
10369 Some(1123454),
10370 Some(2123456),
10371 Some(-3123453),
10372 Some(-3123456),
10373 Some(0),
10374 Some(123),
10375 Some(123456789),
10376 Some(-123456789),
10377 None,
10378 ];
10379 let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
10380 let array128: Vec<Option<i128>> =
10381 array64.iter().map(|num| num.map(|x| x as i128)).collect();
10382 let array256: Vec<Option<i256>> = array128
10383 .iter()
10384 .map(|num| num.map(i256::from_i128))
10385 .collect();
10386
10387 test_decimal_to_string::<Decimal32Type, i32>(
10388 DataType::Utf8View,
10389 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10390 );
10391 test_decimal_to_string::<Decimal32Type, i32>(
10392 DataType::Utf8,
10393 create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10394 );
10395 test_decimal_to_string::<Decimal32Type, i64>(
10396 DataType::LargeUtf8,
10397 create_decimal32_array(array32, 7, 3).unwrap(),
10398 );
10399
10400 test_decimal_to_string::<Decimal64Type, i32>(
10401 DataType::Utf8View,
10402 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10403 );
10404 test_decimal_to_string::<Decimal64Type, i32>(
10405 DataType::Utf8,
10406 create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10407 );
10408 test_decimal_to_string::<Decimal64Type, i64>(
10409 DataType::LargeUtf8,
10410 create_decimal64_array(array64, 7, 3).unwrap(),
10411 );
10412
10413 test_decimal_to_string::<Decimal128Type, i32>(
10414 DataType::Utf8View,
10415 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10416 );
10417 test_decimal_to_string::<Decimal128Type, i32>(
10418 DataType::Utf8,
10419 create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10420 );
10421 test_decimal_to_string::<Decimal128Type, i64>(
10422 DataType::LargeUtf8,
10423 create_decimal128_array(array128, 7, 3).unwrap(),
10424 );
10425
10426 test_decimal_to_string::<Decimal256Type, i32>(
10427 DataType::Utf8View,
10428 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10429 );
10430 test_decimal_to_string::<Decimal256Type, i32>(
10431 DataType::Utf8,
10432 create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10433 );
10434 test_decimal_to_string::<Decimal256Type, i64>(
10435 DataType::LargeUtf8,
10436 create_decimal256_array(array256, 7, 3).unwrap(),
10437 );
10438 }
10439
10440 #[test]
10441 fn test_cast_numeric_to_decimal128_precision_overflow() {
10442 let array = Int64Array::from(vec![1234567]);
10443 let array = Arc::new(array) as ArrayRef;
10444 let casted_array = cast_with_options(
10445 &array,
10446 &DataType::Decimal128(7, 3),
10447 &CastOptions {
10448 safe: true,
10449 format_options: FormatOptions::default(),
10450 },
10451 );
10452 assert!(casted_array.is_ok());
10453 assert!(casted_array.unwrap().is_null(0));
10454
10455 let err = cast_with_options(
10456 &array,
10457 &DataType::Decimal128(7, 3),
10458 &CastOptions {
10459 safe: false,
10460 format_options: FormatOptions::default(),
10461 },
10462 );
10463 assert_eq!(
10464 "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
10465 err.unwrap_err().to_string()
10466 );
10467 }
10468
10469 #[test]
10470 fn test_cast_numeric_to_decimal256_precision_overflow() {
10471 let array = Int64Array::from(vec![1234567]);
10472 let array = Arc::new(array) as ArrayRef;
10473 let casted_array = cast_with_options(
10474 &array,
10475 &DataType::Decimal256(7, 3),
10476 &CastOptions {
10477 safe: true,
10478 format_options: FormatOptions::default(),
10479 },
10480 );
10481 assert!(casted_array.is_ok());
10482 assert!(casted_array.unwrap().is_null(0));
10483
10484 let err = cast_with_options(
10485 &array,
10486 &DataType::Decimal256(7, 3),
10487 &CastOptions {
10488 safe: false,
10489 format_options: FormatOptions::default(),
10490 },
10491 );
10492 assert_eq!(
10493 "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
10494 err.unwrap_err().to_string()
10495 );
10496 }
10497
10498 fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
10500 array: Vec<i64>,
10501 cast_options: &CastOptions,
10502 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10503 let array = PrimitiveArray::<T>::new(array.into(), None);
10504 let array = Arc::new(array) as ArrayRef;
10505 let interval = DataType::Interval(IntervalUnit::MonthDayNano);
10506 let out = cast_with_options(&array, &interval, cast_options)?;
10507 let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
10508 Ok(out)
10509 }
10510
10511 #[test]
10512 fn test_cast_from_duration_to_interval() {
10513 let array = vec![1234567];
10515 let casted_array =
10516 cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
10517 .unwrap();
10518 assert_eq!(
10519 casted_array.data_type(),
10520 &DataType::Interval(IntervalUnit::MonthDayNano)
10521 );
10522 assert_eq!(
10523 casted_array.value(0),
10524 IntervalMonthDayNano::new(0, 0, 1234567000000000)
10525 );
10526
10527 let array = vec![i64::MAX];
10528 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10529 array.clone(),
10530 &CastOptions::default(),
10531 )
10532 .unwrap();
10533 assert!(!casted_array.is_valid(0));
10534
10535 let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10536 array,
10537 &CastOptions {
10538 safe: false,
10539 format_options: FormatOptions::default(),
10540 },
10541 );
10542 assert!(casted_array.is_err());
10543
10544 let array = vec![1234567];
10546 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10547 array,
10548 &CastOptions::default(),
10549 )
10550 .unwrap();
10551 assert_eq!(
10552 casted_array.data_type(),
10553 &DataType::Interval(IntervalUnit::MonthDayNano)
10554 );
10555 assert_eq!(
10556 casted_array.value(0),
10557 IntervalMonthDayNano::new(0, 0, 1234567000000)
10558 );
10559
10560 let array = vec![i64::MAX];
10561 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10562 array.clone(),
10563 &CastOptions::default(),
10564 )
10565 .unwrap();
10566 assert!(!casted_array.is_valid(0));
10567
10568 let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10569 array,
10570 &CastOptions {
10571 safe: false,
10572 format_options: FormatOptions::default(),
10573 },
10574 );
10575 assert!(casted_array.is_err());
10576
10577 let array = vec![1234567];
10579 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10580 array,
10581 &CastOptions::default(),
10582 )
10583 .unwrap();
10584 assert_eq!(
10585 casted_array.data_type(),
10586 &DataType::Interval(IntervalUnit::MonthDayNano)
10587 );
10588 assert_eq!(
10589 casted_array.value(0),
10590 IntervalMonthDayNano::new(0, 0, 1234567000)
10591 );
10592
10593 let array = vec![i64::MAX];
10594 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10595 array.clone(),
10596 &CastOptions::default(),
10597 )
10598 .unwrap();
10599 assert!(!casted_array.is_valid(0));
10600
10601 let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10602 array,
10603 &CastOptions {
10604 safe: false,
10605 format_options: FormatOptions::default(),
10606 },
10607 );
10608 assert!(casted_array.is_err());
10609
10610 let array = vec![1234567];
10612 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10613 array,
10614 &CastOptions::default(),
10615 )
10616 .unwrap();
10617 assert_eq!(
10618 casted_array.data_type(),
10619 &DataType::Interval(IntervalUnit::MonthDayNano)
10620 );
10621 assert_eq!(
10622 casted_array.value(0),
10623 IntervalMonthDayNano::new(0, 0, 1234567)
10624 );
10625
10626 let array = vec![i64::MAX];
10627 let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10628 array,
10629 &CastOptions {
10630 safe: false,
10631 format_options: FormatOptions::default(),
10632 },
10633 )
10634 .unwrap();
10635 assert_eq!(
10636 casted_array.value(0),
10637 IntervalMonthDayNano::new(0, 0, i64::MAX)
10638 );
10639 }
10640
10641 fn cast_from_interval_to_duration<T: ArrowTemporalType>(
10643 array: &IntervalMonthDayNanoArray,
10644 cast_options: &CastOptions,
10645 ) -> Result<PrimitiveArray<T>, ArrowError> {
10646 let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
10647 casted_array
10648 .as_any()
10649 .downcast_ref::<PrimitiveArray<T>>()
10650 .ok_or_else(|| {
10651 ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
10652 })
10653 .cloned()
10654 }
10655
10656 #[test]
10657 fn test_cast_from_interval_to_duration() {
10658 let nullable = CastOptions::default();
10659 let fallible = CastOptions {
10660 safe: false,
10661 format_options: FormatOptions::default(),
10662 };
10663 let v = IntervalMonthDayNano::new(0, 0, 1234567);
10664
10665 let array = vec![v].into();
10667 let casted_array: DurationSecondArray =
10668 cast_from_interval_to_duration(&array, &nullable).unwrap();
10669 assert_eq!(casted_array.value(0), 0);
10670
10671 let array = vec![IntervalMonthDayNano::MAX].into();
10672 let casted_array: DurationSecondArray =
10673 cast_from_interval_to_duration(&array, &nullable).unwrap();
10674 assert!(!casted_array.is_valid(0));
10675
10676 let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
10677 assert!(res.is_err());
10678
10679 let array = vec![v].into();
10681 let casted_array: DurationMillisecondArray =
10682 cast_from_interval_to_duration(&array, &nullable).unwrap();
10683 assert_eq!(casted_array.value(0), 1);
10684
10685 let array = vec![IntervalMonthDayNano::MAX].into();
10686 let casted_array: DurationMillisecondArray =
10687 cast_from_interval_to_duration(&array, &nullable).unwrap();
10688 assert!(!casted_array.is_valid(0));
10689
10690 let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
10691 assert!(res.is_err());
10692
10693 let array = vec![v].into();
10695 let casted_array: DurationMicrosecondArray =
10696 cast_from_interval_to_duration(&array, &nullable).unwrap();
10697 assert_eq!(casted_array.value(0), 1234);
10698
10699 let array = vec![IntervalMonthDayNano::MAX].into();
10700 let casted_array =
10701 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
10702 assert!(!casted_array.is_valid(0));
10703
10704 let casted_array =
10705 cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
10706 assert!(casted_array.is_err());
10707
10708 let array = vec![v].into();
10710 let casted_array: DurationNanosecondArray =
10711 cast_from_interval_to_duration(&array, &nullable).unwrap();
10712 assert_eq!(casted_array.value(0), 1234567);
10713
10714 let array = vec![IntervalMonthDayNano::MAX].into();
10715 let casted_array: DurationNanosecondArray =
10716 cast_from_interval_to_duration(&array, &nullable).unwrap();
10717 assert!(!casted_array.is_valid(0));
10718
10719 let casted_array =
10720 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
10721 assert!(casted_array.is_err());
10722
10723 let array = vec![
10724 IntervalMonthDayNanoType::make_value(0, 1, 0),
10725 IntervalMonthDayNanoType::make_value(-1, 0, 0),
10726 IntervalMonthDayNanoType::make_value(1, 1, 0),
10727 IntervalMonthDayNanoType::make_value(1, 0, 1),
10728 IntervalMonthDayNanoType::make_value(0, 0, -1),
10729 ]
10730 .into();
10731 let casted_array =
10732 cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
10733 assert!(!casted_array.is_valid(0));
10734 assert!(!casted_array.is_valid(1));
10735 assert!(!casted_array.is_valid(2));
10736 assert!(!casted_array.is_valid(3));
10737 assert!(casted_array.is_valid(4));
10738 assert_eq!(casted_array.value(4), -1);
10739 }
10740
10741 fn cast_from_interval_year_month_to_interval_month_day_nano(
10743 array: Vec<i32>,
10744 cast_options: &CastOptions,
10745 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10746 let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
10747 let array = Arc::new(array) as ArrayRef;
10748 let casted_array = cast_with_options(
10749 &array,
10750 &DataType::Interval(IntervalUnit::MonthDayNano),
10751 cast_options,
10752 )?;
10753 casted_array
10754 .as_any()
10755 .downcast_ref::<IntervalMonthDayNanoArray>()
10756 .ok_or_else(|| {
10757 ArrowError::ComputeError(
10758 "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
10759 )
10760 })
10761 .cloned()
10762 }
10763
10764 #[test]
10765 fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
10766 let array = vec![1234567];
10768 let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
10769 array,
10770 &CastOptions::default(),
10771 )
10772 .unwrap();
10773 assert_eq!(
10774 casted_array.data_type(),
10775 &DataType::Interval(IntervalUnit::MonthDayNano)
10776 );
10777 assert_eq!(
10778 casted_array.value(0),
10779 IntervalMonthDayNano::new(1234567, 0, 0)
10780 );
10781 }
10782
10783 fn cast_from_interval_day_time_to_interval_month_day_nano(
10785 array: Vec<IntervalDayTime>,
10786 cast_options: &CastOptions,
10787 ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10788 let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
10789 let array = Arc::new(array) as ArrayRef;
10790 let casted_array = cast_with_options(
10791 &array,
10792 &DataType::Interval(IntervalUnit::MonthDayNano),
10793 cast_options,
10794 )?;
10795 Ok(casted_array
10796 .as_primitive::<IntervalMonthDayNanoType>()
10797 .clone())
10798 }
10799
10800 #[test]
10801 fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
10802 let array = vec![IntervalDayTime::new(123, 0)];
10804 let casted_array =
10805 cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
10806 .unwrap();
10807 assert_eq!(
10808 casted_array.data_type(),
10809 &DataType::Interval(IntervalUnit::MonthDayNano)
10810 );
10811 assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
10812 }
10813
10814 #[test]
10815 fn test_cast_below_unixtimestamp() {
10816 let valid = StringArray::from(vec![
10817 "1900-01-03 23:59:59",
10818 "1969-12-31 00:00:01",
10819 "1989-12-31 00:00:01",
10820 ]);
10821
10822 let array = Arc::new(valid) as ArrayRef;
10823 let casted_array = cast_with_options(
10824 &array,
10825 &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10826 &CastOptions {
10827 safe: false,
10828 format_options: FormatOptions::default(),
10829 },
10830 )
10831 .unwrap();
10832
10833 let ts_array = casted_array
10834 .as_primitive::<TimestampNanosecondType>()
10835 .values()
10836 .iter()
10837 .map(|ts| ts / 1_000_000)
10838 .collect::<Vec<_>>();
10839
10840 let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
10841 let casted_array = cast(&array, &DataType::Date32).unwrap();
10842 let date_array = casted_array.as_primitive::<Date32Type>();
10843 let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
10844 let string_array = casted_array.as_string::<i32>();
10845 assert_eq!("1900-01-03", string_array.value(0));
10846 assert_eq!("1969-12-31", string_array.value(1));
10847 assert_eq!("1989-12-31", string_array.value(2));
10848 }
10849
10850 #[test]
10851 fn test_nested_list() {
10852 let mut list = ListBuilder::new(Int32Builder::new());
10853 list.append_value([Some(1), Some(2), Some(3)]);
10854 list.append_value([Some(4), None, Some(6)]);
10855 let list = list.finish();
10856
10857 let to_field = Field::new("nested", list.data_type().clone(), false);
10858 let to = DataType::List(Arc::new(to_field));
10859 let out = cast(&list, &to).unwrap();
10860 let opts = FormatOptions::default().with_null("null");
10861 let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
10862
10863 assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
10864 assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
10865 }
10866
10867 #[test]
10868 fn test_nested_list_cast() {
10869 let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
10870 builder.append_value([Some([Some(1), Some(2), None]), None]);
10871 builder.append_value([None, Some([]), None]);
10872 builder.append_null();
10873 builder.append_value([Some([Some(2), Some(3)])]);
10874 let start = builder.finish();
10875
10876 let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
10877 builder.append_value([Some([Some(1), Some(2), None]), None]);
10878 builder.append_value([None, Some([]), None]);
10879 builder.append_null();
10880 builder.append_value([Some([Some(2), Some(3)])]);
10881 let expected = builder.finish();
10882
10883 let actual = cast(&start, expected.data_type()).unwrap();
10884 assert_eq!(actual.as_ref(), &expected);
10885 }
10886
10887 const CAST_OPTIONS: CastOptions<'static> = CastOptions {
10888 safe: true,
10889 format_options: FormatOptions::new(),
10890 };
10891
10892 #[test]
10893 #[allow(clippy::assertions_on_constants)]
10894 fn test_const_options() {
10895 assert!(CAST_OPTIONS.safe)
10896 }
10897
10898 #[test]
10899 fn test_list_format_options() {
10900 let options = CastOptions {
10901 safe: false,
10902 format_options: FormatOptions::default().with_null("null"),
10903 };
10904 let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10905 Some(vec![Some(0), Some(1), Some(2)]),
10906 Some(vec![Some(0), None, Some(2)]),
10907 ]);
10908 let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
10909 let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
10910 assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
10911 }
10912 #[test]
10913 fn test_cast_string_to_timestamp_invalid_tz() {
10914 let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
10916 let array = StringArray::from(vec![Some(bad_timestamp)]);
10917
10918 let data_types = [
10919 DataType::Timestamp(TimeUnit::Second, None),
10920 DataType::Timestamp(TimeUnit::Millisecond, None),
10921 DataType::Timestamp(TimeUnit::Microsecond, None),
10922 DataType::Timestamp(TimeUnit::Nanosecond, None),
10923 ];
10924
10925 let cast_options = CastOptions {
10926 safe: false,
10927 ..Default::default()
10928 };
10929
10930 for dt in data_types {
10931 assert_eq!(
10932 cast_with_options(&array, &dt, &cast_options)
10933 .unwrap_err()
10934 .to_string(),
10935 "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
10936 );
10937 }
10938 }
10939 #[test]
10940 fn test_cast_struct_to_struct() {
10941 let struct_type = DataType::Struct(
10942 vec![
10943 Field::new("a", DataType::Boolean, false),
10944 Field::new("b", DataType::Int32, false),
10945 ]
10946 .into(),
10947 );
10948 let to_type = DataType::Struct(
10949 vec![
10950 Field::new("a", DataType::Utf8, false),
10951 Field::new("b", DataType::Utf8, false),
10952 ]
10953 .into(),
10954 );
10955 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10956 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
10957 let struct_array = StructArray::from(vec![
10958 (
10959 Arc::new(Field::new("b", DataType::Boolean, false)),
10960 boolean.clone() as ArrayRef,
10961 ),
10962 (
10963 Arc::new(Field::new("c", DataType::Int32, false)),
10964 int.clone() as ArrayRef,
10965 ),
10966 ]);
10967 let casted_array = cast(&struct_array, &to_type).unwrap();
10968 let casted_array = casted_array.as_struct();
10969 assert_eq!(casted_array.data_type(), &to_type);
10970 let casted_boolean_array = casted_array
10971 .column(0)
10972 .as_string::<i32>()
10973 .into_iter()
10974 .flatten()
10975 .collect::<Vec<_>>();
10976 let casted_int_array = casted_array
10977 .column(1)
10978 .as_string::<i32>()
10979 .into_iter()
10980 .flatten()
10981 .collect::<Vec<_>>();
10982 assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
10983 assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
10984
10985 let to_type = DataType::Struct(
10987 vec![
10988 Field::new("a", DataType::Date32, false),
10989 Field::new("b", DataType::Utf8, false),
10990 ]
10991 .into(),
10992 );
10993 assert!(!can_cast_types(&struct_type, &to_type));
10994 let result = cast(&struct_array, &to_type);
10995 assert_eq!(
10996 "Cast error: Casting from Boolean to Date32 not supported",
10997 result.unwrap_err().to_string()
10998 );
10999 }
11000
11001 #[test]
11002 fn test_cast_struct_to_struct_nullability() {
11003 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11004 let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
11005 let struct_array = StructArray::from(vec![
11006 (
11007 Arc::new(Field::new("b", DataType::Boolean, false)),
11008 boolean.clone() as ArrayRef,
11009 ),
11010 (
11011 Arc::new(Field::new("c", DataType::Int32, true)),
11012 int.clone() as ArrayRef,
11013 ),
11014 ]);
11015
11016 let to_type = DataType::Struct(
11018 vec![
11019 Field::new("a", DataType::Utf8, false),
11020 Field::new("b", DataType::Utf8, true),
11021 ]
11022 .into(),
11023 );
11024 cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
11025
11026 let to_type = DataType::Struct(
11028 vec![
11029 Field::new("a", DataType::Utf8, false),
11030 Field::new("b", DataType::Utf8, false),
11031 ]
11032 .into(),
11033 );
11034 cast(&struct_array, &to_type)
11035 .expect_err("Cast nullable to non-nullable struct field should fail");
11036
11037 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11038 let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
11039 let struct_array = StructArray::from(vec![
11040 (
11041 Arc::new(Field::new("b", DataType::Boolean, false)),
11042 boolean.clone() as ArrayRef,
11043 ),
11044 (
11045 Arc::new(Field::new("c", DataType::Int32, false)),
11046 int.clone() as ArrayRef,
11047 ),
11048 ]);
11049
11050 let to_type = DataType::Struct(
11052 vec![
11053 Field::new("a", DataType::Utf8, false),
11054 Field::new("b", DataType::Utf8, false),
11055 ]
11056 .into(),
11057 );
11058 cast(&struct_array, &to_type)
11059 .expect("Cast non-nullable to non-nullable struct field should work");
11060
11061 let to_type = DataType::Struct(
11063 vec![
11064 Field::new("a", DataType::Utf8, false),
11065 Field::new("b", DataType::Int8, false),
11066 ]
11067 .into(),
11068 );
11069 cast(&struct_array, &to_type).expect_err(
11070 "Cast non-nullable to non-nullable struct field returning null should fail",
11071 );
11072 }
11073
11074 #[test]
11075 fn test_cast_struct_to_non_struct() {
11076 let boolean = Arc::new(BooleanArray::from(vec![true, false]));
11077 let struct_array = StructArray::from(vec![(
11078 Arc::new(Field::new("a", DataType::Boolean, false)),
11079 boolean.clone() as ArrayRef,
11080 )]);
11081 let to_type = DataType::Utf8;
11082 let result = cast(&struct_array, &to_type);
11083 assert_eq!(
11084 r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
11085 result.unwrap_err().to_string()
11086 );
11087 }
11088
11089 #[test]
11090 fn test_cast_non_struct_to_struct() {
11091 let array = StringArray::from(vec!["a", "b"]);
11092 let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
11093 let result = cast(&array, &to_type);
11094 assert_eq!(
11095 r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
11096 result.unwrap_err().to_string()
11097 );
11098 }
11099
11100 #[test]
11101 fn test_cast_struct_with_different_field_order() {
11102 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11104 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11105 let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11106
11107 let struct_array = StructArray::from(vec![
11108 (
11109 Arc::new(Field::new("a", DataType::Boolean, false)),
11110 boolean.clone() as ArrayRef,
11111 ),
11112 (
11113 Arc::new(Field::new("b", DataType::Int32, false)),
11114 int.clone() as ArrayRef,
11115 ),
11116 (
11117 Arc::new(Field::new("c", DataType::Utf8, false)),
11118 string.clone() as ArrayRef,
11119 ),
11120 ]);
11121
11122 let to_type = DataType::Struct(
11124 vec![
11125 Field::new("c", DataType::Utf8, false),
11126 Field::new("a", DataType::Utf8, false), Field::new("b", DataType::Utf8, false), ]
11129 .into(),
11130 );
11131
11132 let result = cast(&struct_array, &to_type).unwrap();
11133 let result_struct = result.as_struct();
11134
11135 assert_eq!(result_struct.data_type(), &to_type);
11136 assert_eq!(result_struct.num_columns(), 3);
11137
11138 let c_column = result_struct.column(0).as_string::<i32>();
11140 assert_eq!(
11141 c_column.into_iter().flatten().collect::<Vec<_>>(),
11142 vec!["foo", "bar", "baz", "qux"]
11143 );
11144
11145 let a_column = result_struct.column(1).as_string::<i32>();
11147 assert_eq!(
11148 a_column.into_iter().flatten().collect::<Vec<_>>(),
11149 vec!["false", "false", "true", "true"]
11150 );
11151
11152 let b_column = result_struct.column(2).as_string::<i32>();
11154 assert_eq!(
11155 b_column.into_iter().flatten().collect::<Vec<_>>(),
11156 vec!["42", "28", "19", "31"]
11157 );
11158 }
11159
11160 #[test]
11161 fn test_cast_struct_with_missing_field() {
11162 let boolean = Arc::new(BooleanArray::from(vec![false, true]));
11164 let struct_array = StructArray::from(vec![(
11165 Arc::new(Field::new("a", DataType::Boolean, false)),
11166 boolean.clone() as ArrayRef,
11167 )]);
11168
11169 let to_type = DataType::Struct(
11170 vec![
11171 Field::new("a", DataType::Utf8, false),
11172 Field::new("b", DataType::Int32, false), ]
11174 .into(),
11175 );
11176
11177 let result = cast(&struct_array, &to_type);
11178 assert!(result.is_err());
11179 assert_eq!(
11180 result.unwrap_err().to_string(),
11181 "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
11182 );
11183 }
11184
11185 #[test]
11186 fn test_cast_struct_with_subset_of_fields() {
11187 let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11189 let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11190 let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11191
11192 let struct_array = StructArray::from(vec![
11193 (
11194 Arc::new(Field::new("a", DataType::Boolean, false)),
11195 boolean.clone() as ArrayRef,
11196 ),
11197 (
11198 Arc::new(Field::new("b", DataType::Int32, false)),
11199 int.clone() as ArrayRef,
11200 ),
11201 (
11202 Arc::new(Field::new("c", DataType::Utf8, false)),
11203 string.clone() as ArrayRef,
11204 ),
11205 ]);
11206
11207 let to_type = DataType::Struct(
11209 vec![
11210 Field::new("c", DataType::Utf8, false),
11211 Field::new("a", DataType::Utf8, false),
11212 ]
11213 .into(),
11214 );
11215
11216 let result = cast(&struct_array, &to_type).unwrap();
11217 let result_struct = result.as_struct();
11218
11219 assert_eq!(result_struct.data_type(), &to_type);
11220 assert_eq!(result_struct.num_columns(), 2);
11221
11222 let c_column = result_struct.column(0).as_string::<i32>();
11224 assert_eq!(
11225 c_column.into_iter().flatten().collect::<Vec<_>>(),
11226 vec!["foo", "bar", "baz", "qux"]
11227 );
11228
11229 let a_column = result_struct.column(1).as_string::<i32>();
11231 assert_eq!(
11232 a_column.into_iter().flatten().collect::<Vec<_>>(),
11233 vec!["false", "false", "true", "true"]
11234 );
11235 }
11236
11237 #[test]
11238 fn test_can_cast_struct_rename_field() {
11239 let from_type = DataType::Struct(
11241 vec![
11242 Field::new("a", DataType::Int32, false),
11243 Field::new("b", DataType::Utf8, false),
11244 ]
11245 .into(),
11246 );
11247
11248 let to_type = DataType::Struct(
11249 vec![
11250 Field::new("a", DataType::Int64, false),
11251 Field::new("c", DataType::Boolean, false), ]
11253 .into(),
11254 );
11255
11256 assert!(can_cast_types(&from_type, &to_type));
11257 }
11258
11259 fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
11260 run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
11261 run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
11262 run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
11263 run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
11264 }
11265
11266 #[test]
11267 fn test_decimal_to_decimal_coverage() {
11268 let test_cases = [
11269 DecimalCastTestConfig {
11271 input_prec: 5,
11272 input_scale: 1,
11273 input_repr: 99999, output_prec: 10,
11275 output_scale: 6,
11276 expected_output_repr: Ok(9999900000), },
11278 DecimalCastTestConfig {
11280 input_prec: 5,
11281 input_scale: 1,
11282 input_repr: 99, output_prec: 7,
11284 output_scale: 6,
11285 expected_output_repr: Ok(9900000), },
11287 DecimalCastTestConfig {
11289 input_prec: 5,
11290 input_scale: 1,
11291 input_repr: 99999, output_prec: 7,
11293 output_scale: 6,
11294 expected_output_repr: Err("Invalid argument error: 9999.900000 is too large to store in a {} of precision 7. Max is 9.999999".to_string()) },
11296 DecimalCastTestConfig {
11298 input_prec: 5,
11299 input_scale: 3,
11300 input_repr: 99999, output_prec: 10,
11302 output_scale: 2,
11303 expected_output_repr: Ok(10000), },
11305 DecimalCastTestConfig {
11307 input_prec: 5,
11308 input_scale: 3,
11309 input_repr: 99994, output_prec: 10,
11311 output_scale: 2,
11312 expected_output_repr: Ok(9999), },
11314 DecimalCastTestConfig {
11316 input_prec: 5,
11317 input_scale: 3,
11318 input_repr: 99999, output_prec: 10,
11320 output_scale: 3,
11321 expected_output_repr: Ok(99999), },
11323 DecimalCastTestConfig {
11325 input_prec: 10,
11326 input_scale: 5,
11327 input_repr: 999999, output_prec: 8,
11329 output_scale: 7,
11330 expected_output_repr: Ok(99999900), },
11332 DecimalCastTestConfig {
11334 input_prec: 10,
11335 input_scale: 5,
11336 input_repr: 9999999, output_prec: 8,
11338 output_scale: 7,
11339 expected_output_repr: Err("Invalid argument error: 99.9999900 is too large to store in a {} of precision 8. Max is 9.9999999".to_string()) },
11341 DecimalCastTestConfig {
11343 input_prec: 7,
11344 input_scale: 4,
11345 input_repr: 9999999, output_prec: 6,
11347 output_scale: 2,
11348 expected_output_repr: Ok(100000),
11349 },
11350 DecimalCastTestConfig {
11352 input_prec: 10,
11353 input_scale: 5,
11354 input_repr: 12345678, output_prec: 8,
11356 output_scale: 3,
11357 expected_output_repr: Ok(123457), },
11359 DecimalCastTestConfig {
11361 input_prec: 10,
11362 input_scale: 5,
11363 input_repr: 9999999, output_prec: 4,
11365 output_scale: 3,
11366 expected_output_repr: Err("Invalid argument error: 100.000 is too large to store in a {} of precision 4. Max is 9.999".to_string()) },
11368 DecimalCastTestConfig {
11370 input_prec: 10,
11371 input_scale: 5,
11372 input_repr: 999999, output_prec: 6,
11374 output_scale: 5,
11375 expected_output_repr: Ok(999999), },
11377 DecimalCastTestConfig {
11379 input_prec: 10,
11380 input_scale: 5,
11381 input_repr: 9999999, output_prec: 6,
11383 output_scale: 5,
11384 expected_output_repr: Err("Invalid argument error: 99.99999 is too large to store in a {} of precision 6. Max is 9.99999".to_string()) },
11386 DecimalCastTestConfig {
11388 input_prec: 7,
11389 input_scale: 4,
11390 input_repr: 12345, output_prec: 7,
11392 output_scale: 6,
11393 expected_output_repr: Ok(1234500), },
11395 DecimalCastTestConfig {
11397 input_prec: 7,
11398 input_scale: 4,
11399 input_repr: 123456, output_prec: 7,
11401 output_scale: 6,
11402 expected_output_repr: Err("Invalid argument error: 12.345600 is too large to store in a {} of precision 7. Max is 9.999999".to_string()) },
11404 DecimalCastTestConfig {
11406 input_prec: 7,
11407 input_scale: 5,
11408 input_repr: 1234567, output_prec: 7,
11410 output_scale: 4,
11411 expected_output_repr: Ok(123457), },
11413 DecimalCastTestConfig {
11415 input_prec: 7,
11416 input_scale: 5,
11417 input_repr: 9999999, output_prec: 7,
11419 output_scale: 5,
11420 expected_output_repr: Ok(9999999), },
11422 DecimalCastTestConfig {
11424 input_prec: 7,
11425 input_scale: 0,
11426 input_repr: 1234567, output_prec: 8,
11428 output_scale: 0,
11429 expected_output_repr: Ok(1234567), },
11431 DecimalCastTestConfig {
11433 input_prec: 7,
11434 input_scale: 0,
11435 input_repr: 1234567, output_prec: 6,
11437 output_scale: 0,
11438 expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
11439 },
11440 DecimalCastTestConfig {
11442 input_prec: 7,
11443 input_scale: 0,
11444 input_repr: 123456, output_prec: 6,
11446 output_scale: 0,
11447 expected_output_repr: Ok(123456), },
11449 ];
11450
11451 for t in test_cases {
11452 run_decimal_cast_test_case_between_multiple_types(t);
11453 }
11454 }
11455
11456 #[test]
11457 fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
11458 let test_cases = [
11459 DecimalCastTestConfig {
11460 input_prec: 5,
11461 input_scale: 0,
11462 input_repr: 99999,
11463 output_prec: 10,
11464 output_scale: 5,
11465 expected_output_repr: Ok(9999900000),
11466 },
11467 DecimalCastTestConfig {
11468 input_prec: 5,
11469 input_scale: 0,
11470 input_repr: -99999,
11471 output_prec: 10,
11472 output_scale: 5,
11473 expected_output_repr: Ok(-9999900000),
11474 },
11475 DecimalCastTestConfig {
11476 input_prec: 5,
11477 input_scale: 2,
11478 input_repr: 99999,
11479 output_prec: 10,
11480 output_scale: 5,
11481 expected_output_repr: Ok(99999000),
11482 },
11483 DecimalCastTestConfig {
11484 input_prec: 5,
11485 input_scale: -2,
11486 input_repr: -99999,
11487 output_prec: 10,
11488 output_scale: 3,
11489 expected_output_repr: Ok(-9999900000),
11490 },
11491 DecimalCastTestConfig {
11492 input_prec: 5,
11493 input_scale: 3,
11494 input_repr: -12345,
11495 output_prec: 6,
11496 output_scale: 5,
11497 expected_output_repr: Err("Invalid argument error: -12.34500 is too small to store in a {} of precision 6. Min is -9.99999".to_string())
11498 },
11499 ];
11500
11501 for t in test_cases {
11502 run_decimal_cast_test_case_between_multiple_types(t);
11503 }
11504 }
11505
11506 #[test]
11507 fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
11508 let test_cases = [
11509 DecimalCastTestConfig {
11510 input_prec: 5,
11511 input_scale: 0,
11512 input_repr: 99999,
11513 output_scale: -3,
11514 output_prec: 3,
11515 expected_output_repr: Ok(100),
11516 },
11517 DecimalCastTestConfig {
11518 input_prec: 5,
11519 input_scale: 0,
11520 input_repr: -99999,
11521 output_prec: 1,
11522 output_scale: -5,
11523 expected_output_repr: Ok(-1),
11524 },
11525 DecimalCastTestConfig {
11526 input_prec: 10,
11527 input_scale: 2,
11528 input_repr: 123456789,
11529 output_prec: 5,
11530 output_scale: -2,
11531 expected_output_repr: Ok(12346),
11532 },
11533 DecimalCastTestConfig {
11534 input_prec: 10,
11535 input_scale: 4,
11536 input_repr: -9876543210,
11537 output_prec: 7,
11538 output_scale: 0,
11539 expected_output_repr: Ok(-987654),
11540 },
11541 DecimalCastTestConfig {
11542 input_prec: 7,
11543 input_scale: 4,
11544 input_repr: 9999999,
11545 output_prec: 6,
11546 output_scale: 3,
11547 expected_output_repr:
11548 Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
11549 },
11550 ];
11551 for t in test_cases {
11552 run_decimal_cast_test_case_between_multiple_types(t);
11553 }
11554 }
11555
11556 #[test]
11557 fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
11558 let array = vec![Some(123456789)];
11559 let array = create_decimal128_array(array, 24, 2).unwrap();
11560 let input_type = DataType::Decimal128(24, 2);
11561 let output_type = DataType::Decimal128(6, 2);
11562 assert!(can_cast_types(&input_type, &output_type));
11563
11564 let options = CastOptions {
11565 safe: false,
11566 ..Default::default()
11567 };
11568 let result = cast_with_options(&array, &output_type, &options);
11569 assert_eq!(
11570 result.unwrap_err().to_string(),
11571 "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11572 );
11573 }
11574
11575 #[test]
11576 fn test_decimal_to_decimal_same_scale() {
11577 let array = vec![Some(520)];
11578 let array = create_decimal128_array(array, 4, 2).unwrap();
11579 let input_type = DataType::Decimal128(4, 2);
11580 let output_type = DataType::Decimal128(3, 2);
11581 assert!(can_cast_types(&input_type, &output_type));
11582
11583 let options = CastOptions {
11584 safe: false,
11585 ..Default::default()
11586 };
11587 let result = cast_with_options(&array, &output_type, &options);
11588 assert_eq!(
11589 result.unwrap().as_primitive::<Decimal128Type>().value(0),
11590 520
11591 );
11592
11593 assert_eq!(
11595 &cast(
11596 &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
11597 &DataType::Decimal128(2, 0)
11598 )
11599 .unwrap(),
11600 &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
11601 );
11602 }
11603
11604 #[test]
11605 fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
11606 let array = vec![Some(123456789)];
11607 let array = create_decimal128_array(array, 24, 4).unwrap();
11608 let input_type = DataType::Decimal128(24, 4);
11609 let output_type = DataType::Decimal128(6, 2);
11610 assert!(can_cast_types(&input_type, &output_type));
11611
11612 let options = CastOptions {
11613 safe: false,
11614 ..Default::default()
11615 };
11616 let result = cast_with_options(&array, &output_type, &options);
11617 assert_eq!(
11618 result.unwrap_err().to_string(),
11619 "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11620 );
11621 }
11622
11623 #[test]
11624 fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
11625 let array = vec![Some(123456789)];
11626 let array = create_decimal128_array(array, 24, 2).unwrap();
11627 let input_type = DataType::Decimal128(24, 2);
11628 let output_type = DataType::Decimal128(6, 3);
11629 assert!(can_cast_types(&input_type, &output_type));
11630
11631 let options = CastOptions {
11632 safe: false,
11633 ..Default::default()
11634 };
11635 let result = cast_with_options(&array, &output_type, &options);
11636 assert_eq!(
11637 result.unwrap_err().to_string(),
11638 "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
11639 );
11640 }
11641
11642 #[test]
11643 fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
11644 let array = vec![Some(123456789)];
11645 let array = create_decimal128_array(array, 24, 2).unwrap();
11646 let input_type = DataType::Decimal128(24, 2);
11647 let output_type = DataType::Decimal256(6, 2);
11648 assert!(can_cast_types(&input_type, &output_type));
11649
11650 let options = CastOptions {
11651 safe: false,
11652 ..Default::default()
11653 };
11654 let result = cast_with_options(&array, &output_type, &options).unwrap_err();
11655 assert_eq!(
11656 result.to_string(),
11657 "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
11658 );
11659 }
11660
11661 #[test]
11662 fn test_first_none() {
11663 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11664 None,
11665 Some(vec![Some(1), Some(2)]),
11666 ])) as ArrayRef;
11667 let data_type =
11668 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11669 let opt = CastOptions::default();
11670 let r = cast_with_options(&array, &data_type, &opt).unwrap();
11671
11672 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11673 vec![None, Some(vec![Some(1), Some(2)])],
11674 2,
11675 )) as ArrayRef;
11676 assert_eq!(*fixed_array, *r);
11677 }
11678
11679 #[test]
11680 fn test_first_last_none() {
11681 let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11682 None,
11683 Some(vec![Some(1), Some(2)]),
11684 None,
11685 ])) as ArrayRef;
11686 let data_type =
11687 DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11688 let opt = CastOptions::default();
11689 let r = cast_with_options(&array, &data_type, &opt).unwrap();
11690
11691 let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11692 vec![None, Some(vec![Some(1), Some(2)]), None],
11693 2,
11694 )) as ArrayRef;
11695 assert_eq!(*fixed_array, *r);
11696 }
11697
11698 #[test]
11699 fn test_cast_decimal_error_output() {
11700 let array = Int64Array::from(vec![1]);
11701 let error = cast_with_options(
11702 &array,
11703 &DataType::Decimal32(1, 1),
11704 &CastOptions {
11705 safe: false,
11706 format_options: FormatOptions::default(),
11707 },
11708 )
11709 .unwrap_err();
11710 assert_eq!(
11711 error.to_string(),
11712 "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
11713 );
11714
11715 let array = Int64Array::from(vec![-1]);
11716 let error = cast_with_options(
11717 &array,
11718 &DataType::Decimal32(1, 1),
11719 &CastOptions {
11720 safe: false,
11721 format_options: FormatOptions::default(),
11722 },
11723 )
11724 .unwrap_err();
11725 assert_eq!(
11726 error.to_string(),
11727 "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
11728 );
11729 }
11730
11731 #[test]
11732 fn test_run_end_encoded_to_primitive() {
11733 let run_ends = Int32Array::from(vec![2, 5, 6]);
11735 let values = Int32Array::from(vec![1, 2, 3]);
11736 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11737 let array_ref = Arc::new(run_array) as ArrayRef;
11738 let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
11740 let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
11742 assert_eq!(
11743 result_run_array.values(),
11744 &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
11745 );
11746 }
11747
11748 #[test]
11749 #[should_panic = "assertion `left == right` failed\n left: ScalarBuffer([1, 1, 2])\n right: [2, 2, 3]"]
11750 fn test_sliced_run_end_encoded_to_primitive() {
11753 let run_ends = Int32Array::from(vec![2, 5, 6]);
11754 let values = Int32Array::from(vec![1, 2, 3]);
11755 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11757 let run_array = run_array.slice(3, 3); let array_ref = Arc::new(run_array) as ArrayRef;
11759
11760 let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
11761 let result_run_array = cast_result.as_primitive::<Int64Type>();
11762 assert_eq!(result_run_array.values(), &[2, 2, 3]);
11763 }
11764
11765 #[test]
11766 fn test_run_end_encoded_to_string() {
11767 let run_ends = Int32Array::from(vec![2, 3, 5]);
11768 let values = Int32Array::from(vec![10, 20, 30]);
11769 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11770 let array_ref = Arc::new(run_array) as ArrayRef;
11771
11772 let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
11774
11775 let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
11777 assert_eq!(result_array.value(0), "10");
11779 assert_eq!(result_array.value(1), "10");
11780 assert_eq!(result_array.value(2), "20");
11781 }
11782
11783 #[test]
11784 fn test_primitive_to_run_end_encoded() {
11785 let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
11787 let array_ref = Arc::new(source_array) as ArrayRef;
11788
11789 let target_type = DataType::RunEndEncoded(
11791 Arc::new(Field::new("run_ends", DataType::Int32, false)),
11792 Arc::new(Field::new("values", DataType::Int32, true)),
11793 );
11794 let cast_result = cast(&array_ref, &target_type).unwrap();
11795
11796 let result_run_array = cast_result
11798 .as_any()
11799 .downcast_ref::<RunArray<Int32Type>>()
11800 .unwrap();
11801
11802 assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
11804
11805 let values_array = result_run_array.values().as_primitive::<Int32Type>();
11807 assert_eq!(values_array.values(), &[1, 2, 3]);
11808 }
11809
11810 #[test]
11811 fn test_primitive_to_run_end_encoded_with_nulls() {
11812 let source_array = Int32Array::from(vec![
11813 Some(1),
11814 Some(1),
11815 None,
11816 None,
11817 Some(2),
11818 Some(2),
11819 Some(3),
11820 Some(3),
11821 None,
11822 None,
11823 Some(4),
11824 Some(4),
11825 Some(5),
11826 Some(5),
11827 None,
11828 None,
11829 ]);
11830 let array_ref = Arc::new(source_array) as ArrayRef;
11831 let target_type = DataType::RunEndEncoded(
11832 Arc::new(Field::new("run_ends", DataType::Int32, false)),
11833 Arc::new(Field::new("values", DataType::Int32, true)),
11834 );
11835 let cast_result = cast(&array_ref, &target_type).unwrap();
11836 let result_run_array = cast_result
11837 .as_any()
11838 .downcast_ref::<RunArray<Int32Type>>()
11839 .unwrap();
11840 assert_eq!(
11841 result_run_array.run_ends().values(),
11842 &[2, 4, 6, 8, 10, 12, 14, 16]
11843 );
11844 assert_eq!(
11845 result_run_array
11846 .values()
11847 .as_primitive::<Int32Type>()
11848 .values(),
11849 &[1, 0, 2, 3, 0, 4, 5, 0]
11850 );
11851 assert_eq!(result_run_array.values().null_count(), 3);
11852 }
11853
11854 #[test]
11855 fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
11856 let source_array = Int64Array::from(vec![
11857 Some(1),
11858 Some(1),
11859 None,
11860 None,
11861 None,
11862 None,
11863 None,
11864 None,
11865 None,
11866 None,
11867 Some(4),
11868 Some(20),
11869 Some(500),
11870 Some(500),
11871 None,
11872 None,
11873 ]);
11874 let array_ref = Arc::new(source_array) as ArrayRef;
11875 let target_type = DataType::RunEndEncoded(
11876 Arc::new(Field::new("run_ends", DataType::Int16, false)),
11877 Arc::new(Field::new("values", DataType::Int64, true)),
11878 );
11879 let cast_result = cast(&array_ref, &target_type).unwrap();
11880 let result_run_array = cast_result
11881 .as_any()
11882 .downcast_ref::<RunArray<Int16Type>>()
11883 .unwrap();
11884 assert_eq!(
11885 result_run_array.run_ends().values(),
11886 &[2, 10, 11, 12, 14, 16]
11887 );
11888 assert_eq!(
11889 result_run_array
11890 .values()
11891 .as_primitive::<Int64Type>()
11892 .values(),
11893 &[1, 0, 4, 20, 500, 0]
11894 );
11895 assert_eq!(result_run_array.values().null_count(), 2);
11896 }
11897
11898 #[test]
11899 fn test_string_to_run_end_encoded() {
11900 let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
11902 let array_ref = Arc::new(source_array) as ArrayRef;
11903
11904 let target_type = DataType::RunEndEncoded(
11906 Arc::new(Field::new("run_ends", DataType::Int32, false)),
11907 Arc::new(Field::new("values", DataType::Utf8, true)),
11908 );
11909 let cast_result = cast(&array_ref, &target_type).unwrap();
11910
11911 let result_run_array = cast_result
11913 .as_any()
11914 .downcast_ref::<RunArray<Int32Type>>()
11915 .unwrap();
11916
11917 assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
11919
11920 let values_array = result_run_array.values().as_string::<i32>();
11922 assert_eq!(values_array.value(0), "a");
11923 assert_eq!(values_array.value(1), "b");
11924 assert_eq!(values_array.value(2), "c");
11925 }
11926
11927 #[test]
11928 fn test_empty_array_to_run_end_encoded() {
11929 let source_array = Int32Array::from(Vec::<i32>::new());
11931 let array_ref = Arc::new(source_array) as ArrayRef;
11932
11933 let target_type = DataType::RunEndEncoded(
11935 Arc::new(Field::new("run_ends", DataType::Int32, false)),
11936 Arc::new(Field::new("values", DataType::Int32, true)),
11937 );
11938 let cast_result = cast(&array_ref, &target_type).unwrap();
11939
11940 let result_run_array = cast_result
11942 .as_any()
11943 .downcast_ref::<RunArray<Int32Type>>()
11944 .unwrap();
11945
11946 assert_eq!(result_run_array.run_ends().len(), 0);
11948 assert_eq!(result_run_array.values().len(), 0);
11949 }
11950
11951 #[test]
11952 fn test_run_end_encoded_with_nulls() {
11953 let run_ends = Int32Array::from(vec![2, 3, 5]);
11955 let values = Int32Array::from(vec![Some(1), None, Some(2)]);
11956 let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11957 let array_ref = Arc::new(run_array) as ArrayRef;
11958
11959 let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
11961
11962 let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
11964 assert_eq!(result_run_array.value(0), "1");
11965 assert!(result_run_array.is_null(2));
11966 assert_eq!(result_run_array.value(4), "2");
11967 }
11968
11969 #[test]
11970 fn test_different_index_types() {
11971 let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
11973 let array_ref = Arc::new(source_array) as ArrayRef;
11974
11975 let target_type = DataType::RunEndEncoded(
11976 Arc::new(Field::new("run_ends", DataType::Int16, false)),
11977 Arc::new(Field::new("values", DataType::Int32, true)),
11978 );
11979 let cast_result = cast(&array_ref, &target_type).unwrap();
11980 assert_eq!(cast_result.data_type(), &target_type);
11981
11982 let run_array = cast_result
11985 .as_any()
11986 .downcast_ref::<RunArray<Int16Type>>()
11987 .unwrap();
11988 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
11989 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
11990 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
11991 assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
11992
11993 let target_type = DataType::RunEndEncoded(
11995 Arc::new(Field::new("run_ends", DataType::Int64, false)),
11996 Arc::new(Field::new("values", DataType::Int32, true)),
11997 );
11998 let cast_result = cast(&array_ref, &target_type).unwrap();
11999 assert_eq!(cast_result.data_type(), &target_type);
12000
12001 let run_array = cast_result
12004 .as_any()
12005 .downcast_ref::<RunArray<Int64Type>>()
12006 .unwrap();
12007 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12008 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12009 assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12010 assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
12011 }
12012
12013 #[test]
12014 fn test_unsupported_cast_to_run_end_encoded() {
12015 let field = Field::new("item", DataType::Int32, false);
12017 let struct_array = StructArray::from(vec![(
12018 Arc::new(field),
12019 Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
12020 )]);
12021 let array_ref = Arc::new(struct_array) as ArrayRef;
12022
12023 let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
12027
12028 assert!(cast_result.is_err());
12030 }
12031
12032 #[test]
12034 fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
12035 let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); let values = StringArray::from(vec!["a", "b", "c"]);
12038
12039 let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12040 let array_ref = Arc::new(ree_array) as ArrayRef;
12041
12042 let target_type = DataType::RunEndEncoded(
12044 Arc::new(Field::new("run_ends", DataType::Int16, false)),
12045 Arc::new(Field::new("values", DataType::Utf8, true)),
12046 );
12047 let cast_options = CastOptions {
12048 safe: false, format_options: FormatOptions::default(),
12050 };
12051
12052 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12054 cast_with_options(&array_ref, &target_type, &cast_options);
12055
12056 let e = result.expect_err("Cast should have failed but succeeded");
12057 assert!(
12058 e.to_string()
12059 .contains("Cast error: Can't cast value 100000 to type Int16")
12060 );
12061 }
12062
12063 #[test]
12064 fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
12065 let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); let values = StringArray::from(vec!["a", "b", "c"]);
12068
12069 let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12070 let array_ref = Arc::new(ree_array) as ArrayRef;
12071
12072 let target_type = DataType::RunEndEncoded(
12074 Arc::new(Field::new("run_ends", DataType::Int16, false)),
12075 Arc::new(Field::new("values", DataType::Utf8, true)),
12076 );
12077 let cast_options = CastOptions {
12078 safe: true,
12079 format_options: FormatOptions::default(),
12080 };
12081
12082 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12084 cast_with_options(&array_ref, &target_type, &cast_options);
12085 let e = result.expect_err("Cast should have failed but succeeded");
12086 assert!(
12087 e.to_string()
12088 .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
12089 );
12090 }
12091
12092 #[test]
12094 fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
12095 let run_ends = Int16Array::from(vec![2, 5, 8]); let values = StringArray::from(vec!["a", "b", "c"]);
12098
12099 let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12100 let array_ref = Arc::new(ree_array) as ArrayRef;
12101
12102 let target_type = DataType::RunEndEncoded(
12104 Arc::new(Field::new("run_ends", DataType::Int64, false)),
12105 Arc::new(Field::new("values", DataType::Utf8, true)),
12106 );
12107 let cast_options = CastOptions {
12108 safe: false,
12109 format_options: FormatOptions::default(),
12110 };
12111
12112 let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12114 cast_with_options(&array_ref, &target_type, &cast_options);
12115
12116 let array_ref = result.expect("Cast should have succeeded but failed");
12117 let run_array = array_ref
12119 .as_any()
12120 .downcast_ref::<RunArray<Int64Type>>()
12121 .unwrap();
12122
12123 assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
12126 assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
12127 assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
12128 assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12129 }
12130
12131 #[test]
12132 fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
12133 let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
12135 let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
12136 let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
12137
12138 let target_type = DataType::RunEndEncoded(
12140 Arc::new(Field::new("run_ends", DataType::Int64, false)),
12141 Arc::new(Field::new("values", DataType::Utf8, true)),
12142 );
12143 let cast_options = CastOptions {
12144 safe: false,
12145 format_options: FormatOptions::default(),
12146 };
12147
12148 let result = cast_with_options(&array_ref, &target_type, &cast_options)
12150 .expect("Cast should have succeeded but failed");
12151
12152 let run_array = result
12155 .as_any()
12156 .downcast_ref::<RunArray<Int64Type>>()
12157 .unwrap();
12158 assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
12159 assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
12160 assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12161
12162 assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
12164 }
12165
12166 fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
12167 vec![
12168 Some(vec![Some(1), Some(2), Some(3)]),
12169 Some(vec![Some(4), Some(5), Some(6)]),
12170 None,
12171 Some(vec![Some(7), Some(8), Some(9)]),
12172 Some(vec![None, Some(10)]),
12173 ]
12174 }
12175
12176 #[test]
12177 fn test_cast_list_view_to_list() {
12178 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12179 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12180 assert!(can_cast_types(list_view.data_type(), &target_type));
12181 let cast_result = cast(&list_view, &target_type).unwrap();
12182 let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12183 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12184 assert_eq!(got_list, &expected_list);
12185 }
12186
12187 #[test]
12188 fn test_cast_list_to_list_view() {
12189 let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12190 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12191 assert!(can_cast_types(list.data_type(), &target_type));
12192 let cast_result = cast(&list, &target_type).unwrap();
12193
12194 let got_list_view = cast_result
12195 .as_any()
12196 .downcast_ref::<ListViewArray>()
12197 .unwrap();
12198 let expected_list_view =
12199 ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12200 assert_eq!(got_list_view, &expected_list_view);
12201 }
12202
12203 #[test]
12204 fn test_cast_large_list_view_to_large_list() {
12205 let list_view =
12206 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12207 let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
12208 assert!(can_cast_types(list_view.data_type(), &target_type));
12209 let cast_result = cast(&list_view, &target_type).unwrap();
12210 let got_list = cast_result
12211 .as_any()
12212 .downcast_ref::<LargeListArray>()
12213 .unwrap();
12214
12215 let expected_list =
12216 LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12217 assert_eq!(got_list, &expected_list);
12218 }
12219
12220 #[test]
12221 fn test_cast_large_list_to_large_list_view() {
12222 let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12223 let target_type =
12224 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
12225 assert!(can_cast_types(list.data_type(), &target_type));
12226 let cast_result = cast(&list, &target_type).unwrap();
12227
12228 let got_list_view = cast_result
12229 .as_any()
12230 .downcast_ref::<LargeListViewArray>()
12231 .unwrap();
12232 let expected_list_view =
12233 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12234 assert_eq!(got_list_view, &expected_list_view);
12235 }
12236
12237 #[test]
12238 fn test_cast_list_view_to_list_out_of_order() {
12239 let list_view = ListViewArray::new(
12240 Arc::new(Field::new("item", DataType::Int32, true)),
12241 ScalarBuffer::from(vec![0, 6, 3]),
12242 ScalarBuffer::from(vec![3, 3, 3]),
12243 Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
12244 None,
12245 );
12246 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12247 assert!(can_cast_types(list_view.data_type(), &target_type));
12248 let cast_result = cast(&list_view, &target_type).unwrap();
12249 let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12250 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12251 Some(vec![Some(1), Some(2), Some(3)]),
12252 Some(vec![Some(7), Some(8), Some(9)]),
12253 Some(vec![Some(4), Some(5), Some(6)]),
12254 ]);
12255 assert_eq!(got_list, &expected_list);
12256 }
12257
12258 #[test]
12259 fn test_cast_list_view_to_list_overlapping() {
12260 let list_view = ListViewArray::new(
12261 Arc::new(Field::new("item", DataType::Int32, true)),
12262 ScalarBuffer::from(vec![0, 0]),
12263 ScalarBuffer::from(vec![1, 2]),
12264 Arc::new(Int32Array::from(vec![1, 2])),
12265 None,
12266 );
12267 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12268 assert!(can_cast_types(list_view.data_type(), &target_type));
12269 let cast_result = cast(&list_view, &target_type).unwrap();
12270 let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12271 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12272 Some(vec![Some(1)]),
12273 Some(vec![Some(1), Some(2)]),
12274 ]);
12275 assert_eq!(got_list, &expected_list);
12276 }
12277
12278 #[test]
12279 fn test_cast_list_view_to_list_empty() {
12280 let values: Vec<Option<Vec<Option<i32>>>> = vec![];
12281 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
12282 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12283 assert!(can_cast_types(list_view.data_type(), &target_type));
12284 let cast_result = cast(&list_view, &target_type).unwrap();
12285 let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12286 let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
12287 assert_eq!(got_list, &expected_list);
12288 }
12289
12290 #[test]
12291 fn test_cast_list_view_to_list_different_inner_type() {
12292 let values = int32_list_values();
12293 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
12294 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
12295 assert!(can_cast_types(list_view.data_type(), &target_type));
12296 let cast_result = cast(&list_view, &target_type).unwrap();
12297 let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12298
12299 let expected_list =
12300 ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
12301 list.map(|list| {
12302 list.into_iter()
12303 .map(|v| v.map(|v| v as i64))
12304 .collect::<Vec<_>>()
12305 })
12306 }));
12307 assert_eq!(got_list, &expected_list);
12308 }
12309
12310 #[test]
12311 fn test_cast_list_view_to_list_out_of_order_with_nulls() {
12312 let list_view = ListViewArray::new(
12313 Arc::new(Field::new("item", DataType::Int32, true)),
12314 ScalarBuffer::from(vec![0, 6, 3]),
12315 ScalarBuffer::from(vec![3, 3, 3]),
12316 Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
12317 Some(NullBuffer::from(vec![false, true, false])),
12318 );
12319 let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12320 assert!(can_cast_types(list_view.data_type(), &target_type));
12321 let cast_result = cast(&list_view, &target_type).unwrap();
12322 let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12323 let expected_list = ListArray::new(
12324 Arc::new(Field::new("item", DataType::Int32, true)),
12325 OffsetBuffer::from_lengths([3, 3, 3]),
12326 Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
12327 Some(NullBuffer::from(vec![false, true, false])),
12328 );
12329 assert_eq!(got_list, &expected_list);
12330 }
12331
12332 #[test]
12333 fn test_cast_list_view_to_large_list_view() {
12334 let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12335 let target_type =
12336 DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
12337 assert!(can_cast_types(list_view.data_type(), &target_type));
12338 let cast_result = cast(&list_view, &target_type).unwrap();
12339 let got = cast_result
12340 .as_any()
12341 .downcast_ref::<LargeListViewArray>()
12342 .unwrap();
12343
12344 let expected =
12345 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12346 assert_eq!(got, &expected);
12347 }
12348
12349 #[test]
12350 fn test_cast_large_list_view_to_list_view() {
12351 let list_view =
12352 LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12353 let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12354 assert!(can_cast_types(list_view.data_type(), &target_type));
12355 let cast_result = cast(&list_view, &target_type).unwrap();
12356 let got = cast_result
12357 .as_any()
12358 .downcast_ref::<ListViewArray>()
12359 .unwrap();
12360
12361 let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12362 assert_eq!(got, &expected);
12363 }
12364
12365 #[test]
12366 fn test_cast_time32_second_to_int64() {
12367 let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
12368 let array = Arc::new(array) as Arc<dyn Array>;
12369 let to_type = DataType::Int64;
12370 let cast_options = CastOptions::default();
12371
12372 assert!(can_cast_types(array.data_type(), &to_type));
12373
12374 let result = cast_with_options(&array, &to_type, &cast_options);
12375 assert!(
12376 result.is_ok(),
12377 "Failed to cast Time32(Second) to Int64: {:?}",
12378 result.err()
12379 );
12380
12381 let cast_array = result.unwrap();
12382 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12383
12384 assert_eq!(cast_array.value(0), 1000);
12385 assert_eq!(cast_array.value(1), 2000);
12386 assert_eq!(cast_array.value(2), 3000);
12387 }
12388
12389 #[test]
12390 fn test_cast_time32_millisecond_to_int64() {
12391 let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
12392 let array = Arc::new(array) as Arc<dyn Array>;
12393 let to_type = DataType::Int64;
12394 let cast_options = CastOptions::default();
12395
12396 assert!(can_cast_types(array.data_type(), &to_type));
12397
12398 let result = cast_with_options(&array, &to_type, &cast_options);
12399 assert!(
12400 result.is_ok(),
12401 "Failed to cast Time32(Millisecond) to Int64: {:?}",
12402 result.err()
12403 );
12404
12405 let cast_array = result.unwrap();
12406 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12407
12408 assert_eq!(cast_array.value(0), 1000);
12409 assert_eq!(cast_array.value(1), 2000);
12410 assert_eq!(cast_array.value(2), 3000);
12411 }
12412
12413 #[test]
12414 fn test_cast_string_to_time32_second_to_int64() {
12415 let array = StringArray::from(vec!["03:12:44"]);
12418 let array = Arc::new(array) as Arc<dyn Array>;
12419 let cast_options = CastOptions::default();
12420
12421 let time32_type = DataType::Time32(TimeUnit::Second);
12423 let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
12424
12425 let int64_type = DataType::Int64;
12427 assert!(can_cast_types(time32_array.data_type(), &int64_type));
12428
12429 let result = cast_with_options(&time32_array, &int64_type, &cast_options);
12430
12431 assert!(
12432 result.is_ok(),
12433 "Failed to cast Time32(Second) to Int64: {:?}",
12434 result.err()
12435 );
12436
12437 let cast_array = result.unwrap();
12438 let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12439
12440 assert_eq!(cast_array.value(0), 11564);
12442 }
12443 #[test]
12444 fn test_string_dicts_to_binary_view() {
12445 let expected = BinaryViewArray::from_iter(vec![
12446 VIEW_TEST_DATA[1],
12447 VIEW_TEST_DATA[0],
12448 None,
12449 VIEW_TEST_DATA[3],
12450 None,
12451 VIEW_TEST_DATA[1],
12452 VIEW_TEST_DATA[4],
12453 ]);
12454
12455 let values_arrays: [ArrayRef; _] = [
12456 Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
12457 Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
12458 Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
12459 ];
12460 for values in values_arrays {
12461 let keys =
12462 Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
12463 let string_dict_array =
12464 DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
12465
12466 let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
12467 assert_eq!(casted.as_ref(), &expected);
12468 }
12469 }
12470
12471 #[test]
12472 fn test_binary_dicts_to_string_view() {
12473 let expected = StringViewArray::from_iter(vec![
12474 VIEW_TEST_DATA[1],
12475 VIEW_TEST_DATA[0],
12476 None,
12477 VIEW_TEST_DATA[3],
12478 None,
12479 VIEW_TEST_DATA[1],
12480 VIEW_TEST_DATA[4],
12481 ]);
12482
12483 let values_arrays: [ArrayRef; _] = [
12484 Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
12485 Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
12486 Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
12487 ];
12488 for values in values_arrays {
12489 let keys =
12490 Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
12491 let string_dict_array =
12492 DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
12493
12494 let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
12495 assert_eq!(casted.as_ref(), &expected);
12496 }
12497 }
12498}