Skip to main content

datafusion_physical_expr/expressions/
cast.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use 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::nested_struct::validate_struct_compatibility;
30use datafusion_common::{Result, not_impl_err};
31use datafusion_expr_common::columnar_value::ColumnarValue;
32use datafusion_expr_common::interval_arithmetic::Interval;
33use datafusion_expr_common::sort_properties::ExprProperties;
34
35const DEFAULT_CAST_OPTIONS: CastOptions<'static> = CastOptions {
36    safe: false,
37    format_options: DEFAULT_FORMAT_OPTIONS,
38};
39
40const DEFAULT_SAFE_CAST_OPTIONS: CastOptions<'static> = CastOptions {
41    safe: true,
42    format_options: DEFAULT_FORMAT_OPTIONS,
43};
44
45/// Check if struct-to-struct casting is allowed by validating field compatibility.
46///
47/// This function applies the same validation rules as execution time to ensure
48/// planning-time validation matches runtime validation, enabling fail-fast behavior
49/// instead of deferring errors to execution.
50fn can_cast_struct_types(source: &DataType, target: &DataType) -> bool {
51    match (source, target) {
52        (Struct(source_fields), Struct(target_fields)) => {
53            // Apply the same struct compatibility rules as at execution time.
54            // This ensures planning-time validation matches execution-time validation.
55            validate_struct_compatibility(source_fields, target_fields).is_ok()
56        }
57        _ => false,
58    }
59}
60
61/// CAST expression casts an expression to a specific data type and returns a runtime error on invalid cast
62#[derive(Debug, Clone, Eq)]
63pub struct CastExpr {
64    /// The expression to cast
65    pub expr: Arc<dyn PhysicalExpr>,
66    /// The data type to cast to
67    cast_type: DataType,
68    /// Cast options
69    cast_options: CastOptions<'static>,
70}
71
72// Manually derive PartialEq and Hash to work around https://github.com/rust-lang/rust/issues/78808
73impl PartialEq for CastExpr {
74    fn eq(&self, other: &Self) -> bool {
75        self.expr.eq(&other.expr)
76            && self.cast_type.eq(&other.cast_type)
77            && self.cast_options.eq(&other.cast_options)
78    }
79}
80
81impl Hash for CastExpr {
82    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
83        self.expr.hash(state);
84        self.cast_type.hash(state);
85        self.cast_options.hash(state);
86    }
87}
88
89impl CastExpr {
90    /// Create a new CastExpr
91    pub fn new(
92        expr: Arc<dyn PhysicalExpr>,
93        cast_type: DataType,
94        cast_options: Option<CastOptions<'static>>,
95    ) -> Self {
96        Self {
97            expr,
98            cast_type,
99            cast_options: cast_options.unwrap_or(DEFAULT_CAST_OPTIONS),
100        }
101    }
102
103    /// The expression to cast
104    pub fn expr(&self) -> &Arc<dyn PhysicalExpr> {
105        &self.expr
106    }
107
108    /// The data type to cast to
109    pub fn cast_type(&self) -> &DataType {
110        &self.cast_type
111    }
112
113    /// The cast options
114    pub fn cast_options(&self) -> &CastOptions<'static> {
115        &self.cast_options
116    }
117
118    /// Check if casting from the specified source type to the target type is a
119    /// widening cast (e.g. from `Int8` to `Int16`).
120    pub fn check_bigger_cast(cast_type: &DataType, src: &DataType) -> bool {
121        if cast_type.eq(src) {
122            return true;
123        }
124        matches!(
125            (src, cast_type),
126            (Int8, Int16 | Int32 | Int64)
127                | (Int16, Int32 | Int64)
128                | (Int32, Int64)
129                | (UInt8, UInt16 | UInt32 | UInt64)
130                | (UInt16, UInt32 | UInt64)
131                | (UInt32, UInt64)
132                | (
133                    Int8 | Int16 | Int32 | UInt8 | UInt16 | UInt32,
134                    Float32 | Float64
135                )
136                | (Int64 | UInt64, Float64)
137                | (Utf8, LargeUtf8)
138        )
139    }
140
141    /// Check if the cast is a widening cast (e.g. from `Int8` to `Int16`).
142    pub fn is_bigger_cast(&self, src: &DataType) -> bool {
143        Self::check_bigger_cast(&self.cast_type, src)
144    }
145}
146
147impl fmt::Display for CastExpr {
148    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
149        write!(f, "CAST({} AS {})", self.expr, self.cast_type)
150    }
151}
152
153impl PhysicalExpr for CastExpr {
154    /// Return a reference to Any that can be used for downcasting
155    fn as_any(&self) -> &dyn Any {
156        self
157    }
158
159    fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
160        Ok(self.cast_type.clone())
161    }
162
163    fn nullable(&self, input_schema: &Schema) -> Result<bool> {
164        self.expr.nullable(input_schema)
165    }
166
167    fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
168        let value = self.expr.evaluate(batch)?;
169        value.cast_to(&self.cast_type, Some(&self.cast_options))
170    }
171
172    fn return_field(&self, input_schema: &Schema) -> Result<FieldRef> {
173        Ok(self
174            .expr
175            .return_field(input_schema)?
176            .as_ref()
177            .clone()
178            .with_data_type(self.cast_type.clone())
179            .into())
180    }
181
182    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
183        vec![&self.expr]
184    }
185
186    fn with_new_children(
187        self: Arc<Self>,
188        children: Vec<Arc<dyn PhysicalExpr>>,
189    ) -> Result<Arc<dyn PhysicalExpr>> {
190        Ok(Arc::new(CastExpr::new(
191            Arc::clone(&children[0]),
192            self.cast_type.clone(),
193            Some(self.cast_options.clone()),
194        )))
195    }
196
197    fn evaluate_bounds(&self, children: &[&Interval]) -> Result<Interval> {
198        // Cast current node's interval to the right type:
199        children[0].cast_to(&self.cast_type, &self.cast_options)
200    }
201
202    fn propagate_constraints(
203        &self,
204        interval: &Interval,
205        children: &[&Interval],
206    ) -> Result<Option<Vec<Interval>>> {
207        let child_interval = children[0];
208        // Get child's datatype:
209        let cast_type = child_interval.data_type();
210        Ok(Some(vec![
211            interval.cast_to(&cast_type, &DEFAULT_SAFE_CAST_OPTIONS)?,
212        ]))
213    }
214
215    /// A [`CastExpr`] preserves the ordering of its child if the cast is done
216    /// under the same datatype family.
217    fn get_properties(&self, children: &[ExprProperties]) -> Result<ExprProperties> {
218        let source_datatype = children[0].range.data_type();
219        let target_type = &self.cast_type;
220
221        let unbounded = Interval::make_unbounded(target_type)?;
222        if (source_datatype.is_numeric() || source_datatype == Boolean)
223            && target_type.is_numeric()
224            || source_datatype.is_temporal() && target_type.is_temporal()
225            || source_datatype.eq(target_type)
226        {
227            Ok(children[0].clone().with_range(unbounded))
228        } else {
229            Ok(ExprProperties::new_unknown().with_range(unbounded))
230        }
231    }
232
233    fn fmt_sql(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234        write!(f, "CAST(")?;
235        self.expr.fmt_sql(f)?;
236        write!(f, " AS {:?}", self.cast_type)?;
237
238        write!(f, ")")
239    }
240}
241
242/// Return a PhysicalExpression representing `expr` casted to
243/// `cast_type`, if any casting is needed.
244///
245/// Note that such casts may lose type information
246pub fn cast_with_options(
247    expr: Arc<dyn PhysicalExpr>,
248    input_schema: &Schema,
249    cast_type: DataType,
250    cast_options: Option<CastOptions<'static>>,
251) -> Result<Arc<dyn PhysicalExpr>> {
252    let expr_type = expr.data_type(input_schema)?;
253    if expr_type == cast_type {
254        Ok(Arc::clone(&expr))
255    } else if can_cast_types(&expr_type, &cast_type) {
256        Ok(Arc::new(CastExpr::new(expr, cast_type, cast_options)))
257    } else if can_cast_struct_types(&expr_type, &cast_type) {
258        // Allow struct-to-struct casts that pass name-based compatibility validation.
259        // This validation is applied at planning time (now) to fail fast, rather than
260        // deferring errors to execution time. The name-based casting logic will be
261        // executed at runtime via ColumnarValue::cast_to.
262        Ok(Arc::new(CastExpr::new(expr, cast_type, cast_options)))
263    } else {
264        not_impl_err!("Unsupported CAST from {expr_type} to {cast_type}")
265    }
266}
267
268/// Return a PhysicalExpression representing `expr` casted to
269/// `cast_type`, if any casting is needed.
270///
271/// Note that such casts may lose type information
272pub fn cast(
273    expr: Arc<dyn PhysicalExpr>,
274    input_schema: &Schema,
275    cast_type: DataType,
276) -> Result<Arc<dyn PhysicalExpr>> {
277    cast_with_options(expr, input_schema, cast_type, None)
278}
279
280#[cfg(test)]
281mod tests {
282    use super::*;
283
284    use crate::expressions::column::col;
285
286    use arrow::{
287        array::{
288            Array, Decimal128Array, Float32Array, Float64Array, Int8Array, Int16Array,
289            Int32Array, Int64Array, StringArray, Time64NanosecondArray,
290            TimestampNanosecondArray, UInt32Array,
291        },
292        datatypes::*,
293    };
294    use datafusion_physical_expr_common::physical_expr::fmt_sql;
295    use insta::assert_snapshot;
296
297    // runs an end-to-end test of physical type cast
298    // 1. construct a record batch with a column "a" of type A
299    // 2. construct a physical expression of CAST(a AS B)
300    // 3. evaluate the expression
301    // 4. verify that the resulting expression is of type B
302    // 5. verify that the resulting values are downcastable and correct
303    macro_rules! generic_decimal_to_other_test_cast {
304        ($DECIMAL_ARRAY:ident, $A_TYPE:expr, $TYPEARRAY:ident, $TYPE:expr, $VEC:expr,$CAST_OPTIONS:expr) => {{
305            let schema = Schema::new(vec![Field::new("a", $A_TYPE, true)]);
306            let batch = RecordBatch::try_new(
307                Arc::new(schema.clone()),
308                vec![Arc::new($DECIMAL_ARRAY)],
309            )?;
310            // verify that we can construct the expression
311            let expression =
312                cast_with_options(col("a", &schema)?, &schema, $TYPE, $CAST_OPTIONS)?;
313
314            // verify that its display is correct
315            assert_eq!(format!("CAST(a@0 AS {})", $TYPE), format!("{}", expression));
316
317            // verify that the expression's type is correct
318            assert_eq!(expression.data_type(&schema)?, $TYPE);
319
320            // compute
321            let result = expression
322                .evaluate(&batch)?
323                .into_array(batch.num_rows())
324                .expect("Failed to convert to array");
325
326            // verify that the array's data_type is correct
327            assert_eq!(*result.data_type(), $TYPE);
328
329            // verify that the data itself is downcastable
330            let result = result
331                .as_any()
332                .downcast_ref::<$TYPEARRAY>()
333                .expect("failed to downcast");
334
335            // verify that the result itself is correct
336            for (i, x) in $VEC.iter().enumerate() {
337                match x {
338                    Some(x) => assert_eq!(result.value(i), *x),
339                    None => assert!(result.is_null(i)),
340                }
341            }
342        }};
343    }
344
345    // runs an end-to-end test of physical type cast
346    // 1. construct a record batch with a column "a" of type A
347    // 2. construct a physical expression of CAST(a AS B)
348    // 3. evaluate the expression
349    // 4. verify that the resulting expression is of type B
350    // 5. verify that the resulting values are downcastable and correct
351    macro_rules! generic_test_cast {
352        ($A_ARRAY:ident, $A_TYPE:expr, $A_VEC:expr, $TYPEARRAY:ident, $TYPE:expr, $VEC:expr, $CAST_OPTIONS:expr) => {{
353            let schema = Schema::new(vec![Field::new("a", $A_TYPE, true)]);
354            let a_vec_len = $A_VEC.len();
355            let a = $A_ARRAY::from($A_VEC);
356            let batch =
357                RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
358
359            // verify that we can construct the expression
360            let expression =
361                cast_with_options(col("a", &schema)?, &schema, $TYPE, $CAST_OPTIONS)?;
362
363            // verify that its display is correct
364            assert_eq!(format!("CAST(a@0 AS {})", $TYPE), format!("{}", expression));
365
366            // verify that the expression's type is correct
367            assert_eq!(expression.data_type(&schema)?, $TYPE);
368
369            // compute
370            let result = expression
371                .evaluate(&batch)?
372                .into_array(batch.num_rows())
373                .expect("Failed to convert to array");
374
375            // verify that the array's data_type is correct
376            assert_eq!(*result.data_type(), $TYPE);
377
378            // verify that the len is correct
379            assert_eq!(result.len(), a_vec_len);
380
381            // verify that the data itself is downcastable
382            let result = result
383                .as_any()
384                .downcast_ref::<$TYPEARRAY>()
385                .expect("failed to downcast");
386
387            // verify that the result itself is correct
388            for (i, x) in $VEC.iter().enumerate() {
389                match x {
390                    Some(x) => assert_eq!(result.value(i), *x),
391                    None => assert!(result.is_null(i)),
392                }
393            }
394        }};
395    }
396
397    #[test]
398    fn test_cast_decimal_to_decimal() -> Result<()> {
399        let array = vec![
400            Some(1234),
401            Some(2222),
402            Some(3),
403            Some(4000),
404            Some(5000),
405            None,
406        ];
407
408        let decimal_array = array
409            .clone()
410            .into_iter()
411            .collect::<Decimal128Array>()
412            .with_precision_and_scale(10, 3)?;
413
414        generic_decimal_to_other_test_cast!(
415            decimal_array,
416            Decimal128(10, 3),
417            Decimal128Array,
418            Decimal128(20, 6),
419            [
420                Some(1_234_000),
421                Some(2_222_000),
422                Some(3_000),
423                Some(4_000_000),
424                Some(5_000_000),
425                None
426            ],
427            None
428        );
429
430        let decimal_array = array
431            .into_iter()
432            .collect::<Decimal128Array>()
433            .with_precision_and_scale(10, 3)?;
434
435        generic_decimal_to_other_test_cast!(
436            decimal_array,
437            Decimal128(10, 3),
438            Decimal128Array,
439            Decimal128(10, 2),
440            [Some(123), Some(222), Some(0), Some(400), Some(500), None],
441            None
442        );
443
444        Ok(())
445    }
446
447    #[test]
448    fn test_cast_decimal_to_decimal_overflow() -> Result<()> {
449        let array = vec![Some(123456789)];
450
451        let decimal_array = array
452            .clone()
453            .into_iter()
454            .collect::<Decimal128Array>()
455            .with_precision_and_scale(10, 3)?;
456
457        let schema = Schema::new(vec![Field::new("a", Decimal128(10, 3), false)]);
458        let batch = RecordBatch::try_new(
459            Arc::new(schema.clone()),
460            vec![Arc::new(decimal_array)],
461        )?;
462        let expression =
463            cast_with_options(col("a", &schema)?, &schema, Decimal128(6, 2), None)?;
464        let e = expression.evaluate(&batch).unwrap_err().strip_backtrace(); // panics on OK
465        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");
466        // safe cast should return null
467        let expression_safe = cast_with_options(
468            col("a", &schema)?,
469            &schema,
470            Decimal128(6, 2),
471            Some(DEFAULT_SAFE_CAST_OPTIONS),
472        )?;
473        let result_safe = expression_safe
474            .evaluate(&batch)?
475            .into_array(batch.num_rows())
476            .expect("failed to convert to array");
477
478        assert!(result_safe.is_null(0));
479
480        Ok(())
481    }
482
483    #[test]
484    fn test_cast_decimal_to_numeric() -> Result<()> {
485        let array = vec![Some(1), Some(2), Some(3), Some(4), Some(5), None];
486        // decimal to i8
487        let decimal_array = array
488            .clone()
489            .into_iter()
490            .collect::<Decimal128Array>()
491            .with_precision_and_scale(10, 0)?;
492        generic_decimal_to_other_test_cast!(
493            decimal_array,
494            Decimal128(10, 0),
495            Int8Array,
496            Int8,
497            [
498                Some(1_i8),
499                Some(2_i8),
500                Some(3_i8),
501                Some(4_i8),
502                Some(5_i8),
503                None
504            ],
505            None
506        );
507
508        // decimal to i16
509        let decimal_array = array
510            .clone()
511            .into_iter()
512            .collect::<Decimal128Array>()
513            .with_precision_and_scale(10, 0)?;
514        generic_decimal_to_other_test_cast!(
515            decimal_array,
516            Decimal128(10, 0),
517            Int16Array,
518            Int16,
519            [
520                Some(1_i16),
521                Some(2_i16),
522                Some(3_i16),
523                Some(4_i16),
524                Some(5_i16),
525                None
526            ],
527            None
528        );
529
530        // decimal to i32
531        let decimal_array = array
532            .clone()
533            .into_iter()
534            .collect::<Decimal128Array>()
535            .with_precision_and_scale(10, 0)?;
536        generic_decimal_to_other_test_cast!(
537            decimal_array,
538            Decimal128(10, 0),
539            Int32Array,
540            Int32,
541            [
542                Some(1_i32),
543                Some(2_i32),
544                Some(3_i32),
545                Some(4_i32),
546                Some(5_i32),
547                None
548            ],
549            None
550        );
551
552        // decimal to i64
553        let decimal_array = array
554            .into_iter()
555            .collect::<Decimal128Array>()
556            .with_precision_and_scale(10, 0)?;
557        generic_decimal_to_other_test_cast!(
558            decimal_array,
559            Decimal128(10, 0),
560            Int64Array,
561            Int64,
562            [
563                Some(1_i64),
564                Some(2_i64),
565                Some(3_i64),
566                Some(4_i64),
567                Some(5_i64),
568                None
569            ],
570            None
571        );
572
573        // decimal to float32
574        let array = vec![
575            Some(1234),
576            Some(2222),
577            Some(3),
578            Some(4000),
579            Some(5000),
580            None,
581        ];
582        let decimal_array = array
583            .clone()
584            .into_iter()
585            .collect::<Decimal128Array>()
586            .with_precision_and_scale(10, 3)?;
587        generic_decimal_to_other_test_cast!(
588            decimal_array,
589            Decimal128(10, 3),
590            Float32Array,
591            Float32,
592            [
593                Some(1.234_f32),
594                Some(2.222_f32),
595                Some(0.003_f32),
596                Some(4.0_f32),
597                Some(5.0_f32),
598                None
599            ],
600            None
601        );
602
603        // decimal to float64
604        let decimal_array = array
605            .into_iter()
606            .collect::<Decimal128Array>()
607            .with_precision_and_scale(20, 6)?;
608        generic_decimal_to_other_test_cast!(
609            decimal_array,
610            Decimal128(20, 6),
611            Float64Array,
612            Float64,
613            [
614                Some(0.001234_f64),
615                Some(0.002222_f64),
616                Some(0.000003_f64),
617                Some(0.004_f64),
618                Some(0.005_f64),
619                None
620            ],
621            None
622        );
623        Ok(())
624    }
625
626    #[test]
627    fn test_cast_numeric_to_decimal() -> Result<()> {
628        // int8
629        generic_test_cast!(
630            Int8Array,
631            Int8,
632            vec![1, 2, 3, 4, 5],
633            Decimal128Array,
634            Decimal128(3, 0),
635            [Some(1), Some(2), Some(3), Some(4), Some(5)],
636            None
637        );
638
639        // int16
640        generic_test_cast!(
641            Int16Array,
642            Int16,
643            vec![1, 2, 3, 4, 5],
644            Decimal128Array,
645            Decimal128(5, 0),
646            [Some(1), Some(2), Some(3), Some(4), Some(5)],
647            None
648        );
649
650        // int32
651        generic_test_cast!(
652            Int32Array,
653            Int32,
654            vec![1, 2, 3, 4, 5],
655            Decimal128Array,
656            Decimal128(10, 0),
657            [Some(1), Some(2), Some(3), Some(4), Some(5)],
658            None
659        );
660
661        // int64
662        generic_test_cast!(
663            Int64Array,
664            Int64,
665            vec![1, 2, 3, 4, 5],
666            Decimal128Array,
667            Decimal128(20, 0),
668            [Some(1), Some(2), Some(3), Some(4), Some(5)],
669            None
670        );
671
672        // int64 to different scale
673        generic_test_cast!(
674            Int64Array,
675            Int64,
676            vec![1, 2, 3, 4, 5],
677            Decimal128Array,
678            Decimal128(20, 2),
679            [Some(100), Some(200), Some(300), Some(400), Some(500)],
680            None
681        );
682
683        // float32
684        generic_test_cast!(
685            Float32Array,
686            Float32,
687            vec![1.5, 2.5, 3.0, 1.123_456_8, 5.50],
688            Decimal128Array,
689            Decimal128(10, 2),
690            [Some(150), Some(250), Some(300), Some(112), Some(550)],
691            None
692        );
693
694        // float64
695        generic_test_cast!(
696            Float64Array,
697            Float64,
698            vec![1.5, 2.5, 3.0, 1.123_456_8, 5.50],
699            Decimal128Array,
700            Decimal128(20, 4),
701            [
702                Some(15000),
703                Some(25000),
704                Some(30000),
705                Some(11235),
706                Some(55000)
707            ],
708            None
709        );
710        Ok(())
711    }
712
713    #[test]
714    fn test_cast_i32_u32() -> Result<()> {
715        generic_test_cast!(
716            Int32Array,
717            Int32,
718            vec![1, 2, 3, 4, 5],
719            UInt32Array,
720            UInt32,
721            [
722                Some(1_u32),
723                Some(2_u32),
724                Some(3_u32),
725                Some(4_u32),
726                Some(5_u32)
727            ],
728            None
729        );
730        Ok(())
731    }
732
733    #[test]
734    fn test_cast_i32_utf8() -> Result<()> {
735        generic_test_cast!(
736            Int32Array,
737            Int32,
738            vec![1, 2, 3, 4, 5],
739            StringArray,
740            Utf8,
741            [Some("1"), Some("2"), Some("3"), Some("4"), Some("5")],
742            None
743        );
744        Ok(())
745    }
746
747    #[test]
748    fn test_cast_i64_t64() -> Result<()> {
749        let original = vec![1, 2, 3, 4, 5];
750        let expected: Vec<Option<i64>> = original
751            .iter()
752            .map(|i| Some(Time64NanosecondArray::from(vec![*i]).value(0)))
753            .collect();
754        generic_test_cast!(
755            Int64Array,
756            Int64,
757            original,
758            TimestampNanosecondArray,
759            Timestamp(TimeUnit::Nanosecond, None),
760            expected,
761            None
762        );
763        Ok(())
764    }
765
766    // Tests for timestamp timezone casting have been moved to timestamps.slt
767    // See the "Casting between timestamp with and without timezone" section
768
769    #[test]
770    fn invalid_cast() {
771        // Ensure a useful error happens at plan time if invalid casts are used
772        let schema = Schema::new(vec![Field::new("a", Int32, false)]);
773
774        let result = cast(
775            col("a", &schema).unwrap(),
776            &schema,
777            Interval(IntervalUnit::MonthDayNano),
778        );
779        result.expect_err("expected Invalid CAST");
780    }
781
782    #[test]
783    fn invalid_cast_with_options_error() -> Result<()> {
784        // Ensure a useful error happens at plan time if invalid casts are used
785        let schema = Schema::new(vec![Field::new("a", Utf8, false)]);
786        let a = StringArray::from(vec!["9.1"]);
787        let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
788        let expression = cast_with_options(col("a", &schema)?, &schema, Int32, None)?;
789        let result = expression.evaluate(&batch);
790
791        match result {
792            Ok(_) => panic!("expected error"),
793            Err(e) => {
794                assert!(
795                    e.to_string()
796                        .contains("Cannot cast string '9.1' to value of Int32 type")
797                )
798            }
799        }
800        Ok(())
801    }
802
803    #[test]
804    #[ignore] // TODO: https://github.com/apache/datafusion/issues/5396
805    fn test_cast_decimal() -> Result<()> {
806        let schema = Schema::new(vec![Field::new("a", Int64, false)]);
807        let a = Int64Array::from(vec![100]);
808        let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
809        let expression =
810            cast_with_options(col("a", &schema)?, &schema, Decimal128(38, 38), None)?;
811        expression.evaluate(&batch)?;
812        Ok(())
813    }
814
815    #[test]
816    fn test_fmt_sql() -> Result<()> {
817        let schema = Schema::new(vec![Field::new("a", Int32, true)]);
818
819        // Test numeric casting
820        let expr = cast(col("a", &schema)?, &schema, Int64)?;
821        let display_string = expr.to_string();
822        assert_eq!(display_string, "CAST(a@0 AS Int64)");
823        let sql_string = fmt_sql(expr.as_ref()).to_string();
824        assert_eq!(sql_string, "CAST(a AS Int64)");
825
826        // Test string casting
827        let schema = Schema::new(vec![Field::new("b", Utf8, true)]);
828        let expr = cast(col("b", &schema)?, &schema, Int32)?;
829        let display_string = expr.to_string();
830        assert_eq!(display_string, "CAST(b@0 AS Int32)");
831        let sql_string = fmt_sql(expr.as_ref()).to_string();
832        assert_eq!(sql_string, "CAST(b AS Int32)");
833
834        Ok(())
835    }
836}