@@ -8481,6 +8481,10 @@ mod tests {
8481
8481
assert ! ( decimal_arr. is_null( 25 ) ) ;
8482
8482
assert ! ( decimal_arr. is_null( 26 ) ) ;
8483
8483
assert ! ( decimal_arr. is_null( 27 ) ) ;
8484
+ assert_eq ! ( "0.00" , decimal_arr. value_as_string( 28 ) ) ;
8485
+ assert_eq ! ( "0.00" , decimal_arr. value_as_string( 29 ) ) ;
8486
+ assert_eq ! ( "12345.00" , decimal_arr. value_as_string( 30 ) ) ;
8487
+ assert_eq ! ( decimal_arr. len( ) , 31 ) ;
8484
8488
8485
8489
// Decimal256
8486
8490
let output_type = DataType :: Decimal256 ( 76 , 3 ) ;
@@ -8517,6 +8521,10 @@ mod tests {
8517
8521
assert ! ( decimal_arr. is_null( 25 ) ) ;
8518
8522
assert ! ( decimal_arr. is_null( 26 ) ) ;
8519
8523
assert ! ( decimal_arr. is_null( 27 ) ) ;
8524
+ assert_eq ! ( "0.000" , decimal_arr. value_as_string( 28 ) ) ;
8525
+ assert_eq ! ( "0.000" , decimal_arr. value_as_string( 29 ) ) ;
8526
+ assert_eq ! ( "12345.000" , decimal_arr. value_as_string( 30 ) ) ;
8527
+ assert_eq ! ( decimal_arr. len( ) , 31 ) ;
8520
8528
}
8521
8529
8522
8530
#[ test]
@@ -8550,10 +8558,30 @@ mod tests {
8550
8558
Some ( "1.-23499999" ) ,
8551
8559
Some ( "-1.-23499999" ) ,
8552
8560
Some ( "--1.23499999" ) ,
8561
+ Some ( "0" ) ,
8562
+ Some ( "000.000" ) ,
8563
+ Some ( "0000000000000000012345.000" ) ,
8553
8564
] ) ;
8554
8565
let array = Arc :: new ( str_array) as ArrayRef ;
8555
8566
8556
8567
test_cast_string_to_decimal ( array) ;
8568
+
8569
+ let test_cases = [
8570
+ ( None , None ) ,
8571
+ // (Some(""), None),
8572
+ // (Some(" "), None),
8573
+ ( Some ( "0" ) , Some ( "0" ) ) ,
8574
+ ( Some ( "000.000" ) , Some ( "0" ) ) ,
8575
+ ( Some ( "12345" ) , Some ( "12345" ) ) ,
8576
+ ( Some ( "000000000000000000000000000012345" ) , Some ( "12345" ) ) ,
8577
+ ( Some ( "-123" ) , Some ( "-123" ) ) ,
8578
+ ( Some ( "+123" ) , Some ( "123" ) ) ,
8579
+ ] ;
8580
+ let inputs = test_cases. iter ( ) . map ( |entry| entry. 0 ) . collect :: < Vec < _ > > ( ) ;
8581
+ let expected = test_cases. iter ( ) . map ( |entry| entry. 1 ) . collect :: < Vec < _ > > ( ) ;
8582
+
8583
+ let array = Arc :: new ( StringArray :: from ( inputs) ) as ArrayRef ;
8584
+ test_cast_string_to_decimal_scale_zero ( array, & expected) ;
8557
8585
}
8558
8586
8559
8587
#[ test]
@@ -8587,10 +8615,67 @@ mod tests {
8587
8615
Some ( "1.-23499999" ) ,
8588
8616
Some ( "-1.-23499999" ) ,
8589
8617
Some ( "--1.23499999" ) ,
8618
+ Some ( "0" ) ,
8619
+ Some ( "000.000" ) ,
8620
+ Some ( "0000000000000000012345.000" ) ,
8590
8621
] ) ;
8591
8622
let array = Arc :: new ( str_array) as ArrayRef ;
8592
8623
8593
8624
test_cast_string_to_decimal ( array) ;
8625
+
8626
+ let test_cases = [
8627
+ ( None , None ) ,
8628
+ ( Some ( "" ) , None ) ,
8629
+ ( Some ( " " ) , None ) ,
8630
+ ( Some ( "0" ) , Some ( "0" ) ) ,
8631
+ ( Some ( "000.000" ) , Some ( "0" ) ) ,
8632
+ ( Some ( "12345" ) , Some ( "12345" ) ) ,
8633
+ ( Some ( "000000000000000000000000000012345" ) , Some ( "12345" ) ) ,
8634
+ ( Some ( "-123" ) , Some ( "-123" ) ) ,
8635
+ ( Some ( "+123" ) , Some ( "123" ) ) ,
8636
+ ] ;
8637
+ let inputs = test_cases. iter ( ) . map ( |entry| entry. 0 ) . collect :: < Vec < _ > > ( ) ;
8638
+ let expected = test_cases. iter ( ) . map ( |entry| entry. 1 ) . collect :: < Vec < _ > > ( ) ;
8639
+
8640
+ let array = Arc :: new ( LargeStringArray :: from ( inputs) ) as ArrayRef ;
8641
+ test_cast_string_to_decimal_scale_zero ( array, & expected) ;
8642
+ }
8643
+
8644
+ fn test_cast_string_to_decimal_scale_zero (
8645
+ array : ArrayRef ,
8646
+ expected_as_string : & [ Option < & str > ] ,
8647
+ ) {
8648
+ // Decimal128
8649
+ let output_type = DataType :: Decimal128 ( 38 , 0 ) ;
8650
+ assert ! ( can_cast_types( array. data_type( ) , & output_type) ) ;
8651
+ let casted_array = cast ( & array, & output_type) . unwrap ( ) ;
8652
+ let decimal_arr = casted_array. as_primitive :: < Decimal128Type > ( ) ;
8653
+ assert_decimal_array_contents ( decimal_arr, expected_as_string) ;
8654
+
8655
+ // Decimal256
8656
+ let output_type = DataType :: Decimal256 ( 76 , 0 ) ;
8657
+ assert ! ( can_cast_types( array. data_type( ) , & output_type) ) ;
8658
+ let casted_array = cast ( & array, & output_type) . unwrap ( ) ;
8659
+ let decimal_arr = casted_array. as_primitive :: < Decimal256Type > ( ) ;
8660
+ assert_decimal_array_contents ( decimal_arr, expected_as_string) ;
8661
+ }
8662
+
8663
+ fn assert_decimal_array_contents < T > (
8664
+ array : & PrimitiveArray < T > ,
8665
+ expected_as_string : & [ Option < & str > ] ,
8666
+ ) where
8667
+ T : DecimalType + ArrowPrimitiveType ,
8668
+ {
8669
+ assert_eq ! ( array. len( ) , expected_as_string. len( ) ) ;
8670
+ for ( i, expected) in expected_as_string. iter ( ) . enumerate ( ) {
8671
+ let actual = if array. is_null ( i) {
8672
+ None
8673
+ } else {
8674
+ Some ( array. value_as_string ( i) )
8675
+ } ;
8676
+ let actual = actual. as_ref ( ) . map ( |s| s. as_ref ( ) ) ;
8677
+ assert_eq ! ( * expected, actual, "Expected at position {}" , i) ;
8678
+ }
8594
8679
}
8595
8680
8596
8681
#[ test]
0 commit comments