← Back to Pull requests

MDEV-42172: Reject EXCHANGE PARTITION with inconsistent key order #6262

Open
Huxley93 opened this pull request with 285 lines diff across 4 files
View on GitHub
Huxley93 Huxley93 commented on Nov 28, 2026 05:09 UTC
Author

MDEV-42172: Reject EXCHANGE PARTITION with inconsistent unique-key order

Problem

ALTER TABLE ... EXCHANGE PARTITION compares unique indexes by name and
definition, 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 .frm key order.
InnoDB then marks only that partition with
HA_DUPLICATE_KEY_NOT_IN_ORDER. Because ha_partition::open() requires all
partitions to agree on this flag, the next full-table open fails with
HA_ERR_INITIALIZATION (error 174). SELECT, SHOW CREATE TABLE, and
CHECK TABLE are affected.

The non-partitioned table is changed symmetrically: it receives the old
partition's dictionary but keeps its own .frm definition.

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 records
the 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_NOSAME from TABLE::key_info, so long unique keys
that 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_METADATA error.

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.test with:

  • rejection of a unique-key order mismatch without changing either table;

  • acceptance of identical engine-visible unique-key order;

  • acceptance of non-unique key order differences;

  • rejection when all partitions of the partitioned table are already flagged;

  • rejection when the exchange table itself is flagged; and

  • acceptance of long-unique order differences, with row movement checked on

  • both tables after the exchange.

    Validation uses full Debug source builds and MTR BEFORE/AFTER controls on
    10.11:

  • the unpatched build accepts the exchange, then fails full-table open with

  • error 174 and shows the symmetric dictionary-order change; and
  • the patched build rejects all three inconsistent-order cases, passes the six

  • data-integrity assertions, and passes the existing
    partition_exchange_innodb regression.

    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 were
    rerun 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>

    MDEV-42172: Reject EXCHANGE PARTITION with inconsistent key order
    
    EXCHANGE PARTITION accepts tables whose engine-visible unique keys have
    the same names and definitions in a different order. The exchange then
    leaves partitions with different HA_DUPLICATE_KEY_NOT_IN_ORDER flags,
    making the partitioned table fail to open with error 174.
    
    Reject the exchange when either handler has that flag or when the ordered
    TABLE-layer unique-key names differ. Non-unique and long-unique key order
    remain outside the comparison.
    
    Add MTR coverage for rejection without data changes, matching unique
    order, non-unique order, both flag cases, and long-unique order.
    
    MDEV-42172: Compare unique key order during metadata comparison
    
    Fold the ordered engine-visible unique-key check into mysql_compare_tables()'s
    existing key loop. Keep HA_DUPLICATE_KEY_NOT_IN_ORDER checks at the partition
    exchange call site.
    

    </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.

    Diff: 285 lines (+285 / -0) across 4 files Reviewer: gkodinov Review duration: 12132m
    All Vouch review confidence checks passed
    Residual risk 0.06 = Change Risk (0.41) × (1 − Review Confidence (0.86)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.41 · 285 lines diff across 4 files
    0.41
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.90 · 3 substantive comment(s)
    0.90
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    85%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.338
    Real PR Conversation Timeline
    5 items
    gkodinov gkodinov commented on code on Dec 02, 2026 12:28 UTC
    Code Review
    sql/sql_partition_admin.cc:1
    @@ -184,6 +184,34 @@ static bool check_exchange_partition(TABLE *table, TABLE *part_table)
     }
     
     
    +static bool unique_key_order_matches(const TABLE *table,

    Why don't you just fix mysql_compare_tables() instead of adding another pass? It's used for partitions exclusively.

    gkodinov gkodinov approved these changes on Dec 02, 2026 12:30 UTC
    Approved Reviewer

    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.

    Huxley93 Huxley93 commented on Dec 02, 2026 12:39 UTC
    Author

    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.

    gkodinov gkodinov commented on Dec 02, 2026 13:38 UTC
    Contributor

    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

    Reviewers
    Assignees
    Huxley93 Huxley93
    Labels
    risk: low confidence: 85.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 41.2% (MEDIUM) · Higher = larger blast radius
    41.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 85.6% · Lower = rushed or shallow review
    85.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 5.9% · Safe to merge
    5.9%
    Repository