## 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/random.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::Float64Array;
    22
    use arrow::datatypes::DataType;
    23
    use arrow::datatypes::DataType::Float64;
    24
    use rand::{rng, Rng};
    25
    26
    use datafusion_common::{assert_or_internal_err, DataFusionError, Result};
    27
    use datafusion_expr::{ColumnarValue, ScalarFunctionArgs};
    28
    use datafusion_expr::{Documentation, ScalarUDFImpl, Signature, Volatility};
    29
    use datafusion_macros::user_doc;
    30
    31
    #[user_doc(
    32
        doc_section(label = "Math Functions"),
    33
        description = r#"Returns a random float value in the range [0, 1).
    34
    The random seed is unique to each row."#,
    35
        syntax_example = "random()",
    36
        sql_example = r#"```sql
    37
    > SELECT random();
    38
    +------------------+
    39
    | random()         |
    40
    +------------------+
    41
    | 0.7389238902938  |
    42
    +------------------+
    43
    ```"#
    44
    )]
    45
    #[derive(Debug, PartialEq, Eq, Hash)]
    46
    pub struct RandomFunc {
    47
        signature: Signature,
    48
    }
    49
    50
    impl Default for RandomFunc {
    51
    0
        fn default() -> Self {
    52
    0
            RandomFunc::new()
    53
    0
        }
    54
    }
    55
    56
    impl RandomFunc {
    57
    0
        pub fn new() -> Self {
    58
    0
            Self {
    59
    0
                signature: Signature::nullary(Volatility::Volatile),
    60
    0
            }
    61
    0
        }
    62
    }
    63
    64
    impl ScalarUDFImpl for RandomFunc {
    65
    0
        fn as_any(&self) -> &dyn Any {
    66
    0
            self
    67
    0
        }
    68
    69
    0
        fn name(&self) -> &str {
    70
    0
            "random"
    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(Float64)
    79
    0
        }
    80
    81
    0
        fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
    82
    0
            assert_or_internal_err!(
    83
    0
                args.args.is_empty(),
    84
    0
                "{} function does not accept arguments",
    85
    0
                self.name()
    86
            );
    87
    0
            let mut rng = rng();
    88
    0
            let mut values = vec![0.0; args.number_rows];
    89
            // Equivalent to set each element with rng.random_range(0.0..1.0), but more efficient
    90
    0
            rng.fill(&mut values[..]);
    91
    0
            let array = Float64Array::from(values);
    92
    93
    0
            Ok(ColumnarValue::Array(Arc::new(array)))
    94
    0
        }
    95
    96
    0
        fn documentation(&self) -> Option<&Documentation> {
    97
    0
            self.doc()
    98
    0
        }
    99
    }