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 allows NULL value to NOT NULL column #57435

Closed
mzhang77 opened this issue Nov 17, 2024 · 6 comments · Fixed by #55477
Closed

tidb allows NULL value to NOT NULL column #57435

mzhang77 opened this issue Nov 17, 2024 · 6 comments · Fixed by #55477
Assignees
Labels
affects-7.5 This bug affects the 7.5.x(LTS) versions. affects-8.1 This bug affects the 8.1.x(LTS) versions. affects-8.5 This bug affects the 8.5.x(LTS) versions. severity/moderate sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.

Comments

@mzhang77
Copy link

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

mysql> select @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|            |
+------------+
1 row in set (0.00 sec)

mysql> create table t( pk int primary key not null auto_increment, data binary(12) not null);
Query OK, 0 rows affected (0.06 sec)

mysql> insert into t(data) values (null);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from t;
+----+----------------------------+
| pk | data                       |
+----+----------------------------+
|  1 | 0x000000000000000000000000 |
+----+----------------------------+
1 row in set (0.00 sec)

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

Tidb should not allow insert of NULL value to NOT NULL column. See below mysql result:

mysql> select @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|            |
+------------+
1 row in set (0.00 sec)

mysql> create table t( pk int primary key not null auto_increment, data binary(12) not null);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t(data) values (null);
ERROR 1048 (23000): Column 'data' cannot be null
mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 8.0.40    |
+-----------+
1 row in set (0.00 sec)

3. What did you see instead (Required)

tidb allows insert of NULL value to NOT NULL column, it's incompatible with mysql

4. What is your TiDB version? (Required)

mysql> select @@version;
+--------------------+
| @@version          |
+--------------------+
| 8.0.11-TiDB-v7.5.3 |
+--------------------+
1 row in set (0.00 sec)
@mzhang77 mzhang77 added the type/bug The issue is confirmed as a bug. label Nov 17, 2024
@dveeden
Copy link
Contributor

dveeden commented Nov 18, 2024

To make things easy to copy-paste:

select @@sql_mode;
create table t( pk int primary key not null auto_increment, data binary(12) not null);
insert into t(data) values (null);
select @@version;

Looks like a SET SESSION sql_mode='' might be missing.

MySQL 9.1.0:

mysql-9.1.0> SET SESSION sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql-9.1.0> select @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|            |
+------------+
1 row in set (0.00 sec)

mysql-9.1.0> create table t( pk int primary key not null auto_increment, data binary(12) not null);
Query OK, 0 rows affected (0.02 sec)

mysql-9.1.0> insert into t(data) values (null);
ERROR 1048 (23000): Column 'data' cannot be null
mysql-9.1.0> select @@version;
+-----------+
| @@version |
+-----------+
| 9.1.0     |
+-----------+
1 row in set (0.00 sec)

TiDB v8.4.0:

mysql-8.0.11-TiDB-v8.4.0> \W
Show warnings enabled.
mysql-8.0.11-TiDB-v8.4.0> SET SESSION sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql-8.0.11-TiDB-v8.4.0> select @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|            |
+------------+
1 row in set (0.00 sec)

mysql-8.0.11-TiDB-v8.4.0> create table t( pk int primary key not null auto_increment, data binary(12) not null);
Query OK, 0 rows affected (0.05 sec)

mysql-8.0.11-TiDB-v8.4.0> insert into t(data) values (null);
Query OK, 1 row affected, 1 warning (0.01 sec)

Warning (Code 1048): Column 'data' cannot be null
mysql-8.0.11-TiDB-v8.4.0> select @@version;
+--------------------+
| @@version          |
+--------------------+
| 8.0.11-TiDB-v8.4.0 |
+--------------------+
1 row in set (0.00 sec)

So the issue here is that without any SQL mode set MySQL returns an error for 1048 and TiDB returns a warning.

Note that MySQL 5.7.33 also returns an error, just like MySQL 8.0 and MySQL 9.1

@dveeden
Copy link
Contributor

dveeden commented Nov 18, 2024

Errror 1048 is known in the code as ErrBadNull

@jebter jebter added the sig/sql-infra SIG: SQL Infra label Nov 19, 2024
@dveeden dveeden mentioned this issue Nov 19, 2024
13 tasks
@dveeden
Copy link
Contributor

dveeden commented Nov 19, 2024

Extending the test to include INSERT IGNORE:

SET SESSION sql_mode='';
select @@sql_mode;
create table t( pk int primary key not null auto_increment, data binary(12) not null);
insert into t(data) values (null);
insert ignore into t(data) values (null);
select @@version;

@YangKeao
Copy link
Member

YangKeao commented Nov 22, 2024

Maybe fixed by #55477. Let me verify it.

@YangKeao YangKeao self-assigned this Nov 22, 2024
@dveeden
Copy link
Contributor

dveeden commented Nov 22, 2024

@YangKeao I was also working on #57504

@dveeden
Copy link
Contributor

dveeden commented Nov 22, 2024

mysql-8.0.11-TiDB-v8.5.0-alpha-199-g14ff938d8c> \W
Show warnings enabled.
mysql-8.0.11-TiDB-v8.5.0-alpha-199-g14ff938d8c> SET SESSION sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql-8.0.11-TiDB-v8.5.0-alpha-199-g14ff938d8c> select @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|            |
+------------+
1 row in set (0.00 sec)

mysql-8.0.11-TiDB-v8.5.0-alpha-199-g14ff938d8c> create table t( pk int primary key not null auto_increment, data binary(12) not null);
Query OK, 0 rows affected (0.01 sec)

mysql-8.0.11-TiDB-v8.5.0-alpha-199-g14ff938d8c> insert into t(data) values (null);
ERROR 1048 (23000): Column 'data' cannot be null
mysql-8.0.11-TiDB-v8.5.0-alpha-199-g14ff938d8c> insert ignore into t(data) values (null);
Query OK, 1 row affected, 1 warning (0.00 sec)

Warning (Code 1048): Column 'data' cannot be null
mysql-8.0.11-TiDB-v8.5.0-alpha-199-g14ff938d8c> select @@version;
+------------------------------------------+
| @@version                                |
+------------------------------------------+
| 8.0.11-TiDB-v8.5.0-alpha-199-g14ff938d8c |
+------------------------------------------+
1 row in set (0.00 sec)

mysql-8.0.11-TiDB-v8.5.0-alpha-199-g14ff938d8c> 

So this is fixed in master:

  • INSERT with NULL results in an error, not a warning
  • INSERT IGNORE with NULL results in a warning

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. affects-8.1 This bug affects the 8.1.x(LTS) versions. affects-8.5 This bug affects the 8.5.x(LTS) versions. severity/moderate sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.
Projects
None yet
5 participants