use crate::error::{CoreError, ErrorContext};
use std::fmt;
#[derive(Debug)]
pub enum ArrowCompatError {
TypeMismatch { expected: String, actual: String },
ShapeMismatch {
expected: Vec<usize>,
actual: Vec<usize>,
},
ColumnOutOfBounds { index: usize, num_columns: usize },
ColumnNotFound { name: String },
NullValuesPresent {
null_count: usize,
total_count: usize,
},
ArrowError(arrow::error::ArrowError),
IoError(std::io::Error),
SchemaError(String),
ZeroCopyNotPossible(String),
InconsistentColumnLengths {
expected_len: usize,
column_index: usize,
column_len: usize,
},
}
impl fmt::Display for ArrowCompatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TypeMismatch { expected, actual } => {
write!(
f,
"Arrow type mismatch: expected {}, got {}",
expected, actual
)
}
Self::ShapeMismatch { expected, actual } => {
write!(
f,
"Shape mismatch: expected {:?}, got {:?}",
expected, actual
)
}
Self::ColumnOutOfBounds { index, num_columns } => {
write!(
f,
"Column index {} out of bounds (num_columns={})",
index, num_columns
)
}
Self::ColumnNotFound { name } => {
write!(f, "Column '{}' not found in RecordBatch", name)
}
Self::NullValuesPresent {
null_count,
total_count,
} => {
write!(
f,
"Found {} null values out of {} total (use nullable conversion instead)",
null_count, total_count
)
}
Self::ArrowError(e) => write!(f, "Arrow error: {}", e),
Self::IoError(e) => write!(f, "I/O error: {}", e),
Self::SchemaError(msg) => write!(f, "Schema error: {}", msg),
Self::ZeroCopyNotPossible(reason) => {
write!(f, "Zero-copy not possible: {}", reason)
}
Self::InconsistentColumnLengths {
expected_len,
column_index,
column_len,
} => {
write!(
f,
"Inconsistent column lengths: expected {} rows, column {} has {} rows",
expected_len, column_index, column_len
)
}
}
}
}
impl std::error::Error for ArrowCompatError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::ArrowError(e) => Some(e),
Self::IoError(e) => Some(e),
_ => None,
}
}
}
impl From<arrow::error::ArrowError> for ArrowCompatError {
fn from(e: arrow::error::ArrowError) -> Self {
Self::ArrowError(e)
}
}
impl From<std::io::Error> for ArrowCompatError {
fn from(e: std::io::Error) -> Self {
Self::IoError(e)
}
}
impl From<ArrowCompatError> for CoreError {
fn from(e: ArrowCompatError) -> Self {
CoreError::ComputationError(ErrorContext::new(format!("Arrow interop: {}", e)))
}
}
pub type ArrowResult<T> = Result<T, ArrowCompatError>;