@@ -9,3 +9,75 @@ pub use self::arrays::*;
9
9
pub use self :: batches:: * ;
10
10
pub use self :: compare:: * ;
11
11
pub use self :: format_data_type:: * ;
12
+
13
+ // ----------------------------------------------------------------
14
+
15
+ use std:: sync:: Arc ;
16
+
17
+ use arrow:: {
18
+ array:: { Array as _, AsArray as _, ListArray } ,
19
+ datatypes:: { DataType , Field } ,
20
+ } ;
21
+
22
+ /// Convert any `BinaryArray` to `LargeBinaryArray`, because we treat them logivally the same
23
+ pub fn widen_binary_arrays ( list_array : & ListArray ) -> ListArray {
24
+ let list_data_type = list_array. data_type ( ) ;
25
+ if let DataType :: List ( field) = list_data_type
26
+ && field. data_type ( ) == & DataType :: Binary
27
+ {
28
+ re_tracing:: profile_function!( ) ;
29
+ let large_binary_field = Field :: new ( "item" , DataType :: LargeBinary , true ) ;
30
+ let target_type = DataType :: List ( Arc :: new ( large_binary_field) ) ;
31
+
32
+ #[ expect( clippy:: unwrap_used) ]
33
+ arrow:: compute:: kernels:: cast:: cast ( list_array, & target_type)
34
+ . unwrap ( )
35
+ . as_list ( )
36
+ . clone ( )
37
+ } else {
38
+ list_array. clone ( )
39
+ }
40
+ }
41
+
42
+ #[ cfg( test) ]
43
+ mod tests {
44
+ use super :: * ;
45
+ use arrow:: array:: { BinaryBuilder , ListBuilder } ;
46
+
47
+ #[ test]
48
+ fn test_widen_list_binary ( ) {
49
+ // Create test data
50
+ let mut list_builder = ListBuilder :: new ( BinaryBuilder :: new ( ) ) ;
51
+
52
+ // First list: [b"hello", b"world"]
53
+ list_builder. values ( ) . append_value ( b"hello" ) ;
54
+ list_builder. values ( ) . append_value ( b"world" ) ;
55
+ list_builder. append ( true ) ;
56
+
57
+ // Second list: [b"rust", b"arrow"]
58
+ list_builder. values ( ) . append_value ( b"rust" ) ;
59
+ list_builder. values ( ) . append_value ( b"arrow" ) ;
60
+ list_builder. append ( true ) ;
61
+
62
+ // Third list: null
63
+ list_builder. append_null ( ) ;
64
+
65
+ let original_list = list_builder. finish ( ) ;
66
+
67
+ // Widen to LargeBinaryArray
68
+ let widened_list = widen_binary_arrays ( & original_list) ;
69
+
70
+ // Verify the result
71
+ assert_eq ! ( widened_list. len( ) , 3 ) ;
72
+ assert ! ( !widened_list. is_null( 0 ) ) ;
73
+ assert ! ( !widened_list. is_null( 1 ) ) ;
74
+ assert ! ( widened_list. is_null( 2 ) ) ;
75
+
76
+ // Check data type
77
+ if let DataType :: List ( field) = widened_list. data_type ( ) {
78
+ assert_eq ! ( field. data_type( ) , & DataType :: LargeBinary ) ;
79
+ } else {
80
+ panic ! ( "Expected List data type" ) ;
81
+ }
82
+ }
83
+ }
0 commit comments