Skip to content
/ server Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions mysql-test/main/generate_invisible_primary_key.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
CREATE TABLE t0 (c1 INT, c2 INT);
SHOW INDEX FROM t0;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
SET sql_generate_invisible_primary_key=ON;
CREATE TABLE t1 (c1 INT, c2 INT);
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
INSERT INTO t1 VALUES (3, 0);
INSERT INTO t1 VALUES (4, 0);
SELECT _rowid, t1.* FROM t1;
_rowid c1 c2
1 3 0
2 4 0
ALTER TABLE t1 ADD c3 INT;
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
ALTER TABLE t1 ADD _inv_PK INT;
ALTER TABLE t1 ADD _inv_PK1 INT;
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
ALTER TABLE t1 DROP PRIMARY KEY;
ERROR 42000: Can't DROP INDEX `PRIMARY`; check that it exists
ALTER TABLE t1 ADD PRIMARY KEY (c1);
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t1 0 PRIMARY 1 c1 A 2 NULL NULL BTREE NO
SELECT _rowid, t1.* FROM t1;
_rowid c1 c2 c3 _inv_PK _inv_PK1
3 3 0 NULL NULL NULL
4 4 0 NULL NULL NULL
ALTER TABLE t1 DROP PRIMARY KEY;
CREATE TABLE t2 (c1 INT, c2 INT, _inv_PK INT);
SHOW INDEX FROM t2;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
CREATE TABLE t3 (c1 INT, c2 INT);
SHOW INDEX FROM t3;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
CREATE TABLE t4 (_inv_PK SERIAL INVISIBLE PRIMARY KEY, c2 INT);
ALTER TABLE t4 ADD PRIMARY KEY (c2);
ERROR 42000: Multiple primary key defined
SHOW INDEX FROM t4;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t4 0 PRIMARY 1 _inv_PK A 0 NULL NULL BTREE NO
DROP TABLE t0, t1, t2, t3, t4;
SET sql_generate_invisible_primary_key=OFF;
75 changes: 75 additions & 0 deletions mysql-test/main/generate_invisible_primary_key.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#
# Test of sql_generate_invisible_primary_key option without debug (MDEV-21181)
#

#
# When option is off, invisible PK is not generated
#
CREATE TABLE t0 (c1 INT, c2 INT);
SHOW INDEX FROM t0;

SET sql_generate_invisible_primary_key=ON;

#
# Check that PK index gets created when not specified in CREATE TABLE
#
CREATE TABLE t1 (c1 INT, c2 INT);
SHOW INDEX FROM t1;

INSERT INTO t1 VALUES (3, 0);
INSERT INTO t1 VALUES (4, 0);
SELECT _rowid, t1.* FROM t1;

#
# Check that generated PK persists on ALTER TABLE without new PK
#
ALTER TABLE t1 ADD c3 INT;
SHOW INDEX FROM t1;

#
# Check that adding column with name of generated PK succeeds
#
ALTER TABLE t1 ADD _inv_PK INT;
ALTER TABLE t1 ADD _inv_PK1 INT;
SHOW INDEX FROM t1;

#
# Trying to drop autogenerated PK should fail
#
--error ER_CANT_DROP_FIELD_OR_KEY
ALTER TABLE t1 DROP PRIMARY KEY;

#
# Check that generated PK is dropped on ALTER TABLE with new PK
#
ALTER TABLE t1 ADD PRIMARY KEY (c1);
SHOW INDEX FROM t1;
SELECT _rowid, t1.* FROM t1;

#
# Check that user PK that replaced generated PK can be dropped
#
ALTER TABLE t1 DROP PRIMARY KEY;

#
# Check that creating table with column named _inv_PK succeeds
#
CREATE TABLE t2 (c1 INT, c2 INT, _inv_PK INT);
SHOW INDEX FROM t2;

#
# Generated PK should be fully invisible to user
#
CREATE TABLE t3 (c1 INT, c2 INT);
SHOW INDEX FROM t3;

#
# Manually defined invisible primary key is not automatically dropped
#
CREATE TABLE t4 (_inv_PK SERIAL INVISIBLE PRIMARY KEY, c2 INT);
--error ER_MULTIPLE_PRI_KEY
ALTER TABLE t4 ADD PRIMARY KEY (c2);
SHOW INDEX FROM t4;

DROP TABLE t0, t1, t2, t3, t4;
SET sql_generate_invisible_primary_key=OFF;
57 changes: 57 additions & 0 deletions mysql-test/main/generate_invisible_primary_key_debug.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
SET SESSION debug_dbug="+d,test_invisible_index,test_completely_invisible";
CREATE TABLE t0 (c1 INT, c2 INT);
SHOW INDEX FROM t0;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t0 1 invisible 1 invisible A NULL NULL NULL YES BTREE NO
SET sql_generate_invisible_primary_key=ON;
CREATE TABLE t1 (c1 INT, c2 INT);
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t1 0 PRIMARY 1 _inv_PK A 0 NULL NULL BTREE NO
t1 1 invisible 1 invisible A NULL NULL NULL YES BTREE NO
INSERT INTO t1 VALUES (3, 0);
INSERT INTO t1 VALUES (4, 0);
SELECT _rowid, t1.* FROM t1;
_rowid c1 c2
1 3 0
2 4 0
ALTER TABLE t1 ADD c3 INT;
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t1 1 invisible 1 invisible A NULL NULL NULL YES BTREE NO
t1 0 PRIMARY 1 _inv_PK A 2 NULL NULL BTREE NO
ALTER TABLE t1 ADD _inv_PK INT;
ALTER TABLE t1 ADD _inv_PK1 INT;
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t1 1 invisible 1 invisible A NULL NULL NULL YES BTREE NO
t1 0 PRIMARY 1 _inv_PK2 A 2 NULL NULL BTREE NO
ALTER TABLE t1 DROP PRIMARY KEY;
ERROR 42000: Can't DROP INDEX `PRIMARY`; check that it exists
ALTER TABLE t1 ADD PRIMARY KEY (c1);
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t1 0 PRIMARY 1 c1 A 2 NULL NULL BTREE NO
t1 1 invisible 1 invisible A NULL NULL NULL YES BTREE NO
SELECT _rowid, t1.* FROM t1;
_rowid c1 c2 c3 _inv_PK _inv_PK1
3 3 0 NULL NULL NULL
4 4 0 NULL NULL NULL
ALTER TABLE t1 DROP PRIMARY KEY;
CREATE TABLE t2 (c1 INT, c2 INT, _inv_PK INT);
SHOW INDEX FROM t2;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t2 0 PRIMARY 1 _inv_PK1 A 0 NULL NULL BTREE NO
t2 1 invisible 1 invisible A NULL NULL NULL YES BTREE NO
SET SESSION debug_dbug="";
CREATE TABLE t3 (c1 INT, c2 INT);
SHOW INDEX FROM t3;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
CREATE TABLE t4 (_inv_PK SERIAL INVISIBLE PRIMARY KEY, c2 INT);
ALTER TABLE t4 ADD PRIMARY KEY (c2);
ERROR 42000: Multiple primary key defined
SHOW INDEX FROM t4;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t4 0 PRIMARY 1 _inv_PK A 0 NULL NULL BTREE NO
DROP TABLE t0, t1, t2, t3, t4;
SET sql_generate_invisible_primary_key=OFF;
80 changes: 80 additions & 0 deletions mysql-test/main/generate_invisible_primary_key_debug.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#
# Test of sql_generate_invisible_primary_key option (MDEV-21181)
#
--source include/have_debug.inc

SET SESSION debug_dbug="+d,test_invisible_index,test_completely_invisible";

#
# When option is off, invisible PK is not generated
#
CREATE TABLE t0 (c1 INT, c2 INT);
SHOW INDEX FROM t0;

SET sql_generate_invisible_primary_key=ON;

#
# Check that PK index gets created when not specified in CREATE TABLE
#
CREATE TABLE t1 (c1 INT, c2 INT);
SHOW INDEX FROM t1;

INSERT INTO t1 VALUES (3, 0);
INSERT INTO t1 VALUES (4, 0);
SELECT _rowid, t1.* FROM t1;

#
# Check that generated PK persists on ALTER TABLE without new PK
#
ALTER TABLE t1 ADD c3 INT;
SHOW INDEX FROM t1;

#
# Check that adding column with name of generated PK succeeds
#
ALTER TABLE t1 ADD _inv_PK INT;
ALTER TABLE t1 ADD _inv_PK1 INT;
SHOW INDEX FROM t1;

#
# Trying to drop autogenerated PK should fail
#
--error ER_CANT_DROP_FIELD_OR_KEY
ALTER TABLE t1 DROP PRIMARY KEY;

#
# Check that generated PK is dropped on ALTER TABLE with new PK
#
ALTER TABLE t1 ADD PRIMARY KEY (c1);
SHOW INDEX FROM t1;
SELECT _rowid, t1.* FROM t1;

#
# Check that user PK that replaced generated PK can be dropped
#
ALTER TABLE t1 DROP PRIMARY KEY;

#
# Check that creating table with column named _inv_PK succeeds
#
CREATE TABLE t2 (c1 INT, c2 INT, _inv_PK INT);
SHOW INDEX FROM t2;

SET SESSION debug_dbug="";

#
# Generated PK should be fully invisible to user
#
CREATE TABLE t3 (c1 INT, c2 INT);
SHOW INDEX FROM t3;

#
# Manually defined invisible primary key is not automatically dropped
#
CREATE TABLE t4 (_inv_PK SERIAL INVISIBLE PRIMARY KEY, c2 INT);
--error ER_MULTIPLE_PRI_KEY
ALTER TABLE t4 ADD PRIMARY KEY (c2);
SHOW INDEX FROM t4;

DROP TABLE t0, t1, t2, t3, t4;
SET sql_generate_invisible_primary_key=OFF;
4 changes: 4 additions & 0 deletions mysql-test/main/mysqld--help.result
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,9 @@ The following specify which files/extra groups are read (specified before remain
--sort-buffer-size=#
Each thread that needs to do a sort allocates a buffer of
this size
--sql-generate-invisible-primary-key
Automatically generate an invisible auto-increment
primary key for tables created without a primary key
--sql-mode=name Sets the sql mode. Any combination of: REAL_AS_FLOAT,
PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE,
IGNORE_BAD_TABLE_OPTIONS, ONLY_FULL_GROUP_BY,
Expand Down Expand Up @@ -2140,6 +2143,7 @@ slave-type-conversions
slow-launch-time 2
slow-query-log FALSE
sort-buffer-size 2097152
sql-generate-invisible-primary-key FALSE
sql-mode STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
sql-safe-updates FALSE
stack-trace TRUE
Expand Down
10 changes: 10 additions & 0 deletions mysql-test/suite/sys_vars/r/sysvars_server_embedded.result
Original file line number Diff line number Diff line change
Expand Up @@ -3672,6 +3672,16 @@ NUMERIC_BLOCK_SIZE NULL
ENUM_VALUE_LIST OFF,ON
READ_ONLY NO
COMMAND_LINE_ARGUMENT NULL
VARIABLE_NAME SQL_GENERATE_INVISIBLE_PRIMARY_KEY
VARIABLE_SCOPE SESSION
VARIABLE_TYPE BOOLEAN
VARIABLE_COMMENT Automatically generate an invisible auto-increment primary key for tables created without a primary key
NUMERIC_MIN_VALUE NULL
NUMERIC_MAX_VALUE NULL
NUMERIC_BLOCK_SIZE NULL
ENUM_VALUE_LIST OFF,ON
READ_ONLY NO
COMMAND_LINE_ARGUMENT OPTIONAL
VARIABLE_NAME SQL_IF_EXISTS
VARIABLE_SCOPE SESSION
VARIABLE_TYPE BOOLEAN
Expand Down
10 changes: 10 additions & 0 deletions mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result
Original file line number Diff line number Diff line change
Expand Up @@ -4472,6 +4472,16 @@ NUMERIC_BLOCK_SIZE NULL
ENUM_VALUE_LIST OFF,ON
READ_ONLY NO
COMMAND_LINE_ARGUMENT NULL
VARIABLE_NAME SQL_GENERATE_INVISIBLE_PRIMARY_KEY
VARIABLE_SCOPE SESSION
VARIABLE_TYPE BOOLEAN
VARIABLE_COMMENT Automatically generate an invisible auto-increment primary key for tables created without a primary key
NUMERIC_MIN_VALUE NULL
NUMERIC_MAX_VALUE NULL
NUMERIC_BLOCK_SIZE NULL
ENUM_VALUE_LIST OFF,ON
READ_ONLY NO
COMMAND_LINE_ARGUMENT OPTIONAL
VARIABLE_NAME SQL_IF_EXISTS
VARIABLE_SCOPE SESSION
VARIABLE_TYPE BOOLEAN
Expand Down
1 change: 1 addition & 0 deletions sql/sql_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ typedef struct system_variables
#endif // USER_VAR_TRACKING
my_bool tcp_nodelay;
my_bool optimizer_record_context;
my_bool generate_invisible_primary_key;
plugin_ref table_plugin;
plugin_ref tmp_table_plugin;
plugin_ref enforced_table_plugin;
Expand Down
Loading