Skip to content

Conversation

@xinghuayu007
Copy link
Contributor

@xinghuayu007 xinghuayu007 commented Aug 13, 2020

Proposed changes

replace is an user defined function, which is to replace all old substrings with a new substring in a string, as follow:
mysql> select replace("http://www.baidu.com:9090", "9090", "");
+------------------------------------------------------+
| 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)
  • 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
  • Compiling and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • If this change need a document change, I have updated the document
  • 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...

@@ -0,0 +1,46 @@
---
{
"title": "str_replace",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In MySQL, this function named replace(), so better to name it as replace()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

| http://www.baidu.com: |
+------------------------------------------------------+
```
##keyword
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
##keyword
## keyword


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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return null if any of them is null

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("")));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

morningman
morningman previously approved these changes Aug 14, 2020
Copy link
Contributor

@morningman morningman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@morningman
Copy link
Contributor

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'

@morningman morningman self-assigned this Aug 16, 2020
@morningman morningman added area/sql/function Issues or PRs related to the SQL functions kind/feature Categorizes issue or PR as related to a new feature. labels Aug 16, 2020
@xinghuayu007
Copy link
Contributor Author

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!

@morningman
Copy link
Contributor

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

@xinghuayu007 xinghuayu007 changed the title udf: str_replace function udf: eplace function Aug 18, 2020
@xinghuayu007 xinghuayu007 changed the title udf: eplace function udf: replace function Aug 18, 2020
@xinghuayu007
Copy link
Contributor Author

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

I have fixed PR.

Copy link
Contributor

@morningman morningman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@morningman morningman added the approved Indicates a PR has been approved by one committer. label Aug 18, 2020
@morningman morningman merged commit bfb39a2 into apache:master Aug 20, 2020
acelyc111 pushed a commit to acelyc111/incubator-doris that referenced this pull request Aug 21, 2020
replace is an user defined function, which is to replace all old substrings with a new substring in a string, as follow:
mysql> select replace("http://www.baidu.com:9090", "9090", "");
+------------------------------------------------------+
| replace('http://www.baidu.com:9090', '9090', '') |
+------------------------------------------------------+
| http://www.baidu.com: |
+------------------------------------------------------+
@yangzhg yangzhg mentioned this pull request Feb 9, 2021
yiguolei pushed a commit to yiguolei/incubator-doris that referenced this pull request Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by one committer. area/sql/function Issues or PRs related to the SQL functions kind/feature Categorizes issue or PR as related to a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants