Skip to content

Commit d1f3763

Browse files
committed
bugfix: non-deterministic vcols in indexes
1 parent 6b0f4c2 commit d1f3763

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

mysql-test/suite/vcol/inc/vcol_keys.inc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,27 @@ drop table t1;
157157
--echo # - vcol_ins_upd.inc
158158
--echo # - vcol_select.inc
159159

160-
--echo #
161-
--echo # TODO: CHECK
160+
#
161+
# Restrictions when indexed:
162+
#
163+
164+
--error ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED
165+
create table t1 (a int, b timestamp as (now()), key (b));
166+
create table t1 (a int, b timestamp as (now()));
167+
--error ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED
168+
alter table t1 add index (b);
169+
drop table t1;
170+
171+
--error ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED
172+
create table t1 (a int, b varchar(100) as (user()), key (b));
173+
create table t1 (a int, b varchar(100) as (user()));
174+
--error ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED
175+
alter table t1 add index (b);
176+
drop table t1;
162177

178+
--error ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED
179+
create table t1 (a int, b double as (rand()), key (b));
180+
create table t1 (a int, b double as (rand()));
181+
--error ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED
182+
alter table t1 add index (b);
183+
drop table t1;

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,21 @@ drop table t1;
150150
# on virtual columns can be found in:
151151
# - vcol_ins_upd.inc
152152
# - vcol_select.inc
153-
#
154-
# TODO: CHECK
153+
create table t1 (a int, b timestamp as (now()), key (b));
154+
ERROR HY000: Function or expression 'current_timestamp()' cannot be used in the GENERATED ALWAYS AS clause of `b`
155+
create table t1 (a int, b timestamp as (now()));
156+
alter table t1 add index (b);
157+
ERROR HY000: Function or expression 'current_timestamp()' cannot be used in the GENERATED ALWAYS AS clause of `b`
158+
drop table t1;
159+
create table t1 (a int, b varchar(100) as (user()), key (b));
160+
ERROR HY000: Function or expression 'user()' cannot be used in the GENERATED ALWAYS AS clause of `b`
161+
create table t1 (a int, b varchar(100) as (user()));
162+
alter table t1 add index (b);
163+
ERROR HY000: Function or expression 'user()' cannot be used in the GENERATED ALWAYS AS clause of `b`
164+
drop table t1;
165+
create table t1 (a int, b double as (rand()), key (b));
166+
ERROR HY000: Function or expression 'rand()' cannot be used in the GENERATED ALWAYS AS clause of `b`
167+
create table t1 (a int, b double as (rand()));
168+
alter table t1 add index (b);
169+
ERROR HY000: Function or expression 'rand()' cannot be used in the GENERATED ALWAYS AS clause of `b`
170+
drop table t1;

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,21 @@ drop table t1;
260260
# on virtual columns can be found in:
261261
# - vcol_ins_upd.inc
262262
# - vcol_select.inc
263-
#
264-
# TODO: CHECK
263+
create table t1 (a int, b timestamp as (now()), key (b));
264+
ERROR HY000: Function or expression 'current_timestamp()' cannot be used in the GENERATED ALWAYS AS clause of `b`
265+
create table t1 (a int, b timestamp as (now()));
266+
alter table t1 add index (b);
267+
ERROR HY000: Function or expression 'current_timestamp()' cannot be used in the GENERATED ALWAYS AS clause of `b`
268+
drop table t1;
269+
create table t1 (a int, b varchar(100) as (user()), key (b));
270+
ERROR HY000: Function or expression 'user()' cannot be used in the GENERATED ALWAYS AS clause of `b`
271+
create table t1 (a int, b varchar(100) as (user()));
272+
alter table t1 add index (b);
273+
ERROR HY000: Function or expression 'user()' cannot be used in the GENERATED ALWAYS AS clause of `b`
274+
drop table t1;
275+
create table t1 (a int, b double as (rand()), key (b));
276+
ERROR HY000: Function or expression 'rand()' cannot be used in the GENERATED ALWAYS AS clause of `b`
277+
create table t1 (a int, b double as (rand()));
278+
alter table t1 add index (b);
279+
ERROR HY000: Function or expression 'rand()' cannot be used in the GENERATED ALWAYS AS clause of `b`
280+
drop table t1;

sql/sql_table.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3877,10 +3877,21 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
38773877
}
38783878
}
38793879
#endif
3880-
if (key->type == Key::PRIMARY && sql_field->vcol_info)
3880+
if (sql_field->vcol_info)
38813881
{
3882-
my_error(ER_PRIMARY_KEY_BASED_ON_VIRTUAL_COLUMN, MYF(0));
3883-
DBUG_RETURN(TRUE);
3882+
if (key->type == Key::PRIMARY)
3883+
{
3884+
my_error(ER_PRIMARY_KEY_BASED_ON_VIRTUAL_COLUMN, MYF(0));
3885+
DBUG_RETURN(TRUE);
3886+
}
3887+
if (sql_field->vcol_info->flags & VCOL_NOT_STRICTLY_DETERMINISTIC)
3888+
{
3889+
/* use check_expression() to report an error */
3890+
check_expression(sql_field->vcol_info, sql_field->field_name,
3891+
VCOL_GENERATED_STORED);
3892+
DBUG_ASSERT(thd->is_error());
3893+
DBUG_RETURN(TRUE);
3894+
}
38843895
}
38853896
if (!(sql_field->flags & NOT_NULL_FLAG))
38863897
{

0 commit comments

Comments
 (0)