use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::IntoArray;
use crate::arrays::ConstantArray;
use crate::arrays::InterleaveArray;
use crate::dtype::DType;
use crate::dtype::FieldName;
use crate::scalar::NumericOperator;
use crate::scalar::Scalar;
use crate::scalar_fn::builtin::boolean::Boolean;
use crate::scalar_fn::builtin::cast::Cast;
use crate::scalar_fn::builtin::compare::Compare;
use crate::scalar_fn::builtin::fill_null::FillNull;
use crate::scalar_fn::builtin::get_item::GetItem;
use crate::scalar_fn::builtin::is_not_null::IsNotNull;
use crate::scalar_fn::builtin::mask::Mask;
use crate::scalar_fn::builtin::not::Not;
use crate::scalar_fn::builtin::numeric::Numeric;
use crate::scalar_fn::builtin::operators::BooleanOperator;
use crate::scalar_fn::builtin::operators::CompareOperator;
pub trait ArrayBuiltins: Sized {
fn cast(&self, dtype: DType) -> VortexResult<ArrayRef>;
fn fill_null(&self, fill_value: impl Into<Scalar>) -> VortexResult<ArrayRef>;
fn get_item(&self, field_name: impl Into<FieldName>) -> VortexResult<ArrayRef>;
fn is_not_null(&self) -> VortexResult<ArrayRef>;
fn mask(self, mask: ArrayRef) -> VortexResult<ArrayRef>;
fn not(&self) -> VortexResult<ArrayRef>;
fn interleave(
&self,
values: impl IntoIterator<Item = ArrayRef>,
row_indices: ArrayRef,
) -> VortexResult<ArrayRef>;
fn compare(&self, rhs: ArrayRef, op: CompareOperator) -> VortexResult<ArrayRef>;
fn boolean(&self, rhs: ArrayRef, op: BooleanOperator) -> VortexResult<ArrayRef>;
fn numeric(&self, rhs: ArrayRef, op: NumericOperator) -> VortexResult<ArrayRef>;
}
impl ArrayBuiltins for ArrayRef {
fn cast(&self, dtype: DType) -> VortexResult<ArrayRef> {
if self.dtype() == &dtype {
return Ok(self.clone());
}
Ok(Cast::try_new(self.clone(), dtype)?.into_array())
}
fn fill_null(&self, fill_value: impl Into<Scalar>) -> VortexResult<ArrayRef> {
let fill_value = fill_value.into();
if !self.dtype().is_nullable() {
return self.cast(FillNull::output_dtype(self.dtype(), fill_value.dtype())?);
}
Ok(FillNull::try_new(
self.clone(),
ConstantArray::new(fill_value, self.len()).into_array(),
)?
.into_array())
}
fn get_item(&self, field_name: impl Into<FieldName>) -> VortexResult<ArrayRef> {
Ok(GetItem::try_new(self.clone(), field_name)?.into_array())
}
fn is_not_null(&self) -> VortexResult<ArrayRef> {
Ok(IsNotNull::new(self.clone()).into_array())
}
fn mask(self, mask: ArrayRef) -> VortexResult<ArrayRef> {
Ok(Mask::try_new(self, mask)?.into_array())
}
fn not(&self) -> VortexResult<ArrayRef> {
Ok(Not::try_new(self.clone())?.into_array())
}
fn interleave(
&self,
values: impl IntoIterator<Item = ArrayRef>,
row_indices: ArrayRef,
) -> VortexResult<ArrayRef> {
Ok(
InterleaveArray::try_new(values.into_iter().collect(), self.clone(), row_indices)?
.into_array(),
)
}
fn compare(&self, rhs: ArrayRef, op: CompareOperator) -> VortexResult<ArrayRef> {
Ok(Compare::try_new(self.clone(), rhs, op)?.into_array())
}
fn boolean(&self, rhs: ArrayRef, op: BooleanOperator) -> VortexResult<ArrayRef> {
Ok(Boolean::try_new(self.clone(), rhs, op)?.into_array())
}
fn numeric(&self, rhs: ArrayRef, op: NumericOperator) -> VortexResult<ArrayRef> {
Ok(Numeric::try_new(self.clone(), rhs, op)?.into_array())
}
}