Skip to content

Commit ae53f68

Browse files
committed
MDEV-30016 Virtual columns do not support autoincrement columns
change vcol_upgrade test to use stored gcols
1 parent a6b327e commit ae53f68

File tree

8 files changed

+55
-7
lines changed

8 files changed

+55
-7
lines changed

mysql-test/std_data/vcol_autoinc.MYI

0 Bytes
Binary file not shown.

mysql-test/std_data/vcol_autoinc.frm

7.46 KB
Binary file not shown.

mysql-test/suite/gcol/inc/gcol_column_def_options.inc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ alter table t1 add column (h int generated always as (a+1) virtual, i int as(5)
4949
drop table t1;
5050

5151
--echo # DEFAULT
52-
--error 1064
52+
--error ER_PARSE_ERROR
5353
create table t1 (a int, b int generated always as (a+1) virtual default 0);
5454
create table t1 (a int);
55-
--error 1064
55+
--error ER_PARSE_ERROR
5656
alter table t1 add column b int generated always as (a+1) virtual default 0;
5757
drop table t1;
5858

5959
--echo # AUTO_INCREMENT
60-
--error 1064
60+
--error ER_PARSE_ERROR
6161
create table t1 (a int, b int generated always as (a+1) virtual AUTO_INCREMENT);
6262
create table t1 (a int);
63-
--error 1064
63+
--error ER_PARSE_ERROR
6464
alter table t1 add column b int generated always as (a+1) virtual AUTO_INCREMENT;
6565
drop table t1;
6666

@@ -138,7 +138,7 @@ create table t1 (a int, b int generated always as (a % 2) stored references t2(a
138138
show create table t1;
139139
drop table t1;
140140
create table t1 (a int, b int generated always as (a % 2) virtual);
141-
--error 1064
141+
--error ER_PARSE_ERROR
142142
alter table t1 modify b int generated always as (a % 2) stored references t2(a);
143143
show create table t1;
144144
drop table t1;
@@ -199,6 +199,13 @@ create table t1 (a int, b int generated always as(-b) virtual, c int generated a
199199
create table t1 (a int, b int generated always as(-c) virtual, c int generated always as (b + 1) virtual);
200200
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
201201
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);
202+
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
203+
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);
204+
create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key), col_int_key int);
205+
show create table t1;
206+
insert t1 (col_int_key) values (10),(20),(30);
207+
select * from t1;
208+
drop table t1;
202209

203210
--echo # Bug#20339347: FAIL TO USE CREATE ....SELECT STATEMENT TO CREATE A NEW TABLE
204211
create table t1 (a int, b int generated always as(-a) virtual, c int generated always as (b + 1) stored);

mysql-test/suite/gcol/r/gcol_column_def_options_innodb.result

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,24 @@ create table t1 (a int, b int generated always as(-c) virtual, c int generated a
260260
ERROR 01000: Expression for field `b` is referring to uninitialized field `c`
261261
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);
262262
ERROR HY000: Function or expression 'AUTO_INCREMENT' cannot be used in the GENERATED ALWAYS AS clause of `pk`
263+
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);
264+
ERROR HY000: Function or expression 'AUTO_INCREMENT' cannot be used in the GENERATED ALWAYS AS clause of `pk`
265+
create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key), col_int_key int);
266+
show create table t1;
267+
Table Create Table
268+
t1 CREATE TABLE `t1` (
269+
`pk` int(11) NOT NULL AUTO_INCREMENT,
270+
`col_int_nokey` int(11) GENERATED ALWAYS AS (`pk` + `col_int_key`) VIRTUAL,
271+
`col_int_key` int(11) DEFAULT NULL,
272+
PRIMARY KEY (`pk`)
273+
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
274+
insert t1 (col_int_key) values (10),(20),(30);
275+
select * from t1;
276+
pk col_int_nokey col_int_key
277+
1 11 10
278+
2 22 20
279+
3 33 30
280+
drop table t1;
263281
# Bug#20339347: FAIL TO USE CREATE ....SELECT STATEMENT TO CREATE A NEW TABLE
264282
create table t1 (a int, b int generated always as(-a) virtual, c int generated always as (b + 1) stored);
265283
insert into t1(a) values(1),(2);

mysql-test/suite/gcol/r/gcol_column_def_options_myisam.result

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,24 @@ create table t1 (a int, b int generated always as(-c) virtual, c int generated a
260260
ERROR 01000: Expression for field `b` is referring to uninitialized field `c`
261261
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);
262262
ERROR HY000: Function or expression 'AUTO_INCREMENT' cannot be used in the GENERATED ALWAYS AS clause of `pk`
263+
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);
264+
ERROR HY000: Function or expression 'AUTO_INCREMENT' cannot be used in the GENERATED ALWAYS AS clause of `pk`
265+
create table t1 (pk int auto_increment primary key, col_int_nokey int generated always as (pk + col_int_key), col_int_key int);
266+
show create table t1;
267+
Table Create Table
268+
t1 CREATE TABLE `t1` (
269+
`pk` int(11) NOT NULL AUTO_INCREMENT,
270+
`col_int_nokey` int(11) GENERATED ALWAYS AS (`pk` + `col_int_key`) VIRTUAL,
271+
`col_int_key` int(11) DEFAULT NULL,
272+
PRIMARY KEY (`pk`)
273+
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
274+
insert t1 (col_int_key) values (10),(20),(30);
275+
select * from t1;
276+
pk col_int_nokey col_int_key
277+
1 11 10
278+
2 22 20
279+
3 33 30
280+
drop table t1;
263281
# Bug#20339347: FAIL TO USE CREATE ....SELECT STATEMENT TO CREATE A NEW TABLE
264282
create table t1 (a int, b int generated always as(-a) virtual, c int generated always as (b + 1) stored);
265283
insert into t1(a) values(1),(2);

mysql-test/suite/vcol/r/upgrade.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ show create table vcol_autoinc;
66
Table Create Table
77
vcol_autoinc CREATE TABLE `vcol_autoinc` (
88
`pk` int(11) NOT NULL AUTO_INCREMENT,
9-
`v3` int(11) GENERATED ALWAYS AS (`pk`) VIRTUAL,
9+
`v3` int(11) GENERATED ALWAYS AS (`pk`) STORED,
1010
PRIMARY KEY (`pk`)
1111
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
1212
select * from vcol_autoinc;

sql/field.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ enum enum_vcol_info_type
498498
VCOL_GENERATED_VIRTUAL, VCOL_GENERATED_STORED,
499499
VCOL_DEFAULT, VCOL_CHECK_FIELD, VCOL_CHECK_TABLE,
500500
/* Additional types should be added here */
501+
502+
VCOL_GENERATED_VIRTUAL_INDEXED, // this is never written in .frm
501503
/* Following is the highest value last */
502504
VCOL_TYPE_NONE = 127 // Since the 0 value is already in use
503505
};
@@ -507,6 +509,7 @@ static inline const char *vcol_type_name(enum_vcol_info_type type)
507509
switch (type)
508510
{
509511
case VCOL_GENERATED_VIRTUAL:
512+
case VCOL_GENERATED_VIRTUAL_INDEXED:
510513
case VCOL_GENERATED_STORED:
511514
return "GENERATED ALWAYS AS";
512515
case VCOL_DEFAULT:

sql/table.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,8 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
26292629
share->stored_fields--;
26302630
if (reg_field->flags & BLOB_FLAG)
26312631
share->virtual_not_stored_blob_fields++;
2632+
if (reg_field->flags & PART_KEY_FLAG)
2633+
vcol_info->set_vcol_type(VCOL_GENERATED_VIRTUAL_INDEXED);
26322634
/* Correct stored_rec_length as non stored fields are last */
26332635
recpos= (uint) (reg_field->ptr - record);
26342636
if (share->stored_rec_length >= recpos)
@@ -3159,7 +3161,7 @@ bool Virtual_column_info::fix_and_check_expr(THD *thd, TABLE *table)
31593161
get_vcol_type_name(), name.str);
31603162
DBUG_RETURN(1);
31613163
}
3162-
else if (unlikely(res.errors & VCOL_AUTO_INC))
3164+
else if (res.errors & VCOL_AUTO_INC && vcol_type != VCOL_GENERATED_VIRTUAL)
31633165
{
31643166
/*
31653167
An auto_increment field may not be used in an expression for

0 commit comments

Comments
 (0)