## Coverage Report

#### Created: 2025-11-22 06:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)

    /Users/andrewlamb/Software/datafusion/datafusion/functions/src/math/iszero.rs

    Line
    Count
    Source
    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
    use std::any::Any;
    19
    use std::sync::Arc;
    20
    21
    use arrow::array::{ArrayRef, AsArray, BooleanArray};
    22
    use arrow::datatypes::DataType::{Boolean, Float32, Float64};
    23
    use arrow::datatypes::{DataType, Float32Type, Float64Type};
    24
    25
    use datafusion_common::{exec_err, Result};
    26
    use datafusion_expr::TypeSignature::Exact;
    27
    use datafusion_expr::{
    28
        ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
    29
        Volatility,
    30
    };
    31
    use datafusion_macros::user_doc;
    32
    33
    use crate::utils::make_scalar_function;
    34
    35
    #[user_doc(
    36
        doc_section(label = "Math Functions"),
    37
        description = "Returns true if a given number is +0.0 or -0.0 otherwise returns false.",
    38
        syntax_example = "iszero(numeric_expression)",
    39
        sql_example = r#"```sql
    40
    > SELECT iszero(0);
    41
    +------------+
    42
    | iszero(0)  |
    43
    +------------+
    44
    | true       |
    45
    +------------+
    46
    ```"#,
    47
        standard_argument(name = "numeric_expression", prefix = "Numeric")
    48
    )]
    49
    #[derive(Debug, PartialEq, Eq, Hash)]
    50
    pub struct IsZeroFunc {
    51
        signature: Signature,
    52
    }
    53
    54
    impl Default for IsZeroFunc {
    55
    0
        fn default() -> Self {
    56
    0
            IsZeroFunc::new()
    57
    0
        }
    58
    }
    59
    60
    impl IsZeroFunc {
    61
    0
        pub fn new() -> Self {
    62
            use DataType::*;
    63
    0
            Self {
    64
    0
                signature: Signature::one_of(
    65
    0
                    vec![Exact(vec![Float32]), Exact(vec![Float64])],
    66
    0
                    Volatility::Immutable,
    67
    0
                ),
    68
    0
            }
    69
    0
        }
    70
    }
    71
    72
    impl ScalarUDFImpl for IsZeroFunc {
    73
    0
        fn as_any(&self) -> &dyn Any {
    74
    0
            self
    75
    0
        }
    76
    77
    0
        fn name(&self) -> &str {
    78
    0
            "iszero"
    79
    0
        }
    80
    81
    0
        fn signature(&self) -> &Signature {
    82
    0
            &self.signature
    83
    0
        }
    84
    85
    0
        fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
    86
    0
            Ok(Boolean)
    87
    0
        }
    88
    89
    0
        fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
    90
    0
            make_scalar_function(iszero, vec![])(&args.args)
    91
    0
        }
    92
    93
    0
        fn documentation(&self) -> Option<&Documentation> {
    94
    0
            self.doc()
    95
    0
        }
    96
    }
    97
    98
    /// Iszero SQL function
    99
    0
    pub fn iszero(args: &[ArrayRef]) -> Result<ArrayRef> {
    100
    0
        match args[0].data_type() {
    101
    0
            Float64 => Ok(Arc::new(BooleanArray::from_unary(
    102
    0
                args[0].as_primitive::<Float64Type>(),
    103
    0
                |x| x == 0.0,
    104
    0
            )) as ArrayRef),
    105
    106
    0
            Float32 => Ok(Arc::new(BooleanArray::from_unary(
    107
    0
                args[0].as_primitive::<Float32Type>(),
    108
    0
                |x| x == 0.0,
    109
    0
            )) as ArrayRef),
    110
    111
    0
            other => exec_err!("Unsupported data type {other:?} for function iszero"),
    112
        }
    113
    0
    }
    114
    115
    #[cfg(test)]
    116
    mod test {
    117
        use std::sync::Arc;
    118
    119
        use arrow::array::{ArrayRef, Float32Array, Float64Array};
    120
    121
        use datafusion_common::cast::as_boolean_array;
    122
    123
        use crate::math::iszero::iszero;
    124
    125
        #[test]
    126
        fn test_iszero_f64() {
    127
            let args: Vec<ArrayRef> =
    128
                vec![Arc::new(Float64Array::from(vec![1.0, 0.0, 3.0, -0.0]))];
    129
    130
            let result = iszero(&args).expect("failed to initialize function iszero");
    131
            let booleans =
    132
                as_boolean_array(&result).expect("failed to initialize function iszero");
    133
    134
            assert_eq!(booleans.len(), 4);
    135
            assert!(!booleans.value(0));
    136
            assert!(booleans.value(1));
    137
            assert!(!booleans.value(2));
    138
            assert!(booleans.value(3));
    139
        }
    140
    141
        #[test]
    142
        fn test_iszero_f32() {
    143
            let args: Vec<ArrayRef> =
    144
                vec![Arc::new(Float32Array::from(vec![1.0, 0.0, 3.0, -0.0]))];
    145
    146
            let result = iszero(&args).expect("failed to initialize function iszero");
    147
            let booleans =
    148
                as_boolean_array(&result).expect("failed to initialize function iszero");
    149
    150
            assert_eq!(booleans.len(), 4);
    151
            assert!(!booleans.value(0));
    152
            assert!(booleans.value(1));
    153
            assert!(!booleans.value(2));
    154
            assert!(booleans.value(3));
    155
        }
    156
    }