Skip to content

Commit f51ecfe

Browse files
committed
MDEV-15146 SQLError[4122]: View is not system versioned
don't expand AS OF in views, and, in particular, don't auto-add AS OF NOW().
1 parent 39157fb commit f51ecfe

File tree

5 files changed

+88
-15
lines changed

5 files changed

+88
-15
lines changed

mysql-test/suite/versioning/r/view.result

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ x
1717
create or replace view vt1 as select * from t1;
1818
show create view vt1;
1919
View Create View character_set_client collation_connection
20-
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t1`.`x` AS `x` from `t1` FOR SYSTEM_TIME ALL where `t1`.`row_end` = MAX_RESULT or `t1`.`row_end` is nulllatin1 latin1_swedish_ci
20+
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t1`.`x` AS `x` from `t1` latin1 latin1_swedish_ci
2121
drop view vt1;
2222
drop view vt2;
2323
create or replace view vt1 as select * from t1 for system_time all;
@@ -69,7 +69,7 @@ create or replace table t1 (x int) with system versioning;
6969
create or replace view vt1(c) as select x from t1;
7070
show create view vt1;
7171
View Create View character_set_client collation_connection
72-
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t1`.`x` AS `c` from `t1` FOR SYSTEM_TIME ALL where `t1`.`row_end` = MAX_RESULT or `t1`.`row_end` is nulllatin1 latin1_swedish_ci
72+
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t1`.`x` AS `c` from `t1` latin1 latin1_swedish_ci
7373
# VIEW over JOIN of versioned tables [#153]
7474
create or replace table t1 (a int) with system versioning;
7575
create or replace table t2 (b int) with system versioning;
@@ -87,15 +87,15 @@ create or replace table t3 (x int);
8787
create or replace view vt1 as select * from t1, t2, t3;
8888
show create view vt1;
8989
View Create View character_set_client collation_connection
90-
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t1`.`a` AS `a`,`t2`.`b` AS `b`,`t3`.`x` AS `x` from ((`t1` FOR SYSTEM_TIME ALL join `t2` FOR SYSTEM_TIME ALL) join `t3`) where (`t1`.`row_end` = MAX_RESULT or `t1`.`row_end` is null) and (`t2`.`row_end` = MAX_RESULT or `t2`.`row_end` is null) latin1 latin1_swedish_ci
90+
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t1`.`a` AS `a`,`t2`.`b` AS `b`,`t3`.`x` AS `x` from ((`t1` join `t2`) join `t3`) latin1 latin1_swedish_ci
9191
create or replace view vt1 as select * from t3, t2, t1;
9292
show create view vt1;
9393
View Create View character_set_client collation_connection
94-
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t3`.`x` AS `x`,`t2`.`b` AS `b`,`t1`.`a` AS `a` from ((`t3` join `t2` FOR SYSTEM_TIME ALL) join `t1` FOR SYSTEM_TIME ALL) where (`t2`.`row_end` = MAX_RESULT or `t2`.`row_end` is null) and (`t1`.`row_end` = MAX_RESULT or `t1`.`row_end` is null) latin1 latin1_swedish_ci
94+
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t3`.`x` AS `x`,`t2`.`b` AS `b`,`t1`.`a` AS `a` from ((`t3` join `t2`) join `t1`) latin1 latin1_swedish_ci
9595
create or replace view vt1 as select a, t2.row_end as endo from t3, t1, t2;
9696
show create view vt1;
9797
View Create View character_set_client collation_connection
98-
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t1`.`a` AS `a`,`t2`.`row_end` AS `endo` from ((`t3` join `t1` FOR SYSTEM_TIME ALL) join `t2` FOR SYSTEM_TIME ALL) where (`t1`.`row_end` = MAX_RESULT or `t1`.`row_end` is null) and (`t2`.`row_end` = MAX_RESULT or `t2`.`row_end` is null) latin1 latin1_swedish_ci
98+
vt1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vt1` AS select `t1`.`a` AS `a`,`t2`.`row_end` AS `endo` from ((`t3` join `t1`) join `t2`) latin1 latin1_swedish_ci
9999
# VIEW over UNION [#269]
100100
create or replace view vt1 as select * from t1 union select * from t1;
101101
select * from vt1;
@@ -118,3 +118,20 @@ execute stmt;
118118
a
119119
drop database test;
120120
create database test;
121+
use test;
122+
create table t1 (a int) with system versioning;
123+
insert t1 values (1),(2);
124+
set @a=now(6);
125+
create view v1 as select * from t1;
126+
delete from t1;
127+
select * from v1;
128+
a
129+
select * from v1 for system_time as of @a;
130+
a
131+
1
132+
2
133+
show create view v1;
134+
View Create View character_set_client collation_connection
135+
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` latin1 latin1_swedish_ci
136+
drop view v1;
137+
drop table t1;

mysql-test/suite/versioning/t/view.test

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,21 @@ create or replace table t2 (b int) with system versioning;
9696
prepare stmt from 'select a from v1 inner join t2 group by a order by a';
9797
execute stmt;
9898
execute stmt;
99-
100-
10199
drop database test;
102100
create database test;
101+
use test;
102+
103+
#
104+
# MDEV-15146 SQLError[4122]: View is not system versioned
105+
#
106+
107+
create table t1 (a int) with system versioning;
108+
insert t1 values (1),(2);
109+
set @a=now(6);
110+
create view v1 as select * from t1;
111+
delete from t1;
112+
select * from v1;
113+
select * from v1 for system_time as of @a;
114+
show create view v1;
115+
drop view v1;
116+
drop table t1;

sql/share/errmsg-utf8.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7825,7 +7825,7 @@ ER_VERS_FIELD_WRONG_TYPE
78257825
ER_VERS_ENGINE_UNSUPPORTED
78267826
eng "Transaction system versioning for %`s is not supported"
78277827

7828-
ER_UNUSED_22
7828+
ER_UNUSED_23
78297829
eng "You should never see it"
78307830

78317831
ER_PARTITION_WRONG_TYPE

sql/sql_select.cc

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,47 @@ bool vers_select_conds_t::init_from_sysvar(THD *thd)
690690
return false;
691691
}
692692

693+
void vers_select_conds_t::print(String *str, enum_query_type query_type)
694+
{
695+
const static LEX_CSTRING unit_type[]=
696+
{
697+
{ STRING_WITH_LEN("") },
698+
{ STRING_WITH_LEN("TIMESTAMP ") },
699+
{ STRING_WITH_LEN("TRANACTION ") }
700+
};
701+
switch (type) {
702+
case SYSTEM_TIME_UNSPECIFIED:
703+
break;
704+
case SYSTEM_TIME_AS_OF:
705+
str->append(STRING_WITH_LEN(" FOR SYSTEM_TIME AS OF "));
706+
str->append(unit_type + unit_start);
707+
start->print(str, query_type);
708+
break;
709+
case SYSTEM_TIME_FROM_TO:
710+
str->append(STRING_WITH_LEN(" FOR SYSTEM_TIME FROM "));
711+
str->append(unit_type + unit_start);
712+
start->print(str, query_type);
713+
str->append(STRING_WITH_LEN(" TO "));
714+
str->append(unit_type + unit_end);
715+
end->print(str, query_type);
716+
break;
717+
case SYSTEM_TIME_BETWEEN:
718+
str->append(STRING_WITH_LEN(" FOR SYSTEM_TIME BETWEEN "));
719+
str->append(unit_type + unit_start);
720+
start->print(str, query_type);
721+
str->append(STRING_WITH_LEN(" AND "));
722+
str->append(unit_type + unit_end);
723+
end->print(str, query_type);
724+
break;
725+
case SYSTEM_TIME_BEFORE:
726+
DBUG_ASSERT(0);
727+
break;
728+
case SYSTEM_TIME_ALL:
729+
str->append(" FOR SYSTEM_TIME ALL");
730+
break;
731+
}
732+
}
733+
693734
int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr)
694735
{
695736
DBUG_ENTER("SELECT_LEX::vers_setup_cond");
@@ -704,6 +745,9 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
704745
DBUG_RETURN(0);
705746
}
706747

748+
if (thd->lex->is_view_context_analysis())
749+
DBUG_RETURN(0);
750+
707751
if (!versioned_tables)
708752
{
709753
for (table= tables; table; table= table->next_local)
@@ -977,6 +1021,7 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
9771021
DBUG_ASSERT(0);
9781022
}
9791023
}
1024+
vers_conditions.type= SYSTEM_TIME_ALL;
9801025

9811026
if (cond1)
9821027
{
@@ -1005,9 +1050,6 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
10051050
this->where->top_level_item();
10061051
}
10071052

1008-
if (outer_table)
1009-
outer_table->vers_conditions.type= SYSTEM_TIME_ALL;
1010-
10111053
// Invalidate current SP [#52, #422]
10121054
if (thd->spcont)
10131055
{
@@ -25998,10 +26040,8 @@ void TABLE_LIST::print(THD *thd, table_map eliminated_tables, String *str,
2599826040
#endif /* WITH_PARTITION_STORAGE_ENGINE */
2599926041
}
2600026042
if (table && table->versioned())
26001-
{
26002-
// versioning conditions are already unwrapped to WHERE clause
26003-
str->append(" FOR SYSTEM_TIME ALL");
26004-
}
26043+
vers_conditions.print(str, query_type);
26044+
2600526045
if (my_strcasecmp(table_alias_charset, cmp_name, alias.str))
2600626046
{
2600726047
char t_alias_buff[MAX_ALIAS_NAME];

sql/table.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,6 +1900,8 @@ struct vers_select_conds_t
19001900
Item * s= NULL, vers_sys_type_t u_end= VERS_UNDEFINED,
19011901
Item * e= NULL);
19021902

1903+
void print(String *str, enum_query_type query_type);
1904+
19031905
bool init_from_sysvar(THD *thd);
19041906

19051907
bool operator== (vers_system_time_t b)

0 commit comments

Comments
 (0)