From b08f0850826b2a26c83f25d398cd9677f428f3c9 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 3 Sep 2019 20:42:45 +0300 Subject: [PATCH] app/vmselect/promql: reset timeseries name on group_left and group_right as Prometheus does --- app/vmselect/promql/binary_op.go | 2 + app/vmselect/promql/exec_test.go | 84 +++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/app/vmselect/promql/binary_op.go b/app/vmselect/promql/binary_op.go index 609ce10201..e3b2478e83 100644 --- a/app/vmselect/promql/binary_op.go +++ b/app/vmselect/promql/binary_op.go @@ -322,6 +322,7 @@ func adjustBinaryOpTags(be *binaryOpExpr, left, right []*timeseries) ([]*timeser } src := tssRight[0] for _, ts := range tssLeft { + resetMetricGroupIfRequired(be, ts) ts.MetricName.AddMissingTags(joinTags, &src.MetricName) rvsLeft = append(rvsLeft, ts) rvsRight = append(rvsRight, src) @@ -332,6 +333,7 @@ func adjustBinaryOpTags(be *binaryOpExpr, left, right []*timeseries) ([]*timeser } src := tssLeft[0] for _, ts := range tssRight { + resetMetricGroupIfRequired(be, ts) ts.MetricName.AddMissingTags(joinTags, &src.MetricName) rvsLeft = append(rvsLeft, src) rvsRight = append(rvsRight, ts) diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index 5b4d2015d3..5929084cd5 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -1869,9 +1869,9 @@ func TestExecSuccess(t *testing.T) { resultExpected := []netstorage.Result{r} f(q, resultExpected) }) - t.Run(`vector * on(foo) group_left() duplicate_timeseries`, func(t *testing.T) { + t.Run(`vector * on(foo) group_left() duplicate_nonoverlapping_timeseries`, func(t *testing.T) { t.Parallel() - q := `label_set(time()/10, "foo", "bar") + on(foo) group_left() ( + q := `label_set(time()/10, "foo", "bar", "xx", "yy", "__name__", "qwert") + on(foo) group_left() ( label_set(time() < 1400, "foo", "bar", "op", "le"), label_set(time() >= 1400, "foo", "bar", "op", "ge"), )` @@ -1880,13 +1880,85 @@ func TestExecSuccess(t *testing.T) { Values: []float64{1100, 1320, 1540, 1760, 1980, 2200}, Timestamps: timestampsExpected, } - r1.MetricName.Tags = []storage.Tag{{ - Key: []byte("foo"), - Value: []byte("bar"), - }} + r1.MetricName.Tags = []storage.Tag{ + { + Key: []byte("foo"), + Value: []byte("bar"), + }, + { + Key: []byte("xx"), + Value: []byte("yy"), + }, + } resultExpected := []netstorage.Result{r1} f(q, resultExpected) }) + t.Run(`vector * on(foo) group_left(__name__)`, func(t *testing.T) { + t.Parallel() + q := `label_set(time()/10, "foo", "bar", "xx", "yy", "__name__", "qwert") + on(foo) group_left(__name__) + label_set(time(), "foo", "bar", "__name__", "aaa")` + r1 := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{1100, 1320, 1540, 1760, 1980, 2200}, + Timestamps: timestampsExpected, + } + r1.MetricName.MetricGroup = []byte("aaa") + r1.MetricName.Tags = []storage.Tag{ + { + Key: []byte("foo"), + Value: []byte("bar"), + }, + { + Key: []byte("xx"), + Value: []byte("yy"), + }, + } + resultExpected := []netstorage.Result{r1} + f(q, resultExpected) + }) + t.Run(`vector * on(foo) group_right()`, func(t *testing.T) { + t.Parallel() + q := `sort(label_set(time()/10, "foo", "bar", "xx", "yy", "__name__", "qwert") + on(foo) group_right(xx) ( + label_set(time(), "foo", "bar", "__name__", "aaa"), + label_set(time()+3, "foo", "bar", "__name__", "yyy","ppp", "123"), + ))` + r1 := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{1100, 1320, 1540, 1760, 1980, 2200}, + Timestamps: timestampsExpected, + } + r1.MetricName.Tags = []storage.Tag{ + { + Key: []byte("foo"), + Value: []byte("bar"), + }, + { + Key: []byte("xx"), + Value: []byte("yy"), + }, + } + r2 := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{1103, 1323, 1543, 1763, 1983, 2203}, + Timestamps: timestampsExpected, + } + r2.MetricName.Tags = []storage.Tag{ + { + Key: []byte("foo"), + Value: []byte("bar"), + }, + { + Key: []byte("ppp"), + Value: []byte("123"), + }, + { + Key: []byte("xx"), + Value: []byte("yy"), + }, + } + resultExpected := []netstorage.Result{r1, r2} + f(q, resultExpected) + }) t.Run(`vector * on() group_left scalar`, func(t *testing.T) { t.Parallel() q := `sort_desc((label_set(time(), "foo", "bar") or label_set(10, "foo", "qwert")) * on() group_left 2)`