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 14:13:55 UTC

[GitHub] [incubator-doris] xinghuayu007 opened a new pull request #4347: udf: str_replace function

xinghuayu007 opened a new pull request #4347:
URL: https://github.com/apache/incubator-doris/pull/4347


   ## Proposed changes
   
   str_replace is an user defined function, which is to replace all old substrings with a new substring in a string, as follow: 
   mysql> select str_replace("http://www.baidu.com:9090", "9090", "");
   +------------------------------------------------------+
   | str_replace('http://www.baidu.com:9090', '9090', '') |
   +------------------------------------------------------+
   | http://www.baidu.com:                                |
   +------------------------------------------------------+
   
   
   ## Types of changes
   
   What types of changes does your code introduce to Doris?
   _Put an `x` in the boxes that apply_
   
   - [] Bugfix (non-breaking change which fixes an issue)
   - [x] New feature (non-breaking change which adds functionality)
   - [] Breaking change (fix or feature that would cause existing functionality to not work as expected)
   - [] Documentation Update (if none of the other choices apply)
   - [] Code refactor (Modify the code structure, format the code, etc...)
   
   ## Checklist
   
   _Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code._
   
   - [] I have create an issue on (Fix #ISSUE), and have described the bug/feature there in detail
   - [x] Compiling and unit tests pass locally with my changes
   - [x] I have added tests that prove my fix is effective or that my feature works
   - [x] If this change need a document change, I have updated the document
   - [x] Any dependent changes have been merged
   
   ## Further comments
   
   If this is a relatively large or complex change, kick off the discussion at dev@doris.apache.org by explaining why you chose the solution you did and what alternatives you considered, 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


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

Posted by GitBox <gi...@apache.org>.
morningman commented on pull request #4347:
URL: https://github.com/apache/incubator-doris/pull/4347#issuecomment-674540296


   I tested the name `replace` and it works. So I suggest to rename the `str_replace` to `replace`.
   
   You can just patch the following `diff` file to your code.
   
   ```
   diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup
   index cd4ea3cdb..7b5e8d2d2 100644
   --- a/fe/fe-core/src/main/cup/sql_parser.cup
   +++ b/fe/fe-core/src/main/cup/sql_parser.cup
   @@ -4592,6 +4592,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
   diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py
   index c966b7d8f..ed26fae15 100755
   --- a/gensrc/script/doris_builtins_functions.py
   +++ b/gensrc/script/doris_builtins_functions.py
   @@ -605,7 +605,7 @@ visible_functions = [
                '15FunctionContextENS2_18FunctionStateScopeE'],
        [['concat'], 'VARCHAR', ['VARCHAR', '...'],
                '_ZN5doris15StringFunctions6concatEPN9doris_udf15FunctionContextEiPKNS1_9StringValE'],
   -    [['str_replace'], 'VARCHAR', ['VARCHAR', 'VARCHAR', 'VARCHAR'],
   +    [['replace'], 'VARCHAR', ['VARCHAR', 'VARCHAR', 'VARCHAR'],
                '_ZN5doris15StringFunctions11str_replaceEPN9doris_udf15FunctionContextERKNS1_9StringValES6_S6_'],
        [['concat_ws'], 'VARCHAR', ['VARCHAR', 'VARCHAR', '...'],
                '_ZN5doris15StringFunctions9concat_wsEPN9doris_udf'
   ```


----------------------------------------------------------------
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


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

Posted by GitBox <gi...@apache.org>.
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


[GitHub] [incubator-doris] xinghuayu007 commented on pull request #4347: udf: replace function

Posted by GitBox <gi...@apache.org>.
xinghuayu007 commented on pull request #4347:
URL: https://github.com/apache/incubator-doris/pull/4347#issuecomment-675279285


   > Hi @xinghuayu007 , please let know if you finish modifying this PR, thank you.
   
   I have fixed PR.


----------------------------------------------------------------
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


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

Posted by GitBox <gi...@apache.org>.
morningman commented on pull request #4347:
URL: https://github.com/apache/incubator-doris/pull/4347#issuecomment-674940135


   Hi @xinghuayu007 , please let know if you finish modifying this PR, thank you.


----------------------------------------------------------------
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


[GitHub] [incubator-doris] xinghuayu007 commented on pull request #4347: udf: str_replace function

Posted by GitBox <gi...@apache.org>.
xinghuayu007 commented on pull request #4347:
URL: https://github.com/apache/incubator-doris/pull/4347#issuecomment-674681260


   > I tested the name `replace` and it works. So I suggest to rename the `str_replace` to `replace`.
   > 
   > You can just patch the following `diff` file to your code.
   > 
   > ```
   > diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup
   > index cd4ea3cdb..7b5e8d2d2 100644
   > --- a/fe/fe-core/src/main/cup/sql_parser.cup
   > +++ b/fe/fe-core/src/main/cup/sql_parser.cup
   > @@ -4592,6 +4592,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
   > diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py
   > index c966b7d8f..ed26fae15 100755
   > --- a/gensrc/script/doris_builtins_functions.py
   > +++ b/gensrc/script/doris_builtins_functions.py
   > @@ -605,7 +605,7 @@ visible_functions = [
   >              '15FunctionContextENS2_18FunctionStateScopeE'],
   >      [['concat'], 'VARCHAR', ['VARCHAR', '...'],
   >              '_ZN5doris15StringFunctions6concatEPN9doris_udf15FunctionContextEiPKNS1_9StringValE'],
   > -    [['str_replace'], 'VARCHAR', ['VARCHAR', 'VARCHAR', 'VARCHAR'],
   > +    [['replace'], 'VARCHAR', ['VARCHAR', 'VARCHAR', 'VARCHAR'],
   >              '_ZN5doris15StringFunctions11str_replaceEPN9doris_udf15FunctionContextERKNS1_9StringValES6_S6_'],
   >      [['concat_ws'], 'VARCHAR', ['VARCHAR', 'VARCHAR', '...'],
   >              '_ZN5doris15StringFunctions9concat_wsEPN9doris_udf'
   > ```
   
   Thxs!


----------------------------------------------------------------
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


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

Posted by GitBox <gi...@apache.org>.
xinghuayu007 commented on a change in pull request #4347:
URL: https://github.com/apache/incubator-doris/pull/4347#discussion_r470363837



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

Review comment:
       replace is a keyword has been used by sql syntax analysis.




----------------------------------------------------------------
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


[GitHub] [incubator-doris] morningman merged pull request #4347: udf: replace function

Posted by GitBox <gi...@apache.org>.
morningman merged pull request #4347:
URL: https://github.com/apache/incubator-doris/pull/4347


   


----------------------------------------------------------------
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