@@ -50,6 +50,15 @@ pub(crate) fn cast_single_element_fixed_size_list_to_values(
5050 cast_with_options ( values, to, cast_options)
5151}
5252
53+ pub ( crate ) fn cast_list_to_list_view < OffsetSize > ( array : & dyn Array ) -> Result < ArrayRef , ArrowError >
54+ where
55+ OffsetSize : OffsetSizeTrait ,
56+ {
57+ let list = array. as_list :: < OffsetSize > ( ) ;
58+ let list_view: GenericListViewArray < OffsetSize > = list. clone ( ) . into ( ) ;
59+ Ok ( Arc :: new ( list_view) )
60+ }
61+
5362pub ( crate ) fn cast_fixed_size_list_to_list < OffsetSize > (
5463 array : & dyn Array ,
5564) -> Result < ArrayRef , ArrowError >
@@ -160,6 +169,48 @@ pub(crate) fn cast_list_values<O: OffsetSizeTrait>(
160169 ) ?) )
161170}
162171
172+ /// Helper function to cast the values in a list view to a list
173+ pub ( crate ) fn cast_list_view_values < O : OffsetSizeTrait > (
174+ array : & dyn Array ,
175+ to : & FieldRef ,
176+ cast_options : & CastOptions ,
177+ ) -> Result < ArrayRef , ArrowError > {
178+ let list_view = array. as_list_view :: < O > ( ) ;
179+ let list_view_offsets = list_view. offsets ( ) ;
180+ let sizes = list_view. sizes ( ) ;
181+ let source_values = list_view. values ( ) ;
182+
183+ // Construct the indices and offsets for the new list array by iterating over the list view subarrays
184+ let mut indices = Vec :: with_capacity ( list_view. values ( ) . len ( ) ) ;
185+ let mut offsets = Vec :: with_capacity ( list_view. len ( ) + 1 ) ;
186+ // Add the offset for the first subarray
187+ offsets. push ( O :: usize_as ( 0 ) ) ;
188+ for i in 0 ..list_view. len ( ) {
189+ // For each subarray, add the indices of the values to take
190+ let offset = list_view_offsets[ i] . as_usize ( ) ;
191+ let size = sizes[ i] . as_usize ( ) ;
192+ let end = offset + size;
193+ for j in offset..end {
194+ indices. push ( j as i32 ) ;
195+ }
196+ // Add the offset for the next subarray
197+ offsets. push ( O :: usize_as ( indices. len ( ) ) ) ;
198+ }
199+
200+ // Take the values from the source values using the indices, creating a new array
201+ let values = arrow_select:: take:: take ( source_values, & Int32Array :: from ( indices) , None ) ?;
202+
203+ // Cast the values to the target data type
204+ let values = cast_with_options ( & values, to. data_type ( ) , cast_options) ?;
205+
206+ Ok ( Arc :: new ( GenericListArray :: < O > :: try_new (
207+ to. clone ( ) ,
208+ OffsetBuffer :: new ( offsets. into ( ) ) ,
209+ values,
210+ list_view. nulls ( ) . cloned ( ) ,
211+ ) ?) )
212+ }
213+
163214/// Cast the container type of List/Largelist array along with the inner datatype
164215pub ( crate ) fn cast_list < I : OffsetSizeTrait , O : OffsetSizeTrait > (
165216 array : & dyn Array ,
0 commit comments