1use std::any::Any;
19use std::fmt;
20use std::hash::Hash;
21use std::sync::Arc;
22
23use crate::physical_expr::PhysicalExpr;
24
25use arrow::compute::{CastOptions, can_cast_types};
26use arrow::datatypes::{DataType, DataType::*, FieldRef, Schema};
27use arrow::record_batch::RecordBatch;
28use datafusion_common::format::DEFAULT_FORMAT_OPTIONS;
29use datafusion_common::{Result, not_impl_err};
30use datafusion_expr_common::columnar_value::ColumnarValue;
31use datafusion_expr_common::interval_arithmetic::Interval;
32use datafusion_expr_common::sort_properties::ExprProperties;
33
34const DEFAULT_CAST_OPTIONS: CastOptions<'static> = CastOptions {
35 safe: false,
36 format_options: DEFAULT_FORMAT_OPTIONS,
37};
38
39const DEFAULT_SAFE_CAST_OPTIONS: CastOptions<'static> = CastOptions {
40 safe: true,
41 format_options: DEFAULT_FORMAT_OPTIONS,
42};
43
44#[derive(Debug, Clone, Eq)]
46pub struct CastExpr {
47 pub expr: Arc<dyn PhysicalExpr>,
49 cast_type: DataType,
51 cast_options: CastOptions<'static>,
53}
54
55impl PartialEq for CastExpr {
57 fn eq(&self, other: &Self) -> bool {
58 self.expr.eq(&other.expr)
59 && self.cast_type.eq(&other.cast_type)
60 && self.cast_options.eq(&other.cast_options)
61 }
62}
63
64impl Hash for CastExpr {
65 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
66 self.expr.hash(state);
67 self.cast_type.hash(state);
68 self.cast_options.hash(state);
69 }
70}
71
72impl CastExpr {
73 pub fn new(
75 expr: Arc<dyn PhysicalExpr>,
76 cast_type: DataType,
77 cast_options: Option<CastOptions<'static>>,
78 ) -> Self {
79 Self {
80 expr,
81 cast_type,
82 cast_options: cast_options.unwrap_or(DEFAULT_CAST_OPTIONS),
83 }
84 }
85
86 pub fn expr(&self) -> &Arc<dyn PhysicalExpr> {
88 &self.expr
89 }
90
91 pub fn cast_type(&self) -> &DataType {
93 &self.cast_type
94 }
95
96 pub fn cast_options(&self) -> &CastOptions<'static> {
98 &self.cast_options
99 }
100
101 pub fn check_bigger_cast(cast_type: &DataType, src: &DataType) -> bool {
104 if cast_type.eq(src) {
105 return true;
106 }
107 matches!(
108 (src, cast_type),
109 (Int8, Int16 | Int32 | Int64)
110 | (Int16, Int32 | Int64)
111 | (Int32, Int64)
112 | (UInt8, UInt16 | UInt32 | UInt64)
113 | (UInt16, UInt32 | UInt64)
114 | (UInt32, UInt64)
115 | (
116 Int8 | Int16 | Int32 | UInt8 | UInt16 | UInt32,
117 Float32 | Float64
118 )
119 | (Int64 | UInt64, Float64)
120 | (Utf8, LargeUtf8)
121 )
122 }
123
124 pub fn is_bigger_cast(&self, src: &DataType) -> bool {
126 Self::check_bigger_cast(&self.cast_type, src)
127 }
128}
129
130impl fmt::Display for CastExpr {
131 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132 write!(f, "CAST({} AS {:?})", self.expr, self.cast_type)
133 }
134}
135
136impl PhysicalExpr for CastExpr {
137 fn as_any(&self) -> &dyn Any {
139 self
140 }
141
142 fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
143 Ok(self.cast_type.clone())
144 }
145
146 fn nullable(&self, input_schema: &Schema) -> Result<bool> {
147 self.expr.nullable(input_schema)
148 }
149
150 fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
151 let value = self.expr.evaluate(batch)?;
152 value.cast_to(&self.cast_type, Some(&self.cast_options))
153 }
154
155 fn return_field(&self, input_schema: &Schema) -> Result<FieldRef> {
156 Ok(self
157 .expr
158 .return_field(input_schema)?
159 .as_ref()
160 .clone()
161 .with_data_type(self.cast_type.clone())
162 .into())
163 }
164
165 fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
166 vec![&self.expr]
167 }
168
169 fn with_new_children(
170 self: Arc<Self>,
171 children: Vec<Arc<dyn PhysicalExpr>>,
172 ) -> Result<Arc<dyn PhysicalExpr>> {
173 Ok(Arc::new(CastExpr::new(
174 Arc::clone(&children[0]),
175 self.cast_type.clone(),
176 Some(self.cast_options.clone()),
177 )))
178 }
179
180 fn evaluate_bounds(&self, children: &[&Interval]) -> Result<Interval> {
181 children[0].cast_to(&self.cast_type, &self.cast_options)
183 }
184
185 fn propagate_constraints(
186 &self,
187 interval: &Interval,
188 children: &[&Interval],
189 ) -> Result<Option<Vec<Interval>>> {
190 let child_interval = children[0];
191 let cast_type = child_interval.data_type();
193 Ok(Some(vec![
194 interval.cast_to(&cast_type, &DEFAULT_SAFE_CAST_OPTIONS)?,
195 ]))
196 }
197
198 fn get_properties(&self, children: &[ExprProperties]) -> Result<ExprProperties> {
201 let source_datatype = children[0].range.data_type();
202 let target_type = &self.cast_type;
203
204 let unbounded = Interval::make_unbounded(target_type)?;
205 if (source_datatype.is_numeric() || source_datatype == Boolean)
206 && target_type.is_numeric()
207 || source_datatype.is_temporal() && target_type.is_temporal()
208 || source_datatype.eq(target_type)
209 {
210 Ok(children[0].clone().with_range(unbounded))
211 } else {
212 Ok(ExprProperties::new_unknown().with_range(unbounded))
213 }
214 }
215
216 fn fmt_sql(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 write!(f, "CAST(")?;
218 self.expr.fmt_sql(f)?;
219 write!(f, " AS {:?}", self.cast_type)?;
220
221 write!(f, ")")
222 }
223}
224
225pub fn cast_with_options(
230 expr: Arc<dyn PhysicalExpr>,
231 input_schema: &Schema,
232 cast_type: DataType,
233 cast_options: Option<CastOptions<'static>>,
234) -> Result<Arc<dyn PhysicalExpr>> {
235 let expr_type = expr.data_type(input_schema)?;
236 if expr_type == cast_type {
237 Ok(Arc::clone(&expr))
238 } else if can_cast_types(&expr_type, &cast_type) {
239 Ok(Arc::new(CastExpr::new(expr, cast_type, cast_options)))
240 } else {
241 not_impl_err!("Unsupported CAST from {expr_type} to {cast_type}")
242 }
243}
244
245pub fn cast(
250 expr: Arc<dyn PhysicalExpr>,
251 input_schema: &Schema,
252 cast_type: DataType,
253) -> Result<Arc<dyn PhysicalExpr>> {
254 cast_with_options(expr, input_schema, cast_type, None)
255}
256
257#[cfg(test)]
258mod tests {
259 use super::*;
260
261 use crate::expressions::column::col;
262
263 use arrow::{
264 array::{
265 Array, Decimal128Array, Float32Array, Float64Array, Int8Array, Int16Array,
266 Int32Array, Int64Array, StringArray, Time64NanosecondArray,
267 TimestampNanosecondArray, UInt32Array,
268 },
269 datatypes::*,
270 };
271 use datafusion_physical_expr_common::physical_expr::fmt_sql;
272 use insta::assert_snapshot;
273
274 macro_rules! generic_decimal_to_other_test_cast {
281 ($DECIMAL_ARRAY:ident, $A_TYPE:expr, $TYPEARRAY:ident, $TYPE:expr, $VEC:expr,$CAST_OPTIONS:expr) => {{
282 let schema = Schema::new(vec![Field::new("a", $A_TYPE, true)]);
283 let batch = RecordBatch::try_new(
284 Arc::new(schema.clone()),
285 vec![Arc::new($DECIMAL_ARRAY)],
286 )?;
287 let expression =
289 cast_with_options(col("a", &schema)?, &schema, $TYPE, $CAST_OPTIONS)?;
290
291 assert_eq!(
293 format!("CAST(a@0 AS {:?})", $TYPE),
294 format!("{}", expression)
295 );
296
297 assert_eq!(expression.data_type(&schema)?, $TYPE);
299
300 let result = expression
302 .evaluate(&batch)?
303 .into_array(batch.num_rows())
304 .expect("Failed to convert to array");
305
306 assert_eq!(*result.data_type(), $TYPE);
308
309 let result = result
311 .as_any()
312 .downcast_ref::<$TYPEARRAY>()
313 .expect("failed to downcast");
314
315 for (i, x) in $VEC.iter().enumerate() {
317 match x {
318 Some(x) => assert_eq!(result.value(i), *x),
319 None => assert!(!result.is_valid(i)),
320 }
321 }
322 }};
323 }
324
325 macro_rules! generic_test_cast {
332 ($A_ARRAY:ident, $A_TYPE:expr, $A_VEC:expr, $TYPEARRAY:ident, $TYPE:expr, $VEC:expr, $CAST_OPTIONS:expr) => {{
333 let schema = Schema::new(vec![Field::new("a", $A_TYPE, true)]);
334 let a_vec_len = $A_VEC.len();
335 let a = $A_ARRAY::from($A_VEC);
336 let batch =
337 RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
338
339 let expression =
341 cast_with_options(col("a", &schema)?, &schema, $TYPE, $CAST_OPTIONS)?;
342
343 assert_eq!(
345 format!("CAST(a@0 AS {:?})", $TYPE),
346 format!("{}", expression)
347 );
348
349 assert_eq!(expression.data_type(&schema)?, $TYPE);
351
352 let result = expression
354 .evaluate(&batch)?
355 .into_array(batch.num_rows())
356 .expect("Failed to convert to array");
357
358 assert_eq!(*result.data_type(), $TYPE);
360
361 assert_eq!(result.len(), a_vec_len);
363
364 let result = result
366 .as_any()
367 .downcast_ref::<$TYPEARRAY>()
368 .expect("failed to downcast");
369
370 for (i, x) in $VEC.iter().enumerate() {
372 match x {
373 Some(x) => assert_eq!(result.value(i), *x),
374 None => assert!(!result.is_valid(i)),
375 }
376 }
377 }};
378 }
379
380 #[test]
381 fn test_cast_decimal_to_decimal() -> Result<()> {
382 let array = vec![
383 Some(1234),
384 Some(2222),
385 Some(3),
386 Some(4000),
387 Some(5000),
388 None,
389 ];
390
391 let decimal_array = array
392 .clone()
393 .into_iter()
394 .collect::<Decimal128Array>()
395 .with_precision_and_scale(10, 3)?;
396
397 generic_decimal_to_other_test_cast!(
398 decimal_array,
399 Decimal128(10, 3),
400 Decimal128Array,
401 Decimal128(20, 6),
402 [
403 Some(1_234_000),
404 Some(2_222_000),
405 Some(3_000),
406 Some(4_000_000),
407 Some(5_000_000),
408 None
409 ],
410 None
411 );
412
413 let decimal_array = array
414 .into_iter()
415 .collect::<Decimal128Array>()
416 .with_precision_and_scale(10, 3)?;
417
418 generic_decimal_to_other_test_cast!(
419 decimal_array,
420 Decimal128(10, 3),
421 Decimal128Array,
422 Decimal128(10, 2),
423 [Some(123), Some(222), Some(0), Some(400), Some(500), None],
424 None
425 );
426
427 Ok(())
428 }
429
430 #[test]
431 fn test_cast_decimal_to_decimal_overflow() -> Result<()> {
432 let array = vec![Some(123456789)];
433
434 let decimal_array = array
435 .clone()
436 .into_iter()
437 .collect::<Decimal128Array>()
438 .with_precision_and_scale(10, 3)?;
439
440 let schema = Schema::new(vec![Field::new("a", Decimal128(10, 3), false)]);
441 let batch = RecordBatch::try_new(
442 Arc::new(schema.clone()),
443 vec![Arc::new(decimal_array)],
444 )?;
445 let expression =
446 cast_with_options(col("a", &schema)?, &schema, Decimal128(6, 2), None)?;
447 let e = expression.evaluate(&batch).unwrap_err().strip_backtrace(); assert_snapshot!(e, @"Arrow error: Invalid argument error: 123456.79 is too large to store in a Decimal128 of precision 6. Max is 9999.99");
449 let expression_safe = cast_with_options(
451 col("a", &schema)?,
452 &schema,
453 Decimal128(6, 2),
454 Some(DEFAULT_SAFE_CAST_OPTIONS),
455 )?;
456 let result_safe = expression_safe
457 .evaluate(&batch)?
458 .into_array(batch.num_rows())
459 .expect("failed to convert to array");
460
461 assert!(result_safe.is_null(0));
462
463 Ok(())
464 }
465
466 #[test]
467 fn test_cast_decimal_to_numeric() -> Result<()> {
468 let array = vec![Some(1), Some(2), Some(3), Some(4), Some(5), None];
469 let decimal_array = array
471 .clone()
472 .into_iter()
473 .collect::<Decimal128Array>()
474 .with_precision_and_scale(10, 0)?;
475 generic_decimal_to_other_test_cast!(
476 decimal_array,
477 Decimal128(10, 0),
478 Int8Array,
479 Int8,
480 [
481 Some(1_i8),
482 Some(2_i8),
483 Some(3_i8),
484 Some(4_i8),
485 Some(5_i8),
486 None
487 ],
488 None
489 );
490
491 let decimal_array = array
493 .clone()
494 .into_iter()
495 .collect::<Decimal128Array>()
496 .with_precision_and_scale(10, 0)?;
497 generic_decimal_to_other_test_cast!(
498 decimal_array,
499 Decimal128(10, 0),
500 Int16Array,
501 Int16,
502 [
503 Some(1_i16),
504 Some(2_i16),
505 Some(3_i16),
506 Some(4_i16),
507 Some(5_i16),
508 None
509 ],
510 None
511 );
512
513 let decimal_array = array
515 .clone()
516 .into_iter()
517 .collect::<Decimal128Array>()
518 .with_precision_and_scale(10, 0)?;
519 generic_decimal_to_other_test_cast!(
520 decimal_array,
521 Decimal128(10, 0),
522 Int32Array,
523 Int32,
524 [
525 Some(1_i32),
526 Some(2_i32),
527 Some(3_i32),
528 Some(4_i32),
529 Some(5_i32),
530 None
531 ],
532 None
533 );
534
535 let decimal_array = array
537 .into_iter()
538 .collect::<Decimal128Array>()
539 .with_precision_and_scale(10, 0)?;
540 generic_decimal_to_other_test_cast!(
541 decimal_array,
542 Decimal128(10, 0),
543 Int64Array,
544 Int64,
545 [
546 Some(1_i64),
547 Some(2_i64),
548 Some(3_i64),
549 Some(4_i64),
550 Some(5_i64),
551 None
552 ],
553 None
554 );
555
556 let array = vec![
558 Some(1234),
559 Some(2222),
560 Some(3),
561 Some(4000),
562 Some(5000),
563 None,
564 ];
565 let decimal_array = array
566 .clone()
567 .into_iter()
568 .collect::<Decimal128Array>()
569 .with_precision_and_scale(10, 3)?;
570 generic_decimal_to_other_test_cast!(
571 decimal_array,
572 Decimal128(10, 3),
573 Float32Array,
574 Float32,
575 [
576 Some(1.234_f32),
577 Some(2.222_f32),
578 Some(0.003_f32),
579 Some(4.0_f32),
580 Some(5.0_f32),
581 None
582 ],
583 None
584 );
585
586 let decimal_array = array
588 .into_iter()
589 .collect::<Decimal128Array>()
590 .with_precision_and_scale(20, 6)?;
591 generic_decimal_to_other_test_cast!(
592 decimal_array,
593 Decimal128(20, 6),
594 Float64Array,
595 Float64,
596 [
597 Some(0.001234_f64),
598 Some(0.002222_f64),
599 Some(0.000003_f64),
600 Some(0.004_f64),
601 Some(0.005_f64),
602 None
603 ],
604 None
605 );
606 Ok(())
607 }
608
609 #[test]
610 fn test_cast_numeric_to_decimal() -> Result<()> {
611 generic_test_cast!(
613 Int8Array,
614 Int8,
615 vec![1, 2, 3, 4, 5],
616 Decimal128Array,
617 Decimal128(3, 0),
618 [Some(1), Some(2), Some(3), Some(4), Some(5)],
619 None
620 );
621
622 generic_test_cast!(
624 Int16Array,
625 Int16,
626 vec![1, 2, 3, 4, 5],
627 Decimal128Array,
628 Decimal128(5, 0),
629 [Some(1), Some(2), Some(3), Some(4), Some(5)],
630 None
631 );
632
633 generic_test_cast!(
635 Int32Array,
636 Int32,
637 vec![1, 2, 3, 4, 5],
638 Decimal128Array,
639 Decimal128(10, 0),
640 [Some(1), Some(2), Some(3), Some(4), Some(5)],
641 None
642 );
643
644 generic_test_cast!(
646 Int64Array,
647 Int64,
648 vec![1, 2, 3, 4, 5],
649 Decimal128Array,
650 Decimal128(20, 0),
651 [Some(1), Some(2), Some(3), Some(4), Some(5)],
652 None
653 );
654
655 generic_test_cast!(
657 Int64Array,
658 Int64,
659 vec![1, 2, 3, 4, 5],
660 Decimal128Array,
661 Decimal128(20, 2),
662 [Some(100), Some(200), Some(300), Some(400), Some(500)],
663 None
664 );
665
666 generic_test_cast!(
668 Float32Array,
669 Float32,
670 vec![1.5, 2.5, 3.0, 1.123_456_8, 5.50],
671 Decimal128Array,
672 Decimal128(10, 2),
673 [Some(150), Some(250), Some(300), Some(112), Some(550)],
674 None
675 );
676
677 generic_test_cast!(
679 Float64Array,
680 Float64,
681 vec![1.5, 2.5, 3.0, 1.123_456_8, 5.50],
682 Decimal128Array,
683 Decimal128(20, 4),
684 [
685 Some(15000),
686 Some(25000),
687 Some(30000),
688 Some(11235),
689 Some(55000)
690 ],
691 None
692 );
693 Ok(())
694 }
695
696 #[test]
697 fn test_cast_i32_u32() -> Result<()> {
698 generic_test_cast!(
699 Int32Array,
700 Int32,
701 vec![1, 2, 3, 4, 5],
702 UInt32Array,
703 UInt32,
704 [
705 Some(1_u32),
706 Some(2_u32),
707 Some(3_u32),
708 Some(4_u32),
709 Some(5_u32)
710 ],
711 None
712 );
713 Ok(())
714 }
715
716 #[test]
717 fn test_cast_i32_utf8() -> Result<()> {
718 generic_test_cast!(
719 Int32Array,
720 Int32,
721 vec![1, 2, 3, 4, 5],
722 StringArray,
723 Utf8,
724 [Some("1"), Some("2"), Some("3"), Some("4"), Some("5")],
725 None
726 );
727 Ok(())
728 }
729
730 #[test]
731 fn test_cast_i64_t64() -> Result<()> {
732 let original = vec![1, 2, 3, 4, 5];
733 let expected: Vec<Option<i64>> = original
734 .iter()
735 .map(|i| Some(Time64NanosecondArray::from(vec![*i]).value(0)))
736 .collect();
737 generic_test_cast!(
738 Int64Array,
739 Int64,
740 original,
741 TimestampNanosecondArray,
742 Timestamp(TimeUnit::Nanosecond, None),
743 expected,
744 None
745 );
746 Ok(())
747 }
748
749 #[test]
753 fn invalid_cast() {
754 let schema = Schema::new(vec![Field::new("a", Int32, false)]);
756
757 let result = cast(
758 col("a", &schema).unwrap(),
759 &schema,
760 Interval(IntervalUnit::MonthDayNano),
761 );
762 result.expect_err("expected Invalid CAST");
763 }
764
765 #[test]
766 fn invalid_cast_with_options_error() -> Result<()> {
767 let schema = Schema::new(vec![Field::new("a", Utf8, false)]);
769 let a = StringArray::from(vec!["9.1"]);
770 let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
771 let expression = cast_with_options(col("a", &schema)?, &schema, Int32, None)?;
772 let result = expression.evaluate(&batch);
773
774 match result {
775 Ok(_) => panic!("expected error"),
776 Err(e) => {
777 assert!(
778 e.to_string()
779 .contains("Cannot cast string '9.1' to value of Int32 type")
780 )
781 }
782 }
783 Ok(())
784 }
785
786 #[test]
787 #[ignore] fn test_cast_decimal() -> Result<()> {
789 let schema = Schema::new(vec![Field::new("a", Int64, false)]);
790 let a = Int64Array::from(vec![100]);
791 let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
792 let expression =
793 cast_with_options(col("a", &schema)?, &schema, Decimal128(38, 38), None)?;
794 expression.evaluate(&batch)?;
795 Ok(())
796 }
797
798 #[test]
799 fn test_fmt_sql() -> Result<()> {
800 let schema = Schema::new(vec![Field::new("a", Int32, true)]);
801
802 let expr = cast(col("a", &schema)?, &schema, Int64)?;
804 let display_string = expr.to_string();
805 assert_eq!(display_string, "CAST(a@0 AS Int64)");
806 let sql_string = fmt_sql(expr.as_ref()).to_string();
807 assert_eq!(sql_string, "CAST(a AS Int64)");
808
809 let schema = Schema::new(vec![Field::new("b", Utf8, true)]);
811 let expr = cast(col("b", &schema)?, &schema, Int32)?;
812 let display_string = expr.to_string();
813 assert_eq!(display_string, "CAST(b@0 AS Int32)");
814 let sql_string = fmt_sql(expr.as_ref()).to_string();
815 assert_eq!(sql_string, "CAST(b AS Int32)");
816
817 Ok(())
818 }
819}