duckdb/
arrow_batch.rs

1use super::{
2    arrow::{datatypes::SchemaRef, record_batch::RecordBatch},
3    Statement,
4};
5
6/// A handle for the resulting RecordBatch of a query.
7#[must_use = "Arrow is lazy and will do nothing unless consumed"]
8pub struct Arrow<'stmt> {
9    pub(crate) stmt: Option<&'stmt Statement<'stmt>>,
10}
11
12#[allow(clippy::needless_lifetimes)]
13impl<'stmt> Arrow<'stmt> {
14    #[inline]
15    pub(crate) fn new(stmt: &'stmt Statement<'stmt>) -> Arrow<'stmt> {
16        Arrow { stmt: Some(stmt) }
17    }
18
19    /// return arrow schema
20    #[inline]
21    pub fn get_schema(&self) -> SchemaRef {
22        self.stmt.unwrap().stmt.schema()
23    }
24}
25
26#[allow(clippy::needless_lifetimes)]
27impl<'stmt> Iterator for Arrow<'stmt> {
28    type Item = RecordBatch;
29
30    fn next(&mut self) -> Option<Self::Item> {
31        Some(RecordBatch::from(&self.stmt?.step()?))
32    }
33}
34
35/// A handle for the resulting RecordBatch of a query in streaming
36#[must_use = "Arrow stream is lazy and will not fetch data unless consumed"]
37#[allow(clippy::needless_lifetimes)]
38pub struct ArrowStream<'stmt> {
39    pub(crate) stmt: Option<&'stmt Statement<'stmt>>,
40    pub(crate) schema: SchemaRef,
41}
42
43#[allow(clippy::needless_lifetimes)]
44impl<'stmt> ArrowStream<'stmt> {
45    #[inline]
46    pub(crate) fn new(stmt: &'stmt Statement<'stmt>, schema: SchemaRef) -> ArrowStream<'stmt> {
47        ArrowStream {
48            stmt: Some(stmt),
49            schema,
50        }
51    }
52
53    /// return arrow schema
54    #[inline]
55    pub fn get_schema(&self) -> SchemaRef {
56        self.schema.clone()
57    }
58}
59
60#[allow(clippy::needless_lifetimes)]
61impl<'stmt> Iterator for ArrowStream<'stmt> {
62    type Item = RecordBatch;
63
64    fn next(&mut self) -> Option<Self::Item> {
65        Some(RecordBatch::from(&self.stmt?.stream_step(self.get_schema())?))
66    }
67}