From adc36d00b703c7544a8c6351aba30e073b1ba051 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 10 Jan 2020 21:56:59 +0200 Subject: [PATCH] app/vmselect/promql: properly handle `aggr(aggr_over_time(...))` --- app/vmselect/promql/exec_test.go | 35 ++++++++++++++++++++++++++++++++ app/vmselect/promql/rollup.go | 9 ++++++++ 2 files changed, 44 insertions(+) diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index 769a0e4b5a..4c613fe870 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -4538,6 +4538,41 @@ func TestExecSuccess(t *testing.T) { resultExpected := []netstorage.Result{r1, r2, r3} f(q, resultExpected) }) + t.Run(`avg(aggr_over_time(multi-func))`, func(t *testing.T) { + t.Parallel() + q := `avg(aggr_over_time(("min_over_time", "max_over_time"), time()[:10s]))` + r := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{900, 1100, 1300, 1500, 1700, 1900}, + Timestamps: timestampsExpected, + } + resultExpected := []netstorage.Result{r} + f(q, resultExpected) + }) + t.Run(`avg(aggr_over_time(multi-func)) by (rollup)`, func(t *testing.T) { + t.Parallel() + q := `sort(avg(aggr_over_time(("min_over_time", "max_over_time"), time()[:10s])) by (rollup))` + r1 := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{800, 1000, 1200, 1400, 1600, 1800}, + Timestamps: timestampsExpected, + } + r1.MetricName.Tags = []storage.Tag{{ + Key: []byte("rollup"), + Value: []byte("min_over_time"), + }} + r2 := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{1000, 1200, 1400, 1600, 1800, 2000}, + Timestamps: timestampsExpected, + } + r2.MetricName.Tags = []storage.Tag{{ + Key: []byte("rollup"), + Value: []byte("max_over_time"), + }} + resultExpected := []netstorage.Result{r1, r2} + f(q, resultExpected) + }) t.Run(`rollup_candlestick()`, func(t *testing.T) { t.Parallel() q := `sort(rollup_candlestick(round(rand(0),0.01)[:10s]))` diff --git a/app/vmselect/promql/rollup.go b/app/vmselect/promql/rollup.go index bd0733b3ed..385f702895 100644 --- a/app/vmselect/promql/rollup.go +++ b/app/vmselect/promql/rollup.go @@ -136,6 +136,15 @@ var rollupFuncsKeepMetricGroup = map[string]bool{ } func getRollupAggrFuncNames(expr metricsql.Expr) ([]string, error) { + afe, ok := expr.(*metricsql.AggrFuncExpr) + if ok { + // This is for incremental aggregate function case: + // + // sum(aggr_over_time(...)) + // + // See aggr_incremental.go for details. + expr = afe.Args[0] + } fe, ok := expr.(*metricsql.FuncExpr) if !ok { logger.Panicf("BUG: unexpected expression; want metricsql.FuncExpr; got %T; value: %s", expr, expr.AppendString(nil))