You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2021/07/08 01:40:40 UTC

[incubator-doris] branch master updated: [Feature][Function] support bit_length function (#6140)

This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new c929a89  [Feature][Function] support bit_length function (#6140)
c929a89 is described below

commit c929a8935a922f454e09a808caee6ed256f75dc5
Author: DinoZhang <vi...@gmail.com>
AuthorDate: Thu Jul 8 09:40:30 2021 +0800

    [Feature][Function] support bit_length function (#6140)
    
    support bit_length function like mysql
---
 be/src/exprs/string_functions.cpp                  | 10 ++++
 be/src/exprs/string_functions.h                    |  3 ++
 be/test/exprs/string_functions_test.cpp            | 14 ++++++
 docs/.vuepress/sidebar/en.js                       |  1 +
 docs/.vuepress/sidebar/zh-CN.js                    |  1 +
 .../sql-functions/string-functions/bit_length.md   | 54 ++++++++++++++++++++++
 .../sql-functions/string-functions/bit_length.md   | 54 ++++++++++++++++++++++
 gensrc/script/doris_builtins_functions.py          |  2 +
 8 files changed, 139 insertions(+)

diff --git a/be/src/exprs/string_functions.cpp b/be/src/exprs/string_functions.cpp
index 7f9e80b..c805519 100644
--- a/be/src/exprs/string_functions.cpp
+++ b/be/src/exprs/string_functions.cpp
@@ -995,4 +995,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
diff --git a/be/src/exprs/string_functions.h b/be/src/exprs/string_functions.h
index 58bdba5..05a548d 100644
--- a/be/src/exprs/string_functions.h
+++ b/be/src/exprs/string_functions.h
@@ -177,6 +177,9 @@ public:
 
     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
 
diff --git a/be/test/exprs/string_functions_test.cpp b/be/test/exprs/string_functions_test.cpp
index f9d7534..abfa969 100644
--- a/be/test/exprs/string_functions_test.cpp
+++ b/be/test/exprs/string_functions_test.cpp
@@ -609,6 +609,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) {
diff --git a/docs/.vuepress/sidebar/en.js b/docs/.vuepress/sidebar/en.js
index 719e273..ad8633e 100644
--- a/docs/.vuepress/sidebar/en.js
+++ b/docs/.vuepress/sidebar/en.js
@@ -294,6 +294,7 @@ module.exports = [
             children: [
               "append_trailing_char_if_absent",
               "ascii",
+              "bit_length",
               "char_length",
               "concat",
               "concat_ws",
diff --git a/docs/.vuepress/sidebar/zh-CN.js b/docs/.vuepress/sidebar/zh-CN.js
index 41c4f9e..67fd951 100644
--- a/docs/.vuepress/sidebar/zh-CN.js
+++ b/docs/.vuepress/sidebar/zh-CN.js
@@ -299,6 +299,7 @@ module.exports = [
             children: [
               "append_trailing_char_if_absent",
               "ascii",
+              "bit_length",
               "char_length",
               "concat",
               "concat_ws",
diff --git a/docs/en/sql-reference/sql-functions/string-functions/bit_length.md b/docs/en/sql-reference/sql-functions/string-functions/bit_length.md
new file mode 100644
index 0000000..134e0e2
--- /dev/null
+++ b/docs/en/sql-reference/sql-functions/string-functions/bit_length.md
@@ -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
diff --git a/docs/zh-CN/sql-reference/sql-functions/string-functions/bit_length.md b/docs/zh-CN/sql-reference/sql-functions/string-functions/bit_length.md
new file mode 100644
index 0000000..be76317
--- /dev/null
+++ b/docs/zh-CN/sql-reference/sql-functions/string-functions/bit_length.md
@@ -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
diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py
index 3ce5056..bc69c0d 100755
--- a/gensrc/script/doris_builtins_functions.py
+++ b/gensrc/script/doris_builtins_functions.py
@@ -677,6 +677,8 @@ visible_functions = [
 	'_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'],

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org