Skip to content

Commit 8da487f

Browse files
committed
Merge branch 'main' into zachmu/enginetests5
2 parents 9753305 + b8ae9a1 commit 8da487f

File tree

215 files changed

+4879
-5587
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

215 files changed

+4879
-5587
lines changed

engine.go

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (e *Engine) AnalyzeQuery(
209209
ctx *sql.Context,
210210
query string,
211211
) (sql.Node, error) {
212-
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
212+
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.EventScheduler, e.Parser)
213213
parsed, _, _, qFlags, err := binder.Parse(query, nil, false)
214214
if err != nil {
215215
return nil, err
@@ -237,7 +237,7 @@ func (e *Engine) PrepareParsedQuery(
237237
statementKey, query string,
238238
stmt sqlparser.Statement,
239239
) (sql.Node, error) {
240-
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
240+
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.EventScheduler, e.Parser)
241241
node, _, err := binder.BindOnly(stmt, query, nil)
242242

243243
if err != nil {
@@ -443,13 +443,24 @@ func (e *Engine) QueryWithBindings(ctx *sql.Context, query string, parsed sqlpar
443443
if err2 != nil {
444444
return nil, nil, nil, errors.Wrap(err, "unable to clear autocommit transaction: "+err2.Error())
445445
}
446+
return nil, nil, nil, err
447+
}
446448

449+
var schema sql.Schema
450+
iter, schema = rowexec.FinalizeIters(ctx, analyzed, qFlags, iter)
451+
if err != nil {
452+
clearAutocommitErr := clearAutocommitTransaction(ctx)
453+
if clearAutocommitErr != nil {
454+
return nil, nil, nil, errors.Wrap(err, "unable to clear autocommit transaction: "+clearAutocommitErr.Error())
455+
}
447456
return nil, nil, nil, err
448457
}
449458

450-
iter = finalizeIters(ctx, analyzed, qFlags, iter)
459+
if schema == nil {
460+
schema = analyzed.Schema()
461+
}
451462

452-
return analyzed.Schema(), iter, qFlags, nil
463+
return schema, iter, qFlags, nil
453464
}
454465

455466
// PrepQueryPlanForExecution prepares a query plan for execution and returns the result schema with a row iterator to
@@ -478,13 +489,24 @@ func (e *Engine) PrepQueryPlanForExecution(ctx *sql.Context, _ string, plan sql.
478489
if err2 != nil {
479490
return nil, nil, nil, errors.Wrap(err, "unable to clear autocommit transaction: "+err2.Error())
480491
}
492+
return nil, nil, nil, err
493+
}
481494

495+
var schema sql.Schema
496+
iter, schema = rowexec.FinalizeIters(ctx, plan, qFlags, iter)
497+
if err != nil {
498+
clearAutocommitErr := clearAutocommitTransaction(ctx)
499+
if clearAutocommitErr != nil {
500+
return nil, nil, nil, errors.Wrap(err, "unable to clear autocommit transaction: "+clearAutocommitErr.Error())
501+
}
482502
return nil, nil, nil, err
483503
}
484504

485-
iter = finalizeIters(ctx, plan, qFlags, iter)
505+
if schema == nil {
506+
schema = plan.Schema()
507+
}
486508

487-
return plan.Schema(), iter, qFlags, nil
509+
return schema, iter, qFlags, nil
488510
}
489511

490512
// BoundQueryPlan returns query plan for the given statement with the given bindings applied
@@ -495,7 +517,7 @@ func (e *Engine) BoundQueryPlan(ctx *sql.Context, query string, parsed sqlparser
495517

496518
query = sql.RemoveSpaceAndDelimiter(query, ';')
497519

498-
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
520+
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.EventScheduler, e.Parser)
499521
binder.SetBindings(bindings)
500522

501523
// Begin a transaction if necessary (no-op if one is in flight)
@@ -549,7 +571,7 @@ func (e *Engine) preparedStatement(ctx *sql.Context, query string, parsed sqlpar
549571
preparedAst, preparedDataFound = e.PreparedDataCache.GetCachedStmt(ctx.Session.ID(), query)
550572
}
551573

552-
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
574+
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.EventScheduler, e.Parser)
553575
if preparedDataFound {
554576
parsed = preparedAst
555577
binder.SetBindings(bindings)
@@ -725,18 +747,19 @@ func (e *Engine) CloseSession(connID uint32) {
725747
}
726748

727749
func (e *Engine) beginTransaction(ctx *sql.Context) error {
728-
beginNewTransaction := ctx.GetTransaction() == nil || plan.ReadCommitted(ctx)
729-
if beginNewTransaction {
730-
ctx.GetLogger().Tracef("beginning new transaction")
731-
ts, ok := ctx.Session.(sql.TransactionSession)
732-
if ok {
733-
tx, err := ts.StartTransaction(ctx, sql.ReadWrite)
734-
if err != nil {
735-
return err
736-
}
750+
if ctx.GetTransaction() != nil {
751+
return nil
752+
}
737753

738-
ctx.SetTransaction(tx)
754+
ctx.GetLogger().Tracef("beginning new transaction")
755+
ts, ok := ctx.Session.(sql.TransactionSession)
756+
if ok {
757+
tx, err := ts.StartTransaction(ctx, sql.ReadWrite)
758+
if err != nil {
759+
return err
739760
}
761+
762+
ctx.SetTransaction(tx)
740763
}
741764

742765
return nil
@@ -782,6 +805,10 @@ func (e *Engine) EngineAnalyzer() *analyzer.Analyzer {
782805
return e.Analyzer
783806
}
784807

808+
func (e *Engine) EngineEventScheduler() sql.EventScheduler {
809+
return e.EventScheduler
810+
}
811+
785812
// InitializeEventScheduler initializes the EventScheduler for the engine with the given sql.Context
786813
// getter function, |ctxGetterFunc, the EventScheduler |status|, and the |period| for the event scheduler
787814
// to check for events to execute. If |period| is less than 1, then it is ignored and the default period
@@ -792,8 +819,6 @@ func (e *Engine) InitializeEventScheduler(ctxGetterFunc func() (*sql.Context, fu
792819
if err != nil {
793820
return err
794821
}
795-
796-
e.Analyzer.EventScheduler = e.EventScheduler
797822
return nil
798823
}
799824

@@ -830,7 +855,7 @@ func (e *Engine) executeEvent(ctx *sql.Context, dbName, createEventStatement, us
830855
return err
831856
}
832857

833-
iter = finalizeIters(ctx, definitionNode, nil, iter)
858+
iter, _ = rowexec.FinalizeIters(ctx, definitionNode, nil, iter)
834859

835860
// Drain the iterate to execute the event body/definition
836861
// NOTE: No row data is returned for an event; we just need to execute the statements
@@ -863,12 +888,3 @@ func findCreateEventNode(planTree sql.Node) (*plan.CreateEvent, error) {
863888

864889
return createEventNode, nil
865890
}
866-
867-
// finalizeIters applies the final transformations on sql.RowIter before execution.
868-
func finalizeIters(ctx *sql.Context, analyzed sql.Node, qFlags *sql.QueryFlags, iter sql.RowIter) sql.RowIter {
869-
iter = rowexec.AddTriggerRollbackIter(ctx, qFlags, iter)
870-
iter = rowexec.AddTransactionCommittingIter(qFlags, iter)
871-
iter = plan.AddTrackedRowIter(ctx, analyzed, iter)
872-
iter = rowexec.AddExpressionCloser(analyzed, iter)
873-
return iter
874-
}

engine_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ func TestTrackProcess(t *testing.T) {
231231
require.True(ok)
232232

233233
iter, err := rowexec.DefaultBuilder.Build(ctx, result, nil)
234-
iter = finalizeIters(ctx, result, nil, iter)
234+
require.NoError(err)
235+
iter, _ = rowexec.FinalizeIters(ctx, result, nil, iter)
235236
require.NoError(err)
236237
_, err = sql.RowIterToRows(ctx, iter)
237238
require.NoError(err)

enginetest/engine_only_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func TestAnalyzer_Exp(t *testing.T) {
442442
require.NoError(t, err)
443443

444444
ctx := enginetest.NewContext(harness)
445-
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, sql.NewMysqlParser())
445+
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, e.EngineEventScheduler(), nil)
446446
parsed, _, _, _, err := b.Parse(tt.query, nil, false)
447447
require.NoError(t, err)
448448

@@ -866,10 +866,6 @@ func (s SimpleTableFunction) WithChildren(_ ...sql.Node) (sql.Node, error) {
866866
return s, nil
867867
}
868868

869-
func (s SimpleTableFunction) CheckPrivileges(_ *sql.Context, _ sql.PrivilegedOperationChecker) bool {
870-
return true
871-
}
872-
873869
// CollationCoercibility implements the interface sql.CollationCoercible.
874870
func (SimpleTableFunction) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
875871
return sql.Collation_binary, 7

enginetest/enginetests.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,6 @@ func TestQueryPlan(t *testing.T, harness Harness, e QueryEngine, tt queries.Quer
583583
func TestQueryPlanWithName(t *testing.T, name string, harness Harness, e QueryEngine, query, expectedPlan string, options sql.DescribeOptions) {
584584
t.Run(name, func(t *testing.T) {
585585
ctx := NewContext(harness)
586-
587586
parsed, qFlags, err := planbuilder.Parse(ctx, e.EngineAnalyzer().Catalog, query)
588587
require.NoError(t, err)
589588

@@ -1710,12 +1709,12 @@ func TestInsertScriptsPrepared(t *testing.T, harness Harness) {
17101709
func TestGeneratedColumns(t *testing.T, harness Harness) {
17111710
harness.Setup(setup.MydbData)
17121711
for _, script := range queries.GeneratedColumnTests {
1713-
TestScriptPrepared(t, harness, script)
1712+
TestScript(t, harness, script)
17141713
}
17151714
for _, script := range queries.BrokenGeneratedColumnTests {
17161715
t.Run(script.Name, func(t *testing.T) {
17171716
t.Skip(script.Name)
1718-
TestScriptPrepared(t, harness, script)
1717+
TestScript(t, harness, script)
17191718
})
17201719
}
17211720
}

enginetest/evaluation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func injectBindVarsAndPrepare(
526526
}
527527
}
528528

529-
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, sql.NewMysqlParser())
529+
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, e.EngineEventScheduler(), nil)
530530
b.SetParserOptions(sql.LoadSqlMode(ctx).ParserOptions())
531531
resPlan, _, err := b.BindOnly(parsed, q, nil)
532532
if err != nil {

enginetest/memory_engine_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,9 @@ func newMergableIndex(dbs []sql.Database, tableName string, exprs ...sql.Express
10281028
if db == nil {
10291029
return nil
10301030
}
1031+
if tableRevision, ok := table.(*memory.TableRevision); ok {
1032+
table = tableRevision.Table
1033+
}
10311034
return &memory.Index{
10321035
DB: db.Name(),
10331036
DriverName: memory.IndexDriverId,

enginetest/plangen/cmd/plangen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func generatePlansForSuite(spec PlanSpec, w *bytes.Buffer) error {
165165

166166
if !tt.Skip {
167167
ctx := enginetest.NewContext(harness)
168-
binder := planbuilder.New(ctx, engine.EngineAnalyzer().Catalog, sql.NewMysqlParser())
168+
binder := planbuilder.New(ctx, engine.EngineAnalyzer().Catalog, engine.EngineEventScheduler(), nil)
169169
parsed, _, _, qFlags, err := binder.Parse(tt.Query, nil, false)
170170
if err != nil {
171171
exit(fmt.Errorf("%w\nfailed to parse query: %s", err, tt.Query))

0 commit comments

Comments
 (0)