From 5181c6babe8264ce3eb052860bb873141161c377 Mon Sep 17 00:00:00 2001 From: Nathaniel Cook Date: Tue, 17 May 2016 09:57:32 -0600 Subject: [PATCH] add more parameter checks, improve error messages to user --- influxql/ast.go | 11 +++++++---- influxql/functions.go | 5 ++++- influxql/parser_test.go | 3 +++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/influxql/ast.go b/influxql/ast.go index 910682208ed..d475eea60ab 100644 --- a/influxql/ast.go +++ b/influxql/ast.go @@ -1619,10 +1619,13 @@ func (s *SelectStatement) validateAggregates(tr targetRequirement) error { } else if !ok { return fmt.Errorf("must use aggregate function with %s", expr.Name) } - for i, arg := range expr.Args[1:3] { - if _, ok := arg.(*IntegerLiteral); !ok { - return fmt.Errorf("expected integer argument as %dth arg in %s", i+1, expr.Name) - } + if arg, ok := expr.Args[1].(*IntegerLiteral); !ok { + return fmt.Errorf("expected integer argument as second arg in %s", expr.Name) + } else if arg.Val <= 0 { + return fmt.Errorf("second arg to %s must be greater than 0, got %d", expr.Name, arg.Val) + } + if _, ok := expr.Args[2].(*IntegerLiteral); !ok { + return fmt.Errorf("expected integer argument as third arg in %s", expr.Name) } default: if err := s.validSelectWithAggregate(); err != nil { diff --git a/influxql/functions.go b/influxql/functions.go index b78e04797ff..0476e292d2e 100644 --- a/influxql/functions.go +++ b/influxql/functions.go @@ -403,12 +403,15 @@ func (r *FloatHoltWintersReducer) roundTime(t int64) int64 { } func (r *FloatHoltWintersReducer) Emit() []FloatPoint { - if l := len(r.points); l < 2 || r.seasonal && l < r.m { + if l := len(r.points); l < 2 || r.seasonal && l < r.m || r.h <= 0 { return nil } // First fill in r.y with values and NaNs for missing values start, stop := r.roundTime(r.points[0].Time), r.roundTime(r.points[len(r.points)-1].Time) count := (stop - start) / r.interval + if count <= 0 { + return nil + } r.y = make([]float64, 1, count) r.y[0] = r.points[0].Value t := r.roundTime(r.points[0].Time) diff --git a/influxql/parser_test.go b/influxql/parser_test.go index 87835cc4cb5..7c46be5fcc8 100644 --- a/influxql/parser_test.go +++ b/influxql/parser_test.go @@ -2175,6 +2175,9 @@ func TestParser_ParseStatement(t *testing.T) { {s: `SELECT holt_winters(value) FROM myseries where time < now() and time > now() - 1d`, err: `invalid number of arguments for holt_winters, expected 3, got 1`}, {s: `SELECT holt_winters(value, 10, 2) FROM myseries where time < now() and time > now() - 1d`, err: `must use aggregate function with holt_winters`}, {s: `SELECT holt_winters(min(value), 10, 2) FROM myseries where time < now() and time > now() - 1d`, err: `holt_winters aggregate requires a GROUP BY interval`}, + {s: `SELECT holt_winters(min(value), 0, 2) FROM myseries where time < now() and time > now() - 1d GROUP BY time(1d)`, err: `second arg to holt_winters must be greater than 0, got 0`}, + {s: `SELECT holt_winters(min(value), false, 2) FROM myseries where time < now() and time > now() - 1d GROUP BY time(1d)`, err: `expected integer argument as second arg in holt_winters`}, + {s: `SELECT holt_winters(min(value), 10, 'string') FROM myseries where time < now() and time > now() - 1d GROUP BY time(1d)`, err: `expected integer argument as third arg in holt_winters`}, {s: `SELECT field1 from myseries WHERE host =~ 'asd' LIMIT 1`, err: `found asd, expected regex at line 1, char 42`}, {s: `SELECT value > 2 FROM cpu`, err: `invalid operator > in SELECT clause at line 1, char 8; operator is intended for WHERE clause`}, {s: `SELECT value = 2 FROM cpu`, err: `invalid operator = in SELECT clause at line 1, char 8; operator is intended for WHERE clause`},