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

feat(query/stdlib): enable read group min max #19158

Merged
merged 1 commit into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
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
75 changes: 75 additions & 0 deletions cmd/influxd/launcher/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,80 @@ from(bucket: v.bucket)
,result,table,kk,_value
,,0,kk0,32
,,1,kk1,35
`,
},
{
name: "min group",
data: []string{
"m0,k=k0,kk=kk0 f=0i 0",
"m0,k=k0,kk=kk1 f=1i 1000000000",
"m0,k=k0,kk=kk0 f=2i 2000000000",
"m0,k=k0,kk=kk1 f=3i 3000000000",
"m0,k=k0,kk=kk0 f=4i 4000000000",
"m0,k=k0,kk=kk1 f=5i 5000000000",
"m0,k=k0,kk=kk0 f=6i 6000000000",
"m0,k=k0,kk=kk1 f=5i 7000000000",
"m0,k=k0,kk=kk0 f=0i 8000000000",
"m0,k=k0,kk=kk1 f=6i 9000000000",
"m0,k=k0,kk=kk0 f=6i 10000000000",
"m0,k=k0,kk=kk1 f=7i 11000000000",
"m0,k=k0,kk=kk0 f=5i 12000000000",
"m0,k=k0,kk=kk1 f=8i 13000000000",
"m0,k=k0,kk=kk0 f=9i 14000000000",
"m0,k=k0,kk=kk1 f=5i 15000000000",
},
op: "readGroup(min)",
query: `
from(bucket: v.bucket)
|> range(start: 1970-01-01T00:00:00Z, stop: 1970-01-01T00:00:15Z)
|> group(columns: ["kk"])
|> min()
|> keep(columns: ["kk", "_value"])
`,
want: `
#datatype,string,long,string,long
#group,false,false,true,false
#default,_result,,,
,result,table,kk,_value
,,0,kk0,0
,,1,kk1,1
`,
},
{
name: "max group",
data: []string{
"m0,k=k0,kk=kk0 f=0i 0",
"m0,k=k0,kk=kk1 f=1i 1000000000",
"m0,k=k0,kk=kk0 f=2i 2000000000",
"m0,k=k0,kk=kk1 f=3i 3000000000",
"m0,k=k0,kk=kk0 f=4i 4000000000",
"m0,k=k0,kk=kk1 f=5i 5000000000",
"m0,k=k0,kk=kk0 f=6i 6000000000",
"m0,k=k0,kk=kk1 f=5i 7000000000",
"m0,k=k0,kk=kk0 f=0i 8000000000",
"m0,k=k0,kk=kk1 f=6i 9000000000",
"m0,k=k0,kk=kk0 f=6i 10000000000",
"m0,k=k0,kk=kk1 f=7i 11000000000",
"m0,k=k0,kk=kk0 f=5i 12000000000",
"m0,k=k0,kk=kk1 f=8i 13000000000",
"m0,k=k0,kk=kk0 f=9i 14000000000",
"m0,k=k0,kk=kk1 f=5i 15000000000",
},
op: "readGroup(max)",
query: `
from(bucket: v.bucket)
|> range(start: 1970-01-01T00:00:00Z, stop: 1970-01-01T00:00:15Z)
|> group(columns: ["kk"])
|> max()
|> keep(columns: ["kk", "_value"])
`,
want: `
#datatype,string,long,string,long
#group,false,false,true,false
#default,_result,,,
,result,table,kk,_value
,,0,kk0,9
,,1,kk1,8
`,
},
}
Expand All @@ -2247,6 +2321,7 @@ from(bucket: v.bucket)
feature.PushDownWindowAggregateMean(): true,
feature.PushDownWindowAggregateMin(): true,
feature.PushDownWindowAggregateMax(): true,
feature.PushDownGroupAggregateMinMax(): true,
}))

l.SetupOrFail(t)
Expand Down
6 changes: 6 additions & 0 deletions flags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@
contact: Monitoring Team
lifetime: temporary
expose: true

- name: Push Down Group Aggregate Min Max
description: Enable the min and max variants of the PushDownGroupAggregate planner rule
key: pushDownGroupAggregateMinMax
default: false
contact: Query Team
16 changes: 16 additions & 0 deletions kit/feature/list.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions query/stdlib/influxdata/influxdb/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,8 @@ func (rule PushDownGroupAggregateRule) Pattern() plan.Pattern {
universe.SumKind,
universe.FirstKind,
universe.LastKind,
universe.MinKind,
universe.MaxKind,
},
plan.Pat(ReadGroupPhysKind))
}
Expand Down Expand Up @@ -1075,6 +1077,28 @@ func (PushDownGroupAggregateRule) Rewrite(ctx context.Context, pn plan.Node) (pl
AggregateMethod: universe.LastKind,
})
return node, true, nil
case universe.MinKind:
// ReadGroup() -> min => ReadGroup(min)
if feature.PushDownGroupAggregateMinMax().Enabled(ctx) {
node := plan.CreatePhysicalNode("ReadGroupAggregate", &ReadGroupPhysSpec{
ReadRangePhysSpec: group.ReadRangePhysSpec,
GroupMode: group.GroupMode,
GroupKeys: group.GroupKeys,
AggregateMethod: universe.MinKind,
})
return node, true, nil
}
case universe.MaxKind:
// ReadGroup() -> max => ReadGroup(max)
if feature.PushDownGroupAggregateMinMax().Enabled(ctx) {
node := plan.CreatePhysicalNode("ReadGroupAggregate", &ReadGroupPhysSpec{
ReadRangePhysSpec: group.ReadRangePhysSpec,
GroupMode: group.GroupMode,
GroupKeys: group.GroupKeys,
AggregateMethod: universe.MaxKind,
})
return node, true, nil
}
}
return pn, false, nil
}
Expand Down Expand Up @@ -1102,6 +1126,12 @@ func canPushGroupedAggregate(ctx context.Context, pn plan.Node) bool {
case universe.LastKind:
agg := pn.ProcedureSpec().(*universe.LastProcedureSpec)
return caps.HaveLast() && agg.Column == execute.DefaultValueColLabel
case universe.MaxKind:
agg := pn.ProcedureSpec().(*universe.MaxProcedureSpec)
return caps.HaveMax() && agg.Column == execute.DefaultValueColLabel
case universe.MinKind:
agg := pn.ProcedureSpec().(*universe.MinProcedureSpec)
return caps.HaveMin() && agg.Column == execute.DefaultValueColLabel
}
return false
}
Expand Down
64 changes: 62 additions & 2 deletions query/stdlib/influxdata/influxdb/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2672,7 +2672,9 @@ func TestPushDownBareAggregateRule(t *testing.T) {
//
func TestPushDownGroupAggregateRule(t *testing.T) {
// Turn on all flags
ctx, _ := feature.Annotate(context.Background(), mock.NewFlagger(map[feature.Flag]interface{}{}))
ctx, _ := feature.Annotate(context.Background(), mock.NewFlagger(map[feature.Flag]interface{}{
feature.PushDownGroupAggregateMinMax(): true,
}))

caps := func(c query.GroupCapability) context.Context {
deps := influxdb.StorageDependencies{
Expand Down Expand Up @@ -2726,6 +2728,20 @@ func TestPushDownGroupAggregateRule(t *testing.T) {
},
}
}
minProcedureSpecVal := func() *universe.MinProcedureSpec {
return &universe.MinProcedureSpec{
SelectorConfig: execute.SelectorConfig{
Column: execute.DefaultValueColLabel,
},
}
}
maxProcedureSpecVal := func() *universe.MaxProcedureSpec {
return &universe.MaxProcedureSpec{
SelectorConfig: execute.SelectorConfig{
Column: execute.DefaultValueColLabel,
},
}
}
countProcedureSpec := func() *universe.CountProcedureSpec {
return &universe.CountProcedureSpec{
AggregateConfig: execute.DefaultAggregateConfig,
Expand Down Expand Up @@ -2829,12 +2845,56 @@ func TestPushDownGroupAggregateRule(t *testing.T) {
// ReadGroup() -> last => ReadGroup() -> last
tests = append(tests, plantest.RuleTestCase{
Context: caps(mockGroupCapability{}),
Name: "RewriteGroupLast",
Name: "NoLastCapability",
Rules: []plan.Rule{influxdb.PushDownGroupAggregateRule{}},
Before: simplePlanWithAgg("last", lastProcedureSpec()),
NoChange: true,
})

// ReadGroup() -> max => ReadGroup(max)
tests = append(tests, plantest.RuleTestCase{
Context: caps(mockGroupCapability{max: true}),
Name: "RewriteGroupMax",
Rules: []plan.Rule{influxdb.PushDownGroupAggregateRule{}},
Before: simplePlanWithAgg("max", maxProcedureSpecVal()),
After: &plantest.PlanSpec{
Nodes: []plan.Node{
plan.CreateLogicalNode("ReadGroupAggregate", readGroupAgg("max")),
},
},
})

// ReadGroup() -> max => ReadGroup() -> max
tests = append(tests, plantest.RuleTestCase{
Context: caps(mockGroupCapability{}),
Name: "NoMaxCapability",
Rules: []plan.Rule{influxdb.PushDownGroupAggregateRule{}},
Before: simplePlanWithAgg("max", maxProcedureSpecVal()),
NoChange: true,
})

// ReadGroup() -> min => ReadGroup(min)
tests = append(tests, plantest.RuleTestCase{
Context: caps(mockGroupCapability{min: true}),
Name: "RewriteGroupMin",
Rules: []plan.Rule{influxdb.PushDownGroupAggregateRule{}},
Before: simplePlanWithAgg("min", minProcedureSpecVal()),
After: &plantest.PlanSpec{
Nodes: []plan.Node{
plan.CreateLogicalNode("ReadGroupAggregate", readGroupAgg("min")),
},
},
})

// ReadGroup() -> min => ReadGroup() -> min
tests = append(tests, plantest.RuleTestCase{
Context: caps(mockGroupCapability{}),
Name: "NoMinCapability",
Rules: []plan.Rule{influxdb.PushDownGroupAggregateRule{}},
Before: simplePlanWithAgg("min", minProcedureSpecVal()),
NoChange: true,
})

// Rewrite with successors
// ReadGroup() -> count -> sum {2} => ReadGroup(count) -> sum {2}
tests = append(tests, plantest.RuleTestCase{
Expand Down
7 changes: 7 additions & 0 deletions storage/flux/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/values"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/errors"
"github.com/influxdata/influxdb/v2/models"
"github.com/influxdata/influxdb/v2/query"
Expand Down Expand Up @@ -273,6 +274,12 @@ func (gi *groupIterator) Do(f func(flux.Table) error) error {
req.Range.Start = int64(gi.spec.Bounds.Start)
req.Range.End = int64(gi.spec.Bounds.Stop)

if len(gi.spec.GroupKeys) > 0 && gi.spec.GroupMode == query.GroupModeNone {
return &influxdb.Error{
Code: influxdb.EInternal,
Msg: "cannot have group mode none with group key values",
}
}
req.Group = convertGroupMode(gi.spec.GroupMode)
req.GroupKeys = gi.spec.GroupKeys

Expand Down
Loading