generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 180
Support Regex for replace eval function #4456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
75d30cb
Support Regex for replace eval function
ahkcs 16c140a
remove UDF and use REGEXP_REPLACE_3
ahkcs 4c5b55d
Add PCRE-to-Java backreference conversion
ahkcs cda7a2f
remove verbose comment
ahkcs 89a03e0
remove more
ahkcs 7024259
move from rexNodeVisitor to PPLFuncImpTable
ahkcs 343367a
error code handling
ahkcs 91e6f7f
update error code handling
ahkcs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,9 +207,15 @@ REPLACE | |
| Description | ||
| >>>>>>>>>>> | ||
|
|
||
| Usage: replace(str, substr, newstr) returns a string with all occurrences of substr replaced by newstr in str. If any argument is NULL, the function returns NULL. | ||
| Usage: replace(str, pattern, replacement) returns a string with all occurrences of the pattern replaced by the replacement string in str. If any argument is NULL, the function returns NULL. | ||
|
|
||
| Example:: | ||
| **Regular Expression Support**: The pattern argument supports Java regex syntax, including: | ||
|
|
||
| Argument type: STRING, STRING (regex pattern), STRING (replacement) | ||
|
|
||
| Return type: STRING | ||
|
|
||
| Literal String Replacement Examples:: | ||
|
|
||
| os> source=people | eval `REPLACE('helloworld', 'world', 'universe')` = REPLACE('helloworld', 'world', 'universe'), `REPLACE('helloworld', 'invalid', 'universe')` = REPLACE('helloworld', 'invalid', 'universe') | fields `REPLACE('helloworld', 'world', 'universe')`, `REPLACE('helloworld', 'invalid', 'universe')` | ||
| fetched rows / total rows = 1/1 | ||
|
|
@@ -219,6 +225,41 @@ Example:: | |
| | hellouniverse | helloworld | | ||
| +--------------------------------------------+----------------------------------------------+ | ||
|
|
||
| Regex Pattern Examples:: | ||
|
|
||
| os> source=people | eval `Remove digits` = REPLACE('test123', '\\d+', ''), `Collapse spaces` = REPLACE('hello world', ' +', ' '), `Remove special` = REPLACE('hello@world!', '[^a-zA-Z]', '') | fields `Remove digits`, `Collapse spaces`, `Remove special` | ||
| fetched rows / total rows = 1/1 | ||
| +---------------+-----------------+----------------+ | ||
| | Remove digits | Collapse spaces | Remove special | | ||
| |---------------+-----------------+----------------| | ||
| | test | hello world | helloworld | | ||
| +---------------+-----------------+----------------+ | ||
|
|
||
| Capture Group and Backreference Examples:: | ||
|
|
||
| os> source=people | eval `Swap date` = REPLACE('1/14/2023', '^(\\d{1,2})/(\\d{1,2})/', '$2/$1/'), `Reverse words` = REPLACE('Hello World', '(\\w+) (\\w+)', '$2 $1'), `Extract domain` = REPLACE('[email protected]', '.*@(.+)', '$1') | fields `Swap date`, `Reverse words`, `Extract domain` | ||
| fetched rows / total rows = 1/1 | ||
| +-----------+---------------+----------------+ | ||
| | Swap date | Reverse words | Extract domain | | ||
| |-----------+---------------+----------------| | ||
| | 14/1/2023 | World Hello | example.com | | ||
| +-----------+---------------+----------------+ | ||
|
|
||
| Advanced Regex Examples:: | ||
|
|
||
| os> source=people | eval `Clean phone` = REPLACE('(555) 123-4567', '[^0-9]', ''), `Remove vowels` = REPLACE('hello world', '[aeiou]', ''), `Add prefix` = REPLACE('test', '^', 'pre_') | fields `Clean phone`, `Remove vowels`, `Add prefix` | ||
| fetched rows / total rows = 1/1 | ||
| +-------------+---------------+------------+ | ||
| | Clean phone | Remove vowels | Add prefix | | ||
| |-------------+---------------+------------| | ||
| | 5551234567 | hll wrld | pre_test | | ||
| +-------------+---------------+------------+ | ||
|
|
||
| **Note**: When using regex patterns in PPL queries: | ||
|
|
||
| * Backslashes must be escaped (use ``\\`` instead of ``\``) - e.g., ``\\d`` for digit pattern, ``\\w+`` for word characters | ||
| * Backreferences support both PCRE-style (``\1``, ``\2``, etc.) and Java-style (``$1``, ``$2``, etc.) syntax. PCRE-style backreferences are automatically converted to Java-style internally. | ||
|
|
||
|
|
||
| REVERSE | ||
| ------- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| package org.opensearch.sql.calcite.remote; | ||
|
|
||
| import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT; | ||
| import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_STATE_COUNTRY; | ||
| import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_STATE_COUNTRY_WITH_NULL; | ||
| import static org.opensearch.sql.util.MatcherUtils.*; | ||
|
|
@@ -24,6 +25,7 @@ public void init() throws Exception { | |
|
|
||
| loadIndex(Index.STATE_COUNTRY); | ||
| loadIndex(Index.STATE_COUNTRY_WITH_NULL); | ||
| loadIndex(Index.ACCOUNT); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -300,6 +302,77 @@ public void testReplace() throws IOException { | |
| verifyDataRows(actual, rows("Jane", 20, "heLLo")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReplaceWithRegexPattern() throws IOException { | ||
| JSONObject actual = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where account_number = 1 | eval street_only = replace(address," | ||
| + " '\\\\d+ ', '') | fields address, street_only", | ||
| TEST_INDEX_ACCOUNT)); | ||
|
|
||
| verifySchema(actual, schema("address", "string"), schema("street_only", "string")); | ||
|
|
||
| verifyDataRows(actual, rows("880 Holmes Lane", "Holmes Lane")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReplaceWithCaptureGroups() throws IOException { | ||
| JSONObject actual = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where account_number = 1 | eval swapped = replace(firstname," | ||
| + " '^(.)(.)', '\\\\2\\\\1') | fields firstname, swapped", | ||
| TEST_INDEX_ACCOUNT)); | ||
|
|
||
| verifySchema(actual, schema("firstname", "string"), schema("swapped", "string")); | ||
|
|
||
| verifyDataRows(actual, rows("Amber", "mAber")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReplaceWithEmailDomainReplacement() throws IOException { | ||
| JSONObject actual = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where account_number = 1 | eval new_email =" | ||
| + " replace(email, '([^@]+)@(.+)', '\\\\[email protected]') | fields email," | ||
| + " new_email", | ||
| TEST_INDEX_ACCOUNT)); | ||
|
|
||
| verifySchema(actual, schema("email", "string"), schema("new_email", "string")); | ||
|
|
||
| verifyDataRows(actual, rows("[email protected]", "[email protected]")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReplaceWithCharacterClasses() throws IOException { | ||
| JSONObject actual = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where account_number = 1 | eval masked = replace(address, '[a-zA-Z]'," | ||
| + " 'X') | fields address, masked", | ||
| TEST_INDEX_ACCOUNT)); | ||
|
|
||
| verifySchema(actual, schema("address", "string"), schema("masked", "string")); | ||
|
|
||
| verifyDataRows(actual, rows("880 Holmes Lane", "880 XXXXXX XXXX")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReplaceWithAnchors() throws IOException { | ||
| JSONObject actual = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where account_number = 1 | eval street_name = replace(address," | ||
| + " '^\\\\d+\\\\s+', '') | fields address, street_name", | ||
| TEST_INDEX_ACCOUNT)); | ||
|
|
||
| verifySchema(actual, schema("address", "string"), schema("street_name", "string")); | ||
|
|
||
| verifyDataRows(actual, rows("880 Holmes Lane", "Holmes Lane")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLeft() throws IOException { | ||
| JSONObject actual = | ||
|
|
@@ -326,6 +399,51 @@ public void testStrCmp() throws IOException { | |
| verifyDataRows(actual, rows("Jane", 20)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReplaceWithInvalidRegexPattern() { | ||
| // Test invalid regex pattern - unclosed character class | ||
| Throwable e1 = | ||
| assertThrowsWithReplace( | ||
| Exception.class, | ||
| () -> | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | eval result = replace(firstname, '[unclosed', 'X') | fields" | ||
| + " firstname, result", | ||
| TEST_INDEX_ACCOUNT))); | ||
| verifyErrorMessageContains(e1, "Invalid regex pattern"); | ||
| verifyErrorMessageContains(e1, "Unclosed character class"); | ||
| verifyErrorMessageContains(e1, "400 Bad Request"); | ||
|
|
||
| // Test invalid regex pattern - unclosed group | ||
| Throwable e2 = | ||
| assertThrowsWithReplace( | ||
| Exception.class, | ||
| () -> | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | eval result = replace(firstname, '(invalid', 'X') | fields" | ||
| + " firstname, result", | ||
| TEST_INDEX_ACCOUNT))); | ||
| verifyErrorMessageContains(e2, "Invalid regex pattern"); | ||
| verifyErrorMessageContains(e2, "Unclosed group"); | ||
| verifyErrorMessageContains(e2, "400 Bad Request"); | ||
|
|
||
| // Test invalid regex pattern - dangling metacharacter | ||
| Throwable e3 = | ||
| assertThrowsWithReplace( | ||
| Exception.class, | ||
| () -> | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | eval result = replace(firstname, '?invalid', 'X') | fields" | ||
| + " firstname, result", | ||
| TEST_INDEX_ACCOUNT))); | ||
| verifyErrorMessageContains(e3, "Invalid regex pattern"); | ||
| verifyErrorMessageContains(e3, "Dangling meta character"); | ||
| verifyErrorMessageContains(e3, "400 Bad Request"); | ||
| } | ||
|
|
||
| private void prepareTrim() throws IOException { | ||
| Request request1 = | ||
| new Request("PUT", "/opensearch-sql_test_index_state_country/_doc/5?refresh=true"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.