use std::sync::Arc;
use polars_error::{polars_bail, PolarsResult};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{Field, Metadata};
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ArrowSchema {
pub fields: Vec<Field>,
pub metadata: Metadata,
}
pub type ArrowSchemaRef = Arc<ArrowSchema>;
impl ArrowSchema {
#[inline]
pub fn with_metadata(self, metadata: Metadata) -> Self {
Self {
fields: self.fields,
metadata,
}
}
#[inline]
pub fn len(&self) -> usize {
self.fields.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.fields.is_empty()
}
pub fn filter<F: Fn(usize, &Field) -> bool>(self, predicate: F) -> Self {
let fields = self
.fields
.into_iter()
.enumerate()
.filter_map(|(index, f)| {
if (predicate)(index, &f) {
Some(f)
} else {
None
}
})
.collect();
ArrowSchema {
fields,
metadata: self.metadata,
}
}
pub fn try_project(&self, indices: &[usize]) -> PolarsResult<Self> {
let fields = indices.iter().map(|&i| {
let Some(out) = self.fields.get(i) else {
polars_bail!(
SchemaFieldNotFound: "projection index {} is out of bounds for schema of length {}",
i, self.fields.len()
);
};
Ok(out.clone())
}).collect::<PolarsResult<Vec<_>>>()?;
Ok(ArrowSchema {
fields,
metadata: self.metadata.clone(),
})
}
}
impl From<Vec<Field>> for ArrowSchema {
fn from(fields: Vec<Field>) -> Self {
Self {
fields,
..Default::default()
}
}
}