Skip to content

ddl: the 'max-index-length' check does not respect non-restricted sql_mode #34931

@bb7133

Description

@bb7133

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

drop table if exists t;
set @@sql_mode='';
create table t (id int, name varchar(2048), index(name)) charset=utf8;

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

In MySQL 5.7 & MySQL 8:

mysql> set @@sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql> create table t (id int, name varchar(2048), index(name)) charset=utf8;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> show warnings;
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                     |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 3719 | 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. |
| Warning | 1071 | Specified key was too long; max key length is 3072 bytes                                                                                                                    |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show create table t;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                          |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| t     | CREATE TABLE `t` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(2048) DEFAULT NULL,
  KEY `name` (`name`(1024))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Notice that in non-strict SQL mode, MySQL converted the error to warning and truncated the index name as a prefix one("name(1024)"), which met the maximum length of index(3072 bytes / 3 bytes per utf8 encoding = 1024).

3. What did you see instead (Required)

In TiDB(all versions with default configuration):

mysql> set @@sql_mode='';
Query OK, 0 rows affected (0.00 sec)

tidb> create table t (id int, name varchar(2048), index(name)) charset=utf8;
ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytes

4. What is your TiDB version? (Required)

tidb> select version();
+--------------------+
| version()          |
+--------------------+
| 5.7.25-TiDB-v6.0.0 |
+--------------------+
1 row in set (0.00 sec)

I am using v6.0 but it applies to all existing versions.

Metadata

Metadata

Assignees

Labels

help wantedDenotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.severity/moderatesig/sql-infraSIG: SQL Infratype/bugThe issue is confirmed as a bug.type/compatibility

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions