-
Notifications
You must be signed in to change notification settings - Fork 67
/
job.go
196 lines (180 loc) · 4.72 KB
/
job.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
package compiler
import (
"errors"
"fmt"
"github.com/brimdata/zed/compiler/ast"
"github.com/brimdata/zed/compiler/ast/dag"
"github.com/brimdata/zed/compiler/data"
"github.com/brimdata/zed/compiler/kernel"
"github.com/brimdata/zed/compiler/optimizer"
"github.com/brimdata/zed/compiler/parser"
"github.com/brimdata/zed/compiler/semantic"
"github.com/brimdata/zed/lakeparse"
"github.com/brimdata/zed/runtime/op"
"github.com/brimdata/zed/zbuf"
)
type Job struct {
pctx *op.Context
builder *kernel.Builder
optimizer *optimizer.Optimizer
consts []dag.Op
outputs []zbuf.Puller
readers []*kernel.Reader
puller zbuf.Puller
}
func NewJob(pctx *op.Context, inAST ast.Op, src *data.Source, head *lakeparse.Commitish) (*Job, error) {
parserAST := ast.Copy(inAST)
// An AST always begins with a Sequential op with at least one
// operator. If the first op is a From or a Parallel whose branches
// are Sequentials with a leading From, then we presume there is
// no externally defined input. Otherwise, we expect two readers
// to be defined for a Join and one reader for anything else. When input
// is expected like this, we set up one or two readers inside of an
// automatically inserted From. These readers can be accessed by the
// caller via runtime.readers. In most cases, the AST is left
// with an ast.From at the entry point, and hence a dag.From for the
// DAG's entry point.
seq, ok := parserAST.(*ast.Sequential)
if !ok {
return nil, fmt.Errorf("internal error: AST must begin with a Sequential op: %T", parserAST)
}
if len(seq.Ops) == 0 {
return nil, errors.New("internal error: AST Sequential op cannot be empty")
}
var readers []*kernel.Reader
var from *ast.From
switch o := seq.Ops[0].(type) {
case *ast.From:
// Already have an entry point with From. Do nothing.
case *ast.Join:
readers = []*kernel.Reader{{}, {}}
trunk0 := ast.Trunk{
Kind: "Trunk",
Source: readers[0],
}
trunk1 := ast.Trunk{
Kind: "Trunk",
Source: readers[1],
}
from = &ast.From{
Kind: "From",
Trunks: []ast.Trunk{trunk0, trunk1},
}
default:
trunk := ast.Trunk{Kind: "Trunk"}
if head != nil {
// For the lakes, if there is no from operator, then
// we default to scanning HEAD (without any of the
// from options).
trunk.Source = &ast.Pool{
Kind: "Pool",
Spec: ast.PoolSpec{
Pool: &ast.String{
Kind: "String",
Text: "HEAD",
},
},
}
} else {
readers = []*kernel.Reader{{}}
trunk.Source = readers[0]
}
from = &ast.From{
Kind: "From",
Trunks: []ast.Trunk{trunk},
}
if isParallelWithLeadingFroms(o) {
from = nil
readers = nil
}
}
if from != nil {
seq.Prepend(from)
}
entry, err := semantic.Analyze(pctx.Context, seq, src, head)
if err != nil {
return nil, err
}
return &Job{
pctx: pctx,
builder: kernel.NewBuilder(pctx, src),
optimizer: optimizer.New(pctx.Context, entry, src),
readers: readers,
}, nil
}
func isParallelWithLeadingFroms(o ast.Op) bool {
par, ok := o.(*ast.Parallel)
if !ok {
return false
}
for _, o := range par.Ops {
if !isSequentialWithLeadingFrom(o) {
return false
}
}
return true
}
func isSequentialWithLeadingFrom(o ast.Op) bool {
seq, ok := o.(*ast.Sequential)
if !ok && len(seq.Ops) == 0 {
return false
}
_, ok = seq.Ops[0].(*ast.From)
return ok
}
func (j *Job) Entry() dag.Op {
//XXX need to prepend consts depending on context
return j.optimizer.Entry()
}
// This must be called before the zbuf.Filter interface will work.
func (j *Job) Optimize() error {
return j.optimizer.OptimizeScan()
}
func (j *Job) Parallelize(n int) error {
return j.optimizer.Parallelize(n)
}
func Parse(src string, filenames ...string) (ast.Op, error) {
parsed, err := parser.ParseZed(filenames, src)
if err != nil {
return nil, err
}
return ast.UnpackMapAsOp(parsed)
}
// MustParse is like Parse but panics if an error is encountered.
func MustParse(query string) ast.Op {
o, err := (*anyCompiler)(nil).Parse(query)
if err != nil {
panic(err)
}
return o
}
func (j *Job) Builder() *kernel.Builder {
return j.builder
}
func (j *Job) Build() error {
outputs, err := j.builder.Build(j.optimizer.Entry())
if err != nil {
return err
}
j.outputs = outputs
return nil
}
func (j *Job) Puller() zbuf.Puller {
if j.puller == nil {
switch outputs := j.outputs; len(outputs) {
case 0:
return nil
case 1:
j.puller = op.NewCatcher(op.NewSingle(outputs[0]))
default:
j.puller = op.NewMux(j.pctx, outputs)
}
}
return j.puller
}
type anyCompiler struct{}
// Parse concatenates the source files in filenames followed by src and parses
// the resulting program.
func (*anyCompiler) Parse(src string, filenames ...string) (ast.Op, error) {
return Parse(src, filenames...)
}