@@ -48,7 +48,7 @@ use crate::cast::dictionary::*;
48
48
use crate :: cast:: list:: * ;
49
49
use crate :: cast:: map:: * ;
50
50
use crate :: cast:: run_array:: {
51
- can_cast_run_end_encoded , cast_to_run_end_encoded, run_end_encoded_cast,
51
+ can_cast_to_run_end_encoded , cast_to_run_end_encoded, run_end_encoded_cast,
52
52
} ;
53
53
use crate :: cast:: string:: * ;
54
54
@@ -142,7 +142,7 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
142
142
}
143
143
( Dictionary ( _, value_type) , _) => can_cast_types ( value_type, to_type) ,
144
144
( RunEndEncoded ( _, value_type) , _) => can_cast_types ( value_type. data_type ( ) , to_type) ,
145
- ( _, RunEndEncoded ( _, _value_type) ) => can_cast_run_end_encoded ( from_type, to_type) ,
145
+ ( _, RunEndEncoded ( _, _value_type) ) => can_cast_to_run_end_encoded ( from_type, to_type) ,
146
146
( _, Dictionary ( _, value_type) ) => can_cast_types ( from_type, value_type) ,
147
147
( List ( list_from) | LargeList ( list_from) , List ( list_to) | LargeList ( list_to) ) => {
148
148
can_cast_types ( list_from. data_type ( ) , list_to. data_type ( ) )
@@ -10716,13 +10716,13 @@ mod tests {
10716
10716
) ) as ArrayRef ;
10717
10717
assert_eq ! ( * fixed_array, * r) ;
10718
10718
}
10719
+
10719
10720
#[ cfg( test) ]
10720
10721
mod run_end_encoded_tests {
10721
10722
use super :: * ;
10722
10723
use arrow_schema:: { DataType , Field } ;
10723
10724
use std:: sync:: Arc ;
10724
10725
10725
- /// Test casting FROM RunEndEncoded to primitive types
10726
10726
#[ test]
10727
10727
fn test_run_end_encoded_to_primitive ( ) {
10728
10728
// Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
@@ -10740,10 +10740,8 @@ mod tests {
10740
10740
) ;
10741
10741
}
10742
10742
10743
- /// Test casting FROM RunEndEncoded to string
10744
10743
#[ test]
10745
10744
fn test_run_end_encoded_to_string ( ) {
10746
- // Create a RunEndEncoded array with Int32 values: [10, 10, 20, 30, 30]
10747
10745
let run_ends = Int32Array :: from ( vec ! [ 2 , 3 , 5 ] ) ;
10748
10746
let values = Int32Array :: from ( vec ! [ 10 , 20 , 30 ] ) ;
10749
10747
let run_array = RunArray :: < Int32Type > :: try_new ( & run_ends, & values) . unwrap ( ) ;
@@ -10760,7 +10758,6 @@ mod tests {
10760
10758
assert_eq ! ( result_array. value( 2 ) , "20" ) ;
10761
10759
}
10762
10760
10763
- /// Test casting TO RunEndEncoded from primitive types
10764
10761
#[ test]
10765
10762
fn test_primitive_to_run_end_encoded ( ) {
10766
10763
// Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
@@ -10788,7 +10785,94 @@ mod tests {
10788
10785
assert_eq ! ( values_array. values( ) , & [ 1 , 2 , 3 ] ) ;
10789
10786
}
10790
10787
10791
- /// Test casting TO RunEndEncoded from string
10788
+ #[ test]
10789
+ fn test_primitive_to_run_end_encoded_with_nulls ( ) {
10790
+ let source_array = Int32Array :: from ( vec ! [
10791
+ Some ( 1 ) ,
10792
+ Some ( 1 ) ,
10793
+ None ,
10794
+ None ,
10795
+ Some ( 2 ) ,
10796
+ Some ( 2 ) ,
10797
+ Some ( 3 ) ,
10798
+ Some ( 3 ) ,
10799
+ None ,
10800
+ None ,
10801
+ Some ( 4 ) ,
10802
+ Some ( 4 ) ,
10803
+ Some ( 5 ) ,
10804
+ Some ( 5 ) ,
10805
+ None ,
10806
+ None ,
10807
+ ] ) ;
10808
+ let array_ref = Arc :: new ( source_array) as ArrayRef ;
10809
+ let target_type = DataType :: RunEndEncoded (
10810
+ Arc :: new ( Field :: new ( "run_ends" , DataType :: Int32 , false ) ) ,
10811
+ Arc :: new ( Field :: new ( "values" , DataType :: Int32 , true ) ) ,
10812
+ ) ;
10813
+ let cast_result = cast ( & array_ref, & target_type) . unwrap ( ) ;
10814
+ let result_run_array = cast_result
10815
+ . as_any ( )
10816
+ . downcast_ref :: < RunArray < Int32Type > > ( )
10817
+ . unwrap ( ) ;
10818
+ assert_eq ! (
10819
+ result_run_array. run_ends( ) . values( ) ,
10820
+ & [ 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ]
10821
+ ) ;
10822
+ assert_eq ! (
10823
+ result_run_array
10824
+ . values( )
10825
+ . as_primitive:: <Int32Type >( )
10826
+ . values( ) ,
10827
+ & [ 1 , 0 , 2 , 3 , 0 , 4 , 5 , 0 ]
10828
+ ) ;
10829
+ assert_eq ! ( result_run_array. values( ) . null_count( ) , 3 ) ;
10830
+ }
10831
+
10832
+ #[ test]
10833
+ fn test_primitive_to_run_end_encoded_with_nulls_consecutive ( ) {
10834
+ let source_array = Int64Array :: from ( vec ! [
10835
+ Some ( 1 ) ,
10836
+ Some ( 1 ) ,
10837
+ None ,
10838
+ None ,
10839
+ None ,
10840
+ None ,
10841
+ None ,
10842
+ None ,
10843
+ None ,
10844
+ None ,
10845
+ Some ( 4 ) ,
10846
+ Some ( 20 ) ,
10847
+ Some ( 500 ) ,
10848
+ Some ( 500 ) ,
10849
+ None ,
10850
+ None ,
10851
+ ] ) ;
10852
+ let array_ref = Arc :: new ( source_array) as ArrayRef ;
10853
+ let target_type = DataType :: RunEndEncoded (
10854
+ Arc :: new ( Field :: new ( "run_ends" , DataType :: Int16 , false ) ) ,
10855
+ Arc :: new ( Field :: new ( "values" , DataType :: Int64 , true ) ) ,
10856
+ ) ;
10857
+ let cast_result = cast ( & array_ref, & target_type) . unwrap ( ) ;
10858
+ let result_run_array = cast_result
10859
+ . as_any ( )
10860
+ . downcast_ref :: < RunArray < Int16Type > > ( )
10861
+ . unwrap ( ) ;
10862
+ assert_eq ! (
10863
+ result_run_array. run_ends( ) . values( ) ,
10864
+ & [ 2 , 10 , 11 , 12 , 14 , 16 ]
10865
+ ) ;
10866
+ assert_eq ! (
10867
+ result_run_array
10868
+ . values( )
10869
+ . as_primitive:: <Int64Type >( )
10870
+ . values( ) ,
10871
+ & [ 1 , 0 , 4 , 20 , 500 , 0 ]
10872
+ ) ;
10873
+ assert_eq ! ( result_run_array. values( ) . null_count( ) , 2 ) ;
10874
+ }
10875
+
10792
10876
#[ test]
10793
10877
fn test_string_to_run_end_encoded ( ) {
10794
10878
// Create a String array with repeated values: ["a", "a", "b", "c", "c"]
@@ -10818,7 +10902,6 @@ mod tests {
10818
10902
assert_eq ! ( values_array. value( 2 ) , "c" ) ;
10819
10903
}
10820
10904
10821
- /// Test casting with type conversion (Int32 -> RunEndEncoded<Int32, String>)
10822
10905
#[ test]
10823
10906
fn test_cast_with_type_conversion ( ) {
10824
10907
// Create an Int32 array: [1, 1, 2, 2, 3]
@@ -10851,7 +10934,6 @@ mod tests {
10851
10934
assert_eq ! ( values_array. value( 2 ) , "3" ) ;
10852
10935
}
10853
10936
10854
- /// Test casting empty array to RunEndEncoded
10855
10937
#[ test]
10856
10938
fn test_empty_array_to_run_end_encoded ( ) {
10857
10939
// Create an empty Int32 array
@@ -10876,7 +10958,6 @@ mod tests {
10876
10958
assert_eq ! ( result_run_array. values( ) . len( ) , 0 ) ;
10877
10959
}
10878
10960
10879
- /// Test casting RunEndEncoded with nulls
10880
10961
#[ test]
10881
10962
fn test_run_end_encoded_with_nulls ( ) {
10882
10963
// Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
@@ -10895,7 +10976,6 @@ mod tests {
10895
10976
assert_eq ! ( result_run_array. value( 4 ) , "2" ) ;
10896
10977
}
10897
10978
10898
- /// Test different index types (Int16, Int64)
10899
10979
#[ test]
10900
10980
fn test_different_index_types ( ) {
10901
10981
// Test with Int16 index type
@@ -10917,6 +10997,7 @@ mod tests {
10917
10997
let cast_result = cast ( & array_ref, & target_type) . unwrap ( ) ;
10918
10998
assert_eq ! ( cast_result. data_type( ) , & target_type) ;
10919
10999
}
11000
+
10920
11001
#[ test]
10921
11002
fn test_unsupported_cast_to_run_end_encoded ( ) {
10922
11003
// Create a Struct array - complex nested type that might not be supported
@@ -10935,8 +11016,10 @@ mod tests {
10935
11016
// Expect this to fail
10936
11017
assert ! ( cast_result. is_err( ) ) ;
10937
11018
}
11019
+
10938
11020
#[ test]
10939
11021
fn test_cast_run_end_encoded_int64_to_int16_should_fail ( ) {
11022
+ /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
10940
11023
use arrow_array:: { Int64Array , RunArray , StringArray } ;
10941
11024
use arrow_schema:: { DataType , Field } ;
10942
11025
use std:: sync:: Arc ;
@@ -10973,8 +11056,10 @@ mod tests {
10973
11056
}
10974
11057
}
10975
11058
}
11059
+
10976
11060
#[ test]
10977
11061
fn test_cast_run_end_encoded_int16_to_int64_should_succeed ( ) {
11062
+ /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
10978
11063
use arrow_array:: { Int16Array , RunArray , StringArray } ;
10979
11064
use arrow_schema:: { DataType , Field } ;
10980
11065
use std:: sync:: Arc ;
@@ -11023,6 +11108,7 @@ mod tests {
11023
11108
11024
11109
#[ test]
11025
11110
fn test_cast_run_end_encoded_int32_to_int16_should_fail ( ) {
11111
+ /// Test casting RunEndEncoded<Int32, String> to RunEndEncoded<Int16, String> should fail
11026
11112
use arrow_array:: { Int32Array , RunArray , StringArray } ;
11027
11113
use arrow_schema:: { DataType , Field } ;
11028
11114
use std:: sync:: Arc ;
@@ -11031,9 +11117,6 @@ mod tests {
11031
11117
let run_ends = Int32Array :: from ( vec ! [ 1000 , 50000 , 80000 ] ) ; // values too large for Int16
11032
11118
let values = StringArray :: from ( vec ! [ "x" , "y" , "z" ] ) ;
11033
11119
11034
- println ! ( "Original run_ends null count: {}" , run_ends. null_count( ) ) ;
11035
- println ! ( "Original run_ends values: {:?}" , run_ends. values( ) ) ;
11036
-
11037
11120
let ree_array = RunArray :: < Int32Type > :: try_new ( & run_ends, & values) . unwrap ( ) ;
11038
11121
let array_ref = Arc :: new ( ree_array) as ArrayRef ;
11039
11122
0 commit comments