@@ -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
727749func (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- }
0 commit comments