This is an automated email from the ASF dual-hosted git repository.
liuhangyuan 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 89c7234 Support starts_with (str, prefix) function (#2813)
89c7234 is described below
commit 89c7234c1cc7b5fdcb2f3f79ee07ccae2eb0c9d9
Author: Lishi <[email protected]>
AuthorDate: Tue Jan 21 14:09:08 2020 +0800
Support starts_with (str, prefix) function (#2813)
Support starts_with function
---
be/src/exprs/string_functions.cpp | 10 +++++
be/src/exprs/string_functions.h | 3 ++
be/test/exprs/string_functions_test.cpp | 33 +++++++++++++++
.../sql-functions/string-functions/starts_with.md | 46 +++++++++++++++++++++
.../string-functions/starts_with_EN.md | 47 ++++++++++++++++++++++
gensrc/script/doris_builtins_functions.py | 2 +
gensrc/script/doris_functions.py | 1 +
7 files changed, 142 insertions(+)
diff --git a/be/src/exprs/string_functions.cpp
b/be/src/exprs/string_functions.cpp
index 48d045a..92860d8 100644
--- a/be/src/exprs/string_functions.cpp
+++ b/be/src/exprs/string_functions.cpp
@@ -79,6 +79,16 @@ StringVal StringFunctions::right(
return substring(context, str, IntVal(pos), len);
}
+BooleanVal StringFunctions::starts_with(
+ FunctionContext* context, const StringVal& str, const StringVal&
prefix) {
+ if (str.is_null || prefix.is_null) {
+ return BooleanVal::null();
+ }
+ re2::StringPiece str_sp(reinterpret_cast<char*>(str.ptr), str.len);
+ re2::StringPiece prefix_sp(reinterpret_cast<char*>(prefix.ptr),
prefix.len);
+ return BooleanVal(str_sp.starts_with(prefix_sp));
+}
+
BooleanVal StringFunctions::ends_with(
FunctionContext* context, const StringVal& str, const StringVal&
suffix) {
if (str.is_null || suffix.is_null) {
diff --git a/be/src/exprs/string_functions.h b/be/src/exprs/string_functions.h
index 92967e9..502d555 100644
--- a/be/src/exprs/string_functions.h
+++ b/be/src/exprs/string_functions.h
@@ -49,6 +49,9 @@ public:
static doris_udf::StringVal right(
doris_udf::FunctionContext* context, const doris_udf::StringVal& str,
const doris_udf::IntVal& len);
+ static doris_udf::BooleanVal starts_with(
+ doris_udf::FunctionContext* context, const doris_udf::StringVal& str,
+ const doris_udf::StringVal& prefix);
static doris_udf::BooleanVal ends_with(
doris_udf::FunctionContext* context, const doris_udf::StringVal& str,
const doris_udf::StringVal& suffix);
diff --git a/be/test/exprs/string_functions_test.cpp
b/be/test/exprs/string_functions_test.cpp
index fbb6576..8a2cd81 100644
--- a/be/test/exprs/string_functions_test.cpp
+++ b/be/test/exprs/string_functions_test.cpp
@@ -190,6 +190,39 @@ TEST_F(StringFunctionsTest, ends_with) {
ASSERT_EQ(nullRet, StringFunctions::ends_with(context, StringVal::null(),
StringVal::null()));
}
+TEST_F(StringFunctionsTest, starts_with) {
+ doris_udf::FunctionContext* context = new doris_udf::FunctionContext();
+ doris_udf::BooleanVal falseRet = doris_udf::BooleanVal(false);
+ doris_udf::BooleanVal trueRet = doris_udf::BooleanVal(true);
+ doris_udf::BooleanVal nullRet = doris_udf::BooleanVal::null();
+
+ ASSERT_EQ(trueRet, StringFunctions::starts_with(context, StringVal(""),
StringVal("")));
+
+ ASSERT_EQ(trueRet, StringFunctions::starts_with(context, StringVal(" "),
StringVal(" ")));
+
+ ASSERT_EQ(trueRet, StringFunctions::starts_with(context,
StringVal("hello"), StringVal("")));
+
+ ASSERT_EQ(falseRet, StringFunctions::starts_with(context, StringVal(""),
StringVal("hello")));
+
+ ASSERT_EQ(trueRet, StringFunctions::starts_with(context,
StringVal("hello"), StringVal("hello")));
+
+ ASSERT_EQ(falseRet, StringFunctions::starts_with(context,
StringVal("hello"), StringVal(" ")));
+
+ ASSERT_EQ(falseRet, StringFunctions::starts_with(context, StringVal(" "),
StringVal("world")));
+
+ ASSERT_EQ(trueRet, StringFunctions::starts_with(context, StringVal("hello
world"), StringVal("hello")));
+
+ ASSERT_EQ(falseRet, StringFunctions::starts_with(context, StringVal("hello
world"), StringVal("world")));
+
+ ASSERT_EQ(trueRet, StringFunctions::starts_with(context, StringVal("hello
world"), StringVal("hello world")));
+
+ ASSERT_EQ(nullRet, StringFunctions::starts_with(context, StringVal("hello
world"), StringVal::null()));
+
+ ASSERT_EQ(nullRet, StringFunctions::starts_with(context,
StringVal::null(), StringVal("hello world")));
+
+ ASSERT_EQ(nullRet, StringFunctions::starts_with(context,
StringVal::null(), StringVal::null()));
+}
+
}
int main(int argc, char** argv) {
diff --git
a/docs/documentation/cn/sql-reference/sql-functions/string-functions/starts_with.md
b/docs/documentation/cn/sql-reference/sql-functions/string-functions/starts_with.md
new file mode 100644
index 0000000..5997abd
--- /dev/null
+++
b/docs/documentation/cn/sql-reference/sql-functions/string-functions/starts_with.md
@@ -0,0 +1,46 @@
+<!--
+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.
+-->
+
+# starts_with
+## description
+### Syntax
+
+`BOOLEAN STARTS_WITH (VARCHAR str, VARCHAR prefix)`
+
+如果字符串以指定前缀开头,返回true。否则,返回false。任意参数为NULL,返回NULL。
+
+## example
+
+```
+MySQL [(none)]> select starts_with("hello world","hello");
++-------------------------------------+
+| starts_with('hello world', 'hello') |
++-------------------------------------+
+| 1 |
++-------------------------------------+
+
+MySQL [(none)]> select starts_with("hello world","world");
++-------------------------------------+
+| starts_with('hello world', 'world') |
++-------------------------------------+
+| 0 |
++-------------------------------------+
+```
+##keyword
+STARTS_WITH
\ No newline at end of file
diff --git
a/docs/documentation/en/sql-reference/sql-functions/string-functions/starts_with_EN.md
b/docs/documentation/en/sql-reference/sql-functions/string-functions/starts_with_EN.md
new file mode 100644
index 0000000..81f1456
--- /dev/null
+++
b/docs/documentation/en/sql-reference/sql-functions/string-functions/starts_with_EN.md
@@ -0,0 +1,47 @@
+<!--
+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.
+-->
+
+# starts_with
+## Description
+### Syntax
+
+`BOOLEAN STARTS_WITH (VARCHAR str, VARCHAR prefix)`
+
+It returns true if the string starts with the specified prefix, otherwise it
returns false.
+If any parameter is NULL, it returns NULL.
+
+## example
+
+```
+MySQL [(none)]> select starts_with("hello world","hello");
++-------------------------------------+
+| starts_with('hello world', 'hello') |
++-------------------------------------+
+| 1 |
++-------------------------------------+
+
+MySQL [(none)]> select starts_with("hello world","world");
++-------------------------------------+
+| starts_with('hello world', 'world') |
++-------------------------------------+
+| 0 |
++-------------------------------------+
+```
+##keyword
+STARTS_WITH
\ No newline at end of file
diff --git a/gensrc/script/doris_builtins_functions.py
b/gensrc/script/doris_builtins_functions.py
index 11f106a..0b938a0 100755
--- a/gensrc/script/doris_builtins_functions.py
+++ b/gensrc/script/doris_builtins_functions.py
@@ -506,6 +506,8 @@ visible_functions = [
'15FunctionContextERKNS1_9StringValERKNS1_6IntValE'],
[['ends_with'], 'BOOLEAN', ['VARCHAR', 'VARCHAR'],
'_ZN5doris15StringFunctions9ends_withEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'],
+ [['starts_with'], 'BOOLEAN', ['VARCHAR', 'VARCHAR'],
+
'_ZN5doris15StringFunctions11starts_withEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'],
[['space'], 'VARCHAR', ['INT'],
'_ZN5doris15StringFunctions5spaceEPN9doris_udf15FunctionContextERKNS1_6IntValE'],
[['repeat'], 'VARCHAR', ['VARCHAR', 'INT'],
diff --git a/gensrc/script/doris_functions.py b/gensrc/script/doris_functions.py
index 8425603..a651747 100755
--- a/gensrc/script/doris_functions.py
+++ b/gensrc/script/doris_functions.py
@@ -107,6 +107,7 @@ functions = [
['String_Left', 'VARCHAR', ['VARCHAR', 'INT'], 'StringFunctions::left',
['strleft', 'left']],
['String_Right', 'VARCHAR', ['VARCHAR', 'INT'], 'StringFunctions::right',
['strright', 'right']],
['String_Ends_With', 'BOOLEAN', ['VARCHAR', 'VARCHAR'],
'StringFunctions::ends_with', ['ends_with']],
+ ['String_Starts_With', 'BOOLEAN', ['VARCHAR', 'VARCHAR'],
'StringFunctions::starts_with', ['starts_with']],
['String_Length', 'INT', ['VARCHAR'], 'StringFunctions::length', ['length']],
['String_Lower', 'VARCHAR', ['VARCHAR'], 'StringFunctions::lower', ['lower',
'lcase']],
['String_Upper', 'VARCHAR', ['VARCHAR'], 'StringFunctions::upper', ['upper',
'ucase']],
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]