Skip to content

Commit 5e42511

Browse files
authored
SQL: SELECT from archive table [closes #127]
1 parent c5801dd commit 5e42511

File tree

6 files changed

+56
-8
lines changed

6 files changed

+56
-8
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ ERROR HY000: VTMD error: `test.t0_vtmd` exists and not empty!
8686
drop table t0_vtmd;
8787
create table t0 (y int) with system versioning;
8888
create or replace table t0 (x int) with system versioning;
89+
insert into t0 values (1);
90+
set @t0= now(6);
8991
alter table t0 add column (y int);
92+
select * from t0 for system_time as of @t0;
93+
x
94+
1
95+
select * from t0;
96+
x y
97+
1 NULL
9098
call check_vtmd('t0_vtmd');
9199
@start > 0 and @start < @inf
92100
1

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ create table t0 (y int) with system versioning;
8686
create or replace table t0 (x int) with system versioning;
8787

8888
# alter
89+
insert into t0 values (1);
90+
set @t0= now(6);
8991
alter table t0 add column (y int);
92+
select * from t0 for system_time as of @t0;
93+
select * from t0;
9094
call check_vtmd('t0_vtmd');
9195

9296
call drop_archives('t0_vtmd');

sql/sql_parse.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113

114114
#include "wsrep_mysqld.h"
115115
#include "wsrep_thd.h"
116+
#include "vtmd.h"
116117

117118
static void wsrep_mysql_parse(THD *thd, char *rawbuf, uint length,
118119
Parser_state *parser_state,
@@ -6373,6 +6374,21 @@ static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)
63736374
if (check_dependencies_in_with_clauses(lex->with_clauses_list))
63746375
return 1;
63756376

6377+
if (thd->variables.vers_alter_history == VERS_ALTER_HISTORY_SURVIVE)
6378+
{
6379+
for (TABLE_LIST *table= all_tables; table; table= table->next_local)
6380+
{
6381+
if (table->vers_conditions)
6382+
{
6383+
VTMD_exists vtmd(*table);
6384+
if (vtmd.check_exists(thd))
6385+
return 1;
6386+
if (vtmd.exists && vtmd.setup_select(thd))
6387+
return 1;
6388+
}
6389+
}
6390+
}
6391+
63766392
if (!(res= open_and_lock_tables(thd, all_tables, TRUE, 0)))
63776393
{
63786394
if (lex->describe)

sql/sql_show.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,12 +1280,15 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list)
12801280
if (vtmd.find_archive_name(thd, archive_name))
12811281
goto exit;
12821282

1283-
archive.init_one_table(table_list->db, table_list->db_length, archive_name.ptr(),
1284-
archive_name.length(), archive_name.ptr(), TL_READ);
1283+
if (archive_name.length() > 0)
1284+
{
1285+
archive.init_one_table(table_list->db, table_list->db_length, archive_name.ptr(),
1286+
archive_name.length(), archive_name.ptr(), TL_READ);
12851287

1286-
archive.alias= table_list->table_name;
1287-
archive.vers_force_alias= true;
1288-
table_list= &archive;
1288+
archive.alias= table_list->table_name;
1289+
archive.vers_force_alias= true;
1290+
table_list= &archive;
1291+
}
12891292
}
12901293

12911294
if (mysqld_show_create_get_fields(thd, table_list, &field_list, &buffer))

sql/vtmd.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "table_cache.h" // tdc_remove_table()
88
#include "key.h"
99
#include "sql_show.h"
10+
#include "sql_parse.h"
11+
#include "sql_lex.h"
1012

1113
LString VERS_VTMD_TEMPLATE(C_STRING_WITH_LEN("vtmd_template"));
1214

@@ -546,8 +548,6 @@ VTMD_table::find_archive_name(THD *thd, String &out)
546548
if (select->skip_record(thd) > 0)
547549
{
548550
vtmd.table->field[FLD_ARCHIVE_NAME]->val_str(&out);
549-
if (out.length() == 0) // Handle AS OF NOW or RENAME TABLE case
550-
out.set(about.table_name, about.table_name_length, system_charset_info);
551551
break;
552552
}
553553
}
@@ -644,3 +644,20 @@ VTMD_table::get_archive_tables(THD *thd, const char *db, size_t db_length,
644644

645645
return false;
646646
}
647+
648+
bool VTMD_table::setup_select(THD* thd)
649+
{
650+
SString archive_name;
651+
if (find_archive_name(thd, archive_name))
652+
return true;
653+
654+
if (archive_name.length() == 0)
655+
return false;
656+
657+
about.table_name= (char *) thd->memdup(archive_name.c_ptr_safe(), archive_name.length() + 1);
658+
about.table_name_length= archive_name.length();
659+
DBUG_ASSERT(!about.mdl_request.ticket);
660+
about.mdl_request.init(MDL_key::TABLE, about.db, about.table_name,
661+
about.mdl_request.type, about.mdl_request.duration);
662+
return false;
663+
}

sql/vtmd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class VTMD_table
5252

5353
protected:
5454
TABLE_LIST vtmd;
55-
const TABLE_LIST &about;
55+
TABLE_LIST &about;
5656
SString_t vtmd_name;
5757

5858
private:

0 commit comments

Comments
 (0)