@@ -28,8 +28,10 @@ use tonic::transport::Server;
2828use tonic:: { Request , Response , Status , Streaming } ;
2929
3030use crate :: Registry ;
31- use crate :: dataset:: { Chunk , DataStream } ;
32- use crate :: record:: { Records , arrow_schema_to_strata, strata_schema_to_arrow_schema} ;
31+ use crate :: record:: {
32+ Batch , BatchPage , DataStream , arrow_schema_to_strata, strata_schema_to_arrow_schema,
33+ } ;
34+ use schema:: Schema as StrataSchema ;
3335use crate :: router:: { Body , Method } ;
3436
3537/// Start the Flight server on `addr`, serving every registered provider.
@@ -115,20 +117,20 @@ fn ticket_target(ticket: &Ticket) -> Result<(String, String), Status> {
115117
116118type FlightStream < T > = BoxStream < ' static , Result < T , Status > > ;
117119
118- /// Encode one [`Chunk`] (a page) : its batches as Arrow `FlightData`, then a
119- /// metadata-only message whose `app_metadata` is the chunk's cursor (when present)
120- /// — the per-batch checkpoint, forwarded opaquely.
121- fn encode_chunk ( chunk : Chunk ) -> FlightStream < FlightData > {
122- let Chunk { records , cursor } = chunk ;
123- let schema = records . schema . clone ( ) ;
124- let batches = futures:: stream:: iter (
125- records
126- . batches
127- . into_iter ( )
128- . map ( Ok :: < RecordBatch , FlightError > ) ,
129- ) ;
120+ /// Encode one [`BatchPage`] : its columns as Arrow `FlightData`
121+ fn encode_chunk ( chunk : BatchPage , schema : & StrataSchema ) -> FlightStream < FlightData > {
122+ let BatchPage { data , cursor } = chunk ;
123+ let batch = match data . to_record_batch ( schema ) {
124+ Ok ( batch ) => batch ,
125+ Err ( e ) => {
126+ return futures:: stream:: once ( async move { Err ( Status :: internal ( e . to_string ( ) ) ) } )
127+ . boxed ( ) ;
128+ }
129+ } ;
130+ let arrow_schema = batch . schema ( ) ;
131+ let batches = futures :: stream :: iter ( [ Ok :: < RecordBatch , FlightError > ( batch ) ] ) ;
130132 let data = FlightDataEncoderBuilder :: new ( )
131- . with_schema ( schema )
133+ . with_schema ( arrow_schema )
132134 . build ( batches)
133135 . map ( |d| d. map_err ( |e| Status :: internal ( e. to_string ( ) ) ) ) ;
134136 let trailer = cursor. map ( |cursor| {
@@ -225,10 +227,11 @@ impl FlightService for StrataFlight {
225227 . read ( & path)
226228 . await
227229 . map_err ( |e| Status :: internal ( e. to_string ( ) ) ) ?;
230+ let schema = stream. schema . clone ( ) ;
228231 let out = stream
229232 . chunks
230- . flat_map ( |chunk| match chunk {
231- Ok ( chunk) => encode_chunk ( chunk) ,
233+ . flat_map ( move |chunk| match chunk {
234+ Ok ( chunk) => encode_chunk ( chunk, & schema ) ,
232235 Err ( e) => {
233236 futures:: stream:: once ( async move { Err ( Status :: internal ( e. to_string ( ) ) ) } )
234237 . boxed ( )
@@ -291,24 +294,19 @@ impl FlightService for StrataFlight {
291294 . clone ( ) ;
292295 let schema = arrow_schema_to_strata ( & arrow_schema) ;
293296
294- // One chunk per batch; the tail streams lazily. No cursor on the write path.
295- let rest_schema = arrow_schema. clone ( ) ;
296- let head_chunk = futures:: stream:: iter ( first_batch. map ( move |batch| {
297- Ok :: < _ , anyhow:: Error > ( Chunk {
298- records : Records {
299- schema : arrow_schema,
300- batches : vec ! [ batch] ,
301- } ,
297+ // One page per batch; the tail streams lazily. No cursor on the write path.
298+ // The first batch was pulled only to make the schema available, so it is put
299+ // back on the front here.
300+ let head_chunk = futures:: stream:: iter ( first_batch. map ( |batch| {
301+ Ok :: < _ , anyhow:: Error > ( BatchPage {
302+ data : Batch :: from_record_batch ( & batch) ,
302303 cursor : None ,
303304 } )
304305 } ) ) ;
305- let rest_chunks = decoder. map ( move |batch| {
306+ let rest_chunks = decoder. map ( |batch| {
306307 let batch = batch. map_err ( |e| anyhow:: anyhow!( e. to_string( ) ) ) ?;
307- Ok ( Chunk {
308- records : Records {
309- schema : rest_schema. clone ( ) ,
310- batches : vec ! [ batch] ,
311- } ,
308+ Ok ( BatchPage {
309+ data : Batch :: from_record_batch ( & batch) ,
312310 cursor : None ,
313311 } )
314312 } ) ;
0 commit comments