Skip to content

Commit db827c3

Browse files
author
Benny Wang
committed
Fixed bug#22179637: INSERT INTO TABLE FROM SELECT ACCEPTS TO INSERT INTO
GENERATED COLUMNS In current version, miss to constraint generated columns assignment to be only DEFAULT value for INSERT...SELECT statement. This patch provides such a check.
1 parent e067983 commit db827c3

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

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

+33
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ select a,b,c from t1;
105105
--echo # CREATE new_table ... LIKE old_table
106106
--echo # INSERT INTO new_table SELECT * from old_table
107107
create table t2 like t1;
108+
--error ER_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN
108109
insert into t2 select * from t1;
110+
insert into t2(a) select a from t1;
109111
select * from t1;
110112
drop table t2;
111113

@@ -114,7 +116,9 @@ drop table t2;
114116
insert into t1 values (1,default,default);
115117
select * from t1;
116118
create table t2 like t1;
119+
--error ER_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN
117120
insert into t2 (a,b) select a,b from t1;
121+
insert into t2 (a) select a from t1;
118122
select * from t2;
119123
drop table t2;
120124
drop table t1;
@@ -526,3 +530,32 @@ INSERT INTO t2(c2) VALUES(DEFAULT);
526530
SELECT * FROM t2;
527531
DROP TABLE t, t1, t2;
528532

533+
--echo # Bug#22179637: INSERT INTO TABLE FROM SELECT ACCEPTS TO INSERT INTO
534+
--echo # GENERATED COLUMNS
535+
CREATE TABLE t1 (
536+
i1 INTEGER,
537+
i2 INTEGER GENERATED ALWAYS AS (i1 + i1)
538+
);
539+
--error ER_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN
540+
INSERT INTO t1 (i1, i2) SELECT 5, 6;
541+
INSERT INTO t1 (i1) SELECT 5;
542+
--error ER_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN
543+
INSERT INTO t1 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= 4;
544+
INSERT INTO t1 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= DEFAULT;
545+
SELECT * FROM t1;
546+
547+
CREATE TABLE t2 (
548+
i1 INTEGER,
549+
i2 INTEGER GENERATED ALWAYS AS (i1 + i1) STORED
550+
);
551+
--error ER_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN
552+
INSERT INTO t2 (i1, i2) SELECT 5, 6;
553+
--error ER_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN
554+
INSERT INTO t2 (i1, i2) SELECT * FROM t1;
555+
INSERT INTO t2 (i1) SELECT 5;
556+
--error ER_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN
557+
INSERT INTO t2 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= 4;
558+
INSERT INTO t2 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= DEFAULT;
559+
SELECT * FROM t2;
560+
561+
DROP TABLE t1,t2;

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

+37
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ a b c
9898
# INSERT INTO new_table SELECT * from old_table
9999
create table t2 like t1;
100100
insert into t2 select * from t1;
101+
ERROR HY000: The value specified for generated column 'b' in table 't2' is not allowed.
102+
insert into t2(a) select a from t1;
101103
select * from t1;
102104
a b c
103105
2 -2 -2
@@ -111,6 +113,8 @@ a b c
111113
1 -1 -1
112114
create table t2 like t1;
113115
insert into t2 (a,b) select a,b from t1;
116+
ERROR HY000: The value specified for generated column 'b' in table 't2' is not allowed.
117+
insert into t2 (a) select a from t1;
114118
select * from t2;
115119
a b c
116120
2 -2 -2
@@ -650,6 +654,39 @@ SELECT * FROM t2;
650654
c1 c2
651655
1 2
652656
DROP TABLE t, t1, t2;
657+
# Bug#22179637: INSERT INTO TABLE FROM SELECT ACCEPTS TO INSERT INTO
658+
# GENERATED COLUMNS
659+
CREATE TABLE t1 (
660+
i1 INTEGER,
661+
i2 INTEGER GENERATED ALWAYS AS (i1 + i1)
662+
);
663+
INSERT INTO t1 (i1, i2) SELECT 5, 6;
664+
ERROR HY000: The value specified for generated column 'i2' in table 't1' is not allowed.
665+
INSERT INTO t1 (i1) SELECT 5;
666+
INSERT INTO t1 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= 4;
667+
ERROR HY000: The value specified for generated column 'i2' in table 't1' is not allowed.
668+
INSERT INTO t1 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= DEFAULT;
669+
SELECT * FROM t1;
670+
i1 i2
671+
5 10
672+
5 10
673+
CREATE TABLE t2 (
674+
i1 INTEGER,
675+
i2 INTEGER GENERATED ALWAYS AS (i1 + i1) STORED
676+
);
677+
INSERT INTO t2 (i1, i2) SELECT 5, 6;
678+
ERROR HY000: The value specified for generated column 'i2' in table 't2' is not allowed.
679+
INSERT INTO t2 (i1, i2) SELECT * FROM t1;
680+
ERROR HY000: The value specified for generated column 'i2' in table 't2' is not allowed.
681+
INSERT INTO t2 (i1) SELECT 5;
682+
INSERT INTO t2 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= 4;
683+
ERROR HY000: The value specified for generated column 'i2' in table 't2' is not allowed.
684+
INSERT INTO t2 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= DEFAULT;
685+
SELECT * FROM t2;
686+
i1 i2
687+
5 10
688+
5 10
689+
DROP TABLE t1,t2;
653690
DROP VIEW IF EXISTS v1,v2;
654691
DROP TABLE IF EXISTS t1,t2,t3;
655692
DROP PROCEDURE IF EXISTS p1;

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

+37
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ a b c
9898
# INSERT INTO new_table SELECT * from old_table
9999
create table t2 like t1;
100100
insert into t2 select * from t1;
101+
ERROR HY000: The value specified for generated column 'b' in table 't2' is not allowed.
102+
insert into t2(a) select a from t1;
101103
select * from t1;
102104
a b c
103105
2 -2 -2
@@ -111,6 +113,8 @@ a b c
111113
1 -1 -1
112114
create table t2 like t1;
113115
insert into t2 (a,b) select a,b from t1;
116+
ERROR HY000: The value specified for generated column 'b' in table 't2' is not allowed.
117+
insert into t2 (a) select a from t1;
114118
select * from t2;
115119
a b c
116120
2 -2 -2
@@ -500,6 +504,39 @@ SELECT * FROM t2;
500504
c1 c2
501505
1 2
502506
DROP TABLE t, t1, t2;
507+
# Bug#22179637: INSERT INTO TABLE FROM SELECT ACCEPTS TO INSERT INTO
508+
# GENERATED COLUMNS
509+
CREATE TABLE t1 (
510+
i1 INTEGER,
511+
i2 INTEGER GENERATED ALWAYS AS (i1 + i1)
512+
);
513+
INSERT INTO t1 (i1, i2) SELECT 5, 6;
514+
ERROR HY000: The value specified for generated column 'i2' in table 't1' is not allowed.
515+
INSERT INTO t1 (i1) SELECT 5;
516+
INSERT INTO t1 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= 4;
517+
ERROR HY000: The value specified for generated column 'i2' in table 't1' is not allowed.
518+
INSERT INTO t1 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= DEFAULT;
519+
SELECT * FROM t1;
520+
i1 i2
521+
5 10
522+
5 10
523+
CREATE TABLE t2 (
524+
i1 INTEGER,
525+
i2 INTEGER GENERATED ALWAYS AS (i1 + i1) STORED
526+
);
527+
INSERT INTO t2 (i1, i2) SELECT 5, 6;
528+
ERROR HY000: The value specified for generated column 'i2' in table 't2' is not allowed.
529+
INSERT INTO t2 (i1, i2) SELECT * FROM t1;
530+
ERROR HY000: The value specified for generated column 'i2' in table 't2' is not allowed.
531+
INSERT INTO t2 (i1) SELECT 5;
532+
INSERT INTO t2 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= 4;
533+
ERROR HY000: The value specified for generated column 'i2' in table 't2' is not allowed.
534+
INSERT INTO t2 (i1) SELECT 5 ON DUPLICATE KEY UPDATE i2= DEFAULT;
535+
SELECT * FROM t2;
536+
i1 i2
537+
5 10
538+
5 10
539+
DROP TABLE t1,t2;
503540
DROP VIEW IF EXISTS v1,v2;
504541
DROP TABLE IF EXISTS t1,t2,t3;
505542
DROP PROCEDURE IF EXISTS p1;

sql/sql_insert.cc

+11-2
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ bool Sql_cmd_insert_base::mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
13041304
insert_value_list, SELECT_ACL, NULL, false, false);
13051305
if (!res)
13061306
res= check_valid_table_refs(table_list, insert_value_list, map);
1307-
if (!res && lex->insert_table_leaf->table->vfield)
1307+
if (!res && lex->insert_table_leaf->table->has_gcol())
13081308
res= validate_gc_assignment(thd, &insert_field_list, values,
13091309
lex->insert_table_leaf->table);
13101310
thd->lex->in_update_value_clause= false;
@@ -1319,7 +1319,7 @@ bool Sql_cmd_insert_base::mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
13191319
insert_update_list, UPDATE_ACL, NULL, false, true);
13201320
if (!res)
13211321
res= check_valid_table_refs(table_list, insert_update_list, map);
1322-
if (!res && lex->insert_table_leaf->table->vfield)
1322+
if (!res && lex->insert_table_leaf->table->has_gcol())
13231323
res= validate_gc_assignment(thd, &insert_update_list,
13241324
&insert_value_list,
13251325
lex->insert_table_leaf->table);
@@ -2019,6 +2019,10 @@ int Query_result_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
20192019
res= setup_fields(thd, Ref_ptr_array(), values, SELECT_ACL, NULL,
20202020
false, false);
20212021

2022+
if (!res && lex->insert_table_leaf->table->has_gcol())
2023+
res= validate_gc_assignment(thd, fields, &values,
2024+
lex->insert_table_leaf->table);
2025+
20222026
if (duplicate_handling == DUP_UPDATE && !res)
20232027
{
20242028
Name_resolution_context *const context= &lex->select_lex->context;
@@ -2037,6 +2041,11 @@ int Query_result_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
20372041
if (!res)
20382042
res= setup_fields(thd, Ref_ptr_array(), *update.get_changed_columns(),
20392043
UPDATE_ACL, NULL, false, true);
2044+
2045+
if (!res && lex->insert_table_leaf->table->has_gcol())
2046+
res= validate_gc_assignment(thd, update.get_changed_columns(),
2047+
update.update_values,
2048+
lex->insert_table_leaf->table);
20402049
/*
20412050
When we are not using GROUP BY and there are no ungrouped aggregate
20422051
functions

0 commit comments

Comments
 (0)