Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions be/src/exprs/string_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,4 +1007,14 @@ StringVal StringFunctions::replace(FunctionContext* context, const StringVal& or
}
return AnyValUtil::from_string_temp(context, orig_str);
}
// Implementation of BIT_LENGTH
// int bit_length(string input)
// Returns the length in bits of input. If input == NULL, returns
// NULL per MySQL
IntVal StringFunctions::bit_length(FunctionContext* context, const StringVal& str) {
if (str.is_null) {
return IntVal::null();
}
return IntVal(str.len * 8);
}
} // namespace doris
3 changes: 3 additions & 0 deletions be/src/exprs/string_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ class StringFunctions {

static StringVal replace(FunctionContext* context, const StringVal& origStr,
const StringVal& oldStr, const StringVal& newStr);

static doris_udf::IntVal bit_length(doris_udf::FunctionContext* context,
const doris_udf::StringVal& str);
};
} // namespace doris

Expand Down
14 changes: 14 additions & 0 deletions be/test/exprs/string_functions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,20 @@ TEST_F(StringFunctionsTest, parse_url) {
StringVal("port")));
}

TEST_F(StringFunctionsTest, bit_length) {
doris_udf::FunctionContext* context = new doris_udf::FunctionContext();

ASSERT_EQ(IntVal(40), StringFunctions::bit_length(context, StringVal("hello")));

ASSERT_EQ(IntVal::null(), StringFunctions::bit_length(context, StringVal::null()));

ASSERT_EQ(IntVal(0), StringFunctions::bit_length(context, StringVal("")));

ASSERT_EQ(IntVal(88), StringFunctions::bit_length(context, StringVal("hello你好")));

delete context;
}

} // namespace doris

int main(int argc, char** argv) {
Expand Down
1 change: 1 addition & 0 deletions docs/.vuepress/sidebar/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ module.exports = [
children: [
"append_trailing_char_if_absent",
"ascii",
"bit_length",
"char_length",
"concat",
"concat_ws",
Expand Down
1 change: 1 addition & 0 deletions docs/.vuepress/sidebar/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ module.exports = [
children: [
"append_trailing_char_if_absent",
"ascii",
"bit_length",
"char_length",
"concat",
"concat_ws",
Expand Down
54 changes: 54 additions & 0 deletions docs/en/sql-reference/sql-functions/string-functions/bit_length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
{
"title": "bit_length",
"language": "en"
}
---

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# bit_length
## Description
### Syntax

'INT bit_length (VARCHAR str)'


Return length of argument in bits。

## example

```
mysql> select bit_length("abc");
+-------------------+
| bit_length('abc') |
+-------------------+
| 24 |
+-------------------+

mysql> select bit_length("中国");
+----------------------+
| bit_length('中国') |
+----------------------+
| 48 |
+----------------------+
```
## keyword
BIT_LENGTH
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
{
"title": "bit_length",
"language": "zh-CN"
}
---

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# bit_length
## description
### Syntax

`INT bit_length(VARCHAR str)`


返回字符串的位长度。

## example

```
mysql> select bit_length("abc");
+-------------------+
| bit_length('abc') |
+-------------------+
| 24 |
+-------------------+

mysql> select bit_length("中国");
+----------------------+
| bit_length('中国') |
+----------------------+
| 48 |
+----------------------+
```
## keyword
BIT_LENGTH
2 changes: 2 additions & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,8 @@
'_ZN5doris15StringFunctions30append_trailing_char_if_absentEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'],
[['length'], 'INT', ['VARCHAR'],
'_ZN5doris15StringFunctions6lengthEPN9doris_udf15FunctionContextERKNS1_9StringValE'],
[['bit_length'], 'INT', ['VARCHAR'],
'_ZN5doris15StringFunctions10bit_lengthEPN9doris_udf15FunctionContextERKNS1_9StringValE'],
[['char_length', 'character_length'], 'INT', ['VARCHAR'],
'_ZN5doris15StringFunctions16char_utf8_lengthEPN9doris_udf15FunctionContextERKNS1_9StringValE'],
[['lower', 'lcase'], 'VARCHAR', ['VARCHAR'],
Expand Down