-
Notifications
You must be signed in to change notification settings - Fork 153
/
chain.go
75 lines (61 loc) · 1.9 KB
/
chain.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
package experimental
import (
"context"
"fmt"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/lang"
"github.com/influxdata/flux/lang/execdeps"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
)
func init() {
runtime.RegisterPackageValue("experimental", "chain", MakeChainFunction())
}
func MakeChainFunction() values.Function {
chainSignature := runtime.MustLookupBuiltinType("experimental", "chain")
return values.NewFunction("chain", chainSignature, chainCall, false)
}
func chainCall(ctx context.Context, args values.Object) (values.Value, error) {
arguments := interpreter.NewArguments(args)
first, err := arguments.GetRequired("first")
if err != nil {
return nil, err
}
second, err := arguments.GetRequired("second")
if err != nil {
return nil, err
}
compiler := lang.TableObjectCompiler{
Tables: first.(*flux.TableObject),
}
program, err := compiler.Compile(ctx)
if err != nil {
return nil, errors.Wrap(err, codes.Inherit, "error in table object compilation")
}
if !execdeps.HaveExecutionDependencies(ctx) {
return nil, errors.New(codes.Internal, "no execution context for chain to use")
}
deps := execdeps.GetExecutionDependencies(ctx)
if program, ok := program.(lang.LoggingProgram); ok {
program.SetLogger(deps.Logger)
}
query, err := program.Start(ctx, deps.Allocator)
if err != nil {
return nil, errors.Wrap(err, codes.Inherit, "error in table object start")
}
for res := range query.Results() {
if err := res.Tables().Do(func(table flux.Table) error {
defer table.Done()
return nil
}); err != nil {
return nil, err
}
}
deps.Metadata.Add("flux/query-plan",
fmt.Sprintf("%v", plan.Formatted(program.(*lang.Program).PlanSpec, plan.WithDetails())))
return second, nil
}