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::{transform_leaf_type_preserving_encoding, 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, EncodingPreservation, ScalarFunctionArgs,
27    ScalarUDFImpl, Signature, 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![
63                    Coercion::new_exact(TypeSignatureClass::Native(logical_string()))
64                        .with_encoding_preservation(EncodingPreservation::dictionary()),
65                ],
66                Volatility::Immutable,
67            ),
68        }
69    }
70}
71
72impl ScalarUDFImpl for BitLengthFunc {
73    fn name(&self) -> &str {
74        "bit_length"
75    }
76
77    fn signature(&self) -> &Signature {
78        &self.signature
79    }
80
81    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
82        transform_leaf_type_preserving_encoding(&arg_types[0], &|data_type| {
83            utf8_to_int_type(data_type, "bit_length")
84        })
85    }
86
87    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
88        let [array] = take_function_args(self.name(), &args.args)?;
89
90        match array {
91            ColumnarValue::Array(v) => Ok(ColumnarValue::Array(bit_length(v.as_ref())?)),
92            ColumnarValue::Scalar(v) => Ok(ColumnarValue::Scalar(bit_length_scalar(v))),
93        }
94    }
95
96    fn documentation(&self) -> Option<&Documentation> {
97        self.doc()
98    }
99}
100
101fn bit_length_scalar(value: &ScalarValue) -> ScalarValue {
102    match value {
103        ScalarValue::Utf8(v) => {
104            ScalarValue::Int32(v.as_ref().map(|x| (x.len() * 8) as i32))
105        }
106        ScalarValue::LargeUtf8(v) => {
107            ScalarValue::Int64(v.as_ref().map(|x| (x.len() * 8) as i64))
108        }
109        ScalarValue::Utf8View(v) => {
110            ScalarValue::Int32(v.as_ref().map(|x| (x.len() * 8) as i32))
111        }
112        ScalarValue::Dictionary(key_type, value) => {
113            ScalarValue::Dictionary(key_type.clone(), Box::new(bit_length_scalar(value)))
114        }
115        _ => unreachable!("bit length"),
116    }
117}