diff --git a/mysql-test/r/check_constraint.result b/mysql-test/r/check_constraint.result index 1743bb97047..7447550ed09 100644 --- a/mysql-test/r/check_constraint.result +++ b/mysql-test/r/check_constraint.result @@ -140,3 +140,7 @@ create table t1 (a int, b int, check(a>0)); alter table t1 drop column a; ERROR 42S22: Unknown column 'a' in 'CHECK' drop table t1; +create table t1 (a int check (@b in (select user from mysql.user))); +ERROR HY000: Function or expression 'select ...' cannot be used in the CHECK clause of `a` +create table t1 (a int check (a > @b)); +ERROR HY000: Function or expression '@b' cannot be used in the CHECK clause of `a` diff --git a/mysql-test/t/check_constraint.test b/mysql-test/t/check_constraint.test index 7c5d30b6cd3..437463ee457 100644 --- a/mysql-test/t/check_constraint.test +++ b/mysql-test/t/check_constraint.test @@ -86,3 +86,11 @@ create table t1 (a int, b int, check(a>0)); --error ER_BAD_FIELD_ERROR alter table t1 drop column a; drop table t1; + +# +# MDEV-12421 Check constraint with query crashes server and renders DB unusable +# +--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED +create table t1 (a int check (@b in (select user from mysql.user))); +--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED +create table t1 (a int check (a > @b)); diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 412fcce8625..fdb40407ffb 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -6013,8 +6013,8 @@ field_list_item: ; column_def: - field_spec opt_check_constraint - { $$= $1; $$->check_constraint= $2; } + field_spec + { $$= $1; } | field_spec references { $$= $1; } ; @@ -6152,11 +6152,13 @@ field_spec: lex->init_last_field(f, $1.str, NULL); $$= f; } - field_type_or_serial + field_type_or_serial opt_check_constraint { LEX *lex=Lex; $$= $2; + $$->check_constraint= $4; + if ($$->check(thd)) MYSQL_YYABORT;