From 001cdefd7f1302ce9a2b3f50a29e5844db853592 Mon Sep 17 00:00:00 2001 From: Cory LaNou Date: Wed, 20 May 2015 10:42:58 -0600 Subject: [PATCH] return an error if user attempts to group by field --- CHANGELOG.md | 1 + cmd/influxd/server_integration_test.go | 26 ++++++++++++++++++++++++++ tx.go | 10 ++++++++++ 3 files changed, 37 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index da2175fe66..08e6ccfac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - [#2602](https://github.com/influxdb/influxdb/pull/2602): CLI execute command exits without cleaning up liner package. - [#2610](https://github.com/influxdb/influxdb/pull/2610): Fix shard group creation - [#2596](https://github.com/influxdb/influxdb/pull/2596): RC30: `panic: runtime error: index out of range` when insert data points. +- [#2592](https://github.com/influxdb/influxdb/pull/2592): Should return an error if user attempts to group by a field. ## PRs - [#2569](https://github.com/influxdb/influxdb/pull/2569): Add derivative functions diff --git a/cmd/influxd/server_integration_test.go b/cmd/influxd/server_integration_test.go index cfa8871ff2..bdaa7e756b 100644 --- a/cmd/influxd/server_integration_test.go +++ b/cmd/influxd/server_integration_test.go @@ -529,6 +529,32 @@ func runTestsData(t *testing.T, testName string, nodes Cluster, database, retent expected: `{"results":[{"series":[{"name":"cpu","columns":["time","count"],"values":[["1970-01-01T00:00:01Z",10],["1970-01-01T00:00:02Z",10],["1970-01-01T00:00:03Z",10],["1970-01-01T00:00:04Z",10],["1970-01-01T00:00:05Z",7],["1970-01-01T00:00:06Z",3]]}]}]}`, }, + // Test group by + { + reset: true, + name: "GROUP by time", + write: `{"database" : "%DB%", "retentionPolicy" : "%RP%", "points": [ + {"name": "cpu", "time": "2000-01-01T00:00:00Z", "tags": {"host": "server01"}, "fields": {"value": 10}}, + {"name": "cpu", "time": "2000-01-01T01:00:00Z", "tags": {"host": "server02"}, "fields": {"value": 10}}, + {"name": "cpu", "time": "2000-01-01T02:00:00Z", "tags": {"host": "server03"}, "fields": {"value": 10}} + ]}`, + query: `SELECT count(value) FROM cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T02:00:00Z' group by time(1h)`, + queryDb: "%DB%", + expected: `{"results":[{"series":[{"name":"cpu","columns":["time","count"],"values":[["2000-01-01T00:00:00Z",1],["2000-01-01T01:00:00Z",1],["2000-01-01T02:00:00Z",1]]}]}]}`, + }, + { + name: "GROUP by tag", + query: `SELECT count(value) FROM cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T02:00:00Z' group by host`, + queryDb: "%DB%", + expected: `{"results":[{"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","count"],"values":[["2000-01-01T00:00:00Z",1]]},{"name":"cpu","tags":{"host":"server02"},"columns":["time","count"],"values":[["2000-01-01T00:00:00Z",1]]},{"name":"cpu","tags":{"host":"server03"},"columns":["time","count"],"values":[["2000-01-01T00:00:00Z",1]]}]}]}`, + }, + { + name: "GROUP by field", + query: `SELECT count(value) FROM cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T02:00:00Z' group by value`, + queryDb: "%DB%", + expected: `{"results":[{"error":"can not use field in group by clause: value"}]}`, + }, + // Limit and offset { reset: true, diff --git a/tx.go b/tx.go index 6fa7d8c231..3168b63491 100644 --- a/tx.go +++ b/tx.go @@ -93,6 +93,16 @@ func (tx *tx) CreateMapReduceJobs(stmt *influxql.SelectStatement, tagKeys []stri } } + // Validate that group by is not a field + for _, d := range stmt.Dimensions { + switch e := d.Expr.(type) { + case *influxql.VarRef: + if !m.HasTagKey(e.Val) { + return nil, fmt.Errorf("can not use field in group by clause: %s", e.Val) + } + } + } + validateType := func(aname, fname string, t influxql.DataType) error { if t != influxql.Float && t != influxql.Integer { return fmt.Errorf("aggregate '%s' requires numerical field values. Field '%s' is of type %s",