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 crate::PhysicalExpr;
21use arrow::{
22    datatypes::{DataType, Schema},
23    record_batch::RecordBatch,
24};
25use datafusion_common::Result;
26use datafusion_common::ScalarValue;
27use datafusion_expr::ColumnarValue;
28use std::hash::Hash;
29use std::{any::Any, sync::Arc};
30
31/// IS NULL expression
32#[derive(Debug, Eq)]
33pub struct IsNullExpr {
34    /// Input expression
35    arg: Arc<dyn PhysicalExpr>,
36}
37
38// Manually derive PartialEq and Hash to work around https://github.com/rust-lang/rust/issues/78808
39impl PartialEq for IsNullExpr {
40    fn eq(&self, other: &Self) -> bool {
41        self.arg.eq(&other.arg)
42    }
43}
44
45impl Hash for IsNullExpr {
46    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
47        self.arg.hash(state);
48    }
49}
50
51impl IsNullExpr {
52    /// Create new not expression
53    pub fn new(arg: Arc<dyn PhysicalExpr>) -> Self {
54        Self { arg }
55    }
56
57    /// Get the input expression
58    pub fn arg(&self) -> &Arc<dyn PhysicalExpr> {
59        &self.arg
60    }
61}
62
63impl std::fmt::Display for IsNullExpr {
64    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
65        write!(f, "{} IS NULL", self.arg)
66    }
67}
68
69impl PhysicalExpr for IsNullExpr {
70    /// Return a reference to Any that can be used for downcasting
71    fn as_any(&self) -> &dyn Any {
72        self
73    }
74
75    fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
76        Ok(DataType::Boolean)
77    }
78
79    fn nullable(&self, _input_schema: &Schema) -> Result<bool> {
80        Ok(false)
81    }
82
83    fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
84        let arg = self.arg.evaluate(batch)?;
85        match arg {
86            ColumnarValue::Array(array) => Ok(ColumnarValue::Array(Arc::new(
87                arrow::compute::is_null(&array)?,
88            ))),
89            ColumnarValue::Scalar(scalar) => Ok(ColumnarValue::Scalar(
90                ScalarValue::Boolean(Some(scalar.is_null())),
91            )),
92        }
93    }
94
95    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
96        vec![&self.arg]
97    }
98
99    fn with_new_children(
100        self: Arc<Self>,
101        children: Vec<Arc<dyn PhysicalExpr>>,
102    ) -> Result<Arc<dyn PhysicalExpr>> {
103        Ok(Arc::new(IsNullExpr::new(Arc::clone(&children[0]))))
104    }
105
106    fn fmt_sql(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107        self.arg.fmt_sql(f)?;
108        write!(f, " IS NULL")
109    }
110}
111
112/// Create an IS NULL expression
113pub fn is_null(arg: Arc<dyn PhysicalExpr>) -> Result<Arc<dyn PhysicalExpr>> {
114    Ok(Arc::new(IsNullExpr::new(arg)))
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120    use crate::expressions::col;
121    use arrow::array::{
122        Array, BooleanArray, Float64Array, Int32Array, StringArray, UnionArray,
123    };
124    use arrow::buffer::ScalarBuffer;
125    use arrow::datatypes::*;
126    use datafusion_common::cast::as_boolean_array;
127    use datafusion_physical_expr_common::physical_expr::fmt_sql;
128
129    #[test]
130    fn is_null_op() -> Result<()> {
131        let schema = Schema::new(vec![Field::new("a", DataType::Utf8, true)]);
132        let a = StringArray::from(vec![Some("foo"), None]);
133
134        // expression: "a is null"
135        let expr = is_null(col("a", &schema)?).unwrap();
136        let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)])?;
137
138        let result = expr
139            .evaluate(&batch)?
140            .into_array(batch.num_rows())
141            .expect("Failed to convert to array");
142        let result =
143            as_boolean_array(&result).expect("failed to downcast to BooleanArray");
144
145        let expected = &BooleanArray::from(vec![false, true]);
146
147        assert_eq!(expected, result);
148
149        Ok(())
150    }
151
152    fn union_fields() -> UnionFields {
153        [
154            (0, Arc::new(Field::new("A", DataType::Int32, true))),
155            (1, Arc::new(Field::new("B", DataType::Float64, true))),
156            (2, Arc::new(Field::new("C", DataType::Utf8, true))),
157        ]
158        .into_iter()
159        .collect()
160    }
161
162    #[test]
163    fn sparse_union_is_null() {
164        // union of [{A=1}, {A=}, {B=1.1}, {B=1.2}, {B=}, {C=}, {C="a"}]
165        let int_array =
166            Int32Array::from(vec![Some(1), None, None, None, None, None, None]);
167        let float_array =
168            Float64Array::from(vec![None, None, Some(1.1), Some(1.2), None, None, None]);
169        let str_array =
170            StringArray::from(vec![None, None, None, None, None, None, Some("a")]);
171        let type_ids = [0, 0, 1, 1, 1, 2, 2]
172            .into_iter()
173            .collect::<ScalarBuffer<i8>>();
174
175        let children = vec![
176            Arc::new(int_array) as Arc<dyn Array>,
177            Arc::new(float_array),
178            Arc::new(str_array),
179        ];
180
181        let array =
182            UnionArray::try_new(union_fields(), type_ids, None, children).unwrap();
183
184        let result = arrow::compute::is_null(&array).unwrap();
185
186        let expected =
187            &BooleanArray::from(vec![false, true, false, false, true, true, false]);
188        assert_eq!(expected, &result);
189    }
190
191    #[test]
192    fn dense_union_is_null() {
193        // union of [{A=1}, {A=}, {B=3.2}, {B=}, {C="a"}, {C=}]
194        let int_array = Int32Array::from(vec![Some(1), None]);
195        let float_array = Float64Array::from(vec![Some(3.2), None]);
196        let str_array = StringArray::from(vec![Some("a"), None]);
197        let type_ids = [0, 0, 1, 1, 2, 2].into_iter().collect::<ScalarBuffer<i8>>();
198        let offsets = [0, 1, 0, 1, 0, 1]
199            .into_iter()
200            .collect::<ScalarBuffer<i32>>();
201
202        let children = vec![
203            Arc::new(int_array) as Arc<dyn Array>,
204            Arc::new(float_array),
205            Arc::new(str_array),
206        ];
207
208        let array =
209            UnionArray::try_new(union_fields(), type_ids, Some(offsets), children)
210                .unwrap();
211
212        let result = arrow::compute::is_null(&array).unwrap();
213
214        let expected = &BooleanArray::from(vec![false, true, false, true, false, true]);
215        assert_eq!(expected, &result);
216    }
217
218    #[test]
219    fn test_fmt_sql() -> Result<()> {
220        let schema = Schema::new(vec![Field::new("a", DataType::Utf8, true)]);
221
222        // expression: "a is null"
223        let expr = is_null(col("a", &schema)?).unwrap();
224        let display_string = expr.to_string();
225        assert_eq!(display_string, "a@0 IS NULL");
226        let sql_string = fmt_sql(expr.as_ref()).to_string();
227        assert_eq!(sql_string, "a IS NULL");
228
229        Ok(())
230    }
231}