Skip to content

give plan nodes bounds #142

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

Merged
merged 1 commit into from
Oct 18, 2018
Merged
Changes from all 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
5 changes: 3 additions & 2 deletions bounds.go
Original file line number Diff line number Diff line change
@@ -5,12 +5,13 @@ import "time"
type Bounds struct {
Start Time
Stop Time
Now time.Time
}

// IsEmpty reports whether the given bounds
// are empty, i.e., if start >= stop.
func (b Bounds) IsEmpty(now time.Time) bool {
return b.Start.Time(now).Equal(b.Stop.Time(now)) || b.Start.Time(now).After(b.Stop.Time(now))
func (b Bounds) IsEmpty() bool {
return b.Start.Time(b.Now).Equal(b.Stop.Time(b.Now)) || b.Start.Time(b.Now).After(b.Stop.Time(b.Now))
}

// HasZero returns true if the given bounds contain a Go zero time value as either Start or Stop.
10 changes: 5 additions & 5 deletions bounds_test.go
Original file line number Diff line number Diff line change
@@ -73,53 +73,53 @@ func TestBounds_IsEmpty(t *testing.T) {
}{
{
name: "empty bounds / start == stop",
now: time.Date(2018, time.August, 14, 11, 0, 0, 0, time.UTC),
bounds: flux.Bounds{
Start: flux.Now,
Stop: flux.Now,
Now: time.Date(2018, time.August, 14, 11, 0, 0, 0, time.UTC),
},
want: true,
},
{
name: "empty bounds / absolute now == relative now",
now: time.Date(2018, time.August, 14, 11, 0, 0, 0, time.UTC),
bounds: flux.Bounds{
Start: flux.Now,
Stop: flux.Time{
Absolute: time.Date(2018, time.August, 14, 11, 0, 0, 0, time.UTC),
},
Now: time.Date(2018, time.August, 14, 11, 0, 0, 0, time.UTC),
},
want: true,
},
{
name: "start > stop",
now: time.Date(2018, time.August, 14, 11, 0, 0, 0, time.UTC),
bounds: flux.Bounds{
Start: flux.Time{
IsRelative: true,
Relative: time.Hour,
},
Stop: flux.Now,
Now: time.Date(2018, time.August, 14, 11, 0, 0, 0, time.UTC),
},
want: true,
},
{
name: "start < stop",
now: time.Date(2018, time.August, 14, 11, 0, 0, 0, time.UTC),
bounds: flux.Bounds{
Start: flux.Time{
IsRelative: true,
Relative: -1 * time.Hour,
},
Stop: flux.Now,
Now: time.Date(2018, time.August, 14, 11, 0, 0, 0, time.UTC),
},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.bounds.IsEmpty(tt.now)
got := tt.bounds.IsEmpty()
if got != tt.want {
t.Errorf("unexpected result for bounds.IsEmpty(): got %t, want %t", got, tt.want)
}
16 changes: 13 additions & 3 deletions execute/executor.go
Original file line number Diff line number Diff line change
@@ -138,11 +138,21 @@ func (v *createExecutionNodeVisitor) Visit(node plan.PlanNode) error {
kind := spec.Kind()
id := plan.ProcedureIDFromNodeID(node.ID())

// Add explicit stream context if bounds are set on this node
var streamContext streamContext
if node.Bounds() != nil {
streamContext.bounds = &Bounds{
Start: node.Bounds().Start,
Stop: node.Bounds().Stop,
}
}

// Build execution context
ec := executionContext{
ctx: v.ctx,
es: v.es,
parents: make([]DatasetID, len(node.Predecessors())),
ctx: v.ctx,
es: v.es,
parents: make([]DatasetID, len(node.Predecessors())),
streamContext: streamContext,
}

for i, pred := range node.Predecessors() {
38 changes: 29 additions & 9 deletions functions/transformations/range.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import (
plan "github.com/influxdata/flux/planner"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
"github.com/pkg/errors"
)

const RangeKind = "range"
@@ -100,6 +101,18 @@ type RangeProcedureSpec struct {
StopCol string
}

// TimeBounds implements plan.BoundsAwareProcedureSpec
func (s *RangeProcedureSpec) TimeBounds(predecessorBounds *plan.Bounds) *plan.Bounds {
bounds := &plan.Bounds{
Start: values.ConvertTime(s.Bounds.Start.Time(s.Bounds.Now)),
Stop: values.ConvertTime(s.Bounds.Stop.Time(s.Bounds.Now)),
}
if predecessorBounds != nil {
bounds = bounds.Intersect(predecessorBounds)
}
return bounds
}

func newRangeProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*RangeOpSpec)

@@ -111,11 +124,21 @@ func newRangeProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.Proc
spec.TimeCol = execute.DefaultTimeColLabel
}

bounds := flux.Bounds{
Start: spec.Start,
Stop: spec.Stop,
Now: pa.Now(),
}

if bounds.HasZero() {
return nil, errors.New(`cannot pass zero time to 'range'`)
}
if bounds.IsEmpty() {
return nil, errors.New("cannot query an empty range")
}

return &RangeProcedureSpec{
Bounds: flux.Bounds{
Start: spec.Start,
Stop: spec.Stop,
},
Bounds: bounds,
TimeCol: spec.TimeCol,
StartCol: spec.StartCol,
StopCol: spec.StopCol,
@@ -142,12 +165,9 @@ func createRangeTransformation(id execute.DatasetID, mode execute.AccumulationMo
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)

bounds := execute.Bounds{
Start: a.ResolveTime(s.Bounds.Start),
Stop: a.ResolveTime(s.Bounds.Stop),
}
bounds := a.StreamContext().Bounds()

t, err := NewRangeTransformation(d, cache, s, bounds)
t, err := NewRangeTransformation(d, cache, s, *bounds)
if err != nil {
return nil, nil, err
}
13 changes: 12 additions & 1 deletion functions/transformations/shift.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package transformations

import (
"fmt"
"time"

"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
@@ -73,9 +74,18 @@ type ShiftProcedureSpec struct {
plan.DefaultCost
Shift flux.Duration
Columns []string
Now time.Time
}

func newShiftProcedure(qs flux.OperationSpec, _ plan.Administration) (plan.ProcedureSpec, error) {
// TimeBounds implements plan.BoundsAwareProcedureSpec
func (s *ShiftProcedureSpec) TimeBounds(predecessorBounds *plan.Bounds) *plan.Bounds {
if predecessorBounds != nil {
return predecessorBounds.Shift(values.Duration(s.Shift))
}
return nil
}

func newShiftProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*ShiftOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
@@ -84,6 +94,7 @@ func newShiftProcedure(qs flux.OperationSpec, _ plan.Administration) (plan.Proce
return &ShiftProcedureSpec{
Shift: spec.Shift,
Columns: spec.Columns,
Now: pa.Now(),
}, nil
}

110 changes: 110 additions & 0 deletions planner/bounds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package planner

import "github.com/influxdata/flux/values"

// EmptyBounds is a time range containing only a single point
var EmptyBounds = &Bounds{
Start: values.Time(0),
Stop: values.Time(0),
}

// Bounds is a range of time
type Bounds struct {
Start values.Time
Stop values.Time
}

// BoundsAwareProcedureSpec is any procedure
// that modifies the time bounds of its data.
type BoundsAwareProcedureSpec interface {
TimeBounds(predecessorBounds *Bounds) *Bounds
}

// ComputeBounds computes the time bounds for a
// plan node from the bounds of its predecessors.
func ComputeBounds(node PlanNode) error {
var bounds *Bounds

for _, pred := range node.Predecessors() {

if pred.Bounds() != nil && bounds == nil {
bounds = pred.Bounds()
}
if pred.Bounds() != nil && bounds != nil {
bounds = bounds.Union(pred.Bounds())
}
}

if s, ok := node.ProcedureSpec().(BoundsAwareProcedureSpec); ok {
bounds = s.TimeBounds(bounds)
}
node.SetBounds(bounds)
return nil
}

// IsEmpty reports whether the given bounds contain at most a single point
func (b *Bounds) IsEmpty() bool {
return b.Start >= b.Stop
}

// Contains reports whether a given time is contained within the time range
func (b *Bounds) Contains(t values.Time) bool {
return t >= b.Start && t < b.Stop
}

// Overlaps reports whether two given bounds have overlapping time ranges
func (b *Bounds) Overlaps(o *Bounds) bool {
return b.Contains(o.Start) ||
(b.Contains(o.Stop) && o.Stop > b.Start) ||
o.Contains(b.Start)
}

// Union returns the smallest bounds which contain both input bounds.
// It returns empty bounds if one of the input bounds are empty.
func (b *Bounds) Union(o *Bounds) *Bounds {
if b.IsEmpty() || o.IsEmpty() {
return EmptyBounds
}
u := new(Bounds)

u.Start = b.Start
if o.Start < b.Start {
u.Start = o.Start
}

u.Stop = b.Stop
if o.Stop > b.Stop {
u.Stop = o.Stop
}

return u
}

// Intersect returns the intersection of two bounds.
// It returns empty bounds if one of the input bounds are empty.
func (b *Bounds) Intersect(o *Bounds) *Bounds {
if b.IsEmpty() || o.IsEmpty() || !b.Overlaps(o) {
return EmptyBounds
}
i := new(Bounds)

i.Start = b.Start
if o.Start > b.Start {
i.Start = o.Start
}

i.Stop = b.Stop
if o.Stop < b.Stop {
i.Stop = o.Stop
}

return i
}

// Shift moves the start and stop values of a time range by a specified duration
func (b *Bounds) Shift(d values.Duration) *Bounds {
return &Bounds{
Start: b.Start.Add(d),
Stop: b.Stop.Add(d),
}
}
Loading