generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
[Feature] Implementation of mode sed and offset_field in rex PPL command
#4241
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
7bbec52
[Feature] Implementation of mode sed and offset_field in rex PPL command
RyanL1997 83d7228
update rex rst doc
RyanL1997 702ac02
chen - address comment and merge grammar in parser
RyanL1997 3cc1cf2
chen - limit offset field only in extraction mode
RyanL1997 11d9dd7
chen - specify exception type of o_f UDF
RyanL1997 9b553ee
chen - add exception type of o_f UDF - 2
RyanL1997 081f44a
chen - add exception type of o_f UDF - also fix the test
RyanL1997 295c49a
chen - alphabetical order of o_f return
RyanL1997 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
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
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
94 changes: 94 additions & 0 deletions
94
core/src/main/java/org/opensearch/sql/expression/function/udf/RexOffsetFunction.java
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function.udf; | ||
|
|
||
| import java.util.List; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.regex.PatternSyntaxException; | ||
| import org.apache.calcite.adapter.enumerable.NotNullImplementor; | ||
| import org.apache.calcite.adapter.enumerable.NullPolicy; | ||
| import org.apache.calcite.adapter.enumerable.RexToLixTranslator; | ||
| import org.apache.calcite.linq4j.tree.Expression; | ||
| import org.apache.calcite.linq4j.tree.Expressions; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.sql.type.ReturnTypes; | ||
| import org.apache.calcite.sql.type.SqlReturnTypeInference; | ||
| import org.opensearch.sql.calcite.utils.PPLOperandTypes; | ||
| import org.opensearch.sql.expression.function.ImplementorUDF; | ||
| import org.opensearch.sql.expression.function.UDFOperandMetadata; | ||
|
|
||
| /** Custom REX_OFFSET function for calculating regex match positions. */ | ||
| public final class RexOffsetFunction extends ImplementorUDF { | ||
|
|
||
| public RexOffsetFunction() { | ||
| super(new RexOffsetImplementor(), NullPolicy.ARG0); | ||
| } | ||
|
|
||
| @Override | ||
| public SqlReturnTypeInference getReturnTypeInference() { | ||
| return ReturnTypes.VARCHAR_2000_NULLABLE; | ||
| } | ||
|
|
||
| @Override | ||
| public UDFOperandMetadata getOperandMetadata() { | ||
| return PPLOperandTypes.STRING_STRING; | ||
| } | ||
|
|
||
| private static class RexOffsetImplementor implements NotNullImplementor { | ||
|
|
||
| @Override | ||
| public Expression implement( | ||
| RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { | ||
| Expression field = translatedOperands.get(0); | ||
| Expression pattern = translatedOperands.get(1); | ||
|
|
||
| return Expressions.call(RexOffsetFunction.class, "calculateOffsets", field, pattern); | ||
| } | ||
| } | ||
|
|
||
| public static String calculateOffsets(String text, String patternStr) { | ||
| if (text == null || patternStr == null) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| Pattern pattern = Pattern.compile(patternStr); | ||
| Matcher matcher = pattern.matcher(text); | ||
|
|
||
| if (!matcher.find()) { | ||
| return null; | ||
| } | ||
|
|
||
| List<String> offsetPairs = new java.util.ArrayList<>(); | ||
|
|
||
| Pattern namedGroupPattern = Pattern.compile("\\(\\?<([^>]+)>"); | ||
| Matcher namedGroupMatcher = namedGroupPattern.matcher(patternStr); | ||
|
|
||
| int groupIndex = 1; | ||
|
|
||
| while (namedGroupMatcher.find()) { | ||
| String groupName = namedGroupMatcher.group(1); | ||
|
|
||
| if (groupIndex <= matcher.groupCount()) { | ||
| int start = matcher.start(groupIndex); | ||
| int end = matcher.end(groupIndex); | ||
|
|
||
| if (start >= 0 && end >= 0) { | ||
| offsetPairs.add(groupName + "=" + start + "-" + (end - 1)); | ||
| } | ||
| } | ||
| groupIndex++; | ||
| } | ||
|
|
||
| java.util.Collections.sort(offsetPairs); | ||
| return offsetPairs.isEmpty() ? null : String.join("&", offsetPairs); | ||
| } catch (PatternSyntaxException e) { | ||
| throw new IllegalArgumentException( | ||
| "Invalid regex pattern in rex command: " + e.getMessage(), e); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np: If sed/regex is common across different commands, it may worth creating dedicated AST expression nodes and separate these into its own visit method in the future.