common_function/scalars/expression/
is_null.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt;
16use std::fmt::Display;
17use std::sync::Arc;
18
19use datafusion::arrow::compute::is_null;
20use datafusion::arrow::datatypes::DataType;
21use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, Signature, Volatility};
22
23use crate::function::{Function, extract_args};
24
25const NAME: &str = "isnull";
26
27/// The function to check whether an expression is NULL
28#[derive(Clone, Debug)]
29pub(crate) struct IsNullFunction {
30    signature: Signature,
31}
32
33impl Default for IsNullFunction {
34    fn default() -> Self {
35        Self {
36            signature: Signature::any(1, Volatility::Immutable),
37        }
38    }
39}
40
41impl Display for IsNullFunction {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        write!(f, "{}", NAME.to_ascii_uppercase())
44    }
45}
46
47impl Function for IsNullFunction {
48    fn name(&self) -> &str {
49        NAME
50    }
51
52    fn return_type(&self, _: &[DataType]) -> datafusion_common::Result<DataType> {
53        Ok(DataType::Boolean)
54    }
55
56    fn signature(&self) -> &Signature {
57        &self.signature
58    }
59
60    fn invoke_with_args(
61        &self,
62        args: ScalarFunctionArgs,
63    ) -> datafusion_common::Result<ColumnarValue> {
64        let [arg0] = extract_args(self.name(), &args)?;
65        let result = is_null(&arg0)?;
66
67        Ok(ColumnarValue::Array(Arc::new(result)))
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use std::sync::Arc;
74
75    use arrow_schema::Field;
76    use datafusion_common::arrow::array::{AsArray, BooleanArray, Float32Array};
77
78    use super::*;
79    #[test]
80    fn test_is_null_function() {
81        let is_null = IsNullFunction::default();
82        assert_eq!("isnull", is_null.name());
83        assert_eq!(DataType::Boolean, is_null.return_type(&[]).unwrap());
84        let values = vec![None, Some(3.0), None];
85
86        let result = is_null
87            .invoke_with_args(ScalarFunctionArgs {
88                args: vec![ColumnarValue::Array(Arc::new(Float32Array::from(values)))],
89                arg_fields: vec![],
90                number_rows: 3,
91                return_field: Arc::new(Field::new("", DataType::Boolean, false)),
92                config_options: Arc::new(Default::default()),
93            })
94            .unwrap();
95        let ColumnarValue::Array(result) = result else {
96            unreachable!()
97        };
98        let vector = result.as_boolean();
99        let expect = &BooleanArray::from(vec![true, false, true]);
100        assert_eq!(expect, vector);
101    }
102}