-
Notifications
You must be signed in to change notification settings - Fork 546
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
added regexpMathScalar #15522
base: master
Are you sure you want to change the base?
added regexpMathScalar #15522
Conversation
Can one of the admins verify this patch? |
@Lyra126 |
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.
Could you also add related tests, best by extending from ScalarTestCase
please? thanks!
server/src/main/java/io/crate/expression/scalar/regex/RegexpMatchScalar.java
Outdated
Show resolved
Hide resolved
server/src/main/java/io/crate/expression/scalar/regex/RegexpMatchScalar.java
Outdated
Show resolved
Hide resolved
server/src/main/java/io/crate/expression/scalar/regex/RegexpMatchScalar.java
Outdated
Show resolved
Hide resolved
|
||
public RegexpMatchScalar(Signature signature, BoundSignature boundSignature) { | ||
super(signature, boundSignature); | ||
} |
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.
Could you please add a compile
implementation? By this, any literal pattern will be compiled and flags will be parsed only once and not on each row, resulting in performance improvements.
server/src/main/java/io/crate/expression/scalar/regex/RegexpMatchScalar.java
Outdated
Show resolved
Hide resolved
server/src/main/java/io/crate/expression/scalar/regex/RegexpMatchScalar.java
Outdated
Show resolved
Hide resolved
int flagSeparatorIndex = pattern.lastIndexOf('/'); | ||
if (flagSeparatorIndex != -1 && flagSeparatorIndex < pattern.length() - 1) { | ||
pattern = pattern.substring(0, flagSeparatorIndex); | ||
flagsStr = pattern.substring(flagSeparatorIndex + 1); | ||
flags = parseFlags(flagsStr); | ||
} |
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.
Any optional flag would be the 3rd argument and not part of the pattern, see https://www.postgresql.org/docs/current/functions-matching.html.
To support this, a custom toQuery(Function function, LuceneQueryBuilder.Context context)
would be needed to take the 3rd argument into account.
Thank you for your continuous review and help with this issue! This is my first time contributing, and I appreciate the help and clarification. Let me know if I'm missing anything else. |
|
||
@Test | ||
public void testNormalize() throws Exception { | ||
assertNormalize("'foo bar' ~ '([A-Z][^ ]+ ?){2}'", isLiteral(true)); |
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.
You are testing the existing operator and not your new scalar.
Not exactly sure what you want to test here but this expression will fail in anyway as the regex won't match.
I highly recommend to run your tests yourself ;)
assertNormalize("'foo bar' ~ '([A-Z][^ ]+ ?){2}'", isLiteral(true)); | |
assertNormalize("regexp_match('foo bar', '([A-Z][^ ]+ ?){2}')", isLiteral()); |
compilePattern(patternString, flags); | ||
} | ||
|
||
private void compilePattern(String patternString, int flags) throws PatternSyntaxException { |
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.
This is not implementing the interface's compile
method.. Please check existing implementations to understand how it is used.
|
||
List<String> matches = new ArrayList<>(); | ||
java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(pattern).matcher(source); | ||
while (matcher.find()) { |
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.
According to the PG documentation, it should return null
if no match is found.
Additionally, the results of the all groups in the current match should be returned, not the first group only but for all global matches (the global flag 'g' must no be supported by this method).
|
||
// Check if the pattern contains flags | ||
String flagsStr = null; | ||
int flagSeparatorIndex = pattern.lastIndexOf('/'); |
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 wonder where the flags are added to the pattern by the /
separator? I think the existing toQuery
implementations won't work as they only support 2 arguments, a reference and a literal, but not 3.
Summary of the changes / Why this improves CrateDB
Added RegexpMatchScalar
Submitting this pull request as a solution to the following issue: #15117
Checklist
docs/appendices/release-notes/<x.y.0>.rst
for user facing changessql_features
table for user facing changesdocs/appendices/release-notes/<x.y.0>.rst
(E.g. AdminUI)