2017-10-30 17:24:15 +00:00
|
|
|
package query_test
|
2016-02-23 23:43:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-10-30 17:24:15 +00:00
|
|
|
"github.com/influxdata/influxdb/query"
|
2017-10-30 21:40:26 +00:00
|
|
|
"github.com/influxdata/influxql"
|
2016-02-23 23:43:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRewriteStatement(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
stmt string
|
|
|
|
s string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS`,
|
2016-05-10 17:16:55 +00:00
|
|
|
s: `SELECT fieldKey, fieldType FROM _fieldKeys`,
|
2016-02-23 23:43:19 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS ON db0`,
|
|
|
|
s: `SELECT fieldKey, fieldType FROM db0.._fieldKeys`,
|
|
|
|
},
|
2016-02-23 23:43:19 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS FROM cpu`,
|
2016-05-10 17:16:55 +00:00
|
|
|
s: `SELECT fieldKey, fieldType FROM _fieldKeys WHERE _name = 'cpu'`,
|
2016-02-23 23:43:19 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS ON db0 FROM cpu`,
|
|
|
|
s: `SELECT fieldKey, fieldType FROM db0.._fieldKeys WHERE _name = 'cpu'`,
|
|
|
|
},
|
2016-02-23 23:43:19 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS FROM /c.*/`,
|
2016-05-10 17:16:55 +00:00
|
|
|
s: `SELECT fieldKey, fieldType FROM _fieldKeys WHERE _name =~ /c.*/`,
|
2016-02-23 23:43:19 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS ON db0 FROM /c.*/`,
|
|
|
|
s: `SELECT fieldKey, fieldType FROM db0.._fieldKeys WHERE _name =~ /c.*/`,
|
|
|
|
},
|
2016-04-27 14:30:33 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS FROM mydb.myrp2.cpu`,
|
2016-05-10 17:16:55 +00:00
|
|
|
s: `SELECT fieldKey, fieldType FROM mydb.myrp2._fieldKeys WHERE _name = 'cpu'`,
|
2016-04-27 14:30:33 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS ON db0 FROM mydb.myrp2.cpu`,
|
|
|
|
s: `SELECT fieldKey, fieldType FROM mydb.myrp2._fieldKeys WHERE _name = 'cpu'`,
|
|
|
|
},
|
2016-04-27 14:30:33 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS FROM mydb.myrp2./c.*/`,
|
2016-05-10 17:16:55 +00:00
|
|
|
s: `SELECT fieldKey, fieldType FROM mydb.myrp2._fieldKeys WHERE _name =~ /c.*/`,
|
2016-04-27 14:30:33 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW FIELD KEYS ON db0 FROM mydb.myrp2./c.*/`,
|
|
|
|
s: `SELECT fieldKey, fieldType FROM mydb.myrp2._fieldKeys WHERE _name =~ /c.*/`,
|
|
|
|
},
|
2016-04-27 14:30:33 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW SERIES`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT _seriesKey AS "key" FROM /.+/`,
|
2016-04-27 14:30:33 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW SERIES ON db0`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT _seriesKey AS "key" FROM db0../.+/`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-04-27 14:30:33 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW SERIES FROM cpu`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT _seriesKey AS "key" FROM cpu`,
|
2016-04-27 14:30:33 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW SERIES ON db0 FROM cpu`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT _seriesKey AS "key" FROM db0..cpu`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-04-27 14:30:33 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW SERIES FROM mydb.myrp1.cpu`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT _seriesKey AS "key" FROM mydb.myrp1.cpu`,
|
2016-04-27 14:30:33 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW SERIES ON db0 FROM mydb.myrp1.cpu`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT _seriesKey AS "key" FROM mydb.myrp1.cpu`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-04-27 14:30:33 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW SERIES FROM mydb.myrp1./c.*/`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT _seriesKey AS "key" FROM mydb.myrp1./c.*/`,
|
2016-04-27 14:30:33 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW SERIES ON db0 FROM mydb.myrp1./c.*/`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT _seriesKey AS "key" FROM mydb.myrp1./c.*/`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-02-23 23:43:19 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM /.+/`,
|
2016-02-23 23:43:19 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS ON db0`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM db0../.+/`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-02-23 23:43:19 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS FROM cpu`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM cpu`,
|
2016-02-23 23:43:19 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS ON db0 FROM cpu`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM db0..cpu`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-02-23 23:43:19 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS FROM /c.*/`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM /c.*/`,
|
2016-02-23 23:43:19 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS ON db0 FROM /c.*/`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM db0../c.*/`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-02-23 23:43:19 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS FROM cpu WHERE region = 'uswest'`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM cpu WHERE region = 'uswest'`,
|
2016-03-06 14:52:34 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS ON db0 FROM cpu WHERE region = 'uswest'`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM db0..cpu WHERE region = 'uswest'`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-04-27 14:30:33 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS FROM mydb.myrp1.cpu`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM mydb.myrp1.cpu`,
|
2016-04-27 14:30:33 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS ON db0 FROM mydb.myrp1.cpu`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM mydb.myrp1.cpu`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-04-27 14:30:33 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS FROM mydb.myrp1./c.*/`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM mydb.myrp1./c.*/`,
|
2016-04-27 14:30:33 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS ON db0 FROM mydb.myrp1./c.*/`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM mydb.myrp1./c.*/`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-04-27 14:30:33 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS FROM mydb.myrp1.cpu WHERE region = 'uswest'`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM mydb.myrp1.cpu WHERE region = 'uswest'`,
|
2016-04-27 14:30:33 +00:00
|
|
|
},
|
2016-09-13 20:36:56 +00:00
|
|
|
{
|
|
|
|
stmt: `SHOW TAG KEYS ON db0 FROM mydb.myrp1.cpu WHERE region = 'uswest'`,
|
2017-08-24 14:45:46 +00:00
|
|
|
s: `SELECT distinct(_tagKey) AS tagKey FROM mydb.myrp1.cpu WHERE region = 'uswest'`,
|
2016-09-13 20:36:56 +00:00
|
|
|
},
|
2016-02-23 23:43:19 +00:00
|
|
|
{
|
|
|
|
stmt: `SELECT value FROM cpu`,
|
|
|
|
s: `SELECT value FROM cpu`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
stmt, err := influxql.ParseStatement(test.stmt)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error parsing statement: %s", err)
|
|
|
|
} else {
|
2017-10-30 17:24:15 +00:00
|
|
|
stmt, err = query.RewriteStatement(stmt)
|
2016-02-23 23:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error rewriting statement: %s", err)
|
|
|
|
} else if s := stmt.String(); s != test.s {
|
|
|
|
t.Errorf("error rendering string. expected %s, actual: %s", test.s, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|