/
pipeline.go
482 lines (403 loc) · 12.3 KB
/
pipeline.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package libsq
import (
"context"
"database/sql"
"fmt"
"github.com/samber/lo"
"golang.org/x/sync/errgroup"
"github.com/neilotoole/sq/libsq/ast"
"github.com/neilotoole/sq/libsq/ast/render"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/lg"
"github.com/neilotoole/sq/libsq/core/lg/lga"
"github.com/neilotoole/sq/libsq/core/lg/lgm"
"github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/tablefq"
"github.com/neilotoole/sq/libsq/core/tuning"
"github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/libsq/source"
)
// pipeline is used to execute a SLQ query,
// and write the resulting records to a RecordWriter.
type pipeline struct {
// targetGrip is the destination for the ultimate SQL query to
// be executed against.
targetGrip driver.Grip
// qc is the context in which the query is executed.
qc *QueryContext
// rc is the Context for rendering SQL.
// This field is set during pipeline.prepare. It can't be set before
// then because the target DB to use is calculated during pipeline.prepare,
// based on the input query and other context.
rc *render.Context
// query is the SLQ query
query string
// targetSQL is the ultimate SQL query to be executed against targetGrip.
targetSQL string
// tasks contains tasks that must be completed before targetSQL
// is executed against targetGrip. Typically tasks is used to
// set up the joindb before it is queried.
tasks []tasker
}
// newPipeline parses query, returning a pipeline prepared for
// execution via pipeline.execute.
func newPipeline(ctx context.Context, qc *QueryContext, query string) (*pipeline, error) {
log := lg.FromContext(ctx)
a, err := ast.Parse(log, query)
if err != nil {
return nil, err
}
qModel, err := buildQueryModel(qc, a)
if err != nil {
return nil, err
}
p := &pipeline{
qc: qc,
query: query,
}
if err = p.prepare(ctx, qModel); err != nil {
return nil, err
}
return p, nil
}
// execute executes the pipeline, writing results to recw.
func (p *pipeline) execute(ctx context.Context, recw RecordWriter) error {
log := lg.FromContext(ctx)
log.Info("Execute SQL query", lga.Src, p.targetGrip.Source(), lga.SQL, p.targetSQL)
errw := p.targetGrip.SQLDriver().ErrWrapFunc()
// TODO: The tasks might like to be executed in parallel. However,
// what happens if a task does something that is session/connection-dependent?
// When the query executes later (below), it could be on a different
// connection. Maybe the tasks need a means of declaring that they
// must be executed on the same connection as the main query?
if err := p.executeTasks(ctx); err != nil {
return errw(err)
}
var conn sqlz.DB
if len(p.qc.PreExecStmts) > 0 || len(p.qc.PostExecStmts) > 0 {
// If there's pre/post exec work to do, we need to
// obtain a connection from the pool. We are responsible
// for closing these resources.
db, err := p.targetGrip.DB(ctx)
if err != nil {
return errw(err)
}
defer lg.WarnIfCloseError(log, lgm.CloseDB, db)
if conn, err = db.Conn(ctx); err != nil {
return errw(err)
}
defer lg.WarnIfCloseError(log, lgm.CloseConn, conn.(*sql.Conn))
for _, stmt := range p.qc.PreExecStmts {
if _, err = conn.ExecContext(ctx, stmt); err != nil {
return errw(err)
}
}
}
if err := QuerySQL(ctx, p.targetGrip, conn, recw, p.targetSQL); err != nil {
return err
}
if conn != nil && len(p.qc.PostExecStmts) > 0 {
for _, stmt := range p.qc.PostExecStmts {
if _, err := conn.ExecContext(ctx, stmt); err != nil {
return errw(err)
}
}
}
return nil
}
// executeTasks executes any tasks in pipeline.tasks.
// These tasks may exist if preparatory work must be performed
// before pipeline.targetSQL can be executed.
func (p *pipeline) executeTasks(ctx context.Context) error {
switch len(p.tasks) {
case 0:
return nil
case 1:
return p.tasks[0].executeTask(ctx)
default:
}
g, gCtx := errgroup.WithContext(ctx)
g.SetLimit(tuning.OptErrgroupLimit.Get(options.FromContext(ctx)))
for _, task := range p.tasks {
task := task
g.Go(func() error {
select {
case <-gCtx.Done():
return gCtx.Err()
default:
}
return task.executeTask(gCtx)
})
}
return g.Wait()
}
// prepareNoTable is invoked when the queryModel doesn't have a table.
// That is to say, the query doesn't have a "FROM table" clause. It is
// this function's responsibility to figure out what source to use, and
// to set the relevant pipeline fields.
func (p *pipeline) prepareNoTable(ctx context.Context, qm *queryModel) error {
log := lg.FromContext(ctx)
log.Debug("No table in query; will look for source to use...")
var (
src *source.Source
err error
handle = ast.NewInspector(qm.AST).FindFirstHandle()
)
if handle == "" {
src = p.qc.Collection.Active()
if src == nil || !p.qc.Grips.IsSQLSource(src) {
log.Debug("No active SQL source, will use an ephemeral db.")
p.targetGrip, err = p.qc.Grips.OpenEphemeral(ctx)
if err != nil {
return err
}
p.rc = &render.Context{
Renderer: p.targetGrip.SQLDriver().Renderer(),
Args: p.qc.Args,
Dialect: p.targetGrip.SQLDriver().Dialect(),
}
return nil
}
log.Debug("Using active source.", lga.Src, src)
} else if src, err = p.qc.Collection.Get(handle); err != nil {
return err
}
// At this point, src is non-nil.
if p.targetGrip, err = p.qc.Grips.Open(ctx, src); err != nil {
return err
}
p.rc = &render.Context{
Renderer: p.targetGrip.SQLDriver().Renderer(),
Args: p.qc.Args,
Dialect: p.targetGrip.SQLDriver().Dialect(),
}
return nil
}
// prepareFromTable builds the "FROM table" fragment.
//
// When this function returns, pipeline.rc will be set.
func (p *pipeline) prepareFromTable(ctx context.Context, tblSel *ast.TblSelectorNode) (fromClause string,
fromGrip driver.Grip, err error,
) {
handle := tblSel.Handle()
if handle == "" {
handle = p.qc.Collection.ActiveHandle()
if handle == "" {
return "", nil, errz.New("query does not specify source, and no active source")
}
}
src, err := p.qc.Collection.Get(handle)
if err != nil {
return "", nil, err
}
fromGrip, err = p.qc.Grips.Open(ctx, src)
if err != nil {
return "", nil, err
}
rndr := fromGrip.SQLDriver().Renderer()
p.rc = &render.Context{
Renderer: rndr,
Args: p.qc.Args,
Dialect: fromGrip.SQLDriver().Dialect(),
}
fromClause, err = rndr.FromTable(p.rc, tblSel)
if err != nil {
return "", nil, err
}
return fromClause, fromGrip, nil
}
// joinClause models the SQL "JOIN" construct.
type joinClause struct {
leftTbl *ast.TblSelectorNode
joins []*ast.JoinNode
}
// tables returns a new slice containing all referenced tables.
func (jc *joinClause) tables() []*ast.TblSelectorNode {
tbls := make([]*ast.TblSelectorNode, len(jc.joins)+1)
tbls[0] = jc.leftTbl
for i := range jc.joins {
tbls[i+1] = jc.joins[i].Table()
}
return tbls
}
// handles returns the set of (non-empty) handles from the tables,
// without any duplicates.
func (jc *joinClause) handles() []string {
handles := make([]string, len(jc.joins)+1)
handles[0] = jc.leftTbl.Handle()
for i := 0; i < len(jc.joins); i++ {
handles[i+1] = jc.joins[i].Table().Handle()
}
handles = lo.Uniq(handles)
handles = lo.Without(handles, "")
return handles
}
// isSingleSource returns true if the joins refer to the same handle.
func (jc *joinClause) isSingleSource() bool {
leftHandle := jc.leftTbl.Handle()
for _, join := range jc.joins {
joinHandle := join.Table().Handle()
if joinHandle == "" {
continue
}
if joinHandle != leftHandle {
return false
}
}
return true
}
// prepareFromJoin builds the "JOIN" clause.
//
// When this function returns, pipeline.rc will be set.
func (p *pipeline) prepareFromJoin(ctx context.Context, jc *joinClause) (fromClause string,
fromConn driver.Grip, err error,
) {
if jc.isSingleSource() {
return p.joinSingleSource(ctx, jc)
}
return p.joinCrossSource(ctx, jc)
}
// joinSingleSource sets up a join against a single source.
//
// On return, pipeline.rc will be set.
func (p *pipeline) joinSingleSource(ctx context.Context, jc *joinClause) (fromClause string,
fromGrip driver.Grip, err error,
) {
src, err := p.qc.Collection.Get(jc.leftTbl.Handle())
if err != nil {
return "", nil, err
}
fromGrip, err = p.qc.Grips.Open(ctx, src)
if err != nil {
return "", nil, err
}
rndr := fromGrip.SQLDriver().Renderer()
p.rc = &render.Context{
Renderer: rndr,
Args: p.qc.Args,
Dialect: fromGrip.SQLDriver().Dialect(),
}
fromClause, err = rndr.Join(p.rc, jc.leftTbl, jc.joins)
if err != nil {
return "", nil, err
}
return fromClause, fromGrip, nil
}
// joinCrossSource returns a FROM clause that forms part of
// the SQL SELECT statement against fromDB.
//
// On return, pipeline.rc will be set.
func (p *pipeline) joinCrossSource(ctx context.Context, jc *joinClause) (fromClause string,
fromDB driver.Grip, err error,
) {
handles := jc.handles()
srcs := make([]*source.Source, 0, len(handles))
for _, handle := range handles {
var src *source.Source
if src, err = p.qc.Collection.Get(handle); err != nil {
return "", nil, err
}
srcs = append(srcs, src)
}
// Open the join db
joinGrip, err := p.qc.Grips.OpenJoin(ctx, srcs...)
if err != nil {
return "", nil, err
}
rndr := joinGrip.SQLDriver().Renderer()
p.rc = &render.Context{
Renderer: rndr,
Args: p.qc.Args,
Dialect: joinGrip.SQLDriver().Dialect(),
}
leftHandle := jc.leftTbl.Handle()
// TODO: verify not empty
tbls := jc.tables()
for _, tbl := range tbls {
tbl := tbl
handle := tbl.Handle()
if handle == "" {
handle = leftHandle
}
var src *source.Source
if src, err = p.qc.Collection.Get(handle); err != nil {
return "", nil, err
}
var db driver.Grip
if db, err = p.qc.Grips.Open(ctx, src); err != nil {
return "", nil, err
}
task := &joinCopyTask{
fromGrip: db,
fromTbl: tbl.Table(),
toGrip: joinGrip,
toTbl: tbl.TblAliasOrName(),
}
tbl.SyncTblNameAlias()
p.tasks = append(p.tasks, task)
}
fromClause, err = rndr.Join(p.rc, jc.leftTbl, jc.joins)
if err != nil {
return "", nil, err
}
return fromClause, joinGrip, nil
}
// tasker is the interface for executing a DB task.
type tasker interface {
// executeTask executes a task against the DB.
executeTask(ctx context.Context) error
}
// joinCopyTask is a specification of a table data copy task to be performed
// for a cross-source join. That is, the data in fromDB.fromTblName will
// be copied to a table in toGrip. If colNames is
// empty, all cols in fromTbl are to be copied.
type joinCopyTask struct {
fromGrip driver.Grip
fromTbl tablefq.T
toGrip driver.Grip
toTbl tablefq.T
}
func (jt *joinCopyTask) executeTask(ctx context.Context) error {
return execCopyTable(ctx, jt.fromGrip, jt.fromTbl, jt.toGrip, jt.toTbl)
}
// execCopyTable performs the work of copying fromDB.fromTbl to destGrip.destTbl.
func execCopyTable(ctx context.Context, fromDB driver.Grip, fromTbl tablefq.T,
destGrip driver.Grip, destTbl tablefq.T,
) error {
log := lg.FromContext(ctx)
createTblHook := func(ctx context.Context, originRecMeta record.Meta, destGrip driver.Grip,
tx sqlz.DB,
) error {
destColNames := originRecMeta.Names()
destColKinds := originRecMeta.Kinds()
destTblDef := schema.NewTable(destTbl.Table, destColNames, destColKinds)
err := destGrip.SQLDriver().CreateTable(ctx, tx, destTblDef)
if err != nil {
return errz.Wrapf(err, "failed to create dest table %s.%s", destGrip.Source().Handle, destTbl)
}
return nil
}
inserter := NewDBWriter(
"Copy records",
destGrip,
destTbl.Table,
tuning.OptRecBufSize.Get(destGrip.Source().Options),
createTblHook,
)
query := "SELECT * FROM " + fromTbl.Render(fromDB.SQLDriver().Dialect().Enquote)
err := QuerySQL(ctx, fromDB, nil, inserter, query)
if err != nil {
return errz.Wrapf(err, "insert %s.%s failed", destGrip.Source().Handle, destTbl)
}
affected, err := inserter.Wait() // Stop for the writer to finish processing
if err != nil {
return errz.Wrapf(err, "insert %s.%s failed", destGrip.Source().Handle, destTbl)
}
log.Debug("Copied rows to dest", lga.Count, affected,
lga.From, fmt.Sprintf("%s.%s", fromDB.Source().Handle, fromTbl),
lga.To, fmt.Sprintf("%s.%s", destGrip.Source().Handle, destTbl))
return nil
}