Skip to content

Commit

Permalink
MDEV-16365 Setting a column NOT NULL fails to return error for
Browse files Browse the repository at this point in the history
		NULL values when there is no DEFAULT

Copy and inplace algorithm works similarly for
NULL to NOT NULL conversion for the following cases:
(1) strict sql mode - Should give error.
(2) non-strict sql mode - Should give warnings alone
(3) alter ignore table command. - Should give warnings alone.
  • Loading branch information
Thirunarayanan committed Jun 25, 2018
1 parent 1abd877 commit 88aaf59
Show file tree
Hide file tree
Showing 33 changed files with 663 additions and 349 deletions.
1 change: 1 addition & 0 deletions mysql-test/main/selectivity_innodb.result
Expand Up @@ -1150,6 +1150,7 @@ alter table t1 change column a a int;
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status Engine-independent statistics collected
test.t1 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t1 analyze status OK
flush table t1;
explain extended select * from t1 where a between 5 and 7;
Expand Down
10 changes: 10 additions & 0 deletions mysql-test/main/statistics.result
Expand Up @@ -1086,6 +1086,9 @@ test t2 idx4 3 1.1304
ANALYZE TABLE t2 PERSISTENT FOR COLUMNS() INDEXES ALL;
Table Op Msg_type Msg_text
test.t2 analyze status Engine-independent statistics collected
test.t2 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t2 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t2 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t2 analyze status OK
SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name;
db_name table_name index_name prefix_arity avg_frequency
Expand Down Expand Up @@ -1146,6 +1149,11 @@ test t2 idx4 4 1.0000
ANALYZE TABLE t2 PERSISTENT FOR COLUMNS ALL INDEXES ALL;
Table Op Msg_type Msg_text
test.t2 analyze status Engine-independent statistics collected
test.t2 analyze Note Data truncated for column 'avg_length' at row 1
test.t2 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t2 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t2 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t2 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t2 analyze status OK
SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name;
db_name table_name index_name prefix_arity avg_frequency
Expand All @@ -1171,6 +1179,8 @@ test t2 idx3 1 8.5000
ANALYZE TABLE t2 PERSISTENT FOR COLUMNS() INDEXES ALL;
Table Op Msg_type Msg_text
test.t2 analyze status Engine-independent statistics collected
test.t2 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t2 analyze Note Data truncated for column 'avg_frequency' at row 1
test.t2 analyze status OK
SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name;
db_name table_name index_name prefix_arity avg_frequency
Expand Down
4 changes: 4 additions & 0 deletions mysql-test/suite/gcol/r/gcol_keys_innodb.result
Expand Up @@ -691,9 +691,13 @@ a b c
1 127 0
SET STATEMENT sql_mode = 'NO_ENGINE_SUBSTITUTION' FOR
ALTER TABLE t ADD UNIQUE INDEX (c(1));
Warnings:
Warning 1264 Out of range value for column 'b' at row 1
SELECT * FROM t WHERE c = '0';
a b c
1 127 0
Warnings:
Warning 1264 Out of range value for column 'b' at row 1
DROP TABLE t;
#
# Bug#21688115 VIRTUAL COLUMN COMPUTATION SAVE_IN_FIELD()
Expand Down
71 changes: 71 additions & 0 deletions mysql-test/suite/innodb/include/alter_non_null.inc
@@ -0,0 +1,71 @@
--source include/have_innodb.inc

CREATE TABLE t1(f1 INT)ENGINE=INNODB;
INSERT INTO t1 VALUES(NULL);
SELECT * FROM t1;
--enable_info
--error $error_code
ALTER TABLE t1 CHANGE f1 f1 INT NOT NULL;
--disable_info
SELECT * FROM t1;
DROP TABLE t1;

CREATE TABLE t1(f1 CHAR(10))ENGINE=INNODB;
INSERT INTO t1 VALUES(NULL);
SELECT * FROM t1;
--enable_info
--error $error_code
ALTER TABLE t1 CHANGE f1 f1 CHAR(10) NOT NULL;
--disable_info
SELECT * FROM t1;
DROP TABLE t1;

CREATE TABLE t1(f1 VARCHAR(10))ENGINE=INNODB;
INSERT INTO t1 VALUES(NULL);
SELECT * FROM t1;
--enable_info
--error $error_code
ALTER TABLE t1 CHANGE f1 f1 VARCHAR(20) NOT NULL;
--disable_info
SELECT * FROM t1;
DROP TABLE t1;

CREATE TABLE t1(f1 TEXT)ENGINE=INNODB;
INSERT INTO t1 VALUES(NULL);
SELECT * FROM t1;
--enable_info
--error $error_code
ALTER TABLE t1 CHANGE f1 f1 TEXT NOT NULL DEFAULT 'abc';
--disable_info
SELECT * FROM t1;
DROP TABLE t1;

CREATE TABLE t1(f1 INT NOT NULL, f2 INT NOT NULL, f3 INT)ENGINE=INNODB;
INSERT INTO t1 VALUES(2, 2, NULL);
SELECT * FROM t1;
--enable_info
--error $error_code
ALTER TABLE t1 CHANGE f3 f3 INT NOT NULL DEFAULT (f1 + f2);
--disable_info
SELECT * FROM t1;
DROP TABLE t1;

CREATE TABLE t1(f1 INT NOT NULL DEFAULT 0, b TINYINT)ENGINE=InnoDB;
INSERT INTO t1 VALUES(10, NULL);
SELECT * FROM t1;
--enable_info
--error $error_code
ALTER TABLE t1 CHANGE b b TINYINT NOT NULL DEFAULT if(unix_timestamp()>1,1000,0);
--disable_info
SELECT * FROM t1;
DROP TABLE t1;

# Alter ignore should work irrespective of sql mode

CREATE TABLE t1(c1 INT NOT NULL, c2 INT, PRIMARY KEY(c1))ENGINE=INNODB;
INSERT INTO t1 VALUES(1, NULL);
--enable_info
ALTER IGNORE TABLE t1 CHANGE c2 c2 INT NOT NULL DEFAULT 2;
--disable_info
SELECT * FROM t1;
DROP TABLE t1;
63 changes: 63 additions & 0 deletions mysql-test/suite/innodb/include/alter_non_null_debug.inc
@@ -0,0 +1,63 @@
--source include/have_innodb.inc
--source include/have_debug.inc
--source include/have_debug_sync.inc

# Alter table should fail for strict sql mode

CREATE TABLE t1(c1 INT NOT NULL, c2 INT, PRIMARY KEY(c1))ENGINE=INNODB;
INSERT INTO t1 VALUES(1, 1);
--enable_info
SET DEBUG_SYNC= 'row_merge_after_scan
SIGNAL opened WAIT_FOR flushed';
send ALTER TABLE t1 CHANGE c2 c2 INT NOT NULL DEFAULT 2;
connect (con1,localhost,root);
SET DEBUG_SYNC= 'now WAIT_FOR opened';
INSERT INTO t1 VALUES(2, NULL);
UPDATE t1 SET c1 = 3 WHERE c2 = 1;
SET DEBUG_SYNC= 'now SIGNAL flushed';
connection default;
--error $error_code
reap;
--disable_info
SELECT * FROM t1;
DROP TABLE t1;

# Alter table should successfully apply the log for the alter operation

CREATE TABLE t1(c1 INT NOT NULL, c2 INT, PRIMARY KEY(c1))ENGINE=INNODB;
INSERT INTO t1 VALUES(1, 1);
--enable_info
SET DEBUG_SYNC= 'row_merge_after_scan
SIGNAL opened WAIT_FOR flushed';
send ALTER TABLE t1 CHANGE c2 c2 INT NOT NULL DEFAULT 2;
connection con1;
SET DEBUG_SYNC= 'now WAIT_FOR opened';
INSERT INTO t1 VALUES(2, 3);
UPDATE t1 SET c1 = 3 WHERE c2 = 1;
SET DEBUG_SYNC= 'now SIGNAL flushed';
connection default;
reap;
--disable_info
SELECT * FROM t1;
DROP TABLE t1;

# Alter ignore should not give error

CREATE TABLE t1(c1 INT NOT NULL, c2 INT, c3 INT, PRIMARY KEY(c1))ENGINE=INNODB;
INSERT INTO t1 VALUES(1, 2, 3);
--enable_info
SET DEBUG_SYNC= 'row_merge_after_scan
SIGNAL opened WAIT_FOR flushed';
send ALTER IGNORE TABLE t1 CHANGE c2 c2 INT NOT NULL DEFAULT 2;
connection con1;
SET DEBUG_SYNC= 'now WAIT_FOR opened';
UPDATE t1 SET c2= 2 WHERE c1 = 1;
INSERT INTO t1 VALUES (2, NULL, 4);
SET DEBUG_SYNC= 'now SIGNAL flushed';
connection default;
reap;
--disable_info
SELECT * FROM t1;
DROP TABLE t1;
disconnect con1;
SET DEBUG_SYNC='RESET';
42 changes: 42 additions & 0 deletions mysql-test/suite/innodb/r/alter_non_null,COPY,NON-STRICT.rdiff
@@ -0,0 +1,42 @@
7,8c7,8
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
---
> affected rows: 1
> info: Records: 1 Duplicates: 0 Warnings: 1
21,22c21,22
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
---
> affected rows: 1
> info: Records: 1 Duplicates: 0 Warnings: 1
35,36c35,36
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
---
> affected rows: 1
> info: Records: 1 Duplicates: 0 Warnings: 1
49,50c49,50
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
---
> affected rows: 1
> info: Records: 1 Duplicates: 0 Warnings: 1
63,64c63,64
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
---
> affected rows: 1
> info: Records: 1 Duplicates: 0 Warnings: 1
77,78c77,78
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
---
> affected rows: 1
> info: Records: 1 Duplicates: 0 Warnings: 1
88,89c88,89
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
---
> affected rows: 1
> info: Records: 1 Duplicates: 0 Warnings: 1
72 changes: 72 additions & 0 deletions mysql-test/suite/innodb/r/alter_non_null,COPY,STRICT.rdiff
@@ -0,0 +1,72 @@
7,10c7
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f1' at row 1
---
> ERROR 01000: Data truncated for column 'f1' at row 1
13c10
< 0
---
> NULL
21,24c18
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f1' at row 1
---
> ERROR 01000: Data truncated for column 'f1' at row 1
27c21
<
---
> NULL
35,38c29
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f1' at row 1
---
> ERROR 01000: Data truncated for column 'f1' at row 1
41c32
<
---
> NULL
49,52c40
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f1' at row 1
---
> ERROR 01000: Data truncated for column 'f1' at row 1
55c43
<
---
> NULL
63,66c51
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f3' at row 1
---
> ERROR 01000: Data truncated for column 'f3' at row 1
69c54
< 2 2 0
---
> 2 2 NULL
77,80c62
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'b' at row 1
---
> ERROR 01000: Data truncated for column 'b' at row 1
83c65
< 10 0
---
> 10 NULL
88,89c70,71
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
---
> affected rows: 1
> info: Records: 1 Duplicates: 0 Warnings: 1
66 changes: 66 additions & 0 deletions mysql-test/suite/innodb/r/alter_non_null,INPLACE,STRICT.rdiff
@@ -0,0 +1,66 @@
7,10c7
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f1' at row 1
---
> ERROR 01000: Data truncated for column 'f1' at row 1
13c10
< 0
---
> NULL
21,24c18
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f1' at row 1
---
> ERROR 01000: Data truncated for column 'f1' at row 1
27c21
<
---
> NULL
35,38c29
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f1' at row 1
---
> ERROR 01000: Data truncated for column 'f1' at row 1
41c32
<
---
> NULL
49,52c40
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f1' at row 1
---
> ERROR 01000: Data truncated for column 'f1' at row 1
55c43
<
---
> NULL
63,66c51
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'f3' at row 1
---
> ERROR 01000: Data truncated for column 'f3' at row 1
69c54
< 2 2 0
---
> 2 2 NULL
77,80c62
< affected rows: 0
< info: Records: 0 Duplicates: 0 Warnings: 1
< Warnings:
< Warning 1265 Data truncated for column 'b' at row 1
---
> ERROR 01000: Data truncated for column 'b' at row 1
83c65
< 10 0
---
> 10 NULL

0 comments on commit 88aaf59

Please sign in to comment.