@@ -16,10 +16,20 @@ use std::collections::HashMap;
1616use std:: fmt;
1717use std:: fmt:: Debug ;
1818
19+ use databend_common_column:: buffer:: Buffer ;
20+ use databend_common_exception:: ErrorCode ;
21+ use databend_common_exception:: Result ;
1922use databend_common_expression:: BlockMetaInfo ;
2023use databend_common_expression:: BlockMetaInfoDowncast ;
2124use databend_common_expression:: Column ;
25+ use databend_common_expression:: ColumnBuilder ;
26+ use databend_common_expression:: DataBlock ;
27+ use databend_common_expression:: DataSchemaRef ;
2228use databend_common_expression:: Scalar ;
29+ use databend_common_expression:: types:: ArrayColumn ;
30+ use databend_common_expression:: types:: NumberColumn ;
31+ use databend_common_expression:: types:: NumberColumnBuilder ;
32+ use databend_common_expression:: types:: array:: ArrayColumnBuilder ;
2333
2434use crate :: pipelines:: processors:: transforms:: RuntimeFilterDesc ;
2535
@@ -84,15 +94,153 @@ impl JoinRuntimeFilterPacket {
8494 }
8595}
8696
97+ #[ derive( serde:: Serialize , serde:: Deserialize , Clone , Debug , Default , PartialEq ) ]
98+ struct FlightRuntimeFilterPacket {
99+ pub id : usize ,
100+ pub bloom : Option < usize > ,
101+ pub inlist : Option < usize > ,
102+ pub min_max : Option < SerializableDomain > ,
103+ }
104+
105+ #[ derive( serde:: Serialize , serde:: Deserialize , Clone , Debug , Default , PartialEq ) ]
106+ struct FlightJoinRuntimeFilterPacket {
107+ #[ serde( default ) ]
108+ pub build_rows : usize ,
109+ #[ serde( default ) ]
110+ pub packets : Option < HashMap < usize , FlightRuntimeFilterPacket > > ,
111+
112+ pub schema : DataSchemaRef ,
113+ }
114+
115+ impl TryInto < DataBlock > for JoinRuntimeFilterPacket {
116+ type Error = ErrorCode ;
117+
118+ fn try_into ( mut self ) -> Result < DataBlock > {
119+ let mut entities = vec ! [ ] ;
120+ let mut join_flight_packets = None ;
121+
122+ if let Some ( packets) = self . packets . take ( ) {
123+ let mut flight_packets = HashMap :: with_capacity ( packets. len ( ) ) ;
124+
125+ for ( id, packet) in packets {
126+ let mut inlist_pos = None ;
127+ if let Some ( in_list) = packet. inlist {
128+ let len = in_list. len ( ) as u64 ;
129+ inlist_pos = Some ( entities. len ( ) ) ;
130+ entities. push ( Column :: Array ( Box :: new ( ArrayColumn :: new (
131+ in_list,
132+ Buffer :: from ( vec ! [ 0 , len] ) ,
133+ ) ) ) ) ;
134+ }
135+
136+ let mut bloom_pos = None ;
137+ if let Some ( bloom_filter) = packet. bloom {
138+ let len = bloom_filter. len ( ) as u64 ;
139+ bloom_pos = Some ( entities. len ( ) ) ;
140+
141+ let builder = ArrayColumnBuilder {
142+ builder : ColumnBuilder :: Number ( NumberColumnBuilder :: UInt64 ( bloom_filter) ) ,
143+ offsets : vec ! [ 0 , len] ,
144+ } ;
145+ entities. push ( Column :: Array ( Box :: new ( builder. build ( ) ) ) ) ;
146+ }
147+
148+ flight_packets. insert ( id, FlightRuntimeFilterPacket {
149+ id,
150+ bloom : bloom_pos,
151+ inlist : inlist_pos,
152+ min_max : packet. min_max ,
153+ } ) ;
154+ }
155+
156+ join_flight_packets = Some ( flight_packets) ;
157+ }
158+
159+ let data_block = match entities. is_empty ( ) {
160+ true => DataBlock :: empty ( ) ,
161+ false => DataBlock :: new_from_columns ( entities) ,
162+ } ;
163+
164+ let schema = DataSchemaRef :: new ( data_block. infer_schema ( ) ) ;
165+
166+ data_block. add_meta ( Some ( Box :: new ( FlightJoinRuntimeFilterPacket {
167+ build_rows : self . build_rows ,
168+ packets : join_flight_packets,
169+ schema,
170+ } ) ) )
171+ }
172+ }
173+
174+ impl TryFrom < DataBlock > for JoinRuntimeFilterPacket {
175+ type Error = ErrorCode ;
176+
177+ fn try_from ( mut block : DataBlock ) -> Result < Self > {
178+ if let Some ( meta) = block. take_meta ( ) {
179+ let flight_join_rf = FlightJoinRuntimeFilterPacket :: downcast_from ( meta)
180+ . ok_or_else ( || ErrorCode :: Internal ( "It's a bug" ) ) ?;
181+
182+ let Some ( packet) = flight_join_rf. packets else {
183+ return Ok ( JoinRuntimeFilterPacket {
184+ packets : None ,
185+ build_rows : flight_join_rf. build_rows ,
186+ } ) ;
187+ } ;
188+
189+ let mut flight_packets = HashMap :: with_capacity ( packet. len ( ) ) ;
190+ for ( id, flight_packet) in packet {
191+ let mut inlist = None ;
192+ if let Some ( column_idx) = flight_packet. inlist {
193+ let column = block. get_by_offset ( column_idx) . clone ( ) ;
194+ let column = column. into_column ( ) . unwrap ( ) ;
195+ let array_column = column. into_array ( ) . expect ( "it's a bug" ) ;
196+ inlist = Some ( array_column. index ( 0 ) . expect ( "It's a bug" ) ) ;
197+ }
198+
199+ let mut bloom = None ;
200+ if let Some ( column_idx) = flight_packet. bloom {
201+ let column = block. get_by_offset ( column_idx) . clone ( ) ;
202+ let column = column. into_column ( ) . unwrap ( ) ;
203+ let array_column = column. into_array ( ) . expect ( "it's a bug" ) ;
204+ let bloom_value_column = array_column. index ( 0 ) . expect ( "It's a bug" ) ;
205+ bloom = Some ( match bloom_value_column {
206+ Column :: Number ( NumberColumn :: UInt64 ( v) ) => v. to_vec ( ) ,
207+ _ => unreachable ! ( "Unexpected runtime bloom filter column type" ) ,
208+ } )
209+ }
210+
211+ flight_packets. insert ( id, RuntimeFilterPacket {
212+ bloom,
213+ inlist,
214+ id : flight_packet. id ,
215+ min_max : flight_packet. min_max ,
216+ } ) ;
217+ }
218+
219+ return Ok ( JoinRuntimeFilterPacket {
220+ packets : Some ( flight_packets) ,
221+ build_rows : flight_join_rf. build_rows ,
222+ } ) ;
223+ }
224+
225+ Err ( ErrorCode :: Internal (
226+ "Unexpected runtime filter packet meta type. It's a bug" ,
227+ ) )
228+ }
229+ }
230+
87231#[ typetag:: serde( name = "join_runtime_filter_packet" ) ]
88- impl BlockMetaInfo for JoinRuntimeFilterPacket {
232+ impl BlockMetaInfo for FlightJoinRuntimeFilterPacket {
89233 fn equals ( & self , info : & Box < dyn BlockMetaInfo > ) -> bool {
90- JoinRuntimeFilterPacket :: downcast_ref_from ( info) . is_some_and ( |other| self == other)
234+ FlightJoinRuntimeFilterPacket :: downcast_ref_from ( info) . is_some_and ( |other| self == other)
91235 }
92236
93237 fn clone_self ( & self ) -> Box < dyn BlockMetaInfo > {
94238 Box :: new ( self . clone ( ) )
95239 }
240+
241+ fn override_block_schema ( & self ) -> Option < DataSchemaRef > {
242+ Some ( self . schema . clone ( ) )
243+ }
96244}
97245
98246#[ derive( serde:: Serialize , serde:: Deserialize , Clone , Debug , PartialEq ) ]
0 commit comments