2020-08-06 20:58:47 +03:00
|
|
|
package libsq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/ast/render"
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lga"
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"golang.org/x/exp/slog"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"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"
|
2023-04-03 00:17:15 +03:00
|
|
|
"golang.org/x/sync/errgroup"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// engine executes a queryModel and writes to a RecordWriter.
|
|
|
|
type engine struct {
|
2023-04-02 22:49:45 +03:00
|
|
|
log *slog.Logger
|
2023-04-01 12:48:24 +03:00
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
// qc is the context in which the query is executed.
|
2023-04-01 12:48:24 +03:00
|
|
|
qc *QueryContext
|
2021-03-08 09:27:35 +03:00
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
// rc is the Context for rendering SQL.
|
|
|
|
// This field is set during engine.prepare. It can't be set before
|
|
|
|
// then because the target DB to use is calculated during engine.prepare,
|
|
|
|
// based on the input query and other context.
|
|
|
|
rc *render.Context
|
|
|
|
|
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
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
// targetSQL is the ultimate SQL query to be executed against targetDB.
|
2021-03-08 09:27:35 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
func newEngine(ctx context.Context, qc *QueryContext, query string) (*engine, error) {
|
2023-04-30 17:18:56 +03:00
|
|
|
log := lg.From(ctx)
|
2023-04-02 22:49:45 +03:00
|
|
|
|
2023-04-01 12:48:24 +03:00
|
|
|
a, err := ast.Parse(log, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
qModel, err := buildQueryModel(log, a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ng := &engine{
|
|
|
|
log: log,
|
|
|
|
qc: qc,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ng.prepare(ctx, qModel); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ng, nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
// execute executes the plan that was built by engine.prepare.
|
|
|
|
func (ng *engine) execute(ctx context.Context, recw RecordWriter) error {
|
2023-04-02 22:49:45 +03:00
|
|
|
ng.log.Debug(
|
|
|
|
"Execute SQL query",
|
|
|
|
lga.Target, ng.targetDB.Source().Handle,
|
|
|
|
lga.SQL, ng.targetSQL,
|
|
|
|
)
|
2021-03-08 09:27:35 +03:00
|
|
|
|
|
|
|
err := ng.executeTasks(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
return QuerySQL(ctx, ng.targetDB, recw, ng.targetSQL)
|
2021-03-08 09:27:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
2023-04-02 22:49:45 +03:00
|
|
|
return ng.tasks[0].executeTask(ctx)
|
2021-03-08 09:27:35 +03:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-04-03 00:17:15 +03:00
|
|
|
g, gCtx := errgroup.WithContext(ctx)
|
2023-04-08 21:09:27 +03:00
|
|
|
g.SetLimit(driver.Tuning.ErrgroupLimit)
|
2023-04-03 00:17:15 +03:00
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
for _, task := range ng.tasks {
|
|
|
|
task := task
|
|
|
|
|
|
|
|
g.Go(func() error {
|
2023-04-08 21:09:27 +03:00
|
|
|
select {
|
|
|
|
case <-gCtx.Done():
|
|
|
|
return gCtx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2023-04-02 22:49:45 +03:00
|
|
|
return task.executeTask(gCtx)
|
2021-03-08 09:27:35 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.Wait()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
// buildTableFromClause builds the "FROM table" fragment.
|
|
|
|
//
|
|
|
|
// When this function returns, ng.rc will be set.
|
2023-03-22 09:17:34 +03:00
|
|
|
func (ng *engine) buildTableFromClause(ctx context.Context, tblSel *ast.TblSelectorNode) (fromClause string,
|
2022-12-18 11:35:59 +03:00
|
|
|
fromConn driver.Database, err error,
|
|
|
|
) {
|
2023-04-19 08:28:09 +03:00
|
|
|
src, err := ng.qc.Collection.Get(tblSel.Handle())
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:48:24 +03:00
|
|
|
fromConn, err = ng.qc.DBOpener.Open(ctx, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
rndr := fromConn.SQLDriver().Renderer()
|
|
|
|
ng.rc = &render.Context{
|
|
|
|
Renderer: rndr,
|
|
|
|
Args: ng.qc.Args,
|
|
|
|
Dialect: fromConn.SQLDriver().Dialect(),
|
|
|
|
}
|
|
|
|
|
|
|
|
fromClause, err = rndr.FromTable(ng.rc, tblSel)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fromClause, fromConn, nil
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
// buildJoinFromClause builds the "JOIN" clause.
|
|
|
|
//
|
|
|
|
// When this function returns, ng.rc will be set.
|
2023-03-22 09:17:34 +03:00
|
|
|
func (ng *engine) buildJoinFromClause(ctx context.Context, fnJoin *ast.JoinNode) (fromClause string,
|
2022-12-18 11:35:59 +03:00
|
|
|
fromConn driver.Database, err error,
|
|
|
|
) {
|
2023-03-22 09:17:34 +03:00
|
|
|
if fnJoin.LeftTbl() == nil || fnJoin.LeftTbl().TblName() == "" {
|
2020-08-06 20:58:47 +03:00
|
|
|
return "", nil, errz.Errorf("JOIN is missing left table reference")
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
if fnJoin.RightTbl() == nil || fnJoin.RightTbl().TblName() == "" {
|
2020-08-06 20:58:47 +03:00
|
|
|
return "", nil, errz.Errorf("JOIN is missing right table reference")
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
if fnJoin.LeftTbl().Handle() != fnJoin.RightTbl().Handle() {
|
2020-08-06 20:58:47 +03:00
|
|
|
return ng.crossSourceJoin(ctx, fnJoin)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ng.singleSourceJoin(ctx, fnJoin)
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
// singleSourceJoin sets up a join against a single source.
|
|
|
|
//
|
|
|
|
// On return, ng.rc will be set.
|
2023-03-22 09:17:34 +03:00
|
|
|
func (ng *engine) singleSourceJoin(ctx context.Context, fnJoin *ast.JoinNode) (fromClause string,
|
|
|
|
fromDB driver.Database, err error,
|
2022-12-18 11:35:59 +03:00
|
|
|
) {
|
2023-04-19 08:28:09 +03:00
|
|
|
src, err := ng.qc.Collection.Get(fnJoin.LeftTbl().Handle())
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:48:24 +03:00
|
|
|
fromDB, err = ng.qc.DBOpener.Open(ctx, src)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
rndr := fromDB.SQLDriver().Renderer()
|
|
|
|
ng.rc = &render.Context{
|
|
|
|
Renderer: rndr,
|
|
|
|
Args: ng.qc.Args,
|
|
|
|
Dialect: fromDB.SQLDriver().Dialect(),
|
|
|
|
}
|
|
|
|
|
|
|
|
fromClause, err = rndr.Join(ng.rc, fnJoin)
|
2020-08-06 20:58:47 +03:00
|
|
|
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.
|
2023-04-07 11:00:49 +03:00
|
|
|
//
|
|
|
|
// On return, ng.rc will be set.
|
2023-03-22 09:17:34 +03:00
|
|
|
func (ng *engine) crossSourceJoin(ctx context.Context, fnJoin *ast.JoinNode) (fromClause string, fromDB driver.Database,
|
2022-12-18 11:35:59 +03:00
|
|
|
err error,
|
|
|
|
) {
|
2023-03-22 09:17:34 +03:00
|
|
|
leftTblName, rightTblName := fnJoin.LeftTbl().TblName(), fnJoin.RightTbl().TblName()
|
2020-08-06 20:58:47 +03:00
|
|
|
if leftTblName == rightTblName {
|
2023-04-02 22:49:45 +03:00
|
|
|
return "", nil, errz.Errorf("JOIN tables must have distinct names (or use aliases): duplicate tbl name {%s}",
|
2023-03-22 09:17:34 +03:00
|
|
|
fnJoin.LeftTbl().TblName())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
leftSrc, err := ng.qc.Collection.Get(fnJoin.LeftTbl().Handle())
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
rightSrc, err := ng.qc.Collection.Get(fnJoin.RightTbl().Handle())
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2021-03-08 09:27:35 +03:00
|
|
|
|
|
|
|
// Open the join db
|
2023-04-01 12:48:24 +03:00
|
|
|
joinDB, err := ng.qc.JoinDBOpener.OpenJoin(ctx, leftSrc, rightSrc)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
rndr := joinDB.SQLDriver().Renderer()
|
|
|
|
ng.rc = &render.Context{
|
|
|
|
Renderer: rndr,
|
|
|
|
Args: ng.qc.Args,
|
|
|
|
Dialect: joinDB.SQLDriver().Dialect(),
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:48:24 +03:00
|
|
|
leftDB, err := ng.qc.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,
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:48:24 +03:00
|
|
|
rightDB, err := ng.qc.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
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
fromClause, err = rndr.Join(ng.rc, fnJoin)
|
2020-08-06 20:58:47 +03:00
|
|
|
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.
|
2023-04-02 22:49:45 +03:00
|
|
|
executeTask(ctx context.Context) error
|
2021-03-08 09:27:35 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
func (jt *joinCopyTask) executeTask(ctx context.Context) error {
|
|
|
|
return execCopyTable(ctx, 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.
|
2023-04-02 22:49:45 +03:00
|
|
|
func execCopyTable(ctx context.Context, fromDB driver.Database, fromTblName string,
|
|
|
|
destDB driver.Database, destTblName string,
|
2022-12-18 11:35:59 +03:00
|
|
|
) error {
|
2023-04-30 17:18:56 +03:00
|
|
|
log := lg.From(ctx)
|
2023-04-02 22:49:45 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
inserter := NewDBWriter(destDB, destTblName, driver.Tuning.RecordChSize, createTblHook)
|
2021-01-04 05:56:22 +03:00
|
|
|
|
2021-03-08 09:27:35 +03:00
|
|
|
query := "SELECT * FROM " + fromDB.SQLDriver().Dialect().Enquote(fromTblName)
|
2023-04-02 22:49:45 +03:00
|
|
|
err := QuerySQL(ctx, fromDB, inserter, query)
|
2020-08-06 20:58:47 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-04-02 22:49:45 +03:00
|
|
|
log.Debug("Copied %d rows to %s.%s", affected, destDB.Source().Handle, destTblName)
|
2020-08-06 20:58:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
// queryModel is a model of an SLQ query built from the AST.
|
2020-08-06 20:58:47 +03:00
|
|
|
type queryModel struct {
|
2023-03-28 09:48:24 +03:00
|
|
|
AST *ast.AST
|
|
|
|
Table ast.Tabler
|
|
|
|
Cols []ast.ResultColumn
|
|
|
|
Range *ast.RowRangeNode
|
|
|
|
Where *ast.WhereNode
|
|
|
|
OrderBy *ast.OrderByNode
|
|
|
|
GroupBy *ast.GroupByNode
|
|
|
|
Distinct *ast.UniqueNode
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (qm *queryModel) String() string {
|
2023-03-28 09:48:24 +03:00
|
|
|
return fmt.Sprintf("%v | %v | %v", qm.Table, qm.Cols, qm.Range)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// buildQueryModel creates a queryModel instance from the AST.
|
2023-04-02 22:49:45 +03:00
|
|
|
func buildQueryModel(log *slog.Logger, a *ast.AST) (*queryModel, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
if len(a.Segments()) == 0 {
|
|
|
|
return nil, errz.Errorf("query model error: query does not have enough segments")
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
insp := ast.NewInspector(a)
|
2023-03-28 09:48:24 +03:00
|
|
|
tablerSeg, err := insp.FindFinalTablerSegment()
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
if len(tablerSeg.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",
|
2023-03-28 09:48:24 +03:00
|
|
|
len(tablerSeg.Children()))
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
tabler, ok := tablerSeg.Children()[0].(ast.Tabler)
|
2020-08-06 20:58:47 +03:00
|
|
|
if !ok {
|
2022-12-18 05:43:53 +03:00
|
|
|
return nil, errz.Errorf(
|
2023-04-02 22:49:45 +03:00
|
|
|
"the final selectable segment must have exactly one selectable element, but found element %T(%s)",
|
2023-03-28 09:48:24 +03:00
|
|
|
tablerSeg.Children()[0], tablerSeg.Children()[0].Text())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
qm := &queryModel{AST: a, Table: tabler}
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// Look for range
|
2023-03-28 09:48:24 +03:00
|
|
|
for seg := tablerSeg.Next(); seg != nil; seg = seg.Next() {
|
2020-08-06 20:58:47 +03:00
|
|
|
// Check if the first element of the segment is a row range, if not, just skip
|
2023-03-26 04:20:53 +03:00
|
|
|
if rr, ok := seg.Children()[0].(*ast.RowRangeNode); ok {
|
2020-08-06 20:58:47 +03:00
|
|
|
if len(seg.Children()) != 1 {
|
2022-12-18 05:43:53 +03:00
|
|
|
return nil, errz.Errorf(
|
2023-04-02 22:49:45 +03:00
|
|
|
"segment [%d] with row range must have exactly one element, but found %d: %s",
|
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 {
|
2023-04-02 22:49:45 +03:00
|
|
|
return nil, errz.Errorf("only one row range permitted, but found {%s} and {%s}",
|
2022-12-18 05:43:53 +03:00
|
|
|
qm.Range.Text(), rr.Text())
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
log.Debug("found row range: %s", rr.Text())
|
2020-08-06 20:58:47 +03:00
|
|
|
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()
|
2023-03-22 09:17:34 +03:00
|
|
|
colExprs := make([]ast.ResultColumn, len(elems))
|
2020-08-06 20:58:47 +03:00
|
|
|
for i, elem := range elems {
|
2023-03-22 09:17:34 +03:00
|
|
|
colExpr, ok := elem.(ast.ResultColumn)
|
2020-08-06 20:58:47 +03:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2023-03-26 04:20:53 +03:00
|
|
|
if qm.OrderBy, err = insp.FindOrderByNode(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-26 11:01:41 +03:00
|
|
|
if qm.GroupBy, err = insp.FindGroupByNode(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
if qm.Distinct, err = insp.FindUniqueNode(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
return qm, nil
|
|
|
|
}
|