@@ -8209,6 +8209,10 @@ mod tests {
8209
8209
assert ! ( decimal_arr. is_null( 25 ) ) ;
8210
8210
assert ! ( decimal_arr. is_null( 26 ) ) ;
8211
8211
assert ! ( decimal_arr. is_null( 27 ) ) ;
8212
+ assert_eq ! ( "0.00" , decimal_arr. value_as_string( 28 ) ) ;
8213
+ assert_eq ! ( "0.00" , decimal_arr. value_as_string( 29 ) ) ;
8214
+ assert_eq ! ( "12345.00" , decimal_arr. value_as_string( 30 ) ) ;
8215
+ assert_eq ! ( decimal_arr. len( ) , 31 ) ;
8212
8216
8213
8217
// Decimal256
8214
8218
let output_type = DataType :: Decimal256 ( 76 , 3 ) ;
@@ -8245,6 +8249,10 @@ mod tests {
8245
8249
assert ! ( decimal_arr. is_null( 25 ) ) ;
8246
8250
assert ! ( decimal_arr. is_null( 26 ) ) ;
8247
8251
assert ! ( decimal_arr. is_null( 27 ) ) ;
8252
+ assert_eq ! ( "0.000" , decimal_arr. value_as_string( 28 ) ) ;
8253
+ assert_eq ! ( "0.000" , decimal_arr. value_as_string( 29 ) ) ;
8254
+ assert_eq ! ( "12345.000" , decimal_arr. value_as_string( 30 ) ) ;
8255
+ assert_eq ! ( decimal_arr. len( ) , 31 ) ;
8248
8256
}
8249
8257
8250
8258
#[ test]
@@ -8278,10 +8286,30 @@ mod tests {
8278
8286
Some ( "1.-23499999" ) ,
8279
8287
Some ( "-1.-23499999" ) ,
8280
8288
Some ( "--1.23499999" ) ,
8289
+ Some ( "0" ) ,
8290
+ Some ( "000.000" ) ,
8291
+ Some ( "0000000000000000012345.000" ) ,
8281
8292
] ) ;
8282
8293
let array = Arc :: new ( str_array) as ArrayRef ;
8283
8294
8284
8295
test_cast_string_to_decimal ( array) ;
8296
+
8297
+ let test_cases = [
8298
+ ( None , None ) ,
8299
+ // (Some(""), None),
8300
+ // (Some(" "), None),
8301
+ ( Some ( "0" ) , Some ( "0" ) ) ,
8302
+ ( Some ( "000.000" ) , Some ( "0" ) ) ,
8303
+ ( Some ( "12345" ) , Some ( "12345" ) ) ,
8304
+ ( Some ( "000000000000000000000000000012345" ) , Some ( "12345" ) ) ,
8305
+ ( Some ( "-123" ) , Some ( "-123" ) ) ,
8306
+ ( Some ( "+123" ) , Some ( "123" ) ) ,
8307
+ ] ;
8308
+ let inputs = test_cases. iter ( ) . map ( |entry| entry. 0 ) . collect :: < Vec < _ > > ( ) ;
8309
+ let expected = test_cases. iter ( ) . map ( |entry| entry. 1 ) . collect :: < Vec < _ > > ( ) ;
8310
+
8311
+ let array = Arc :: new ( StringArray :: from ( inputs) ) as ArrayRef ;
8312
+ test_cast_string_to_decimal_scale_zero ( array, & expected) ;
8285
8313
}
8286
8314
8287
8315
#[ test]
@@ -8315,10 +8343,67 @@ mod tests {
8315
8343
Some ( "1.-23499999" ) ,
8316
8344
Some ( "-1.-23499999" ) ,
8317
8345
Some ( "--1.23499999" ) ,
8346
+ Some ( "0" ) ,
8347
+ Some ( "000.000" ) ,
8348
+ Some ( "0000000000000000012345.000" ) ,
8318
8349
] ) ;
8319
8350
let array = Arc :: new ( str_array) as ArrayRef ;
8320
8351
8321
8352
test_cast_string_to_decimal ( array) ;
8353
+
8354
+ let test_cases = [
8355
+ ( None , None ) ,
8356
+ ( Some ( "" ) , None ) ,
8357
+ ( Some ( " " ) , None ) ,
8358
+ ( Some ( "0" ) , Some ( "0" ) ) ,
8359
+ ( Some ( "000.000" ) , Some ( "0" ) ) ,
8360
+ ( Some ( "12345" ) , Some ( "12345" ) ) ,
8361
+ ( Some ( "000000000000000000000000000012345" ) , Some ( "12345" ) ) ,
8362
+ ( Some ( "-123" ) , Some ( "-123" ) ) ,
8363
+ ( Some ( "+123" ) , Some ( "123" ) ) ,
8364
+ ] ;
8365
+ let inputs = test_cases. iter ( ) . map ( |entry| entry. 0 ) . collect :: < Vec < _ > > ( ) ;
8366
+ let expected = test_cases. iter ( ) . map ( |entry| entry. 1 ) . collect :: < Vec < _ > > ( ) ;
8367
+
8368
+ let array = Arc :: new ( LargeStringArray :: from ( inputs) ) as ArrayRef ;
8369
+ test_cast_string_to_decimal_scale_zero ( array, & expected) ;
8370
+ }
8371
+
8372
+ fn test_cast_string_to_decimal_scale_zero (
8373
+ array : ArrayRef ,
8374
+ expected_as_string : & [ Option < & str > ] ,
8375
+ ) {
8376
+ // Decimal128
8377
+ let output_type = DataType :: Decimal128 ( 38 , 0 ) ;
8378
+ assert ! ( can_cast_types( array. data_type( ) , & output_type) ) ;
8379
+ let casted_array = cast ( & array, & output_type) . unwrap ( ) ;
8380
+ let decimal_arr = casted_array. as_primitive :: < Decimal128Type > ( ) ;
8381
+ assert_decimal_array_contents ( decimal_arr, expected_as_string) ;
8382
+
8383
+ // Decimal256
8384
+ let output_type = DataType :: Decimal256 ( 76 , 0 ) ;
8385
+ assert ! ( can_cast_types( array. data_type( ) , & output_type) ) ;
8386
+ let casted_array = cast ( & array, & output_type) . unwrap ( ) ;
8387
+ let decimal_arr = casted_array. as_primitive :: < Decimal256Type > ( ) ;
8388
+ assert_decimal_array_contents ( decimal_arr, expected_as_string) ;
8389
+ }
8390
+
8391
+ fn assert_decimal_array_contents < T > (
8392
+ array : & PrimitiveArray < T > ,
8393
+ expected_as_string : & [ Option < & str > ] ,
8394
+ ) where
8395
+ T : DecimalType + ArrowPrimitiveType ,
8396
+ {
8397
+ assert_eq ! ( array. len( ) , expected_as_string. len( ) ) ;
8398
+ for ( i, expected) in expected_as_string. iter ( ) . enumerate ( ) {
8399
+ let actual = if array. is_null ( i) {
8400
+ None
8401
+ } else {
8402
+ Some ( array. value_as_string ( i) )
8403
+ } ;
8404
+ let actual = actual. as_ref ( ) . map ( |s| s. as_ref ( ) ) ;
8405
+ assert_eq ! ( * expected, actual, "Expected at position {}" , i) ;
8406
+ }
8322
8407
}
8323
8408
8324
8409
#[ test]
0 commit comments