use std::iter::FusedIterator;
use super::{
Result, Statement,
arrow::{datatypes::SchemaRef, record_batch::RecordBatch},
};
#[must_use = "Arrow is lazy and will do nothing unless consumed"]
pub struct Arrow<'stmt> {
pub(crate) stmt: Option<&'stmt Statement<'stmt>>,
}
#[allow(clippy::needless_lifetimes)]
impl<'stmt> Arrow<'stmt> {
#[inline]
pub(crate) fn new(stmt: &'stmt Statement<'stmt>) -> Self {
Arrow { stmt: Some(stmt) }
}
#[inline]
pub fn get_schema(&self) -> SchemaRef {
self.stmt
.expect("Arrow iterator always holds a statement")
.stmt
.schema()
}
}
#[allow(clippy::needless_lifetimes)]
impl<'stmt> Iterator for Arrow<'stmt> {
type Item = RecordBatch;
fn next(&mut self) -> Option<Self::Item> {
match self.stmt?.step() {
Ok(Some(array)) => Some(RecordBatch::from(&array)),
Ok(None) => None,
Err(err) => panic!("Failed to fetch Arrow record batch: {err}"),
}
}
}
#[must_use = "Arrow stream is lazy and will not fetch data unless consumed"]
pub struct ArrowStream<'stmt> {
stmt: &'stmt Statement<'stmt>,
done: bool,
}
#[allow(clippy::needless_lifetimes)]
impl<'stmt> ArrowStream<'stmt> {
#[inline]
pub(crate) fn new(stmt: &'stmt Statement<'stmt>) -> Self {
ArrowStream { stmt, done: false }
}
#[inline]
pub fn get_schema(&self) -> SchemaRef {
self.stmt.stmt.schema()
}
}
#[allow(clippy::needless_lifetimes)]
impl<'stmt> Iterator for ArrowStream<'stmt> {
type Item = Result<RecordBatch>;
fn next(&mut self) -> Option<Self::Item> {
if self.done {
return None;
}
match self.stmt.step() {
Ok(Some(array)) => Some(Ok(RecordBatch::from(&array))),
Ok(None) => {
self.done = true;
None
}
Err(e) => {
self.done = true;
Some(Err(e))
}
}
}
}
impl FusedIterator for ArrowStream<'_> {}