## 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/abs.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
    //! math expressions
    19
    20
    use std::any::Any;
    21
    use std::sync::Arc;
    22
    23
    use arrow::array::{
    24
        ArrayRef, Decimal128Array, Decimal256Array, Decimal32Array, Decimal64Array,
    25
        Float16Array, Float32Array, Float64Array, Int16Array, Int32Array, Int64Array,
    26
        Int8Array,
    27
    };
    28
    use arrow::datatypes::DataType;
    29
    use arrow::error::ArrowError;
    30
    use datafusion_common::{not_impl_err, utils::take_function_args, Result};
    31
    use datafusion_expr::interval_arithmetic::Interval;
    32
    use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
    33
    use datafusion_expr::{
    34
        ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
    35
        Volatility,
    36
    };
    37
    use datafusion_macros::user_doc;
    38
    use num_traits::sign::Signed;
    39
    40
    type MathArrayFunction = fn(&ArrayRef) -> Result<ArrayRef>;
    41
    42
    #[macro_export]
    43
    macro_rules! make_abs_function {
    44
        ($ARRAY_TYPE:ident) => {{
    45
    0
            |input: &ArrayRef| {
    46
    0
                let array = downcast_named_arg!(&input, "abs arg", $ARRAY_TYPE);
    47
    0
                let res: $ARRAY_TYPE = array.unary(|x| x.abs());
    48
    0
                Ok(Arc::new(res) as ArrayRef)
    49
    0
            }
    50
        }};
    51
    }
    52
    53
    macro_rules! make_try_abs_function {
    54
        ($ARRAY_TYPE:ident) => {{
    55
    0
            |input: &ArrayRef| {
    56
    0
                let array = downcast_named_arg!(&input, "abs arg", $ARRAY_TYPE);
    57
    0
                let res: $ARRAY_TYPE = array.try_unary(|x| {
    58
    0
                    x.checked_abs().ok_or_else(|| {
    59
    0
                        ArrowError::ComputeError(format!(
    60
    0
                            "{} overflow on abs({})",
    61
    0
                            stringify!($ARRAY_TYPE),
    62
    0
                            x
    63
    0
                        ))
    64
    0
                    })
    65
    0
                })?;
    66
    0
                Ok(Arc::new(res) as ArrayRef)
    67
    0
            }
    68
        }};
    69
    }
    70
    71
    #[macro_export]
    72
    macro_rules! make_wrapping_abs_function {
    73
        ($ARRAY_TYPE:ident) => {{
    74
    0
            |input: &ArrayRef| {
    75
    0
                let array = downcast_named_arg!(&input, "abs arg", $ARRAY_TYPE);
    76
    0
                let res: $ARRAY_TYPE = array
    77
    0
                    .unary(|x| x.wrapping_abs())
    78
    0
                    .with_data_type(input.data_type().clone());
    79
    0
                Ok(Arc::new(res) as ArrayRef)
    80
    0
            }
    81
        }};
    82
    }
    83
    84
    /// Abs SQL function
    85
    /// Return different implementations based on input datatype to reduce branches during execution
    86
    0
    fn create_abs_function(input_data_type: &DataType) -> Result<MathArrayFunction> {
    87
    0
        match input_data_type {
    88
    0
            DataType::Float16 => Ok(make_abs_function!(Float16Array)),
    89
    0
            DataType::Float32 => Ok(make_abs_function!(Float32Array)),
    90
    0
            DataType::Float64 => Ok(make_abs_function!(Float64Array)),
    91
    92
            // Types that may overflow, such as abs(-128_i8).
    93
    0
            DataType::Int8 => Ok(make_try_abs_function!(Int8Array)),
    94
    0
            DataType::Int16 => Ok(make_try_abs_function!(Int16Array)),
    95
    0
            DataType::Int32 => Ok(make_try_abs_function!(Int32Array)),
    96
    0
            DataType::Int64 => Ok(make_try_abs_function!(Int64Array)),
    97
    98
            // Types of results are the same as the input.
    99
            DataType::Null
    100
            | DataType::UInt8
    101
            | DataType::UInt16
    102
            | DataType::UInt32
    103
    0
            | DataType::UInt64 => Ok(|input: &ArrayRef| Ok(Arc::clone(input))),
    104
    105
            // Decimal types
    106
    0
            DataType::Decimal32(_, _) => Ok(make_wrapping_abs_function!(Decimal32Array)),
    107
    0
            DataType::Decimal64(_, _) => Ok(make_wrapping_abs_function!(Decimal64Array)),
    108
    0
            DataType::Decimal128(_, _) => Ok(make_wrapping_abs_function!(Decimal128Array)),
    109
    0
            DataType::Decimal256(_, _) => Ok(make_wrapping_abs_function!(Decimal256Array)),
    110
    111
    0
            other => not_impl_err!("Unsupported data type {other:?} for function abs"),
    112
        }
    113
    0
    }
    114
    #[user_doc(
    115
        doc_section(label = "Math Functions"),
    116
        description = "Returns the absolute value of a number.",
    117
        syntax_example = "abs(numeric_expression)",
    118
        sql_example = r#"```sql
    119
    > SELECT abs(-5);
    120
    +----------+
    121
    | abs(-5)  |
    122
    +----------+
    123
    | 5        |
    124
    +----------+
    125
    ```"#,
    126
        standard_argument(name = "numeric_expression", prefix = "Numeric")
    127
    )]
    128
    #[derive(Debug, PartialEq, Eq, Hash)]
    129
    pub struct AbsFunc {
    130
        signature: Signature,
    131
    }
    132
    133
    impl Default for AbsFunc {
    134
    0
        fn default() -> Self {
    135
    0
            Self::new()
    136
    0
        }
    137
    }
    138
    139
    impl AbsFunc {
    140
    0
        pub fn new() -> Self {
    141
    0
            Self {
    142
    0
                signature: Signature::numeric(1, Volatility::Immutable),
    143
    0
            }
    144
    0
        }
    145
    }
    146
    147
    impl ScalarUDFImpl for AbsFunc {
    148
    0
        fn as_any(&self) -> &dyn Any {
    149
    0
            self
    150
    0
        }
    151
    152
    0
        fn name(&self) -> &str {
    153
    0
            "abs"
    154
    0
        }
    155
    156
    0
        fn signature(&self) -> &Signature {
    157
    0
            &self.signature
    158
    0
        }
    159
    160
    0
        fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
    161
    0
            Ok(arg_types[0].clone())
    162
    0
        }
    163
    164
    0
        fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
    165
    0
            let args = ColumnarValue::values_to_arrays(&args.args)?;
    166
    0
            let [input] = take_function_args(self.name(), args)?;
    167
    168
    0
            let input_data_type = input.data_type();
    169
    0
            let abs_fun = create_abs_function(input_data_type)?;
    170
    171
    0
            abs_fun(&input).map(ColumnarValue::Array)
    172
    0
        }
    173
    174
    0
        fn output_ordering(&self, input: &[ExprProperties]) -> Result<SortProperties> {
    175
            // Non-decreasing for x ≥ 0 and symmetrically non-increasing for x ≤ 0.
    176
    0
            let arg = &input[0];
    177
    0
            let range = &arg.range;
    178
    0
            let zero_point = Interval::make_zero(&range.lower().data_type())?;
    179
    180
    0
            if range.gt_eq(&zero_point)? == Interval::TRUE {
    181
    0
                Ok(arg.sort_properties)
    182
    0
            } else if range.lt_eq(&zero_point)? == Interval::TRUE {
    183
    0
                Ok(-arg.sort_properties)
    184
            } else {
    185
    0
                Ok(SortProperties::Unordered)
    186
            }
    187
    0
        }
    188
    189
    0
        fn documentation(&self) -> Option<&Documentation> {
    190
    0
            self.doc()
    191
    0
        }
    192
    }