Commit 9a645dae authored by Nikita Malyavin's avatar Nikita Malyavin

MDEV-23632 ALTER TABLE...ADD KEY creates corrupted index on virtual column

mysql_col_offset was not updated after the new column has been added by an
INSTANT ALTER TABLE -- table data dictionary had been remaining the same.

When the virtual column is added or removed, table was usually evicted and
then reopened, which triggered vcol info rebuild on the next open.

However this also should be done when the usual column is added or removed:
mariadb always stores virtual field at the end of maria record,
so the shift should always happen.

Fix:
expand the eviction condition to the case when usual fields are
added/removed

Note:
this should happen only in the case of !new_clustered:
* When new_clustered is true, a new data dictionary is created, and vcol
metadata is rebuilt in `alter_rebuild_apply_log()`
* We can't do it in `new_clustered` case, because the old table is not yet
subctituted correctly
parent f0baa864
......@@ -62,3 +62,28 @@ INSERT INTO t1 (i) VALUES (1),(2);
SELECT * FROM t1 WHERE y BETWEEN 2012 AND 2016 FOR UPDATE;
y i b vi
DROP TABLE t1;
#
# MDEV-23632 ALTER TABLE...ADD KEY creates corrupted index on virtual column
#
CREATE TABLE t1(a INT PRIMARY KEY, b INT, g INT GENERATED ALWAYS AS(b)VIRTUAL) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1,1,default);
ALTER TABLE t1 ADD COLUMN c INT;
ALTER TABLE t1 ADD KEY(g);
CHECK TABLE t1;
Table Op Msg_type Msg_text
test.t1 check status OK
SELECT g FROM t1 FORCE INDEX (g);
g
1
DROP TABLE t1;
CREATE TABLE t1(a INT, b INT, g INT GENERATED ALWAYS AS(b)VIRTUAL) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1,1,default);
ALTER TABLE t1 ADD COLUMN c INT PRIMARY KEY;
ALTER TABLE t1 ADD KEY(g);
CHECK TABLE t1;
Table Op Msg_type Msg_text
test.t1 check status OK
SELECT g FROM t1 FORCE INDEX (g);
g
1
DROP TABLE t1;
......@@ -52,3 +52,23 @@ SELECT * FROM t1 WHERE y BETWEEN 2012 AND 2016 FOR UPDATE;
INSERT INTO t1 (i) VALUES (1),(2);
SELECT * FROM t1 WHERE y BETWEEN 2012 AND 2016 FOR UPDATE;
DROP TABLE t1;
--echo #
--echo # MDEV-23632 ALTER TABLE...ADD KEY creates corrupted index on virtual column
--echo #
CREATE TABLE t1(a INT PRIMARY KEY, b INT, g INT GENERATED ALWAYS AS(b)VIRTUAL) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1,1,default);
ALTER TABLE t1 ADD COLUMN c INT;
ALTER TABLE t1 ADD KEY(g);
CHECK TABLE t1;
SELECT g FROM t1 FORCE INDEX (g);
DROP TABLE t1;
CREATE TABLE t1(a INT, b INT, g INT GENERATED ALWAYS AS(b)VIRTUAL) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1,1,default);
ALTER TABLE t1 ADD COLUMN c INT PRIMARY KEY; # Triggers `new_clustered`
ALTER TABLE t1 ADD KEY(g);
CHECK TABLE t1;
SELECT g FROM t1 FORCE INDEX (g);
DROP TABLE t1;
......@@ -9899,7 +9899,10 @@ ha_innobase::commit_inplace_alter_table(
}
}
if (ctx0->num_to_drop_vcol || ctx0->num_to_add_vcol) {
if (ctx0->num_to_drop_vcol || ctx0->num_to_add_vcol
|| (ctx0->new_table->n_v_cols && !new_clustered
&& (ha_alter_info->alter_info->drop_list.elements
|| ha_alter_info->alter_info->create_list.elements))) {
DBUG_ASSERT(ctx0->old_table->get_ref_count() == 1);
trx_commit_for_mysql(m_prebuilt->trx);
......
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