diff --git a/mysql-test/std_data/vcol_autoinc.MYI b/mysql-test/std_data/vcol_autoinc.MYI index ddb3f2e0748..9b174844f9f 100644 Binary files a/mysql-test/std_data/vcol_autoinc.MYI and b/mysql-test/std_data/vcol_autoinc.MYI differ diff --git a/mysql-test/std_data/vcol_autoinc.frm b/mysql-test/std_data/vcol_autoinc.frm index bff7983735c..ee43f878856 100644 Binary files a/mysql-test/std_data/vcol_autoinc.frm and b/mysql-test/std_data/vcol_autoinc.frm differ diff --git a/mysql-test/suite/gcol/inc/gcol_column_def_options.inc b/mysql-test/suite/gcol/inc/gcol_column_def_options.inc index 17e926758ee..6f4a8ab7240 100644 --- a/mysql-test/suite/gcol/inc/gcol_column_def_options.inc +++ b/mysql-test/suite/gcol/inc/gcol_column_def_options.inc @@ -49,18 +49,18 @@ alter table t1 add column (h int generated always as (a+1) virtual, i int as(5) drop table t1; --echo # DEFAULT ---error 1064 +--error ER_PARSE_ERROR create table t1 (a int, b int generated always as (a+1) virtual default 0); create table t1 (a int); ---error 1064 +--error ER_PARSE_ERROR alter table t1 add column b int generated always as (a+1) virtual default 0; drop table t1; --echo # AUTO_INCREMENT ---error 1064 +--error ER_PARSE_ERROR create table t1 (a int, b int generated always as (a+1) virtual AUTO_INCREMENT); create table t1 (a int); ---error 1064 +--error ER_PARSE_ERROR alter table t1 add column b int generated always as (a+1) virtual AUTO_INCREMENT; drop table t1; @@ -138,7 +138,7 @@ create table t1 (a int, b int generated always as (a % 2) stored references t2(a show create table t1; drop table t1; create table t1 (a int, b int generated always as (a % 2) virtual); ---error 1064 +--error ER_PARSE_ERROR alter table t1 modify b int generated always as (a % 2) stored references t2(a); show create table t1; drop table t1; @@ -199,6 +199,13 @@ create table t1 (a int, b int generated always as(-b) virtual, c int generated a create table t1 (a int, b int generated always as(-c) virtual, c int generated always as (b + 1) virtual); --error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key) stored, col_int_key int); +--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED +create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key) unique, col_int_key int); +create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key), col_int_key int); +show create table t1; +insert t1 (col_int_key) values (10),(20),(30); +select * from t1; +drop table t1; --echo # Bug#20339347: FAIL TO USE CREATE ....SELECT STATEMENT TO CREATE A NEW TABLE create table t1 (a int, b int generated always as(-a) virtual, c int generated always as (b + 1) stored); diff --git a/mysql-test/suite/gcol/r/gcol_column_def_options_innodb.result b/mysql-test/suite/gcol/r/gcol_column_def_options_innodb.result index daa372fac8b..2422bdca363 100644 --- a/mysql-test/suite/gcol/r/gcol_column_def_options_innodb.result +++ b/mysql-test/suite/gcol/r/gcol_column_def_options_innodb.result @@ -260,6 +260,24 @@ create table t1 (a int, b int generated always as(-c) virtual, c int generated a ERROR 01000: Expression for field `b` is referring to uninitialized field `c` create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key) stored, col_int_key int); ERROR HY000: Function or expression 'AUTO_INCREMENT' cannot be used in the GENERATED ALWAYS AS clause of `pk` +create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key) unique, col_int_key int); +ERROR HY000: Function or expression 'AUTO_INCREMENT' cannot be used in the GENERATED ALWAYS AS clause of `pk` +create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key), col_int_key int); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `pk` int(11) NOT NULL AUTO_INCREMENT, + `col_int_nokey` int(11) GENERATED ALWAYS AS (`pk` + `col_int_key`) VIRTUAL, + `col_int_key` int(11) DEFAULT NULL, + PRIMARY KEY (`pk`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci +insert t1 (col_int_key) values (10),(20),(30); +select * from t1; +pk col_int_nokey col_int_key +1 11 10 +2 22 20 +3 33 30 +drop table t1; # Bug#20339347: FAIL TO USE CREATE ....SELECT STATEMENT TO CREATE A NEW TABLE create table t1 (a int, b int generated always as(-a) virtual, c int generated always as (b + 1) stored); insert into t1(a) values(1),(2); diff --git a/mysql-test/suite/gcol/r/gcol_column_def_options_myisam.result b/mysql-test/suite/gcol/r/gcol_column_def_options_myisam.result index 59215426b31..82c4b65512f 100644 --- a/mysql-test/suite/gcol/r/gcol_column_def_options_myisam.result +++ b/mysql-test/suite/gcol/r/gcol_column_def_options_myisam.result @@ -260,6 +260,24 @@ create table t1 (a int, b int generated always as(-c) virtual, c int generated a ERROR 01000: Expression for field `b` is referring to uninitialized field `c` create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key) stored, col_int_key int); ERROR HY000: Function or expression 'AUTO_INCREMENT' cannot be used in the GENERATED ALWAYS AS clause of `pk` +create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key) unique, col_int_key int); +ERROR HY000: Function or expression 'AUTO_INCREMENT' cannot be used in the GENERATED ALWAYS AS clause of `pk` +create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key), col_int_key int); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `pk` int(11) NOT NULL AUTO_INCREMENT, + `col_int_nokey` int(11) GENERATED ALWAYS AS (`pk` + `col_int_key`) VIRTUAL, + `col_int_key` int(11) DEFAULT NULL, + PRIMARY KEY (`pk`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci +insert t1 (col_int_key) values (10),(20),(30); +select * from t1; +pk col_int_nokey col_int_key +1 11 10 +2 22 20 +3 33 30 +drop table t1; # Bug#20339347: FAIL TO USE CREATE ....SELECT STATEMENT TO CREATE A NEW TABLE create table t1 (a int, b int generated always as(-a) virtual, c int generated always as (b + 1) stored); insert into t1(a) values(1),(2); diff --git a/mysql-test/suite/vcol/r/upgrade.result b/mysql-test/suite/vcol/r/upgrade.result index fea6588a5f3..5393a3543dc 100644 --- a/mysql-test/suite/vcol/r/upgrade.result +++ b/mysql-test/suite/vcol/r/upgrade.result @@ -6,7 +6,7 @@ show create table vcol_autoinc; Table Create Table vcol_autoinc CREATE TABLE `vcol_autoinc` ( `pk` int(11) NOT NULL AUTO_INCREMENT, - `v3` int(11) GENERATED ALWAYS AS (`pk`) VIRTUAL, + `v3` int(11) GENERATED ALWAYS AS (`pk`) STORED, PRIMARY KEY (`pk`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci select * from vcol_autoinc; diff --git a/sql/field.h b/sql/field.h index 757a6cc23d0..ce15184ea1d 100644 --- a/sql/field.h +++ b/sql/field.h @@ -498,6 +498,8 @@ enum enum_vcol_info_type VCOL_GENERATED_VIRTUAL, VCOL_GENERATED_STORED, VCOL_DEFAULT, VCOL_CHECK_FIELD, VCOL_CHECK_TABLE, /* Additional types should be added here */ + + VCOL_GENERATED_VIRTUAL_INDEXED, // this is never written in .frm /* Following is the highest value last */ VCOL_TYPE_NONE = 127 // Since the 0 value is already in use }; @@ -507,6 +509,7 @@ static inline const char *vcol_type_name(enum_vcol_info_type type) switch (type) { case VCOL_GENERATED_VIRTUAL: + case VCOL_GENERATED_VIRTUAL_INDEXED: case VCOL_GENERATED_STORED: return "GENERATED ALWAYS AS"; case VCOL_DEFAULT: diff --git a/sql/table.cc b/sql/table.cc index 54a1e8b1b57..b2d8c4676e6 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -2629,6 +2629,8 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write, share->stored_fields--; if (reg_field->flags & BLOB_FLAG) share->virtual_not_stored_blob_fields++; + if (reg_field->flags & PART_KEY_FLAG) + vcol_info->set_vcol_type(VCOL_GENERATED_VIRTUAL_INDEXED); /* Correct stored_rec_length as non stored fields are last */ recpos= (uint) (reg_field->ptr - record); if (share->stored_rec_length >= recpos) @@ -3159,7 +3161,7 @@ bool Virtual_column_info::fix_and_check_expr(THD *thd, TABLE *table) get_vcol_type_name(), name.str); DBUG_RETURN(1); } - else if (unlikely(res.errors & VCOL_AUTO_INC)) + else if (res.errors & VCOL_AUTO_INC && vcol_type != VCOL_GENERATED_VIRTUAL) { /* An auto_increment field may not be used in an expression for