-
Notifications
You must be signed in to change notification settings - Fork 153
/
filter.go
205 lines (175 loc) · 5.36 KB
/
filter.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
package universe
import (
"fmt"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/semantic"
"github.com/pkg/errors"
)
const FilterKind = "filter"
type FilterOpSpec struct {
Fn *semantic.FunctionExpression `json:"fn"`
}
func init() {
filterSignature := flux.FunctionSignature(
map[string]semantic.PolyType{
"fn": semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{
"r": semantic.Tvar(1),
},
Required: semantic.LabelSet{"r"},
Return: semantic.Bool,
}),
},
[]string{"fn"},
)
flux.RegisterPackageValue("universe", FilterKind, flux.FunctionValue(FilterKind, createFilterOpSpec, filterSignature))
flux.RegisterOpSpec(FilterKind, newFilterOp)
plan.RegisterProcedureSpec(FilterKind, newFilterProcedure, FilterKind)
execute.RegisterTransformation(FilterKind, createFilterTransformation)
plan.RegisterPhysicalRules(
RemoveTrivialFilterRule{},
)
}
func createFilterOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
f, err := args.GetRequiredFunction("fn")
if err != nil {
return nil, err
}
fn, err := interpreter.ResolveFunction(f)
if err != nil {
return nil, err
}
return &FilterOpSpec{
Fn: fn,
}, nil
}
func newFilterOp() flux.OperationSpec {
return new(FilterOpSpec)
}
func (s *FilterOpSpec) Kind() flux.OperationKind {
return FilterKind
}
type FilterProcedureSpec struct {
plan.DefaultCost
Fn *semantic.FunctionExpression
}
func newFilterProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*FilterOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
return &FilterProcedureSpec{
Fn: spec.Fn,
}, nil
}
func (s *FilterProcedureSpec) Kind() plan.ProcedureKind {
return FilterKind
}
func (s *FilterProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(FilterProcedureSpec)
ns.Fn = s.Fn.Copy().(*semantic.FunctionExpression)
return ns
}
// TriggerSpec implements plan.TriggerAwareProcedureSpec
func (s *FilterProcedureSpec) TriggerSpec() plan.TriggerSpec {
return plan.NarrowTransformationTriggerSpec{}
}
func createFilterTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*FilterProcedureSpec)
if !ok {
return nil, nil, fmt.Errorf("invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t, err := NewFilterTransformation(d, cache, s)
if err != nil {
return nil, nil, err
}
return t, d, nil
}
type filterTransformation struct {
d execute.Dataset
cache execute.TableBuilderCache
fn *execute.RowPredicateFn
}
func NewFilterTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *FilterProcedureSpec) (*filterTransformation, error) {
fn, err := execute.NewRowPredicateFn(spec.Fn)
if err != nil {
return nil, err
}
return &filterTransformation{
d: d,
cache: cache,
fn: fn,
}, nil
}
func (t *filterTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
func (t *filterTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
builder, created := t.cache.TableBuilder(tbl.Key())
if !created {
return fmt.Errorf("filter found duplicate table with key: %v", tbl.Key())
}
if err := execute.AddTableCols(tbl, builder); err != nil {
return err
}
// Prepare the function for the column types.
cols := tbl.Cols()
if err := t.fn.Prepare(cols); err != nil {
// TODO(nathanielc): Should we not fail the query for failed compilation?
return err
}
// Append only matching rows to table
return tbl.Do(func(cr flux.ColReader) error {
l := cr.Len()
for i := 0; i < l; i++ {
if pass, err := t.fn.Eval(i, cr); err != nil {
return errors.Wrap(err, "failed to evaluate filter function")
} else if !pass {
// No match, skipping
continue
}
if err := execute.AppendRecord(i, cr, builder); err != nil {
return err
}
}
return nil
})
}
func (t *filterTransformation) UpdateWatermark(id execute.DatasetID, mark execute.Time) error {
return t.d.UpdateWatermark(mark)
}
func (t *filterTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *filterTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}
// RemoveTrivialFilterRule removes Filter nodes whose predicate always evaluates to true.
type RemoveTrivialFilterRule struct{}
func (RemoveTrivialFilterRule) Name() string {
return "MergeFilterAnyRule"
}
func (RemoveTrivialFilterRule) Pattern() plan.Pattern {
return plan.Pat(FilterKind, plan.Any())
}
func (RemoveTrivialFilterRule) Rewrite(filterNode plan.Node) (plan.Node, bool, error) {
filterSpec := filterNode.ProcedureSpec().(*FilterProcedureSpec)
if filterSpec.Fn == nil ||
filterSpec.Fn.Block == nil ||
filterSpec.Fn.Block.Body == nil {
return filterNode, false, nil
}
if boolean, ok := filterSpec.Fn.Block.Body.(*semantic.BooleanLiteral); !ok || !boolean.Value {
return filterNode, false, nil
}
anyNode := filterNode.Predecessors()[0]
return anyNode, true, nil
}