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
19 changes: 19 additions & 0 deletions be/src/exprs/string_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,4 +998,23 @@ StringVal StringFunctions::split_part(FunctionContext* context, const StringVal&
int len = (find[field.val - 1] == -1 ? content.len : find[field.val - 1]) - start_pos;
return StringVal(content.ptr + start_pos, len);
}

StringVal StringFunctions::replace(FunctionContext *context, const StringVal &origStr, const StringVal &oldStr, const StringVal &newStr) {
if (origStr.is_null || oldStr.is_null || newStr.is_null) {
return StringVal::null();
}
std::string orig_str = std::string(reinterpret_cast<const char *>(origStr.ptr), origStr.len);
std::string old_str = std::string(reinterpret_cast<const char *>(oldStr.ptr), oldStr.len);
std::string new_str = std::string(reinterpret_cast<const char *>(newStr.ptr), newStr.len);
std::string::size_type pos = 0;
std::string::size_type oldLen = old_str.size();
std::string::size_type newLen = new_str.size();
while ((pos = orig_str.find(old_str, pos)))
{
if(pos == std::string::npos) break;
orig_str.replace(pos, oldLen, new_str);
pos += newLen;
}
return AnyValUtil::from_string_temp(context, orig_str);
}
}
3 changes: 3 additions & 0 deletions be/src/exprs/string_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ class StringFunctions {

static StringVal split_part(FunctionContext* context, const StringVal& content,
const StringVal& delimiter, const IntVal& field);

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

Expand Down
35 changes: 35 additions & 0 deletions be/test/exprs/string_functions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,41 @@ TEST_F(StringFunctionsTest, rpad) {
ASSERT_EQ(StringVal("呵呵hih"),
StringFunctions::rpad(ctx, StringVal("呵呵"), IntVal(5), StringVal("hi")));
}

TEST_F(StringFunctionsTest, replace) {
//exist substring
ASSERT_EQ(StringVal("http://www.baidu.com:8080"),
StringFunctions::replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal("9090"), StringVal("8080")));

//not exist substring
ASSERT_EQ(StringVal("http://www.baidu.com:9090"),
StringFunctions::replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal("9070"), StringVal("8080")));

//old substring is empty
ASSERT_EQ(StringVal("http://www.baidu.com:9090"),
StringFunctions::replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal(""), StringVal("8080")));

//new substring is empty
ASSERT_EQ(StringVal("http://www.baidu.com:"),
StringFunctions::replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal("9090"), StringVal("")));

//origin string is null
ASSERT_EQ(StringVal::null(),
StringFunctions::replace(ctx, StringVal::null(), StringVal("hello"), StringVal("8080")));

//old substring is null
ASSERT_EQ(StringVal::null(),
StringFunctions::replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal::null(), StringVal("8080")));

//new substring is null
ASSERT_EQ(StringVal::null(),
StringFunctions::replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal("hello"), StringVal::null()));

//substring contains Chinese character
ASSERT_EQ(StringVal("http://华夏zhongguo:9090"),
StringFunctions::replace(ctx, StringVal("http://中国hello:9090"), StringVal("中国hello"), StringVal("华夏zhongguo")));
}

} // namespace doris

int main(int argc, char** argv) {
Expand Down
46 changes: 46 additions & 0 deletions docs/en/sql-reference/sql-functions/string-functions/replace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
{
"title": "replace",
"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.
-->

# replace
## description
### Syntax

`VARCHAR REPLACE (VARCHAR str, VARCHAR old, VARCHAR new)`

replace all old substring with new substring in str

## example

```
mysql> select replace("http://www.baidu.com:9090", "9090", "");
+------------------------------------------------------+
| replace('http://www.baidu.com:9090', '9090', '') |
+------------------------------------------------------+
| http://www.baidu.com: |
+------------------------------------------------------+
```
## keyword
REPLACE
46 changes: 46 additions & 0 deletions docs/zh-CN/sql-reference/sql-functions/string-functions/replace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
{
"title": "replace",
"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.
-->

# replace
## description
### Syntax

`VARCHAR REPLACE (VARCHAR str, VARCHAR old, VARCHAR new)`

将str字符串中的old子串全部替换为new串

## example

```
mysql> select replace("http://www.baidu.com:9090", "9090", "");
+------------------------------------------------------+
| replace('http://www.baidu.com:9090', '9090', '') |
+------------------------------------------------------+
| http://www.baidu.com: |
+------------------------------------------------------+
```
## keyword
REPLACE
2 changes: 2 additions & 0 deletions fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -4593,6 +4593,8 @@ keyword ::=
{: RESULT = id; :}
| KW_REPEATABLE:id
{: RESULT = id; :}
| KW_REPLACE:id
{: RESULT = id; :}
| KW_REPLACE_IF_NOT_NULL:id
{: RESULT = id; :}
| KW_REPOSITORY:id
Expand Down
2 changes: 2 additions & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,8 @@
'15FunctionContextENS2_18FunctionStateScopeE'],
[['concat'], 'VARCHAR', ['VARCHAR', '...'],
'_ZN5doris15StringFunctions6concatEPN9doris_udf15FunctionContextEiPKNS1_9StringValE'],
[['replace'], 'VARCHAR', ['VARCHAR', 'VARCHAR', 'VARCHAR'],
'_ZN5doris15StringFunctions7replaceEPN9doris_udf15FunctionContextERKNS1_9StringValES6_S6_'],
[['concat_ws'], 'VARCHAR', ['VARCHAR', 'VARCHAR', '...'],
'_ZN5doris15StringFunctions9concat_wsEPN9doris_udf'
'15FunctionContextERKNS1_9StringValEiPS5_'],
Expand Down