-
Notifications
You must be signed in to change notification settings - Fork 25.6k
EQL: implement stringContains function #54380
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
Conversation
|
Pinging @elastic/es-ql (:Query Languages/EQL) |
|
|
||
| private final Expression haystack, needle, caseSensitive; | ||
|
|
||
| public StringContains(Source source, Expression haystack, Expression needle, Expression caseSensitive) { |
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 think (per a slack conversation) that we'll handle case-sensitivity separately, so no need for the argument?
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.
yeah, was not clear on this last week, so left it here for now. will remove as needed.
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.
Removed the optional parameter.
astefan
left a comment
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.
Left some comments.
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.eql.expression.function.scalar.stringcontains; |
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.
Package I think should be org.elasticsearch.xpack.eql.expression.function.scalar.string.
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.
Moved to the scalar.string package.
|
|
||
| private final Expression haystack, needle, caseSensitive; | ||
|
|
||
| public StringContains(Source source, Expression haystack, Expression needle, Expression caseSensitive) { |
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.
Personally, I find haystack and needle inappropriate here. I know that it fits with the purpose of the function :-), but I prefer string and pattern or substring.
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.
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.
Renamed params to string and substring.
| this.caseSensitive = arguments().get(2); | ||
| } | ||
|
|
||
| private static Expression toDefault(Expression exp) { |
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'm not convinced this method is really needed. It's just a short one-liner... Maybe other reviewers thing otherwise, but I would keep using the ternary operator inline.
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.
Removed the method since no more optional param.
| private final Pipe haystack, needle, caseSensitive; | ||
|
|
||
| public StringContainsFunctionPipe(Source source, Expression expression, Pipe haystack, Pipe needle, Pipe caseSensitive) { | ||
| super(source, expression, Arrays.asList(haystack, needle)); |
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.
No caseSensitive?
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.
Removed caseSensitive param completely.
|
|
||
| public class StringContainsFunctionProcessor implements Processor { | ||
|
|
||
| public static final String NAME = "sstringcontains"; |
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.
sstc maybe?
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.
Updated the NAME
|
|
||
| import static org.elasticsearch.common.Strings.hasLength; | ||
|
|
||
| final class StringContainsUtils { |
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.
Why this class? Shouldn't this method belong to a class that has other String utility method like this one here? And is this really needed to be placed in a separate class?
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 think these methods could be in function.scalar.string.StringUtils
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.
Moved the method to function.scalar.string.StringUtils.
...n/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistry.java
Show resolved
Hide resolved
|
@elasticmachine run elasticsearch-ci/2 |
|
|
||
| /** | ||
| * EQL specific stringContains function. | ||
| * https://eql.readthedocs.io/en/latest/query-guide/functions.html#stringContains |
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.
we can probably remove this link since Elasticsearch will eventually be the authoritative documentation
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.
Removed.
| } | ||
|
|
||
| public static Object doProcess(Object string, Object substring) { | ||
| if (string == null) { |
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.
| if (string == null) { | |
| if (string == null || substring == null) { |
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.
throwing exception for now if the second param is null, consistent across all 3 functions that I implemented so far
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.
are we leaning towards returning nulls more often and not throwing exceptions?
| return (String) SubstringFunctionProcessor.doProcess(s, start, end); | ||
| } | ||
|
|
||
| public static Boolean stringContains(String string, String substring) { |
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.
before substring for alphabetical order
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.
Moved.
| "params":{"v0":"file_name.keyword","v1":-4,"v2":null,"v3":".exe"} | ||
|
|
||
|
|
||
| stringContains |
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.
between startswith and substring?
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.
Moved.
astefan
left a comment
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.
LGTM. Left few minor comments.
| assertThat(e.getMessage(), equalTo("A string/char is required; received [null]")); | ||
| } else { | ||
| assertThat(StringContainsFunctionProcessor.doProcess(string, substring), | ||
| equalTo(string == null? null : 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.
string == null ? null : 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.
Fixed.
| protected static final int NUMBER_OF_TEST_RUNS = 20; | ||
|
|
||
| private static void run(Callable<Void> callable) throws Exception { | ||
| for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { |
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.
Imo, I don't think we need this high degree of unit tests for this particular functionality, but wait for @costin's take on this before modifying this or leaving it as is.
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.
Removed.
|
|
||
| private String error(String query) { | ||
| VerificationException e = expectThrows(VerificationException.class, | ||
| () -> plan(query)); |
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 think this line fits inside the previous one.
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.
Updated.
* EQL: implement stringContains function * Address code review comments * Fix javadoc comment for stringContains function * Address code review comments * Adjust queryfolder_tests.txt to the new format
Related to #54136