From ebcbed6d74f36401d37e41480fbb693d6fb5411a Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Thu, 25 Jul 2024 20:27:50 +0200 Subject: [PATCH] post-fixes for TRUNCATE * fix the truncate-by-handler variant, used by InnoDB * test that insert works after truncate, meaning graph table was emptied * test that the vector index size is zero after truncate in MyISAM --- mysql-test/main/vector,myisam.rdiff | 33 +++++++++++++++++++++++++++++ mysql-test/main/vector.result | 11 ++++++---- mysql-test/main/vector.test | 14 ++++++++++-- sql/handler.cc | 11 ++++++++-- sql/sql_base.cc | 4 ++-- sql/table.h | 2 +- sql/vector_mhnsw.cc | 5 +++-- sql/vector_mhnsw.h | 2 +- 8 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 mysql-test/main/vector,myisam.rdiff diff --git a/mysql-test/main/vector,myisam.rdiff b/mysql-test/main/vector,myisam.rdiff new file mode 100644 index 00000000000..50037667d42 --- /dev/null +++ b/mysql-test/main/vector,myisam.rdiff @@ -0,0 +1,33 @@ +--- a/mysql-test/main/vector.result ++++ b/mysql-test/main/vector.result +@@ -305,8 +305,30 @@ + create table t1 (id int auto_increment primary key, v blob not null, vector index (v)); + insert t1 (id, v) values (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'); + truncate table t1; ++ ++MyISAM file: datadir/test/t1#i#01 ++Record format: Packed ++Character set: ? (0) ++Data records: 0 Deleted blocks: 0 ++Recordlength: 30 ++ ++table description: ++Key Start Len Index Type ++1 3 6 unique varbin NULL ++2 2 1 multip. int8 + insert t1 (id, v) values (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'); + truncate table t1; ++ ++MyISAM file: datadir/test/t1#i#01 ++Record format: Packed ++Character set: ? (0) ++Data records: 0 Deleted blocks: 0 ++Recordlength: 30 ++ ++table description: ++Key Start Len Index Type ++1 3 6 unique varbin NULL ++2 2 1 multip. int8 + insert t1 (id, v) values (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'); + select id, hex(v) from t1; + id hex(v) diff --git a/mysql-test/main/vector.result b/mysql-test/main/vector.result index f80f56e523a..7a3b9c2f451 100644 --- a/mysql-test/main/vector.result +++ b/mysql-test/main/vector.result @@ -303,11 +303,14 @@ id hex(v) drop table t1, t2; db.opt create table t1 (id int auto_increment primary key, v blob not null, vector index (v)); +insert t1 (id, v) values (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'); truncate table t1; -insert t1 (v) values (x'e360d63ebe554f3fcdbc523f4522193f5236083d'); +insert t1 (id, v) values (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'); truncate table t1; -select * from t1; -id v +insert t1 (id, v) values (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'); +select id, hex(v) from t1; +id hex(v) +1 E360D63EBE554F3FCDBC523F4522193F5236083D show create table t1; Table Create Table t1 CREATE TABLE `t1` ( @@ -315,6 +318,6 @@ t1 CREATE TABLE `t1` ( `v` blob NOT NULL, PRIMARY KEY (`id`), VECTOR KEY `v` (`v`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci +) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci drop table t1; db.opt diff --git a/mysql-test/main/vector.test b/mysql-test/main/vector.test index f3d4c7ccfa8..d71e8dc19ce 100644 --- a/mysql-test/main/vector.test +++ b/mysql-test/main/vector.test @@ -103,10 +103,20 @@ drop table t1, t2; list_files $datadir/test; create table t1 (id int auto_increment primary key, v blob not null, vector index (v)); +insert t1 (id, v) values (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'); truncate table t1; -insert t1 (v) values (x'e360d63ebe554f3fcdbc523f4522193f5236083d'); +if ($MTR_COMBINATION_MYISAM) { +--replace_result $datadir datadir +--exec $MYISAMCHK -d $datadir/test/t1#i#01 +} +insert t1 (id, v) values (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'); truncate table t1; -select * from t1; +if ($MTR_COMBINATION_MYISAM) { +--replace_result $datadir datadir +--exec $MYISAMCHK -d $datadir/test/t1#i#01 +} +insert t1 (id, v) values (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'); +select id, hex(v) from t1; replace_result InnoDB MyISAM; show create table t1; drop table t1; diff --git a/sql/handler.cc b/sql/handler.cc index 3c385e620a4..d258fb769f2 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -5478,7 +5478,7 @@ handler::ha_delete_all_rows() int err= delete_all_rows(); if (!err) - err= table->hlindexes_on_delete_all(); + err= table->hlindexes_on_delete_all(false); return err; } @@ -5497,7 +5497,14 @@ handler::ha_truncate() m_lock_type == F_WRLCK); mark_trx_read_write(); - return truncate(); + int err= truncate(); + if (!err && table->s->hlindexes()) + { + if (!(err= table->hlindex_open(table->s->keys))) + err= table->hlindexes_on_delete_all(true); + } + + return err; } diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 599305a11f8..c0070eaf7f6 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -9952,11 +9952,11 @@ int TABLE::hlindexes_on_delete(const uchar *buf) return 0; } -int TABLE::hlindexes_on_delete_all() +int TABLE::hlindexes_on_delete_all(bool truncate) { DBUG_ASSERT(s->hlindexes() == (hlindex != NULL)); if (hlindex && hlindex->in_use) - if (int err= mhnsw_delete_all(this, key_info + s->keys)) + if (int err= mhnsw_delete_all(this, key_info + s->keys, truncate)) return err; return 0; } diff --git a/sql/table.h b/sql/table.h index b30dd6d5103..ed3815d7d9f 100644 --- a/sql/table.h +++ b/sql/table.h @@ -1801,7 +1801,7 @@ public: int hlindexes_on_insert(); int hlindexes_on_update(); int hlindexes_on_delete(const uchar *buf); - int hlindexes_on_delete_all(); + int hlindexes_on_delete_all(bool truncate); int reset_hlindexes(); void prepare_triggers_for_insert_stmt_or_event(); diff --git a/sql/vector_mhnsw.cc b/sql/vector_mhnsw.cc index 1564d106b79..8eb35866550 100644 --- a/sql/vector_mhnsw.cc +++ b/sql/vector_mhnsw.cc @@ -1250,7 +1250,7 @@ int mhnsw_invalidate(TABLE *table, const uchar *rec, KEY *keyinfo) return 0; } -int mhnsw_delete_all(TABLE *table, KEY *keyinfo) +int mhnsw_delete_all(TABLE *table, KEY *keyinfo, bool truncate) { TABLE *graph= table->hlindex; @@ -1259,7 +1259,8 @@ int mhnsw_delete_all(TABLE *table, KEY *keyinfo) DBUG_ASSERT(keyinfo->algorithm == HA_KEY_ALG_VECTOR); DBUG_ASSERT(keyinfo->usable_key_parts == 1); - if (int err= graph->file->ha_delete_all_rows()) + if (int err= truncate ? graph->file->truncate() + : graph->file->delete_all_rows()) return err; MHNSW_Context *ctx; diff --git a/sql/vector_mhnsw.h b/sql/vector_mhnsw.h index 05365d48f28..12e109b339d 100644 --- a/sql/vector_mhnsw.h +++ b/sql/vector_mhnsw.h @@ -26,7 +26,7 @@ int mhnsw_insert(TABLE *table, KEY *keyinfo); int mhnsw_read_first(TABLE *table, KEY *keyinfo, Item *dist, ulonglong limit); int mhnsw_read_next(TABLE *table); int mhnsw_invalidate(TABLE *table, const uchar *rec, KEY *keyinfo); -int mhnsw_delete_all(TABLE *table, KEY *keyinfo); +int mhnsw_delete_all(TABLE *table, KEY *keyinfo, bool truncate); void mhnsw_free(TABLE_SHARE *share); extern ulonglong mhnsw_cache_size;