@@ -74,6 +74,7 @@ use num_traits::AsPrimitive;
74
74
use super :: arithmetic_modulo:: vectorize_modulo;
75
75
use super :: decimal:: register_decimal_to_float32;
76
76
use super :: decimal:: register_decimal_to_float64;
77
+ use super :: decimal:: register_decimal_to_int;
77
78
use crate :: scalars:: decimal:: register_decimal_arithmetic;
78
79
79
80
pub fn register ( registry : & mut FunctionRegistry ) {
@@ -582,92 +583,96 @@ pub fn register_number_to_number(registry: &mut FunctionRegistry) {
582
583
let name = format!( "to_{dest_type}" ) . to_lowercase( ) ;
583
584
if src_type. can_lossless_cast_to( * dest_type) {
584
585
registry. register_1_arg:: <NumberType <SRC_TYPE >, NumberType <DEST_TYPE >, _, _>(
585
- & name,
586
- |_, domain| {
587
- let ( domain, overflowing) = domain. overflow_cast( ) ;
588
- debug_assert!( !overflowing) ;
589
- FunctionDomain :: Domain ( domain)
590
- } ,
591
- |val, _| {
592
- val. as_( )
593
- } ,
594
- ) ;
586
+ & name,
587
+ |_, domain| {
588
+ let ( domain, overflowing) = domain. overflow_cast( ) ;
589
+ debug_assert!( !overflowing) ;
590
+ FunctionDomain :: Domain ( domain)
591
+ } ,
592
+ |val, _| {
593
+ val. as_( )
594
+ } ,
595
+ ) ;
595
596
} else {
596
597
registry. register_passthrough_nullable_1_arg:: <NumberType <SRC_TYPE >, NumberType <DEST_TYPE >, _, _>(
597
- & name,
598
- |_, domain| {
599
- let ( domain, overflowing) = domain. overflow_cast( ) ;
600
- if overflowing {
601
- FunctionDomain :: MayThrow
602
- } else {
603
- FunctionDomain :: Domain ( domain)
604
- }
605
- } ,
606
- vectorize_with_builder_1_arg:: <NumberType <SRC_TYPE >, NumberType <DEST_TYPE >>(
607
- move |val, output, ctx| {
608
- match num_traits:: cast:: cast( val) {
609
- Some ( val) => output. push( val) ,
610
- None => {
611
- ctx. set_error( output. len( ) , "number overflowed" ) ;
612
- output. push( DEST_TYPE :: default ( ) ) ;
598
+ & name,
599
+ |_, domain| {
600
+ let ( domain, overflowing) = domain. overflow_cast( ) ;
601
+ if overflowing {
602
+ FunctionDomain :: MayThrow
603
+ } else {
604
+ FunctionDomain :: Domain ( domain)
605
+ }
613
606
} ,
614
- }
615
- }
616
- ) ,
617
- ) ;
607
+ vectorize_with_builder_1_arg:: <NumberType <SRC_TYPE >, NumberType <DEST_TYPE >>(
608
+ move |val, output, ctx| {
609
+ match num_traits:: cast:: cast( val) {
610
+ Some ( val) => output. push( val) ,
611
+ None => {
612
+ ctx. set_error( output. len( ) , "number overflowed" ) ;
613
+ output. push( DEST_TYPE :: default ( ) ) ;
614
+ } ,
615
+ }
616
+ }
617
+ ) ,
618
+ ) ;
618
619
}
619
620
620
621
let name = format!( "try_to_{dest_type}" ) . to_lowercase( ) ;
621
622
if src_type. can_lossless_cast_to( * dest_type) {
622
623
registry. register_combine_nullable_1_arg:: <NumberType <SRC_TYPE >, NumberType <DEST_TYPE >, _, _>(
623
- & name,
624
- |_, domain| {
625
- let ( domain, overflowing) = domain. overflow_cast( ) ;
626
- debug_assert!( !overflowing) ;
627
- FunctionDomain :: Domain ( NullableDomain {
628
- has_null: false ,
629
- value: Some ( Box :: new(
630
- domain,
631
- ) ) ,
632
- } )
633
- } ,
634
- vectorize_1_arg:: <NumberType <SRC_TYPE >, NullableType <NumberType <DEST_TYPE >>>( |val, _| {
635
- Some ( val. as_( ) )
636
- } )
637
- ) ;
624
+ & name,
625
+ |_, domain| {
626
+ let ( domain, overflowing) = domain. overflow_cast( ) ;
627
+ debug_assert!( !overflowing) ;
628
+ FunctionDomain :: Domain ( NullableDomain {
629
+ has_null: false ,
630
+ value: Some ( Box :: new(
631
+ domain,
632
+ ) ) ,
633
+ } )
634
+ } ,
635
+ vectorize_1_arg:: <NumberType <SRC_TYPE >, NullableType <NumberType <DEST_TYPE >>>( |val, _| {
636
+ Some ( val. as_( ) )
637
+ } )
638
+ ) ;
638
639
} else {
639
640
registry. register_combine_nullable_1_arg:: <NumberType <SRC_TYPE >, NumberType <DEST_TYPE >, _, _>(
640
- & name,
641
- |_, domain| {
642
- let ( domain, overflowing) = domain. overflow_cast( ) ;
643
- FunctionDomain :: Domain ( NullableDomain {
644
- has_null: overflowing,
645
- value: Some ( Box :: new(
646
- domain,
647
- ) ) ,
648
- } )
649
- } ,
650
- vectorize_with_builder_1_arg:: <NumberType <SRC_TYPE >, NullableType <NumberType <DEST_TYPE >>>(
651
- |val, output, _| {
652
- if let Some ( new_val) = num_traits:: cast:: cast( val) {
653
- output. push( new_val) ;
654
- } else {
655
- output. push_null( ) ;
656
- }
657
- }
658
- ) ,
659
- ) ;
641
+ & name,
642
+ |_, domain| {
643
+ let ( domain, overflowing) = domain. overflow_cast( ) ;
644
+ FunctionDomain :: Domain ( NullableDomain {
645
+ has_null: overflowing,
646
+ value: Some ( Box :: new(
647
+ domain,
648
+ ) ) ,
649
+ } )
650
+ } ,
651
+ vectorize_with_builder_1_arg:: <NumberType <SRC_TYPE >, NullableType <NumberType <DEST_TYPE >>>(
652
+ |val, output, _| {
653
+ if let Some ( new_val) = num_traits:: cast:: cast( val) {
654
+ output. push( new_val) ;
655
+ } else {
656
+ output. push_null( ) ;
657
+ }
658
+ }
659
+ ) ,
660
+ ) ;
660
661
}
661
662
}
662
663
} ) ,
663
664
NumberClass :: Decimal128 => {
664
- // todo(youngsofun): add decimal try_cast and decimal to int
665
+ // todo(youngsofun): add decimal try_cast and decimal to int and float
665
666
if matches!( dest_type, NumberDataType :: Float32 ) {
666
667
register_decimal_to_float32( registry) ;
667
668
}
668
669
if matches!( dest_type, NumberDataType :: Float64 ) {
669
670
register_decimal_to_float64( registry) ;
670
671
}
672
+
673
+ with_number_mapped_type!( |DEST_TYPE | match dest_type {
674
+ NumberDataType :: DEST_TYPE => register_decimal_to_int:: <DEST_TYPE >( registry) ,
675
+ } )
671
676
}
672
677
NumberClass :: Decimal256 => {
673
678
// already registered in Decimal128 branch
0 commit comments