From d61377f38d8c042fef196529e1d31511c02d4d5e Mon Sep 17 00:00:00 2001 From: Jason Wilder Date: Thu, 23 Apr 2015 10:49:16 -0600 Subject: [PATCH] Fix panic: runtime error: index out of range Fixes #2374 --- influxql/functions.go | 4 ++++ influxql/functions_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/influxql/functions.go b/influxql/functions.go index eff918534ee..ef9704c33e3 100644 --- a/influxql/functions.go +++ b/influxql/functions.go @@ -107,6 +107,10 @@ func InitializeReduceFunc(c *Call) (ReduceFunc, error) { case "last": return ReduceLast, nil case "percentile": + if len(c.Args) != 2 { + return nil, fmt.Errorf("expected float argument in percentile()") + } + lit, ok := c.Args[1].(*NumberLiteral) if !ok { return nil, fmt.Errorf("expected float argument in percentile()") diff --git a/influxql/functions_test.go b/influxql/functions_test.go index 2dfaa11ffe9..1f965e256f4 100644 --- a/influxql/functions_test.go +++ b/influxql/functions_test.go @@ -65,3 +65,36 @@ func TestMapMean(t *testing.T) { } } + +func TestInitializeReduceFuncPercentile(t *testing.T) { + // No args + c := &Call{ + Name: "percentile", + Args: []Expr{}, + } + _, err := InitializeReduceFunc(c) + if err == nil { + t.Errorf("InitializedReduceFunc(%v) expected error. got nil", c) + } + + if exp := "expected float argument in percentile()"; err.Error() != exp { + t.Errorf("InitializedReduceFunc(%v) mismatch. exp %v got %v", c, exp, err.Error()) + } + + // No percentile arg + c = &Call{ + Name: "percentile", + Args: []Expr{ + &VarRef{Val: "field1"}, + }, + } + + _, err = InitializeReduceFunc(c) + if err == nil { + t.Errorf("InitializedReduceFunc(%v) expected error. got nil", c) + } + + if exp := "expected float argument in percentile()"; err.Error() != exp { + t.Errorf("InitializedReduceFunc(%v) mismatch. exp %v got %v", c, exp, err.Error()) + } +}