datafusion_physical_expr/expressions/
is_null.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! IS NULL expression
19
20use std::hash::Hash;
21use std::{any::Any, sync::Arc};
22
23use crate::PhysicalExpr;
24use arrow::{
25    datatypes::{DataType, Schema},
26    record_batch::RecordBatch,
27};
28use datafusion_common::Result;
29use datafusion_common::ScalarValue;
30use datafusion_expr::ColumnarValue;
31
32/// IS NULL expression
33#[derive(Debug, Eq)]
34pub struct IsNullExpr {
35    /// Input expression
36    arg: Arc<dyn PhysicalExpr>,
37}
38
39// Manually derive PartialEq and Hash to work around https://github.com/rust-lang/rust/issues/78808
40impl PartialEq for IsNullExpr {
41    fn eq(&self, other: &Self) -> bool {
42        self.arg.eq(&other.arg)
43    }
44}
45
46impl Hash for IsNullExpr {
47    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
48        self.arg.hash(state);
49    }
50}
51
52impl IsNullExpr {
53    /// Create new not expression
54    pub fn new(arg: Arc<dyn PhysicalExpr>) -> Self {
55        Self { arg }
56    }
57
58    /// Get the input expression
59    pub fn arg(&self) -> &Arc<dyn PhysicalExpr> {
60        &self.arg
61    }
62}
63
64impl std::fmt::Display for IsNullExpr {
65    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
66        write!(f, "{} IS NULL", self.arg)
67    }
68}
69
70impl PhysicalExpr for IsNullExpr {
71    /// Return a reference to Any that can be used for downcasting
72    fn as_any(&self) -> &dyn Any {
73        self
74    }
75
76    fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
77        Ok(DataType::Boolean)
78    }
79
80    fn nullable(&self, _input_schema: &Schema) -> Result<bool> {
81        Ok(false)
82    }
83
84    fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
85        let arg = self.arg.evaluate(batch)?;
86        match arg {
87            ColumnarValue::Array(array) => Ok(ColumnarValue::Array(Arc::new(
88                arrow::compute::is_null(&array)?,
89            ))),
90            ColumnarValue::Scalar(scalar) => Ok(ColumnarValue::Scalar(
91                ScalarValue::Boolean(Some(scalar.is_null())),
92            )),
93        }
94    }
95
96    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
97        vec![&self.arg]
98    }
99
100    fn with_new_children(
101        self: Arc<Self>,
102        children: Vec<Arc<dyn PhysicalExpr>>,
103    ) -> Result<Arc<dyn PhysicalExpr>> {
104        Ok(Arc::new(IsNullExpr::new(Arc::clone(&children[0]))))
105    }
106}
107
108/// Create an IS NULL expression
109pub fn is_null(arg: Arc<dyn PhysicalExpr>) -> Result<Arc<dyn PhysicalExpr>> {
110    Ok(Arc::new(IsNullExpr::new(arg)))
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116    use crate::expressions::col;
117    use arrow::array::{
118        Array, BooleanArray, Float64Array, Int32Array, StringArray, UnionArray,
119    };
120    use arrow::buffer::ScalarBuffer;
121    use arrow::datatypes::*;
122    use datafusion_common::cast::as_boolean_array;
123
124    #[test]
125    fn is_null_op() -> Result<()> {
126        let schema = Schema::new(vec![Field::new("a", DataType::Utf8, true)]);
127        let a = StringArray::from(vec![Some("foo"), None]);
128
129        // expression: "a is null"
130        let expr = is_null(col("a", &schema)?).unwrap();
131        let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)])?;
132
133        let result = expr
134            .evaluate(&batch)?
135            .into_array(batch.num_rows())
136            .expect("Failed to convert to array");
137        let result =
138            as_boolean_array(&result).expect("failed to downcast to BooleanArray");
139
140        let expected = &BooleanArray::from(vec![false, true]);
141
142        assert_eq!(expected, result);
143
144        Ok(())
145    }
146
147    fn union_fields() -> UnionFields {
148        [
149            (0, Arc::new(Field::new("A", DataType::Int32, true))),
150            (1, Arc::new(Field::new("B", DataType::Float64, true))),
151            (2, Arc::new(Field::new("C", DataType::Utf8, true))),
152        ]
153        .into_iter()
154        .collect()
155    }
156
157    #[test]
158    fn sparse_union_is_null() {
159        // union of [{A=1}, {A=}, {B=1.1}, {B=1.2}, {B=}, {C=}, {C="a"}]
160        let int_array =
161            Int32Array::from(vec![Some(1), None, None, None, None, None, None]);
162        let float_array =
163            Float64Array::from(vec![None, None, Some(1.1), Some(1.2), None, None, None]);
164        let str_array =
165            StringArray::from(vec![None, None, None, None, None, None, Some("a")]);
166        let type_ids = [0, 0, 1, 1, 1, 2, 2]
167            .into_iter()
168            .collect::<ScalarBuffer<i8>>();
169
170        let children = vec![
171            Arc::new(int_array) as Arc<dyn Array>,
172            Arc::new(float_array),
173            Arc::new(str_array),
174        ];
175
176        let array =
177            UnionArray::try_new(union_fields(), type_ids, None, children).unwrap();
178
179        let result = arrow::compute::is_null(&array).unwrap();
180
181        let expected =
182            &BooleanArray::from(vec![false, true, false, false, true, true, false]);
183        assert_eq!(expected, &result);
184    }
185
186    #[test]
187    fn dense_union_is_null() {
188        // union of [{A=1}, {A=}, {B=3.2}, {B=}, {C="a"}, {C=}]
189        let int_array = Int32Array::from(vec![Some(1), None]);
190        let float_array = Float64Array::from(vec![Some(3.2), None]);
191        let str_array = StringArray::from(vec![Some("a"), None]);
192        let type_ids = [0, 0, 1, 1, 2, 2].into_iter().collect::<ScalarBuffer<i8>>();
193        let offsets = [0, 1, 0, 1, 0, 1]
194            .into_iter()
195            .collect::<ScalarBuffer<i32>>();
196
197        let children = vec![
198            Arc::new(int_array) as Arc<dyn Array>,
199            Arc::new(float_array),
200            Arc::new(str_array),
201        ];
202
203        let array =
204            UnionArray::try_new(union_fields(), type_ids, Some(offsets), children)
205                .unwrap();
206
207        let result = arrow::compute::is_null(&array).unwrap();
208
209        let expected = &BooleanArray::from(vec![false, true, false, true, false, true]);
210        assert_eq!(expected, &result);
211    }
212}