use arrow_array::{ArrayRef, RecordBatch};
use arrow_schema::{DataType, Schema};
use std::sync::Arc;
use crate::column_store::ColumnStore;
use crate::datatable::ColumnPtrs;
#[derive(Debug, Clone)]
pub struct ArrowStore {
batch: RecordBatch,
column_ptrs: Vec<ColumnPtrs>,
}
impl ArrowStore {
pub fn new(batch: RecordBatch) -> Self {
let column_ptrs = (0..batch.num_columns())
.map(|i| ColumnPtrs::from_array(batch.column(i)))
.collect();
Self { batch, column_ptrs }
}
#[inline]
pub fn batch(&self) -> &RecordBatch {
&self.batch
}
#[inline]
pub fn into_batch(self) -> RecordBatch {
self.batch
}
#[inline]
pub fn column(&self, index: usize) -> Option<&ArrayRef> {
self.batch.columns().get(index)
}
#[inline]
pub fn column_ptr(&self, index: usize) -> Option<&ColumnPtrs> {
self.column_ptrs.get(index)
}
#[inline]
pub fn column_ptrs(&self) -> &[ColumnPtrs] {
&self.column_ptrs
}
}
impl ColumnStore for ArrowStore {
fn row_count(&self) -> usize {
self.batch.num_rows()
}
fn column_count(&self) -> usize {
self.batch.num_columns()
}
fn schema(&self) -> Arc<Schema> {
self.batch.schema()
}
fn column_names(&self) -> Vec<String> {
self.batch
.schema()
.fields()
.iter()
.map(|f| f.name().clone())
.collect()
}
fn data_type(&self, index: usize) -> Option<DataType> {
self.batch
.schema()
.fields()
.get(index)
.map(|f| f.data_type().clone())
}
}