use polars_error::{PolarsResult, polars_ensure};
use crate::array::{Array, ArrayRef};
use crate::datatypes::{ArrowSchema, ArrowSchemaRef};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecordBatchT<A: AsRef<dyn Array>> {
height: usize,
schema: ArrowSchemaRef,
arrays: Vec<A>,
}
pub type RecordBatch = RecordBatchT<ArrayRef>;
impl<A: AsRef<dyn Array>> RecordBatchT<A> {
pub fn new(length: usize, schema: ArrowSchemaRef, arrays: Vec<A>) -> Self {
Self::try_new(length, schema, arrays).unwrap()
}
pub fn try_new(height: usize, schema: ArrowSchemaRef, arrays: Vec<A>) -> PolarsResult<Self> {
polars_ensure!(
schema.len() == arrays.len(),
ComputeError: "RecordBatch requires an equal number of fields and arrays",
);
polars_ensure!(
arrays.iter().all(|arr| arr.as_ref().len() == height),
ComputeError: "RecordBatch requires all its arrays to have an equal number of rows",
);
Ok(Self {
height,
schema,
arrays,
})
}
pub fn arrays(&self) -> &[A] {
&self.arrays
}
pub fn schema(&self) -> &ArrowSchema {
&self.schema
}
pub fn columns(&self) -> &[A] {
&self.arrays
}
pub fn len(&self) -> usize {
self.height
}
pub fn height(&self) -> usize {
self.height
}
pub fn width(&self) -> usize {
self.arrays.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn into_arrays(self) -> Vec<A> {
self.arrays
}
pub fn into_schema_and_arrays(self) -> (ArrowSchemaRef, Vec<A>) {
(self.schema, self.arrays)
}
}
impl<A: AsRef<dyn Array>> From<RecordBatchT<A>> for Vec<A> {
fn from(c: RecordBatchT<A>) -> Self {
c.into_arrays()
}
}
impl<A: AsRef<dyn Array>> std::ops::Deref for RecordBatchT<A> {
type Target = [A];
#[inline]
fn deref(&self) -> &[A] {
self.arrays()
}
}