more tests, simplify mapFunc signatures
parent
78bc740434
commit
b1becfbcfc
|
@ -2435,6 +2435,132 @@ func TestServer_Query_AggregateSelectors(t *testing.T) {
|
|||
command: `SELECT time, tx, last(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"results":[{"series":[{"name":"network","columns":["time","tx","last"],"values":[["2000-01-01T00:00:20Z",55,40],["2000-01-01T00:00:50Z",40,50],["2000-01-01T00:01:20Z",4,5]]}]}]}`,
|
||||
},
|
||||
&Query{
|
||||
name: "count - baseline 30s",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT count(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"results":[{"series":[{"name":"network","columns":["time","count"],"values":[["2000-01-01T00:00:00Z",3],["2000-01-01T00:00:30Z",3],["2000-01-01T00:01:00Z",3]]}]}]}`,
|
||||
},
|
||||
&Query{
|
||||
name: "count - time",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT time, count(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "count - tx",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT tx, count(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "distinct - baseline 30s",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT distinct(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"results":[{"series":[{"name":"network","columns":["time","distinct"],"values":[["2000-01-01T00:00:00Z",[10,40]],["2000-01-01T00:00:30Z",[40,50]],["2000-01-01T00:01:00Z",[5,70,90]]]}]}]}`,
|
||||
},
|
||||
&Query{
|
||||
name: "distinct - time",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT time, distinct(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: aggregate function distinct() can not be combined with other functions or fields"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "distinct - tx",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT tx, distinct(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: aggregate function distinct() can not be combined with other functions or fields"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "mean - baseline 30s",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT mean(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"results":[{"series":[{"name":"network","columns":["time","mean"],"values":[["2000-01-01T00:00:00Z",30],["2000-01-01T00:00:30Z",46.666666666666664],["2000-01-01T00:01:00Z",55]]}]}]}`,
|
||||
},
|
||||
&Query{
|
||||
name: "mean - time",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT time, mean(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "mean - tx",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT tx, mean(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "median - baseline 30s",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT median(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"results":[{"series":[{"name":"network","columns":["time","median"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",70]]}]}]}`,
|
||||
},
|
||||
&Query{
|
||||
name: "median - time",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT time, median(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "median - tx",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT tx, median(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "spread - baseline 30s",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT spread(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"results":[{"series":[{"name":"network","columns":["time","spread"],"values":[["2000-01-01T00:00:00Z",30],["2000-01-01T00:00:30Z",10],["2000-01-01T00:01:00Z",85]]}]}]}`,
|
||||
},
|
||||
&Query{
|
||||
name: "spread - time",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT time, spread(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "spread - tx",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT tx, spread(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "stddev - baseline 30s",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT stddev(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"results":[{"series":[{"name":"network","columns":["time","stddev"],"values":[["2000-01-01T00:00:00Z",17.320508075688775],["2000-01-01T00:00:30Z",5.773502691896258],["2000-01-01T00:01:00Z",44.44097208657794]]}]}]}`,
|
||||
},
|
||||
&Query{
|
||||
name: "stddev - time",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT time, stddev(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "stddev - tx",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT tx, stddev(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "percentile - baseline 30s",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT percentile(rx, 75) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"results":[{"series":[{"name":"network","columns":["time","percentile"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",70]]}]}]}`,
|
||||
},
|
||||
&Query{
|
||||
name: "percentile - time",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT time, percentile(rx, 75) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
&Query{
|
||||
name: "percentile - tx",
|
||||
params: url.Values{"db": []string{"db0"}},
|
||||
command: `SELECT tx, percentile(rx, 75) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`,
|
||||
exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`,
|
||||
},
|
||||
}...)
|
||||
|
||||
for i, query := range test.queries {
|
||||
|
|
|
@ -62,21 +62,21 @@ func initializeMapFunc(c *influxql.Call) (mapFunc, error) {
|
|||
return MapDistinct, nil
|
||||
case "sum":
|
||||
return func(itr Iterator) interface{} {
|
||||
return MapSum(itr, c)
|
||||
return MapSum(itr, c.Fields()[0])
|
||||
}, nil
|
||||
case "mean":
|
||||
return func(itr Iterator) interface{} {
|
||||
return MapMean(itr, c)
|
||||
return MapMean(itr, c.Fields()[0])
|
||||
}, nil
|
||||
case "median":
|
||||
return MapStddev, nil
|
||||
case "min":
|
||||
return func(itr Iterator) interface{} {
|
||||
return MapMin(itr, c)
|
||||
return MapMin(itr, c.Fields()[0])
|
||||
}, nil
|
||||
case "max":
|
||||
return func(itr Iterator) interface{} {
|
||||
return MapMax(itr, c)
|
||||
return MapMax(itr, c.Fields()[0])
|
||||
}, nil
|
||||
case "spread":
|
||||
return MapSpread, nil
|
||||
|
@ -84,11 +84,11 @@ func initializeMapFunc(c *influxql.Call) (mapFunc, error) {
|
|||
return MapStddev, nil
|
||||
case "first":
|
||||
return func(itr Iterator) interface{} {
|
||||
return MapFirst(itr, c)
|
||||
return MapFirst(itr, c.Fields()[0])
|
||||
}, nil
|
||||
case "last":
|
||||
return func(itr Iterator) interface{} {
|
||||
return MapLast(itr, c)
|
||||
return MapLast(itr, c.Fields()[0])
|
||||
}, nil
|
||||
|
||||
case "top", "bottom":
|
||||
|
@ -350,7 +350,7 @@ const (
|
|||
)
|
||||
|
||||
// MapSum computes the summation of values in an iterator.
|
||||
func MapSum(itr Iterator, call *influxql.Call) interface{} {
|
||||
func MapSum(itr Iterator, fieldName string) interface{} {
|
||||
n := float64(0)
|
||||
count := 0
|
||||
var resultType NumberType
|
||||
|
@ -363,7 +363,7 @@ func MapSum(itr Iterator, call *influxql.Call) interface{} {
|
|||
n += float64(n1)
|
||||
resultType = Int64Type
|
||||
case map[string]interface{}:
|
||||
if d, r, ok := decodeValueAndNumberType(n1[call.Fields()[0]]); ok {
|
||||
if d, r, ok := decodeValueAndNumberType(n1[fieldName]); ok {
|
||||
resultType = r
|
||||
n += d
|
||||
}
|
||||
|
@ -410,7 +410,7 @@ func ReduceSum(values []interface{}) interface{} {
|
|||
}
|
||||
|
||||
// MapMean computes the count and sum of values in an iterator to be combined by the reducer.
|
||||
func MapMean(itr Iterator, call *influxql.Call) interface{} {
|
||||
func MapMean(itr Iterator, fieldName string) interface{} {
|
||||
out := &meanMapOutput{}
|
||||
|
||||
for k, v := itr.Next(); k != -1; k, v = itr.Next() {
|
||||
|
@ -422,7 +422,7 @@ func MapMean(itr Iterator, call *influxql.Call) interface{} {
|
|||
out.Mean += (float64(n1) - out.Mean) / float64(out.Count)
|
||||
out.ResultType = Int64Type
|
||||
case map[string]interface{}:
|
||||
if d, r, ok := decodeValueAndNumberType(n1[call.Fields()[0]]); ok {
|
||||
if d, r, ok := decodeValueAndNumberType(n1[fieldName]); ok {
|
||||
out.Mean += (d - out.Mean) / float64(out.Count)
|
||||
out.ResultType = r
|
||||
}
|
||||
|
@ -624,7 +624,7 @@ type minMaxMapOut struct {
|
|||
}
|
||||
|
||||
// MapMin collects the values to pass to the reducer
|
||||
func MapMin(itr Iterator, call *influxql.Call) interface{} {
|
||||
func MapMin(itr Iterator, fieldName string) interface{} {
|
||||
min := &minMaxMapOut{}
|
||||
|
||||
pointsYielded := false
|
||||
|
@ -638,7 +638,7 @@ func MapMin(itr Iterator, call *influxql.Call) interface{} {
|
|||
val = float64(n)
|
||||
min.Type = Int64Type
|
||||
case map[string]interface{}:
|
||||
if d, t, ok := decodeValueAndNumberType(n[call.Fields()[0]]); ok {
|
||||
if d, t, ok := decodeValueAndNumberType(n[fieldName]); ok {
|
||||
val, min.Type = d, t
|
||||
} else {
|
||||
continue
|
||||
|
@ -733,7 +733,7 @@ func decodeValueAndNumberType(v interface{}) (float64, NumberType, bool) {
|
|||
}
|
||||
|
||||
// MapMax collects the values to pass to the reducer
|
||||
func MapMax(itr Iterator, call *influxql.Call) interface{} {
|
||||
func MapMax(itr Iterator, fieldName string) interface{} {
|
||||
max := &minMaxMapOut{}
|
||||
|
||||
pointsYielded := false
|
||||
|
@ -747,7 +747,7 @@ func MapMax(itr Iterator, call *influxql.Call) interface{} {
|
|||
val = float64(n)
|
||||
max.Type = Int64Type
|
||||
case map[string]interface{}:
|
||||
if d, t, ok := decodeValueAndNumberType(n[call.Fields()[0]]); ok {
|
||||
if d, t, ok := decodeValueAndNumberType(n[fieldName]); ok {
|
||||
val, max.Type = d, t
|
||||
} else {
|
||||
continue
|
||||
|
@ -957,7 +957,7 @@ type firstLastMapOutput struct {
|
|||
|
||||
// MapFirst collects the values to pass to the reducer
|
||||
// This function assumes time ordered input
|
||||
func MapFirst(itr Iterator, call *influxql.Call) interface{} {
|
||||
func MapFirst(itr Iterator, fieldName string) interface{} {
|
||||
var fields map[string]interface{}
|
||||
k, v := itr.Next()
|
||||
fields = itr.Fields()
|
||||
|
@ -965,12 +965,12 @@ func MapFirst(itr Iterator, call *influxql.Call) interface{} {
|
|||
return nil
|
||||
}
|
||||
if n, ok := v.(map[string]interface{}); ok {
|
||||
v = n[call.Fields()[0]]
|
||||
v = n[fieldName]
|
||||
}
|
||||
|
||||
nextk, nextv := itr.Next()
|
||||
if n, ok := nextv.(map[string]interface{}); ok {
|
||||
nextv = n[call.Fields()[0]]
|
||||
nextv = n[fieldName]
|
||||
}
|
||||
for nextk == k {
|
||||
if greaterThan(nextv, v) {
|
||||
|
@ -1023,13 +1023,13 @@ func ReduceFirst(values []interface{}) interface{} {
|
|||
}
|
||||
|
||||
// MapLast collects the values to pass to the reducer
|
||||
func MapLast(itr Iterator, call *influxql.Call) interface{} {
|
||||
func MapLast(itr Iterator, fieldName string) interface{} {
|
||||
out := &firstLastMapOutput{}
|
||||
pointsYielded := false
|
||||
|
||||
for k, v := itr.Next(); k != -1; k, v = itr.Next() {
|
||||
if n, ok := v.(map[string]interface{}); ok {
|
||||
v = n[call.Fields()[0]]
|
||||
v = n[fieldName]
|
||||
}
|
||||
// Initialize last
|
||||
if !pointsYielded {
|
||||
|
|
|
@ -67,8 +67,7 @@ func (t *testIterator) TMin() int64 {
|
|||
|
||||
func TestMapMeanNoValues(t *testing.T) {
|
||||
iter := &testIterator{}
|
||||
var c *influxql.Call
|
||||
if got := MapMean(iter, c); got != nil {
|
||||
if got := MapMean(iter, ""); got != nil {
|
||||
t.Errorf("output mismatch: exp nil got %v", got)
|
||||
}
|
||||
}
|
||||
|
@ -97,8 +96,7 @@ func TestMapMean(t *testing.T) {
|
|||
values: test.input,
|
||||
}
|
||||
|
||||
var c *influxql.Call
|
||||
got := MapMean(iter, c)
|
||||
got := MapMean(iter, "")
|
||||
if got == nil {
|
||||
t.Fatalf("MapMean(%v): output mismatch: exp %v got %v", test.input, test.output, got)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue