tidb icon indicating copy to clipboard operation
tidb copied to clipboard

traverse table constraints and set column flag errors

Open ciscoxll opened this issue 8 years ago • 3 comments

Please answer these questions before submitting your issue. Thanks!

  1. What did you do? If possible, provide a recipe for reproducing the error.

    An error was found in the set Column Flag MultipleKeyFlag.

  2. What did you expect to see?

  • mysql
mysql> CREATE TABLE t3 (
    ->    col1 INT NOT NULL,
    ->    col2 DATE NOT NULL,
    ->    col3 INT NOT NULL,
    ->    col4 INT NOT NULL,
    ->    UNIQUE KEY (col1, col2, col3)
    ->   )
    -> PARTITION BY RANGE( col3 ) (
    ->   PARTITION p1 VALUES LESS THAN (11),
    ->   PARTITION p2 VALUES LESS THAN (15)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> SHOW COLUMNS FROM t3;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1  | int(11) | NO   | PRI | NULL    |       |
| col2  | date    | NO   | PRI | NULL    |       |
| col3  | int(11) | NO   | PRI | NULL    |       |
| col4  | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
4 rows in set (0.03 sec)
  1. What did you see instead?
  • tidb
mysql> CREATE TABLE t3 (
    ->    col1 INT NOT NULL,
    ->    col2 DATE NOT NULL,
    ->    col3 INT NOT NULL,
    ->    col4 INT NOT NULL,
    ->    UNIQUE KEY (col1, col2, col3)
    ->   )
    -> PARTITION BY RANGE( col3 ) (
    ->   PARTITION p1 VALUES LESS THAN (11),
    ->   PARTITION p2 VALUES LESS THAN (15)
    -> );
Query OK, 0 rows affected (0.13 sec)

mysql> SHOW COLUMNS FROM t3;
+-------+---------+------+------+---------+-------+
| Field | Type    | Null | Key  | Default | Extra |
+-------+---------+------+------+---------+-------+
| col1  | int(11) | NO   | MUL  | NULL    |       |
| col2  | date    | NO   |      | NULL    |       |
| col3  | int(11) | NO   |      | NULL    |       |
| col4  | int(11) | NO   |      | NULL    |       |
+-------+---------+------+------+---------+-------+
4 rows in set (0.00 sec)
  • tidb log
2018/07/24 19:57:20.314 partition.go:290: [info] col1 HasUniKeyFlag :false HasMultipleKeyFlag :true HasPriKeyFlag :false
2018/07/24 19:57:20.314 partition.go:290: [info] col2 HasUniKeyFlag :false HasMultipleKeyFlag :false HasPriKeyFlag :false
2018/07/24 19:57:20.314 partition.go:290: [info] col3 HasUniKeyFlag :false HasMultipleKeyFlag :false HasPriKeyFlag :false
2018/07/24 19:57:20.314 partition.go:290: [info] col4 HasUniKeyFlag :false HasMultipleKeyFlag :false HasPriKeyFlag :false
  1. What version of TiDB are you using (tidb-server -V or run select tidb_version(); on TiDB)?
  • master

ciscoxll avatar Jul 24 '18 12:07 ciscoxll

Pasteable testcase:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (
 col1 INT NOT NULL,
 col2 DATE NOT NULL,
 col3 INT NOT NULL,
 col4 INT NOT NULL,
 UNIQUE KEY (col1, col2, col3)
)
 PARTITION BY RANGE( col3 ) (
 PARTITION p1 VALUES LESS THAN (11),
 PARTITION p2 VALUES LESS THAN (15)
);
SHOW COLUMNS FROM t1;

Confirming this is still an issue:

tidb> SHOW COLUMNS FROM t1;
+-------+---------+------+------+---------+-------+
| Field | Type    | Null | Key  | Default | Extra |
+-------+---------+------+------+---------+-------+
| col1  | int(11) | NO   | MUL  | NULL    |       |
| col2  | date    | NO   |      | NULL    |       |
| col3  | int(11) | NO   |      | NULL    |       |
| col4  | int(11) | NO   |      | NULL    |       |
+-------+---------+------+------+---------+-------+
4 rows in set (0.00 sec)

tidb> SELECT tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v4.0.0-beta.2-183-g57db6cec7
Git Commit Hash: 57db6cec7ffad78e74b6ac6c67a2bfe9a6718d17
Git Branch: master
UTC Build Time: 2020-04-04 08:06:18
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
1 row in set (0.01 sec)

I am not sure if the MySQL behavior is correct for TiDB: InnoDB will promote the first unique not null index to primary key. Does TiDB create it's surrogate handle here instead though? In either case, the result is still wrong for TiDB. It could show MUL for each of the columns in the unique index.

wwar avatar Apr 10 '20 18:04 wwar

I am not sure if the MySQL behavior is correct for TiDB: InnoDB will promote the first unique not null index to primary key. Does TiDB create it's surrogate handle here instead though?

No, there's no implicit primary key in TiDB.

In either case, the result is still wrong for TiDB. It could show MUL for each of the columns in the unique index.

Yeah this is the bug we need to fix.

bb7133 avatar Aug 25 '20 05:08 bb7133

@nullnotnil

In either case, the result is still wrong for TiDB. It could show MUL for each of the columns in the unique index.

Here is the output of MySQL:

mysql> CREATE TABLE t1 (  col1 INT,  col2 DATE NOT NULL,  col3 INT NOT NULL,  col4 INT NOT NULL,  UNIQUE KEY (col1, col2, col3));
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW COLUMNS FROM t1;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| col1  | int  | YES  | MUL | NULL    |       |
| col2  | date | NO   |     | NULL    |       |
| col3  | int  | NO   |     | NULL    |       |
| col4  | int  | NO   |     | NULL    |       |
+-------+------+------+-----+---------+-------+
4 rows in set (0.01 sec)

Showing only the first column is intended: https://github.com/pingcap/tidb/blob/6785ae626a442327a1dece38ef34bca42c4b4e0d/ddl/ddl_api.go#L258-L278

wjhuang2016 avatar Nov 04 '20 07:11 wjhuang2016