summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mysql-test/r/key.result15
-rw-r--r--mysql-test/t/key.test23
-rw-r--r--sql/sql_select.cc9
3 files changed, 46 insertions, 1 deletions
diff --git a/mysql-test/r/key.result b/mysql-test/r/key.result
index a2bed75a709..6c115435fb6 100644
--- a/mysql-test/r/key.result
+++ b/mysql-test/r/key.result
@@ -530,3 +530,18 @@ ORDER BY c.b, c.d
a b c d e f g h i j a b c d
2 2 1 2004-11-30 12:00:00 1 0 0 0 0 0 2 3388000 -553000 NULL
DROP TABLE t1, t2;
+DROP TABLE IF EXISTS t1;
+CREATE TABLE t1 (a INT PRIMARY KEY AUTO_INCREMENT);
+INSERT INTO t1 VALUES (), (), ();
+SELECT 1 AS c1
+FROM t1
+ORDER BY (
+SELECT 1 AS c2
+FROM t1
+GROUP BY GREATEST(LAST_INSERT_ID(), t1.a) ASC
+LIMIT 1);
+c1
+1
+1
+1
+DROP TABLE t1;
diff --git a/mysql-test/t/key.test b/mysql-test/t/key.test
index f1eb8e68b49..cd6c480407d 100644
--- a/mysql-test/t/key.test
+++ b/mysql-test/t/key.test
@@ -501,3 +501,26 @@ ORDER BY c.b, c.d
;
DROP TABLE t1, t2;
+
+#
+# Bug #31148: bool close_thread_table(THD*, TABLE**): Assertion
+# `table->key_read == 0' failed.
+#
+
+--disable_warnings
+DROP TABLE IF EXISTS t1;
+--enable_warnings
+
+CREATE TABLE t1 (a INT PRIMARY KEY AUTO_INCREMENT);
+
+INSERT INTO t1 VALUES (), (), ();
+
+SELECT 1 AS c1
+FROM t1
+ORDER BY (
+ SELECT 1 AS c2
+ FROM t1
+ GROUP BY GREATEST(LAST_INSERT_ID(), t1.a) ASC
+ LIMIT 1);
+
+DROP TABLE t1;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index eb2731205ec..d041b2edcfa 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -6651,7 +6651,14 @@ void JOIN::cleanup(bool full)
for (tab= join_tab, end= tab+tables; tab != end; tab++)
{
if (tab->table)
- tab->table->file->ha_index_or_rnd_end();
+ {
+ if (tab->table->key_read)
+ {
+ tab->table->key_read= 0;
+ tab->table->file->extra(HA_EXTRA_NO_KEYREAD);
+ }
+ tab->table->file->ha_index_or_rnd_end();
+ }
}
}
}