Skip to content

Commit 3687ede

Browse files
committed
clarify the order of evaluation for INSERT
1 parent f93a2a3 commit 3687ede

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

mysql-test/r/default.result

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3068,3 +3068,11 @@ t1 CREATE TABLE `t1` (
30683068
`a` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT concat('A')
30693069
) ENGINE=MyISAM DEFAULT CHARSET=latin1
30703070
DROP TABLE t1;
3071+
create table t1 (a int default 1, b int default (1+1), c int);
3072+
insert t1 (c) values (a);
3073+
insert t1 (c) values (b);
3074+
select * from t1;
3075+
a b c
3076+
1 2 1
3077+
1 2 NULL
3078+
drop table t1;

mysql-test/t/default.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,3 +1840,12 @@ CREATE TABLE t1 (a VARCHAR(20) CHARACTER SET latin1 DEFAULT CONCAT('
18401840
CREATE OR REPLACE TABLE t1 (a char(2) default concat('A') COLLATE utf8mb4_unicode_ci);
18411841
SHOW CREATE TABLE t1;
18421842
DROP TABLE t1;
1843+
1844+
#
1845+
# Order of evaluation:
1846+
#
1847+
create table t1 (a int default 1, b int default (1+1), c int);
1848+
insert t1 (c) values (a);
1849+
insert t1 (c) values (b);
1850+
select * from t1;
1851+
drop table t1;

sql/item.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,13 +942,16 @@ bool Item_field::register_field_in_write_map(void *arg)
942942
943943
Fields are initialized in this order:
944944
- All fields that have default value as a constant are initialized first.
945+
- Then user-specified values from the INSERT list
945946
- Then all fields that has a default expression, in field_index order.
946947
- Last all virtual fields, in field_index order.
947948
948949
This means:
949950
- For default fields we can't access the same field or a field after
950951
itself that doesn't have a non-constant default value.
951952
- A virtual fields can't access itself or a virtual field after itself.
953+
- user-specified values will not see virtual fields or default expressions,
954+
as in INSERT t1 (a) VALUES (b);
952955
953956
This is used by fix_vcol_expr() when a table is opened
954957

0 commit comments

Comments
 (0)