@@ -544,9 +544,22 @@ where
544
544
let result = integer_array
545
545
. iter ( )
546
546
. map ( |integer| {
547
- integer. map ( |integer| format ! ( "{:x}" , integer. to_usize( ) . unwrap( ) ) )
547
+ if let Some ( value) = integer {
548
+ if let Some ( value_usize) = value. to_usize ( ) {
549
+ Ok ( Some ( format ! ( "{:x}" , value_usize) ) )
550
+ } else if let Some ( value_isize) = value. to_isize ( ) {
551
+ Ok ( Some ( format ! ( "{:x}" , value_isize) ) )
552
+ } else {
553
+ Err ( DataFusionError :: Internal ( format ! (
554
+ "Unsupported data type {:?} for function to_hex" ,
555
+ integer,
556
+ ) ) )
557
+ }
558
+ } else {
559
+ Ok ( None )
560
+ }
548
561
} )
549
- . collect :: < GenericStringArray < i32 > > ( ) ;
562
+ . collect :: < Result < GenericStringArray < i32 > > > ( ) ? ;
550
563
551
564
Ok ( Arc :: new ( result) as ArrayRef )
552
565
}
@@ -573,3 +586,51 @@ pub fn uuid(args: &[ColumnarValue]) -> Result<ColumnarValue> {
573
586
let array = GenericStringArray :: < i32 > :: from_iter_values ( values) ;
574
587
Ok ( ColumnarValue :: Array ( Arc :: new ( array) ) )
575
588
}
589
+
590
+ #[ cfg( test) ]
591
+ mod tests {
592
+
593
+ use crate :: string_expressions;
594
+ use arrow:: { array:: Int32Array , datatypes:: Int32Type } ;
595
+
596
+ use super :: * ;
597
+
598
+ #[ test]
599
+ // Test to_hex function for zero
600
+ fn to_hex_zero ( ) -> Result < ( ) > {
601
+ let array = vec ! [ 0 ] . into_iter ( ) . collect :: < Int32Array > ( ) ;
602
+ let array_ref = Arc :: new ( array) ;
603
+ let hex_value_arc = string_expressions:: to_hex :: < Int32Type > ( & [ array_ref] ) ?;
604
+ let hex_value = as_string_array ( & hex_value_arc) ?;
605
+ let expected = StringArray :: from ( vec ! [ Some ( "0" ) ] ) ;
606
+ assert_eq ! ( & expected, hex_value) ;
607
+
608
+ Ok ( ( ) )
609
+ }
610
+
611
+ #[ test]
612
+ // Test to_hex function for positive number
613
+ fn to_hex_positive_number ( ) -> Result < ( ) > {
614
+ let array = vec ! [ 100 ] . into_iter ( ) . collect :: < Int32Array > ( ) ;
615
+ let array_ref = Arc :: new ( array) ;
616
+ let hex_value_arc = string_expressions:: to_hex :: < Int32Type > ( & [ array_ref] ) ?;
617
+ let hex_value = as_string_array ( & hex_value_arc) ?;
618
+ let expected = StringArray :: from ( vec ! [ Some ( "64" ) ] ) ;
619
+ assert_eq ! ( & expected, hex_value) ;
620
+
621
+ Ok ( ( ) )
622
+ }
623
+
624
+ #[ test]
625
+ // Test to_hex function for negative number
626
+ fn to_hex_negative_number ( ) -> Result < ( ) > {
627
+ let array = vec ! [ -1 ] . into_iter ( ) . collect :: < Int32Array > ( ) ;
628
+ let array_ref = Arc :: new ( array) ;
629
+ let hex_value_arc = string_expressions:: to_hex :: < Int32Type > ( & [ array_ref] ) ?;
630
+ let hex_value = as_string_array ( & hex_value_arc) ?;
631
+ let expected = StringArray :: from ( vec ! [ Some ( "ffffffffffffffff" ) ] ) ;
632
+ assert_eq ! ( & expected, hex_value) ;
633
+
634
+ Ok ( ( ) )
635
+ }
636
+ }
0 commit comments