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