@@ -293,13 +293,17 @@ impl<'a> GetOptions<'a> {
293293
294294#[ cfg( test) ]
295295mod test {
296+ use std:: str:: FromStr ;
297+ use std:: sync:: Arc ;
298+
296299 use super :: { GetOptions , variant_get} ;
297300 use crate :: variant_array:: { ShreddedVariantFieldArray , StructArrayBuilder } ;
298301 use crate :: { VariantArray , VariantArrayBuilder , json_to_variant} ;
299302 use arrow:: array:: {
300303 Array , ArrayRef , AsArray , BinaryViewArray , BooleanArray , Date32Array , Decimal32Array ,
301304 Decimal64Array , Decimal128Array , Decimal256Array , Float32Array , Float64Array , Int8Array ,
302- Int16Array , Int32Array , Int64Array , StringArray , StructArray ,
305+ Int16Array , Int32Array , Int64Array , NullBuilder , StringArray , StructArray ,
306+ Time64MicrosecondArray ,
303307 } ;
304308 use arrow:: buffer:: NullBuffer ;
305309 use arrow:: compute:: CastOptions ;
@@ -312,7 +316,6 @@ mod test {
312316 EMPTY_VARIANT_METADATA_BYTES , Variant , VariantDecimal4 , VariantDecimal8 , VariantDecimal16 ,
313317 VariantDecimalType , VariantPath ,
314318 } ;
315- use std:: sync:: Arc ;
316319
317320 fn single_variant_get_test ( input_json : & str , path : VariantPath , expected_json : & str ) {
318321 // Create input array from JSON string
@@ -969,6 +972,158 @@ mod test {
969972 Date32Array :: from( vec![ Some ( -12345 ) , Some ( 17586 ) , Some ( 20000 ) ] )
970973 ) ;
971974
975+ perfectly_shredded_variant_array_fn ! ( perfectly_shredded_time_variant_array, || {
976+ Time64MicrosecondArray :: from( vec![ Some ( 12345000 ) , Some ( 87654000 ) , Some ( 135792000 ) ] )
977+ } ) ;
978+
979+ perfectly_shredded_to_arrow_primitive_test ! (
980+ get_variant_perfectly_shredded_time_as_time,
981+ DataType :: Time64 ( TimeUnit :: Microsecond ) ,
982+ perfectly_shredded_time_variant_array,
983+ Time64MicrosecondArray :: from( vec![ Some ( 12345000 ) , Some ( 87654000 ) , Some ( 135792000 ) ] )
984+ ) ;
985+
986+ perfectly_shredded_variant_array_fn ! ( perfectly_shredded_null_variant_array, || {
987+ let mut builder = NullBuilder :: new( ) ;
988+ builder. append_nulls( 3 ) ;
989+ builder. finish( )
990+ } ) ;
991+
992+ perfectly_shredded_to_arrow_primitive_test ! (
993+ get_variant_perfectly_shredded_null_as_null,
994+ DataType :: Null ,
995+ perfectly_shredded_null_variant_array,
996+ arrow:: array:: NullArray :: new( 3 )
997+ ) ;
998+
999+ perfectly_shredded_variant_array_fn ! ( perfectly_shredded_decimal4_variant_array, || {
1000+ Decimal32Array :: from( vec![ Some ( 12345 ) , Some ( 23400 ) , Some ( -12342 ) ] )
1001+ . with_precision_and_scale( 5 , 2 )
1002+ . unwrap( )
1003+ } ) ;
1004+
1005+ perfectly_shredded_to_arrow_primitive_test ! (
1006+ get_variant_perfectly_shredded_decimal4_as_decimal4,
1007+ DataType :: Decimal32 ( 5 , 2 ) ,
1008+ perfectly_shredded_decimal4_variant_array,
1009+ Decimal32Array :: from( vec![ Some ( 12345 ) , Some ( 23400 ) , Some ( -12342 ) ] )
1010+ . with_precision_and_scale( 5 , 2 )
1011+ . unwrap( )
1012+ ) ;
1013+
1014+ perfectly_shredded_variant_array_fn ! (
1015+ perfectly_shredded_decimal8_variant_array_cast2decimal32,
1016+ || {
1017+ Decimal64Array :: from( vec![ Some ( 123456 ) , Some ( 145678 ) , Some ( -123456 ) ] )
1018+ . with_precision_and_scale( 6 , 1 )
1019+ . unwrap( )
1020+ }
1021+ ) ;
1022+
1023+ // The input will be cast to Decimal32 when transformed to Variant
1024+ // This tests will covert the logic DataType::Decimal64(the original array)
1025+ // -> Variant::Decimal4(VariantArray) -> DataType::Decimal64(the result array)
1026+ perfectly_shredded_to_arrow_primitive_test ! (
1027+ get_variant_perfectly_shredded_decimal8_through_decimal32_as_decimal8,
1028+ DataType :: Decimal64 ( 6 , 1 ) ,
1029+ perfectly_shredded_decimal8_variant_array_cast2decimal32,
1030+ Decimal64Array :: from( vec![ Some ( 123456 ) , Some ( 145678 ) , Some ( -123456 ) ] )
1031+ . with_precision_and_scale( 6 , 1 )
1032+ . unwrap( )
1033+ ) ;
1034+
1035+ // This tests will covert the logic DataType::Decimal64(the original array)
1036+ // -> Variant::Decimal8(VariantArray) -> DataType::Decimal64(the result array)
1037+ perfectly_shredded_variant_array_fn ! ( perfectly_shredded_decimal8_variant_array, || {
1038+ Decimal64Array :: from( vec![ Some ( 1234567809 ) , Some ( 1456787000 ) , Some ( -1234561203 ) ] )
1039+ . with_precision_and_scale( 10 , 1 )
1040+ . unwrap( )
1041+ } ) ;
1042+
1043+ perfectly_shredded_to_arrow_primitive_test ! (
1044+ get_variant_perfectly_shredded_decimal8_as_decimal8,
1045+ DataType :: Decimal64 ( 10 , 1 ) ,
1046+ perfectly_shredded_decimal8_variant_array,
1047+ Decimal64Array :: from( vec![ Some ( 1234567809 ) , Some ( 1456787000 ) , Some ( -1234561203 ) ] )
1048+ . with_precision_and_scale( 10 , 1 )
1049+ . unwrap( )
1050+ ) ;
1051+
1052+ // This tests will covert the logic DataType::Decimal128(the original array)
1053+ // -> Variant::Decimal4(VariantArray) -> DataType::Decimal128(the result array)
1054+ perfectly_shredded_variant_array_fn ! (
1055+ perfectly_shredded_decimal16_within_decimal4_variant_array,
1056+ || {
1057+ Decimal128Array :: from( vec![
1058+ Some ( i128 :: from( 1234589 ) ) ,
1059+ Some ( i128 :: from( 2344444 ) ) ,
1060+ Some ( i128 :: from( -1234789 ) ) ,
1061+ ] )
1062+ . with_precision_and_scale( 7 , 3 )
1063+ . unwrap( )
1064+ }
1065+ ) ;
1066+
1067+ // This tests will covert the logic DataType::Decimal128(the original array)
1068+ // -> Variant::Decimal4(VariantArray) -> DataType::Decimal128(the result array)
1069+ perfectly_shredded_to_arrow_primitive_test ! (
1070+ get_variant_perfectly_shredded_decimal16_within_decimal4_as_decimal16,
1071+ DataType :: Decimal128 ( 7 , 3 ) ,
1072+ perfectly_shredded_decimal16_within_decimal4_variant_array,
1073+ Decimal128Array :: from( vec![
1074+ Some ( i128 :: from( 1234589 ) ) ,
1075+ Some ( i128 :: from( 2344444 ) ) ,
1076+ Some ( i128 :: from( -1234789 ) ) ,
1077+ ] )
1078+ . with_precision_and_scale( 7 , 3 )
1079+ . unwrap( )
1080+ ) ;
1081+
1082+ perfectly_shredded_variant_array_fn ! (
1083+ perfectly_shredded_decimal16_within_decimal8_variant_array,
1084+ || {
1085+ Decimal128Array :: from( vec![ Some ( 1234567809 ) , Some ( 1456787000 ) , Some ( -1234561203 ) ] )
1086+ . with_precision_and_scale( 10 , 1 )
1087+ . unwrap( )
1088+ }
1089+ ) ;
1090+
1091+ // This tests will covert the logic DataType::Decimal128(the original array)
1092+ // -> Variant::Decimal8(VariantArray) -> DataType::Decimal128(the result array)
1093+ perfectly_shredded_to_arrow_primitive_test ! (
1094+ get_variant_perfectly_shredded_decimal16_within8_as_decimal16,
1095+ DataType :: Decimal128 ( 10 , 1 ) ,
1096+ perfectly_shredded_decimal16_within_decimal8_variant_array,
1097+ Decimal128Array :: from( vec![ Some ( 1234567809 ) , Some ( 1456787000 ) , Some ( -1234561203 ) ] )
1098+ . with_precision_and_scale( 10 , 1 )
1099+ . unwrap( )
1100+ ) ;
1101+
1102+ perfectly_shredded_variant_array_fn ! ( perfectly_shredded_decimal16_variant_array, || {
1103+ Decimal128Array :: from( vec![
1104+ Some ( i128 :: from_str( "12345678901234567899" ) . unwrap( ) ) ,
1105+ Some ( i128 :: from_str( "23445677483748324300" ) . unwrap( ) ) ,
1106+ Some ( i128 :: from_str( "-12345678901234567899" ) . unwrap( ) ) ,
1107+ ] )
1108+ . with_precision_and_scale( 20 , 3 )
1109+ . unwrap( )
1110+ } ) ;
1111+
1112+ // This tests will covert the logic DataType::Decimal128(the original array)
1113+ // -> Variant::Decimal16(VariantArray) -> DataType::Decimal128(the result array)
1114+ perfectly_shredded_to_arrow_primitive_test ! (
1115+ get_variant_perfectly_shredded_decimal16_as_decimal16,
1116+ DataType :: Decimal128 ( 20 , 3 ) ,
1117+ perfectly_shredded_decimal16_variant_array,
1118+ Decimal128Array :: from( vec![
1119+ Some ( i128 :: from_str( "12345678901234567899" ) . unwrap( ) ) ,
1120+ Some ( i128 :: from_str( "23445677483748324300" ) . unwrap( ) ) ,
1121+ Some ( i128 :: from_str( "-12345678901234567899" ) . unwrap( ) )
1122+ ] )
1123+ . with_precision_and_scale( 20 , 3 )
1124+ . unwrap( )
1125+ ) ;
1126+
9721127 macro_rules! assert_variant_get_as_variant_array_with_default_option {
9731128 ( $variant_array: expr, $array_expected: expr) => { {
9741129 let options = GetOptions :: new( ) ;
0 commit comments