use vortex_array::dtype::DType;
use vortex_array::dtype::FieldName;
use vortex_array::dtype::Nullability;
use vortex_array::scalar::NumericOperator;
use vortex_array::scalar::Scalar;
use vortex_array::scalar_fn::EmptyOptions;
use vortex_array::scalar_fn::builtin::boolean::Boolean;
use vortex_array::scalar_fn::builtin::cast::Cast;
use vortex_array::scalar_fn::builtin::compare::Compare;
use vortex_array::scalar_fn::builtin::fill_null::FillNull;
use vortex_array::scalar_fn::builtin::get_item::GetItem;
use vortex_array::scalar_fn::builtin::is_not_null::IsNotNull;
use vortex_array::scalar_fn::builtin::literal::Literal;
use vortex_array::scalar_fn::builtin::mask::Mask;
use vortex_array::scalar_fn::builtin::not::Not;
use vortex_array::scalar_fn::builtin::numeric::Numeric;
use vortex_array::scalar_fn::builtin::operators::BinaryOperator;
use vortex_array::scalar_fn::builtin::operators::BooleanOperator;
use vortex_array::scalar_fn::builtin::operators::CompareOperator;
use vortex_array::scalar_fn::builtin::pack::Pack;
use vortex_array::scalar_fn::builtin::pack::PackOptions;
use vortex_error::VortexExpect;
use vortex_utils::iter::ReduceBalancedIterExt;
use crate::BoundExpression;
use crate::Expression;
use crate::ScalarFnExprExt;
pub fn root() -> Expression {
Expression::Root
}
pub fn bound_root(dtype: DType) -> BoundExpression {
BoundExpression::new_root(dtype)
}
pub fn is_root(expr: &Expression) -> bool {
expr.is_root()
}
pub fn lit(value: impl Into<Scalar>) -> Expression {
Literal.new_expr(value.into(), [])
}
pub fn bound_lit(value: impl Into<Scalar>) -> BoundExpression {
Literal
.try_new_bound_expr(value.into(), [])
.vortex_expect("literal expressions are always well-typed")
}
pub fn col(field: impl Into<FieldName>) -> Expression {
GetItem.new_expr(field.into(), vec![root()])
}
pub fn bound_col(field: impl Into<FieldName>, scope: DType) -> BoundExpression {
bound_get_item(field, bound_root(scope))
}
pub fn get_item(field: impl Into<FieldName>, child: Expression) -> Expression {
GetItem.new_expr(field.into(), vec![child])
}
pub fn bound_get_item(field: impl Into<FieldName>, child: BoundExpression) -> BoundExpression {
GetItem
.try_new_bound_expr(field.into(), [child])
.vortex_expect("get-item expressions must reference a field in the child dtype")
}
pub fn var(name: impl Into<FieldName>) -> Expression {
Expression::Variable(name.into())
}
pub fn compare(operator: CompareOperator, lhs: Expression, rhs: Expression) -> Expression {
Compare
.try_new_expr(operator, [lhs, rhs])
.vortex_expect("Failed to create compare expression")
}
pub fn bound_compare(
operator: CompareOperator,
lhs: BoundExpression,
rhs: BoundExpression,
) -> BoundExpression {
Compare
.try_new_bound_expr(operator, [lhs, rhs])
.vortex_expect("compare expressions must have compatible operand dtypes")
}
pub fn boolean(operator: BooleanOperator, lhs: Expression, rhs: Expression) -> Expression {
Boolean
.try_new_expr(operator, [lhs, rhs])
.vortex_expect("Failed to create boolean expression")
}
pub fn bound_boolean(
operator: BooleanOperator,
lhs: BoundExpression,
rhs: BoundExpression,
) -> BoundExpression {
Boolean
.try_new_bound_expr(operator, [lhs, rhs])
.vortex_expect("boolean expressions must have compatible operand dtypes")
}
pub fn numeric(operator: NumericOperator, lhs: Expression, rhs: Expression) -> Expression {
Numeric
.try_new_expr(operator, [lhs, rhs])
.vortex_expect("Failed to create numeric expression")
}
pub fn bound_numeric(
operator: NumericOperator,
lhs: BoundExpression,
rhs: BoundExpression,
) -> BoundExpression {
Numeric
.try_new_bound_expr(operator, [lhs, rhs])
.vortex_expect("numeric expressions must have compatible operand dtypes")
}
pub fn binary(operator: impl Into<BinaryOperator>, lhs: Expression, rhs: Expression) -> Expression {
match operator.into() {
BinaryOperator::Compare(operator) => compare(operator, lhs, rhs),
BinaryOperator::Boolean(operator) => boolean(operator, lhs, rhs),
BinaryOperator::Numeric(operator) => numeric(operator, lhs, rhs),
}
}
pub fn eq(lhs: Expression, rhs: Expression) -> Expression {
Compare
.try_new_expr(CompareOperator::Eq, [lhs, rhs])
.vortex_expect("Failed to create Eq comparison expression")
}
pub fn bound_eq(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
bound_compare(CompareOperator::Eq, lhs, rhs)
}
pub fn not_eq(lhs: Expression, rhs: Expression) -> Expression {
Compare
.try_new_expr(CompareOperator::NotEq, [lhs, rhs])
.vortex_expect("Failed to create NotEq comparison expression")
}
pub fn bound_not_eq(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
bound_compare(CompareOperator::NotEq, lhs, rhs)
}
pub fn gt_eq(lhs: Expression, rhs: Expression) -> Expression {
Compare
.try_new_expr(CompareOperator::Gte, [lhs, rhs])
.vortex_expect("Failed to create Gte comparison expression")
}
pub fn bound_gt_eq(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
bound_compare(CompareOperator::Gte, lhs, rhs)
}
pub fn gt(lhs: Expression, rhs: Expression) -> Expression {
Compare
.try_new_expr(CompareOperator::Gt, [lhs, rhs])
.vortex_expect("Failed to create Gt comparison expression")
}
pub fn bound_gt(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
bound_compare(CompareOperator::Gt, lhs, rhs)
}
pub fn lt_eq(lhs: Expression, rhs: Expression) -> Expression {
Compare
.try_new_expr(CompareOperator::Lte, [lhs, rhs])
.vortex_expect("Failed to create Lte comparison expression")
}
pub fn bound_lt_eq(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
bound_compare(CompareOperator::Lte, lhs, rhs)
}
pub fn lt(lhs: Expression, rhs: Expression) -> Expression {
Compare
.try_new_expr(CompareOperator::Lt, [lhs, rhs])
.vortex_expect("Failed to create Lt comparison expression")
}
pub fn bound_lt(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
bound_compare(CompareOperator::Lt, lhs, rhs)
}
pub fn or(lhs: Expression, rhs: Expression) -> Expression {
Boolean
.try_new_expr(BooleanOperator::Or, [lhs, rhs])
.vortex_expect("Failed to create Or boolean expression")
}
pub fn bound_or(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
bound_boolean(BooleanOperator::Or, lhs, rhs)
}
pub fn or_collect<I>(iter: I) -> Option<Expression>
where
I: IntoIterator<Item = Expression>,
{
iter.into_iter().reduce_balanced(or)
}
pub fn bound_or_collect<I>(iter: I) -> Option<BoundExpression>
where
I: IntoIterator<Item = BoundExpression>,
{
iter.into_iter().reduce_balanced(bound_or)
}
pub fn and(lhs: Expression, rhs: Expression) -> Expression {
Boolean
.try_new_expr(BooleanOperator::And, [lhs, rhs])
.vortex_expect("Failed to create And boolean expression")
}
pub fn bound_and(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
bound_boolean(BooleanOperator::And, lhs, rhs)
}
pub fn and_collect<I>(iter: I) -> Option<Expression>
where
I: IntoIterator<Item = Expression>,
{
iter.into_iter().reduce_balanced(and)
}
pub fn bound_and_collect<I>(iter: I) -> Option<BoundExpression>
where
I: IntoIterator<Item = BoundExpression>,
{
iter.into_iter().reduce_balanced(bound_and)
}
pub fn checked_add(lhs: Expression, rhs: Expression) -> Expression {
Numeric
.try_new_expr(NumericOperator::Add, [lhs, rhs])
.vortex_expect("Failed to create Add arithmetic expression")
}
pub fn bound_checked_add(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
bound_numeric(NumericOperator::Add, lhs, rhs)
}
pub fn not(operand: Expression) -> Expression {
Not.new_expr(EmptyOptions, vec![operand])
}
pub fn bound_not(operand: BoundExpression) -> BoundExpression {
Not.try_new_bound_expr(EmptyOptions, [operand])
.vortex_expect("not expressions require a boolean operand")
}
pub fn pack(
elements: impl IntoIterator<Item = (impl Into<FieldName>, Expression)>,
nullability: Nullability,
) -> Expression {
let (names, values): (Vec<_>, Vec<_>) = elements
.into_iter()
.map(|(name, value)| (name.into(), value))
.unzip();
Pack.new_expr(
PackOptions {
names: names.into(),
nullability,
},
values,
)
}
pub fn bound_pack(
elements: impl IntoIterator<Item = (impl Into<FieldName>, BoundExpression)>,
nullability: Nullability,
) -> BoundExpression {
let (names, values): (Vec<_>, Vec<_>) = elements
.into_iter()
.map(|(name, value)| (name.into(), value))
.unzip();
Pack.try_new_bound_expr(
PackOptions {
names: names.into(),
nullability,
},
values,
)
.vortex_expect("pack expressions must have one name per child")
}
pub fn cast(child: Expression, target: DType) -> Expression {
Cast.try_new_expr(target, [child])
.vortex_expect("Failed to create Cast expression")
}
pub fn bound_cast(child: BoundExpression, target: DType) -> BoundExpression {
Cast.try_new_bound_expr(target, [child])
.vortex_expect("cast expressions require a supported source and target dtype")
}
pub fn fill_null(child: Expression, fill_value: Expression) -> Expression {
FillNull.new_expr(EmptyOptions, [child, fill_value])
}
pub fn bound_fill_null(child: BoundExpression, fill_value: BoundExpression) -> BoundExpression {
FillNull
.try_new_bound_expr(EmptyOptions, [child, fill_value])
.vortex_expect("fill-null expressions require compatible child and fill dtypes")
}
pub fn is_not_null(child: Expression) -> Expression {
IsNotNull.new_expr(EmptyOptions, vec![child])
}
pub fn bound_is_not_null(child: BoundExpression) -> BoundExpression {
IsNotNull
.try_new_bound_expr(EmptyOptions, [child])
.vortex_expect("is-not-null expressions are always well-typed")
}
pub fn mask(array: Expression, mask: Expression) -> Expression {
Mask.new_expr(EmptyOptions, [array, mask])
}
pub fn bound_mask(array: BoundExpression, mask: BoundExpression) -> BoundExpression {
Mask.try_new_bound_expr(EmptyOptions, [array, mask])
.vortex_expect("mask expressions require a boolean mask")
}
pub mod bound {
pub use super::bound_and as and;
pub use super::bound_and_collect as and_collect;
pub use super::bound_boolean as boolean;
pub use super::bound_cast as cast;
pub use super::bound_checked_add as checked_add;
pub use super::bound_col as col;
pub use super::bound_compare as compare;
pub use super::bound_eq as eq;
pub use super::bound_fill_null as fill_null;
pub use super::bound_get_item as get_item;
pub use super::bound_gt as gt;
pub use super::bound_gt_eq as gt_eq;
pub use super::bound_is_not_null as is_not_null;
pub use super::bound_lit as lit;
pub use super::bound_lt as lt;
pub use super::bound_lt_eq as lt_eq;
pub use super::bound_mask as mask;
pub use super::bound_not as not;
pub use super::bound_not_eq as not_eq;
pub use super::bound_numeric as numeric;
pub use super::bound_or as or;
pub use super::bound_or_collect as or_collect;
pub use super::bound_pack as pack;
pub use super::bound_root as root;
}