11use arrow_array:: RecordBatch ;
2+ use arrow_flight:: encode:: FlightDataEncoderBuilder ;
23use arrow_flight:: flight_service_server:: FlightServiceServer ;
34use arrow_flight:: PollInfo ;
45use arrow_schema:: { ArrowError , Schema } ;
5- use arrow_select:: concat:: concat_batches;
66use chrono:: Utc ;
7- use crossterm:: event;
87use datafusion:: common:: tree_node:: TreeNode ;
98use std:: net:: SocketAddr ;
109use std:: sync:: Arc ;
@@ -19,7 +18,6 @@ use tonic_web::GrpcWebLayer;
1918use crate :: event:: commit_schema;
2019use crate :: handlers:: http:: cluster:: get_ingestor_info;
2120use crate :: handlers:: http:: fetch_schema;
22- use crate :: handlers:: http:: ingest:: push_logs_unchecked;
2321use crate :: metrics:: QUERY_EXECUTE_TIME ;
2422use crate :: option:: { Mode , CONFIG } ;
2523
@@ -28,14 +26,14 @@ use crate::handlers::livetail::cross_origin_config;
2826use crate :: handlers:: http:: query:: { authorize_and_set_filter_tags, into_query} ;
2927use crate :: query:: { TableScanVisitor , QUERY_SESSION } ;
3028use crate :: storage:: object_storage:: commit_schema_to_storage;
31- use crate :: utils:: arrow:: flight:: { get_query_from_ticket, run_do_get_rpc} ;
29+ use crate :: utils:: arrow:: flight:: { append_temporary_events , get_query_from_ticket, run_do_get_rpc} ;
3230use arrow_flight:: {
3331 flight_service_server:: FlightService , Action , ActionType , Criteria , Empty , FlightData ,
3432 FlightDescriptor , FlightInfo , HandshakeRequest , HandshakeResponse , PutResult , SchemaAsIpc ,
3533 SchemaResult , Ticket ,
3634} ;
37- use arrow_ipc:: writer:: { DictionaryTracker , IpcDataGenerator , IpcWriteOptions } ;
38- use futures:: stream:: BoxStream ;
35+ use arrow_ipc:: writer:: IpcWriteOptions ;
36+ use futures:: { stream, TryStreamExt } ;
3937use tonic:: { Request , Response , Status , Streaming } ;
4038
4139use crate :: handlers:: livetail:: extract_session_key;
@@ -50,13 +48,13 @@ pub struct AirServiceImpl {}
5048
5149#[ tonic:: async_trait]
5250impl FlightService for AirServiceImpl {
53- type HandshakeStream = BoxStream < ' static , Result < HandshakeResponse , Status > > ;
54- type ListFlightsStream = BoxStream < ' static , Result < FlightInfo , Status > > ;
55- type DoGetStream = BoxStream < ' static , Result < FlightData , Status > > ;
56- type DoPutStream = BoxStream < ' static , Result < PutResult , Status > > ;
57- type DoActionStream = BoxStream < ' static , Result < arrow_flight:: Result , Status > > ;
58- type ListActionsStream = BoxStream < ' static , Result < ActionType , Status > > ;
59- type DoExchangeStream = BoxStream < ' static , Result < FlightData , Status > > ;
51+ type HandshakeStream = stream :: BoxStream < ' static , Result < HandshakeResponse , Status > > ;
52+ type ListFlightsStream = stream :: BoxStream < ' static , Result < FlightInfo , Status > > ;
53+ type DoGetStream = stream :: BoxStream < ' static , Result < FlightData , Status > > ;
54+ type DoPutStream = stream :: BoxStream < ' static , Result < PutResult , Status > > ;
55+ type DoActionStream = stream :: BoxStream < ' static , Result < arrow_flight:: Result , Status > > ;
56+ type ListActionsStream = stream :: BoxStream < ' static , Result < ActionType , Status > > ;
57+ type DoExchangeStream = stream :: BoxStream < ' static , Result < FlightData , Status > > ;
6058
6159 async fn handshake (
6260 & self ,
@@ -175,24 +173,27 @@ impl FlightService for AirServiceImpl {
175173 let mut batches = run_do_get_rpc ( im, sql. clone ( ) ) . await ?;
176174 minute_result. append ( & mut batches) ;
177175 }
178- let mr = minute_result. iter ( ) . map ( |rb| rb) . collect :: < Vec < _ > > ( ) ;
179- let schema = STREAM_INFO
180- . schema ( & stream_name)
181- . map_err ( |err| Status :: failed_precondition ( format ! ( "Metadata Error: {}" , err) ) ) ?;
182- let rb = concat_batches ( & schema, mr)
183- . map_err ( |err| Status :: failed_precondition ( format ! ( "ArrowError: {}" , err) ) ) ?;
184-
185- let event = push_logs_unchecked ( rb, & stream_name)
186- . await
187- . map_err ( |err| Status :: internal ( err. to_string ( ) ) ) ?;
188- let mut events = vec ! [ ] ;
189- for batch in minute_result {
190- events. push (
191- push_logs_unchecked ( batch, & stream_name)
192- . await
193- . map_err ( |err| Status :: internal ( err. to_string ( ) ) ) ?,
194- ) ;
195- }
176+ let mr = minute_result. iter ( ) . collect :: < Vec < _ > > ( ) ;
177+ dbg ! ( & mr. len( ) ) ;
178+ let event = append_temporary_events ( & stream_name, mr) . await ?;
179+
180+ // let schema = STREAM_INFO
181+ // .schema(&stream_name)
182+ // .map_err(|err| Status::failed_precondition(format!("Metadata Error: {}", err)))?;
183+ // let rb = concat_batches(&schema, mr)
184+ // .map_err(|err| Status::failed_precondition(format!("ArrowError: {}", err)))?;
185+ //
186+ // let event = push_logs_unchecked(rb, &stream_name)
187+ // .await
188+ // .map_err(|err| Status::internal(err.to_string()))?;
189+ // let mut events = vec![];
190+ // for batch in minute_result {
191+ // events.push(
192+ // push_logs_unchecked(batch, &stream_name)
193+ // .await
194+ // .map_err(|err| Status::internal(err.to_string()))?,
195+ // );
196+ // }
196197 Some ( event)
197198 } else {
198199 None
@@ -210,28 +211,39 @@ impl FlightService for AirServiceImpl {
210211 . await
211212 . map_err ( |err| Status :: internal ( err. to_string ( ) ) )
212213 . unwrap ( ) ;
214+ if results. len ( ) > 1 {
215+ dbg ! ( & results. len( ) ) ;
216+ }
213217
214218 let schemas = results
215219 . iter ( )
216220 . map ( |batch| batch. schema ( ) )
217221 . map ( |s| s. as_ref ( ) . clone ( ) )
218222 . collect :: < Vec < _ > > ( ) ;
219-
220223 let schema = Schema :: try_merge ( schemas) . map_err ( |err| Status :: internal ( err. to_string ( ) ) ) ?;
221- let options = datafusion:: arrow:: ipc:: writer:: IpcWriteOptions :: default ( ) ;
222- let schema_flight_data = SchemaAsIpc :: new ( & schema, & options) ;
223-
224- let mut flights = vec ! [ FlightData :: from( schema_flight_data) ] ;
225- let encoder = IpcDataGenerator :: default ( ) ;
226- let mut tracker = DictionaryTracker :: new ( false ) ;
227- for batch in & results {
228- let ( flight_dictionaries, flight_batch) = encoder
229- . encoded_batch ( batch, & mut tracker, & options)
230- . map_err ( |e| Status :: internal ( e. to_string ( ) ) ) ?;
231- flights. extend ( flight_dictionaries. into_iter ( ) . map ( Into :: into) ) ;
232- flights. push ( flight_batch. into ( ) ) ;
233- }
234- let output = futures:: stream:: iter ( flights. into_iter ( ) . map ( Ok ) ) ;
224+ let input_stream = futures:: stream:: iter ( results. into_iter ( ) . map ( Ok ) ) ;
225+
226+ let flight_data_stream = FlightDataEncoderBuilder :: new ( )
227+ . with_max_flight_data_size ( usize:: MAX )
228+ . with_schema ( schema. into ( ) )
229+ . build ( input_stream) ;
230+
231+ let flight_data_stream = flight_data_stream. map_err ( |err| Status :: unknown ( err. to_string ( ) ) ) ;
232+
233+ // let options = datafusion::arrow::ipc::writer::IpcWriteOptions::default();
234+ // let schema_flight_data = SchemaAsIpc::new(&schema, &options);
235+ //
236+ // let mut flights = vec![FlightData::from(schema_flight_data)];
237+ // let encoder = IpcDataGenerator::default();
238+ // let mut tracker = DictionaryTracker::new(false);
239+ // for batch in &results {
240+ // let (flight_dictionaries, flight_batch) = encoder
241+ // .encoded_batch(batch, &mut tracker, &options)
242+ // .map_err(|e| Status::internal(e.to_string()))?;
243+ // flights.extend(flight_dictionaries.into_iter().map(Into::into));
244+ // flights.push(flight_batch.into());
245+ // }
246+ // let output = futures::stream::iter(flights.into_iter().map(Ok));
235247 if let Some ( events) = events {
236248 events. clear ( & stream_name) ;
237249 // for event in events {
@@ -244,7 +256,9 @@ impl FlightService for AirServiceImpl {
244256 . with_label_values ( & [ & format ! ( "flight-query-{}" , stream_name) ] )
245257 . observe ( time) ;
246258
247- Ok ( Response :: new ( Box :: pin ( output) as Self :: DoGetStream ) )
259+ Ok ( Response :: new (
260+ Box :: pin ( flight_data_stream) as Self :: DoGetStream
261+ ) )
248262 }
249263
250264 async fn do_put (
@@ -297,9 +311,8 @@ pub fn server() -> impl Future<Output = Result<(), Box<dyn std::error::Error + S
297311 let svc = FlightServiceServer :: new ( service)
298312 . max_encoding_message_size ( usize:: MAX )
299313 . max_decoding_message_size ( usize:: MAX )
300- . send_compressed ( CompressionEncoding :: Gzip ) ;
301-
302- dbg ! ( & svc) ;
314+ . send_compressed ( CompressionEncoding :: Zstd )
315+ . accept_compressed ( CompressionEncoding :: Zstd ) ;
303316
304317 let cors = cross_origin_config ( ) ;
305318
0 commit comments