MDEV-42172: Reject EXCHANGE PARTITION with inconsistent key order #6262
Why don't you just fix mysql_compare_tables() instead of adding another pass? It's used for partitions exclusively.
Thank you for your contribution! This is a preliminary review.
Technically this meets the preliminary review success criteria, so LGTM.
But I have a serious optimization proposal below that I believe should be considered.
Thanks! I'll fold the ordered unique-key comparison into
mysql_compare_tables()'s existing key-comparison loop and drop the extra pass
from sql_partition_admin.cc. The two HA_DUPLICATE_KEY_NOT_IN_ORDER checks will
stay at the call site, since the partition handler isn't passed to
mysql_compare_tables(). I'll rerun the MTR coverage and update the PR.
FYI: According to our development cycle we work on bugs In the following periods 15 Mar-30 Apr, 15 Jun-30 Jul, 15 Sep-30 Oct and 15 Dec-31 Jan. So, please, expect to get a review somewhere between these two dates and the goal is to have your PR merged before the second date
gkodinov
Huxley93
MDEV-42172: Reject EXCHANGE PARTITION with inconsistent unique-key order
Problem
ALTER TABLE ... EXCHANGE PARTITIONcompares unique indexes by name anddefinition, but not by their relative order. A non-partitioned table whose
engine-visible unique indexes have the same definitions in a different order
can therefore be exchanged into a partitioned InnoDB table.
The exchanged-in partition keeps the dictionary order of the original
non-partitioned table, while the partitioned table keeps its
.frmkey order.InnoDB then marks only that partition with
HA_DUPLICATE_KEY_NOT_IN_ORDER. Becauseha_partition::open()requires allpartitions to agree on this flag, the next full-table open fails with
HA_ERR_INITIALIZATION(error 174).SELECT,SHOW CREATE TABLE, andCHECK TABLEare affected.The non-partitioned table is changed symmetrically: it receives the old
partition's dictionary but keeps its own
.frmdefinition.Fix
Reject the exchange when either handler already has
HA_DUPLICATE_KEY_NOT_IN_ORDER. For the ordered sequence of unique keys,mysql_compare_tables()now walks the existing table keys by name and recordsthe positions of engine-visible unique keys in the prepared partition
definition. Those positions must increase; a mismatch makes the metadata
comparison fail before the DDL log or rename is written.
The comparison uses
HA_NOSAMEfromTABLE::key_info, so long unique keysthat InnoDB does not expose as unique dictionary indexes remain excluded.
Non-unique key order is not compared. The failure uses the existing
ER_TABLES_DIFFERENT_METADATAerror.This keeps the ordered comparison in the existing metadata-comparison pass;
partition flag aggregation is unchanged.
Tests
Add
mysql-test/suite/parts/t/partition_exchange_unique_order.testwith:both tables after the exchange.
Validation uses full Debug source builds and MTR BEFORE/AFTER controls on
10.11:
error 174 and shows the symmetric dictionary-order change; and
data-integrity assertions, and passes the existing
partition_exchange_innodbregression.The earlier separate-pass implementation also passed the same checks on 10.11,
11.4, 11.8, 12.3, and 13.0. After folding the check into
mysql_compare_tables(), the 10.11 BEFORE/AFTER and existing regression werererun successfully.
A local official MariaDB 10.11.16 Windows ZIP preflight was used only for
initial triage; the source-build MTR results above are the primary evidence.
<details>
<summary>Commit message</summary>
</details>
Licensing
I license this contribution under the 3-clause BSD license, as permitted by
MariaDB's community contribution process.
Target branch
Target 10.11, the lowest maintained GA branch on which the failure was
reproduced. Happy to retarget if requested.