## 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/uuid.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::GenericStringBuilder;
    22
    use arrow::datatypes::DataType;
    23
    use arrow::datatypes::DataType::Utf8;
    24
    use rand::Rng;
    25
    use uuid::Uuid;
    26
    27
    use datafusion_common::{assert_or_internal_err, DataFusionError, Result};
    28
    use datafusion_expr::{ColumnarValue, Documentation, Volatility};
    29
    use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature};
    30
    use datafusion_macros::user_doc;
    31
    32
    #[user_doc(
    33
        doc_section(label = "String Functions"),
    34
        description = "Returns [`UUID v4`](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)) string value which is unique per row.",
    35
        syntax_example = "uuid()",
    36
        sql_example = r#"```sql
    37
    > select uuid();
    38
    +--------------------------------------+
    39
    | uuid()                               |
    40
    +--------------------------------------+
    41
    | 6ec17ef8-1934-41cc-8d59-d0c8f9eea1f0 |
    42
    +--------------------------------------+
    43
    ```"#
    44
    )]
    45
    #[derive(Debug, PartialEq, Eq, Hash)]
    46
    pub struct UuidFunc {
    47
        signature: Signature,
    48
    }
    49
    50
    impl Default for UuidFunc {
    51
    0
        fn default() -> Self {
    52
    0
            Self::new()
    53
    0
        }
    54
    }
    55
    56
    impl UuidFunc {
    57
    0
        pub fn new() -> Self {
    58
    0
            Self {
    59
    0
                signature: Signature::exact(vec![], Volatility::Volatile),
    60
    0
            }
    61
    0
        }
    62
    }
    63
    64
    impl ScalarUDFImpl for UuidFunc {
    65
    0
        fn as_any(&self) -> &dyn Any {
    66
    0
            self
    67
    0
        }
    68
    69
    0
        fn name(&self) -> &str {
    70
    0
            "uuid"
    71
    0
        }
    72
    73
    0
        fn signature(&self) -> &Signature {
    74
    0
            &self.signature
    75
    0
        }
    76
    77
    0
        fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
    78
    0
            Ok(Utf8)
    79
    0
        }
    80
    81
        /// Prints random (v4) uuid values per row
    82
        /// uuid() = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
    83
    0
        fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
    84
    0
            assert_or_internal_err!(
    85
    0
                args.args.is_empty(),
    86
    0
                "{} function does not accept arguments",
    87
    0
                self.name()
    88
            );
    89
    90
            // Generate random u128 values
    91
    0
            let mut rng = rand::rng();
    92
    0
            let mut randoms = vec![0u128; args.number_rows];
    93
    0
            rng.fill(&mut randoms[..]);
    94
    95
    0
            let mut builder = GenericStringBuilder::<i32>::with_capacity(
    96
    0
                args.number_rows,
    97
    0
                args.number_rows * 36,
    98
            );
    99
    100
    0
            let mut buffer = [0u8; 36];
    101
    0
            for x in &mut randoms {
    102
    0
                // From Uuid::new_v4(): Mask out the version and variant bits
    103
    0
                *x = *x & 0xFFFFFFFFFFFF4FFFBFFFFFFFFFFFFFFF | 0x40008000000000000000;
    104
    0
                let uuid = Uuid::from_u128(*x);
    105
    0
                let fmt = uuid::fmt::Hyphenated::from_uuid(uuid);
    106
    0
                builder.append_value(fmt.encode_lower(&mut buffer));
    107
    0
            }
    108
    109
    0
            Ok(ColumnarValue::Array(Arc::new(builder.finish())))
    110
    0
        }
    111
    112
    0
        fn documentation(&self) -> Option<&Documentation> {
    113
    0
            self.doc()
    114
    0
        }
    115
    }