Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can partition a table without its unique keys, leading to duplicate primary keys #57893

Closed
mjonss opened this issue Dec 2, 2024 · 1 comment
Assignees
Labels
affects-7.5 This bug affects the 7.5.x(LTS) versions. duplicate Issues or pull requests already exists. severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.

Comments

@mjonss
Copy link
Contributor

mjonss commented Dec 2, 2024

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

When using PARTITION BY, it allows partitioning on a columns that is not includind in the primary key, leading to not enforcing primary key uniqueness between partitions.

CREATE TABLE t (
a varchar(50) NOT NULL,
b varchar(50) NOT NULL,
c int,
PRIMARY KEY (a,b));

ALTER TABLE t
PARTITION BY RANGE (c) (
PARTITION p0 VALUES LESS THAN (1000000),
PARTITION p1 VALUES LESS THAN (maxvalue));

-- while this fails:
CREATE TABLE t2 (
a varchar(50) NOT NULL,
b varchar(50) NOT NULL,
c int,
PRIMARY KEY (a,b))
PARTITION BY RANGE (c) (
PARTITION p0 VALUES LESS THAN (1000000),
PARTITION p1 VALUES LESS THAN (maxvalue));
-- ERROR 1503 (HY000): A CLUSTERED INDEX must include all columns in the table's partitioning function

-- Which leads to allowing duplicate primary keys:
INSERT INTO t VALUES ('a','a',1), ('a','a',1000000);
SELECT * FROM t;

2. What did you expect to see? (Required)

ALTER TABLE t PARTITION BY RANGE (c) ... failing

3. What did you see instead (Required)

It succeeded, leading to allowing duplicate keys!

tidb> CREATE TABLE t (
    -> a varchar(50) NOT NULL,
    -> b varchar(50) NOT NULL,
    -> c int,
    -> PRIMARY KEY (a,b));
Query OK, 0 rows affected (0.08 sec)

tidb> ALTER TABLE t
    -> PARTITION BY RANGE (c) (
    -> PARTITION p0 VALUES LESS THAN (1000000),
    -> PARTITION p1 VALUES LESS THAN (maxvalue));
Query OK, 0 rows affected, 1 warning (1.06 sec)

tidb> CREATE TABLE t2 (
    -> a varchar(50) NOT NULL,
    -> b varchar(50) NOT NULL,
    -> c int,
    -> PRIMARY KEY (a,b))
    -> PARTITION BY RANGE (c) (
    -> PARTITION p0 VALUES LESS THAN (1000000),
    -> PARTITION p1 VALUES LESS THAN (maxvalue));
ERROR 1503 (HY000): A CLUSTERED INDEX must include all columns in the table's partitioning function

tidb> -- Which leads to allowing duplicate primary keys:
Query OK, 0 rows affected, 1 warning (0.00 sec)

tidb> INSERT INTO t VALUES ('a','a',1), ('a','a',1000000);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

tidb> SELECT * FROM t;
+---+---+---------+
| a | b | c       |
+---+---+---------+
| a | a |       1 |
| a | a | 1000000 |
+---+---+---------+
2 rows in set, 1 warning (0.01 sec)

4. What is your TiDB version? (Required)

@mjonss mjonss added the type/bug The issue is confirmed as a bug. label Dec 2, 2024
@dveeden dveeden added the component/tablepartition This issue is related to Table Partition of TiDB. label Dec 2, 2024
@mjonss mjonss added severity/major and removed component/tablepartition This issue is related to Table Partition of TiDB. labels Dec 2, 2024
@mjonss mjonss self-assigned this Dec 2, 2024
@mjonss mjonss removed may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.1 may-affects-6.5 labels Dec 2, 2024
@mjonss
Copy link
Contributor Author

mjonss commented Dec 2, 2024

Duplicate of #55721.

@mjonss mjonss closed this as completed Dec 2, 2024
@dveeden dveeden closed this as not planned Won't fix, can't repro, duplicate, stale Dec 2, 2024
@jebter jebter added duplicate Issues or pull requests already exists. sig/sql-infra SIG: SQL Infra labels Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-7.5 This bug affects the 7.5.x(LTS) versions. duplicate Issues or pull requests already exists. severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.
Projects
None yet
Development

No branches or pull requests

3 participants