Skip to content

EQL: Add number function#55084

Merged
rw-access merged 24 commits intoelastic:masterfrom
rw-access:eql/number-function
May 13, 2020
Merged

EQL: Add number function#55084
rw-access merged 24 commits intoelastic:masterfrom
rw-access:eql/number-function

Conversation

@rw-access
Copy link
Copy Markdown
Contributor

@rw-access rw-access commented Apr 10, 2020

Resolves #54471

Added function to parse numbers from strings.
One note is that the resolved DataType is double. Since it can return integer, float, or number. Double seemed to be the most inclusive.

@rw-access rw-access added the :Analytics/EQL EQL querying label Apr 10, 2020
@elasticmachine
Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
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 a bunch of comments.
I think we need more integration and unit tests to cover all scenarios (see comment for the toml file).
Also, base argument has been left off in quite few places.


@Override
public final Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newSource = source.resolveAttributes(resolver);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you forget the base here?

def(EndsWith.class, EndsWith::new, "endswith"),
def(IndexOf.class, IndexOf::new, "indexof"),
def(Length.class, Length::new, "length"),
def(ToNumber.class, ToNumber::new, "number"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should add a new FunctionDefinition[] array for math/numerical functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

agreed. @aleksmaus has one in his PR. I wasn't sure if this was math or still string, since it's parsing from strings

try {
return NumberFormat.getNumberInstance(Locale.US).parse(source.toString());
} catch (ParseException e) {
throw new EqlIllegalArgumentException("Unable to convert [{}] to number of base [{}]", source, radix);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here I would prefer to see in the error message the original base. If that is null because the user didn't provide one, then print radix.

expected_event_ids = [1, 2, 3, 5, 11]
description = "test built-in math functions"

[[queries]]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add more tests here, please?
The way I see it, there are tests (just one) for no argument number function and for base 16. How about base 2, explicit base 10, and custom bases?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would still like to see here more tests.

Copy link
Copy Markdown
Contributor Author

@rw-access rw-access Apr 22, 2020

Choose a reason for hiding this comment

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

Added tests for the processor, and added integration tests here
867fc01

@rw-access rw-access requested a review from astefan April 21, 2020 20:34
Copy link
Copy Markdown
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.

Looking better.
Added a second round of comments.

* you may not use this file except in compliance with the Elastic License.
*/

package org.elasticsearch.xpack.eql.expression.function.scalar.string;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe we should add a new package for the numeric/math functions. At the moment this is ...function.scalar.string. ES SQL has .math. Probably worth doing the same here. Add a package ...function.scalar.math.

expected_event_ids = [1, 2, 3, 5, 11]
description = "test built-in math functions"

[[queries]]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would still like to see here more tests.

@rjernst rjernst added the Team:QL (Deprecated) Meta label for query languages team label May 4, 2020
@rw-access rw-access requested a review from astefan May 8, 2020 17:36
Copy link
Copy Markdown
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 two very minor comments though.

assertNull(process(null, null));
assertNull(process(null, 8));
assertNull(process(null, 10));
assertNull(process(null, 16));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

assertNull(process(123, null)) is missing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ah. that's because if the base is null, then the base will be autodetected.
I don't know if there's a way to mention that the parameter was not supplied vs it was evaluated as false. I believe substring() and a few other functions have similar behavior

Copy link
Copy Markdown
Contributor

@aleksmaus aleksmaus left a comment

Choose a reason for hiding this comment

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

Left one question, otherwise LGTM.
Thank you!

@rw-access rw-access merged commit b0863cc into elastic:master May 13, 2020
@rw-access rw-access deleted the eql/number-function branch May 13, 2020 20:07
rw-access added a commit that referenced this pull request May 13, 2020
* EQL: Add number function
* EQL: Fix the locale used for number for deterministic functionality
* EQL: Add more ToNumber tests
* EQL: Add more number ToNumberProcessor unit tests
* EQL: Remove unnecessary overrides, fix processor methods
* EQL: Remove additional unnecessary overrides
* EQL: Lint fixes for ToNumber
* EQL: ToNumber renames from PR feedback
* EQL: Remove NumberFormat locale handling
* EQL: Removed NumberFormat from ToNumber
* EQL: Add number function tests
* EQL: ToNumberProcessorTests formatting
* EQL: Remove newline in ToNumberProcessorTests
* EQL: Add number(..., null) test
* EQL: Create expression.function.scalar.math package
* EQL: Remove painless whitespace for ToNumber.asScript
* EQL: Add Long support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/EQL EQL querying Team:QL (Deprecated) Meta label for query languages team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EQL: implement number function

5 participants