use std::collections::HashSet;
use crate::error::{binding, BindingResult};
use crate::input::sealed::{self, AxisKind};
use crate::{AdapterError, ArraySource, AxisValues, CategoryValues, NumericValues};
pub struct ColumnProjection<'columns> {
source_shape: (usize, usize),
columns: &'columns [(usize, &'columns dyn ArraySource)],
identities: Option<&'columns [(usize, &'columns str)]>,
}
impl<'columns> ColumnProjection<'columns> {
pub fn new(
source_shape: (usize, usize),
columns: &'columns [(usize, &'columns dyn ArraySource)],
) -> std::result::Result<Self, AdapterError> {
let projection = Self {
source_shape,
columns,
identities: None,
};
projection.validate()?;
Ok(projection)
}
pub fn from_identities(
source_shape: (usize, usize),
identities: &'columns [(usize, &'columns str)],
) -> std::result::Result<Self, AdapterError> {
let projection = Self {
source_shape,
columns: &[],
identities: Some(identities),
};
projection.validate()?;
Ok(projection)
}
pub fn with_identities(
mut self,
identities: &'columns [(usize, &'columns str)],
) -> std::result::Result<Self, AdapterError> {
self.validate_identities(identities)?;
self.identities = Some(identities);
Ok(self)
}
fn validate_identities(&self, identities: &[(usize, &str)]) -> BindingResult<()> {
let mut indices = HashSet::new();
let mut names = HashSet::new();
indices
.try_reserve(identities.len())
.map_err(|source| AdapterError::Allocation {
operation: "column identities",
source,
})?;
names
.try_reserve(identities.len())
.map_err(|source| AdapterError::Allocation {
operation: "column identities",
source,
})?;
for &(index, identity) in identities {
crate::source::lookup(Some(self), index, "column identities")?;
if !indices.insert(index) {
return Err(binding(
"column identities",
format!("duplicate logical column {index}"),
));
}
if !names.insert(identity) {
return Err(binding(
"column identities",
format!("duplicate raw identity {identity:?}"),
));
}
}
for &(index, _) in self.columns {
if !indices.contains(&index) {
return Err(binding(
"column identities",
format!("no raw identity supplied for logical column {index}"),
));
}
}
Ok(())
}
fn column(&self, index: usize, role: &'static str) -> BindingResult<&dyn ArraySource> {
crate::source::lookup(Some(self), index, role)?;
self.columns
.iter()
.find(|(column, _)| *column == index)
.map(|(_, source)| *source)
.ok_or_else(|| binding(role, format!("logical column {index} was not projected")))
}
}
fn original_column(error: AdapterError, index: usize) -> AdapterError {
match error {
AdapterError::UnsupportedElement { role, element, .. } => {
AdapterError::UnsupportedElement {
role,
element,
column: Some(index),
}
}
AdapterError::SourceRow { slot, value, .. } => AdapterError::SourceRow {
slot,
value,
column: Some(index),
},
other => other,
}
}
impl sealed::Sealed for ColumnProjection<'_> {
fn column_identities(&self) -> Option<&[(usize, &str)]> {
self.identities
}
fn axis_kind(&self, index: usize, role: &'static str) -> BindingResult<AxisKind> {
sealed::Sealed::axis_kind(self.column(index, role)?, 0, role)
.map_err(|error| original_column(error, index))
}
}
macro_rules! projected_column {
($method:ident, $ty:ty) => {
fn $method(&self, index: usize, role: &'static str) -> BindingResult<Vec<$ty>> {
self.column(index, role)?
.$method(0, role)
.map_err(|error| original_column(error, index))
}
};
}
impl ArraySource for ColumnProjection<'_> {
fn validate(&self) -> BindingResult<()> {
if self.columns.is_empty() && self.identities.is_none() {
return Err(binding(
"column projection",
"supply at least one value column",
));
}
let mut indices = HashSet::new();
indices
.try_reserve(self.columns.len())
.map_err(|source| AdapterError::Allocation {
operation: "column projection",
source,
})?;
for &(index, source) in self.columns {
crate::source::lookup(Some(self), index, "column projection")?;
if !indices.insert(index) {
return Err(binding(
"column projection",
format!("duplicate logical column {index}"),
));
}
source.validate()?;
let actual = source.shape();
if actual != (self.source_shape.0, 1) {
return Err(AdapterError::Shape {
operation: "column projection",
expected: vec![self.source_shape.0, 1],
actual: vec![actual.0, actual.1],
});
}
}
if let Some(identities) = self.identities {
self.validate_identities(identities)?;
}
Ok(())
}
fn shape(&self) -> (usize, usize) {
self.source_shape
}
projected_column!(numeric_column, Option<f64>);
projected_column!(category_column, Option<String>);
projected_column!(boolean_column, Option<bool>);
projected_column!(id_column, usize);
projected_column!(byte_column, u8);
fn axis_column(&self, index: usize, role: &'static str) -> BindingResult<AxisValues> {
match sealed::Sealed::axis_kind(self, index, role)? {
AxisKind::Numeric => {
let values = self.numeric_column(index, role)?;
Ok(AxisValues::Numeric(NumericValues::nullable(
crate::source::identity(Some(self), index, role)?,
values,
)))
}
AxisKind::Category => {
let values = self.category_column(index, role)?;
Ok(AxisValues::Category(CategoryValues::nullable(
crate::source::identity(Some(self), index, role)?,
values,
)?))
}
}
}
}