From b0aae6ca719ddc7f6b28531db43b9d3890ce5f19 Mon Sep 17 00:00:00 2001
From: Sergey Vojtovich <svoj@mariadb.org>
Date: Tue, 27 Aug 2024 22:25:34 +0400
Subject: [PATCH] ALTER TABLE fixes for high-level indexes

quick_rm_table() expects .frm to exist when it removes high-level indexes.
For cases like ALTER TABLE t1 RENAME TO t2, ENGINE=other_engine .frm was
removed earlier.

Another option would be removing high-level indexes explicitly before the
first quick_rm_table() and skipping high-level indexes for subsequent
quick_rm_table(NO_FRM_RENAME).

But this suggested order may also help with ddl log recovery. That is
if we crash before high-level indexes are removed, .frm is going to
exist.
---
 mysql-test/main/vector,myisam.rdiff | 30 ++++++++++++++++++++++++++++-
 mysql-test/main/vector.result       | 28 +++++++++++++++++++++++++++
 mysql-test/main/vector.test         |  6 ++++++
 sql/sql_table.cc                    |  9 +++++----
 4 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/mysql-test/main/vector,myisam.rdiff b/mysql-test/main/vector,myisam.rdiff
index e11c4b58f2a..06f474a9b6c 100644
--- a/mysql-test/main/vector,myisam.rdiff
+++ b/mysql-test/main/vector,myisam.rdiff
@@ -394,7 +394,7 @@
  create vector index if not exists v on t1(v);
  # CHANGE/MODIFY/DROP COLUMN (vector)
  alter table t1 modify column v int;
-@@ -666,34 +666,6 @@
+@@ -666,62 +666,6 @@
  alter table t1 alter key if exists v ignored;
  alter table t1 alter key if exists v not ignored;
  # ENGINE
@@ -425,6 +425,34 @@
 -  `v` blob NOT NULL,
 -  PRIMARY KEY (`id`),
 -  VECTOR KEY `v` (`v`)
+-) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
+-alter table t1 rename to t2, engine=myisam;
+-db.opt
+-t2#i#01.MYD
+-t2#i#01.MYI
+-t2.MYD
+-t2.MYI
+-t2.frm
+-show create table t2;
+-Table	Create Table
+-t2	CREATE TABLE `t2` (
+-  `id` int(11) NOT NULL AUTO_INCREMENT,
+-  `v` blob NOT NULL,
+-  PRIMARY KEY (`id`),
+-  VECTOR KEY `v` (`v`)
+-) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
+-alter table t2 rename to t1, engine=innodb;
+-db.opt
+-t1#i#01.ibd
+-t1.frm
+-t1.ibd
+-show create table t1;
+-Table	Create Table
+-t1	CREATE TABLE `t1` (
+-  `id` int(11) NOT NULL AUTO_INCREMENT,
+-  `v` blob NOT NULL,
+-  PRIMARY KEY (`id`),
+-  VECTOR KEY `v` (`v`)
 -) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
  # CHANGE/MODIFY/DROP COLUMN (vector)
  alter table t1 modify column v int;
diff --git a/mysql-test/main/vector.result b/mysql-test/main/vector.result
index 6349aefbd12..23e53c084e1 100644
--- a/mysql-test/main/vector.result
+++ b/mysql-test/main/vector.result
@@ -781,6 +781,34 @@ t1	CREATE TABLE `t1` (
   PRIMARY KEY (`id`),
   VECTOR KEY `v` (`v`)
 ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
+alter table t1 rename to t2, engine=myisam;
+db.opt
+t2#i#01.MYD
+t2#i#01.MYI
+t2.MYD
+t2.MYI
+t2.frm
+show create table t2;
+Table	Create Table
+t2	CREATE TABLE `t2` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `v` blob NOT NULL,
+  PRIMARY KEY (`id`),
+  VECTOR KEY `v` (`v`)
+) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
+alter table t2 rename to t1, engine=innodb;
+db.opt
+t1#i#01.ibd
+t1.frm
+t1.ibd
+show create table t1;
+Table	Create Table
+t1	CREATE TABLE `t1` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `v` blob NOT NULL,
+  PRIMARY KEY (`id`),
+  VECTOR KEY `v` (`v`)
+) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
 # CHANGE/MODIFY/DROP COLUMN (vector)
 alter table t1 modify column v int;
 ERROR HY000: Incorrect arguments to VECTOR INDEX
diff --git a/mysql-test/main/vector.test b/mysql-test/main/vector.test
index 8820537a387..589015ec900 100644
--- a/mysql-test/main/vector.test
+++ b/mysql-test/main/vector.test
@@ -352,6 +352,12 @@ show create table t1;
 alter table t1 engine=innodb;
 list_files $datadir/test;
 show create table t1;
+alter table t1 rename to t2, engine=myisam;
+list_files $datadir/test;
+show create table t2;
+alter table t2 rename to t1, engine=innodb;
+list_files $datadir/test;
+show create table t1;
 }
 
 --echo # CHANGE/MODIFY/DROP COLUMN (vector)
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index dbfb257458c..7d52b6cbe82 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -11843,10 +11843,6 @@ do_continue:;
   thd->push_internal_handler(&errors_to_warnings);
   error_handler_pushed=1;
 
-  quick_rm_table(thd, old_db_type, &alter_ctx.db, &backup_name,
-                        FN_IS_TMP | (engine_changed ? NO_HA_TABLE | NO_PAR_TABLE: 0));
-
-  debug_crash_here("ddl_log_alter_after_delete_backup");
   if (engine_changed)
   {
     /* the .frm file was removed but not the original table */
@@ -11854,6 +11850,11 @@ do_continue:;
                            NO_FRM_RENAME | (engine_changed ? 0 : FN_IS_TMP));
   }
 
+  debug_crash_here("ddl_log_alter_after_delete_backup");
+
+  quick_rm_table(thd, old_db_type, &alter_ctx.db, &backup_name,
+                        FN_IS_TMP | (engine_changed ? NO_HA_TABLE | NO_PAR_TABLE: 0));
+
   debug_crash_here("ddl_log_alter_after_drop_original_table");
   if (binlog_as_create_select)
   {
-- 
2.30.9