Support append_trailing_char_if_absent function (#3439)
This commit is contained in:
@ -256,6 +256,25 @@ StringVal StringFunctions::rpad(
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
StringVal StringFunctions::append_trailing_char_if_absent(doris_udf::FunctionContext* context,
|
||||
const doris_udf::StringVal& str, const doris_udf::StringVal& trailing_char) {
|
||||
if (str.is_null || trailing_char.is_null || trailing_char.len != 1) {
|
||||
return StringVal::null();
|
||||
}
|
||||
if (str.len == 0) {
|
||||
return trailing_char;
|
||||
}
|
||||
if (str.ptr[str.len - 1] == trailing_char.ptr[0]) {
|
||||
return str;
|
||||
}
|
||||
|
||||
StringVal result(context, str.len + 1);
|
||||
memcpy(result.ptr, str.ptr, str.len);
|
||||
result.ptr[str.len] = trailing_char.ptr[0];
|
||||
return result;
|
||||
}
|
||||
|
||||
// Implementation of LENGTH
|
||||
// int length(string input)
|
||||
// Returns the length in bytes of input. If input == NULL, returns
|
||||
|
||||
@ -67,7 +67,10 @@ public:
|
||||
const doris_udf::IntVal& len, const doris_udf::StringVal& pad);
|
||||
static doris_udf::StringVal rpad(
|
||||
doris_udf::FunctionContext* context, const doris_udf::StringVal& str,
|
||||
const doris_udf::IntVal& len, const doris_udf::StringVal& pad);
|
||||
const doris_udf::IntVal& len, const doris_udf::StringVal& pad);
|
||||
static doris_udf::StringVal append_trailing_char_if_absent(
|
||||
doris_udf::FunctionContext* context, const doris_udf::StringVal& str,
|
||||
const doris_udf::StringVal& trailing_char);
|
||||
static doris_udf::IntVal length(
|
||||
doris_udf::FunctionContext* context, const doris_udf::StringVal& str);
|
||||
static doris_udf::IntVal char_utf8_length(
|
||||
|
||||
@ -28,8 +28,19 @@ namespace doris {
|
||||
|
||||
class StringFunctionsTest : public testing::Test {
|
||||
public:
|
||||
StringFunctionsTest() {
|
||||
StringFunctionsTest() = default;
|
||||
|
||||
void SetUp() {
|
||||
utils = new FunctionUtils();
|
||||
ctx = utils->get_fn_ctx();
|
||||
}
|
||||
void TearDown() {
|
||||
delete utils;
|
||||
}
|
||||
|
||||
private:
|
||||
FunctionUtils* utils;
|
||||
FunctionContext* ctx;
|
||||
};
|
||||
|
||||
TEST_F(StringFunctionsTest, money_format_bigint) {
|
||||
@ -319,14 +330,37 @@ TEST_F(StringFunctionsTest, length) {
|
||||
StringFunctions::length(context, StringVal("")));
|
||||
ASSERT_EQ(IntVal(0),
|
||||
StringFunctions::char_utf8_length(context, StringVal("")));
|
||||
|
||||
|
||||
ASSERT_EQ(IntVal(11),
|
||||
StringFunctions::length(context, StringVal("hello你好")));
|
||||
|
||||
|
||||
ASSERT_EQ(IntVal(7),
|
||||
StringFunctions::char_utf8_length(context, StringVal("hello你好")));
|
||||
}
|
||||
|
||||
TEST_F(StringFunctionsTest, append_trailing_char_if_absent) {
|
||||
ASSERT_EQ(StringVal("ac"), StringFunctions::append_trailing_char_if_absent(ctx,
|
||||
StringVal("a"), StringVal("c")));
|
||||
|
||||
ASSERT_EQ(StringVal("c"), StringFunctions::append_trailing_char_if_absent(ctx,
|
||||
StringVal("c"), StringVal("c")));
|
||||
|
||||
ASSERT_EQ(StringVal("123c"), StringFunctions::append_trailing_char_if_absent(ctx,
|
||||
StringVal("123c"), StringVal("c")));
|
||||
|
||||
ASSERT_EQ(StringVal("c"), StringFunctions::append_trailing_char_if_absent(ctx,
|
||||
StringVal(""), StringVal("c")));
|
||||
|
||||
ASSERT_EQ(StringVal::null(), StringFunctions::append_trailing_char_if_absent(ctx,
|
||||
StringVal::null(), StringVal("c")));
|
||||
|
||||
ASSERT_EQ(StringVal::null(), StringFunctions::append_trailing_char_if_absent(ctx,
|
||||
StringVal("a"), StringVal::null()));
|
||||
|
||||
ASSERT_EQ(StringVal::null(), StringFunctions::append_trailing_char_if_absent(ctx,
|
||||
StringVal("a"), StringVal("abc")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@ -195,6 +195,7 @@ module.exports = [
|
||||
title: "String Functions",
|
||||
directoryPath: "string-functions/",
|
||||
children: [
|
||||
"append_trailing_char_if_absent",
|
||||
"ascii",
|
||||
"concat",
|
||||
"concat_ws",
|
||||
|
||||
@ -207,6 +207,7 @@ module.exports = [
|
||||
title: "字符串函数",
|
||||
directoryPath: "string-functions/",
|
||||
children: [
|
||||
"append_trailing_char_if_absent",
|
||||
"ascii",
|
||||
"concat",
|
||||
"concat_ws",
|
||||
|
||||
@ -0,0 +1,60 @@
|
||||
---
|
||||
{
|
||||
"title": "append_trailing_char_if_absent",
|
||||
"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.
|
||||
-->
|
||||
|
||||
# append_trailing_char_if_absent
|
||||
|
||||
## description
|
||||
|
||||
### Syntax
|
||||
|
||||
`VARCHAR append_trailing_char_if_absent(VARCHAR str, VARCHAR trailing_char)`
|
||||
|
||||
If the s string is non-empty and does not contain the c character at the end, it appends the c character to the end.
|
||||
Trailing_char contains only one character, and it will return NULL if contains more than one character
|
||||
|
||||
## example
|
||||
|
||||
```
|
||||
MySQL [test]> select append_trailing_char_if_absent('a','c');
|
||||
+------------------------------------------+
|
||||
| append_trailing_char_if_absent('a', 'c') |
|
||||
+------------------------------------------+
|
||||
| ac |
|
||||
+------------------------------------------+
|
||||
1 row in set (0.02 sec)
|
||||
|
||||
MySQL [test]> select append_trailing_char_if_absent('ac','c');
|
||||
+-------------------------------------------+
|
||||
| append_trailing_char_if_absent('ac', 'c') |
|
||||
+-------------------------------------------+
|
||||
| ac |
|
||||
+-------------------------------------------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
## keyword
|
||||
|
||||
APPEND_TRAILING_CHAR_IF_ABSENT
|
||||
@ -0,0 +1,60 @@
|
||||
---
|
||||
{
|
||||
"title": "append_trailing_char_if_absent",
|
||||
"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.
|
||||
-->
|
||||
|
||||
# append_trailing_char_if_absent
|
||||
|
||||
## description
|
||||
|
||||
### Syntax
|
||||
|
||||
`VARCHAR append_trailing_char_if_absent(VARCHAR str, VARCHAR trailing_char)`
|
||||
|
||||
如果's'字符串非空并且末尾不包含'c'字符,则将'c'字符附加到末尾。
|
||||
trailing_char只包含一个字符,如果包含多个字符,将返回NULL
|
||||
|
||||
## example
|
||||
|
||||
```
|
||||
MySQL [test]> select append_trailing_char_if_absent('a','c');
|
||||
+------------------------------------------+
|
||||
| append_trailing_char_if_absent('a', 'c') |
|
||||
+------------------------------------------+
|
||||
| ac |
|
||||
+------------------------------------------+
|
||||
1 row in set (0.02 sec)
|
||||
|
||||
MySQL [test]> select append_trailing_char_if_absent('ac','c');
|
||||
+-------------------------------------------+
|
||||
| append_trailing_char_if_absent('ac', 'c') |
|
||||
+-------------------------------------------+
|
||||
| ac |
|
||||
+-------------------------------------------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
## keyword
|
||||
|
||||
APPEND_TRAILING_CHAR_IF_ABSENT
|
||||
@ -531,6 +531,8 @@ visible_functions = [
|
||||
[['rpad'], 'VARCHAR', ['VARCHAR', 'INT', 'VARCHAR'],
|
||||
'_ZN5doris15StringFunctions4rpadEPN9doris_udf'
|
||||
'15FunctionContextERKNS1_9StringValERKNS1_6IntValES6_'],
|
||||
[['append_trailing_char_if_absent'], 'VARCHAR', ['VARCHAR', 'VARCHAR'],
|
||||
'_ZN5doris15StringFunctions30append_trailing_char_if_absentEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'],
|
||||
[['length'], 'INT', ['VARCHAR'],
|
||||
'_ZN5doris15StringFunctions6lengthEPN9doris_udf15FunctionContextERKNS1_9StringValE'],
|
||||
[['char_length', 'character_length'], 'INT', ['VARCHAR'],
|
||||
|
||||
Reference in New Issue
Block a user