@@ -20,6 +20,8 @@ use enum_as_inner::EnumAsInner;
2020
2121use super :: AggregateFunctionRef ;
2222use crate :: types:: binary:: BinaryColumnBuilder ;
23+ use crate :: types:: DataType ;
24+ use crate :: ColumnBuilder ;
2325
2426#[ derive( Clone , Copy , Debug ) ]
2527pub struct StateAddr {
@@ -113,11 +115,11 @@ impl From<StateAddr> for usize {
113115
114116pub fn get_states_layout ( funcs : & [ AggregateFunctionRef ] ) -> Result < StatesLayout > {
115117 let mut registry = AggrStateRegistry :: default ( ) ;
116- let mut serialize_size = Vec :: with_capacity ( funcs. len ( ) ) ;
118+ let mut serialize_type = Vec :: with_capacity ( funcs. len ( ) ) ;
117119 for func in funcs {
118120 func. register_state ( & mut registry) ;
119121 registry. commit ( ) ;
120- serialize_size . push ( func. serialize_size_per_row ( ) ) ;
122+ serialize_type . push ( StateSerdeType ( func. serialize_type ( ) . into ( ) ) ) ;
121123 }
122124
123125 let AggrStateRegistry { states, offsets } = registry;
@@ -132,7 +134,7 @@ pub fn get_states_layout(funcs: &[AggregateFunctionRef]) -> Result<StatesLayout>
132134 Ok ( StatesLayout {
133135 layout,
134136 states_loc,
135- serialize_size ,
137+ serialize_type ,
136138 } )
137139}
138140
@@ -191,18 +193,66 @@ impl AggrStateLoc {
191193 }
192194}
193195
196+ #[ derive( Debug , Clone ) ]
197+ pub enum StateSerdeItem {
198+ DataType ( DataType ) ,
199+ Binary ( Option < usize > ) ,
200+ }
201+
202+ #[ derive( Debug , Clone ) ]
203+ pub struct StateSerdeType ( Box < [ StateSerdeItem ] > ) ;
204+
205+ impl StateSerdeType {
206+ pub fn new ( items : impl Into < Box < [ StateSerdeItem ] > > ) -> Self {
207+ StateSerdeType ( items. into ( ) )
208+ }
209+
210+ pub fn data_type ( & self ) -> DataType {
211+ DataType :: Tuple (
212+ self . 0
213+ . iter ( )
214+ . map ( |item| match item {
215+ StateSerdeItem :: DataType ( data_type) => data_type. clone ( ) ,
216+ StateSerdeItem :: Binary ( _) => DataType :: Binary ,
217+ } )
218+ . collect ( ) ,
219+ )
220+ }
221+ }
222+
194223#[ derive( Debug , Clone ) ]
195224pub struct StatesLayout {
196225 pub layout : Layout ,
197226 pub states_loc : Vec < Box < [ AggrStateLoc ] > > ,
198- serialize_size : Vec < Option < usize > > ,
227+ pub ( super ) serialize_type : Vec < StateSerdeType > ,
199228}
200229
201230impl StatesLayout {
202- pub fn serialize_builders ( & self , num_rows : usize ) -> Vec < BinaryColumnBuilder > {
203- self . serialize_size
231+ pub fn num_aggr_func ( & self ) -> usize {
232+ self . states_loc . len ( )
233+ }
234+
235+ pub fn serialize_builders ( & self , num_rows : usize ) -> Vec < ColumnBuilder > {
236+ self . serialize_type
204237 . iter ( )
205- . map ( |size| BinaryColumnBuilder :: with_capacity ( num_rows, num_rows * size. unwrap_or ( 0 ) ) )
238+ . map ( |serde_type| {
239+ let builder = serde_type
240+ . 0
241+ . iter ( )
242+ . map ( |item| match item {
243+ StateSerdeItem :: DataType ( data_type) => {
244+ ColumnBuilder :: with_capacity ( data_type, num_rows)
245+ }
246+ StateSerdeItem :: Binary ( size) => {
247+ ColumnBuilder :: Binary ( BinaryColumnBuilder :: with_capacity (
248+ num_rows,
249+ num_rows * size. unwrap_or ( 0 ) ,
250+ ) )
251+ }
252+ } )
253+ . collect ( ) ;
254+ ColumnBuilder :: Tuple ( builder)
255+ } )
206256 . collect ( )
207257 }
208258}
0 commit comments