Skip to content

Conversation

@aleksmaus
Copy link
Contributor

Related to #54136

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-ql (:Query Languages/EQL)

@aleksmaus aleksmaus requested a review from imotov March 28, 2020 19:04

private final Expression haystack, needle, caseSensitive;

public StringContains(Source source, Expression haystack, Expression needle, Expression caseSensitive) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the optional parameter.

Copy link
Contributor

@astefan astefan left a 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;
Copy link
Contributor

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.

Copy link
Contributor Author

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) {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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) {
Copy link
Contributor

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.

Copy link
Contributor Author

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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No caseSensitive?

Copy link
Contributor Author

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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sstc maybe?

Copy link
Contributor Author

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 {
Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor Author

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.

@aleksmaus
Copy link
Contributor Author

@elasticmachine run elasticsearch-ci/2

@aleksmaus aleksmaus requested review from astefan and rw-access April 6, 2020 12:54

/**
* EQL specific stringContains function.
* https://eql.readthedocs.io/en/latest/query-guide/functions.html#stringContains
Copy link
Contributor

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

Copy link
Contributor Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (string == null) {
if (string == null || substring == null) {

Copy link
Contributor Author

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

Copy link
Contributor Author

@aleksmaus aleksmaus Apr 6, 2020

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) {
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

between startswith and substring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved.

Copy link
Contributor

@astefan astefan left a 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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string == null ? null : true

Copy link
Contributor Author

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++) {
Copy link
Contributor

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.

Copy link
Contributor Author

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));
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@aleksmaus aleksmaus merged commit 3cd858c into elastic:master Apr 7, 2020
aleksmaus added a commit to aleksmaus/elasticsearch that referenced this pull request Apr 7, 2020
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants