Skip to content
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 temporal functions #872

Merged
merged 11 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions src/query/functions/temporal/aggregation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package temporal

import (
"fmt"
"math"

"github.com/m3db/m3/src/query/executor/transform"
)

const (
// AvgTemporalType calculates the average of all values in the specified interval
AvgTemporalType = "avg_over_time"

// CountTemporalType calculates count of all values in the specified interval
CountTemporalType = "count_over_time"

// MinTemporalType calculates the minimum of all values in the specified interval
MinTemporalType = "min_over_time"

// MaxTemporalType calculates the maximum of all values in the specified interval
MaxTemporalType = "max_over_time"

// SumTemporalType calculates the sum of all values in the specified interval
SumTemporalType = "sum_over_time"

// StdDevTemporalType calculates the standard deviation of all values in the specified interval
StdDevTemporalType = "stddev_over_time"

// StdVarTemporalType calculates the standard variance of all values in the specified interval
StdVarTemporalType = "stdvar_over_time"
)

type aggFunc func([]float64) float64

var (
aggFuncs = map[string]aggFunc{
AvgTemporalType: avgOverTime,
CountTemporalType: countOverTime,
MinTemporalType: minOverTime,
MaxTemporalType: maxOverTime,
SumTemporalType: sumOverTime,
StdDevTemporalType: stddevOverTime,
StdVarTemporalType: stdvarOverTime,
}
)

// NewAggOp creates a new base temporal transform with a specified node
func NewAggOp(args []interface{}, optype string) (transform.Params, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd recommend just taking floats or whatever as your args, and have the logic for providing usable values in the parser instead, so you don't end up with extra parsing scattered around in the function op.

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 the problem here is that some ops require more than one args. Then the question is where should that logic live which determines which argument is need by which function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should add a todo to consolidate how we pass args around, #884 should help us standardize scalar parameters, but unfortunately there's a few special cases in prom which take Scalar, Fetch instead of Fetch, Scalar, or even Fetch, Scalar, Scalar. Prom have a set of required param types by position; may be an idea to do something similar?

if aggregationFunc, ok := aggFuncs[optype]; ok {
return newBaseOp(args, optype, newAggNode, aggregationFunc)
}

return nil, fmt.Errorf("unknown aggregation type: %s", optype)
}

func newAggNode(op baseOp, controller *transform.Controller) Processor {
return &aggNode{
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe just store the function directly so that we don't have to do the lookup in Process ?

op: op,
controller: controller,
aggFunc: op.aggFunc,
}
}

type aggNode struct {
op baseOp
controller *transform.Controller
aggFunc func([]float64) float64
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: define this as a type, i.e. type aggFunc func([]float64) float64

}

func (a *aggNode) Process(values []float64) float64 {
return a.aggFunc(values)
}

func avgOverTime(values []float64) float64 {
Copy link
Contributor

Choose a reason for hiding this comment

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

all these functions look very similar to what Artem wrote. Consider consolidating them ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I liked @nikunjgit's idea in the regular aggregation functions to make a function that returns (sum, count) which simplifies sum, count, average, and can help with stddev/stdvar

sum, count := sumAndCount(values)
return sum / count
}

func countOverTime(values []float64) float64 {
_, count := sumAndCount(values)
if count == 0 {
return math.NaN()
}
return count
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: newline missing in most of these pre-return

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: newline

}

func minOverTime(values []float64) float64 {
var seenNotNaN bool
min := math.Inf(1)
for _, v := range values {
if !math.IsNaN(v) {
seenNotNaN = true
min = math.Min(min, v)
}
}

if !seenNotNaN {
return math.NaN()
}

return min
}

func maxOverTime(values []float64) float64 {
var seenNotNaN bool
max := math.Inf(-1)
for _, v := range values {
if !math.IsNaN(v) {
seenNotNaN = true
max = math.Max(max, v)
}
}

if !seenNotNaN {
return math.NaN()
}

return max
}

func sumOverTime(values []float64) float64 {
sum, _ := sumAndCount(values)
return sum
}

func stddevOverTime(values []float64) float64 {
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of implementing all these functions, can you use : gonum ? Eg: https://github.com/gonum/gonum/blob/73ea1e732937f96d723d31dc5263d214a275d204/stat/stat.go#L1199

Copy link
Collaborator

Choose a reason for hiding this comment

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

If not using gonum, can this just return math.Sqrt(stdvarOverTime(values))

return math.Sqrt(stdvarOverTime(values))
}

func stdvarOverTime(values []float64) float64 {
var aux, count, mean float64
for _, v := range values {
if !math.IsNaN(v) {
count++
delta := v - mean
mean += delta / count
aux += delta * (v - mean)
}
}

if count == 0 {
return math.NaN()
}

return aux / count
}

func sumAndCount(values []float64) (float64, float64) {
sum := 0.0
count := 0.0
for _, v := range values {
if !math.IsNaN(v) {
sum += v
count++
}
}

if count == 0 {
return math.NaN(), 0
}

return sum, count
}
Loading