sqlx-exasol-impl 0.9.2

Driver implementation for sqlx-exasol. Not meant to be used directly.
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
//! This module contains the logic for streaming database rows from a [`ResultSet`] returned from a
//! query execution.
//!
//! While intimidating at first, the types in the module are easier to follow due to their implicit
//! hierarchy.

use std::{
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
    vec,
};

use futures_core::ready;
use futures_util::Stream;
use serde_json::Value;
use sqlx_core::{ext::ustr::UStr, logger::QueryLogger, Either, HashMap};

use crate::{
    column::ExaColumn,
    connection::websocket::{
        future::{
            CloseResultSets, Execute, ExecuteBatch, ExecutePrepared, FetchChunk, WebSocketFuture,
        },
        ExaWebSocket,
    },
    error::ExaProtocolError,
    query_result::ExaQueryResult,
    responses::{DataChunk, MultiResults, QueryResult, ResultSet, ResultSetOutput, SingleResult},
    row::ExaRow,
    SqlxError, SqlxResult,
};

/// Adapter stream that stores a future following the query execution and then a stream of results
/// from the database.
///
/// This is the top of the hierarchy and the actual type used to stream rows from a [`ResultSet`].
pub struct ResultStream<'ws> {
    ws: &'ws mut ExaWebSocket,
    logger: QueryLogger,
    result_set_handles: Vec<u16>,
    state: ResultStreamState,
    had_err: bool,
}

impl<'ws> ResultStream<'ws> {
    pub fn new<F>(ws: &'ws mut ExaWebSocket, logger: QueryLogger, future: F) -> Self
    where
        ResultStreamState: From<F>,
    {
        Self {
            ws,
            logger,
            result_set_handles: Vec::new(),
            state: future.into(),
            had_err: false,
        }
    }

    /// Inner polling function that handles the actual logic.
    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Option<<Self as Stream>::Item>> {
        loop {
            match &mut self.state {
                ResultStreamState::Execute(future) => {
                    let multi_stream = ready!(future.poll_unpin(cx, self.ws))?;
                    self.result_set_handles = multi_stream.handles();
                    self.state = ResultStreamState::Stream(multi_stream);
                }

                ResultStreamState::ExecuteBatch(future) => {
                    let multi_stream = ready!(future.poll_unpin(cx, self.ws))?;
                    self.result_set_handles = multi_stream.handles();
                    self.state = ResultStreamState::Stream(multi_stream);
                }
                ResultStreamState::ExecutePrepared(future) => {
                    let multi_stream = ready!(future.poll_unpin(cx, self.ws))?;
                    self.result_set_handles = multi_stream.handles();
                    self.state = ResultStreamState::Stream(multi_stream);
                }
                ResultStreamState::Stream(stream) => {
                    let Some(either) = ready!(stream.poll_next_unpin(cx, self.ws)).transpose()?
                    else {
                        return Poll::Ready(None);
                    };

                    match &either {
                        Either::Left(q) => self.logger.increase_rows_affected(q.rows_affected()),
                        Either::Right(_) => self.logger.increment_rows_returned(),
                    }

                    return Poll::Ready(Some(Ok(either)));
                }
            }
        }
    }
}

/// The [`Stream`] implementation here encapsulates end-stages actions such as using the
/// [`QueryLogger`] or taking note of whether an error occurred (and stop streaming if it did).
impl Stream for ResultStream<'_> {
    type Item = SqlxResult<Either<ExaQueryResult, ExaRow>>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();

        // This check is important, especially in multi statement queries.
        // If one statement fails, an error gets returned, but if the stream keeps getting polled,
        // the next statement never gets executed, and the stream will pend forever.
        if this.had_err {
            return Poll::Ready(None);
        }

        let poll = this.poll(cx);

        if let Poll::Ready(Some(Err(_))) = &poll {
            this.had_err = true;
        }

        poll
    }
}

impl Drop for ResultStream<'_> {
    fn drop(&mut self) {
        let handles = std::mem::take(&mut self.result_set_handles);
        if !handles.is_empty() {
            // Register the result set handles to be closed in the next database interaction.
            self.ws.pending_close = Some(CloseResultSets::new(handles));
        }
    }
}

/// State used to distinguish between the initial query execution and the subsequent streaming of
/// rows.
pub enum ResultStreamState {
    Execute(Execute),
    ExecuteBatch(ExecuteBatch),
    ExecutePrepared(ExecutePrepared),
    Stream(MultiResultStream),
}

impl From<Execute> for ResultStreamState {
    fn from(value: Execute) -> Self {
        Self::Execute(value)
    }
}

impl From<ExecuteBatch> for ResultStreamState {
    fn from(value: ExecuteBatch) -> Self {
        Self::ExecuteBatch(value)
    }
}

impl From<ExecutePrepared> for ResultStreamState {
    fn from(value: ExecutePrepared) -> Self {
        Self::ExecutePrepared(value)
    }
}

/// Helper trait for defining a stream like interface which also accepts a [`ExaWebSocket`]
/// argument. This allows nesting types as needed and simply propagating the websocket as an
/// argument wherever polling is necessary.
trait WebsocketStream: Unpin {
    type Item;

    fn poll_next_unpin(
        &mut self,
        cx: &mut Context<'_>,
        ws: &mut ExaWebSocket,
    ) -> Poll<Option<Self::Item>>;
}

/// A type encapsulating multiple [`QueryResult`] instances and turning them one by one into
/// [`QueryResultStream`] instances, which are then polled to depletion.
pub struct MultiResultStream {
    next_results: vec::IntoIter<QueryResult>,
    stream: QueryResultStream,
}

impl MultiResultStream {
    pub fn new(first_result: QueryResult, next_results: vec::IntoIter<QueryResult>) -> Self {
        let stream = QueryResultStream::new(first_result);

        Self {
            next_results,
            stream,
        }
    }

    fn handles(&self) -> Vec<u16> {
        let first_handle = match &self.stream {
            QueryResultStream::RowStream(row_stream) => match &row_stream.chunk_stream {
                ChunkStream::Multi(multi_chunk_stream) => Some(multi_chunk_stream.handle),
                ChunkStream::Single(_) => None,
            },
            QueryResultStream::RowCount(_) => None,
        };

        let results_handles_iter = self
            .next_results
            .as_slice()
            .iter()
            .filter_map(QueryResult::handle);

        first_handle
            .into_iter()
            .chain(results_handles_iter)
            .collect()
    }
}

impl WebsocketStream for MultiResultStream {
    type Item = SqlxResult<Either<ExaQueryResult, ExaRow>>;

    fn poll_next_unpin(
        &mut self,
        cx: &mut Context<'_>,
        ws: &mut ExaWebSocket,
    ) -> Poll<Option<Self::Item>> {
        loop {
            if let Some(res) = ready!(self.stream.poll_next_unpin(cx, ws)) {
                return Poll::Ready(Some(res));
            }

            let Some(qr) = self.next_results.next() else {
                return Poll::Ready(None);
            };

            self.stream = QueryResultStream::new(qr);
        }
    }
}

impl From<SingleResult> for MultiResultStream {
    fn from(value: SingleResult) -> Self {
        Self::new(value.into(), Vec::new().into_iter())
    }
}

impl TryFrom<MultiResults> for MultiResultStream {
    type Error = SqlxError;

    fn try_from(value: MultiResults) -> Result<Self, Self::Error> {
        let mut next_results = value.results.into_iter();

        let Some(first_result) = next_results.next() else {
            return Err(ExaProtocolError::NoResponse)?;
        };

        Ok(MultiResultStream::new(first_result, next_results))
    }
}

/// A stream over either a result set or a single element stream containing the count of affected
/// rows.
///
/// This completely encapsulates the result streaming, but does not handle all edge actions such as
/// stopping if an error occurs.
pub enum QueryResultStream {
    RowStream(RowStream),
    RowCount(Option<ExaQueryResult>),
}

impl QueryResultStream {
    fn new(query_result: QueryResult) -> Self {
        match query_result {
            QueryResult::ResultSet { result_set: rs } => Self::RowStream(RowStream::new(rs)),
            QueryResult::RowCount { row_count } => {
                let query_result = ExaQueryResult::new(row_count);
                Self::RowCount(Some(query_result))
            }
        }
    }
}

impl WebsocketStream for QueryResultStream {
    type Item = SqlxResult<Either<ExaQueryResult, ExaRow>>;

    fn poll_next_unpin(
        &mut self,
        cx: &mut Context<'_>,
        ws: &mut ExaWebSocket,
    ) -> Poll<Option<Self::Item>> {
        match self {
            QueryResultStream::RowStream(rs) => rs
                .poll_next_unpin(cx, ws)
                .map(|o| o.map(|r| r.map(Either::Right))),
            QueryResultStream::RowCount(qr) => Poll::Ready(qr.take().map(Either::Left).map(Ok)),
        }
    }
}
/// A stream over the rows of a result set.
/// The stream will fetch the data chunks one by one, while repopulating the chunk iterator, which
/// then gets iterated over to output rows.
///
/// This encapsulates the actual database row streaming from a [`ResultSet`].
pub struct RowStream {
    chunk_stream: ChunkStream,
    chunk_iter: ChunkIter,
}

impl RowStream {
    fn new(rs: ResultSet) -> Self {
        let ResultSet {
            total_rows_num,
            total_rows_pos,
            output,
            columns,
        } = rs;

        let chunk_iter = ChunkIter::new(columns);

        let chunk_stream = match output {
            ResultSetOutput::Handle(handle) => {
                ChunkStream::Multi(MultiChunkStream::new(handle, total_rows_num))
            }
            ResultSetOutput::Data(data) => {
                let num_rows = total_rows_pos;
                ChunkStream::Single(Some(DataChunk { num_rows, data }))
            }
        };

        Self {
            chunk_stream,
            chunk_iter,
        }
    }
}

impl WebsocketStream for RowStream {
    type Item = SqlxResult<ExaRow>;

    fn poll_next_unpin(
        &mut self,
        cx: &mut Context<'_>,
        ws: &mut ExaWebSocket,
    ) -> Poll<Option<Self::Item>> {
        loop {
            if let Some(row) = self.chunk_iter.next() {
                return Poll::Ready(Some(Ok(row)));
            }

            match ready!(self.chunk_stream.poll_next_unpin(cx, ws)?) {
                Some(chunk) => self.chunk_iter.renew(chunk),
                None => return Poll::Ready(None),
            }
        }
    }
}

/// Enum keeping track of the data chunks we expect from the server.
/// If there are less than 1000 rows in the result set, Exasol sends them directly.
/// Hence, we have a single chunk stream variant.
///
/// If there are more, we need to fetch chunks one by one using a [`MultiChunkStream`].
///
/// The purpose of this stream is to retrieve row chunks from the database so that they can be
/// iterated over and returned by the higher level streams.
enum ChunkStream {
    Multi(MultiChunkStream),
    Single(Option<DataChunk>),
}

impl WebsocketStream for ChunkStream {
    type Item = SqlxResult<DataChunk>;

    fn poll_next_unpin(
        &mut self,
        cx: &mut Context<'_>,
        ws: &mut ExaWebSocket,
    ) -> Poll<Option<Self::Item>> {
        match self {
            Self::Multi(s) => s.poll_next_unpin(cx, ws),
            Self::Single(chunk) => Poll::Ready(chunk.take().map(Ok)),
        }
    }
}

/// A stream of chunks for a given result set that was given a handle.
///
/// This is used if the [`ResultSet`] has more than 1000 rows, so data will arrive in chunks of rows
/// which can then be iterated over.
struct MultiChunkStream {
    handle: u16,
    total_rows_num: usize,
    total_rows_pos: usize,
    state: MultiChunkStreamState,
}

impl MultiChunkStream {
    fn new(handle: u16, total_rows_num: usize) -> Self {
        Self {
            handle,
            total_rows_num,
            total_rows_pos: 0,
            state: MultiChunkStreamState::Initial,
        }
    }
}

impl WebsocketStream for MultiChunkStream {
    type Item = SqlxResult<DataChunk>;

    fn poll_next_unpin(
        &mut self,
        cx: &mut Context<'_>,
        ws: &mut ExaWebSocket,
    ) -> Poll<Option<Self::Item>> {
        loop {
            match &mut self.state {
                MultiChunkStreamState::Initial => {
                    let num_bytes = ws.attributes.fetch_size();
                    let future = FetchChunk::new(self.handle, self.total_rows_pos, num_bytes);
                    self.state = MultiChunkStreamState::Polling(future);
                }
                MultiChunkStreamState::Polling(future) => {
                    if self.total_rows_pos >= self.total_rows_num {
                        self.state = MultiChunkStreamState::Finished;
                        continue;
                    }

                    let num_bytes = ws.attributes.fetch_size();
                    let chunk = ready!(future.poll_unpin(cx, ws))?;

                    self.total_rows_pos += chunk.num_rows;
                    let future = FetchChunk::new(self.handle, self.total_rows_pos, num_bytes);
                    self.state = MultiChunkStreamState::Polling(future);

                    return Poll::Ready(Some(Ok(chunk)));
                }
                MultiChunkStreamState::Finished => return Poll::Ready(None),
            }
        }
    }
}

enum MultiChunkStreamState {
    Initial,
    Polling(FetchChunk),
    Finished,
}

/// An iterator over a chunk of data from a result set.
///
/// This is the lowest level of the streaming hierarchy and merely iterates over an already
/// retrieved chunk of rows, not dealing at all with async operations.
struct ChunkIter {
    column_names: Arc<HashMap<UStr, usize>>,
    columns: Arc<[ExaColumn]>,
    chunk_rows_total: usize,
    chunk_rows_pos: usize,
    data: vec::IntoIter<Vec<Value>>,
}

impl ChunkIter {
    fn new(columns: Arc<[ExaColumn]>) -> Self {
        let column_names = columns
            .iter()
            .enumerate()
            .map(|(i, c)| (c.name.clone(), i))
            .collect();

        Self {
            column_names: Arc::new(column_names),
            columns,
            chunk_rows_total: 0,
            chunk_rows_pos: 0,
            data: Vec::new().into_iter(),
        }
    }
}

impl ChunkIter {
    fn renew(&mut self, chunk: DataChunk) {
        self.chunk_rows_pos = 0;
        self.chunk_rows_total = chunk.num_rows;
        self.data = chunk.data.into_iter();
    }
}

impl Iterator for ChunkIter {
    type Item = ExaRow;

    fn next(&mut self) -> Option<Self::Item> {
        debug_assert!(self.chunk_rows_pos <= self.chunk_rows_total);

        let row = ExaRow::new(
            self.data.next()?,
            self.columns.clone(),
            self.column_names.clone(),
        );

        self.chunk_rows_pos += 1;
        Some(row)
    }
}