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

TiDB does not trim space from ENUM DEFAULT definition #29327

Closed
Alkaagr81 opened this issue Nov 1, 2021 · 10 comments · Fixed by #30356
Closed

TiDB does not trim space from ENUM DEFAULT definition #29327

Alkaagr81 opened this issue Nov 1, 2021 · 10 comments · Fixed by #30356
Assignees
Labels
severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.

Comments

@Alkaagr81
Copy link
Collaborator

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

drop table if exists t1;
CREATE TABLE `t1` (   `a` enum('','a','b') NOT NULL DEFAULT 'b' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
show create table t1;
drop table if exists t1;
CREATE TABLE `t1` (   `a` enum('','a','b ') NOT NULL DEFAULT 'b ' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
show create table t1;

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

mysql> drop table if exists t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE `t1` (   `a` enum('','a','b') NOT NULL DEFAULT 'b' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t1;
+-------+------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                       |
+-------+------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `a` enum('','a','b') NOT NULL DEFAULT 'b'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> drop table if exists t1;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `t1` (   `a` enum('','a','b ') NOT NULL DEFAULT 'b ' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t1;
+-------+------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                       |
+-------+------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `a` enum('','a','b') NOT NULL DEFAULT 'b'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

3. What did you see instead (Required)

mysql> drop table if exists t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE `t1` (   `a` enum('','a','b') NOT NULL DEFAULT 'b' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Query OK, 0 rows affected (0.13 sec)

mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                  |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `a` enum('','a','b') COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'b'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> drop table if exists t1;
Query OK, 0 rows affected (0.23 sec)

mysql> CREATE TABLE `t1` (   `a` enum('','a','b ') NOT NULL DEFAULT 'b ' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ERROR 1067 (42000): Invalid default value for 'a'
mysql> show create table t1;
ERROR 1146 (42S02): Table 'test.t1' doesn't exist

4. What is your TiDB version? (Required)

| Release Version: v5.2.2
Edition: Community
Git Commit Hash: da1c21fd45a4ea5900ac16d2f4a248143f378d18
Git Branch: heads/refs/tags/v5.2.2
UTC Build Time: 2021-10-20 06:03:45
GoVersion: go1.16.5
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
@Alkaagr81 Alkaagr81 added the type/bug The issue is confirmed as a bug. label Nov 1, 2021
@morgo
Copy link
Contributor

morgo commented Nov 1, 2021

Verified against master. The specific issue is the default value is not trimmed for comparison to the list of possible values:

tidb> create table t1 (a enum (' ','a','b ') not null default 'b ');
ERROR 1067 (42000): Invalid default value for 'a'
tidb> create table t1 (a enum (' ','a','b ') not null default 'b');
Query OK, 0 rows affected (0.12 sec)

@morgo morgo added help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. labels Nov 1, 2021
@morgo morgo changed the title MySQL does is it trims trailing space from the definition and in TIDB ERROR 1067 (42000): Invalid default value for 'a' TiDB does not trim space from ENUM DEFAULT definition Nov 1, 2021
@xiongjiwei xiongjiwei self-assigned this Nov 2, 2021
@xiongjiwei
Copy link
Contributor

similar with #18949

@morgo
Copy link
Contributor

morgo commented Nov 2, 2021

similar with #18949

It looks like it was an incomplete fix when accounting for DEFAULT.

@c4pt0r
Copy link
Member

c4pt0r commented Nov 15, 2021

Is there anyone working on this issue, if not, I will fix this :)

@xiongjiwei xiongjiwei assigned c4pt0r and unassigned xiongjiwei Nov 15, 2021
@xiongjiwei
Copy link
Contributor

xiongjiwei commented Nov 15, 2021

when the default value has the tailing space, MySQL will trim the space. See the create table SQL below, the default value 'a ' has a tailing space, and in the definition of the table, the space is gone, but in tidb, it will not be trimmed.

We can fix it in the parser, just trimming the tailing space for DefaultValueExpr is enough.

mysql> create table t(a enum('a') not null default 'a ');
Query OK, 0 rows affected (0.03 sec)

mysql> show create table t;
+-------+----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                               |
+-------+----------------------------------------------------------------------------------------------------------------------------+
| t     | CREATE TABLE `t` (
  `a` enum('a') NOT NULL DEFAULT 'a'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

tidb will report error because 'a' != 'a ' (the new collation is not enabled)

@xiongjiwei xiongjiwei removed help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. labels Nov 18, 2021
@bb7133
Copy link
Member

bb7133 commented Nov 25, 2021

@xiongjiwei Are you sure that the default value should be trimmed, no matter whether new collation is enabled?

@xiongjiwei
Copy link
Contributor

Are you sure that the default value should be trimmed, no matter whether new collation is enabled?

actually, I am wrong, it is related to the collation. we should also revert pingcap/parser#1006, that is a wrong fix 😥

@bb7133
Copy link
Member

bb7133 commented Nov 25, 2021

I think it may be fine to keep trimming the space even when new collation is not enabled, as long as it doesn't lead to any bug.

@bb7133
Copy link
Member

bb7133 commented Nov 25, 2021

I don't see any reason that users DO NOT want trimming spaces for SET/ENUM values.

@github-actions
Copy link

github-actions bot commented Dec 6, 2021

Please check whether the issue should be labeled with 'affects-x.y' or 'fixes-x.y.z', and then remove 'needs-more-info' label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants