Commit 16b41a58 authored by jyang's avatar jyang

branches/zip: If a unique index is on a column prefix, such

unique index cannot be upgrade to primary index even if there
is no primary index already defined. Also fix possible corruption
when initialize "ref_length" value in case there is a mismatch
between MySQL and InnoDB primary key. Fix bug #51378: "Init
'ref_length'  to correct value, in case an out of bound MySQL
primary_key".
rb://262 approved by Marko.
parent 6c7ddbe2
......@@ -3304,6 +3304,9 @@ ha_innobase::innobase_initialize_autoinc()
ulint err;
update_thd(ha_thd());
ut_a(prebuilt->trx == thd_to_trx(user_thd));
col_name = field->field_name;
index = innobase_get_index(table->s->next_number_index);
......@@ -3494,23 +3497,67 @@ retry:
of length ref_length! */
if (!row_table_got_default_clust_index(ib_table)) {
if (primary_key >= MAX_KEY) {
sql_print_error("Table %s has a primary key in InnoDB data "
"dictionary, but not in MySQL!", name);
}
prebuilt->clust_index_was_generated = FALSE;
/* MySQL allocates the buffer for ref. key_info->key_length
includes space for all key columns + one byte for each column
that may be NULL. ref_length must be as exact as possible to
save space, because all row reference buffers are allocated
based on ref_length. */
if (UNIV_UNLIKELY(primary_key >= MAX_KEY)) {
sql_print_error("Table %s has a primary key in "
"InnoDB data dictionary, but not "
"in MySQL!", name);
/* This mismatch could cause further problems
if not attended, bring this to the user's attention
by printing a warning in addition to log a message
in the errorlog */
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
ER_NO_SUCH_INDEX,
"InnoDB: Table %s has a "
"primary key in InnoDB data "
"dictionary, but not in "
"MySQL!", name);
/* If primary_key >= MAX_KEY, its (primary_key)
value could be out of bound if continue to index
into key_info[] array. Find InnoDB primary index,
and assign its key_length to ref_length.
In addition, since MySQL indexes are sorted starting
with primary index, unique index etc., initialize
ref_length to the first index key length in
case we fail to find InnoDB cluster index.
Please note, this will not resolve the primary
index mismatch problem, other side effects are
possible if users continue to use the table.
However, we allow this table to be opened so
that user can adopt necessary measures for the
mismatch while still being accessible to the table
date. */
ref_length = table->key_info[0].key_length;
/* Find correspoinding cluster index
key length in MySQL's key_info[] array */
for (ulint i = 0; i < table->s->keys; i++) {
dict_index_t* index;
index = innobase_get_index(i);
if (dict_index_is_clust(index)) {
ref_length =
table->key_info[i].key_length;
}
}
} else {
/* MySQL allocates the buffer for ref.
key_info->key_length includes space for all key
columns + one byte for each column that may be
NULL. ref_length must be as exact as possible to
save space, because all row reference buffers are
allocated based on ref_length. */
ref_length = table->key_info[primary_key].key_length;
}
} else {
if (primary_key != MAX_KEY) {
sql_print_error("Table %s has no primary key in InnoDB data "
sql_print_error(
"Table %s has no primary key in InnoDB data "
"dictionary, but has one in MySQL! If you "
"created the table with a MySQL version < "
"3.23.54 and did not define a primary key, "
......@@ -3519,6 +3566,17 @@ retry:
"key as the primary key. You can fix this "
"error by dump + DROP + CREATE + reimport "
"of the table.", name);
/* This mismatch could cause further problems
if not attended, bring this to the user attention
by printing a warning in addition to log a message
in the errorlog */
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
ER_NO_SUCH_INDEX,
"InnoDB: Table %s has no "
"primary key in InnoDB data "
"dictionary, but has one in "
"MySQL!", name);
}
prebuilt->clust_index_was_generated = TRUE;
......@@ -5402,9 +5460,6 @@ ha_innobase::innobase_get_index(
DBUG_ENTER("innobase_get_index");
ha_statistic_increment(&SSV::ha_read_key_count);
ut_ad(user_thd == ha_thd());
ut_a(prebuilt->trx == thd_to_trx(user_thd));
if (keynr != MAX_KEY && table->s->keys > 0) {
key = table->key_info + keynr;
......
......@@ -528,10 +528,12 @@ innobase_create_key_def(
key_info->name, "PRIMARY");
/* If there is a UNIQUE INDEX consisting entirely of NOT NULL
columns, MySQL will treat it as a PRIMARY KEY unless the
table already has one. */
columns and if the index does not contain column prefix(es)
(only prefix/part of the column is indexed), MySQL will treat the
index as a PRIMARY KEY unless the table already has one. */
if (!new_primary && (key_info->flags & HA_NOSAME)
&& (!(key_info->flags & HA_KEY_HAS_PART_KEY_SEG))
&& row_table_got_default_clust_index(table)) {
uint key_part = key_info->key_parts;
......
create table bug51378 (
col1 int not null,
col2 blob not null,
col3 time not null) engine = innodb;
create unique index idx on bug51378(col1, col2(31));
alter table bug51378 add unique index idx2(col1, col2(31));
create unique index idx3 on bug51378(col1, col3);
SHOW CREATE TABLE bug51378;
Table Create Table
bug51378 CREATE TABLE `bug51378` (
`col1` int(11) NOT NULL,
`col2` blob NOT NULL,
`col3` time NOT NULL,
UNIQUE KEY `idx3` (`col1`,`col3`),
UNIQUE KEY `idx` (`col1`,`col2`(31)),
UNIQUE KEY `idx2` (`col1`,`col2`(31))
) ENGINE=InnoDB DEFAULT CHARSET=latin1
drop index idx3 on bug51378;
SHOW CREATE TABLE bug51378;
Table Create Table
bug51378 CREATE TABLE `bug51378` (
`col1` int(11) NOT NULL,
`col2` blob NOT NULL,
`col3` time NOT NULL,
UNIQUE KEY `idx` (`col1`,`col2`(31)),
UNIQUE KEY `idx2` (`col1`,`col2`(31))
) ENGINE=InnoDB DEFAULT CHARSET=latin1
alter table bug51378 add primary key idx3(col1, col2(31));
SHOW CREATE TABLE bug51378;
Table Create Table
bug51378 CREATE TABLE `bug51378` (
`col1` int(11) NOT NULL,
`col2` blob NOT NULL,
`col3` time NOT NULL,
PRIMARY KEY (`col1`,`col2`(31)),
UNIQUE KEY `idx` (`col1`,`col2`(31)),
UNIQUE KEY `idx2` (`col1`,`col2`(31))
) ENGINE=InnoDB DEFAULT CHARSET=latin1
drop table bug51378;
create table bug51378 (
col1 int not null,
col2 blob not null,
col3 time not null, primary key(col1, col2(31))) engine = innodb;
create unique index idx on bug51378(col1, col2(31));
SHOW CREATE TABLE bug51378;
Table Create Table
bug51378 CREATE TABLE `bug51378` (
`col1` int(11) NOT NULL,
`col2` blob NOT NULL,
`col3` time NOT NULL,
PRIMARY KEY (`col1`,`col2`(31)),
UNIQUE KEY `idx` (`col1`,`col2`(31))
) ENGINE=InnoDB DEFAULT CHARSET=latin1
drop table bug51378;
create table bug51378 (
col1 int not null,
col2 int ) engine = innodb;
create unique index idx on bug51378(col1, col2);
SHOW CREATE TABLE bug51378;
Table Create Table
bug51378 CREATE TABLE `bug51378` (
`col1` int(11) NOT NULL,
`col2` int(11) DEFAULT NULL,
UNIQUE KEY `idx` (`col1`,`col2`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
drop table bug51378;
# This is the test for bug 51378. Unique index created
# through "create index" and "alter table add unique index"
# interfaces should not be treated as primary index if indexed
# columns contain one or more column prefix(es) (only prefix/part of
# the column is indexed)
# On the other hand, if there is a unique index covers all
# columns of a table, and they are non-null columns, and
# full length of the column are indexed, then this index
# will be created as primary index
# Following queries test various scenario, no mismatch
# error message should be printed.
--source include/have_innodb.inc
# Create a table contains a BLOB column
create table bug51378 (
col1 int not null,
col2 blob not null,
col3 time not null) engine = innodb;
# Create following unique indexes on 'col1' and 'col2(31)'
# of the table, the index should not be treated as primary
# key because it indexes only first 31 bytes of col2.
# Thus it contains "column prefix", and will not be
# upgraded to primary index.
# There should not be mismatch message printed in the
# errorlog
create unique index idx on bug51378(col1, col2(31));
alter table bug51378 add unique index idx2(col1, col2(31));
# Unique index on 'col1' and 'col3' will be created as primary index,
# since the index does not contain column prefix
create unique index idx3 on bug51378(col1, col3);
# Show create table would show idx3 created as unique index, internally,
# idx3 is treated as primary index both by MySQL and Innodb
SHOW CREATE TABLE bug51378;
# "GEN_CLUST_INDEX" will be re-created as default primary index
# after idx3 is dropped
drop index idx3 on bug51378;
SHOW CREATE TABLE bug51378;
# Or we can add the primary key through alter table interfaces
alter table bug51378 add primary key idx3(col1, col2(31));
SHOW CREATE TABLE bug51378;
drop table bug51378;
# Or we can create such primary key through create table interfaces
create table bug51378 (
col1 int not null,
col2 blob not null,
col3 time not null, primary key(col1, col2(31))) engine = innodb;
# Unique index on one or more column prefix(es) will be created
# as non-cluster index
create unique index idx on bug51378(col1, col2(31));
SHOW CREATE TABLE bug51378;
drop table bug51378;
# If a table has a NULLABLE column, unique index on it will not
# be treated as primary index.
create table bug51378 (
col1 int not null,
col2 int ) engine = innodb;
# This will be created as non-cluster index since col2 is nullable
create unique index idx on bug51378(col1, col2);
SHOW CREATE TABLE bug51378;
drop table bug51378;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment