summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergei Golubchik <serg@mariadb.org>2022-01-24 21:26:07 +0100
committerSergei Golubchik <serg@mariadb.org>2022-01-26 18:43:06 +0100
commitd751f42a02c08a965510dbbfef0cbe6ca0ccd992 (patch)
treeeca387dd3e9ad48e55a030ab92056f732d2eca24
parent72b5b8b2e3986d207ac016e33ba53e91099c0ef5 (diff)
downloadmariadb-git-d751f42a02c08a965510dbbfef0cbe6ca0ccd992.tar.gz
MDEV-27586 Auto-increment does not work with DESC on MERGE table
also fix handler::get_auto_increment()
-rw-r--r--mysql-test/main/merge.result14
-rw-r--r--mysql-test/main/merge.test11
-rw-r--r--sql/handler.cc11
3 files changed, 32 insertions, 4 deletions
diff --git a/mysql-test/main/merge.result b/mysql-test/main/merge.result
index 348717b3950..2dcc5a1433b 100644
--- a/mysql-test/main/merge.result
+++ b/mysql-test/main/merge.result
@@ -3913,5 +3913,19 @@ select * from tm;
ERROR HY000: Unable to open underlying table which is differently defined or of non-MyISAM type or doesn't exist
drop table tm, t;
#
+# MDEV-27586 Auto-increment does not work with DESC on MERGE table
+#
+create table t (a int not null, primary key(a desc)) engine=myisam;
+create table tm (a int not null auto_increment, primary key(a desc)) engine=merge union=(t) insert_method=first;
+insert into tm () values ();
+insert into tm () values ();
+insert into tm () values ();
+select * from tm;
+a
+3
+2
+1
+drop table tm, t;
+#
# End of 10.8 tests
#
diff --git a/mysql-test/main/merge.test b/mysql-test/main/merge.test
index f9c68cf71f3..0485f3ed1c3 100644
--- a/mysql-test/main/merge.test
+++ b/mysql-test/main/merge.test
@@ -2873,5 +2873,16 @@ select * from tm;
drop table tm, t;
--echo #
+--echo # MDEV-27586 Auto-increment does not work with DESC on MERGE table
+--echo #
+create table t (a int not null, primary key(a desc)) engine=myisam;
+create table tm (a int not null auto_increment, primary key(a desc)) engine=merge union=(t) insert_method=first;
+insert into tm () values ();
+insert into tm () values ();
+insert into tm () values ();
+select * from tm;
+drop table tm, t;
+
+--echo #
--echo # End of 10.8 tests
--echo #
diff --git a/sql/handler.cc b/sql/handler.cc
index 9c4c6287950..d5061ac84d1 100644
--- a/sql/handler.cc
+++ b/sql/handler.cc
@@ -4112,6 +4112,9 @@ void handler::get_auto_increment(ulonglong offset, ulonglong increment,
int error;
MY_BITMAP *old_read_set;
bool rnd_inited= (inited == RND);
+ bool rev= table->key_info[table->s->next_number_index].
+ key_part[table->s->next_number_keypart].key_part_flag &
+ HA_REVERSE_SORT;
if (rnd_inited && ha_rnd_end())
return;
@@ -4133,7 +4136,8 @@ void handler::get_auto_increment(ulonglong offset, ulonglong increment,
if (table->s->next_number_keypart == 0)
{ // Autoincrement at key-start
- error= ha_index_last(table->record[1]);
+ error= rev ? ha_index_first(table->record[1])
+ : ha_index_last(table->record[1]);
/*
MySQL implicitly assumes such method does locking (as MySQL decides to
use nr+increment without checking again with the handler, in
@@ -4148,9 +4152,8 @@ void handler::get_auto_increment(ulonglong offset, ulonglong increment,
table->key_info + table->s->next_number_index,
table->s->next_number_key_offset);
error= ha_index_read_map(table->record[1], key,
- make_prev_keypart_map(table->s->
- next_number_keypart),
- HA_READ_PREFIX_LAST);
+ make_prev_keypart_map(table->s->next_number_keypart),
+ rev ? HA_READ_KEY_EXACT : HA_READ_PREFIX_LAST);
/*
MySQL needs to call us for next row: assume we are inserting ("a",null)
here, we return 3, and next this statement will want to insert