-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[pkg/ottl] Basic math capabilities #15711
[pkg/ottl] Basic math capabilities #15711
Conversation
f1b755b
to
ac72c3d
Compare
f31e5b2
to
de58231
Compare
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 mostly good to me, I agree with your conclusion on the grammar changes based on what I've read. I will take a more in-depth look at the grammar on Monday.
One question I have is around the operations we want to support: do we expect to only have the four in this PR, or will we want more in the future (exponentiation, bitwise operations, etc.)? As it is right now, setting precedence for additional operations would cause the parsed expression to have a pretty deep structure.
@evan-bradley good question. I think we could expect to add those once there is a use case for them. Unfortunately the more operations we add the more precedence we have to handle and the deeper the expression structure will go. I don't think there is a good way to handle this as the Term/Factor/Value concept is really the best way to do precedence. |
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. After taking a second look, I agree with your conclusions about the grammar.
Co-authored-by: Daniel Jaglowski <[email protected]>
Co-authored-by: Daniel Jaglowski <[email protected]>
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 have raised a couple of issues, but I do think they could be fixed here or saved for another day. In general this looks great, so in the interests of keeping things moving I'd be fine with merging this.
{ | ||
name: "multiply large floats", | ||
input: "1.79769313486231570814527423731704356798070e+308 * 1.79769313486231570814527423731704356798070e+308", | ||
expected: math.Inf(0), |
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.
ok, this opens up a small can of worms. What happens if inf
and NaN
come through the pipeline? Do we support them as constants in ottl? Should we?
I can definitely imagine somebody wanting something like drop() where attributes["math_field"] == NaN
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.
Ya this was an outcome of testing I wasn't really expecting. I think +Inf
isn't really a concern, but NaN
will be useful once we can get in those situations. I think with the operations we've defined we will always avoid NaN
tho.
Description:
Adds basic math operations to the grammar. The grammar now supports
+
,-
,*
, and/
for int64 and float64, as well as invocations and paths that return ints and floats. More types (strings, bytes, etc), could be supported in the future.Since paths and invocations are supported, calculation of the expressions has to be done during data processing instead of startup. This means that Expressions are always captured as Getters, and in order for a function to support an Expression it must use a Getter. In the future I'd like to improve this by creating Typed Getters, but I believe that should be saved for a future PR.
To support both Expressions and literal values in the
value
struct I needed to reorganize the grammar so that it can differentiate between anInvocation/Path/Float/Int
that is the start of anexpression
and anInvocation/Path/Float/Int
that is "standalone". This is achieved by using the grammar library's negative lookahead feature. In order for negative lookahead to work correctly in the value's OR clause, it needed to appear only once. As a result, all grammar types that are supported byExpression
were moved into a sub struct. This caused a lot of changes in the PR, but I do believe is necessary. See alecthomas/participle#279 for some details on how I landed at this conclusion.Link to tracking Issue:
Closes #15675
Testing:
Added new unit tests
Documentation:
Update ottl grammar doc