## 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/string/to_hex.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::fmt::Write;
    20
    use std::sync::Arc;
    21
    22
    use crate::utils::make_scalar_function;
    23
    use arrow::array::{ArrayRef, GenericStringBuilder};
    24
    use arrow::datatypes::DataType::{
    25
        Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8, Utf8,
    26
    };
    27
    use arrow::datatypes::{
    28
        ArrowNativeType, ArrowPrimitiveType, DataType, Int16Type, Int32Type, Int64Type,
    29
        Int8Type, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
    30
    };
    31
    use datafusion_common::cast::as_primitive_array;
    32
    use datafusion_common::Result;
    33
    use datafusion_common::{exec_err, plan_err};
    34
    35
    use datafusion_expr::{ColumnarValue, Documentation};
    36
    use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
    37
    use datafusion_expr_common::signature::TypeSignature::Exact;
    38
    use datafusion_macros::user_doc;
    39
    40
    /// Converts the number to its equivalent hexadecimal representation.
    41
    /// to_hex(2147483647) = '7fffffff'
    42
    0
    pub fn to_hex<T: ArrowPrimitiveType>(args: &[ArrayRef]) -> Result<ArrayRef>
    43
    0
    where
    44
    0
        T::Native: std::fmt::LowerHex,
    45
    {
    46
    0
        let integer_array = as_primitive_array::<T>(&args[0])?;
    47
    48
    0
        let mut result = GenericStringBuilder::<i32>::with_capacity(
    49
    0
            integer_array.len(),
    50
            // * 8 to convert to bits, / 4 bits per hex char
    51
    0
            integer_array.len() * (T::Native::get_byte_width() * 8 / 4),
    52
        );
    53
    54
    0
        for integer in integer_array {
    55
    0
            if let Some(value) = integer {
    56
    0
                if let Some(value_usize) = value.to_usize() {
    57
    0
                    write!(result, "{value_usize:x}")?;
    58
    0
                } else if let Some(value_isize) = value.to_isize() {
    59
    0
                    write!(result, "{value_isize:x}")?;
    60
                } else {
    61
    0
                    return exec_err!(
    62
    0
                        "Unsupported data type {integer:?} for function to_hex"
    63
                    );
    64
                }
    65
    0
                result.append_value("");
    66
    0
            } else {
    67
    0
                result.append_null();
    68
    0
            }
    69
        }
    70
    71
    0
        let result = result.finish();
    72
    73
    0
        Ok(Arc::new(result) as ArrayRef)
    74
    0
    }
    75
    76
    #[user_doc(
    77
        doc_section(label = "String Functions"),
    78
        description = "Converts an integer to a hexadecimal string.",
    79
        syntax_example = "to_hex(int)",
    80
        sql_example = r#"```sql
    81
    > select to_hex(12345689);
    82
    +-------------------------+
    83
    | to_hex(Int64(12345689)) |
    84
    +-------------------------+
    85
    | bc6159                  |
    86
    +-------------------------+
    87
    ```"#,
    88
        standard_argument(name = "int", prefix = "Integer")
    89
    )]
    90
    #[derive(Debug, PartialEq, Eq, Hash)]
    91
    pub struct ToHexFunc {
    92
        signature: Signature,
    93
    }
    94
    95
    impl Default for ToHexFunc {
    96
    0
        fn default() -> Self {
    97
    0
            Self::new()
    98
    0
        }
    99
    }
    100
    101
    impl ToHexFunc {
    102
    0
        pub fn new() -> Self {
    103
    0
            Self {
    104
    0
                signature: Signature::one_of(
    105
    0
                    vec![
    106
    0
                        Exact(vec![Int8]),
    107
    0
                        Exact(vec![Int16]),
    108
    0
                        Exact(vec![Int32]),
    109
    0
                        Exact(vec![Int64]),
    110
    0
                        Exact(vec![UInt8]),
    111
    0
                        Exact(vec![UInt16]),
    112
    0
                        Exact(vec![UInt32]),
    113
    0
                        Exact(vec![UInt64]),
    114
    0
                    ],
    115
    0
                    Volatility::Immutable,
    116
    0
                ),
    117
    0
            }
    118
    0
        }
    119
    }
    120
    121
    impl ScalarUDFImpl for ToHexFunc {
    122
    0
        fn as_any(&self) -> &dyn Any {
    123
    0
            self
    124
    0
        }
    125
    126
    0
        fn name(&self) -> &str {
    127
    0
            "to_hex"
    128
    0
        }
    129
    130
    0
        fn signature(&self) -> &Signature {
    131
    0
            &self.signature
    132
    0
        }
    133
    134
    0
        fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
    135
    0
            Ok(match arg_types[0] {
    136
    0
                Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64 => Utf8,
    137
                _ => {
    138
    0
                    return plan_err!("The to_hex function can only accept integers.");
    139
                }
    140
            })
    141
    0
        }
    142
    143
    0
        fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
    144
    0
            match args.args[0].data_type() {
    145
    0
                Int64 => make_scalar_function(to_hex::<Int64Type>, vec![])(&args.args),
    146
    0
                UInt64 => make_scalar_function(to_hex::<UInt64Type>, vec![])(&args.args),
    147
    0
                Int32 => make_scalar_function(to_hex::<Int32Type>, vec![])(&args.args),
    148
    0
                UInt32 => make_scalar_function(to_hex::<UInt32Type>, vec![])(&args.args),
    149
    0
                Int16 => make_scalar_function(to_hex::<Int16Type>, vec![])(&args.args),
    150
    0
                UInt16 => make_scalar_function(to_hex::<UInt16Type>, vec![])(&args.args),
    151
    0
                Int8 => make_scalar_function(to_hex::<Int8Type>, vec![])(&args.args),
    152
    0
                UInt8 => make_scalar_function(to_hex::<UInt8Type>, vec![])(&args.args),
    153
    0
                other => exec_err!("Unsupported data type {other:?} for function to_hex"),
    154
            }
    155
    0
        }
    156
    157
    0
        fn documentation(&self) -> Option<&Documentation> {
    158
    0
            self.doc()
    159
    0
        }
    160
    }
    161
    162
    #[cfg(test)]
    163
    mod tests {
    164
        use arrow::array::{
    165
            Int16Array, Int32Array, Int64Array, Int8Array, StringArray, UInt16Array,
    166
            UInt32Array, UInt64Array, UInt8Array,
    167
        };
    168
        use datafusion_common::cast::as_string_array;
    169
    170
        use super::*;
    171
    172
        macro_rules! test_to_hex_type {
    173
            // Default test with standard input/output
    174
            ($name:ident, $arrow_type:ty, $array_type:ty) => {
    175
                test_to_hex_type!(
    176
                    $name,
    177
                    $arrow_type,
    178
                    $array_type,
    179
                    vec![Some(100), Some(0), None],
    180
                    vec![Some("64"), Some("0"), None]
    181
                );
    182
            };
    183
    184
            // Custom test with custom input/output (eg: positive number)
    185
            ($name:ident, $arrow_type:ty, $array_type:ty, $input:expr, $expected:expr) => {
    186
                #[test]
    187
                fn $name() -> Result<()> {
    188
                    let input = $input;
    189
                    let expected = $expected;
    190
    191
                    let array = <$array_type>::from(input);
    192
                    let array_ref = Arc::new(array);
    193
                    let hex_result = to_hex::<$arrow_type>(&[array_ref])?;
    194
                    let hex_array = as_string_array(&hex_result)?;
    195
                    let expected_array = StringArray::from(expected);
    196
    197
                    assert_eq!(&expected_array, hex_array);
    198
                    Ok(())
    199
                }
    200
            };
    201
        }
    202
    203
        test_to_hex_type!(
    204
            to_hex_int8,
    205
            Int8Type,
    206
            Int8Array,
    207
            vec![Some(100), Some(0), None, Some(-1)],
    208
            vec![Some("64"), Some("0"), None, Some("ffffffffffffffff")]
    209
        );
    210
        test_to_hex_type!(
    211
            to_hex_int16,
    212
            Int16Type,
    213
            Int16Array,
    214
            vec![Some(100), Some(0), None, Some(-1)],
    215
            vec![Some("64"), Some("0"), None, Some("ffffffffffffffff")]
    216
        );
    217
        test_to_hex_type!(
    218
            to_hex_int32,
    219
            Int32Type,
    220
            Int32Array,
    221
            vec![Some(100), Some(0), None, Some(-1)],
    222
            vec![Some("64"), Some("0"), None, Some("ffffffffffffffff")]
    223
        );
    224
        test_to_hex_type!(
    225
            to_hex_int64,
    226
            Int64Type,
    227
            Int64Array,
    228
            vec![Some(100), Some(0), None, Some(-1)],
    229
            vec![Some("64"), Some("0"), None, Some("ffffffffffffffff")]
    230
        );
    231
    232
        test_to_hex_type!(to_hex_uint8, UInt8Type, UInt8Array);
    233
        test_to_hex_type!(to_hex_uint16, UInt16Type, UInt16Array);
    234
        test_to_hex_type!(to_hex_uint32, UInt32Type, UInt32Array);
    235
        test_to_hex_type!(to_hex_uint64, UInt64Type, UInt64Array);
    236
    237
        test_to_hex_type!(
    238
            to_hex_large_signed,
    239
            Int64Type,
    240
            Int64Array,
    241
            vec![Some(i64::MAX), Some(i64::MIN)],
    242
            vec![Some("7fffffffffffffff"), Some("8000000000000000")]
    243
        );
    244
    245
        test_to_hex_type!(
    246
            to_hex_large_unsigned,
    247
            UInt64Type,
    248
            UInt64Array,
    249
            vec![Some(u64::MAX), Some(u64::MIN)],
    250
            vec![Some("ffffffffffffffff"), Some("0")]
    251
        );
    252
    }