-
Notifications
You must be signed in to change notification settings - Fork 152
/
union.go
201 lines (156 loc) · 4.65 KB
/
union.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
package universe
import (
"math"
"sync"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
const UnionKind = "union"
type UnionOpSpec struct {
}
func (s *UnionOpSpec) Kind() flux.OperationKind {
return UnionKind
}
func init() {
unionSignature := semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{
"tables": semantic.NewArrayPolyType(flux.TableObjectType),
},
Required: semantic.LabelSet{"tables"},
Return: flux.TableObjectType,
}
flux.RegisterPackageValue("universe", UnionKind, flux.FunctionValue(UnionKind, createUnionOpSpec, unionSignature))
flux.RegisterOpSpec(UnionKind, newUnionOp)
plan.RegisterProcedureSpec(UnionKind, newUnionProcedure, UnionKind)
execute.RegisterTransformation(UnionKind, createUnionTransformation)
}
func createUnionOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
tables, err := args.GetRequiredArray("tables", semantic.Object)
if err != nil {
return nil, err
}
if tables.Len() < 2 {
return nil, errors.New(codes.Invalid, "union must have at least two streams as input")
}
err = nil
tables.Range(func(i int, parent values.Value) {
p, ok := parent.(*flux.TableObject)
if !ok {
err = errors.New(codes.Invalid, "input to union is not a table object")
}
a.AddParent(p)
})
if err != nil {
return nil, err
}
return &UnionOpSpec{}, nil
}
func newUnionOp() flux.OperationSpec {
return new(UnionOpSpec)
}
type UnionProcedureSpec struct {
plan.DefaultCost
}
func (s *UnionProcedureSpec) Kind() plan.ProcedureKind {
return UnionKind
}
func (s *UnionProcedureSpec) Copy() plan.ProcedureSpec {
return &UnionProcedureSpec{}
}
func newUnionProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
return &UnionProcedureSpec{}, nil
}
type unionTransformation struct {
mu sync.Mutex
parentState map[execute.DatasetID]*unionParentState
d execute.Dataset
cache execute.TableBuilderCache
}
type unionParentState struct {
mark execute.Time
processing execute.Time
finished bool
}
func createUnionTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*UnionProcedureSpec)
if !ok {
return nil, nil, errors.Newf(codes.Invalid, "invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
dataset := execute.NewDataset(id, mode, cache)
transform := NewUnionTransformation(dataset, cache, s, a.Parents())
return transform, dataset, nil
}
func NewUnionTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *UnionProcedureSpec, parents []execute.DatasetID) *unionTransformation {
parentState := make(map[execute.DatasetID]*unionParentState, len(parents))
for _, id := range parents {
parentState[id] = new(unionParentState)
}
return &unionTransformation{
parentState: parentState,
d: d,
cache: cache,
}
}
func (t *unionTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
panic("not implemented")
}
func (t *unionTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
t.mu.Lock()
defer t.mu.Unlock()
var colMap = make([]int, 0, len(tbl.Cols()))
var err error
builder, _ := t.cache.TableBuilder(tbl.Key())
colMap, err = execute.AddNewTableCols(tbl, builder, colMap)
if err != nil {
return err
}
if err := execute.AppendMappedTable(tbl, builder, colMap); err != nil {
return err
}
return nil
}
func (t *unionTransformation) UpdateWatermark(id execute.DatasetID, mark execute.Time) error {
t.mu.Lock()
defer t.mu.Unlock()
t.parentState[id].mark = mark
min := execute.Time(math.MaxInt64)
for _, state := range t.parentState {
if state.mark < min {
min = state.mark
}
}
return t.d.UpdateWatermark(min)
}
func (t *unionTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
t.mu.Lock()
defer t.mu.Unlock()
t.parentState[id].processing = pt
min := execute.Time(math.MaxInt64)
for _, state := range t.parentState {
if state.processing < min {
min = state.processing
}
}
return t.d.UpdateProcessingTime(min)
}
func (t *unionTransformation) Finish(id execute.DatasetID, err error) {
t.mu.Lock()
defer t.mu.Unlock()
t.parentState[id].finished = true
if err != nil {
t.d.Finish(err)
}
finished := true
for _, state := range t.parentState {
finished = finished && state.finished
}
if finished {
t.d.Finish(nil)
}
}