-
Notifications
You must be signed in to change notification settings - Fork 25.6k
EQL: implement cidrMatch function #54186
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-search (:Search/EQL) |
| return; | ||
| } | ||
|
|
||
| CreateIndexRequest request = new CreateIndexRequest(testIndexName) |
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.
Needed mapping to IP type, this creating the index with mapping.
| for (Expression addr: addresses) { | ||
| // Currently we have limited enum for ordinal numbers | ||
| // So just using default here for error messaging | ||
| resolution = isStringAndExact(addr, sourceText(), ParamOrdinal.DEFAULT); |
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.
The current enum for error messages doesn't work great for variadic type of function. Open to suggestions.
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 added a method here in my wildcard PR:
elasticsearch/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Expressions.java
Lines 36 to 44 in d482c47
| public static ParamOrdinal fromIndex(int index) { | |
| switch (index) { | |
| case 0: return ParamOrdinal.FIRST; | |
| case 1: return ParamOrdinal.SECOND; | |
| case 2: return ParamOrdinal.THIRD; | |
| case 3: return ParamOrdinal.FOURTH; | |
| default: return ParamOrdinal.DEFAULT; | |
| } | |
| } |
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.
Also, should these fields also be isStringAndExact? and an isFoldable check?
Here's how I approached that
Lines 80 to 83 in d482c47
| lastResolution = isFoldable(p, sourceText(), ParamOrdinal.fromIndex(index)); | |
| if (lastResolution.unresolved()) { | |
| break; | |
| } |
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.
Cool! Will update after your PR merges.
The ParamOrdinal falls back to ParamOrdinal.DEFAULT after 4 params anyways. Maybe make enum longer if we still want to use enum there?
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.
For how to handle functions that deal with a variable list of parameters, I suggest having a look at org.elasticsearch.xpack.sql.expression.predicate.conditional.Case or org.elasticsearch.xpack.sql.expression.predicate.conditional.Coalesce since these are functions that have a variable list of parameters.
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.
Relying on the same def builder here that was merged with wildcard function for now.
Updated to align with wildcard impl usage of ParamOrdinal.
...l/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatch.java
Outdated
Show resolved
Hide resolved
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 few comments.
| // String | ||
| new FunctionDefinition[] { | ||
| def(Substring.class, Substring::new, "substring"), | ||
| def(CIDRMatch.class, CIDRMatch::new, "cidrmatch"), |
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 prefer listing the functions in 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.
Yep. Updated.
| import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isStringAndExact; | ||
|
|
||
| /** | ||
| * EQL specific cidrMatch function |
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.
Personal preference, but I would like to see a useful description of the function. Not even the original EQL documentation is clear imo: https://eql.readthedocs.io/en/latest/query-guide/functions.html
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.
Added a couple of more lines with a link to the EQL doc.
| for (Expression addr: addresses) { | ||
| // Currently we have limited enum for ordinal numbers | ||
| // So just using default here for error messaging | ||
| resolution = isStringAndExact(addr, sourceText(), ParamOrdinal.DEFAULT); |
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.
For how to handle functions that deal with a variable list of parameters, I suggest having a look at org.elasticsearch.xpack.sql.expression.predicate.conditional.Case or org.elasticsearch.xpack.sql.expression.predicate.conditional.Coalesce since these are functions that have a variable list of parameters.
|
Will address the code review comments and make some adjustments once the wildcard function PR (#54020) is merged, since will have some conflicts and can reuse some code between the functions. |
|
@elastic/es-ql |
|
@elasticmachine run elasticsearch-ci/docs |
|
@elasticmachine run elasticsearch-ci/2 |
costin
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.
Looks good to me - left a comment re: #54795 (waiting for the build to pass before merging).
I would also like some tests regarding incorrect arguments - what if the given string is non-numeric or contains an invalid IP.
How does the current EQL implementation handle that - I'm fine with addressing this in a separate PR however I'd like to have an issue tracking this so it doesn't get lost.
That is, how do we validate and protect against invalid parameters.
| return new CIDRMatch(source(), newChildren.get(0), newChildren.subList(1, newChildren.size())); | ||
| } | ||
|
|
||
| public ScalarFunction asFunction() { |
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.
Please update to take into account #54795 - reduces the class and removes a separate optimizer rule.
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.
Sure, will do once #54795 is merged.
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
|
@costin regarding validation if the string is a valid IP address the original EQL implementation does validation with regex matching etc. In our case I was not sure if we need to add any additional validation, but pass the string down to elasticsearch and let it match against the IP type of field or handle the error if the value can not be used for matching. |
Related to #54132