@@ -44,13 +44,36 @@ use crate::column::chunker::CdcChunk;
4444use crate :: column:: writer:: LevelDataRef ;
4545use crate :: errors:: { ParquetError , Result } ;
4646use arrow_array:: cast:: AsArray ;
47- use arrow_array:: { Array , ArrayRef , OffsetSizeTrait } ;
47+ use arrow_array:: types:: RunEndIndexType ;
48+ use arrow_array:: { Array , ArrayRef , Int32Array , OffsetSizeTrait , RunArray , downcast_run_array} ;
4849use arrow_buffer:: bit_iterator:: BitIndexIterator ;
4950use arrow_buffer:: { NullBuffer , OffsetBuffer , ScalarBuffer } ;
5051use arrow_schema:: { DataType , Field } ;
5152use std:: ops:: Range ;
5253use std:: sync:: Arc ;
5354
55+ /// Expands a [`DataType::RunEndEncoded`] array into a flat (logical) array of its values type.
56+ ///
57+ /// use `arrow_select::take` to materialize the full-length flat array.
58+ /// This is intentionally simple (O(n)); efficiency can/should be improved
59+ fn expand_ree_array ( array : & ArrayRef ) -> Result < ArrayRef > {
60+ downcast_run_array ! (
61+ array => expand_typed_ree( array) ,
62+ _ => unreachable!( "expand_ree_array called on non-REE array" ) ,
63+ )
64+ }
65+
66+ fn expand_typed_ree < R : RunEndIndexType > ( run_array : & RunArray < R > ) -> Result < ArrayRef > {
67+ let run_ends = run_array. run_ends ( ) ;
68+ let values = run_array. values ( ) ;
69+ let len = run_array. len ( ) ;
70+ let indices: Int32Array = ( 0 ..len)
71+ . map ( |i| run_ends. get_physical_index ( i) as i32 )
72+ . collect ( ) ;
73+ arrow_select:: take:: take ( values. as_ref ( ) , & indices, None )
74+ . map_err ( |e| arrow_err ! ( "Failed to expand REE array: {}" , e) )
75+ }
76+
5477/// Performs a depth-first scan of the children of `array`, constructing [`ArrayLevels`]
5578/// for each leaf column encountered
5679pub ( crate ) fn calculate_array_levels ( array : & ArrayRef , field : & Field ) -> Result < Vec < ArrayLevels > > {
@@ -185,6 +208,15 @@ impl LevelInfoBuilder {
185208 let levels = ArrayLevels :: new ( parent_ctx, is_nullable, array. clone ( ) ) ;
186209 Ok ( Self :: Primitive ( levels) )
187210 }
211+ DataType :: RunEndEncoded ( _, value_field) => {
212+ let flat = expand_ree_array ( array) ?;
213+ let flat_field = Field :: new (
214+ field. name ( ) ,
215+ value_field. data_type ( ) . clone ( ) ,
216+ field. is_nullable ( ) ,
217+ ) ;
218+ Self :: try_new ( & flat_field, parent_ctx, & flat)
219+ }
188220 DataType :: Struct ( children) => {
189221 let array = array. as_struct ( ) ;
190222 let def_level = match is_nullable {
0 commit comments