vortex_array/scalar_fns/not/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_compute::logical::LogicalNot;
5use vortex_dtype::DType;
6use vortex_error::VortexResult;
7use vortex_error::vortex_bail;
8use vortex_vector::Datum;
9use vortex_vector::Scalar;
10use vortex_vector::Vector;
11use vortex_vector::bool::BoolScalar;
12
13use crate::expr::ChildName;
14use crate::expr::functions::ArgName;
15use crate::expr::functions::Arity;
16use crate::expr::functions::EmptyOptions;
17use crate::expr::functions::ExecutionArgs;
18use crate::expr::functions::FunctionId;
19use crate::expr::functions::NullHandling;
20use crate::expr::functions::VTable;
21
22pub struct NotFn;
23impl 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::Exact(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}