@@ -363,11 +363,13 @@ impl PhysicalExpr for CastExpr {
363363 interval : & Interval ,
364364 children : & [ & Interval ] ,
365365 ) -> Result < Option < Vec < Interval > > > {
366- let child_interval = children[ 0 ] ;
367- // Get child's datatype:
368- let cast_type = child_interval. data_type ( ) ;
366+ let source_type = children[ 0 ] . data_type ( ) ;
367+ let target_type = self . cast_type ( ) ;
368+ if !can_propagate_cast_constraints ( & source_type, target_type) {
369+ return Ok ( Some ( vec ! [ ] ) ) ;
370+ }
369371 Ok ( Some ( vec ! [
370- interval. cast_to( & cast_type , & DEFAULT_SAFE_CAST_OPTIONS ) ?,
372+ interval. cast_to( & source_type , & DEFAULT_SAFE_CAST_OPTIONS ) ?,
371373 ] ) )
372374 }
373375
@@ -404,6 +406,19 @@ impl PhysicalExpr for CastExpr {
404406 }
405407}
406408
409+ /// Whether output bounds can be cast back to `source` without excluding valid inputs.
410+ ///
411+ /// Used for reverse constraint propagation through a cast from `source` to `target`.
412+ /// Many-to-one casts, such as Float64 to Int32, cannot generally be inverted this way:
413+ /// an output of 0 does not imply an input of 0.0.
414+ /// Returns false for unrecognized conversions so the input range remains unchanged.
415+ fn can_propagate_cast_constraints ( source : & DataType , target : & DataType ) -> bool {
416+ CastExpr :: check_bigger_cast ( target, source)
417+ || ( source. is_integer ( ) && target. is_integer ( ) )
418+ // NaN bounds are unbounded; finite Float32 values widen exactly.
419+ || ( * source == Float32 && * target == Float64 )
420+ }
421+
407422#[ cfg( feature = "proto" ) ]
408423impl CastExpr {
409424 /// Reconstruct a [`CastExpr`] from its protobuf representation.
@@ -572,10 +587,121 @@ mod tests {
572587 as_boolean_array, as_int64_array, as_string_array, as_struct_array,
573588 as_uint8_array,
574589 } ;
590+ use datafusion_common:: rounding:: { next_down, next_up} ;
575591 use datafusion_physical_expr_common:: physical_expr:: fmt_sql;
576592 use insta:: assert_snapshot;
577593 use std:: collections:: HashMap ;
578594
595+ #[ test]
596+ fn test_cast_constraint_propagation ( ) -> Result < ( ) > {
597+ for ( source, target, propagates) in [
598+ ( Utf8 , Int32 , false ) ,
599+ ( Utf8View , Int32 , false ) ,
600+ ( Timestamp ( TimeUnit :: Nanosecond , None ) , Date32 , false ) ,
601+ // branch-55 does not recognize integer/date casts in check_bigger_cast.
602+ ( Int32 , Date32 , false ) ,
603+ ( Date32 , Int32 , false ) ,
604+ ( Utf8 , LargeUtf8 , true ) ,
605+ ( Utf8 , Utf8 , true ) ,
606+ ( Float64 , Int32 , false ) ,
607+ ( Int64 , Float32 , false ) ,
608+ ( Float64 , Float32 , false ) ,
609+ ( Decimal128 ( 4 , 1 ) , Decimal128 ( 4 , 0 ) , false ) ,
610+ ( Decimal128 ( 4 , 1 ) , Int32 , false ) ,
611+ ( Float64 , Decimal128 ( 4 , 1 ) , false ) ,
612+ ( Int8 , Int64 , true ) ,
613+ ( Int64 , Int8 , true ) ,
614+ ( Int32 , UInt32 , true ) ,
615+ ( UInt32 , Int32 , true ) ,
616+ ( Int32 , Float64 , true ) ,
617+ ( Float32 , Float64 , true ) ,
618+ ( Decimal128 ( 4 , 1 ) , Decimal128 ( 4 , 1 ) , true ) ,
619+ ] {
620+ let schema = Schema :: new ( vec ! [ Field :: new( "x" , source. clone( ) , true ) ] ) ;
621+ let expr = CastExpr :: new ( col ( "x" , & schema) ?, target. clone ( ) , None ) ;
622+ let input = Interval :: make_unbounded ( & source) ?;
623+ let value = ScalarValue :: Int32 ( Some ( 0 ) ) . cast_to ( & target) ?;
624+ let output = Interval :: from ( & value) ;
625+ let expected = if propagates {
626+ vec ! [ Interval :: from( & value. cast_to( & source) ?) ]
627+ } else {
628+ vec ! [ ]
629+ } ;
630+ assert_eq ! (
631+ expr. propagate_constraints( & output, & [ & input] ) ?,
632+ Some ( expected) ,
633+ "{source} -> {target}"
634+ ) ;
635+ }
636+ Ok ( ( ) )
637+ }
638+
639+ #[ test]
640+ fn test_float_widening_constraint_boundaries ( ) -> Result < ( ) > {
641+ let mut values = vec ! [
642+ f32 :: NEG_INFINITY ,
643+ -f32 :: MAX ,
644+ -1.0 ,
645+ -f32 :: MIN_POSITIVE ,
646+ -f32 :: from_bits( 1 ) ,
647+ -0.0 ,
648+ 0.0 ,
649+ f32 :: from_bits( 1 ) ,
650+ f32 :: MIN_POSITIVE ,
651+ 1.0 ,
652+ f32 :: MAX ,
653+ f32 :: INFINITY ,
654+ ] ;
655+ // Include both signs of signaling and quiet NaNs with distinct payloads.
656+ values. extend (
657+ [ 0x7f800001 , 0x7f800002 , 0x7fc00001 , 0xff800001 , 0xffc00001 ]
658+ . map ( f32:: from_bits) ,
659+ ) ;
660+ values. extend ( [ next_down ( 1.0f32 ) , next_up ( 1.0f32 ) ] ) ;
661+ let schema = Arc :: new ( Schema :: new ( vec ! [ Field :: new( "x" , Float32 , false ) ] ) ) ;
662+ let batch = RecordBatch :: try_new (
663+ Arc :: clone ( & schema) ,
664+ vec ! [ Arc :: new( Float32Array :: from( values. clone( ) ) ) ] ,
665+ ) ?;
666+ let expr = CastExpr :: new ( col ( "x" , & schema) ?, Float64 , None ) ;
667+ let array = expr. evaluate ( & batch) ?. into_array ( values. len ( ) ) ?;
668+ let widened = array. as_any ( ) . downcast_ref :: < Float64Array > ( ) . unwrap ( ) ;
669+ let mut bounds = vec ! [ -f64 :: MAX , f64 :: MAX ] ;
670+ for value in widened. values ( ) {
671+ bounds. extend ( [ next_down ( * value) , * value, next_up ( * value) ] ) ;
672+ }
673+ // Midpoints exercise rounding back to Float32 in both directions.
674+ bounds. extend ( [
675+ f64:: from ( f32:: from_bits ( 1 ) ) / 2.0 ,
676+ -f64:: from ( f32:: from_bits ( 1 ) ) / 2.0 ,
677+ f64:: midpoint ( 1.0 , f64:: from ( next_up ( 1.0f32 ) ) ) ,
678+ ] ) ;
679+ bounds. sort_by ( f64:: total_cmp) ;
680+ bounds. dedup_by ( |a, b| a. to_bits ( ) == b. to_bits ( ) ) ;
681+ let input = Interval :: make_unbounded ( & Float32 ) ?;
682+ for ( i, lower) in bounds. iter ( ) . enumerate ( ) {
683+ for upper in & bounds[ i..] {
684+ let output = Interval :: make ( Some ( * lower) , Some ( * upper) ) ?;
685+ let propagated = expr. propagate_constraints ( & output, & [ & input] ) ?. unwrap ( ) ;
686+ assert_eq ! ( propagated. len( ) , 1 ) ;
687+ for ( index, value) in values. iter ( ) . enumerate ( ) {
688+ if output. contains_value ( ScalarValue :: Float64 ( Some (
689+ widened. value ( index) ,
690+ ) ) ) ? {
691+ assert ! (
692+ propagated[ 0 ]
693+ . contains_value( ScalarValue :: Float32 ( Some ( * value) ) ) ?,
694+ "input bits={:08x}, output={output}, propagated={:?}" ,
695+ value. to_bits( ) ,
696+ propagated[ 0 ]
697+ ) ;
698+ }
699+ }
700+ }
701+ }
702+ Ok ( ( ) )
703+ }
704+
579705 fn make_struct_array ( fields : Fields , arrays : Vec < ArrayRef > ) -> StructArray {
580706 StructArray :: new ( fields, arrays, None )
581707 }
0 commit comments