Skip to content

Commit

Permalink
Fix error calling MarshalJSON for type *influxdb.Result: json: unsupp…
Browse files Browse the repository at this point in the history
…orted value: NaN

If the MapMean did not have any values in the iterator, it would return
and empty meanMapOutput().  ReduceMean would in turn divider the results
by an unitialized float64 producing a NaN.  NaN is not serializable to JSON
by default.
  • Loading branch information
jwilder committed Apr 23, 2015
1 parent a557ca8 commit b2a07a3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
7 changes: 6 additions & 1 deletion influxql/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,12 @@ func MapMean(itr Iterator) interface{} {
out.Count++
out.Mean += (v.(float64) - out.Mean) / float64(out.Count)
}
return out

if out.Count > 0 {
return out
}

return nil
}

type meanMapOutput struct {
Expand Down
67 changes: 67 additions & 0 deletions influxql/functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package influxql

import "testing"

type point struct {
seriesID uint64
timestamp int64
value interface{}
}

type testIterator struct {
values []point
}

func (t *testIterator) Next() (seriesID uint64, timestamp int64, value interface{}) {
if len(t.values) > 0 {
v := t.values[0]
t.values = t.values[1:]
return v.seriesID, v.timestamp, v.value
}
return 0, 0, nil
}

func TestMapMeanNoValues(t *testing.T) {
iter := &testIterator{}
if got := MapMean(iter); got != nil {
t.Errorf("output mismatch: exp nil got %v", got)
}
}

func TestMapMean(t *testing.T) {

tests := []struct {
input []point
output *meanMapOutput
}{
{ // Single point
input: []point{
point{0, 1, 1.0},
},
output: &meanMapOutput{1, 1},
},
{ // Two points
input: []point{
point{0, 1, 2.0},
point{0, 2, 8.0},
},
output: &meanMapOutput{2, 5.0},
},
}

for _, test := range tests {
iter := &testIterator{
values: test.input,
}

got := MapMean(iter)
if got == nil {
t.Fatalf("MapMean(%v): output mismatch: exp %v got %v", test.input, test.output, got)
}

if got.(*meanMapOutput).Count != test.output.Count || got.(*meanMapOutput).Mean != test.output.Mean {
t.Errorf("output mismatch: exp %v got %v", test.output, got)
}

}
}

0 comments on commit b2a07a3

Please sign in to comment.