-
Notifications
You must be signed in to change notification settings - Fork 214
[BugFix] Fix unexpected shift of extraction for rex with nested capture groups in named groups
#4641
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
[BugFix] Fix unexpected shift of extraction for rex with nested capture groups in named groups
#4641
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7b5ee2c
rex extraction fix
RyanL1997 44a969b
logic cleanup
RyanL1997 27487a6
fix the rex explain test
RyanL1997 82bad3c
fix rex UT
RyanL1997 ede5716
fix no pushdown rex explain IT
RyanL1997 25629e0
add IT for the fix
RyanL1997 bf77fe2
chen - add a todo for using name group API once we fully support JDK 20+
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,11 +16,15 @@ | |
| 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.CompositeOperandTypeChecker; | ||
| import org.apache.calcite.sql.type.OperandTypes; | ||
| import org.apache.calcite.sql.type.SqlReturnTypeInference; | ||
| import org.apache.calcite.sql.type.SqlTypeFamily; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
| import org.opensearch.sql.calcite.utils.PPLOperandTypes; | ||
| import org.opensearch.sql.expression.function.ImplementorUDF; | ||
| import org.opensearch.sql.expression.function.UDFOperandMetadata; | ||
| import org.opensearch.sql.expression.parse.RegexCommonUtils; | ||
|
|
||
| /** Custom REX_EXTRACT_MULTI function for extracting multiple regex matches. */ | ||
| public final class RexExtractMultiFunction extends ImplementorUDF { | ||
|
|
@@ -40,7 +44,17 @@ public SqlReturnTypeInference getReturnTypeInference() { | |
|
|
||
| @Override | ||
| public UDFOperandMetadata getOperandMetadata() { | ||
| return PPLOperandTypes.STRING_STRING_INTEGER_INTEGER; | ||
| // Support both (field, pattern, groupIndex, maxMatch) and (field, pattern, groupName, maxMatch) | ||
| return UDFOperandMetadata.wrap( | ||
| (CompositeOperandTypeChecker) | ||
| PPLOperandTypes.STRING_STRING_INTEGER_INTEGER | ||
| .getInnerTypeChecker() | ||
| .or( | ||
| OperandTypes.family( | ||
| SqlTypeFamily.CHARACTER, | ||
| SqlTypeFamily.CHARACTER, | ||
| SqlTypeFamily.CHARACTER, | ||
| SqlTypeFamily.INTEGER))); | ||
| } | ||
|
|
||
| private static class RexExtractMultiImplementor implements NotNullImplementor { | ||
|
|
@@ -50,35 +64,105 @@ public Expression implement( | |
| RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { | ||
| Expression field = translatedOperands.get(0); | ||
| Expression pattern = translatedOperands.get(1); | ||
| Expression groupIndex = translatedOperands.get(2); | ||
| Expression groupIndexOrName = translatedOperands.get(2); | ||
| Expression maxMatch = translatedOperands.get(3); | ||
|
|
||
| return Expressions.call( | ||
| RexExtractMultiFunction.class, | ||
| "extractMultipleGroups", | ||
| field, | ||
| pattern, | ||
| groupIndex, | ||
| groupIndexOrName, | ||
| maxMatch); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extract multiple regex groups by index (1-based). | ||
| * | ||
| * @param text The input text to extract from | ||
| * @param pattern The regex pattern | ||
| * @param groupIndex The 1-based group index to extract | ||
| * @param maxMatch Maximum number of matches to return (0 = unlimited) | ||
| * @return List of extracted values or null if no matches found | ||
| */ | ||
| public static List<String> extractMultipleGroups( | ||
| String text, String pattern, int groupIndex, int maxMatch) { | ||
| // Query planner already validates null inputs via NullPolicy.ARG0 | ||
| if (text == null || pattern == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return executeMultipleExtractions( | ||
| text, | ||
| pattern, | ||
| maxMatch, | ||
| matcher -> { | ||
| if (groupIndex > 0 && groupIndex <= matcher.groupCount()) { | ||
| return matcher.group(groupIndex); | ||
| } | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Extract multiple occurrences of a named capture group from text. This method avoids the index | ||
| * shifting issue that occurs with nested unnamed groups. | ||
| * | ||
| * @param text The input text to extract from | ||
| * @param pattern The regex pattern with named capture groups | ||
| * @param groupName The name of the capture group to extract | ||
| * @param maxMatch Maximum number of matches to return (0 = unlimited) | ||
| * @return List of extracted values or null if no matches found | ||
| */ | ||
| public static List<String> extractMultipleGroups( | ||
| String text, String pattern, String groupName, int maxMatch) { | ||
| if (text == null || pattern == null || groupName == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return executeMultipleExtractions( | ||
| text, | ||
| pattern, | ||
| maxMatch, | ||
| matcher -> { | ||
| try { | ||
| return matcher.group(groupName); | ||
| } catch (IllegalArgumentException e) { | ||
| // Group name doesn't exist in the pattern, stop processing | ||
| return null; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Common extraction logic for multiple matches to avoid code duplication. | ||
| * | ||
| * @param text The input text | ||
| * @param pattern The regex pattern | ||
| * @param maxMatch Maximum matches (0 = unlimited) | ||
| * @param extractor Function to extract the value from the matcher | ||
| * @return List of extracted values or null if no matches found | ||
| */ | ||
| private static List<String> executeMultipleExtractions( | ||
| String text, | ||
| String pattern, | ||
| int maxMatch, | ||
| java.util.function.Function<Matcher, String> extractor) { | ||
| try { | ||
| Pattern compiledPattern = Pattern.compile(pattern); | ||
| Pattern compiledPattern = RegexCommonUtils.getCompiledPattern(pattern); | ||
| Matcher matcher = compiledPattern.matcher(text); | ||
| List<String> matches = new ArrayList<>(); | ||
|
|
||
| int matchCount = 0; | ||
| while (matcher.find() && (maxMatch == 0 || matchCount < maxMatch)) { | ||
| if (groupIndex > 0 && groupIndex <= matcher.groupCount()) { | ||
| String match = matcher.group(groupIndex); | ||
| if (match != null) { | ||
| matches.add(match); | ||
| matchCount++; | ||
| } | ||
| String match = extractor.apply(matcher); | ||
| if (match != null) { | ||
| matches.add(match); | ||
| matchCount++; | ||
| } else { | ||
| // If extractor returns null, it might indicate an error (like invalid group name) | ||
| // Stop processing to avoid infinite loop | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm currently thinking about adding an error handling here |
||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I found there is a
namedGroups()API inMatcher(since JDK 20?). If we can get correct index here, we don't need to modify the UDFs below? Alternatively we can move capture name -> index logic here from UDFs?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.
That is correct - the core issue is matching named groups to their correct indices, and
Pattern.namedGroups()would be the perfect solution. However, I discovered that we're blocked by acompatibility constraint:
Pattern.namedGroups()was introduced in JDK 202.19-devI agree that directly leveraging the
Pattern.namedGroups()is the right architectural approach - we should definitely migrate to it when we fully upgrade to JDK 20+. At that point, it would be a simple one-line change inCalciteRelNodeVisitor.