Skip to content

Commit

Permalink
Fix panic: runtime error: index out of range
Browse files Browse the repository at this point in the history
Fixes #2374
  • Loading branch information
jwilder committed Apr 23, 2015
1 parent d0cc983 commit 155adb7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions influxql/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
Expand Down
33 changes: 33 additions & 0 deletions influxql/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

0 comments on commit 155adb7

Please sign in to comment.