Skip to content

Commit 22d8bb2

Browse files
committed
MDEV-12421 Check constraint with query crashes server and renders DB unusable
parse CHECK constraint *before* Column_definition validity checks
1 parent 6315426 commit 22d8bb2

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

mysql-test/r/check_constraint.result

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,7 @@ create table t1 (a int, b int, check(a>0));
140140
alter table t1 drop column a;
141141
ERROR 42S22: Unknown column 'a' in 'CHECK'
142142
drop table t1;
143+
create table t1 (a int check (@b in (select user from mysql.user)));
144+
ERROR HY000: Function or expression 'select ...' cannot be used in the CHECK clause of `a`
145+
create table t1 (a int check (a > @b));
146+
ERROR HY000: Function or expression '@b' cannot be used in the CHECK clause of `a`

mysql-test/t/check_constraint.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,11 @@ create table t1 (a int, b int, check(a>0));
8686
--error ER_BAD_FIELD_ERROR
8787
alter table t1 drop column a;
8888
drop table t1;
89+
90+
#
91+
# MDEV-12421 Check constraint with query crashes server and renders DB unusable
92+
#
93+
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
94+
create table t1 (a int check (@b in (select user from mysql.user)));
95+
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
96+
create table t1 (a int check (a > @b));

sql/sql_yacc.yy

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6013,8 +6013,8 @@ field_list_item:
60136013
;
60146014

60156015
column_def:
6016-
field_spec opt_check_constraint
6017-
{ $$= $1; $$->check_constraint= $2; }
6016+
field_spec
6017+
{ $$= $1; }
60186018
| field_spec references
60196019
{ $$= $1; }
60206020
;
@@ -6152,11 +6152,13 @@ field_spec:
61526152
lex->init_last_field(f, $1.str, NULL);
61536153
$<create_field>$= f;
61546154
}
6155-
field_type_or_serial
6155+
field_type_or_serial opt_check_constraint
61566156
{
61576157
LEX *lex=Lex;
61586158
$$= $<create_field>2;
61596159

6160+
$$->check_constraint= $4;
6161+
61606162
if ($$->check(thd))
61616163
MYSQL_YYABORT;
61626164

0 commit comments

Comments
 (0)