commit aefce32ef79
Author: Sergei Golubchik <serg@mariadb.org>
Date:   Fri Jan 10 14:37:52 2025 +0100

    PoC: multi_update::finalize

diff --git a/sql/sql_class.h b/sql/sql_class.h
index 7f152673b00..af405ae57c6 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -6981,6 +6981,7 @@ class multi_update :public select_result_interceptor
   void abort_result_set() override;
   void update_used_tables() override;
   void prepare_to_read_rows() override;
+  int finalize(int local_error, bool run_updates);
 };
 
 class my_var_sp;
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index 00df334d495..9915a78b0db 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -2735,39 +2735,33 @@ int multi_update::send_data(List<Item> &not_used_values)
   DBUG_RETURN(0);
 }
 
-
-void multi_update::abort_result_set()
+int multi_update::finalize(int local_error, bool run_updates)
 {
-  /* the error was handled or nothing deleted and no side effects return */
-  if (unlikely(error_handled ||
-               (!thd->transaction->stmt.modified_non_trans_table && !updated)))
-    return;
-
-  /****************************************************************************
-
-    NOTE: if you change here be aware that almost the same code is in
-     multi_delete::send_eof().
-
-  ***************************************************************************/
-
-  /* Something already updated so we have to invalidate cache */
-  if (updated)
-    query_cache_invalidate3(thd, update_tables, 1);
   /*
     If all tables that has been updated are trans safe then just do rollback.
     If not attempt to do remaining updates.
   */
+  if (run_updates && table_count > 0 && do_updates())
+    local_error= 1;
+  THD_STAGE_INFO(thd, stage_end);
+
+  /*
+    if local_error is not set ON until after do_updates() then
+    later carried out killing should not affect binlogging.
+  */
+  killed_state killed_status= local_error == 0 ? NOT_KILLED : thd->killed;
+
+  /* We must invalidate the query cache before binlog writing and
+  ha_autocommit_... */
+  if (updated)
+    query_cache_invalidate3(thd, update_tables, 1);
 
-  if (! trans_safe)
-  {
-    DBUG_ASSERT(thd->transaction->stmt.modified_non_trans_table);
-    if (do_update && table_count > 1)
-    {
-      /* Add warning here */
-      (void) do_updates();
-    }
-  }
   if (thd->transaction->stmt.modified_non_trans_table)
+    thd->transaction->all.modified_non_trans_table= TRUE;
+  thd->transaction->all.m_unsafe_rollback_flags|=
+    thd->transaction->stmt.m_unsafe_rollback_flags & THD_TRANS::DID_WAIT;
+
+  if (local_error == 0 || thd->transaction->stmt.modified_non_trans_table)
   {
     /*
       The query has to binlog because there's a modified non-transactional table
@@ -2780,17 +2774,46 @@ void multi_update::abort_result_set()
         got caught and if happens later the killed error is written
         into repl event.
       */
-      int errcode= query_error_code(thd, thd->killed == NOT_KILLED);
+      int errcode= 0;
+      if (local_error == 0)
+        thd->clear_error();
+      else
+        errcode= query_error_code(thd, killed_status == NOT_KILLED);
       /* the error of binary logging is ignored */
-      (void)thd->binlog_query(THD::ROW_QUERY_TYPE,
-                        thd->query(), thd->query_length(),
-                        transactional_tables, FALSE, FALSE, errcode);
+      enum_binlog_format old_format= thd->get_current_stmt_binlog_format();
+      for (TABLE *table= all_tables->table; table; table= table->next)
+      {
+        if (table->versioned(VERS_TRX_ID))
+        {
+          thd->set_current_stmt_binlog_format_stmt();
+          break;
+        }
+      }
+      if (thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query(),
+                            thd->query_length(), transactional_tables, FALSE,
+                            FALSE, errcode) > 0)
+        local_error= 1;
+      thd->set_current_stmt_binlog_format(old_format);
     }
-    thd->transaction->all.modified_non_trans_table= TRUE;
   }
-  thd->transaction->all.m_unsafe_rollback_flags|=
-    (thd->transaction->stmt.m_unsafe_rollback_flags & THD_TRANS::DID_WAIT);
-  DBUG_ASSERT(trans_safe || !updated || thd->transaction->stmt.modified_non_trans_table);
+  DBUG_ASSERT(trans_safe || !updated ||
+              thd->transaction->stmt.modified_non_trans_table);
+  return local_error;
+}
+
+
+void multi_update::abort_result_set()
+{
+  /* the error was handled or nothing deleted and no side effects return */
+  if (unlikely(error_handled ||
+               (!thd->transaction->stmt.modified_non_trans_table && !updated)))
+    return;
+
+  /*
+    If all tables that has been updated are trans safe then just do rollback.
+    If not attempt to do remaining updates.
+  */
+  finalize(1, !trans_safe && do_update);
 }
 
 
@@ -3061,88 +3084,11 @@ bool multi_update::send_eof()
 {
   char buff[STRING_BUFFER_USUAL_SIZE];
   ulonglong id;
-  killed_state killed_status= NOT_KILLED;
   DBUG_ENTER("multi_update::send_eof");
   THD_STAGE_INFO(thd, stage_updating_reference_tables);
 
-  /* 
-     Does updates for the last n - 1 tables, returns 0 if ok;
-     error takes into account killed status gained in do_updates()
-  */
-  int local_error= thd->is_error();
-  if (likely(!local_error))
-    local_error = (table_count) ? do_updates() : 0;
-  /*
-    if local_error is not set ON until after do_updates() then
-    later carried out killing should not affect binlogging.
-  */
-  killed_status= (local_error == 0) ? NOT_KILLED : thd->killed;
-  THD_STAGE_INFO(thd, stage_end);
-
-  /****************************************************************************
-
-    NOTE: if you change here be aware that almost the same code is in
-     multi_update::abort_result_set().
-
-  ***************************************************************************/
-
-  /* We must invalidate the query cache before binlog writing and
-  ha_autocommit_... */
-
-  if (updated)
-  {
-    query_cache_invalidate3(thd, update_tables, 1);
-  }
-  /*
-    Write the SQL statement to the binlog if we updated
-    rows and we succeeded or if we updated some non
-    transactional tables.
-    
-    The query has to binlog because there's a modified non-transactional table
-    either from the query's list or via a stored routine: bug#13270,23333
-  */
-
-  if (thd->transaction->stmt.modified_non_trans_table)
-    thd->transaction->all.modified_non_trans_table= TRUE;
-  thd->transaction->all.m_unsafe_rollback_flags|=
-    (thd->transaction->stmt.m_unsafe_rollback_flags & THD_TRANS::DID_WAIT);
-
-  if (likely(local_error == 0 ||
-             thd->transaction->stmt.modified_non_trans_table))
-  {
-    if (WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open())
-    {
-      int errcode= 0;
-      if (likely(local_error == 0))
-        thd->clear_error();
-      else
-        errcode= query_error_code(thd, killed_status == NOT_KILLED);
-
-      bool force_stmt= false;
-      for (TABLE *table= all_tables->table; table; table= table->next)
-      {
-        if (table->versioned(VERS_TRX_ID))
-        {
-          force_stmt= true;
-          break;
-        }
-      }
-      enum_binlog_format save_binlog_format;
-      save_binlog_format= thd->get_current_stmt_binlog_format();
-      if (force_stmt)
-        thd->set_current_stmt_binlog_format_stmt();
-
-      if (thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query(),
-                            thd->query_length(), transactional_tables, FALSE,
-                            FALSE, errcode) > 0)
-	local_error= 1;				// Rollback update
-      thd->set_current_stmt_binlog_format(save_binlog_format);
-    }
-  }
-  DBUG_ASSERT(trans_safe || !updated ||
-              thd->transaction->stmt.modified_non_trans_table);
 
-  if (unlikely(local_error))
+  if (finalize(thd->is_error(), !thd->is_error()))
   {
     error_handled= TRUE; // to force early leave from ::abort_result_set()
     if (thd->killed == NOT_KILLED && !thd->get_stmt_da()->is_set())
