Commit f19cbe50 authored by marko's avatar marko

branches/zip: ha_innobase::prepare_drop_index(): When there is a

foreign key constraint, find a truly equivalent index for it.
If none is available, refuse to drop the index.  MySQL can drop
an index when creating a "stronger" index.

This was reported as Mantis issue #70 and MySQL Bug #38786.

innodb-index.test: Add a test case.

dict_foreign_find_equiv_index(): New function, to replace the
incorrectly written function dict_table_find_equivalent_index().

dict_table_replace_index_in_foreign_list(): Simplify the implementation.
parent 97be6faa
......@@ -2162,6 +2162,30 @@ next_rec:
return(NULL);
}
/**************************************************************************
Find an index that is equivalent to the one passed in and is not marked
for deletion. */
UNIV_INTERN
dict_index_t*
dict_foreign_find_equiv_index(
/*==========================*/
/* out: index equivalent to
foreign->foreign_index, or NULL */
dict_foreign_t* foreign)/* in: foreign key */
{
ut_a(foreign != NULL);
/* Try to find an index which contains the columns as the
first fields and in the right order, and the types are the
same as in foreign->foreign_index */
return(dict_foreign_find_index(
foreign->foreign_table,
foreign->foreign_col_names, foreign->n_fields,
foreign->foreign_index, TRUE, /* check types */
FALSE/* allow columns to be NULL */));
}
/**************************************************************************
Returns an index object by matching on the name and column names and
if more than one index matches return the index with the max id */
......@@ -4485,43 +4509,6 @@ dict_table_get_index_on_name(
}
/**************************************************************************
Find an index that is equivalent to the one passed in and is not marked
for deletion. */
UNIV_INTERN
dict_index_t*
dict_table_find_equivalent_index(
/*=============================*/
/* out: equivalent index, or NULL */
dict_table_t* table, /* in: table */
dict_index_t* index) /* in: index to match */
{
ulint i;
const char** column_names;
dict_index_t* equiv_index;
if (UT_LIST_GET_LEN(table->foreign_list) == 0) {
return(NULL);
}
column_names = mem_alloc(index->n_fields * sizeof *column_names);
/* Convert the column names to the format & type accepted by the find
index function */
for (i = 0; i < index->n_fields; i++) {
column_names[i] = index->fields[i].name;
}
equiv_index = dict_foreign_find_index(
table, column_names, index->n_fields,
index, TRUE, FALSE);
mem_free((void*) column_names);
return(equiv_index);
}
/**************************************************************************
Replace the index passed in with another equivalent index in the tables
foreign key list. */
......@@ -4532,30 +4519,18 @@ dict_table_replace_index_in_foreign_list(
dict_table_t* table, /* in/out: table */
dict_index_t* index) /* in: index to be replaced */
{
dict_index_t* new_index;
new_index = dict_table_find_equivalent_index(table, index);
/* If match found */
if (new_index) {
dict_foreign_t* foreign;
ut_a(new_index != index);
foreign = UT_LIST_GET_FIRST(table->foreign_list);
/* If the list is not empty then this should hold */
ut_a(foreign);
/* Iterate over the foreign index list and replace the index
passed in with the new index */
while (foreign) {
for (foreign = UT_LIST_GET_FIRST(table->foreign_list);
foreign;
foreign = UT_LIST_GET_NEXT(foreign_list, foreign)) {
if (foreign->foreign_index == index) {
foreign->foreign_index = new_index;
}
dict_index_t* new_index
= dict_foreign_find_equiv_index(foreign);
ut_a(new_index);
foreign = UT_LIST_GET_NEXT(foreign_list, foreign);
foreign->foreign_index = new_index;
}
}
}
......
......@@ -983,15 +983,16 @@ ha_innobase::prepare_drop_index(
if (trx->check_foreigns
&& thd_sql_command(user_thd) != SQLCOM_CREATE_INDEX) {
dict_index_t* index
= dict_table_get_first_index(prebuilt->table);
dict_index_t* index;
do {
for (index = dict_table_get_first_index(prebuilt->table);
index;
index = dict_table_get_next_index(index)) {
dict_foreign_t* foreign;
if (!index->to_be_dropped) {
goto next_index;
continue;
}
/* Check if the index is referenced. */
......@@ -1019,20 +1020,61 @@ index_needed:
ut_a(foreign->foreign_index == index);
/* Search for an equivalent index that
the foreign key contraint could use
the foreign key constraint could use
if this index were to be deleted. */
if (!dict_table_find_equivalent_index(
prebuilt->table,
foreign->foreign_index)) {
if (!dict_foreign_find_equiv_index(
foreign)) {
goto index_needed;
}
}
}
}
} else if (thd_sql_command(user_thd) == SQLCOM_CREATE_INDEX) {
/* This is a drop of a foreign key constraint index that
was created by MySQL when the constraint was added. MySQL
does this when the user creates an index explicitly which
can be used in place of the automatically generated index. */
next_index:
index = dict_table_get_next_index(index);
} while (index);
dict_index_t* index;
for (index = dict_table_get_first_index(prebuilt->table);
index;
index = dict_table_get_next_index(index)) {
dict_foreign_t* foreign;
if (!index->to_be_dropped) {
continue;
}
/* Check if this index references some other table */
foreign = dict_table_get_foreign_constraint(
prebuilt->table, index);
if (foreign == NULL) {
continue;
}
ut_a(foreign->foreign_index == index);
/* Search for an equivalent index that the
foreign key constraint could use if this index
were to be deleted. */
if (!dict_foreign_find_equiv_index(foreign)) {
trx_set_detailed_error(
trx,
"Index needed in foreign key "
"constraint");
trx->error_info = foreign->foreign_index;
err = HA_ERR_DROP_INDEX_FK;
break;
}
}
}
func_exit:
......
......@@ -420,6 +420,16 @@ dict_table_get_on_id_low(
/* out: table, NULL if does not exist */
dulint table_id); /* in: table id */
/**************************************************************************
Find an index that is equivalent to the one passed in and is not marked
for deletion. */
UNIV_INTERN
dict_index_t*
dict_foreign_find_equiv_index(
/*==========================*/
/* out: index equivalent to
foreign->foreign_index, or NULL */
dict_foreign_t* foreign);/* in: foreign key */
/**************************************************************************
Returns an index object by matching on the name and column names and if
more than index is found return the index with the higher id.*/
UNIV_INTERN
......@@ -1085,16 +1095,6 @@ dict_table_get_index_on_name(
dict_table_t* table, /* in: table */
const char* name); /* in: name of the index to find */
/**************************************************************************
Find an index that is equivalent to the one passed in and is not marked
for deletion. */
UNIV_INTERN
dict_index_t*
dict_table_find_equivalent_index(
/*=============================*/
/* out: equivalent index, or NULL */
dict_table_t* table, /* in: table */
dict_index_t* index); /* in: index to match */
/**************************************************************************
In case there is more than one index with the same name return the index
with the min(id). */
UNIV_INTERN
......
......@@ -960,3 +960,167 @@ t1 CREATE TABLE `t1` (
KEY `t1st` (`s`(1),`t`(1))
) ENGINE=InnoDB DEFAULT CHARSET=latin1
drop table t1;
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
CREATE TABLE t1(
c1 BIGINT(12) NOT NULL,
PRIMARY KEY (c1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE t2(
c1 BIGINT(16) NOT NULL,
c2 BIGINT(12) NOT NULL,
c3 BIGINT(12) NOT NULL,
PRIMARY KEY (c1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3) REFERENCES t1(c1);
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bigint(16) NOT NULL,
`c2` bigint(12) NOT NULL,
`c3` bigint(12) NOT NULL,
PRIMARY KEY (`c1`),
KEY `fk_t2_ca` (`c3`),
CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`) REFERENCES `t1` (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bigint(16) NOT NULL,
`c2` bigint(12) NOT NULL,
`c3` bigint(12) NOT NULL,
PRIMARY KEY (`c1`),
KEY `i_t2_c3_c2` (`c3`,`c2`),
CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`) REFERENCES `t1` (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
INSERT INTO t2 VALUES(0,0,0);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`) REFERENCES `t1` (`c1`))
INSERT INTO t1 VALUES(0);
INSERT INTO t2 VALUES(0,0,0);
DROP TABLE t2;
CREATE TABLE t2(
c1 BIGINT(16) NOT NULL,
c2 BIGINT(12) NOT NULL,
c3 BIGINT(12) NOT NULL,
PRIMARY KEY (c1,c2,c3)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3) REFERENCES t1(c1);
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bigint(16) NOT NULL,
`c2` bigint(12) NOT NULL,
`c3` bigint(12) NOT NULL,
PRIMARY KEY (`c1`,`c2`,`c3`),
KEY `fk_t2_ca` (`c3`),
CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`) REFERENCES `t1` (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bigint(16) NOT NULL,
`c2` bigint(12) NOT NULL,
`c3` bigint(12) NOT NULL,
PRIMARY KEY (`c1`,`c2`,`c3`),
KEY `i_t2_c3_c2` (`c3`,`c2`),
CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`) REFERENCES `t1` (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO t2 VALUES(0,0,1);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`) REFERENCES `t1` (`c1`))
INSERT INTO t2 VALUES(0,0,0);
DELETE FROM t1;
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`) REFERENCES `t1` (`c1`))
DELETE FROM t2;
DROP TABLE t2;
DROP TABLE t1;
CREATE TABLE t1(
c1 BIGINT(12) NOT NULL,
c2 INT(4) NOT NULL,
PRIMARY KEY (c2,c1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE t2(
c1 BIGINT(16) NOT NULL,
c2 BIGINT(12) NOT NULL,
c3 BIGINT(12) NOT NULL,
PRIMARY KEY (c1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c1);
ERROR HY000: Can't create table '#sql-temporary' (errno: 150)
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
ERROR HY000: Can't create table '#sql-temporary' (errno: 150)
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
ERROR HY000: Can't create table '#sql-temporary' (errno: 150)
ALTER TABLE t1 MODIFY COLUMN c2 BIGINT(12) NOT NULL;
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
ERROR HY000: Can't create table '#sql-temporary' (errno: 150)
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` bigint(12) NOT NULL,
`c2` bigint(12) NOT NULL,
PRIMARY KEY (`c2`,`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bigint(16) NOT NULL,
`c2` bigint(12) NOT NULL,
`c3` bigint(12) NOT NULL,
PRIMARY KEY (`c1`),
KEY `fk_t2_ca` (`c3`,`c2`),
CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`, `c2`) REFERENCES `t1` (`c2`, `c1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE INDEX i_t2_c2_c1 ON t2(c2, c1);
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bigint(16) NOT NULL,
`c2` bigint(12) NOT NULL,
`c3` bigint(12) NOT NULL,
PRIMARY KEY (`c1`),
KEY `fk_t2_ca` (`c3`,`c2`),
KEY `i_t2_c2_c1` (`c2`,`c1`),
CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`, `c2`) REFERENCES `t1` (`c2`, `c1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE INDEX i_t2_c3_c1_c2 ON t2(c3, c1, c2);
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bigint(16) NOT NULL,
`c2` bigint(12) NOT NULL,
`c3` bigint(12) NOT NULL,
PRIMARY KEY (`c1`),
KEY `fk_t2_ca` (`c3`,`c2`),
KEY `i_t2_c2_c1` (`c2`,`c1`),
KEY `i_t2_c3_c1_c2` (`c3`,`c1`,`c2`),
CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`, `c2`) REFERENCES `t1` (`c2`, `c1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bigint(16) NOT NULL,
`c2` bigint(12) NOT NULL,
`c3` bigint(12) NOT NULL,
PRIMARY KEY (`c1`),
KEY `i_t2_c2_c1` (`c2`,`c1`),
KEY `i_t2_c3_c1_c2` (`c3`,`c1`,`c2`),
KEY `i_t2_c3_c2` (`c3`,`c2`),
CONSTRAINT `fk_t2_ca` FOREIGN KEY (`c3`, `c2`) REFERENCES `t1` (`c2`, `c1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
DROP TABLE t2;
DROP TABLE t1;
......@@ -387,3 +387,114 @@ create index t1ut on t1 (u(1), t(1));
create index t1st on t1 (s(1), t(1));
show create table t1;
drop table t1;
#
# Test to check whether CREATE INDEX handles implicit foreign key
# constraint modifications (Issue #70, Bug #38786)
#
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
CREATE TABLE t1(
c1 BIGINT(12) NOT NULL,
PRIMARY KEY (c1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE t2(
c1 BIGINT(16) NOT NULL,
c2 BIGINT(12) NOT NULL,
c3 BIGINT(12) NOT NULL,
PRIMARY KEY (c1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3) REFERENCES t1(c1);
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
SHOW CREATE TABLE t2;
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
SHOW CREATE TABLE t2;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
--error ER_NO_REFERENCED_ROW_2
INSERT INTO t2 VALUES(0,0,0);
INSERT INTO t1 VALUES(0);
INSERT INTO t2 VALUES(0,0,0);
DROP TABLE t2;
CREATE TABLE t2(
c1 BIGINT(16) NOT NULL,
c2 BIGINT(12) NOT NULL,
c3 BIGINT(12) NOT NULL,
PRIMARY KEY (c1,c2,c3)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3) REFERENCES t1(c1);
SHOW CREATE TABLE t2;
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
SHOW CREATE TABLE t2;
--error ER_NO_REFERENCED_ROW_2
INSERT INTO t2 VALUES(0,0,1);
INSERT INTO t2 VALUES(0,0,0);
--error ER_ROW_IS_REFERENCED_2
DELETE FROM t1;
DELETE FROM t2;
DROP TABLE t2;
DROP TABLE t1;
CREATE TABLE t1(
c1 BIGINT(12) NOT NULL,
c2 INT(4) NOT NULL,
PRIMARY KEY (c2,c1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE t2(
c1 BIGINT(16) NOT NULL,
c2 BIGINT(12) NOT NULL,
c3 BIGINT(12) NOT NULL,
PRIMARY KEY (c1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--replace_regex /'test\.#sql-[0-9a-f-]*_1'/'#sql-temporary'/
--error ER_CANT_CREATE_TABLE
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c1);
--replace_regex /'test\.#sql-[0-9a-f-]*_1'/'#sql-temporary'/
--error ER_CANT_CREATE_TABLE
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
--replace_regex /'test\.#sql-[0-9a-f-]*_1'/'#sql-temporary'/
--error ER_CANT_CREATE_TABLE
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
ALTER TABLE t1 MODIFY COLUMN c2 BIGINT(12) NOT NULL;
--replace_regex /'test\.#sql-[0-9a-f-]*_1'/'#sql-temporary'/
--error ER_CANT_CREATE_TABLE
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
SHOW CREATE TABLE t1;
SHOW CREATE TABLE t2;
CREATE INDEX i_t2_c2_c1 ON t2(c2, c1);
SHOW CREATE TABLE t2;
CREATE INDEX i_t2_c3_c1_c2 ON t2(c3, c1, c2);
SHOW CREATE TABLE t2;
CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;
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