## 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/bit_length.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 arrow::compute::kernels::length::bit_length;
    19
    use arrow::datatypes::DataType;
    20
    use std::any::Any;
    21
    22
    use crate::utils::utf8_to_int_type;
    23
    use datafusion_common::types::logical_string;
    24
    use datafusion_common::utils::take_function_args;
    25
    use datafusion_common::{Result, ScalarValue};
    26
    use datafusion_expr::{
    27
        Coercion, ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
    28
        TypeSignatureClass, Volatility,
    29
    };
    30
    use datafusion_macros::user_doc;
    31
    32
    #[user_doc(
    33
        doc_section(label = "String Functions"),
    34
        description = "Returns the bit length of a string.",
    35
        syntax_example = "bit_length(str)",
    36
        sql_example = r#"```sql
    37
    > select bit_length('datafusion');
    38
    +--------------------------------+
    39
    | bit_length(Utf8("datafusion")) |
    40
    +--------------------------------+
    41
    | 80                             |
    42
    +--------------------------------+
    43
    ```"#,
    44
        standard_argument(name = "str", prefix = "String"),
    45
        related_udf(name = "length"),
    46
        related_udf(name = "octet_length")
    47
    )]
    48
    #[derive(Debug, PartialEq, Eq, Hash)]
    49
    pub struct BitLengthFunc {
    50
        signature: Signature,
    51
    }
    52
    53
    impl Default for BitLengthFunc {
    54
    0
        fn default() -> Self {
    55
    0
            Self::new()
    56
    0
        }
    57
    }
    58
    59
    impl BitLengthFunc {
    60
    0
        pub fn new() -> Self {
    61
    0
            Self {
    62
    0
                signature: Signature::coercible(
    63
    0
                    vec![Coercion::new_exact(TypeSignatureClass::Native(
    64
    0
                        logical_string(),
    65
    0
                    ))],
    66
    0
                    Volatility::Immutable,
    67
    0
                ),
    68
    0
            }
    69
    0
        }
    70
    }
    71
    72
    impl ScalarUDFImpl for BitLengthFunc {
    73
    0
        fn as_any(&self) -> &dyn Any {
    74
    0
            self
    75
    0
        }
    76
    77
    0
        fn name(&self) -> &str {
    78
    0
            "bit_length"
    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
            utf8_to_int_type(&arg_types[0], "bit_length")
    87
    0
        }
    88
    89
    0
        fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
    90
    0
            let [array] = take_function_args(self.name(), &args.args)?;
    91
    92
    0
            match array {
    93
    0
                ColumnarValue::Array(v) => Ok(ColumnarValue::Array(bit_length(v.as_ref())?)),
    94
    0
                ColumnarValue::Scalar(v) => match v {
    95
    0
                    ScalarValue::Utf8(v) => Ok(ColumnarValue::Scalar(ScalarValue::Int32(
    96
    0
                        v.as_ref().map(|x| (x.len() * 8) as i32),
    97
                    ))),
    98
    0
                    ScalarValue::LargeUtf8(v) => Ok(ColumnarValue::Scalar(
    99
    0
                        ScalarValue::Int64(v.as_ref().map(|x| (x.len() * 8) as i64)),
    100
                    )),
    101
    0
                    ScalarValue::Utf8View(v) => Ok(ColumnarValue::Scalar(
    102
    0
                        ScalarValue::Int32(v.as_ref().map(|x| (x.len() * 8) as i32)),
    103
                    )),
    104
    0
                    _ => unreachable!("bit length"),
    105
                },
    106
            }
    107
    0
        }
    108
    109
    0
        fn documentation(&self) -> Option<&Documentation> {
    110
    0
            self.doc()
    111
    0
        }
    112
    }