EQL: implement stringContains function#54380
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.
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.
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.
Removed the optional parameter.
| * 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.
Package I think should be org.elasticsearch.xpack.eql.expression.function.scalar.string.
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
Renamed params to string and substring.
| this.caseSensitive = arguments().get(2); | ||
| } | ||
|
|
||
| private static Expression toDefault(Expression exp) { |
There was a problem hiding this comment.
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.
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.
Removed caseSensitive param completely.
|
|
||
| public class StringContainsFunctionProcessor implements Processor { | ||
|
|
||
| public static final String NAME = "sstringcontains"; |
|
|
||
| import static org.elasticsearch.common.Strings.hasLength; | ||
|
|
||
| final class StringContainsUtils { |
There was a problem hiding this comment.
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.
I think these methods could be in function.scalar.string.StringUtils
There was a problem hiding this comment.
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.
we can probably remove this link since Elasticsearch will eventually be the authoritative documentation
| } | ||
|
|
||
| public static Object doProcess(Object string, Object substring) { | ||
| if (string == null) { |
There was a problem hiding this comment.
| if (string == null) { | |
| if (string == null || substring == null) { |
There was a problem hiding this comment.
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.
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.
before substring for alphabetical order
| "params":{"v0":"file_name.keyword","v1":-4,"v2":null,"v3":".exe"} | ||
|
|
||
|
|
||
| stringContains |
There was a problem hiding this comment.
between startswith and substring?
astefan
left a comment
There was a problem hiding this comment.
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.
string == null ? null : true
| 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.
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.
|
|
||
| private String error(String query) { | ||
| VerificationException e = expectThrows(VerificationException.class, | ||
| () -> plan(query)); |
There was a problem hiding this comment.
I think this line fits inside the previous one.
* 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