vortex-array 0.94.0

Vortex in memory columnar data format
Documentation
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! A collection of built-in common scalar functions.
//!
//! It is expected that each Vortex integration may provide its own set of scalar functions with
//! semantics that exactly match the underlying system (e.g. SQL engine, DataFrame library, etc).
//!
//! This set of functions should cover the basics, and in general leans towards the semantics of
//! the equivalent Arrow compute function.

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;

/// A collection of built-in scalar functions that can be applied to expressions or arrays.
pub trait ArrayBuiltins: Sized {
    /// Cast to the given data type.
    fn cast(&self, dtype: DType) -> VortexResult<ArrayRef>;

    /// Replace null values with the given fill value.
    fn fill_null(&self, fill_value: impl Into<Scalar>) -> VortexResult<ArrayRef>;

    /// Get item by field name (for struct types).
    fn get_item(&self, field_name: impl Into<FieldName>) -> VortexResult<ArrayRef>;

    /// Is not null check.
    fn is_not_null(&self) -> VortexResult<ArrayRef>;

    /// Mask the array using the given boolean mask.
    /// The resulting array's validity is the intersection of the original array's validity
    /// and the mask's validity.
    fn mask(self, mask: ArrayRef) -> VortexResult<ArrayRef>;

    /// Boolean negation.
    fn not(&self) -> VortexResult<ArrayRef>;

    /// Random-access gather by `(array_index, row_index)`: output row `i` is taken from
    /// `values[array_indices[i]][row_indices[i]]`, where `self` is the (non-nullable)
    /// `array_indices` selector and `row_indices` names the position within the selected value.
    /// See [`InterleaveArray`].
    fn interleave(
        &self,
        values: impl IntoIterator<Item = ArrayRef>,
        row_indices: ArrayRef,
    ) -> VortexResult<ArrayRef>;

    /// Compare this array with another.
    fn compare(&self, rhs: ArrayRef, op: CompareOperator) -> VortexResult<ArrayRef>;

    /// Apply a Kleene boolean operator to this array and another.
    fn boolean(&self, rhs: ArrayRef, op: BooleanOperator) -> VortexResult<ArrayRef>;

    /// Apply an arithmetic operator to this array and another.
    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())
    }
}