Skip to main content

vortex_functions/
is_null.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::slice;
5
6use vortex_array::ArrayRef;
7use vortex_array::ExecutionCtx;
8use vortex_array::IntoArray;
9use vortex_array::arrays::ConstantArray;
10use vortex_array::arrays::ScalarFnArray;
11use vortex_array::builtins::ArrayBuiltins;
12use vortex_array::dtype::DType;
13use vortex_array::dtype::Nullability;
14use vortex_array::scalar_fn::Arity;
15use vortex_array::scalar_fn::ChildName;
16use vortex_array::scalar_fn::EmptyOptions;
17use vortex_array::scalar_fn::ExecutionArgs;
18use vortex_array::scalar_fn::ReduceNode;
19use vortex_array::scalar_fn::ScalarFnId;
20use vortex_array::scalar_fn::ScalarFnVTable;
21use vortex_array::scalar_fn::ScalarFnVTableExt;
22use vortex_array::scalar_fn::builtin::is_not_null::reduce_null_check;
23use vortex_array::validity::Validity;
24use vortex_error::VortexExpect as _;
25use vortex_error::VortexResult;
26use vortex_session::VortexSession;
27use vortex_session::registry::CachedId;
28
29/// Expression that checks for null values.
30#[derive(Clone)]
31pub struct IsNull;
32
33impl IsNull {
34    /// Creates a lazy null check over `input`.
35    #[expect(clippy::new_ret_no_self, reason = "constructs the lazy result array")]
36    pub fn new(input: ArrayRef) -> ScalarFnArray {
37        ScalarFnArray::try_new(IsNull.bind(EmptyOptions), [input])
38            .vortex_expect("IsNull has one child and an infallible return dtype")
39    }
40}
41
42impl ScalarFnVTable for IsNull {
43    type Options = EmptyOptions;
44
45    fn id(&self) -> ScalarFnId {
46        static ID: CachedId = CachedId::new("vortex.is_null");
47        *ID
48    }
49
50    fn serialize(&self, _instance: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
51        Ok(Some(vec![]))
52    }
53
54    fn deserialize(
55        &self,
56        _metadata: &[u8],
57        _session: &VortexSession,
58    ) -> VortexResult<Self::Options> {
59        Ok(EmptyOptions)
60    }
61
62    fn arity(&self, _options: &Self::Options) -> Arity {
63        Arity::Exact(1)
64    }
65
66    fn child_name(&self, _instance: &Self::Options, child_idx: usize) -> ChildName {
67        match child_idx {
68            0 => ChildName::from("input"),
69            _ => unreachable!("Invalid child index {} for IsNull expression", child_idx),
70        }
71    }
72
73    fn return_dtype(&self, _options: &Self::Options, _arg_dtypes: &[DType]) -> VortexResult<DType> {
74        Ok(DType::Bool(Nullability::NonNullable))
75    }
76
77    fn execute(
78        &self,
79        _data: &Self::Options,
80        args: &dyn ExecutionArgs,
81        _ctx: &mut ExecutionCtx,
82    ) -> VortexResult<ArrayRef> {
83        let child = args.get(0)?;
84        if let Some(scalar) = child.as_constant() {
85            return Ok(ConstantArray::new(scalar.is_null(), args.row_count()).into_array());
86        }
87
88        match child.validity()? {
89            Validity::NonNullable | Validity::AllValid => {
90                Ok(ConstantArray::new(false, args.row_count()).into_array())
91            }
92            Validity::AllInvalid => Ok(ConstantArray::new(true, args.row_count()).into_array()),
93            Validity::Array(a) => a.not(),
94        }
95    }
96
97    fn reduce<T: ReduceNode>(&self, _options: &Self::Options, node: &T) -> VortexResult<Option<T>> {
98        reduce_null_check(node, true, |value| {
99            value.new_node(IsNull.bind(EmptyOptions), slice::from_ref(value))
100        })
101    }
102
103    fn is_strict(&self, _instance: &Self::Options) -> bool {
104        // Null input produces the non-null boolean value `true`.
105        false
106    }
107
108    fn is_infallible(&self, _instance: &Self::Options) -> bool {
109        true
110    }
111}