hyperdb-api-core 0.1.0

Internal implementation details for hyperdb-api. Not a stable API; use hyperdb-api instead.
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! gRPC client for Hyper database.
//!
//! This module provides the [`GrpcClient`] struct for executing queries against
//! Hyper servers via gRPC.

use std::sync::Arc;

use tonic::transport::{Channel, Endpoint};
use tracing::{debug, info, warn};

use crate::client::error::{Error, ErrorKind, Result};

use super::config::GrpcConfig;
use super::error::from_grpc_status;
use super::executor::{GrpcChunkStream, GrpcQueryExecutor};
use super::params::{ParameterStyle, QueryParameters};
use super::proto::hyper_service::query_param::TransferMode;
use super::proto::{
    AttachedDatabase, CancelQueryParam, HyperServiceClient, OutputFormat, QueryParam,
};
use super::result::GrpcQueryResult;

/// Async gRPC client for Hyper database.
///
/// `GrpcClient` provides query-only access to Hyper databases via gRPC.
/// Results are returned in Apache Arrow IPC format.
///
/// gRPC transport is always available - no feature flags required.
///
/// # Limitations
///
/// The gRPC interface is **read-only**:
/// - Only SELECT queries are supported
/// - No INSERT, UPDATE, DELETE, or DDL operations
/// - No COPY protocol for bulk data insertion
///
/// For write operations, use the standard TCP [`Client`](crate::client::Client).
///
/// # Example
///
/// ```no_run
/// use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let config = GrpcConfig::new("http://localhost:7484")
///         .database("my_database.hyper");
///
///     let mut client = GrpcClient::connect(config).await?;
///
///     // Execute a query
///     let result = client.execute_query("SELECT * FROM users").await?;
///     let arrow_data = result.arrow_data();
///
///     // Process arrow_data with arrow crate...
///
///     client.close().await?;
///     Ok(())
/// }
/// ```
#[derive(Debug)]
pub struct GrpcClient {
    /// The underlying gRPC channel
    channel: Channel,
    /// Client configuration
    config: GrpcConfig,
}

impl GrpcClient {
    /// Connects to a Hyper server via gRPC.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = GrpcConfig::new("http://localhost:7484")
    ///     .database("test.hyper");
    ///
    /// let client = GrpcClient::connect(config).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// - Returns [`ErrorKind::Config`] if `config.endpoint` is not a
    ///   well-formed URI, or if TLS configuration fails.
    /// - Returns [`ErrorKind::Connection`] if the gRPC transport
    ///   cannot establish a channel to the endpoint.
    pub async fn connect(config: GrpcConfig) -> Result<Self> {
        info!(endpoint = %config.endpoint, "Connecting to Hyper via gRPC");

        let endpoint = Endpoint::from_shared(config.endpoint.clone())
            .map_err(|e| Error::new(ErrorKind::Config, format!("Invalid gRPC endpoint: {e}")))?;

        // Configure timeouts
        let endpoint = endpoint
            .connect_timeout(config.connect_timeout)
            .timeout(config.request_timeout);

        // Configure TLS if needed
        let endpoint = if config.use_tls {
            // Use system root certificates for TLS validation
            let tls_config = tonic::transport::ClientTlsConfig::new().with_enabled_roots();

            endpoint.tls_config(tls_config).map_err(|e| {
                Error::new(ErrorKind::Config, format!("TLS configuration error: {e}"))
            })?
        } else {
            endpoint
        };

        // Connect
        let channel = endpoint.connect().await.map_err(|e| {
            debug!("gRPC connection error details: {:?}", e);
            Error::new(
                ErrorKind::Connection,
                format!("Failed to connect to gRPC endpoint: {e} (details: {e:?})"),
            )
        })?;

        debug!("gRPC channel established");

        Ok(GrpcClient { channel, config })
    }

    /// Returns the underlying gRPC channel.
    ///
    /// This can be used for advanced use cases like channel cloning or
    /// direct stub access.
    #[must_use]
    pub fn channel(&self) -> &Channel {
        &self.channel
    }

    /// Returns the client configuration.
    pub fn config(&self) -> &GrpcConfig {
        &self.config
    }

    /// Executes a SQL query and returns the result.
    ///
    /// Results are returned in Apache Arrow IPC format. Use the `arrow_data()`
    /// method on the result to get the raw Arrow bytes.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = GrpcConfig::new("http://localhost:7484");
    /// # let mut client = GrpcClient::connect(config).await?;
    /// let result = client.execute_query("SELECT * FROM users LIMIT 10").await?;
    /// let arrow_bytes = result.arrow_data();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The query syntax is invalid
    /// - The referenced tables/columns don't exist
    /// - A non-SELECT query is executed (gRPC is read-only)
    /// - The connection is lost
    pub async fn execute_query(&mut self, sql: &str) -> Result<GrpcQueryResult> {
        self.execute_query_with_options(sql, OutputFormat::ArrowIpc, self.config.transfer_mode)
            .await
    }

    /// Executes a query and returns raw Arrow IPC bytes.
    ///
    /// This is a convenience method that extracts the Arrow data from the result.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = GrpcConfig::new("http://localhost:7484");
    /// # let mut client = GrpcClient::connect(config).await?;
    /// let arrow_bytes = client.execute_query_to_arrow("SELECT * FROM users").await?;
    /// // Parse with arrow crate...
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::execute_query`] (invalid SQL,
    /// missing tables/columns, non-SELECT mutation attempts, or
    /// connection loss).
    pub async fn execute_query_to_arrow(&mut self, sql: &str) -> Result<bytes::Bytes> {
        let result = self.execute_query(sql).await?;
        Ok(result.into_arrow_data())
    }

    /// Executes a parameterized SQL query.
    ///
    /// This provides SQL injection prevention and type safety by separating
    /// the query from its parameters.
    ///
    /// # Arguments
    ///
    /// * `sql` - SQL query with parameter placeholders
    /// * `params` - Query parameters (JSON or Arrow encoded)
    /// * `style` - Parameter style used in the query
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig, QueryParameters, ParameterStyle};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = GrpcConfig::new("http://localhost:7484");
    /// # let mut client = GrpcClient::connect(config).await?;
    /// // Dollar-numbered parameters (mixed types use from_json_value)
    /// let params = QueryParameters::from_json_value(&serde_json::json!([42, "Alice"]))?;
    /// let result = client.execute_query_with_params(
    ///     "SELECT * FROM users WHERE id = $1 AND name = $2",
    ///     params,
    ///     ParameterStyle::DollarNumbered,
    /// ).await?;
    ///
    /// // Named parameters
    /// let params = QueryParameters::json_named()
    ///     .add("min_age", &18)?
    ///     .build();
    /// let result = client.execute_query_with_params(
    ///     "SELECT * FROM users WHERE age >= :min_age",
    ///     params,
    ///     ParameterStyle::Named,
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::execute_query`], plus any
    /// parameter-related error reported by the server (unknown
    /// placeholder, type coercion failure, shape mismatch between the
    /// SQL placeholders and the supplied parameter set).
    pub async fn execute_query_with_params(
        &mut self,
        sql: &str,
        params: QueryParameters,
        style: ParameterStyle,
    ) -> Result<GrpcQueryResult> {
        self.execute_query_with_params_and_options(
            sql,
            params,
            style,
            OutputFormat::ArrowIpc,
            self.config.transfer_mode,
        )
        .await
    }

    /// Executes a parameterized query and returns raw Arrow IPC bytes.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig, QueryParameters, ParameterStyle};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = GrpcConfig::new("http://localhost:7484");
    /// # let mut client = GrpcClient::connect(config).await?;
    /// let params = QueryParameters::json_positional(&[&42i64])?;
    /// let arrow_bytes = client.execute_query_with_params_to_arrow(
    ///     "SELECT * FROM users WHERE id = $1",
    ///     params,
    ///     ParameterStyle::DollarNumbered,
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::execute_query_with_params`].
    pub async fn execute_query_with_params_to_arrow(
        &mut self,
        sql: &str,
        params: QueryParameters,
        style: ParameterStyle,
    ) -> Result<bytes::Bytes> {
        let result = self.execute_query_with_params(sql, params, style).await?;
        Ok(result.into_arrow_data())
    }

    /// Executes a parameterized query with specific options.
    ///
    /// This allows full control over the output format and transfer mode.
    ///
    /// # Errors
    ///
    /// - Returns [`ErrorKind::Protocol`] if the server returns no
    ///   result chunks and does not signal completion.
    /// - Propagates any error from the underlying
    ///   `GrpcQueryExecutor` — auth failure, transport error, or
    ///   server-side SQL error surfaced as [`tonic::Status`].
    pub async fn execute_query_with_params_and_options(
        &mut self,
        sql: &str,
        params: QueryParameters,
        style: ParameterStyle,
        output_format: OutputFormat,
        transfer_mode: TransferMode,
    ) -> Result<GrpcQueryResult> {
        debug!(
            sql = %sql,
            param_style = ?style,
            format = ?output_format,
            mode = ?transfer_mode,
            "Executing parameterized query"
        );

        // Build the query parameter
        let query_param = QueryParam {
            query: sql.to_string(),
            databases: self.build_attached_databases(),
            output_format: output_format.into(),
            settings: self.config.settings.clone(),
            transfer_mode: transfer_mode.into(),
            param_style: i32::from(style),
            parameters: Some(params.into_proto()),
            result_range: None,
            query_row_limit: None,
        };

        // Build headers for authentication and routing
        let headers = self.build_headers();

        // Create executor with configured message size limits
        let client = HyperServiceClient::new(self.channel.clone())
            .max_decoding_message_size(self.config.max_decoding_message_size)
            .max_encoding_message_size(self.config.max_encoding_message_size);
        let mut executor = GrpcQueryExecutor::new(client, headers, transfer_mode);

        // Execute the query
        executor.execute(query_param).await?;

        // Collect all result chunks
        let mut final_result = GrpcQueryResult::default();

        loop {
            if let Some(mut partial_result) = executor.next_result().await? {
                // Merge chunks
                while let Some(chunk) = partial_result.take_chunk() {
                    final_result.chunks.push_back(chunk);
                }
                // Copy metadata from last result
                if partial_result.query_id.is_some() {
                    final_result.query_id = partial_result.query_id;
                }
                if partial_result.schema.is_some() {
                    final_result.schema = partial_result.schema;
                }
                if partial_result.rows_affected.is_some() {
                    final_result.rows_affected = partial_result.rows_affected;
                }
                // Check if complete
                if partial_result.is_complete {
                    final_result.is_complete = true;
                    break;
                }
            } else {
                // No more results
                final_result.is_complete = true;
                break;
            }
        }

        if final_result.chunks.is_empty() && !final_result.is_complete {
            return Err(Error::new(ErrorKind::Protocol, "No result from query"));
        }

        Ok(final_result)
    }

    /// Executes a query with specific options.
    ///
    /// This allows control over the output format and transfer mode.
    ///
    /// # Errors
    ///
    /// - Returns [`ErrorKind::Protocol`] if the server returns no
    ///   result chunks and does not signal completion.
    /// - Propagates any error from the underlying
    ///   `GrpcQueryExecutor` — auth failure, transport error, or
    ///   server-side SQL error surfaced as [`tonic::Status`].
    pub async fn execute_query_with_options(
        &mut self,
        sql: &str,
        output_format: OutputFormat,
        transfer_mode: TransferMode,
    ) -> Result<GrpcQueryResult> {
        debug!(sql = %sql, format = ?output_format, mode = ?transfer_mode, "Executing query");

        // Build the query parameter
        let query_param = QueryParam {
            query: sql.to_string(),
            databases: self.build_attached_databases(),
            output_format: output_format.into(),
            settings: self.config.settings.clone(),
            transfer_mode: transfer_mode.into(),
            param_style: 0, // Default
            parameters: None,
            result_range: None,
            query_row_limit: None,
        };

        // Build headers for authentication and routing
        let headers = self.build_headers();

        // Create executor with configured message size limits
        let client = HyperServiceClient::new(self.channel.clone())
            .max_decoding_message_size(self.config.max_decoding_message_size)
            .max_encoding_message_size(self.config.max_encoding_message_size);
        let mut executor = GrpcQueryExecutor::new(client, headers, transfer_mode);

        // Execute the query
        executor.execute(query_param).await?;

        // Collect all result chunks
        let mut final_result = GrpcQueryResult::default();

        loop {
            if let Some(mut partial_result) = executor.next_result().await? {
                // Merge chunks
                while let Some(chunk) = partial_result.take_chunk() {
                    final_result.chunks.push_back(chunk);
                }
                // Copy metadata from last result
                if partial_result.query_id.is_some() {
                    final_result.query_id = partial_result.query_id;
                }
                if partial_result.schema.is_some() {
                    final_result.schema = partial_result.schema;
                }
                if partial_result.rows_affected.is_some() {
                    final_result.rows_affected = partial_result.rows_affected;
                }
                // Check if complete
                if partial_result.is_complete {
                    final_result.is_complete = true;
                    break;
                }
            } else {
                // No more results
                final_result.is_complete = true;
                break;
            }
        }

        if final_result.chunks.is_empty() && !final_result.is_complete {
            return Err(Error::new(ErrorKind::Protocol, "No result from query"));
        }

        Ok(final_result)
    }

    /// Executes a query and returns a streaming chunk producer.
    ///
    /// Unlike [`execute_query`](Self::execute_query), which drains every
    /// result chunk into a single [`GrpcQueryResult`] before returning, this
    /// method yields chunks lazily: each call to
    /// [`GrpcChunkStream::next_chunk`] pulls just enough from the HTTP/2
    /// stream to produce one Arrow IPC byte chunk. For very large result
    /// sets (hundreds of MB to GB) this keeps client memory bounded by a
    /// single gRPC message (capped at the tonic
    /// `max_decoding_message_size`, default 64 MB) rather than growing to
    /// the full result size.
    ///
    /// Pair this with
    /// [`hyperdb_api::ArrowRowset::from_stream`][arrow_rowset_from_stream] to
    /// decode batches incrementally and keep peak memory constant regardless
    /// of total row count.
    ///
    /// [arrow_rowset_from_stream]: https://docs.rs/hyperdb-api/latest/hyperdb_api/struct.ArrowRowset.html#method.from_stream
    ///
    /// # Errors
    ///
    /// Same failure modes as
    /// [`Self::execute_query_stream_with_options`] — invalid SQL,
    /// auth failure, transport error, etc.
    pub async fn execute_query_stream(&mut self, sql: &str) -> Result<GrpcChunkStream> {
        self.execute_query_stream_with_options(
            sql,
            OutputFormat::ArrowIpc,
            self.config.transfer_mode,
        )
        .await
    }

    /// Streaming variant of [`execute_query_with_options`](Self::execute_query_with_options).
    ///
    /// # Errors
    ///
    /// Propagates any error from the initial
    /// `GrpcQueryExecutor::execute` call — server-side SQL error,
    /// auth failure, or transport-level gRPC error.
    pub async fn execute_query_stream_with_options(
        &mut self,
        sql: &str,
        output_format: OutputFormat,
        transfer_mode: TransferMode,
    ) -> Result<GrpcChunkStream> {
        debug!(sql = %sql, format = ?output_format, mode = ?transfer_mode, "Executing streaming query");

        let query_param = QueryParam {
            query: sql.to_string(),
            databases: self.build_attached_databases(),
            output_format: output_format.into(),
            settings: self.config.settings.clone(),
            transfer_mode: transfer_mode.into(),
            param_style: 0,
            parameters: None,
            result_range: None,
            query_row_limit: None,
        };

        let headers = self.build_headers();

        let client = HyperServiceClient::new(self.channel.clone())
            .max_decoding_message_size(self.config.max_decoding_message_size)
            .max_encoding_message_size(self.config.max_encoding_message_size);
        let mut executor = GrpcQueryExecutor::new(client, headers, transfer_mode);

        executor.execute(query_param).await?;

        Ok(GrpcChunkStream::new(executor))
    }

    /// Cancels an in-flight gRPC query by its `query_id`.
    ///
    /// This is the gRPC analogue of the PG wire `CancelRequest` packet: it
    /// tells the server to stop executing a previously-started query. Unlike
    /// PG wire (where the cancel travels on a *fresh* connection), gRPC
    /// cancels travel as a regular RPC multiplexed over the existing HTTP/2
    /// channel — that's why this call shares `self.channel` with normal
    /// query traffic.
    ///
    /// # When do you have a `query_id`?
    ///
    /// The server assigns a `query_id` for queries started in
    /// [`TransferMode::Async`](super::proto::hyper_service::query_param::TransferMode)
    /// (long-running queries that the client polls). Grab it from
    /// [`GrpcQueryResult::query_id`](super::result::GrpcQueryResult::query_id)
    /// after `execute_query_with_options(..., TransferMode::Async)` returns.
    /// SYNC-mode queries typically complete before the client needs a
    /// cancel — for those, just drop the in-flight future.
    ///
    /// # Query-id lifecycle
    ///
    /// Query ids are stable for the lifetime of a query and are
    /// server-assigned — a given id is never silently re-used for a
    /// different query (Hyper generates them as UUID-like opaque tokens,
    /// not sequential counters). The only race a caller needs to
    /// consider is between obtaining the id and calling `cancel_query`:
    ///
    /// - If the query is still running, the cancel lands and the server
    ///   aborts it.
    /// - If the query has already completed normally between "obtain id"
    ///   and "cancel", the server sees a cancel for an unknown /
    ///   completed query and handles it gracefully (the exact shape
    ///   depends on server build — see the tests in
    ///   `hyperdb-api/tests/grpc_cancel_tests.rs` for details). Either way
    ///   the channel stays healthy.
    ///
    /// There is no scenario where a stale id causes a cancel to target
    /// the *wrong* query, because ids are not reassigned.
    ///
    /// # Errors
    ///
    /// Propagates transport-level errors. A successful cancel returns
    /// `Ok(())` even if the query had already completed on the server;
    /// cancellation is best-effort by design.
    ///
    /// # Relation to the [`Cancellable`](crate::client::cancel::Cancellable) trait
    ///
    /// This is the **fallible user-facing cancel API**: it returns a
    /// `Result<()>` so explicit callers can observe transport-level
    /// failures and react accordingly.
    ///
    /// It is *not* an implementation of the
    /// [`Cancellable`](crate::client::cancel::Cancellable) trait — and cannot
    /// be, because `Cancellable::cancel(&self)` takes no arguments while
    /// gRPC cancels need a per-query `query_id`. A `GrpcClient` can have
    /// many concurrent queries in flight; there is no single "the"
    /// query on it the way there is on a PG wire connection. A future
    /// gRPC streaming result type (when one is introduced) would carry
    /// its `query_id` in a dedicated handle like
    /// `GrpcCancelHandle { client, query_id }`, and *that* handle
    /// would `impl Cancellable` by wrapping this method and swallowing
    /// errors — same shape as
    /// [`impl Cancellable for Client`](crate::client::cancel::Cancellable).
    /// See the [`Cancellable`](crate::client::cancel::Cancellable) trait docs
    /// for the full wrapper pattern.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig, OutputFormat, TransferMode};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = GrpcConfig::new("http://localhost:7484");
    /// # let mut client = GrpcClient::connect(config).await?;
    /// let result = client
    ///     .execute_query_with_options(
    ///         "SELECT * FROM very_large_table",
    ///         OutputFormat::ArrowIpc,
    ///         TransferMode::Async,
    ///     )
    ///     .await?;
    ///
    /// if let Some(query_id) = result.query_id() {
    ///     // Some time later, decide to abort:
    ///     client.cancel_query(query_id).await?;
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn cancel_query(&mut self, query_id: &str) -> Result<()> {
        debug!(query_id = %query_id, "Cancelling gRPC query");

        let param = CancelQueryParam {
            query_id: query_id.to_string(),
        };
        let mut request = tonic::Request::new(param);

        // Apply the client's standard headers (database routing, custom
        // headers). Matches what the query path does so that any server-side
        // routing based on headers lands the cancel on the same backend as
        // the query it's trying to cancel.
        //
        // Header parse failures are logged at `warn!` and then skipped —
        // the cancel goes out without that particular header rather than
        // failing the whole operation. A missing custom header is strictly
        // better than a cancel we never send. The warn! is the only
        // operational signal that routing-critical headers (e.g. the
        // database selector) were dropped, so don't silence it.
        for (key, value) in self.build_headers() {
            match (
                key.parse::<tonic::metadata::MetadataKey<_>>(),
                value.parse(),
            ) {
                (Ok(k), Ok(v)) => {
                    request.metadata_mut().insert(k, v);
                }
                (key_res, value_res) => {
                    warn!(
                        target: "hyperdb_api_core::client",
                        query_id = %query_id,
                        header_key = %key,
                        key_parse_ok = key_res.is_ok(),
                        value_parse_ok = value_res.is_ok(),
                        "cancel: header parse failed, dropping header from cancel request",
                    );
                }
            }
        }
        // Also set the canonical x-hyperdb-query-id metadata — some server
        // deployments route cancels based on this header rather than the
        // payload body.
        match query_id.parse() {
            Ok(value) => {
                request.metadata_mut().insert("x-hyperdb-query-id", value);
            }
            Err(e) => {
                warn!(
                    target: "hyperdb_api_core::client",
                    query_id = %query_id,
                    error = %e,
                    "cancel: x-hyperdb-query-id header parse failed; \
                     cancel routing may fall back to payload-based lookup",
                );
            }
        }

        let mut client = HyperServiceClient::new(self.channel.clone())
            .max_decoding_message_size(self.config.max_decoding_message_size)
            .max_encoding_message_size(self.config.max_encoding_message_size);
        client
            .cancel_query(request)
            .await
            .map_err(from_grpc_status)?;

        info!(query_id = %query_id, "gRPC query cancelled");
        Ok(())
    }

    #[expect(
        clippy::unused_async,
        reason = "async fn retained for API symmetry; callers await regardless of whether the current body is synchronous"
    )]
    /// Closes the gRPC connection.
    ///
    /// This is a no-op as tonic channels are reference-counted and will be
    /// closed when the last reference is dropped.
    ///
    /// # Errors
    ///
    /// Currently infallible — always returns `Ok(())`. The `Result`
    /// return type is preserved for API symmetry with
    /// [`GrpcClientSync::close`] and for forward compatibility if
    /// future tonic channels expose a fallible shutdown.
    pub async fn close(self) -> Result<()> {
        debug!("Closing gRPC connection");
        // Channel is dropped automatically
        Ok(())
    }

    /// Builds the attached databases list from configuration.
    fn build_attached_databases(&self) -> Vec<AttachedDatabase> {
        if let Some(db_path) = &self.config.database {
            debug!(db_path = %db_path, "Attaching database for query");
            // Check if it's a JSON array (multiple databases)
            if db_path.starts_with('[') {
                // Parse JSON - for now just use as single database
                // TODO: Implement proper JSON parsing for multiple databases
                vec![AttachedDatabase {
                    path: db_path.clone(),
                    alias: String::new(), // Empty alias means use default
                }]
            } else {
                vec![AttachedDatabase {
                    path: db_path.clone(),
                    alias: String::new(), // Empty alias means use default
                }]
            }
        } else {
            debug!("No database configured on gRPC client — query will run without attachment");
            vec![]
        }
    }

    /// Builds headers for gRPC requests.
    fn build_headers(&self) -> Vec<(String, String)> {
        let mut headers: Vec<(String, String)> = self
            .config
            .headers
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();

        // Add database header if configured
        if let Some(ref db) = self.config.database {
            headers.push(("x-hyper-database".to_string(), db.clone()));
        }

        headers
    }
}

/// Synchronous wrapper around [`GrpcClient`].
///
/// This provides a blocking API by creating a Tokio runtime internally.
/// For better performance in async contexts, use [`GrpcClient`] directly.
///
/// # Example
///
/// ```no_run
/// use hyperdb_api_core::client::grpc::{GrpcClientSync, GrpcConfig};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = GrpcConfig::new("http://localhost:7484")
///     .database("test.hyper");
///
/// let mut client = GrpcClientSync::connect(config)?;
/// let result = client.execute_query("SELECT * FROM users")?;
/// let arrow_bytes = result.arrow_data();
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct GrpcClientSync {
    /// The async client
    inner: GrpcClient,
    /// Tokio runtime for blocking operations.
    ///
    /// Wrapped in `Arc` so streaming chunk producers
    /// ([`GrpcChunkStreamSync`]) can share the same runtime without having to
    /// borrow the client or create their own.
    runtime: Arc<tokio::runtime::Runtime>,
}

impl GrpcClientSync {
    /// Connects to a Hyper server via gRPC (blocking).
    ///
    /// # Errors
    ///
    /// - Returns [`ErrorKind::Other`] if a current-thread Tokio
    ///   runtime cannot be built.
    /// - Propagates any error from [`GrpcClient::connect`] (invalid
    ///   endpoint, TLS configuration failure, or transport setup
    ///   failure).
    pub fn connect(config: GrpcConfig) -> Result<Self> {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .map_err(|e| {
                Error::new(
                    ErrorKind::Other,
                    format!("Failed to create Tokio runtime: {e}"),
                )
            })?;

        let inner = runtime.block_on(GrpcClient::connect(config))?;

        Ok(GrpcClientSync {
            inner,
            runtime: Arc::new(runtime),
        })
    }

    /// Executes a SQL query (blocking).
    ///
    /// # Errors
    ///
    /// Blocking wrapper around [`GrpcClient::execute_query`]; see that
    /// method for the concrete failure modes.
    pub fn execute_query(&mut self, sql: &str) -> Result<GrpcQueryResult> {
        self.runtime.block_on(self.inner.execute_query(sql))
    }

    /// Executes a query and returns Arrow IPC bytes (blocking).
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::execute_query`].
    pub fn execute_query_to_arrow(&mut self, sql: &str) -> Result<bytes::Bytes> {
        self.runtime
            .block_on(self.inner.execute_query_to_arrow(sql))
    }

    /// Executes a query and returns a blocking streaming chunk producer.
    ///
    /// See [`GrpcClient::execute_query_stream`] for the streaming semantics
    /// and memory behavior. The returned [`GrpcChunkStreamSync`] lets you
    /// pull chunks one at a time without buffering the entire result.
    ///
    /// # Errors
    ///
    /// Same failure modes as [`GrpcClient::execute_query_stream`].
    pub fn execute_query_stream(&mut self, sql: &str) -> Result<GrpcChunkStreamSync> {
        let inner = self
            .runtime
            .block_on(self.inner.execute_query_stream(sql))?;
        Ok(GrpcChunkStreamSync {
            inner,
            runtime: Arc::clone(&self.runtime),
        })
    }

    /// Executes a parameterized SQL query (blocking).
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hyperdb_api_core::client::grpc::{GrpcClientSync, GrpcConfig, QueryParameters, ParameterStyle};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = GrpcConfig::new("http://localhost:7484");
    /// # let mut client = GrpcClientSync::connect(config)?;
    /// let params = QueryParameters::json_positional(&[&42i64])?;
    /// let result = client.execute_query_with_params(
    ///     "SELECT * FROM users WHERE id = $1",
    ///     params,
    ///     ParameterStyle::DollarNumbered,
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Blocking wrapper around
    /// [`GrpcClient::execute_query_with_params`]; see that method for
    /// the concrete failure modes.
    pub fn execute_query_with_params(
        &mut self,
        sql: &str,
        params: QueryParameters,
        style: ParameterStyle,
    ) -> Result<GrpcQueryResult> {
        self.runtime
            .block_on(self.inner.execute_query_with_params(sql, params, style))
    }

    /// Executes a parameterized query and returns Arrow IPC bytes (blocking).
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::execute_query_with_params`].
    pub fn execute_query_with_params_to_arrow(
        &mut self,
        sql: &str,
        params: QueryParameters,
        style: ParameterStyle,
    ) -> Result<bytes::Bytes> {
        self.runtime.block_on(
            self.inner
                .execute_query_with_params_to_arrow(sql, params, style),
        )
    }

    /// Cancels an in-flight gRPC query by its `query_id` (blocking).
    ///
    /// Blocking wrapper around
    /// [`GrpcClient::cancel_query`]. See that method's documentation for
    /// when a `query_id` is available (ASYNC-mode queries), best-effort
    /// cancel semantics, and the full "Relation to the `Cancellable`
    /// trait" discussion.
    ///
    /// # Fallible by design
    ///
    /// The `Result<()>` return is intentional and mirrors the async
    /// `GrpcClient::cancel_query`. Explicit callers get to observe
    /// transport-level failures (network errors, channel closed, auth
    /// expired) so they can record metrics, retry, or surface "cancel
    /// failed" UX. This is *not* an `impl Cancellable for GrpcClientSync`
    /// — it cannot be, because `Cancellable::cancel(&self)` takes no
    /// arguments and has no way to pass the `query_id`.  See the
    /// [`Cancellable`](crate::client::cancel::Cancellable) trait docs for the
    /// infallible-wrapper pattern used by `Drop`-path consumers.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hyperdb_api_core::client::grpc::{GrpcClientSync, GrpcConfig};
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = GrpcConfig::new("http://localhost:7484");
    /// # let mut client = GrpcClientSync::connect(config)?;
    /// # let query_id = "some-query-id";
    /// client.cancel_query(query_id)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Same failure modes as [`GrpcClient::cancel_query`] —
    /// transport-level errors bubble up; a cancel for an
    /// already-completed query returns `Ok(())` by design.
    pub fn cancel_query(&mut self, query_id: &str) -> Result<()> {
        self.runtime.block_on(self.inner.cancel_query(query_id))
    }

    /// Returns the client configuration.
    pub fn config(&self) -> &GrpcConfig {
        self.inner.config()
    }

    /// Closes the connection (blocking).
    ///
    /// # Errors
    ///
    /// Currently infallible — always returns `Ok(())`. The `Result`
    /// return type is preserved for API symmetry with async callers.
    pub fn close(self) -> Result<()> {
        self.runtime.block_on(self.inner.close())
    }
}

/// Blocking wrapper around [`GrpcChunkStream`].
///
/// Returned by [`GrpcClientSync::execute_query_stream`] and the
/// `AuthenticatedGrpcClientSync` equivalent. Yields Arrow IPC byte chunks
/// one at a time, blocking on the shared Tokio runtime as needed.
///
/// Pair with
/// [`hyperdb_api::ArrowRowset::from_stream`][arrow_rowset_from_stream] to
/// decode Arrow record batches incrementally with constant client memory.
///
/// [arrow_rowset_from_stream]: https://docs.rs/hyperdb-api/latest/hyperdb_api/struct.ArrowRowset.html#method.from_stream
#[derive(Debug)]
pub struct GrpcChunkStreamSync {
    inner: GrpcChunkStream,
    runtime: Arc<tokio::runtime::Runtime>,
}

impl GrpcChunkStreamSync {
    /// Returns the next Arrow IPC byte chunk, or `None` when the stream is
    /// complete.
    ///
    /// # Errors
    ///
    /// Same failure modes as [`GrpcChunkStream::next_chunk`] —
    /// transport errors and server-side query failures surface as
    /// [`Error`].
    pub fn next_chunk(&mut self) -> Result<Option<bytes::Bytes>> {
        self.runtime.block_on(self.inner.next_chunk())
    }

    /// Returns the schema reported by the server, if one has been received yet.
    pub fn schema(&self) -> Option<&super::proto::QueryResultSchema> {
        self.inner.schema()
    }

    /// Returns the server-assigned query ID, if one has been received.
    pub fn query_id(&self) -> Option<&str> {
        self.inner.query_id()
    }

    /// Returns the affected row count for DML queries, if reported.
    pub fn rows_affected(&self) -> Option<u64> {
        self.inner.rows_affected()
    }
}