greptimedb 0.0.1

Synchronous, completion-driven, thread-affine Rust SDK for GreptimeDB
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
//! Caller-thread Arrow Flight `DoGet` query stream.

use std::marker::PhantomData;
use std::rc::Rc;

use std::collections::HashMap;

use arrow_array::{ArrayRef, RecordBatch};
use arrow_buffer::Buffer;
use arrow_schema::SchemaRef;
use greptimedb_proto::flight::{FlightData, Ticket};
use greptimedb_proto::v1::{
    self, GreptimeRequest, InsertIntoPlan, PromRangeQuery, QueryRequest, TableName,
    greptime_request, query_request,
};
use prost::Message;

use crate::client::Client;
use crate::config::ClientOptions;
use crate::error::{Error, Result};
use crate::transport::EndpointTransport;

use super::decode::{
    decode_flight_record_batch_with_dictionaries, decode_flight_schema, parse_message,
};

const DO_GET_PATH: &str = "/arrow.flight.protocol.FlightService/DoGet";

/// Thread-affine stream of Arrow batches from `DoGet`.
///
/// The request ticket contains one `GreptimeRequest` whose body is a
/// [`QueryRequest`]. Dropping an unfinished stream cancels it.
pub struct QueryStream {
    call: Option<grpc::ServerStreamingCall<FlightData>>,
    schema: Option<SchemaRef>,
    finished: bool,
    failed: bool,
    dictionaries: HashMap<i64, ArrayRef>,
    affected_rows: u64,
    saw_affected_rows: bool,
    metrics: Vec<Vec<u8>>,
    _not_send_sync: PhantomData<Rc<()>>,
}

impl QueryStream {
    /// Run a SQL query.
    ///
    /// # Errors
    /// Returns a configuration, encoding, or transport error while opening the
    /// stream. Query execution errors arrive from [`Self::next_batch`].
    pub fn sql(
        channel: &mut grpc::SyncChannel<EndpointTransport>,
        options: &ClientOptions,
        query: &str,
    ) -> Result<Self> {
        Self::open(
            channel,
            options,
            QueryRequest {
                query: Some(query_request::Query::Sql(query.to_owned())),
            },
        )
    }

    /// Run a Prometheus range query.
    ///
    /// `start`, `end`, `step`, and `lookback` are the strings GreptimeDB expects
    /// on the PromQL wire format, such as `"1672531200"` or `"15s"`.
    ///
    /// # Errors
    /// Returns a configuration, encoding, or transport error while opening the stream.
    pub fn prom_range_query(
        channel: &mut grpc::SyncChannel<EndpointTransport>,
        options: &ClientOptions,
        query: &str,
        start: &str,
        end: &str,
        step: &str,
        lookback: &str,
    ) -> Result<Self> {
        Self::open(
            channel,
            options,
            QueryRequest {
                query: Some(query_request::Query::PromRangeQuery(PromRangeQuery {
                    query: query.to_owned(),
                    start: start.to_owned(),
                    end: end.to_owned(),
                    step: step.to_owned(),
                    lookback: lookback.to_owned(),
                })),
            },
        )
    }

    /// Run a serialized logical plan.
    ///
    /// # Errors
    /// Returns a configuration, encoding, or transport error while opening the stream.
    pub fn logical_plan(
        channel: &mut grpc::SyncChannel<EndpointTransport>,
        options: &ClientOptions,
        plan: &[u8],
    ) -> Result<Self> {
        Self::open(
            channel,
            options,
            QueryRequest {
                query: Some(query_request::Query::LogicalPlan(plan.to_vec())),
            },
        )
    }

    /// Run `INSERT INTO` expressed as a logical plan for one table.
    ///
    /// The table is addressed in the client's catalog and schema.
    ///
    /// # Errors
    /// Returns a configuration, encoding, or transport error while opening the stream.
    pub fn insert_into_plan(
        channel: &mut grpc::SyncChannel<EndpointTransport>,
        options: &ClientOptions,
        table_name: &str,
        logical_plan: &[u8],
    ) -> Result<Self> {
        Self::open(
            channel,
            options,
            QueryRequest {
                query: Some(query_request::Query::InsertIntoPlan(InsertIntoPlan {
                    table_name: Some(TableName {
                        catalog_name: options.catalog.clone(),
                        schema_name: options.schema.clone(),
                        table_name: table_name.to_owned(),
                    }),
                    logical_plan: logical_plan.to_vec(),
                })),
            },
        )
    }

    /// Borrow the schema announced by the first Flight frame, once seen.
    #[must_use]
    pub fn schema(&self) -> Option<&SchemaRef> {
        self.schema.as_ref()
    }

    /// Read the next record batch.
    ///
    /// The initial schema frame is consumed internally and is not returned.
    /// `Ok(None)` means the server completed the stream successfully.
    ///
    /// # Errors
    /// Returns a transport, protocol, or decode error. A GreptimeDB trailer
    /// status is reported as [`Error::ServerStatus`].
    pub fn next_batch(&mut self) -> Result<Option<RecordBatch>> {
        if self.failed {
            return Err(Error::Protocol(
                "Flight query failed and must be dropped".into(),
            ));
        }
        let result = self.next_batch_inner();
        if result.is_err() {
            self.failed = true;
            if let Some(call) = self.call.as_mut() {
                let _cancel_result = call.cancel();
            }
            self.finished = true;
        }
        result
    }

    fn next_batch_inner(&mut self) -> Result<Option<RecordBatch>> {
        if self.finished {
            return Ok(None);
        }
        loop {
            let received = {
                let call = self.call_mut()?;
                call.recv_next()
            };
            match received {
                Ok(None) => {
                    self.check_trailers()?;
                    self.finished = true;
                    return Ok(None);
                }
                Ok(Some(frame)) => {
                    let message = parse_message(&frame.data_header)?;
                    if message.bodyLength() < 0
                        || usize::try_from(message.bodyLength()).ok() != Some(frame.data_body.len())
                    {
                        return Err(Error::Protocol(
                            "Flight IPC body length does not match header".into(),
                        ));
                    }
                    match message.header_type() {
                        arrow_ipc::MessageHeader::NONE => {
                            self.accept_metadata(&frame.app_metadata)?;
                        }
                        arrow_ipc::MessageHeader::Schema => {
                            if self.schema.is_some() {
                                return Err(Error::Protocol("duplicate Flight schema".into()));
                            }
                            self.schema = Some(decode_flight_schema(&frame.data_header)?);
                        }
                        arrow_ipc::MessageHeader::DictionaryBatch => {
                            let schema = self.schema.as_ref().ok_or_else(|| {
                                Error::Protocol("dictionary preceded Flight schema".into())
                            })?;
                            let dictionary = message
                                .header_as_dictionary_batch()
                                .ok_or_else(|| Error::Decode("missing dictionary header".into()))?;
                            arrow_ipc::reader::read_dictionary(
                                &Buffer::from(frame.data_body),
                                dictionary,
                                schema,
                                &mut self.dictionaries,
                                &message.version(),
                            )
                            .map_err(|error| Error::Decode(error.to_string()))?;
                        }
                        arrow_ipc::MessageHeader::RecordBatch => {
                            let schema = self.schema.clone().ok_or_else(|| {
                                Error::Protocol("record batch preceded Flight schema".into())
                            })?;
                            return decode_flight_record_batch_with_dictionaries(
                                &frame.data_header,
                                &frame.data_body,
                                &schema,
                                &self.dictionaries,
                            )
                            .map(Some);
                        }
                        other => {
                            return Err(Error::Protocol(format!(
                                "unsupported Flight IPC header {other:?}"
                            )));
                        }
                    }
                }
                Err(error) => return Err(self.map_error(error)),
            }
        }
    }

    fn accept_metadata(&mut self, bytes: &[u8]) -> Result<()> {
        let metadata =
            v1::FlightMetadata::decode(bytes).map_err(|error| Error::Decode(error.to_string()))?;
        if metadata.affected_rows.is_none() && metadata.metrics.is_none() {
            return Err(Error::Protocol("empty Flight metadata".into()));
        }
        if let Some(rows) = metadata.affected_rows {
            self.affected_rows = self
                .affected_rows
                .checked_add(u64::from(rows.value))
                .ok_or_else(|| Error::Protocol("affected row count overflowed".into()))?;
            self.saw_affected_rows = true;
        }
        if let Some(metrics) = metadata.metrics {
            self.metrics.push(metrics.metrics);
        }
        Ok(())
    }

    /// Rows reported by metadata frames consumed so far.
    pub const fn affected_rows(&self) -> u64 {
        self.affected_rows
    }

    /// Opaque GreptimeDB metrics payloads received so far.
    pub fn metrics(&self) -> &[Vec<u8>] {
        &self.metrics
    }

    /// Drain the stream into owned record batches.
    ///
    /// # Errors
    /// Returns the first error from [`Self::next_batch`].
    pub fn collect_batches(mut self) -> Result<Vec<RecordBatch>> {
        let mut batches = Vec::new();
        while let Some(batch) = self.next_batch()? {
            batches.push(batch);
        }
        Ok(batches)
    }

    /// Drain remaining frames and return DML affected rows and query metrics.
    ///
    /// # Errors
    /// Returns the first stream, IPC, server diagnostic, or row-count conversion error.
    pub fn finish(mut self) -> Result<(Option<usize>, Vec<Vec<u8>>)> {
        while self.next_batch()?.is_some() {}
        let rows = if self.saw_affected_rows {
            Some(
                usize::try_from(self.affected_rows)
                    .map_err(|_| Error::Protocol("affected row count exceeds usize".into()))?,
            )
        } else {
            None
        };
        Ok((rows, std::mem::take(&mut self.metrics)))
    }

    fn open(
        channel: &mut grpc::SyncChannel<EndpointTransport>,
        options: &ClientOptions,
        query: QueryRequest,
    ) -> Result<Self> {
        let request = GreptimeRequest {
            header: Some(options.build_request_header(None)),
            request: Some(greptime_request::Request::Query(query)),
        };
        let ticket = Ticket {
            ticket: request.encode_to_vec(),
        };
        let call_options = options.try_build_call_options(None)?;
        let call = channel
            .server_streaming(
                DO_GET_PATH,
                &ticket,
                grpc::ProstCodec::<Ticket, FlightData>::new(),
                call_options,
            )
            .map_err(Error::from)?;
        Ok(Self {
            call: Some(call),
            schema: None,
            finished: false,
            failed: false,
            dictionaries: HashMap::new(),
            affected_rows: 0,
            saw_affected_rows: false,
            metrics: Vec::new(),
            _not_send_sync: PhantomData,
        })
    }

    fn call_mut(&mut self) -> Result<&mut grpc::ServerStreamingCall<FlightData>> {
        self.call
            .as_mut()
            .ok_or_else(|| Error::Protocol("query stream is closed".into()))
    }

    fn check_trailers(&self) -> Result<()> {
        let Some(call) = self.call.as_ref() else {
            return Err(Error::Protocol("query stream is closed".into()));
        };
        match call.trailing_metadata() {
            Ok(Some(metadata)) => {
                match crate::bulk::writer::application_error_from_trailers(&metadata) {
                    Some(error) => Err(error),
                    None => Ok(()),
                }
            }
            Ok(None) => Err(Error::Protocol(
                "Flight query ended without trailers".into(),
            )),
            Err(error) => Err(map_status(error)),
        }
    }

    fn map_error(&self, error: grpc::StreamError) -> Error {
        match error {
            grpc::StreamError::WouldBlock => {
                Error::Stream("blocking Flight query returned pending".into())
            }
            grpc::StreamError::Status(status) => {
                let trailers = self
                    .call
                    .as_ref()
                    .and_then(|call| call.trailing_metadata().ok().flatten());
                Error::from_grpc_status_and_trailers(status, trailers.as_ref())
            }
        }
    }
}

impl Client {
    /// Run a SQL query using this client's routing and RPC defaults.
    ///
    /// # Errors
    /// Returns missing-channel, configuration, encoding, or transport errors
    /// while opening the stream. Query execution errors arrive from
    /// [`QueryStream::next_batch`].
    pub fn query(&mut self, sql: &str) -> Result<QueryStream> {
        let client_options = self.options().clone();
        QueryStream::sql(self.channel_mut()?, &client_options, sql)
    }
}

impl Drop for QueryStream {
    fn drop(&mut self) {
        if !self.finished
            && let Some(mut call) = self.call.take()
        {
            #[allow(clippy::let_underscore_must_use)]
            {
                let _ = call.cancel();
            }
        }
    }
}

fn map_status(error: grpc::StreamError) -> Error {
    match error {
        grpc::StreamError::WouldBlock => Error::Stream("Flight query is pending".into()),
        grpc::StreamError::Status(status) => Error::Grpc(status),
    }
}