Skip to main content

datafusion_functions/string/
bit_length.rs

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