Skip to content

Commit

Permalink
MDEV-17503 CREATE SEQUENCE failed with innodb_force_primary_key =1
Browse files Browse the repository at this point in the history
Fixed by adding table flag HA_WANTS_PRIMARY_KEY, which is like
HA_REQUIRE_PRIMARY_KEY but tells SQL upper layer that the storage engine
internally can handle tables without primary keys (for example for
sequences or trough user variables)
  • Loading branch information
montywi committed Oct 29, 2018
1 parent 6a6cc8a commit d30124e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
9 changes: 9 additions & 0 deletions mysql-test/suite/sql_sequence/create.result
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,12 @@ TABLE_ID NAME FLAG N_COLS SPACE ROW_FORMAT ZIP_PAGE_SIZE SPACE_TYPE
DROP SEQUENCE seq1;
CREATE TEMPORARY SEQUENCE seq1 ENGINE=InnoDB ROW_FORMAT=REDUNDANT;
DROP TEMPORARY SEQUENCE seq1;
#
# MDEV-17503 CREATE SEQUENCE failed with innodb_force_primary_key =1
#
set global innodb_force_primary_key =1;
CREATE SEQUENCE s1 START WITH 100 INCREMENT BY 10 ENGINE=innodb;
set global innodb_force_primary_key=default;
ALTER TABLE s1 ADD PRIMARY KEY (next_not_cached_value);
ERROR HY000: Sequence 'test.s1' table structure is invalid (Sequence tables cannot have any keys)
DROP SEQUENCE s1;
11 changes: 11 additions & 0 deletions mysql-test/suite/sql_sequence/create.test
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,14 @@ SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES WHERE NAME='test/seq1';
DROP SEQUENCE seq1;
CREATE TEMPORARY SEQUENCE seq1 ENGINE=InnoDB ROW_FORMAT=REDUNDANT;
DROP TEMPORARY SEQUENCE seq1;

--echo #
--echo # MDEV-17503 CREATE SEQUENCE failed with innodb_force_primary_key =1
--echo #

set global innodb_force_primary_key =1;
CREATE SEQUENCE s1 START WITH 100 INCREMENT BY 10 ENGINE=innodb;
set global innodb_force_primary_key=default;
--error ER_SEQUENCE_INVALID_TABLE_STRUCTURE
ALTER TABLE s1 ADD PRIMARY KEY (next_not_cached_value);
DROP SEQUENCE s1;
4 changes: 4 additions & 0 deletions sql/ha_partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,10 @@ class ha_partition :public handler
with hidden primary key)
(No handler has this limitation currently)
HA_WANTS_PRIMARY_KEY:
Can't define a table without primary key except sequences
(Only InnoDB has this when using innodb_force_primary_key == ON)
HA_STATS_RECORDS_IS_EXACT:
Does the counter of records after the info call specify an exact
value or not. If it does this flag is set.
Expand Down
3 changes: 3 additions & 0 deletions sql/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ enum enum_alter_inplace_result {
/* calling cmp_ref() on the engine is expensive */
#define HA_CMP_REF_IS_EXPENSIVE (1ULL << 54)

/* Engine wants primary keys for everything except sequences */
#define HA_WANTS_PRIMARY_KEY (1ULL << 55)

/* bits in index_flags(index_number) for what you can do with index */
#define HA_READ_NEXT 1 /* TODO really use this flag */
#define HA_READ_PREV 2 /* supports ::index_prev */
Expand Down
4 changes: 3 additions & 1 deletion sql/sql_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4134,7 +4134,9 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
}

if (!unique_key && !primary_key &&
(file->ha_table_flags() & HA_REQUIRE_PRIMARY_KEY))
((file->ha_table_flags() & HA_REQUIRE_PRIMARY_KEY) ||
((file->ha_table_flags() & HA_WANTS_PRIMARY_KEY) &&
!create_info->sequence)))
{
my_message(ER_REQUIRES_PRIMARY_KEY, ER_THD(thd, ER_REQUIRES_PRIMARY_KEY),
MYF(0));
Expand Down
2 changes: 1 addition & 1 deletion storage/innobase/handler/ha_innodb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2918,7 +2918,7 @@ ha_innobase::ha_innobase(
| HA_CAN_RTREEKEYS
| HA_CAN_TABLES_WITHOUT_ROLLBACK
| HA_CONCURRENT_OPTIMIZE
| (srv_force_primary_key ? HA_REQUIRE_PRIMARY_KEY : 0)
| (srv_force_primary_key ? HA_WANTS_PRIMARY_KEY : 0)
),
m_start_of_scan(),
m_mysql_has_locked()
Expand Down

0 comments on commit d30124e

Please sign in to comment.