use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::dtype::DType;
use crate::dtype::extension::ExtDTypeRef;
use crate::stats::ArrayStats;
#[derive(Clone, Debug)]
pub struct ExtensionArray {
pub(super) dtype: DType,
pub(super) storage_array: ArrayRef,
pub(super) stats_set: ArrayStats,
}
impl ExtensionArray {
pub fn new(ext_dtype: ExtDTypeRef, storage_array: ArrayRef) -> Self {
Self::try_new(ext_dtype, storage_array).vortex_expect("Failed to create `ExtensionArray`")
}
pub fn try_new(ext_dtype: ExtDTypeRef, storage_array: ArrayRef) -> VortexResult<Self> {
assert_eq!(
ext_dtype.storage_dtype(),
storage_array.dtype(),
"ExtensionArray: storage_dtype must match storage array DType",
);
Ok(unsafe { Self::new_unchecked(ext_dtype, storage_array) })
}
pub unsafe fn new_unchecked(ext_dtype: ExtDTypeRef, storage_array: ArrayRef) -> Self {
debug_assert_eq!(
ext_dtype.storage_dtype(),
storage_array.dtype(),
"ExtensionArray: storage_dtype must match storage array DType",
);
Self {
dtype: DType::Extension(ext_dtype),
storage_array,
stats_set: ArrayStats::default(),
}
}
pub fn ext_dtype(&self) -> &ExtDTypeRef {
let DType::Extension(ext) = &self.dtype else {
unreachable!("ExtensionArray: dtype must be an ExtDType")
};
ext
}
pub fn storage_array(&self) -> &ArrayRef {
&self.storage_array
}
}