From 16be7469ac7b98b5706623d81829b349fb901b1f Mon Sep 17 00:00:00 2001 From: Sachin Setiya Date: Mon, 29 Jan 2018 12:31:07 +0530 Subject: [PATCH] MDEV-14849 CREATE + ALTER with user-invisible columns produce ... Problem:- create or replace table t1 (pk int auto_increment primary key invisible, i int); alter table t1 modify pk int invisible; This last alter makes a invisible column which is not null and does not have default value. Analysis:- This is caused because our error check for NOT_NULL_FLAG and NO_DEFAULT_VALUE_FLAG flag misses this sql_field , but this is not the fault of error check :).Actually this field come via mysql_prepare_alter_table and it does not have NO_DEFAULT_VALUE_FLAG flag turned on. (If it was create table NO_DEFAULT_VALUE_FLAG would have turned on Column_definition::check) and this would have generated error. Solution:- I have moved the error check to kind last of mysql_prepare_create_table because upto this point we have applied NO_DEFAULT_VALUE_FLAG to required column. --- mysql-test/r/invisible_field.result | 9 +++++++-- mysql-test/t/invisible_field.test | 7 ++++++- sql/sql_table.cc | 16 ++++++++-------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/mysql-test/r/invisible_field.result b/mysql-test/r/invisible_field.result index a58f34e39a0..671b69e4fe3 100644 --- a/mysql-test/r/invisible_field.result +++ b/mysql-test/r/invisible_field.result @@ -26,7 +26,12 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp create table t1(a1 int primary key invisible ,a2 int unique invisible , a3 blob,a4 int not null invisible unique); ERROR HY000: Invisible column `a1` must have a default value create table t1(abc int not null invisible); -ERROR HY000: Invisible column `abc` must have a default value +ERROR 42000: A table must have at least 1 column +MDEV-14849 CREATE + ALTER with user-invisible columns produce invalid table definition +create or replace table t1 (pk int auto_increment primary key invisible, i int); +alter table t1 modify pk int invisible; +ERROR HY000: Invisible column `pk` must have a default value +drop table t1; create table t1(a int invisible, b int); insert into t1 values(1); insert into t1(a) values(2); @@ -439,7 +444,7 @@ d int(11) YES UNI NULL drop table t1; SHOW STATUS LIKE 'Feature_invisible_columns'; Variable_name Value -Feature_invisible_columns 50 +Feature_invisible_columns 52 #invisible is non reserved create table t1(a int unique , invisible int invisible, c int ); desc t1; diff --git a/mysql-test/t/invisible_field.test b/mysql-test/t/invisible_field.test index a68e05cf320..b243125e8e1 100644 --- a/mysql-test/t/invisible_field.test +++ b/mysql-test/t/invisible_field.test @@ -11,8 +11,13 @@ create table t1(a1 int invisible); create table t1(a1 blob,invisible(a1)); --error ER_INVISIBLE_NOT_NULL_WITHOUT_DEFAULT create table t1(a1 int primary key invisible ,a2 int unique invisible , a3 blob,a4 int not null invisible unique); ---error ER_INVISIBLE_NOT_NULL_WITHOUT_DEFAULT +--error ER_TABLE_MUST_HAVE_COLUMNS create table t1(abc int not null invisible); +--echo MDEV-14849 CREATE + ALTER with user-invisible columns produce invalid table definition +create or replace table t1 (pk int auto_increment primary key invisible, i int); +--error ER_INVISIBLE_NOT_NULL_WITHOUT_DEFAULT +alter table t1 modify pk int invisible; +drop table t1; create table t1(a int invisible, b int); #should automatically add null insert into t1 values(1); diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 63b6530b92f..16bd1dc0bf0 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -3593,14 +3593,6 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info, record_offset+= sql_field->pack_length; if (sql_field->flags & VERS_SYSTEM_FIELD) continue; - if (sql_field->invisible == INVISIBLE_USER && - sql_field->flags & NOT_NULL_FLAG && - sql_field->flags & NO_DEFAULT_VALUE_FLAG) - { - my_error(ER_INVISIBLE_NOT_NULL_WITHOUT_DEFAULT, MYF(0), - sql_field->field_name.str); - DBUG_RETURN(TRUE); - } } /* Update virtual fields' offset and give error if All fields are invisible */ @@ -4244,6 +4236,14 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info, my_error(ER_INVALID_DEFAULT, MYF(0), sql_field->field_name.str); DBUG_RETURN(TRUE); } + if (sql_field->invisible == INVISIBLE_USER && + sql_field->flags & NOT_NULL_FLAG && + sql_field->flags & NO_DEFAULT_VALUE_FLAG) + { + my_error(ER_INVISIBLE_NOT_NULL_WITHOUT_DEFAULT, MYF(0), + sql_field->field_name.str); + DBUG_RETURN(TRUE); + } } /* Check table level constraints */