-
Notifications
You must be signed in to change notification settings - Fork 153
/
compiler.go
312 lines (259 loc) · 7.61 KB
/
compiler.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
package lang
import (
"context"
"log"
"time"
"github.com/influxdata/flux"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/spec"
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/plan"
"github.com/pkg/errors"
"go.uber.org/zap"
)
const (
FluxCompilerType = "flux"
ASTCompilerType = "ast"
)
// AddCompilerMappings adds the Flux specific compiler mappings.
func AddCompilerMappings(mappings flux.CompilerMappings) error {
if err := mappings.Add(FluxCompilerType, func() flux.Compiler {
return new(FluxCompiler)
}); err != nil {
return err
}
if err := mappings.Add(ASTCompilerType, func() flux.Compiler {
return new(ASTCompiler)
}); err != nil {
return err
}
return nil
}
// CompileOption represents an option for compilation.
type CompileOption func(*compileOptions)
type compileOptions struct {
verbose bool
planOptions struct {
logical []plan.LogicalOption
physical []plan.PhysicalOption
}
}
func WithLogPlanOpts(lopts ...plan.LogicalOption) CompileOption {
return func(o *compileOptions) {
o.planOptions.logical = append(o.planOptions.logical, lopts...)
}
}
func WithPhysPlanOpts(popts ...plan.PhysicalOption) CompileOption {
return func(o *compileOptions) {
o.planOptions.physical = append(o.planOptions.physical, popts...)
}
}
func defaultOptions() *compileOptions {
o := new(compileOptions)
return o
}
func applyOptions(opts ...CompileOption) *compileOptions {
o := defaultOptions()
for _, opt := range opts {
opt(o)
}
return o
}
// NOTE: compileOptions can be used only when invoking Compile* functions.
// They can't be used when unmarshaling a Compiler and invoking its Compile method.
func Verbose(v bool) CompileOption {
return func(o *compileOptions) {
o.verbose = v
}
}
// Compile evaluates a Flux script producing a flux.Program.
// now parameter must be non-zero, that is the default now time should be set before compiling.
func Compile(q string, now time.Time, opts ...CompileOption) (*AstProgram, error) {
astPkg, err := flux.Parse(q)
if err != nil {
return nil, err
}
return CompileAST(astPkg, now, opts...), nil
}
// CompileAST evaluates a Flux AST and produces a flux.Program.
// now parameter must be non-zero, that is the default now time should be set before compiling.
func CompileAST(astPkg *ast.Package, now time.Time, opts ...CompileOption) *AstProgram {
return &AstProgram{
Program: &Program{
opts: applyOptions(opts...),
},
Ast: astPkg,
Now: now,
}
}
// CompileTableObject evaluates a TableObject and produces a flux.Program.
// now parameter must be non-zero, that is the default now time should be set before compiling.
func CompileTableObject(to *flux.TableObject, now time.Time, opts ...CompileOption) (*Program, error) {
o := applyOptions(opts...)
s := spec.FromTableObject(to, now)
if o.verbose {
log.Println("Query Spec: ", flux.Formatted(s, flux.FmtJSON))
}
ps, err := buildPlan(s, o)
if err != nil {
return nil, err
}
return &Program{
opts: o,
PlanSpec: ps,
}, nil
}
func WalkIR(astPkg *ast.Package, f func(o *flux.Operation) error) error {
if spec, err := spec.FromAST(context.Background(), astPkg, time.Now()); err != nil {
return err
} else {
return spec.Walk(f)
}
}
func buildPlan(spec *flux.Spec, opts *compileOptions) (*plan.Spec, error) {
pb := plan.PlannerBuilder{}
planOptions := opts.planOptions
lopts := planOptions.logical
popts := planOptions.physical
pb.AddLogicalOptions(lopts...)
pb.AddPhysicalOptions(popts...)
ps, err := pb.Build().Plan(spec)
if err != nil {
return nil, err
}
return ps, nil
}
// FluxCompiler compiles a Flux script into a spec.
type FluxCompiler struct {
Query string `json:"query"`
}
func (c FluxCompiler) Compile(ctx context.Context) (flux.Program, error) {
// Ignore context, it will be provided upon Program Start.
return Compile(c.Query, time.Now())
}
func (c FluxCompiler) CompilerType() flux.CompilerType {
return FluxCompilerType
}
// ASTCompiler implements Compiler by producing a Spec from an AST.
type ASTCompiler struct {
AST *ast.Package `json:"ast"`
Now time.Time
}
func (c ASTCompiler) Compile(ctx context.Context) (flux.Program, error) {
now := c.Now
if now.IsZero() {
now = time.Now()
}
// Ignore context, it will be provided upon Program Start.
return CompileAST(c.AST, now), nil
}
func (ASTCompiler) CompilerType() flux.CompilerType {
return ASTCompilerType
}
// PrependFile prepends a file onto the compiler's list of package files.
func (c *ASTCompiler) PrependFile(file *ast.File) {
c.AST.Files = append([]*ast.File{file}, c.AST.Files...)
}
// TableObjectCompiler compiles a TableObject into an executable flux.Program.
// It is not added to CompilerMappings and it is not serializable, because
// it is impossible to use it outside of the context of an ongoing execution.
type TableObjectCompiler struct {
Tables *flux.TableObject
Now time.Time
}
func (c *TableObjectCompiler) Compile(ctx context.Context) (flux.Program, error) {
// Ignore context, it will be provided upon Program Start.
return CompileTableObject(c.Tables, c.Now)
}
func (*TableObjectCompiler) CompilerType() flux.CompilerType {
panic("TableObjectCompiler is not associated with a CompilerType")
}
type DependenciesAwareProgram interface {
SetExecutorDependencies(execute.Dependencies)
SetLogger(logger *zap.Logger)
}
// Program implements the flux.Program interface.
// It will execute a compiled plan using an executor.
type Program struct {
Dependencies execute.Dependencies
Logger *zap.Logger
PlanSpec *plan.Spec
opts *compileOptions
}
func (p *Program) SetExecutorDependencies(deps execute.Dependencies) {
p.Dependencies = deps
}
func (p *Program) SetLogger(logger *zap.Logger) {
p.Logger = logger
}
func (p *Program) Start(ctx context.Context, alloc *memory.Allocator) (flux.Query, error) {
ctx, cancel := context.WithCancel(ctx)
results := make(chan flux.Result)
q := &query{
results: results,
alloc: alloc,
cancel: cancel,
stats: flux.Statistics{
Metadata: make(flux.Metadata),
},
}
e := execute.NewExecutor(p.Dependencies, p.Logger)
resultMap, md, err := e.Execute(ctx, p.PlanSpec, q.alloc)
if err != nil {
return nil, err
}
// There was no error so send the results downstream.
q.wg.Add(1)
go p.processResults(ctx, q, resultMap)
// Begin reading from the metadata channel.
q.wg.Add(1)
go p.readMetadata(q, md)
return q, nil
}
func (p *Program) processResults(ctx context.Context, q *query, resultMap map[string]flux.Result) {
defer q.wg.Done()
defer close(q.results)
for _, res := range resultMap {
select {
case q.results <- res:
case <-ctx.Done():
q.err = ctx.Err()
return
}
}
}
func (p *Program) readMetadata(q *query, metaCh <-chan flux.Metadata) {
defer q.wg.Done()
for md := range metaCh {
q.stats.Metadata.AddAll(md)
}
}
// AstProgram wraps a Program with an AST that will be evaluated upon Start.
// As such, the PlanSpec is populated after Start and evaluation errors are returned by Start.
type AstProgram struct {
*Program
Ast *ast.Package
Now time.Time
}
func (p *AstProgram) Start(ctx context.Context, alloc *memory.Allocator) (flux.Query, error) {
if p.opts == nil {
p.opts = defaultOptions()
}
if p.Now.IsZero() {
p.Now = time.Now()
}
s, err := spec.FromAST(ctx, p.Ast, p.Now)
if err != nil {
return nil, errors.Wrap(err, "error in evaluating AST while starting program")
}
if p.opts.verbose {
log.Println("Query Spec: ", flux.Formatted(s, flux.FmtJSON))
}
ps, err := buildPlan(s, p.opts)
if err != nil {
return nil, errors.Wrap(err, "error in building plan while starting program")
}
p.PlanSpec = ps
return p.Program.Start(ctx, alloc)
}