-
Notifications
You must be signed in to change notification settings - Fork 153
/
group.go
247 lines (206 loc) · 5.79 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package universe
import (
"errors"
"fmt"
"sort"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/semantic"
)
const GroupKind = "group"
const (
groupModeBy = "by"
groupModeExcept = "except"
)
type GroupOpSpec struct {
Mode string `json:"mode"`
Columns []string `json:"columns"`
}
func init() {
groupSignature := flux.FunctionSignature(
map[string]semantic.PolyType{
"mode": semantic.String,
"columns": semantic.NewArrayPolyType(semantic.String),
},
nil,
)
flux.RegisterPackageValue("universe", GroupKind, flux.FunctionValue(GroupKind, createGroupOpSpec, groupSignature))
flux.RegisterOpSpec(GroupKind, newGroupOp)
plan.RegisterProcedureSpec(GroupKind, newGroupProcedure, GroupKind)
plan.RegisterLogicalRules(MergeGroupRule{})
execute.RegisterTransformation(GroupKind, createGroupTransformation)
}
func createGroupOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(GroupOpSpec)
if mode, ok, err := args.GetString("mode"); err != nil {
return nil, err
} else if ok {
if _, err := validateGroupMode(mode); err != nil {
return nil, err
}
spec.Mode = mode
} else {
spec.Mode = groupModeBy
}
if columns, ok, err := args.GetArray("columns", semantic.String); err != nil {
return nil, err
} else if ok {
spec.Columns, err = interpreter.ToStringArray(columns)
if err != nil {
return nil, err
}
} else {
spec.Columns = []string{}
}
return spec, nil
}
func validateGroupMode(mode string) (flux.GroupMode, error) {
switch mode {
case groupModeBy:
return flux.GroupModeBy, nil
case groupModeExcept:
return flux.GroupModeExcept, nil
default:
return flux.GroupModeNone, errors.New(`invalid group mode: must be "by" or "except"`)
}
}
func newGroupOp() flux.OperationSpec {
return new(GroupOpSpec)
}
func (s *GroupOpSpec) Kind() flux.OperationKind {
return GroupKind
}
type GroupProcedureSpec struct {
plan.DefaultCost
GroupMode flux.GroupMode
GroupKeys []string
}
func newGroupProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*GroupOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
mode, err := validateGroupMode(spec.Mode)
if err != nil {
return nil, err
}
p := &GroupProcedureSpec{
GroupMode: mode,
GroupKeys: spec.Columns,
}
return p, nil
}
func (s *GroupProcedureSpec) Kind() plan.ProcedureKind {
return GroupKind
}
func (s *GroupProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(GroupProcedureSpec)
ns.GroupMode = s.GroupMode
ns.GroupKeys = make([]string, len(s.GroupKeys))
copy(ns.GroupKeys, s.GroupKeys)
return ns
}
func createGroupTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*GroupProcedureSpec)
if !ok {
return nil, nil, fmt.Errorf("invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t := NewGroupTransformation(d, cache, s)
return t, d, nil
}
type groupTransformation struct {
d execute.Dataset
cache execute.TableBuilderCache
mode flux.GroupMode
keys []string
}
func NewGroupTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *GroupProcedureSpec) *groupTransformation {
t := &groupTransformation{
d: d,
cache: cache,
mode: spec.GroupMode,
keys: spec.GroupKeys,
}
sort.Strings(t.keys)
return t
}
func (t *groupTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) (err error) {
panic("not implemented")
}
func (t *groupTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
cols := tbl.Cols()
on := make(map[string]bool, len(cols))
switch t.mode {
case flux.GroupModeBy:
for _, k := range t.keys {
on[k] = true
}
case flux.GroupModeExcept:
COLS:
for _, c := range cols {
for _, label := range t.keys {
if c.Label == label {
continue COLS
}
}
on[c.Label] = true
}
default:
panic("unimplemented group mode")
}
colMap := make([]int, 0, len(tbl.Cols()))
return tbl.Do(func(cr flux.ColReader) error {
l := cr.Len()
for i := 0; i < l; i++ {
key := execute.GroupKeyForRowOn(i, cr, on)
builder, _ := t.cache.TableBuilder(key)
colMap, err := execute.AddNewTableCols(tbl, builder, colMap)
if err != nil {
return err
}
err = execute.AppendMappedRecordWithNulls(i, cr, builder, colMap)
if err != nil {
return err
}
}
return nil
})
}
func (t *groupTransformation) UpdateWatermark(id execute.DatasetID, mark execute.Time) error {
return t.d.UpdateWatermark(mark)
}
func (t *groupTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *groupTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}
// `MergeGroupRule` merges two group operations and keeps only the last one
type MergeGroupRule struct{}
func (r MergeGroupRule) Name() string {
return "MergeGroupRule"
}
// returns the pattern that matches `group |> group`
func (r MergeGroupRule) Pattern() plan.Pattern {
return plan.Pat(GroupKind, plan.Pat(GroupKind, plan.Any()))
}
func (r MergeGroupRule) Rewrite(lastGroup plan.Node) (plan.Node, bool, error) {
firstGroup := lastGroup.Predecessors()[0]
lastSpec := lastGroup.ProcedureSpec().(*GroupProcedureSpec)
if lastSpec.GroupMode != flux.GroupModeBy &&
lastSpec.GroupMode != flux.GroupModeExcept {
return lastGroup, false, nil
}
merged, err := plan.MergeToLogicalNode(lastGroup, firstGroup, lastSpec.Copy())
if err != nil {
return nil, false, err
}
return merged, true, nil
}