From 7d2b8df10f5bbe15384b7e5f033edad31ee7ede4 Mon Sep 17 00:00:00 2001 From: Dino Zhang Date: Mon, 21 Jun 2021 10:48:43 +0800 Subject: [PATCH 1/2] support bit_length function --- be/src/exprs/string_functions.cpp | 10 ++++ be/src/exprs/string_functions.h | 3 ++ be/test/exprs/string_functions_test.cpp | 14 +++++ .../string-functions/bit_length.md | 54 +++++++++++++++++++ .../string-functions/bit_length.md | 54 +++++++++++++++++++ gensrc/script/doris_builtins_functions.py | 2 + 6 files changed, 137 insertions(+) create mode 100644 docs/en/sql-reference/sql-functions/string-functions/bit_length.md create mode 100644 docs/zh-CN/sql-reference/sql-functions/string-functions/bit_length.md diff --git a/be/src/exprs/string_functions.cpp b/be/src/exprs/string_functions.cpp index 8f69797be59212..6900338f6d51fc 100644 --- a/be/src/exprs/string_functions.cpp +++ b/be/src/exprs/string_functions.cpp @@ -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 diff --git a/be/src/exprs/string_functions.h b/be/src/exprs/string_functions.h index 7237f9f44c6760..2c8d61391fd7f1 100644 --- a/be/src/exprs/string_functions.h +++ b/be/src/exprs/string_functions.h @@ -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 diff --git a/be/test/exprs/string_functions_test.cpp b/be/test/exprs/string_functions_test.cpp index c0fd49f6301e89..6eada53c90202b 100644 --- a/be/test/exprs/string_functions_test.cpp +++ b/be/test/exprs/string_functions_test.cpp @@ -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) { 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 00000000000000..34a5c4f77da87e --- /dev/null +++ b/docs/en/sql-reference/sql-functions/string-functions/bit_length.md @@ -0,0 +1,54 @@ +--- +{ + "title": "bit_length", + "language": "en" +} +--- + + + +# bit_length +## Description +### Syntax + +'INT bit_length (VARCHAR str)' + + +Return length of argument in bits。 + +## example + +``` +mysql> select bit_length("abc"); ++---------------+ +| length('abc') | ++---------------+ +| 24 | ++---------------+ + +mysql> select bit_length("中国"); ++------------------+ +| 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 00000000000000..9395b99b3d7faa --- /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" +} +--- + + + +# bit_length +## description +### Syntax + +`INT bit_length(VARCHAR str)` + + +返回字符串的位长度。 + +## example + +``` +mysql> select bit_length("abc"); ++---------------+ +| length('abc') | ++---------------+ +| 24 | ++---------------+ + +mysql> select bit_length("中国"); ++------------------+ +| length('中国') | ++------------------+ +| 48 | ++------------------+ +``` +## keyword +BIT_LENGTH diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index 009c40e7ebeec2..f1bcfc2c5001b3 100755 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -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'], From c094e6ac914dfeb71050f14434d5583847996193 Mon Sep 17 00:00:00 2001 From: Dino Zhang Date: Fri, 2 Jul 2021 16:47:55 +0800 Subject: [PATCH 2/2] fix --- docs/.vuepress/sidebar/en.js | 1 + docs/.vuepress/sidebar/zh-CN.js | 1 + .../string-functions/bit_length.md | 20 +++++++++---------- .../string-functions/bit_length.md | 20 +++++++++---------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/docs/.vuepress/sidebar/en.js b/docs/.vuepress/sidebar/en.js index b1d7660f3bf8b4..dd8687c7704b56 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 c8ff8582915cd9..861c3e3fd78093 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 index 34a5c4f77da87e..134e0e24d8296c 100644 --- a/docs/en/sql-reference/sql-functions/string-functions/bit_length.md +++ b/docs/en/sql-reference/sql-functions/string-functions/bit_length.md @@ -37,18 +37,18 @@ Return length of argument in bits。 ``` mysql> select bit_length("abc"); -+---------------+ -| length('abc') | -+---------------+ -| 24 | -+---------------+ ++-------------------+ +| bit_length('abc') | ++-------------------+ +| 24 | ++-------------------+ mysql> select bit_length("中国"); -+------------------+ -| length('中国') | -+------------------+ -| 48 | -+------------------+ ++----------------------+ +| 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 index 9395b99b3d7faa..be76317f38b07b 100644 --- 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 @@ -37,18 +37,18 @@ under the License. ``` mysql> select bit_length("abc"); -+---------------+ -| length('abc') | -+---------------+ -| 24 | -+---------------+ ++-------------------+ +| bit_length('abc') | ++-------------------+ +| 24 | ++-------------------+ mysql> select bit_length("中国"); -+------------------+ -| length('中国') | -+------------------+ -| 48 | -+------------------+ ++----------------------+ +| bit_length('中国') | ++----------------------+ +| 48 | ++----------------------+ ``` ## keyword BIT_LENGTH