hyperdb-api-core 0.1.1

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
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! gRPC query result types.
//!
//! This module provides types for handling gRPC query results, which are
//! returned in Apache Arrow IPC format.

use std::collections::VecDeque;

use bytes::{Bytes, BytesMut};

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

use super::proto::{QueryResultSchema, SqlType};

/// Result of a gRPC query execution.
///
/// Unlike TCP-based queries that return row-at-a-time results, gRPC queries
/// return results in Arrow IPC format, which can contain multiple record batches.
///
/// # 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").await?;
///
/// // Get raw Arrow IPC bytes for all chunks
/// let all_arrow_data = result.arrow_data();
///
/// // Or process chunk by chunk
/// for chunk in result.chunks() {
///     let arrow_bytes = chunk.arrow_data();
///     // Process with arrow crate...
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct GrpcQueryResult {
    /// The query ID assigned by the server
    pub(crate) query_id: Option<String>,
    /// Schema information from the server
    pub(crate) schema: Option<QueryResultSchema>,
    /// Result chunks (Arrow IPC data)
    pub(crate) chunks: VecDeque<GrpcResultChunk>,
    /// Number of rows affected (for DML queries)
    pub(crate) rows_affected: Option<u64>,
    /// Whether the query is complete
    pub(crate) is_complete: bool,
}

impl GrpcQueryResult {
    /// Creates a new empty result.
    pub(crate) fn new() -> Self {
        GrpcQueryResult {
            query_id: None,
            schema: None,
            chunks: VecDeque::new(),
            rows_affected: None,
            is_complete: false,
        }
    }

    /// Returns the query ID assigned by the server, if available.
    #[must_use]
    pub fn query_id(&self) -> Option<&str> {
        self.query_id.as_deref()
    }

    /// Returns the number of columns in the result.
    #[must_use]
    pub fn column_count(&self) -> usize {
        self.schema.as_ref().map_or(0, |s| s.columns.len())
    }

    /// Returns the column descriptions from the server.
    pub fn columns(&self) -> impl Iterator<Item = GrpcColumnInfo<'_>> + '_ {
        self.schema
            .as_ref()
            .map(|s| s.columns.iter())
            .into_iter()
            .flatten()
            .map(|col| GrpcColumnInfo {
                name: &col.name,
                sql_type: col.r#type,
            })
    }

    /// Returns whether there are more result chunks available.
    #[must_use]
    pub fn has_chunks(&self) -> bool {
        !self.chunks.is_empty()
    }

    /// Takes the next result chunk, if available.
    pub fn take_chunk(&mut self) -> Option<GrpcResultChunk> {
        self.chunks.pop_front()
    }

    /// Returns an iterator over the result chunks.
    pub fn chunks(&self) -> impl Iterator<Item = &GrpcResultChunk> {
        self.chunks.iter()
    }

    /// Returns all Arrow IPC data concatenated.
    ///
    /// For queries with multiple chunks, this concatenates all Arrow record
    /// batches. Note that each chunk may have its own schema message, so the
    /// result may contain multiple schema messages.
    ///
    /// Single-chunk results are returned without any copy (refcount bump on
    /// the shared `Bytes`). Multi-chunk results are concatenated into a new
    /// `Bytes`. Prefer `chunk_bytes()` if you can process chunks incrementally.
    #[must_use]
    pub fn arrow_data(&self) -> Bytes {
        match self.chunks.len() {
            0 => Bytes::new(),
            1 => self.chunks[0].data.clone(),
            _ => {
                let total_len: usize = self.chunks.iter().map(|c| c.data.len()).sum();
                let mut buf = BytesMut::with_capacity(total_len);
                for chunk in &self.chunks {
                    buf.extend_from_slice(&chunk.data);
                }
                buf.freeze()
            }
        }
    }

    /// Consumes the result and returns all Arrow IPC data.
    ///
    /// Single-chunk results are returned without any copy. Multi-chunk
    /// results are concatenated into a new `Bytes`.
    #[must_use]
    pub fn into_arrow_data(mut self) -> Bytes {
        match self.chunks.len() {
            0 => Bytes::new(),
            1 => self.chunks.pop_front().map(|c| c.data).unwrap_or_default(),
            _ => {
                let total_len: usize = self.chunks.iter().map(|c| c.data.len()).sum();
                let mut buf = BytesMut::with_capacity(total_len);
                for chunk in self.chunks {
                    buf.extend_from_slice(&chunk.data);
                }
                buf.freeze()
            }
        }
    }

    /// Returns an iterator over the raw Arrow IPC byte chunks.
    ///
    /// Each chunk is a refcount-bumped `Bytes` sharing the original gRPC frame
    /// allocation — no copies are performed. This is the preferred way to feed
    /// results into an incremental Arrow IPC decoder.
    pub fn chunk_bytes(&self) -> impl Iterator<Item = Bytes> + '_ {
        self.chunks.iter().map(|c| c.data.clone())
    }

    /// Returns the number of rows affected by a DML query.
    ///
    /// For SELECT queries, this is `None`. For INSERT/UPDATE/DELETE queries,
    /// this returns the number of rows affected.
    #[must_use]
    pub fn rows_affected(&self) -> Option<u64> {
        self.rows_affected
    }

    /// Returns whether the query execution is complete.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.is_complete
    }
}

impl Default for GrpcQueryResult {
    fn default() -> Self {
        Self::new()
    }
}

/// A single chunk of gRPC query results.
///
/// Each chunk contains Arrow IPC data (schema + record batch) and metadata
/// about the chunk. The data is held as `Bytes`, so it shares the underlying
/// allocation with the gRPC frame it was decoded from — cloning the chunk or
/// extracting the bytes is a refcount bump, not a copy.
#[derive(Debug)]
pub struct GrpcResultChunk {
    /// The chunk ID (for async/adaptive transfer modes)
    pub(crate) chunk_id: u64,
    /// Arrow IPC data (may include schema + record batch)
    pub(crate) data: Bytes,
    /// Number of rows in this chunk
    pub(crate) row_count: Option<usize>,
}

impl GrpcResultChunk {
    /// Creates a new result chunk.
    pub(crate) fn new(chunk_id: u64, data: Bytes) -> Self {
        GrpcResultChunk {
            chunk_id,
            data,
            row_count: None,
        }
    }

    /// Returns the chunk ID.
    pub fn chunk_id(&self) -> u64 {
        self.chunk_id
    }

    /// Returns the Arrow IPC data for this chunk.
    pub fn arrow_data(&self) -> &[u8] {
        &self.data
    }

    /// Returns the Arrow IPC data as a shared `Bytes` handle.
    ///
    /// This is a refcount bump, not a copy — the returned `Bytes` shares the
    /// same allocation as the chunk.
    pub fn arrow_bytes(&self) -> Bytes {
        self.data.clone()
    }

    /// Consumes the chunk and returns the Arrow IPC data.
    pub fn into_arrow_data(self) -> Bytes {
        self.data
    }

    /// Returns the number of rows in this chunk, if known.
    pub fn row_count(&self) -> Option<usize> {
        self.row_count
    }

    /// Returns whether this chunk is empty (no data).
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}

/// Column information from a gRPC query result.
#[derive(Debug)]
pub struct GrpcColumnInfo<'a> {
    /// Column name
    pub name: &'a str,
    /// SQL type information
    pub sql_type: Option<SqlType>,
}

impl GrpcColumnInfo<'_> {
    /// Returns the column name.
    #[must_use]
    pub fn name(&self) -> &str {
        self.name
    }

    /// Returns the SQL type tag (e.g., "INTEGER", "TEXT").
    #[must_use]
    pub fn type_name(&self) -> Option<&'static str> {
        use crate::client::grpc::proto::hyper_service::sql_type::TypeTag;

        self.sql_type
            .as_ref()
            .and_then(|t| match TypeTag::try_from(t.tag).ok()? {
                TypeTag::HyperUnspecified => None,
                TypeTag::HyperBool => Some("BOOLEAN"),
                TypeTag::HyperBigInt => Some("BIGINT"),
                TypeTag::HyperSmallInt => Some("SMALLINT"),
                TypeTag::HyperInt => Some("INTEGER"),
                TypeTag::HyperNumeric => Some("NUMERIC"),
                TypeTag::HyperDouble => Some("DOUBLE PRECISION"),
                TypeTag::HyperFloat => Some("REAL"),
                TypeTag::HyperOid => Some("OID"),
                TypeTag::HyperByteA => Some("BYTEA"),
                TypeTag::HyperText => Some("TEXT"),
                TypeTag::HyperVarchar => Some("VARCHAR"),
                TypeTag::HyperChar => Some("CHAR"),
                TypeTag::HyperJson => Some("JSON"),
                TypeTag::HyperDate => Some("DATE"),
                TypeTag::HyperInterval => Some("INTERVAL"),
                TypeTag::HyperTime => Some("TIME"),
                TypeTag::HyperTimestamp => Some("TIMESTAMP"),
                TypeTag::HyperTimestampTz => Some("TIMESTAMPTZ"),
                TypeTag::HyperGeography => Some("GEOGRAPHY"),
                TypeTag::HyperArrayOfFloat => Some("FLOAT[]"),
            })
    }
}

/// Converts Hyper SQL types from the gRPC schema to hyper-types.
///
/// This is useful for applications that need to work with Hyper's type system
/// rather than Arrow's type system.
#[allow(
    dead_code,
    reason = "helper retained for callers that want to convert gRPC schema to hyper-types"
)]
pub(super) fn sql_type_to_hyper_type(sql_type: &SqlType) -> Result<crate::types::SqlType> {
    use super::proto::hyper_service::sql_type::{Modifier, TypeTag};
    use crate::types::SqlType as HyperSqlType;

    let tag = TypeTag::try_from(sql_type.tag).map_err(|_| {
        Error::new(
            ErrorKind::Conversion,
            format!("Unknown SQL type tag: {}", sql_type.tag),
        )
    })?;

    // Extract modifier for types that need it
    let modifier = &sql_type.modifier;

    match tag {
        TypeTag::HyperUnspecified => Err(Error::new(ErrorKind::Conversion, "Unspecified SQL type")),
        TypeTag::HyperBool => Ok(HyperSqlType::Bool),
        TypeTag::HyperSmallInt => Ok(HyperSqlType::SmallInt),
        TypeTag::HyperInt => Ok(HyperSqlType::Int),
        TypeTag::HyperBigInt => Ok(HyperSqlType::BigInt),
        TypeTag::HyperFloat => Ok(HyperSqlType::Float),
        TypeTag::HyperDouble => Ok(HyperSqlType::Double),
        TypeTag::HyperNumeric => {
            // Extract precision/scale from modifier
            let (precision, scale) = match modifier {
                Some(Modifier::NumericModifier(m)) => (m.precision, m.scale),
                _ => (38, 0), // Default precision/scale
            };
            Ok(HyperSqlType::Numeric { precision, scale })
        }
        TypeTag::HyperText => Ok(HyperSqlType::Text),
        TypeTag::HyperVarchar => {
            let max_length = match modifier {
                Some(Modifier::MaxLength(len)) => Some(*len),
                _ => None,
            };
            Ok(HyperSqlType::Varchar { max_length })
        }
        TypeTag::HyperChar => {
            let length = match modifier {
                Some(Modifier::MaxLength(len)) => *len,
                _ => 1,
            };
            Ok(HyperSqlType::Char { length })
        }
        TypeTag::HyperByteA => Ok(HyperSqlType::ByteA),
        TypeTag::HyperOid => Ok(HyperSqlType::Oid),
        TypeTag::HyperJson => Ok(HyperSqlType::Json),
        TypeTag::HyperDate => Ok(HyperSqlType::Date),
        TypeTag::HyperTime => Ok(HyperSqlType::Time),
        TypeTag::HyperTimestamp => Ok(HyperSqlType::Timestamp),
        TypeTag::HyperTimestampTz => Ok(HyperSqlType::TimestampTz),
        TypeTag::HyperInterval => Ok(HyperSqlType::Interval),
        TypeTag::HyperGeography => Ok(HyperSqlType::Geography),
        TypeTag::HyperArrayOfFloat => {
            // Array types are not directly supported in crate::types::SqlType
            Err(Error::new(
                ErrorKind::Conversion,
                "Array types not yet supported",
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_grpc_result_empty() {
        let result = GrpcQueryResult::new();
        assert!(!result.has_chunks());
        assert!(result.arrow_data().is_empty());
        assert_eq!(result.column_count(), 0);
    }

    #[test]
    fn test_grpc_result_with_chunks() {
        let mut result = GrpcQueryResult::new();
        result
            .chunks
            .push_back(GrpcResultChunk::new(0, Bytes::from_static(&[1, 2, 3])));
        result
            .chunks
            .push_back(GrpcResultChunk::new(1, Bytes::from_static(&[4, 5, 6])));

        assert!(result.has_chunks());
        assert_eq!(result.arrow_data().as_ref(), &[1, 2, 3, 4, 5, 6]);

        let chunk = result.take_chunk().unwrap();
        assert_eq!(chunk.chunk_id(), 0);
        assert_eq!(chunk.arrow_data(), &[1, 2, 3]);
    }

    #[test]
    fn test_sql_type_mapping() {
        use crate::client::grpc::proto::hyper_service::sql_type::TypeTag;

        let sql_type = SqlType {
            tag: TypeTag::HyperInt.into(),
            modifier: None,
        };

        let hyper_type = sql_type_to_hyper_type(&sql_type).unwrap();
        assert_eq!(hyper_type, crate::types::SqlType::Int);
    }
}