1515// specific language governing permissions and limitations
1616// under the License.
1717
18- use std:: vec;
19-
2018use crate :: cast:: * ;
21- use arrow_array:: Array ;
19+ use arrow_array:: { Array , ArrayRef } ;
2220
2321/// Attempts to cast a `RunArray` with index type K into
2422/// `to_type` for supported types.
@@ -137,18 +135,7 @@ pub(crate) fn cast_to_run_end_encoded<K: RunEndIndexType>(
137135 }
138136
139137 // Identify run boundaries by comparing consecutive values
140- let mut run_ends = Vec :: new ( ) ;
141- let mut values_indexes = vec ! [ 0usize ] ; // Always include the first index
142- let mut current_data = cast_array. slice ( 0 , 1 ) . to_data ( ) ;
143- for idx in 1 ..cast_array. len ( ) {
144- let next_data = cast_array. slice ( idx, 1 ) . to_data ( ) ;
145- if current_data != next_data {
146- run_ends. push ( idx) ;
147- values_indexes. push ( idx) ;
148- current_data = next_data;
149- }
150- }
151- run_ends. push ( cast_array. len ( ) ) ;
138+ let ( run_ends, values_indexes) = compute_run_boundaries ( cast_array) ;
152139
153140 // Build the run_ends array
154141 for run_end in run_ends {
@@ -167,3 +154,23 @@ pub(crate) fn cast_to_run_end_encoded<K: RunEndIndexType>(
167154 let run_array = RunArray :: < K > :: try_new ( & run_ends_array, values_array. as_ref ( ) ) ?;
168155 Ok ( Arc :: new ( run_array) )
169156}
157+
158+ fn compute_run_boundaries ( array : & ArrayRef ) -> ( Vec < usize > , Vec < usize > ) {
159+ let mut run_ends = Vec :: new ( ) ;
160+ let mut values_indexes = Vec :: new ( ) ;
161+ if array. is_empty ( ) {
162+ return ( run_ends, values_indexes) ;
163+ }
164+ values_indexes. push ( 0 ) ;
165+ let mut current_data = array. slice ( 0 , 1 ) . to_data ( ) ;
166+ for idx in 1 ..array. len ( ) {
167+ let next_data = array. slice ( idx, 1 ) . to_data ( ) ;
168+ if current_data != next_data {
169+ run_ends. push ( idx) ;
170+ values_indexes. push ( idx) ;
171+ current_data = next_data;
172+ }
173+ }
174+ run_ends. push ( array. len ( ) ) ;
175+ ( run_ends, values_indexes)
176+ }
0 commit comments