You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2020/08/13 15:13:12 UTC

[GitHub] [incubator-doris] morningman commented on a change in pull request #4347: udf: str_replace function

morningman commented on a change in pull request #4347:
URL: https://github.com/apache/incubator-doris/pull/4347#discussion_r470004948



##########
File path: docs/zh-CN/sql-reference/sql-functions/string-functions/str_replace.md
##########
@@ -0,0 +1,46 @@
+---
+{
+    "title": "str_replace",

Review comment:
       In MySQL, this function named `replace()`, so better to name it as `replace()`

##########
File path: docs/en/sql-reference/sql-functions/string-functions/str_replace.md
##########
@@ -0,0 +1,46 @@
+---
+{
+    "title": "str_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.
+-->
+
+# str_replace
+## description
+### Syntax
+
+`VARCHAR STR_REPLACE (VARCHAR str, VARCHAR old, VARCHAR new)`
+
+replace all old substring with new substring in str 
+
+## example
+
+```
+mysql> select str_replace("http://www.baidu.com:9090", "9090", "");
++------------------------------------------------------+
+| str_replace('http://www.baidu.com:9090', '9090', '') |
++------------------------------------------------------+
+| http://www.baidu.com:                                |
++------------------------------------------------------+
+```
+##keyword

Review comment:
       ```suggestion
   ## keyword
   ```

##########
File path: be/src/exprs/string_functions.cpp
##########
@@ -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::str_replace(FunctionContext *context, const StringVal &origStr, const StringVal &oldStr, const StringVal &newStr) {
+    if (origStr.is_null || oldStr.is_null || newStr.is_null) {
+        return origStr;

Review comment:
       Should return null if any of them is null

##########
File path: be/test/exprs/string_functions_test.cpp
##########
@@ -494,6 +494,19 @@ TEST_F(StringFunctionsTest, rpad) {
     ASSERT_EQ(StringVal("呵呵hih"),
               StringFunctions::rpad(ctx, StringVal("呵呵"), IntVal(5), StringVal("hi")));
 }
+
+TEST_F(StringFunctionsTest, str_replace) {
+    ASSERT_EQ(StringVal("http://www.baidu.com:8080"),
+            StringFunctions::str_replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal("9090"), StringVal("8080")));
+
+    ASSERT_EQ(StringVal("http://www.baidu.com:9090"),
+        StringFunctions::str_replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal("9070"), StringVal("8080")));
+    ASSERT_EQ(StringVal("http://www.baidu.com:9090"),
+        StringFunctions::str_replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal(""), StringVal("8080")));
+    ASSERT_EQ(StringVal("http://www.baidu.com:"),
+        StringFunctions::str_replace(ctx, StringVal("http://www.baidu.com:9090"), StringVal("9090"), StringVal("")));

Review comment:
       Better to add more tests to test to corner case, like `null`, `empty string`, `expr2 longer than expr1`, Chinese char, etc...




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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