-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add derivative functions #2569
Add derivative functions #2569
Conversation
May be supported in the future but workaround is to run separate queries.
Derivative must be of the form derviative(field, duration) or derivative(agg(field), duration).
Calculates the derivative of consequtive points and normalizes the value to a given interval. It supports simple derivates over fields as well as nested derivatives over another aggregate function. Fixes #1822
Looks pretty solid to me. Nothing immediately jumps out as crazy, so 👍 |
### Bugfixes | ||
- [#2545](https://github.com/influxdb/influxdb/pull/2545): Use "value" as the field name for graphite input. Thanks @cannium. | ||
- [#2558](https://github.com/influxdb/influxdb/pull/2558): Fix client response check - thanks @vladlopes! | ||
|
||
## PRs |
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.
Minor, but I'm not sure this section adds anything to the CHANGELOG. The PR will already be referenced by each page for the issue links. My vote would be to remove it.
All comments addressed. |
@@ -642,6 +642,35 @@ type SelectStatement struct { | |||
FillValue interface{} | |||
} | |||
|
|||
// HasDerivative returns true if one of the fields in the statement is a |
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.
Comment appears to be incorrect.
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.
That is "fields"?
// derivative aggregate | ||
func (s *SelectStatement) HasDerivative() bool { | ||
for _, f := range s.FunctionCalls() { | ||
if strings.HasSuffix(f.Name, "derivative") { |
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.
A suggestion, not a blocker right now, but you could instead add IsDerivative()
to functions.go. I added IsNumeric()
recently. That way all the code for checking stuff about functions is in one file. Then this code becomes:
if f.IsDerivative() {
...
}
It would also allow the check to be more precise -- if "derivate" or "non_negative_derivative".
If it's not specified, it defaults to 1s for raw queries and to the group by duration on group by queries.
return nil, fmt.Errorf("expected two arguments for percentile()") | ||
return nil, fmt.Errorf("expected two arguments for %s()", c.Name) | ||
} | ||
} else if strings.HasSuffix(c.Name, "derivative") { |
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.
Oh yeah, since this is used so much, definitely worth adding IsDerivative()
to functions.go. Next time.
+1, thanks for the changes. |
This PR adds support for
derivative
andnon_negative_derivative
aggregate function. These are useful for calculating a rate of change on counter style measurements. They support derivatives over fields as well as nested aggregate functions over a field.In the case of
non_negative_derivative
, negative values are dropped.One limitation with this implementation is that if a query uses a derivative function, there can be no other fields or functions in the select clause. The parser will return an error in this case.
The functions require a field or another aggregate function over a field and optional duration interval. If a duration interval is not specified, and there is a group by clause time clause, the group by time is used. If there is no group by time clause, the interval defaults to 1 second.
Fixes #1822 #1477
Some examples given the following data:
Derivative normalized per day
Derivative normalized per hour
Derivative over the mean value normalized per day
Non-negative derivative over the mean value normalized per day