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 } ;
56use arrow_select:: concat:: concat_batches;
67use chrono:: Utc ;
7- use crossterm:: event;
88use datafusion:: common:: tree_node:: TreeNode ;
99use std:: net:: SocketAddr ;
1010use std:: sync:: Arc ;
@@ -28,14 +28,14 @@ use crate::handlers::livetail::cross_origin_config;
2828use crate :: handlers:: http:: query:: { authorize_and_set_filter_tags, into_query} ;
2929use crate :: query:: { TableScanVisitor , QUERY_SESSION } ;
3030use crate :: storage:: object_storage:: commit_schema_to_storage;
31- use crate :: utils:: arrow:: flight:: { get_query_from_ticket, run_do_get_rpc} ;
31+ use crate :: utils:: arrow:: flight:: { add_temporary_events , get_query_from_ticket, run_do_get_rpc} ;
3232use arrow_flight:: {
3333 flight_service_server:: FlightService , Action , ActionType , Criteria , Empty , FlightData ,
3434 FlightDescriptor , FlightInfo , HandshakeRequest , HandshakeResponse , PutResult , SchemaAsIpc ,
3535 SchemaResult , Ticket ,
3636} ;
37- use arrow_ipc:: writer:: { DictionaryTracker , IpcDataGenerator , IpcWriteOptions } ;
38- use futures:: stream:: BoxStream ;
37+ use arrow_ipc:: writer:: IpcWriteOptions ;
38+ use futures:: { stream, TryStreamExt } ;
3939use tonic:: { Request , Response , Status , Streaming } ;
4040
4141use crate :: handlers:: livetail:: extract_session_key;
@@ -50,13 +50,13 @@ pub struct AirServiceImpl {}
5050
5151#[ tonic:: async_trait]
5252impl 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 > > ;
53+ type HandshakeStream = stream :: BoxStream < ' static , Result < HandshakeResponse , Status > > ;
54+ type ListFlightsStream = stream :: BoxStream < ' static , Result < FlightInfo , Status > > ;
55+ type DoGetStream = stream :: BoxStream < ' static , Result < FlightData , Status > > ;
56+ type DoPutStream = stream :: BoxStream < ' static , Result < PutResult , Status > > ;
57+ type DoActionStream = stream :: BoxStream < ' static , Result < arrow_flight:: Result , Status > > ;
58+ type ListActionsStream = stream :: BoxStream < ' static , Result < ActionType , Status > > ;
59+ type DoExchangeStream = stream :: BoxStream < ' static , Result < FlightData , Status > > ;
6060
6161 async fn handshake (
6262 & self ,
@@ -175,24 +175,26 @@ impl FlightService for AirServiceImpl {
175175 let mut batches = run_do_get_rpc ( im, sql. clone ( ) ) . await ?;
176176 minute_result. append ( & mut batches) ;
177177 }
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- }
178+ let mr = minute_result. iter ( ) . collect :: < Vec < _ > > ( ) ;
179+ let event = add_temporary_events ( & stream_name, mr) . await ?;
180+
181+ // let schema = STREAM_INFO
182+ // .schema(&stream_name)
183+ // .map_err(|err| Status::failed_precondition(format!("Metadata Error: {}", err)))?;
184+ // let rb = concat_batches(&schema, mr)
185+ // .map_err(|err| Status::failed_precondition(format!("ArrowError: {}", err)))?;
186+ //
187+ // let event = push_logs_unchecked(rb, &stream_name)
188+ // .await
189+ // .map_err(|err| Status::internal(err.to_string()))?;
190+ // let mut events = vec![];
191+ // for batch in minute_result {
192+ // events.push(
193+ // push_logs_unchecked(batch, &stream_name)
194+ // .await
195+ // .map_err(|err| Status::internal(err.to_string()))?,
196+ // );
197+ // }
196198 Some ( event)
197199 } else {
198200 None
@@ -216,22 +218,30 @@ impl FlightService for AirServiceImpl {
216218 . map ( |batch| batch. schema ( ) )
217219 . map ( |s| s. as_ref ( ) . clone ( ) )
218220 . collect :: < Vec < _ > > ( ) ;
219-
220221 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 ) ) ;
222+ let input_stream = futures:: stream:: iter ( results. into_iter ( ) . map ( Ok ) ) ;
223+
224+ let flight_data_stream = FlightDataEncoderBuilder :: new ( )
225+ . with_max_flight_data_size ( usize:: MAX )
226+ . with_schema ( schema. into ( ) )
227+ . build ( input_stream) ;
228+
229+ let flight_data_stream = flight_data_stream. map_err ( |err| Status :: unknown ( err. to_string ( ) ) ) ;
230+
231+ // let options = datafusion::arrow::ipc::writer::IpcWriteOptions::default();
232+ // let schema_flight_data = SchemaAsIpc::new(&schema, &options);
233+ //
234+ // let mut flights = vec![FlightData::from(schema_flight_data)];
235+ // let encoder = IpcDataGenerator::default();
236+ // let mut tracker = DictionaryTracker::new(false);
237+ // for batch in &results {
238+ // let (flight_dictionaries, flight_batch) = encoder
239+ // .encoded_batch(batch, &mut tracker, &options)
240+ // .map_err(|e| Status::internal(e.to_string()))?;
241+ // flights.extend(flight_dictionaries.into_iter().map(Into::into));
242+ // flights.push(flight_batch.into());
243+ // }
244+ // let output = futures::stream::iter(flights.into_iter().map(Ok));
235245 if let Some ( events) = events {
236246 events. clear ( & stream_name) ;
237247 // for event in events {
@@ -244,7 +254,9 @@ impl FlightService for AirServiceImpl {
244254 . with_label_values ( & [ & format ! ( "flight-query-{}" , stream_name) ] )
245255 . observe ( time) ;
246256
247- Ok ( Response :: new ( Box :: pin ( output) as Self :: DoGetStream ) )
257+ Ok ( Response :: new (
258+ Box :: pin ( flight_data_stream) as Self :: DoGetStream
259+ ) )
248260 }
249261
250262 async fn do_put (
0 commit comments