-
Notifications
You must be signed in to change notification settings - Fork 153
/
compile.go
401 lines (366 loc) · 11 KB
/
compile.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package flux
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
const (
TablesParameter = "tables"
tableKindKey = "kind"
tableParentsKey = "parents"
tableSpecKey = "spec"
)
type CreateOperationSpec func(args Arguments, a *Administration) (OperationSpec, error)
// MustValue panics if err is not nil, otherwise value is returned.
func MustValue(v values.Value, err error) values.Value {
if err != nil {
panic(err)
}
return v
}
// FunctionValue creates a values.Value from the operation spec and signature.
// Name is the name of the function as it would be called.
// c is a function reference of type CreateOperationSpec
// sig is a function signature type that specifies the names and types of each argument for the function.
func FunctionValue(name string, c CreateOperationSpec, ft semantic.MonoType) (values.Value, error) {
return functionValue(name, c, ft, false)
}
// FunctionValueWithSideEffect creates a values.Value from the operation spec and signature.
// Name is the name of the function as it would be called.
// c is a function reference of type CreateOperationSpec
// sig is a function signature type that specifies the names and types of each argument for the function.
func FunctionValueWithSideEffect(name string, c CreateOperationSpec, ft semantic.MonoType) (values.Value, error) {
return functionValue(name, c, ft, true)
}
func functionValue(name string, c CreateOperationSpec, mt semantic.MonoType, sideEffects bool) (values.Value, error) {
if c == nil {
c = func(args Arguments, a *Administration) (OperationSpec, error) {
return nil, errors.Newf(codes.Unimplemented, "function %q is not implemented", name)
}
}
if mt.Nature() != semantic.Function {
return nil, errors.Newf(codes.Invalid, "cannot implement function %q with value of type %v", name, mt)
}
return &function{
t: mt,
name: name,
createOpSpec: c,
hasSideEffect: sideEffects,
}, nil
}
var _ = tableSpecKey // So that linter doesn't think tableSpecKey is unused, considering above TODO.
// TableObject represents the value returned by a transformation.
// As such, it holds the OperationSpec of the transformation it is associated with,
// and it is a values.Value (and, also, a values.Object).
// It can be compiled and executed as a flux.Program by using a lang.TableObjectCompiler.
type TableObject struct {
// TODO(Josh): Remove args once the
// OperationSpec interface has an Equal method.
t semantic.MonoType
args Arguments
Kind OperationKind
Spec OperationSpec
Source struct {
Stack []interpreter.StackEntry
}
Parents []*TableObject
// Owned is set to true when this TableObject has
// ownership claimed by an operation spec.
// A TableObject should only be owned by one spec.
// TableObjects are initially created unowned.
Owned bool
}
func (t *TableObject) Dynamic() values.Dynamic {
panic(values.UnexpectedKind(t.Type().Nature(), semantic.Dynamic))
}
func (t *TableObject) IsNull() bool {
return false
}
func (t *TableObject) String() string {
str := new(strings.Builder)
t.str(str, false)
return str.String()
}
func (t *TableObject) str(b *strings.Builder, arrow bool) {
multiParent := len(t.Parents) > 1
if multiParent {
b.WriteString("( ")
}
for _, v := range t.Parents {
v.str(b, !multiParent)
if multiParent {
b.WriteString("; ")
}
}
if multiParent {
b.WriteString(" ) -> ")
}
b.WriteString(string(t.Kind))
if arrow {
b.WriteString(" -> ")
}
}
func (t *TableObject) Type() semantic.MonoType {
return t.t
}
func (t *TableObject) Str() string {
panic(values.UnexpectedKind(semantic.Array, semantic.String))
}
func (t *TableObject) Bytes() []byte {
panic(values.UnexpectedKind(semantic.Array, semantic.Bytes))
}
func (t *TableObject) Int() int64 {
panic(values.UnexpectedKind(semantic.Array, semantic.Int))
}
func (t *TableObject) UInt() uint64 {
panic(values.UnexpectedKind(semantic.Array, semantic.UInt))
}
func (t *TableObject) Float() float64 {
panic(values.UnexpectedKind(semantic.Array, semantic.Float))
}
func (t *TableObject) Bool() bool {
panic(values.UnexpectedKind(semantic.Array, semantic.Bool))
}
func (t *TableObject) Time() values.Time {
panic(values.UnexpectedKind(semantic.Array, semantic.Time))
}
func (t *TableObject) Duration() values.Duration {
panic(values.UnexpectedKind(semantic.Array, semantic.Duration))
}
func (t *TableObject) Regexp() *regexp.Regexp {
panic(values.UnexpectedKind(semantic.Array, semantic.Regexp))
}
func (t *TableObject) Array() values.Array {
return t
}
func (t *TableObject) Object() values.Object {
panic(values.UnexpectedKind(semantic.Array, semantic.Object))
}
func (t *TableObject) Equal(rhs values.Value) bool {
v, ok := rhs.(*TableObject)
return ok && t == v
}
func (t *TableObject) Retain() {}
func (t *TableObject) Release() {}
func (t *TableObject) Function() values.Function {
panic(values.UnexpectedKind(semantic.Array, semantic.Function))
}
func (t *TableObject) Dict() values.Dictionary {
panic(values.UnexpectedKind(semantic.Array, semantic.Dictionary))
}
func (t *TableObject) Vector() values.Vector {
panic(values.UnexpectedKind(semantic.Array, semantic.Vector))
}
func (t *TableObject) Get(i int) values.Value {
panic("cannot index into stream")
}
func (t *TableObject) Set(i int, v values.Value) {
panic("cannot index into stream")
}
func (t *TableObject) Append(v values.Value) {
panic("cannot append onto stream")
}
func (t *TableObject) Len() int {
panic("length of stream not supported")
}
func (t *TableObject) Range(f func(i int, v values.Value)) {
panic("cannot range over values in stream")
}
func (t *TableObject) Sort(f func(i, j values.Value) bool) {
panic("cannot sort stream")
}
type Administration struct {
parents []*TableObject
}
func newAdministration() *Administration {
return &Administration{
parents: make([]*TableObject, 0, 8),
}
}
// AddParentFromArgs reads the args for the `table` argument and adds the value as a parent.
func (a *Administration) AddParentFromArgs(args Arguments) error {
parent, ok := args.Get(TablesParameter)
if !ok {
return errors.Newf(codes.Invalid, "could not find %s parameter", TablesParameter)
}
p, ok := parent.(*TableObject)
if !ok {
return errors.Newf(codes.Invalid, "argument is not a table object: got %T", parent)
}
a.AddParent(p)
return nil
}
// AddParent instructs the evaluation Context that a new edge should be created from the parent to the current operation.
// Duplicate parents will be removed, so the caller need not concern itself with which parents have already been added.
func (a *Administration) AddParent(np *TableObject) {
// Check for duplicates
for _, v := range a.parents {
if v == np {
return
}
}
a.parents = append(a.parents, np)
}
type function struct {
name string
t semantic.MonoType
createOpSpec CreateOperationSpec
hasSideEffect bool
}
func (f *function) Dynamic() values.Dynamic {
panic(values.UnexpectedKind(semantic.Function, semantic.Dynamic))
}
func (f *function) Type() semantic.MonoType {
return f.t
}
func (f *function) IsNull() bool {
return false
}
func (f *function) Str() string {
panic(values.UnexpectedKind(semantic.Function, semantic.String))
}
func (f *function) Bytes() []byte {
panic(values.UnexpectedKind(semantic.Function, semantic.Bytes))
}
func (f *function) Int() int64 {
panic(values.UnexpectedKind(semantic.Function, semantic.Int))
}
func (f *function) UInt() uint64 {
panic(values.UnexpectedKind(semantic.Function, semantic.UInt))
}
func (f *function) Float() float64 {
panic(values.UnexpectedKind(semantic.Function, semantic.Float))
}
func (f *function) Bool() bool {
panic(values.UnexpectedKind(semantic.Function, semantic.Bool))
}
func (f *function) Time() values.Time {
panic(values.UnexpectedKind(semantic.Function, semantic.Time))
}
func (f *function) Duration() values.Duration {
panic(values.UnexpectedKind(semantic.Function, semantic.Duration))
}
func (f *function) Regexp() *regexp.Regexp {
panic(values.UnexpectedKind(semantic.Function, semantic.Regexp))
}
func (f *function) Array() values.Array {
panic(values.UnexpectedKind(semantic.Function, semantic.Array))
}
func (f *function) Object() values.Object {
panic(values.UnexpectedKind(semantic.Function, semantic.Object))
}
func (f *function) Function() values.Function {
return f
}
func (f *function) Dict() values.Dictionary {
panic(values.UnexpectedKind(semantic.Function, semantic.Dictionary))
}
func (f *function) Vector() values.Vector {
panic(values.UnexpectedKind(semantic.Function, semantic.Vector))
}
func (f *function) Equal(rhs values.Value) bool {
if f.Type() != rhs.Type() {
return false
}
v, ok := rhs.(*function)
return ok && (f == v)
}
func (f *function) Retain() {}
func (f *function) Release() {}
func (f *function) HasSideEffect() bool {
return f.hasSideEffect
}
func (f *function) Call(ctx context.Context, args values.Object) (values.Value, error) {
return interpreter.DoFunctionCallContext(f.call, ctx, args)
}
func (f *function) call(ctx context.Context, args interpreter.Arguments) (values.Value, error) {
returnType, err := f.t.ReturnType()
if err != nil {
return nil, err
}
a := newAdministration()
arguments := Arguments{Arguments: args}
spec, err := f.createOpSpec(arguments, a)
if err != nil {
return nil, err
}
t := &TableObject{
t: returnType,
args: arguments,
Kind: spec.Kind(),
Spec: spec,
Parents: a.parents,
}
t.Source.Stack = interpreter.Stack(ctx)
return t, nil
}
func (f *function) String() string {
return fmt.Sprintf("%v", f.t)
}
type Arguments struct {
interpreter.Arguments
}
func (a Arguments) GetTime(name string) (Time, bool, error) {
v, ok := a.Get(name)
if !ok {
return Time{}, false, nil
}
qt, err := ToQueryTime(v)
if err != nil {
return Time{}, ok, err
}
return qt, ok, nil
}
func (a Arguments) GetRequiredTime(name string) (Time, error) {
qt, ok, err := a.GetTime(name)
if err != nil {
return Time{}, err
}
if !ok {
return Time{}, errors.Newf(codes.Invalid, "missing required keyword argument %q", name)
}
return qt, nil
}
func (a Arguments) GetDuration(name string) (Duration, bool, error) {
v, ok := a.Get(name)
if !ok {
return ConvertDuration(0), false, nil
}
return v.Duration(), true, nil
}
func (a Arguments) GetRequiredDuration(name string) (Duration, error) {
d, ok, err := a.GetDuration(name)
if err != nil {
return ConvertDuration(0), err
}
if !ok {
return ConvertDuration(0), errors.Newf(codes.Invalid, "missing required keyword argument %q", name)
}
return d, nil
}
func ToQueryTime(value values.Value) (Time, error) {
switch value.Type().Nature() {
case semantic.Time:
return Time{
Absolute: value.Time().Time(),
}, nil
case semantic.Duration:
return Time{
Relative: value.Duration().Duration(),
IsRelative: true,
}, nil
case semantic.Int:
return Time{
Absolute: time.Unix(value.Int(), 0),
}, nil
default:
return Time{}, errors.Newf(codes.Invalid, "value is not a time, got %v", value.Type())
}
}