2020-08-06 20:58:47 +03:00
|
|
|
package libsq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-12 21:24:01 +03:00
|
|
|
"github.com/neilotoole/errgroup"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/lg"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/ast"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2020-08-23 22:00:13 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlmodel"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlz"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
// engine executes a queryModel and writes to a RecordWriter.
|
|
|
|
type engine struct {
|
|
|
|
log lg.Log
|
|
|
|
srcs *source.Set
|
|
|
|
dbOpener driver.DatabaseOpener
|
|
|
|
joinDBOpener driver.JoinDatabaseOpener
|
2021-03-08 09:27:35 +03:00
|
|
|
|
|
|
|
// tasks contains tasks that must be completed before targetSQL
|
|
|
|
// is executed against targetDB. Typically tasks is used to
|
|
|
|
// set up the joindb before it is queried.
|
|
|
|
tasks []tasker
|
|
|
|
|
|
|
|
// targetSQL is the ultimate SQL query to be executed against
|
|
|
|
// targetDB.
|
|
|
|
targetSQL string
|
|
|
|
|
|
|
|
// targetDB is the destination for the ultimate SQL query to
|
|
|
|
// be executed against.
|
|
|
|
targetDB driver.Database
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
// prepare prepares the engine to execute queryModel.
|
|
|
|
// When this method returns, targetDB and targetSQL will be set,
|
|
|
|
// as will any tasks (may be empty). The tasks must be executed
|
|
|
|
// against targetDB before targetSQL is executed (the engine.execute
|
|
|
|
// method does this work).
|
|
|
|
func (ng *engine) prepare(ctx context.Context, qm *queryModel) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
selectable := qm.Selectable
|
|
|
|
|
|
|
|
var fromClause string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch selectable := selectable.(type) {
|
|
|
|
case *ast.TblSelector:
|
2021-03-08 09:27:35 +03:00
|
|
|
fromClause, ng.targetDB, err = ng.buildTableFromClause(ctx, selectable)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *ast.Join:
|
2021-03-08 09:27:35 +03:00
|
|
|
fromClause, ng.targetDB, err = ng.buildJoinFromClause(ctx, selectable)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errz.Errorf("unknown selectable %T: %q", selectable, selectable)
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
fragBuilder, qb := ng.targetDB.SQLDriver().SQLBuilder()
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
selectColsClause, err := fragBuilder.SelectCols(qm.Cols)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
qb.SetSelect(selectColsClause)
|
|
|
|
qb.SetFrom(fromClause)
|
|
|
|
|
|
|
|
if qm.Range != nil {
|
2022-12-18 09:07:38 +03:00
|
|
|
var rangeClause string
|
2020-08-06 20:58:47 +03:00
|
|
|
rangeClause, err = fragBuilder.Range(qm.Range)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-18 09:07:38 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
qb.SetRange(rangeClause)
|
|
|
|
}
|
|
|
|
|
|
|
|
if qm.Where != nil {
|
2022-12-18 09:07:38 +03:00
|
|
|
var whereClause string
|
|
|
|
whereClause, err = fragBuilder.Where(qm.Where)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
qb.SetWhere(whereClause)
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
ng.targetSQL, err = qb.SQL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute executes the plan that was built by engine.prepare.
|
|
|
|
func (ng *engine) execute(ctx context.Context, recw RecordWriter) error {
|
|
|
|
ng.log.Debugf("engine.execute: [%s]: %s", ng.targetDB.Source().Handle, ng.targetSQL)
|
|
|
|
|
|
|
|
err := ng.executeTasks(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
return QuerySQL(ctx, ng.log, ng.targetDB, recw, ng.targetSQL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// executeTasks executes any tasks in engine.tasks.
|
|
|
|
// These tasks may exist if preparatory work must be performed
|
|
|
|
// before engine.targetSQL can be executed.
|
|
|
|
func (ng *engine) executeTasks(ctx context.Context) error {
|
|
|
|
switch len(ng.tasks) {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
return ng.tasks[0].executeTask(ctx, ng.log)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
g, gCtx := errgroup.WithContextN(ctx, driver.Tuning.ErrgroupNumG, driver.Tuning.ErrgroupQSize)
|
|
|
|
for _, task := range ng.tasks {
|
|
|
|
task := task
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
return task.executeTask(gCtx, ng.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.Wait()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2022-12-18 03:51:33 +03:00
|
|
|
func (ng *engine) buildTableFromClause(ctx context.Context, tblSel *ast.TblSelector) (fromClause string,
|
2022-12-18 11:35:59 +03:00
|
|
|
fromConn driver.Database, err error,
|
|
|
|
) {
|
2020-08-06 20:58:47 +03:00
|
|
|
src, err := ng.srcs.Get(tblSel.DSName)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fromConn, err = ng.dbOpener.Open(ctx, src)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fragBuilder, _ := fromConn.SQLDriver().SQLBuilder()
|
|
|
|
fromClause, err = fragBuilder.FromTable(tblSel)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fromClause, fromConn, nil
|
|
|
|
}
|
|
|
|
|
2022-12-18 03:51:33 +03:00
|
|
|
func (ng *engine) buildJoinFromClause(ctx context.Context, fnJoin *ast.Join) (fromClause string,
|
2022-12-18 11:35:59 +03:00
|
|
|
fromConn driver.Database, err error,
|
|
|
|
) {
|
2020-08-06 20:58:47 +03:00
|
|
|
if fnJoin.LeftTbl() == nil || fnJoin.LeftTbl().SelValue() == "" {
|
|
|
|
return "", nil, errz.Errorf("JOIN is missing left table reference")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fnJoin.RightTbl() == nil || fnJoin.RightTbl().SelValue() == "" {
|
|
|
|
return "", nil, errz.Errorf("JOIN is missing right table reference")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fnJoin.LeftTbl().DSName != fnJoin.RightTbl().DSName {
|
|
|
|
return ng.crossSourceJoin(ctx, fnJoin)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ng.singleSourceJoin(ctx, fnJoin)
|
|
|
|
}
|
|
|
|
|
2022-12-18 05:43:53 +03:00
|
|
|
func (ng *engine) singleSourceJoin(ctx context.Context, fnJoin *ast.Join) (fromClause string, fromDB driver.Database,
|
2022-12-18 11:35:59 +03:00
|
|
|
err error,
|
|
|
|
) {
|
2020-08-06 20:58:47 +03:00
|
|
|
src, err := ng.srcs.Get(fnJoin.LeftTbl().DSName)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fromDB, err = ng.dbOpener.Open(ctx, src)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fragBuilder, _ := fromDB.SQLDriver().SQLBuilder()
|
|
|
|
fromClause, err = fragBuilder.Join(fnJoin)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fromClause, fromDB, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// crossSourceJoin returns a FROM clause that forms part of
|
|
|
|
// the SQL SELECT statement against fromDB.
|
2022-12-18 05:43:53 +03:00
|
|
|
func (ng *engine) crossSourceJoin(ctx context.Context, fnJoin *ast.Join) (fromClause string, fromDB driver.Database,
|
2022-12-18 11:35:59 +03:00
|
|
|
err error,
|
|
|
|
) {
|
2020-08-06 20:58:47 +03:00
|
|
|
leftTblName, rightTblName := fnJoin.LeftTbl().SelValue(), fnJoin.RightTbl().SelValue()
|
|
|
|
if leftTblName == rightTblName {
|
2022-12-18 05:43:53 +03:00
|
|
|
return "", nil, errz.Errorf("JOIN tables must have distinct names (or use aliases): duplicate tbl name %q",
|
|
|
|
fnJoin.LeftTbl().SelValue())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
leftSrc, err := ng.srcs.Get(fnJoin.LeftTbl().DSName)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rightSrc, err := ng.srcs.Get(fnJoin.RightTbl().DSName)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2021-03-08 09:27:35 +03:00
|
|
|
|
|
|
|
// Open the join db
|
|
|
|
joinDB, err := ng.joinDBOpener.OpenJoin(ctx, leftSrc, rightSrc)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
leftDB, err := ng.dbOpener.Open(ctx, leftSrc)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2021-03-08 09:27:35 +03:00
|
|
|
leftCopyTask := &joinCopyTask{
|
|
|
|
fromDB: leftDB,
|
|
|
|
fromTblName: leftTblName,
|
|
|
|
toDB: joinDB,
|
|
|
|
toTblName: leftTblName,
|
|
|
|
}
|
|
|
|
|
|
|
|
rightDB, err := ng.dbOpener.Open(ctx, rightSrc)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2021-03-08 09:27:35 +03:00
|
|
|
rightCopyTask := &joinCopyTask{
|
|
|
|
fromDB: rightDB,
|
|
|
|
fromTblName: rightTblName,
|
|
|
|
toDB: joinDB,
|
|
|
|
toTblName: rightTblName,
|
|
|
|
}
|
|
|
|
|
|
|
|
ng.tasks = append(ng.tasks, leftCopyTask)
|
|
|
|
ng.tasks = append(ng.tasks, rightCopyTask)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
joinDBFragBuilder, _ := joinDB.SQLDriver().SQLBuilder()
|
|
|
|
fromClause, err = joinDBFragBuilder.Join(fnJoin)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fromClause, joinDB, nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
// tasker is the interface for executing a DB task.
|
|
|
|
type tasker interface {
|
|
|
|
// executeTask executes a task against the DB.
|
|
|
|
executeTask(ctx context.Context, log lg.Log) error
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// 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
|
2021-03-08 09:27:35 +03:00
|
|
|
// be copied to a table in toDB. If colNames is
|
2020-08-06 20:58:47 +03:00
|
|
|
// empty, all cols in fromTblName are to be copied.
|
|
|
|
type joinCopyTask struct {
|
|
|
|
fromDB driver.Database
|
|
|
|
fromTblName string
|
2021-03-08 09:27:35 +03:00
|
|
|
toDB driver.Database
|
|
|
|
toTblName string
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
func (jt *joinCopyTask) executeTask(ctx context.Context, log lg.Log) error {
|
|
|
|
return execCopyTable(ctx, log, jt.fromDB, jt.fromTblName, jt.toDB, jt.toTblName)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// execCopyTable performs the work of copying fromDB.fromTblName to destDB.destTblName.
|
2022-12-18 05:43:53 +03:00
|
|
|
func execCopyTable(ctx context.Context, log lg.Log, fromDB driver.Database, fromTblName string, destDB driver.Database,
|
2022-12-18 11:35:59 +03:00
|
|
|
destTblName string,
|
|
|
|
) error {
|
2022-12-18 05:43:53 +03:00
|
|
|
createTblHook := func(ctx context.Context, originRecMeta sqlz.RecordMeta, destDB driver.Database,
|
2022-12-18 11:35:59 +03:00
|
|
|
tx sqlz.DB,
|
|
|
|
) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
destColNames := originRecMeta.Names()
|
|
|
|
destColKinds := originRecMeta.Kinds()
|
|
|
|
destTblDef := sqlmodel.NewTableDef(destTblName, destColNames, destColKinds)
|
|
|
|
|
|
|
|
err := destDB.SQLDriver().CreateTable(ctx, tx, destTblDef)
|
|
|
|
if err != nil {
|
|
|
|
return errz.Wrapf(err, "failed to create dest table %s.%s", destDB.Source().Handle, destTblName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 05:56:22 +03:00
|
|
|
inserter := NewDBWriter(log, destDB, destTblName, driver.Tuning.RecordChSize, createTblHook)
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
query := "SELECT * FROM " + fromDB.SQLDriver().Dialect().Enquote(fromTblName)
|
2020-08-06 20:58:47 +03:00
|
|
|
err := QuerySQL(ctx, log, fromDB, inserter, query)
|
|
|
|
if err != nil {
|
|
|
|
return errz.Wrapf(err, "insert %s.%s failed", destDB.Source().Handle, destTblName)
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := inserter.Wait() // Wait for the writer to finish processing
|
|
|
|
if err != nil {
|
|
|
|
return errz.Wrapf(err, "insert %s.%s failed", destDB.Source().Handle, destTblName)
|
|
|
|
}
|
|
|
|
log.Debugf("Copied %d rows to %s.%s", affected, destDB.Source().Handle, destTblName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// queryModel is a model of a SLQ query built from the AST.
|
|
|
|
type queryModel struct {
|
|
|
|
AST *ast.AST
|
|
|
|
Selectable ast.Selectable
|
|
|
|
Cols []ast.ColExpr
|
|
|
|
Range *ast.RowRange
|
|
|
|
Where *ast.Where
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qm *queryModel) String() string {
|
|
|
|
return fmt.Sprintf("%v | %v | %v", qm.Selectable, qm.Cols, qm.Range)
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildQueryModel creates a queryModel instance from the AST.
|
|
|
|
func buildQueryModel(log lg.Log, a *ast.AST) (*queryModel, error) {
|
|
|
|
if len(a.Segments()) == 0 {
|
|
|
|
return nil, errz.Errorf("query model error: query does not have enough segments")
|
|
|
|
}
|
|
|
|
|
|
|
|
insp := ast.NewInspector(log, a)
|
|
|
|
selectableSeg, err := insp.FindFinalSelectableSegment()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(selectableSeg.Children()) != 1 {
|
2022-12-18 05:43:53 +03:00
|
|
|
return nil, errz.Errorf(
|
|
|
|
"the final selectable segment must have exactly one selectable element, but found %d elements",
|
2020-08-12 21:24:01 +03:00
|
|
|
len(selectableSeg.Children()))
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
selectable, ok := selectableSeg.Children()[0].(ast.Selectable)
|
|
|
|
if !ok {
|
2022-12-18 05:43:53 +03:00
|
|
|
return nil, errz.Errorf(
|
|
|
|
"the final selectable segment must have exactly one selectable element, but found element %T(%q)",
|
2020-08-12 21:24:01 +03:00
|
|
|
selectableSeg.Children()[0], selectableSeg.Children()[0].Text())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
qm := &queryModel{AST: a, Selectable: selectable}
|
|
|
|
|
|
|
|
// Look for range
|
|
|
|
for seg := selectableSeg.Next(); seg != nil; seg = seg.Next() {
|
|
|
|
// Check if the first element of the segment is a row range, if not, just skip
|
|
|
|
if rr, ok := seg.Children()[0].(*ast.RowRange); ok {
|
|
|
|
if len(seg.Children()) != 1 {
|
2022-12-18 05:43:53 +03:00
|
|
|
return nil, errz.Errorf(
|
|
|
|
"segment [%d] with row range must have exactly one element, but found %d: %q",
|
2020-08-12 21:24:01 +03:00
|
|
|
seg.SegIndex(), len(seg.Children()), seg.Text())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if qm.Range != nil {
|
2022-12-18 05:43:53 +03:00
|
|
|
return nil, errz.Errorf("only one row range permitted, but found %q and %q",
|
|
|
|
qm.Range.Text(), rr.Text())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("found row range: %q", rr.Text())
|
|
|
|
qm.Range = rr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
seg, err := insp.FindColExprSegment()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-09 16:46:46 +03:00
|
|
|
if seg != nil {
|
2020-08-06 20:58:47 +03:00
|
|
|
elems := seg.Children()
|
|
|
|
colExprs := make([]ast.ColExpr, len(elems))
|
|
|
|
for i, elem := range elems {
|
|
|
|
colExpr, ok := elem.(ast.ColExpr)
|
|
|
|
if !ok {
|
|
|
|
return nil, errz.Errorf("expected element in segment [%d] to be col expr, but was %T", i, elem)
|
|
|
|
}
|
|
|
|
|
|
|
|
colExprs[i] = colExpr
|
|
|
|
}
|
|
|
|
|
|
|
|
qm.Cols = colExprs
|
|
|
|
}
|
|
|
|
|
|
|
|
whereClauses, err := insp.FindWhereClauses()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(whereClauses) > 1 {
|
2020-08-09 16:46:46 +03:00
|
|
|
return nil, errz.Errorf("only one WHERE clause is supported, but found %d", len(whereClauses))
|
2020-08-06 20:58:47 +03:00
|
|
|
} else if len(whereClauses) == 1 {
|
|
|
|
qm.Where = whereClauses[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return qm, nil
|
|
|
|
}
|