Skip to content
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
10 changes: 10 additions & 0 deletions mysql-test/r/check_constraints.result
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,16 @@ ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITH
ALTER TABLE t1 MODIFY COLUMN f1 INT DEFAULT 20, algorithm=copy;
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1;
#
# Bug#121124 - An unrelated CHECK constraint on DATETIME should not
# prevent an online ENUM extension.
#
CREATE TABLE t1 (e ENUM('a','b') NOT NULL, d DATETIME(6),
CONSTRAINT ck CHECK (d IS NULL));
ALTER TABLE t1 MODIFY e ENUM('a','b','c') NOT NULL, ALGORITHM=INSTANT;
ALTER TABLE t1 MODIFY e ENUM('a','b','c','d') NOT NULL, ALGORITHM=INPLACE;
ALTER TABLE t1 MODIFY d DATETIME(6) DEFAULT NULL, ALGORITHM=INPLACE;
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with CHANGE COLUMN syntax.
#-----------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions mysql-test/t/check_constraints.test
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,18 @@ ALTER TABLE t1 MODIFY COLUMN f1 INT DEFAULT 20, algorithm=copy;
DROP TABLE t1;


--echo #
--echo # Bug#121124 - An unrelated CHECK constraint on DATETIME should not
--echo # prevent an online ENUM extension.
--echo #
CREATE TABLE t1 (e ENUM('a','b') NOT NULL, d DATETIME(6),
CONSTRAINT ck CHECK (d IS NULL));
ALTER TABLE t1 MODIFY e ENUM('a','b','c') NOT NULL, ALGORITHM=INSTANT;
ALTER TABLE t1 MODIFY e ENUM('a','b','c','d') NOT NULL, ALGORITHM=INPLACE;
ALTER TABLE t1 MODIFY d DATETIME(6) DEFAULT NULL, ALGORITHM=INPLACE;
DROP TABLE t1;


--echo #-----------------------------------------------------------------------
--echo # Test case to verify check constraint with CHANGE COLUMN syntax.
--echo #-----------------------------------------------------------------------
Expand Down
11 changes: 5 additions & 6 deletions sql/sql_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7580,8 +7580,7 @@ static bool prepare_key(
Create_field *sql_field;
while ((sql_field = it++) &&
my_strcasecmp(system_charset_info, first_col->get_field_name(),
sql_field->field_name))
;
sql_field->field_name));
if (!sql_field) {
my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0),
first_col->get_field_name());
Expand Down Expand Up @@ -19204,8 +19203,7 @@ bool mysql_alter_table(THD *thd, const char *new_db, const char *new_name,
mysql_rename_table(
thd, old_db_type, alter_ctx.db, backup_name, alter_ctx.db,
backup_name, *schema, alter_ctx.db, alter_ctx.alias,
FN_FROM_IS_TMP | NO_FK_CHECKS | NO_FK_RENAME | NO_CC_RENAME))
;
FN_FROM_IS_TMP | NO_FK_CHECKS | NO_FK_RENAME | NO_CC_RENAME));
}
goto err_with_mdl;
}
Expand Down Expand Up @@ -20916,9 +20914,10 @@ static bool is_any_check_constraints_evaluation_required(
continue;

// Check if data type is changed.
if (!my_strcasecmp(system_charset_info, itm_fld->field_name,
if (fld.change &&
!my_strcasecmp(system_charset_info, itm_fld->field_name,
fld.field_name) &&
(itm_fld->data_type() != fld.sql_type))
(itm_fld->data_type() != real_type_to_type(fld.sql_type)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

real_type_to_type function only normalizes temporal types but here we compare Field::type() with a real type fld.sql_type.

For ENUM/SET it becomes MYSQL_TYPE_STRING vs MYSQL_TYPE_ENUM``/MYSQL_TYPE_SET`.

Reproduction example:

CREATE TABLE t (
  e ENUM('a','b'),
  CONSTRAINT ck CHECK (e IS NULL OR e IN ('a','b'))
);

ALTER TABLE t
  MODIFY e ENUM('a','b') DEFAULT 'a',
 ALGORITHM=INPLACE;

The ALTER only changes the default, does not affect existing row and the CHECK does not use DEFAULT(e) but it fails with ER_ALTER_OPERATION_NOT_SUPPORTED.

Should we compare itm_fld->field->real_type() with fld.sql_type instead ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also useful too add this test as a regression test.

return true;
}

Expand Down
Loading