|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_compute::logical::LogicalNot; |
| 5 | +use vortex_dtype::DType; |
| 6 | +use vortex_error::VortexResult; |
| 7 | +use vortex_error::vortex_bail; |
| 8 | +use vortex_vector::Datum; |
| 9 | +use vortex_vector::Scalar; |
| 10 | +use vortex_vector::Vector; |
| 11 | +use vortex_vector::bool::BoolScalar; |
| 12 | + |
| 13 | +use crate::expr::ChildName; |
| 14 | +use crate::expr::functions::ArgName; |
| 15 | +use crate::expr::functions::Arity; |
| 16 | +use crate::expr::functions::EmptyOptions; |
| 17 | +use crate::expr::functions::ExecutionArgs; |
| 18 | +use crate::expr::functions::FunctionId; |
| 19 | +use crate::expr::functions::NullHandling; |
| 20 | +use crate::expr::functions::VTable; |
| 21 | + |
| 22 | +pub struct NotFn; |
| 23 | +impl VTable for NotFn { |
| 24 | + type Options = EmptyOptions; |
| 25 | + |
| 26 | + fn id(&self) -> FunctionId { |
| 27 | + FunctionId::from("vortex.not") |
| 28 | + } |
| 29 | + |
| 30 | + fn arity(&self, _: &Self::Options) -> Arity { |
| 31 | + Arity::Fixed(1) |
| 32 | + } |
| 33 | + |
| 34 | + fn null_handling(&self, _options: &Self::Options) -> NullHandling { |
| 35 | + NullHandling::Propagate |
| 36 | + } |
| 37 | + |
| 38 | + fn arg_name(&self, _: &Self::Options, arg_idx: usize) -> ArgName { |
| 39 | + match arg_idx { |
| 40 | + 0 => ChildName::from("input"), |
| 41 | + _ => unreachable!("Invalid child index {} for Not expression", arg_idx), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + fn return_dtype(&self, _options: &Self::Options, arg_types: &[DType]) -> VortexResult<DType> { |
| 46 | + let child_dtype = &arg_types[0]; |
| 47 | + if !matches!(child_dtype, DType::Bool(_)) { |
| 48 | + vortex_bail!( |
| 49 | + "Not expression expects a boolean child, got: {}", |
| 50 | + child_dtype |
| 51 | + ); |
| 52 | + } |
| 53 | + Ok(child_dtype.clone()) |
| 54 | + } |
| 55 | + |
| 56 | + fn execute(&self, _: &Self::Options, args: &ExecutionArgs) -> VortexResult<Datum> { |
| 57 | + Ok(match args.input_datums(0) { |
| 58 | + Datum::Scalar(Scalar::Bool(sc)) => { |
| 59 | + Datum::Scalar(BoolScalar::new(sc.value().map(|v| !v)).into()) |
| 60 | + } |
| 61 | + Datum::Vector(Vector::Bool(vec)) => Datum::Vector(vec.clone().not().into()), |
| 62 | + _ => unreachable!("Not expects a boolean"), |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
0 commit comments