Skip to content

Commit d88464a

Browse files
author
Tatiana Azundris Nuernberg
committed
Bug#35497623: SHOW INDEXES with generated column may access wrong key-part
In SHOW INDEXES we iterate over the KEY_PART_INFOs of a KEY, and if it's a functional index, we try to show its gcol_info (as in, "generated column info"). Unfortunately in the unpatched server, we try to obtain that gcol_info from the first key-part in the key, rather than from the current key-part. This patch rectifies this. While we're at it, we also make Column_name NULLable, seeing as we occasionally set it to NULL (as we do in the test case added by this patch). This aligns the result of SHOW INDEXES for temporary tables with that of regular tables. Change-Id: I021c905150e38cd84739e5f4f69494781fb3d88e
1 parent bfa27aa commit d88464a

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

mysql-test/r/functional_index.result

+30-2
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ DROP TABLE t1;
158158
CREATE TABLE t1 (col1 INT, INDEX ((ABS(col1))));
159159
DROP TABLE t1;
160160
# Print out functional indexes on a temporary table. Note that
161-
# "Column_name" should be empty (NULL), and Expression should contain the
161+
# "Column_name" should be NULL, and Expression should contain the
162162
# expression for the functional index.
163163
CREATE TEMPORARY TABLE t1(a INT);
164164
CREATE INDEX idx ON t1 ((ABS(a)));
165165
SHOW KEYS FROM t1;
166166
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
167-
t1 1 idx 1 A 0 NULL NULL YES BTREE YES abs(`a`)
167+
t1 1 idx 1 NULL A 0 NULL NULL YES BTREE YES abs(`a`)
168168
DROP TABLE t1;
169169
CREATE TABLE t1 (col1 INT, INDEX ((CONCAT(''))));
170170
ERROR HY000: The used storage engine cannot index the expression 'concat(_utf8mb4'')'.
@@ -776,3 +776,31 @@ INDEX i1((FIND_IN_SET('Admin', roles)))
776776

777777
# Clean up.
778778
DROP TABLE t1;
779+
780+
#
781+
# Bug#35497623: SHOW INDEXES with generated column may access wrong key-part
782+
#
783+
784+
# SHOW INDEXES on regular table:
785+
CREATE TABLE t1 (c1 INT);
786+
CREATE TABLE t2
787+
(UNIQUE i3 TYPE BTREE (c1, (c1 REGEXP c1))) TABLE t1;
788+
SHOW INDEXES IN t2;
789+
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
790+
t2 0 i3 1 c1 A 0 NULL NULL YES BTREE YES NULL
791+
t2 0 i3 2 NULL A 0 NULL NULL YES BTREE YES regexp_like(`c1`,`c1`)
792+
/* regular table */
793+
DROP TABLE t2;
794+
DROP TABLE t1;
795+
796+
# SHOW INDEXES on temporary table (different code path!):
797+
CREATE TABLE t1 (c1 INT);
798+
CREATE TEMPORARY TABLE t2
799+
(UNIQUE i3 TYPE BTREE (c1, (c1 REGEXP c1))) TABLE t1;
800+
SHOW INDEXES IN t2;
801+
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
802+
t2 0 i3 1 c1 A 0 NULL NULL YES BTREE YES NULL
803+
t2 0 i3 2 NULL A 0 NULL NULL YES BTREE YES regexp_like(`c1`,`c1`)
804+
/* temporary table */
805+
DROP TABLE t2;
806+
DROP TABLE t1;

mysql-test/t/functional_index.test

+24-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ CREATE TABLE t1 (col1 INT, INDEX ((ABS(col1))));
137137
DROP TABLE t1;
138138

139139
--echo # Print out functional indexes on a temporary table. Note that
140-
--echo # "Column_name" should be empty (NULL), and Expression should contain the
140+
--echo # "Column_name" should be NULL, and Expression should contain the
141141
--echo # expression for the functional index.
142142
CREATE TEMPORARY TABLE t1(a INT);
143143
CREATE INDEX idx ON t1 ((ABS(a)));
@@ -678,7 +678,29 @@ CREATE TABLE t1 (
678678
INDEX i1((FIND_IN_SET('Admin', roles)))
679679
);
680680

681-
682681
--echo
683682
--echo # Clean up.
684683
DROP TABLE t1;
684+
685+
--echo
686+
--echo #
687+
--echo # Bug#35497623: SHOW INDEXES with generated column may access wrong key-part
688+
--echo #
689+
690+
--echo
691+
--echo # SHOW INDEXES on regular table:
692+
CREATE TABLE t1 (c1 INT);
693+
CREATE TABLE t2
694+
(UNIQUE i3 TYPE BTREE (c1, (c1 REGEXP c1))) TABLE t1;
695+
SHOW INDEXES IN t2; /* regular table */
696+
DROP TABLE t2;
697+
DROP TABLE t1;
698+
699+
--echo
700+
--echo # SHOW INDEXES on temporary table (different code path!):
701+
CREATE TABLE t1 (c1 INT);
702+
CREATE TEMPORARY TABLE t2
703+
(UNIQUE i3 TYPE BTREE (c1, (c1 REGEXP c1))) TABLE t1;
704+
SHOW INDEXES IN t2; /* temporary table */
705+
DROP TABLE t2;
706+
DROP TABLE t1;

sql/sql_show.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -4397,7 +4397,7 @@ static int get_schema_tmp_table_keys_record(THD *thd, Table_ref *tables,
43974397
// Expression for functional key parts
43984398
if (key_part->field != nullptr &&
43994399
key_part->field->is_field_for_functional_index()) {
4400-
Value_generator *gcol = key_info->key_part->field->gcol_info;
4400+
Value_generator *gcol = key_part->field->gcol_info;
44014401

44024402
table->field[TMP_TABLE_KEYS_EXPRESSION]->store(
44034403
gcol->expr_str.str, gcol->expr_str.length, cs);
@@ -4965,7 +4965,7 @@ ST_FIELD_INFO tmp_table_keys_fields_info[] = {
49654965
{"INDEX_SCHEMA", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 0, nullptr, 0},
49664966
{"INDEX_NAME", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 0, "Key_name", 0},
49674967
{"SEQ_IN_INDEX", 2, MYSQL_TYPE_LONGLONG, 0, 0, "Seq_in_index", 0},
4968-
{"COLUMN_NAME", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 0, "Column_name", 0},
4968+
{"COLUMN_NAME", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 1, "Column_name", 0},
49694969
{"COLLATION", 1, MYSQL_TYPE_STRING, 0, 1, "Collation", 0},
49704970
{"CARDINALITY", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 1,
49714971
"Cardinality", 0},

0 commit comments

Comments
 (0)