datafusion-dft 0.3.0

An opinionated and batteries included DataFusion implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::execution::AppExecution;
use arrow_flight::encode::FlightDataEncoderBuilder;
use arrow_flight::error::FlightError;
use arrow_flight::flight_service_server::{FlightService, FlightServiceServer};
use arrow_flight::sql::server::FlightSqlService;
use arrow_flight::sql::{
    ActionClosePreparedStatementRequest, ActionCreatePreparedStatementRequest,
    ActionCreatePreparedStatementResult, Any, CommandGetCatalogs, CommandGetDbSchemas,
    CommandGetSqlInfo, CommandGetTableTypes, CommandGetTables, CommandGetXdbcTypeInfo,
    CommandPreparedStatementQuery, CommandStatementQuery, SqlInfo, TicketStatementQuery,
};
use arrow_flight::{
    Action, FlightDescriptor, FlightEndpoint, FlightInfo, IpcMessage, SchemaAsIpc, Ticket,
};
use color_eyre::Result;
use datafusion::arrow::datatypes::Schema;
use datafusion::arrow::error::ArrowError;
use datafusion::arrow::ipc::writer::IpcWriteOptions;
use datafusion::logical_expr::LogicalPlan;
use datafusion::prelude::{col, lit};
use datafusion::sql::parser::DFParser;
use datafusion_app::local::ExecutionContext;
use datafusion_app::observability::ObservabilityRequestDetails;
use futures::{StreamExt, TryStreamExt};
use jiff::Timestamp;
use log::{debug, error, info};
use metrics::{counter, histogram};
use prost::bytes::Bytes;
use prost::Message;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use tonic::{Code, Request, Response, Status};
use uuid::Uuid;

/// Prepared statement handle containing the logical plan and metadata
#[derive(Clone)]
pub struct PreparedStatementHandle {
    pub plan: LogicalPlan,
    pub parameter_schema: Option<Arc<Schema>>,
    pub dataset_schema: Arc<Schema>,
    pub created_at: Timestamp,
}

#[derive(Clone)]
pub struct FlightSqlServiceImpl {
    requests: Arc<Mutex<HashMap<Uuid, LogicalPlan>>>,
    prepared_statements: Arc<Mutex<HashMap<Uuid, PreparedStatementHandle>>>,
    execution: ExecutionContext,
}

impl FlightSqlServiceImpl {
    pub fn new(execution: AppExecution) -> Self {
        let requests = HashMap::new();
        let prepared_statements = HashMap::new();
        Self {
            execution: execution.execution_ctx().clone(),
            requests: Arc::new(Mutex::new(requests)),
            prepared_statements: Arc::new(Mutex::new(prepared_statements)),
        }
    }

    /// Return a [`FlightServiceServer`] that can be used with a
    /// [`Server`](tonic::transport::Server)
    pub fn service(&self) -> FlightServiceServer<Self> {
        // wrap up tonic goop
        FlightServiceServer::new(self.clone())
    }

    async fn do_get_common_handler(
        &self,
        request_id: String,
    ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status> {
        match Uuid::from_str(&request_id) {
            Ok(id) => {
                info!("getting plan for id: {:?}", id);
                // Limit the scope of the lock
                let maybe_plan = {
                    let guard = self
                        .requests
                        .lock()
                        .map_err(|_| Status::internal("Failed to acquire lock on requests"))?;
                    guard.get(&id).cloned()
                };
                if let Some(plan) = maybe_plan {
                    let stream = self
                        .execution
                        .execute_logical_plan(plan)
                        .await
                        .map_err(|e| Status::internal(e.to_string()))?;
                    let builder = FlightDataEncoderBuilder::new();
                    let flight_data_stream = builder
                        .build(stream.map_err(|e| FlightError::ExternalError(Box::new(e))))
                        .map_err(|e| Status::internal(e.to_string()))
                        .boxed();
                    Ok(Response::new(flight_data_stream))
                } else {
                    Err(Status::internal("plan not found for id"))
                }
            }
            Err(e) => {
                error!("error decoding handle to uuid for {request_id}: {:?}", e);
                Err(Status::internal(
                    "error decoding handle to uuid for {request_id}",
                ))
            }
        }
    }

    async fn record_request(
        &self,
        start: Timestamp,
        request_id: Option<String>,
        response_err: Option<&Status>,
        path: String,
        latency_metric: &'static str,
    ) {
        let duration = Timestamp::now() - start;
        let grpc_code = match &response_err {
            None => Code::Ok,
            Some(status) => status.code(),
        };
        let ctx = self.execution.session_ctx();
        let req = ObservabilityRequestDetails {
            request_id,
            path,
            sql: None,
            rows: None,
            start_ms: start.as_millisecond(),
            duration_ms: duration.get_milliseconds(),
            status: grpc_code as u16,
        };
        if let Err(e) = self
            .execution
            .observability()
            .try_record_request(ctx, req)
            .await
        {
            error!("error recording request: {}", e)
        }

        histogram!(latency_metric).record(duration.get_milliseconds() as f64);
    }

    async fn create_flight_info_for_logical_plan(
        &self,
        logical_plan: LogicalPlan,
        request_id: Uuid,
        _request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        let schema = logical_plan.schema();
        let ticket = TicketStatementQuery {
            statement_handle: request_id.to_string().into(),
        };
        debug!("created ticket handle: {:?}", ticket.statement_handle);
        let mut bytes: Vec<u8> = Vec::new();
        if ticket.encode(&mut bytes).is_ok() {
            let info = FlightInfo::new()
                .try_with_schema(schema.as_arrow())
                .unwrap()
                .with_endpoint(FlightEndpoint::new().with_ticket(Ticket::new(bytes.clone())))
                .with_descriptor(FlightDescriptor::new_cmd(bytes));
            debug!("created flight info: {:?}", info);

            let mut guard = self
                .requests
                .lock()
                .map_err(|_| Status::internal("failed to acquire lock on requests"))?;
            guard.insert(request_id, logical_plan);

            Ok(Response::new(info))
        } else {
            error!("error encoding ticket");
            Err(Status::internal("error encoding ticket"))
        }
    }

    async fn create_flight_info(
        &self,
        query: String,
        request_id: Uuid,
        request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        debug!(
            "creating flight info for request id {request_id} with query: {:?}",
            query
        );
        let dialect = datafusion::sql::sqlparser::dialect::GenericDialect {};
        match DFParser::parse_sql_with_dialect(&query, &dialect) {
            Ok(statements) => {
                let statement = statements[0].clone();
                let start = std::time::Instant::now();

                let logical_plan = self
                    .execution
                    .statement_to_logical_plan(statement)
                    .await
                    .map_err(|e| Status::internal(e.to_string()))?;

                debug!("logical planning took: {:?}", start.elapsed());
                self.create_flight_info_for_logical_plan(logical_plan, request_id, request)
                    .await
            }
            Err(e) => {
                error!("error parsing SQL query: {:?}", e);
                Err(Status::internal("error parsing SQL query"))
            }
        }
    }

    async fn do_get_statement_handler(
        &self,
        request_id: String,
        ticket: TicketStatementQuery,
    ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status> {
        debug!("do_get_statement ticket: {:?}", ticket);
        self.do_get_common_handler(request_id).await
    }

    async fn do_get_fallback_handler(
        &self,
        request_id: String,
        message: Any,
    ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status> {
        debug!("do_get_fallback message: {:?}", message);
        self.do_get_common_handler(request_id).await
    }
}

#[tonic::async_trait]
impl FlightSqlService for FlightSqlServiceImpl {
    type FlightService = FlightSqlServiceImpl;

    async fn get_flight_info_catalogs(
        &self,
        _query: CommandGetCatalogs,
        request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        counter!("requests", "endpoint" => "get_flight_info_catalogs").increment(1);
        let start = Timestamp::now();
        let request_id = uuid::Uuid::new_v4();
        let query = "SELECT DISTINCT table_catalog FROM information_schema.tables".to_string();
        let res = self.create_flight_info(query, request_id, request).await;

        // TODO: Move recording to after response is sent to not impact response latency
        self.record_request(
            start,
            Some(request_id.to_string()),
            res.as_ref().err(),
            "/get_flight_info_catalogs".to_string(),
            "get_flight_info_catalogs_latency_ms",
        )
        .await;
        res
    }

    async fn get_flight_info_schemas(
        &self,
        command: CommandGetDbSchemas,
        request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        counter!("requests", "endpoint" => "get_flight_info_schemas").increment(1);
        let start = Timestamp::now();
        let request_id = uuid::Uuid::new_v4();
        let CommandGetDbSchemas {
            catalog,
            db_schema_filter_pattern,
        } = command;
        let query = match (catalog, db_schema_filter_pattern) {
            (Some(catalog), Some(filter)) => format!("SELECT DISTINCT table_catalog, table_schema FROM information_schema.tables WHERE table_catalog = '{catalog}' AND table_schema ILIKE '%{filter}%' ORDER BY table_catalog, table_schema"),
            (None, Some(filter)) => format!("SELECT DISTINCT table_catalog, table_schema FROM information_schema.tables WHERE table_schema ILIKE '%{filter}%' ORDER BY table_catalog, table_schema"),
            (Some(catalog), None) => format!("SELECT DISTINCT table_catalog, table_schema FROM information_schema.tables WHERE table_catalog = '{catalog}' ORDER BY table_catalog, table_schema"),
            (None, None) => "SELECT DISTINCT table_catalog, table_schema FROM information_schema.tables ORDER BY table_catalog, table_schema".to_string()
        };
        let res = self.create_flight_info(query, request_id, request).await;

        // TODO: Move recording to after response is sent to not impact response latency
        self.record_request(
            start,
            Some(request_id.to_string()),
            res.as_ref().err(),
            "/get_flight_info_schemas".to_string(),
            "get_flight_info_schemas_latency_ms",
        )
        .await;
        res
    }

    async fn get_flight_info_tables(
        &self,
        command: CommandGetTables,
        request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        counter!("requests", "endpoint" => "get_flight_info_tables").increment(1);
        let start = Timestamp::now();
        let CommandGetTables {
            catalog,
            db_schema_filter_pattern,
            table_name_filter_pattern,
            table_types,
            include_schema: _include_schema,
        } = command;
        let base_query = "SELECT * FROM information_schema.tables";

        let mut df = self
            .execution
            .session_ctx()
            .sql(base_query)
            .await
            .map_err(|e| Status::internal(e.to_string()))?;

        if let Some(catalog) = catalog {
            df = df
                .filter(col("table_catalog").eq(lit(catalog)))
                .map_err(|e| Status::internal(e.to_string()))?;
        };
        if let Some(schema_filter) = db_schema_filter_pattern {
            df = df
                .filter(col("table_schema").ilike(lit(format!("%{schema_filter}%"))))
                .map_err(|e| Status::internal(e.to_string()))?;
        };
        if let Some(table_filter) = table_name_filter_pattern {
            df = df
                .filter(col("table_name").ilike(lit(format!("%{table_filter}%"))))
                .map_err(|e| Status::internal(e.to_string()))?;
        };
        if !table_types.is_empty() {
            let table_exprs = table_types.iter().map(lit).collect();
            df = df
                .filter(col("table_type").in_list(table_exprs, false))
                .map_err(|e| Status::internal(e.to_string()))?;
        };
        df = df
            .sort(vec![
                col("table_catalog").sort(true, false),
                col("table_schema").sort(true, false),
                col("table_name").sort(true, false),
            ])
            .map_err(|e| Status::internal(e.to_string()))?;
        let logical_plan = df.into_unoptimized_plan();

        let request_id = uuid::Uuid::new_v4();
        let res = self
            .create_flight_info_for_logical_plan(logical_plan, request_id, request)
            .await;

        // TODO: Move recording to after response is sent to not impact response latency
        self.record_request(
            start,
            Some(request_id.to_string()),
            res.as_ref().err(),
            "/get_flight_info_tables".to_string(),
            "get_flight_info_tables_latency_ms",
        )
        .await;
        res
    }

    async fn get_flight_info_table_types(
        &self,
        _query: CommandGetTableTypes,
        request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        counter!("requests", "endpoint" => "get_flight_info_table_types").increment(1);
        let start = Timestamp::now();
        let request_id = uuid::Uuid::new_v4();
        let query = "SELECT DISTINCT table_type FROM information_schema.tables ORDER BY table_type"
            .to_string();
        let res = self.create_flight_info(query, request_id, request).await;

        // TODO: Move recording to after response is sent to not impact response latency
        self.record_request(
            start,
            Some(request_id.to_string()),
            res.as_ref().err(),
            "/get_flight_info_table_types".to_string(),
            "get_flight_info_table_types_latency_ms",
        )
        .await;
        res
    }

    async fn get_flight_info_sql_info(
        &self,
        _query: CommandGetSqlInfo,
        request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        counter!("requests", "endpoint" => "get_flight_info_sql_info").increment(1);
        let start = Timestamp::now();
        let request_id = uuid::Uuid::new_v4();

        // For now, return basic server info via a simple SQL query
        // TODO: Implement full SqlInfo support with DenseUnion schema
        let query = format!(
            "SELECT '{}' as server_name, '{}' as server_version, '57.0' as arrow_version, false as read_only",
            "datafusion-dft",
            env!("CARGO_PKG_VERSION")
        );

        let res = self.create_flight_info(query, request_id, request).await;

        // TODO: Move recording to after response is sent to not impact response latency
        self.record_request(
            start,
            Some(request_id.to_string()),
            res.as_ref().err(),
            "/get_flight_info_sql_info".to_string(),
            "get_flight_info_sql_info_latency_ms",
        )
        .await;
        res
    }

    async fn get_flight_info_xdbc_type_info(
        &self,
        query: CommandGetXdbcTypeInfo,
        request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        counter!("requests", "endpoint" => "get_flight_info_xdbc_type_info").increment(1);
        let start = Timestamp::now();
        let request_id = uuid::Uuid::new_v4();

        // Build query to return XDBC type info for DataFusion-supported types
        // If data_type filter is provided, we would filter to that type
        // For now, return all supported types
        let type_filter = if let Some(data_type) = query.data_type {
            format!(" WHERE data_type = {}", data_type)
        } else {
            String::new()
        };

        // Return basic type information for common Arrow/DataFusion types
        // This is a simplified version - a full implementation would include all XDBC type metadata
        let query = format!(
            "SELECT * FROM (VALUES \
                (-5, 'BIGINT', 19, NULL, NULL, NULL, 1, 0, 3, 0, 0, 0, 'BIGINT', -5, 0, 10, 0), \
                (4, 'INTEGER', 10, NULL, NULL, NULL, 1, 0, 3, 0, 0, 0, 'INTEGER', 4, 0, 10, 0), \
                (5, 'SMALLINT', 5, NULL, NULL, NULL, 1, 0, 3, 0, 0, 0, 'SMALLINT', 5, 0, 10, 0), \
                (-6, 'TINYINT', 3, NULL, NULL, NULL, 1, 0, 3, 0, 0, 0, 'TINYINT', -6, 0, 10, 0), \
                (8, 'DOUBLE', 15, NULL, NULL, NULL, 1, 0, 3, 0, 0, 0, 'DOUBLE PRECISION', 8, 0, 2, 0), \
                (7, 'REAL', 7, NULL, NULL, NULL, 1, 0, 3, 0, 0, 0, 'REAL', 7, 0, 2, 0), \
                (12, 'VARCHAR', 2147483647, '''', '''', 'length', 1, 1, 3, 0, 0, 0, 'VARCHAR', 12, 0, 0, 0), \
                (91, 'DATE', 10, '''', '''', NULL, 1, 0, 3, 0, 0, 0, 'DATE', 91, 0, 0, 0), \
                (93, 'TIMESTAMP', 23, '''', '''', NULL, 1, 0, 3, 0, 0, 0, 'TIMESTAMP', 93, 3, 0, 0), \
                (-7, 'BOOLEAN', 1, NULL, NULL, NULL, 1, 0, 3, 0, 0, 0, 'BOOLEAN', -7, 0, 0, 0), \
                (-2, 'BINARY', 2147483647, '''', '''', 'length', 1, 0, 3, 0, 0, 0, 'BINARY', -2, 0, 0, 0), \
                (2, 'DECIMAL', 38, NULL, NULL, 'precision,scale', 1, 0, 3, 0, 0, 0, 'DECIMAL', 2, 0, 10, 0) \
            ) AS types(\
                type_name_num, type_name_str, column_size, literal_prefix, literal_suffix, create_params, \
                nullable, case_sensitive, searchable, unsigned_attribute, fixed_prec_scale, auto_increment, \
                local_type_name, data_type, minimum_scale, maximum_scale, sql_datetime_sub\
            ){type_filter}"
        );

        let res = self.create_flight_info(query, request_id, request).await;

        // TODO: Move recording to after response is sent to not impact response latency
        self.record_request(
            start,
            Some(request_id.to_string()),
            res.as_ref().err(),
            "/get_flight_info_xdbc_type_info".to_string(),
            "get_flight_info_xdbc_type_info_latency_ms",
        )
        .await;
        res
    }

    async fn get_flight_info_statement(
        &self,
        query: CommandStatementQuery,
        request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        counter!("requests", "endpoint" => "get_flight_info").increment(1);
        let start = Timestamp::now();
        let CommandStatementQuery { query, .. } = query;
        let request_id = uuid::Uuid::new_v4();
        let res = self
            .create_flight_info(query.clone(), request_id, request)
            .await;

        // TODO: Move recording to after response is sent to not impact response latency
        self.record_request(
            start,
            Some(request_id.to_string()),
            res.as_ref().err(),
            "/get_flight_info_statement".to_string(),
            "get_flight_info_statement_latency_ms",
        )
        .await;
        res
    }

    async fn do_get_statement(
        &self,
        ticket: TicketStatementQuery,
        request: Request<Ticket>,
    ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status> {
        counter!("requests", "endpoint" => "do_get_statement").increment(1);
        let start = Timestamp::now();
        let request_id =
            try_request_id_from_request(request).map_err(|e| Status::internal(e.to_string()))?;
        debug!("do_get_statement for request_id: {}", &request_id);
        let res = self
            .do_get_statement_handler(request_id.clone(), ticket)
            .await;

        // TODO: Move recording to after response is sent to not impact response latency
        self.record_request(
            start,
            Some(request_id),
            res.as_ref().err(),
            "/do_get_statement".to_string(),
            "do_get_statement_latency_ms",
        )
        .await;
        res
    }

    async fn do_get_fallback(
        &self,
        request: Request<Ticket>,
        message: Any,
    ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status> {
        counter!("requests", "endpoint" => "do_get_fallback").increment(1);
        let start = Timestamp::now();
        let request_id =
            try_request_id_from_request(request).map_err(|e| Status::internal(e.to_string()))?;
        debug!("do_get_fallback for request_id: {}", &request_id);
        let res = self
            .do_get_fallback_handler(request_id.clone(), message)
            .await;

        // TODO: Move recording to after response is sent to not impact response latency
        self.record_request(
            start,
            Some(request_id),
            res.as_ref().err(),
            "/do_get_fallback".to_string(),
            "do_get_fallback_latency_ms",
        )
        .await;
        res
    }

    async fn do_action_create_prepared_statement(
        &self,
        query: ActionCreatePreparedStatementRequest,
        _request: Request<Action>,
    ) -> Result<ActionCreatePreparedStatementResult, Status> {
        counter!("requests", "endpoint" => "do_action_create_prepared_statement").increment(1);
        let start = Timestamp::now();
        let request_id = Uuid::new_v4();

        debug!("Creating prepared statement for SQL: {}", query.query);

        // Parse and create logical plan
        let dialect = datafusion::sql::sqlparser::dialect::GenericDialect {};
        let statements = DFParser::parse_sql_with_dialect(&query.query, &dialect)
            .map_err(|e| Status::internal(format!("Failed to parse SQL: {}", e)))?;

        if statements.is_empty() {
            return Err(Status::invalid_argument("No SQL statement provided"));
        }

        let statement = statements[0].clone();
        let logical_plan = self
            .execution
            .statement_to_logical_plan(statement)
            .await
            .map_err(|e| Status::internal(format!("Failed to create logical plan: {}", e)))?;

        // Extract schemas
        let dataset_schema = logical_plan.schema().as_arrow().clone();
        // For now, we don't support parameterized queries, so parameter_schema is None
        let parameter_schema = None;

        // Store the prepared statement
        let handle = PreparedStatementHandle {
            plan: logical_plan,
            parameter_schema,
            dataset_schema: Arc::new(dataset_schema.clone()),
            created_at: Timestamp::now(),
        };

        {
            let mut guard = self
                .prepared_statements
                .lock()
                .map_err(|_| Status::internal("Failed to acquire lock on prepared statements"))?;
            guard.insert(request_id, handle);

            // Update active prepared statements gauge
            metrics::gauge!("prepared_statements_active").set(guard.len() as f64);
        }

        // Serialize dataset schema to IPC format
        let options = IpcWriteOptions::default();
        let IpcMessage(dataset_schema_bytes) = SchemaAsIpc::new(&dataset_schema, &options)
            .try_into()
            .map_err(|e: ArrowError| {
                Status::internal(format!("Failed to serialize schema: {}", e))
            })?;

        // Build response
        let result = ActionCreatePreparedStatementResult {
            prepared_statement_handle: Bytes::from(request_id.as_bytes().to_vec()),
            dataset_schema: dataset_schema_bytes,
            parameter_schema: Bytes::new(), // No parameters supported yet
        };

        // Record metrics
        let duration = Timestamp::now() - start;
        histogram!("do_action_create_prepared_statement_latency_ms")
            .record(duration.get_milliseconds() as f64);

        let ctx = self.execution.session_ctx();
        let req = ObservabilityRequestDetails {
            request_id: Some(request_id.to_string()),
            path: "/do_action/create_prepared_statement".to_string(),
            sql: Some(query.query),
            start_ms: start.as_millisecond(),
            duration_ms: duration.get_milliseconds(),
            rows: None,
            status: 0,
        };
        if let Err(e) = self
            .execution
            .observability()
            .try_record_request(ctx, req)
            .await
        {
            error!("Error recording request: {}", e);
        }

        Ok(result)
    }

    async fn do_action_close_prepared_statement(
        &self,
        query: ActionClosePreparedStatementRequest,
        _request: Request<Action>,
    ) -> Result<(), Status> {
        counter!("requests", "endpoint" => "do_action_close_prepared_statement").increment(1);
        let start = Timestamp::now();

        let handle_bytes = query.prepared_statement_handle.to_vec();
        let request_id = Uuid::from_slice(&handle_bytes).map_err(|e| {
            Status::invalid_argument(format!("Invalid prepared statement handle: {}", e))
        })?;

        debug!("Closing prepared statement: {}", request_id);

        // Remove from storage
        {
            let mut guard = self
                .prepared_statements
                .lock()
                .map_err(|_| Status::internal("Failed to acquire lock on prepared statements"))?;

            if guard.remove(&request_id).is_none() {
                return Err(Status::not_found(format!(
                    "Prepared statement not found: {}",
                    request_id
                )));
            }

            // Update active prepared statements gauge
            metrics::gauge!("prepared_statements_active").set(guard.len() as f64);
        }

        // Record metrics
        let duration = Timestamp::now() - start;
        histogram!("do_action_close_prepared_statement_latency_ms")
            .record(duration.get_milliseconds() as f64);

        let ctx = self.execution.session_ctx();
        let req = ObservabilityRequestDetails {
            request_id: Some(request_id.to_string()),
            path: "/do_action/close_prepared_statement".to_string(),
            sql: None,
            start_ms: start.as_millisecond(),
            duration_ms: duration.get_milliseconds(),
            rows: None,
            status: 0,
        };
        if let Err(e) = self
            .execution
            .observability()
            .try_record_request(ctx, req)
            .await
        {
            error!("Error recording request: {}", e);
        }

        Ok(())
    }

    async fn get_flight_info_prepared_statement(
        &self,
        query: CommandPreparedStatementQuery,
        request: Request<FlightDescriptor>,
    ) -> Result<Response<FlightInfo>, Status> {
        counter!("requests", "endpoint" => "get_flight_info_prepared_statement").increment(1);
        let start = Timestamp::now();

        // Extract the prepared statement handle
        let handle_bytes = query.prepared_statement_handle.to_vec();
        let handle_uuid = Uuid::from_slice(&handle_bytes).map_err(|e| {
            Status::invalid_argument(format!("Invalid prepared statement handle: {}", e))
        })?;

        debug!(
            "Getting flight info for prepared statement: {}",
            handle_uuid
        );

        // Look up the prepared statement
        let prepared_stmt = {
            let guard = self
                .prepared_statements
                .lock()
                .map_err(|_| Status::internal("Failed to acquire lock on prepared statements"))?;

            guard.get(&handle_uuid).cloned().ok_or_else(|| {
                Status::not_found(format!("Prepared statement not found: {}", handle_uuid))
            })?
        };

        // Create a new request ID for this execution
        let request_id = Uuid::new_v4();

        // Create FlightInfo from the stored logical plan
        let res = self
            .create_flight_info_for_logical_plan(prepared_stmt.plan, request_id, request)
            .await;

        // Record observability
        let duration = Timestamp::now() - start;
        histogram!("get_flight_info_prepared_statement_latency_ms")
            .record(duration.get_milliseconds() as f64);

        let ctx = self.execution.session_ctx();
        let req = ObservabilityRequestDetails {
            request_id: Some(request_id.to_string()),
            path: "/get_flight_info_prepared_statement".to_string(),
            sql: None, // Don't log SQL for prepared statements
            start_ms: start.as_millisecond(),
            duration_ms: duration.get_milliseconds(),
            rows: None,
            status: if res.is_ok() {
                0
            } else {
                tonic::Code::Internal as u16
            },
        };
        if let Err(e) = self
            .execution
            .observability()
            .try_record_request(ctx, req)
            .await
        {
            error!("Error recording request: {}", e);
        }

        res
    }

    async fn do_get_prepared_statement(
        &self,
        query: CommandPreparedStatementQuery,
        request: Request<Ticket>,
    ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status> {
        counter!("requests", "endpoint" => "do_get_prepared_statement").increment(1);
        let start = Timestamp::now();

        // Extract the request ID from the ticket
        let request_id =
            try_request_id_from_request(request).map_err(|e| Status::internal(e.to_string()))?;

        debug!("do_get_prepared_statement for request_id: {}", &request_id);

        // The request_id in the ticket should correspond to a logical plan in the requests HashMap
        // that was created by get_flight_info_prepared_statement
        let res = self
            .do_get_statement_handler(
                request_id.clone(),
                TicketStatementQuery {
                    statement_handle: query.prepared_statement_handle,
                },
            )
            .await;

        // Record observability
        let duration = Timestamp::now() - start;
        histogram!("do_get_prepared_statement_latency_ms")
            .record(duration.get_milliseconds() as f64);

        let ctx = self.execution.session_ctx();
        let req = ObservabilityRequestDetails {
            request_id: Some(request_id),
            path: "/do_get_prepared_statement".to_string(),
            sql: None,
            start_ms: start.as_millisecond(),
            duration_ms: duration.get_milliseconds(),
            rows: None,
            status: if res.is_ok() {
                0
            } else {
                tonic::Code::Internal as u16
            },
        };
        if let Err(e) = self
            .execution
            .observability()
            .try_record_request(ctx, req)
            .await
        {
            error!("Error recording request: {}", e);
        }

        res
    }

    async fn register_sql_info(&self, _id: i32, _result: &SqlInfo) {}
}

fn try_request_id_from_request(request: Request<Ticket>) -> Result<String> {
    let ticket = request.into_inner();
    let bytes = ticket.ticket.to_vec();

    let request_id = String::from_utf8(
        TicketStatementQuery::decode(bytes.as_slice())?
            .statement_handle
            .to_vec(),
    )?;
    Ok(request_id)
}