From a1cdc71ae5ccd74223f2487583ea9ae7d95c9394 Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Mon, 1 Feb 2016 17:08:37 -0500 Subject: [PATCH] Add binary expr tests to influxql package No boolean operator tests because the parser doesn't currently allow it. I am currently keeping the code that performs this for now until we decide whether to allow boolean operators inside of a select clause. It would be easy to have the new query engine do something like this: SELECT mean(value) < 10 FROM cpu GROUP BY time(1m) And then you would have it return true or false for each interval, but the same can be done through a where clause too. --- influxql/select_test.go | 204 ++++++++++++++++++++++++++++++++++------ 1 file changed, 176 insertions(+), 28 deletions(-) diff --git a/influxql/select_test.go b/influxql/select_test.go index 5e49e6a01a..ab6cb319bb 100644 --- a/influxql/select_test.go +++ b/influxql/select_test.go @@ -363,8 +363,8 @@ func TestSelect_Raw(t *testing.T) { } } -// Ensure a SELECT binary expr add query can be executed. -func TestSelect_BinaryExpr_Add_RHS_Float(t *testing.T) { +// Ensure a SELECT binary expr queries can be executed as floats. +func TestSelect_BinaryExpr_Float(t *testing.T) { var ic IteratorCreator ic.CreateIteratorFn = func(opt influxql.IteratorOptions) (influxql.Iterator, error) { return &FloatIterator{Points: []influxql.FloatPoint{ @@ -374,39 +374,187 @@ func TestSelect_BinaryExpr_Add_RHS_Float(t *testing.T) { }}, nil } - // Execute selection. - itrs, err := influxql.Select(MustParseSelectStatement(`SELECT value + 2 FROM cpu WHERE time >= '1970-01-01T00:00:00Z' AND time < '1970-01-02T00:00:00Z'`), &ic) - if err != nil { - t.Fatal(err) - } else if a := Iterators(itrs).ReadAll(); !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: 22}}, - {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: 12}}, - {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: 21}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) + for _, test := range []struct { + Name string + Statement string + Points [][]influxql.Point + }{ + { + Name: "rhs binary add", + Statement: `SELECT value + 2 FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: 22}}, + {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: 12}}, + {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: 21}}, + }, + }, + { + Name: "lhs binary add", + Statement: `SELECT 2 + value FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: 22}}, + {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: 12}}, + {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: 21}}, + }, + }, + { + Name: "rhs binary multiply", + Statement: `SELECT value * 2 FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: 40}}, + {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: 20}}, + {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: 38}}, + }, + }, + { + Name: "lhs binary multiply", + Statement: `SELECT 2 * value FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: 40}}, + {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: 20}}, + {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: 38}}, + }, + }, + { + Name: "rhs binary subtract", + Statement: `SELECT value - 2 FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: 18}}, + {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: 8}}, + {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: 17}}, + }, + }, + { + Name: "lhs binary subtract", + Statement: `SELECT 2 - value FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: -18}}, + {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: -8}}, + {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: -17}}, + }, + }, + { + Name: "rhs binary division", + Statement: `SELECT value / 2 FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: 10}}, + {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: 5}}, + {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: float64(19) / 2}}, + }, + }, + { + Name: "lhs binary division", + Statement: `SELECT 38 / value FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: 1.9}}, + {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: 3.8}}, + {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: 2}}, + }, + }, + } { + itrs, err := influxql.Select(MustParseSelectStatement(test.Statement), &ic) + if err != nil { + t.Errorf("%s: parse error: %s", test.Name, err) + } else if a := Iterators(itrs).ReadAll(); !deep.Equal(a, test.Points) { + t.Errorf("%s: unexpected points: %s", test.Name, spew.Sdump(a)) + } } } -// Ensure a SELECT binary expr add query can be executed. -func TestSelect_BinaryExpr_Add_LHS_Float(t *testing.T) { +// Ensure a SELECT binary expr queries can be executed as integers. +func TestSelect_BinaryExpr_Integer(t *testing.T) { var ic IteratorCreator ic.CreateIteratorFn = func(opt influxql.IteratorOptions) (influxql.Iterator, error) { - return &FloatIterator{Points: []influxql.FloatPoint{ - {Name: "cpu", Time: 0 * Second, Value: 20, Aux: []interface{}{float64(20)}}, - {Name: "cpu", Time: 5 * Second, Value: 10, Aux: []interface{}{float64(10)}}, - {Name: "cpu", Time: 9 * Second, Value: 19, Aux: []interface{}{float64(19)}}, + return &IntegerIterator{Points: []influxql.IntegerPoint{ + {Name: "cpu", Time: 0 * Second, Value: 20, Aux: []interface{}{int64(20)}}, + {Name: "cpu", Time: 5 * Second, Value: 10, Aux: []interface{}{int64(10)}}, + {Name: "cpu", Time: 9 * Second, Value: 19, Aux: []interface{}{int64(19)}}, }}, nil } - // Execute selection. - itrs, err := influxql.Select(MustParseSelectStatement(`SELECT 2 + value FROM cpu WHERE time >= '1970-01-01T00:00:00Z' AND time < '1970-01-02T00:00:00Z'`), &ic) - if err != nil { - t.Fatal(err) - } else if a := Iterators(itrs).ReadAll(); !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Name: "cpu", Time: 0 * Second, Value: 22}}, - {&influxql.FloatPoint{Name: "cpu", Time: 5 * Second, Value: 12}}, - {&influxql.FloatPoint{Name: "cpu", Time: 9 * Second, Value: 21}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) + for _, test := range []struct { + Name string + Statement string + Points [][]influxql.Point + }{ + { + Name: "rhs binary add", + Statement: `SELECT value + 2 FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.IntegerPoint{Name: "cpu", Time: 0 * Second, Value: 22}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 5 * Second, Value: 12}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 9 * Second, Value: 21}}, + }, + }, + { + Name: "lhs binary add", + Statement: `SELECT 2 + value FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.IntegerPoint{Name: "cpu", Time: 0 * Second, Value: 22}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 5 * Second, Value: 12}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 9 * Second, Value: 21}}, + }, + }, + { + Name: "rhs binary multiply", + Statement: `SELECT value * 2 FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.IntegerPoint{Name: "cpu", Time: 0 * Second, Value: 40}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 5 * Second, Value: 20}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 9 * Second, Value: 38}}, + }, + }, + { + Name: "lhs binary multiply", + Statement: `SELECT 2 * value FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.IntegerPoint{Name: "cpu", Time: 0 * Second, Value: 40}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 5 * Second, Value: 20}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 9 * Second, Value: 38}}, + }, + }, + { + Name: "rhs binary subtract", + Statement: `SELECT value - 2 FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.IntegerPoint{Name: "cpu", Time: 0 * Second, Value: 18}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 5 * Second, Value: 8}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 9 * Second, Value: 17}}, + }, + }, + { + Name: "lhs binary subtract", + Statement: `SELECT 2 - value FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.IntegerPoint{Name: "cpu", Time: 0 * Second, Value: -18}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 5 * Second, Value: -8}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 9 * Second, Value: -17}}, + }, + }, + { + Name: "rhs binary division", + Statement: `SELECT value / 2 FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.IntegerPoint{Name: "cpu", Time: 0 * Second, Value: 10}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 5 * Second, Value: 5}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 9 * Second, Value: 9}}, + }, + }, + { + Name: "lhs binary division", + Statement: `SELECT 38 / value FROM cpu`, + Points: [][]influxql.Point{ + {&influxql.IntegerPoint{Name: "cpu", Time: 0 * Second, Value: 1}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 5 * Second, Value: 3}}, + {&influxql.IntegerPoint{Name: "cpu", Time: 9 * Second, Value: 2}}, + }, + }, + } { + itrs, err := influxql.Select(MustParseSelectStatement(test.Statement), &ic) + if err != nil { + t.Errorf("%s: parse error: %s", test.Name, err) + } else if a := Iterators(itrs).ReadAll(); !deep.Equal(a, test.Points) { + t.Errorf("%s: unexpected points: %s", test.Name, spew.Sdump(a)) + } } }