@@ -1910,6 +1910,7 @@ mod tests {
1910
1910
1911
1911
#[ test]
1912
1912
fn test_cast_numeric_to_decimal ( ) {
1913
+ // test cast type
1913
1914
let data_types = vec ! [
1914
1915
DataType :: Int8 ,
1915
1916
DataType :: Int16 ,
@@ -1924,21 +1925,58 @@ mod tests {
1924
1925
}
1925
1926
assert ! ( !can_cast_types( & DataType :: UInt64 , & decimal_type) ) ;
1926
1927
1927
- // test i8 to decimal type
1928
- let array = Int8Array :: from ( vec ! [ 1 , 2 , 3 , 4 , 5 ] ) ;
1929
- let array = Arc :: new ( array) as ArrayRef ;
1930
- let casted_array = cast ( & array, & decimal_type) . unwrap ( ) ;
1931
- let decimal_array = casted_array
1932
- . as_any ( )
1933
- . downcast_ref :: < DecimalArray > ( )
1934
- . unwrap ( ) ;
1935
- assert_eq ! ( & decimal_type, decimal_array. data_type( ) ) ;
1936
- for i in 0 ..array. len ( ) {
1937
- assert_eq ! (
1938
- 10_i128 . pow( 6 ) * ( i as i128 + 1 ) ,
1939
- decimal_array. value( i as usize )
1940
- ) ;
1928
+ // test cast data
1929
+ let input_datas = vec ! [
1930
+ Arc :: new( Int8Array :: from( vec![
1931
+ Some ( 1 ) ,
1932
+ Some ( 2 ) ,
1933
+ Some ( 3 ) ,
1934
+ None ,
1935
+ Some ( 5 ) ,
1936
+ ] ) ) as ArrayRef , // i8
1937
+ Arc :: new( Int16Array :: from( vec![
1938
+ Some ( 1 ) ,
1939
+ Some ( 2 ) ,
1940
+ Some ( 3 ) ,
1941
+ None ,
1942
+ Some ( 5 ) ,
1943
+ ] ) ) as ArrayRef , // i16
1944
+ Arc :: new( Int32Array :: from( vec![
1945
+ Some ( 1 ) ,
1946
+ Some ( 2 ) ,
1947
+ Some ( 3 ) ,
1948
+ None ,
1949
+ Some ( 5 ) ,
1950
+ ] ) ) as ArrayRef , // i32
1951
+ Arc :: new( Int64Array :: from( vec![
1952
+ Some ( 1 ) ,
1953
+ Some ( 2 ) ,
1954
+ Some ( 3 ) ,
1955
+ None ,
1956
+ Some ( 5 ) ,
1957
+ ] ) ) as ArrayRef , // i64
1958
+ ] ;
1959
+
1960
+ // i8, i16, i32, i64
1961
+ for array in input_datas {
1962
+ let casted_array = cast ( & array, & decimal_type) . unwrap ( ) ;
1963
+ let decimal_array = casted_array
1964
+ . as_any ( )
1965
+ . downcast_ref :: < DecimalArray > ( )
1966
+ . unwrap ( ) ;
1967
+ assert_eq ! ( & decimal_type, decimal_array. data_type( ) ) ;
1968
+ for i in 0 ..array. len ( ) {
1969
+ if i == 3 {
1970
+ assert ! ( decimal_array. is_null( i as usize ) ) ;
1971
+ } else {
1972
+ assert_eq ! (
1973
+ 10_i128 . pow( 6 ) * ( i as i128 + 1 ) ,
1974
+ decimal_array. value( i as usize )
1975
+ ) ;
1976
+ }
1977
+ }
1941
1978
}
1979
+
1942
1980
// test i8 to decimal type with overflow the result type
1943
1981
// the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
1944
1982
let array = Int8Array :: from ( vec ! [ 1 , 2 , 3 , 4 , 100 ] ) ;
@@ -1947,54 +1985,6 @@ mod tests {
1947
1985
assert ! ( casted_array. is_err( ) ) ;
1948
1986
assert_eq ! ( "Invalid argument error: The value of 1000 i128 is not compatible with Decimal(3,1)" , casted_array. unwrap_err( ) . to_string( ) ) ;
1949
1987
1950
- // test i16 to decimal type
1951
- let array = Int16Array :: from ( vec ! [ 1 , 2 , 3 , 4 , 5 ] ) ;
1952
- let array = Arc :: new ( array) as ArrayRef ;
1953
- let casted_array = cast ( & array, & decimal_type) . unwrap ( ) ;
1954
- let decimal_array = casted_array
1955
- . as_any ( )
1956
- . downcast_ref :: < DecimalArray > ( )
1957
- . unwrap ( ) ;
1958
- assert_eq ! ( & decimal_type, decimal_array. data_type( ) ) ;
1959
- for i in 0 ..array. len ( ) {
1960
- assert_eq ! (
1961
- 10_i128 . pow( 6 ) * ( i as i128 + 1 ) ,
1962
- decimal_array. value( i as usize )
1963
- ) ;
1964
- }
1965
-
1966
- // test i32 to decimal type
1967
- let array = Int32Array :: from ( vec ! [ 1 , 2 , 3 , 4 , 5 ] ) ;
1968
- let array = Arc :: new ( array) as ArrayRef ;
1969
- let casted_array = cast ( & array, & decimal_type) . unwrap ( ) ;
1970
- let decimal_array = casted_array
1971
- . as_any ( )
1972
- . downcast_ref :: < DecimalArray > ( )
1973
- . unwrap ( ) ;
1974
- assert_eq ! ( & decimal_type, decimal_array. data_type( ) ) ;
1975
- for i in 0 ..array. len ( ) {
1976
- assert_eq ! (
1977
- 10_i128 . pow( 6 ) * ( i as i128 + 1 ) ,
1978
- decimal_array. value( i as usize )
1979
- ) ;
1980
- }
1981
-
1982
- // test i64 to decimal type
1983
- let array = Int64Array :: from ( vec ! [ 1 , 2 , 3 , 4 , 5 ] ) ;
1984
- let array = Arc :: new ( array) as ArrayRef ;
1985
- let casted_array = cast ( & array, & decimal_type) . unwrap ( ) ;
1986
- let decimal_array = casted_array
1987
- . as_any ( )
1988
- . downcast_ref :: < DecimalArray > ( )
1989
- . unwrap ( ) ;
1990
- assert_eq ! ( & decimal_type, decimal_array. data_type( ) ) ;
1991
- for i in 0 ..array. len ( ) {
1992
- assert_eq ! (
1993
- 10_i128 . pow( 6 ) * ( i as i128 + 1 ) ,
1994
- decimal_array. value( i as usize )
1995
- ) ;
1996
- }
1997
-
1998
1988
// test f32 to decimal type
1999
1989
let f_data: Vec < f32 > = vec ! [ 1.1 , 2.2 , 4.4 , 1.123_456_8 ] ;
2000
1990
let array = Float32Array :: from ( f_data. clone ( ) ) ;
0 commit comments