Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spread #1591

Merged
merged 6 commits into from
Feb 12, 2015
Merged

Spread #1591

Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions influxql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func (p *Planner) planCall(e *Executor, c *Call) (Processor, error) {
mapFn, reduceFn = MapMin, ReduceMin
case "max":
mapFn, reduceFn = MapMax, ReduceMax
case "spread":
mapFn, reduceFn = MapSpread, ReduceSpread
case "stddev":
mapFn, reduceFn = MapStddev, ReduceStddev
case "first":
Expand Down Expand Up @@ -753,6 +755,53 @@ func ReduceMax(key Key, values []interface{}, e *Emitter) {
}
}

type spreadMapOutput struct {
min, max float64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be exported so that they can be serialized later

}

// MapSpread collects the values to pass to the reducer
func MapSpread(itr Iterator, e *Emitter, tmax int64) {
var out spreadMapOutput
pointsYielded := false

for k, v := itr.Next(); k != 0; k, v = itr.Next() {
val := v.(float64)
// Initialize
if !pointsYielded {
out.max = val
out.min = val
pointsYielded = true
}
out.max = math.Max(out.max, val)
out.min = math.Min(out.min, val)
}
if pointsYielded {
e.Emit(Key{tmax, itr.Tags()}, out)
}
}

// ReduceSpread computes the spread of values.
func ReduceSpread(key Key, values []interface{}, e *Emitter) {
var result spreadMapOutput
pointsYielded := false

for _, v := range values {
val := v.(spreadMapOutput)
// Initialize
if !pointsYielded {
result.max = val.max
result.min = val.min
pointsYielded = true
}
result.max = math.Max(result.max, val.max)
result.min = math.Min(result.min, val.min)
}
spread := result.max - result.min
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much point calculating spread if pointsYielded is false after the for loop.

if pointsYielded {
e.Emit(key, spread)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.Emit(key, result.max - result.min)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. changed.

}
}

// MapStddev collects the values to pass to the reducer
func MapStddev(itr Iterator, e *Emitter, tmax int64) {
var values []float64
Expand Down
66 changes: 66 additions & 0 deletions influxql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,72 @@ func TestPlanner_Plan_MinWithoutResults(t *testing.T) {
}
}

// Ensure the planner can plan and execute a spread query with results
func TestPlanner_Plan_SpreadWithResults(t *testing.T) {
tx := NewTx()
tx.CreateIteratorsFunc = func(stmt *influxql.SelectStatement) ([]influxql.Iterator, error) {
return []influxql.Iterator{
NewIterator(nil, []Point{
{"2000-01-01T00:00:00Z", float64(100)},
{"2000-01-01T00:00:10Z", float64(200)},
{"2000-01-01T00:00:20Z", float64(300)},
{"2000-01-01T00:00:30Z", float64(400)},
}),
NewIterator(nil, []Point{
{"2000-01-01T00:01:00Z", float64(0)},
{"2000-01-01T00:01:10Z", float64(1)},
{"2000-01-01T00:01:20Z", float64(100)},
{"2000-01-01T00:01:30Z", float64(1000)},
}),
NewIterator(nil, []Point{
{"2000-01-01T00:02:20Z", float64(0)},
{"2000-01-01T00:02:30Z", float64(-10)},
{"2000-01-01T00:02:40Z", float64(-20)},
{"2000-01-01T00:02:50Z", float64(-30)},
}),
NewIterator(nil, []Point{
{"2000-01-01T00:03:20Z", float64(-10)},
{"2000-01-01T00:03:50Z", float64(0)},
{"2000-01-01T00:03:30Z", float64(-30)},
{"2000-01-01T00:03:40Z", float64(-20)},
}),
NewIterator(nil, []Point{
{"2000-01-01T00:04:20Z", float64(10)},
{"2000-01-01T00:04:30Z", float64(30)},
{"2000-01-01T00:04:40Z", float64(0)},
{"2000-01-01T00:04:50Z", float64(20)},
})}, nil
}

// Expected resultset.
exp := minify(`[{"name":"cpu","columns":["time","spread"],"values":[["2000-01-01T00:00:00Z",300],["2000-01-01T00:01:00Z",1000],["2000-01-01T00:02:00Z",30],["2000-01-01T00:03:00Z",30],["2000-01-01T00:04:00Z",30]]}]`)

// Execute and compare with results.
rs := MustPlanAndExecute(NewDB(tx), `2000-01-01T12:00:00Z`,
`SELECT spread(value) FROM cpu WHERE time >= '2000-01-01' GROUP BY time(1m)`)
if act := minify(jsonify(rs)); exp != act {
t.Fatalf("unexpected resultset: %s", act)
}
}

// Ensure the planner can plan and execute a spread query without results
func TestPlanner_Plan_SpreadWithoutResults(t *testing.T) {
tx := NewTx()
tx.CreateIteratorsFunc = func(stmt *influxql.SelectStatement) ([]influxql.Iterator, error) {
return []influxql.Iterator{NewIterator(nil, []Point{})}, nil
}

// Expected resultset.
exp := "null"

// Execute and compare with results.
rs := MustPlanAndExecute(NewDB(tx), `2000-01-01T12:00:00Z`,
`SELECT spread(value) FROM cpu WHERE time >= '2000-01-01' GROUP BY time(1m)`)
if act := minify(jsonify(rs)); exp != act {
t.Fatalf("unexpected resultset: %s", act)
}
}

// Ensure the planner can plan and execute a standard deviation query with results
func TestPlanner_Plan_StddevWithResults(t *testing.T) {
tx := NewTx()
Expand Down
5 changes: 3 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2154,10 +2154,11 @@ func (s *Server) executeShowMeasurementsStatement(stmt *influxql.ShowMeasurement
row.Values = append(row.Values, []interface{}{v})
}

// Make a result.
// Make result.
result := &Result{
Rows: influxql.Rows{row},
Rows: make(influxql.Rows, 1),
}
result.Rows[0] = row
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was due to go vet failing. There is an unfortunate side affect of typed slices outside of the package that it doesn't understand, and will likely not change this behavior.


return result
}
Expand Down