@@ -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
@@ -830,7 +852,7 @@ func (e *Engine) executeEvent(ctx *sql.Context, dbName, createEventStatement, us
830852 return err
831853 }
832854
833- iter = finalizeIters (ctx , definitionNode , nil , iter )
855+ iter , _ = rowexec . FinalizeIters (ctx , definitionNode , nil , iter )
834856
835857 // Drain the iterate to execute the event body/definition
836858 // NOTE: No row data is returned for an event; we just need to execute the statements
@@ -863,12 +885,3 @@ func findCreateEventNode(planTree sql.Node) (*plan.CreateEvent, error) {
863885
864886 return createEventNode , nil
865887}
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