Skip to content

Commit e2f70d6

Browse files
midenokvuvova
authored andcommitted
Vers SQL: Refactoring: better init of vers_conditions
Unit-based history point (vers_history_point_t; Vers_history_point).
1 parent df0e181 commit e2f70d6

File tree

4 files changed

+193
-198
lines changed

4 files changed

+193
-198
lines changed

sql/sql_select.cc

Lines changed: 36 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -677,53 +677,37 @@ bool vers_select_conds_t::init_from_sysvar(THD *thd)
677677
{
678678
vers_asof_timestamp_t &in= thd->variables.vers_asof_timestamp;
679679
type= (vers_system_time_t) in.type;
680-
unit_start= VERS_TIMESTAMP;
680+
start.unit= VERS_TIMESTAMP;
681681
from_query= false;
682682
if (type != SYSTEM_TIME_UNSPECIFIED && type != SYSTEM_TIME_ALL)
683683
{
684684
DBUG_ASSERT(type == SYSTEM_TIME_AS_OF);
685-
start= new (thd->mem_root)
685+
start.item= new (thd->mem_root)
686686
Item_datetime_literal(thd, &in.ltime, TIME_SECOND_PART_DIGITS);
687-
if (!start)
687+
if (!start.item)
688688
return true;
689689
}
690690
else
691-
start= NULL;
692-
end= NULL;
691+
start.item= NULL;
692+
end.empty();
693693
return false;
694694
}
695695

696696
void vers_select_conds_t::print(String *str, enum_query_type query_type)
697697
{
698-
const static LEX_CSTRING unit_type[]=
699-
{
700-
{ STRING_WITH_LEN("") },
701-
{ STRING_WITH_LEN("TIMESTAMP ") },
702-
{ STRING_WITH_LEN("TRANSACTION ") }
703-
};
704698
switch (type) {
705699
case SYSTEM_TIME_UNSPECIFIED:
706700
break;
707701
case SYSTEM_TIME_AS_OF:
708-
str->append(STRING_WITH_LEN(" FOR SYSTEM_TIME AS OF "));
709-
str->append(unit_type + unit_start);
710-
start->print(str, query_type);
702+
start.print(str, query_type, STRING_WITH_LEN(" FOR SYSTEM_TIME AS OF "));
711703
break;
712704
case SYSTEM_TIME_FROM_TO:
713-
str->append(STRING_WITH_LEN(" FOR SYSTEM_TIME FROM "));
714-
str->append(unit_type + unit_start);
715-
start->print(str, query_type);
716-
str->append(STRING_WITH_LEN(" TO "));
717-
str->append(unit_type + unit_end);
718-
end->print(str, query_type);
705+
start.print(str, query_type, STRING_WITH_LEN(" FOR SYSTEM_TIME FROM "));
706+
end.print(str, query_type, STRING_WITH_LEN(" TO "));
719707
break;
720708
case SYSTEM_TIME_BETWEEN:
721-
str->append(STRING_WITH_LEN(" FOR SYSTEM_TIME BETWEEN "));
722-
str->append(unit_type + unit_start);
723-
start->print(str, query_type);
724-
str->append(STRING_WITH_LEN(" AND "));
725-
str->append(unit_type + unit_end);
726-
end->print(str, query_type);
709+
start.print(str, query_type, STRING_WITH_LEN(" FOR SYSTEM_TIME BETWEEN "));
710+
end.print(str, query_type, STRING_WITH_LEN(" AND "));
727711
break;
728712
case SYSTEM_TIME_BEFORE:
729713
DBUG_ASSERT(0);
@@ -871,8 +855,8 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
871855
vers_system_time_t/VERS_TRX_ID at stage of fix_fields()
872856
(this is large refactoring). */
873857
vers_conditions.resolve_units(timestamps_only);
874-
if (timestamps_only && (vers_conditions.unit_start == VERS_TRX_ID ||
875-
vers_conditions.unit_end == VERS_TRX_ID))
858+
if (timestamps_only && (vers_conditions.start.unit == VERS_TRX_ID ||
859+
vers_conditions.end.unit == VERS_TRX_ID))
876860
{
877861
my_error(ER_VERS_ENGINE_UNSUPPORTED, MYF(0), table->table_name.str);
878862
DBUG_RETURN(-1);
@@ -889,53 +873,8 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
889873
{
890874
if (vers_conditions)
891875
{
892-
if (vers_conditions.start)
893-
{
894-
if (!vers_conditions.unit_start)
895-
vers_conditions.unit_start= t->s->versioned;
896-
switch (vers_conditions.unit_start)
897-
{
898-
case VERS_TIMESTAMP:
899-
{
900-
vers_conditions.start= newx Item_datetime_from_unixtime_typecast(
901-
thd, vers_conditions.start, 6);
902-
break;
903-
}
904-
case VERS_TRX_ID:
905-
{
906-
vers_conditions.start= newx Item_longlong_typecast(
907-
thd, vers_conditions.start);
908-
break;
909-
}
910-
default:
911-
DBUG_ASSERT(0);
912-
break;
913-
}
914-
}
915-
916-
if (vers_conditions.end)
917-
{
918-
if (!vers_conditions.unit_end)
919-
vers_conditions.unit_end= t->s->versioned;
920-
switch (vers_conditions.unit_end)
921-
{
922-
case VERS_TIMESTAMP:
923-
{
924-
vers_conditions.end= newx Item_datetime_from_unixtime_typecast(
925-
thd, vers_conditions.end, 6);
926-
break;
927-
}
928-
case VERS_TRX_ID:
929-
{
930-
vers_conditions.end= newx Item_longlong_typecast(
931-
thd, vers_conditions.end);
932-
break;
933-
}
934-
default:
935-
DBUG_ASSERT(0);
936-
break;
937-
}
938-
}
876+
vers_conditions.start.add_typecast(thd, t->s->versioned);
877+
vers_conditions.end.add_typecast(thd, t->s->versioned);
939878
}
940879
switch (vers_conditions.type)
941880
{
@@ -957,26 +896,19 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
957896
cond1= or_items(thd, cond1, newx Item_func_isnull(thd, row_end));
958897
break;
959898
case SYSTEM_TIME_AS_OF:
960-
cond1= newx Item_func_le(thd, row_start,
961-
vers_conditions.start);
962-
cond2= newx Item_func_gt(thd, row_end,
963-
vers_conditions.start);
899+
cond1= newx Item_func_le(thd, row_start, vers_conditions.start.item);
900+
cond2= newx Item_func_gt(thd, row_end, vers_conditions.start.item);
964901
break;
965902
case SYSTEM_TIME_FROM_TO:
966-
cond1= newx Item_func_lt(thd, row_start,
967-
vers_conditions.end);
968-
cond2= newx Item_func_ge(thd, row_end,
969-
vers_conditions.start);
903+
cond1= newx Item_func_lt(thd, row_start, vers_conditions.end.item);
904+
cond2= newx Item_func_ge(thd, row_end, vers_conditions.start.item);
970905
break;
971906
case SYSTEM_TIME_BETWEEN:
972-
cond1= newx Item_func_le(thd, row_start,
973-
vers_conditions.end);
974-
cond2= newx Item_func_ge(thd, row_end,
975-
vers_conditions.start);
907+
cond1= newx Item_func_le(thd, row_start, vers_conditions.end.item);
908+
cond2= newx Item_func_ge(thd, row_end, vers_conditions.start.item);
976909
break;
977910
case SYSTEM_TIME_BEFORE:
978-
cond1= newx Item_func_lt(thd, row_end,
979-
vers_conditions.start);
911+
cond1= newx Item_func_lt(thd, row_end, vers_conditions.start.item);
980912
break;
981913
default:
982914
DBUG_ASSERT(0);
@@ -995,29 +927,29 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
995927
cond1= newx Item_func_eq(thd, row_end, curr);
996928
break;
997929
case SYSTEM_TIME_AS_OF:
998-
trx_id0= vers_conditions.unit_start == VERS_TIMESTAMP ?
999-
newx Item_func_vtq_id(thd, vers_conditions.start, TR_table::FLD_TRX_ID) :
1000-
vers_conditions.start;
930+
trx_id0= vers_conditions.start.unit == VERS_TIMESTAMP
931+
? newx Item_func_vtq_id(thd, vers_conditions.start.item, TR_table::FLD_TRX_ID)
932+
: vers_conditions.start.item;
1001933
cond1= newx Item_func_vtq_trx_sees_eq(thd, trx_id0, row_start);
1002934
cond2= newx Item_func_vtq_trx_sees(thd, row_end, trx_id0);
1003935
break;
1004936
case SYSTEM_TIME_FROM_TO:
1005937
case SYSTEM_TIME_BETWEEN:
1006-
trx_id0= vers_conditions.unit_start == VERS_TIMESTAMP ?
1007-
newx Item_func_vtq_id(thd, vers_conditions.start, TR_table::FLD_TRX_ID, true) :
1008-
vers_conditions.start;
1009-
trx_id1= vers_conditions.unit_end == VERS_TIMESTAMP ?
1010-
newx Item_func_vtq_id(thd, vers_conditions.end, TR_table::FLD_TRX_ID, false) :
1011-
vers_conditions.end;
1012-
cond1= vers_conditions.type == SYSTEM_TIME_FROM_TO ?
1013-
newx Item_func_vtq_trx_sees(thd, trx_id1, row_start) :
1014-
newx Item_func_vtq_trx_sees_eq(thd, trx_id1, row_start);
938+
trx_id0= vers_conditions.start.unit == VERS_TIMESTAMP
939+
? newx Item_func_vtq_id(thd, vers_conditions.start.item, TR_table::FLD_TRX_ID, true)
940+
: vers_conditions.start.item;
941+
trx_id1= vers_conditions.end.unit == VERS_TIMESTAMP
942+
? newx Item_func_vtq_id(thd, vers_conditions.end.item, TR_table::FLD_TRX_ID, false)
943+
: vers_conditions.end.item;
944+
cond1= vers_conditions.type == SYSTEM_TIME_FROM_TO
945+
? newx Item_func_vtq_trx_sees(thd, trx_id1, row_start)
946+
: newx Item_func_vtq_trx_sees_eq(thd, trx_id1, row_start);
1015947
cond2= newx Item_func_vtq_trx_sees_eq(thd, row_end, trx_id0);
1016948
break;
1017949
case SYSTEM_TIME_BEFORE:
1018-
trx_id0= vers_conditions.unit_start == VERS_TIMESTAMP ?
1019-
newx Item_func_vtq_id(thd, vers_conditions.start, TR_table::FLD_TRX_ID) :
1020-
vers_conditions.start;
950+
trx_id0= vers_conditions.start.unit == VERS_TIMESTAMP
951+
? newx Item_func_vtq_id(thd, vers_conditions.start.item, TR_table::FLD_TRX_ID)
952+
: vers_conditions.start.item;
1021953
cond1= newx Item_func_lt(thd, row_end, trx_id0);
1022954
break;
1023955
default:

sql/sql_yacc.yy

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -752,27 +752,6 @@ bool LEX::set_bincmp(CHARSET_INFO *cs, bool bin)
752752
} while(0)
753753

754754

755-
void vers_select_conds_t::init(vers_system_time_t t, vers_sys_type_t u_start,
756-
Item *s, vers_sys_type_t u_end, Item *e)
757-
{
758-
type= t;
759-
unit_start= u_start;
760-
unit_end= u_end;
761-
start= fix_dec(s);
762-
end= fix_dec(e);
763-
used= from_query= false;
764-
}
765-
766-
Item *vers_select_conds_t::fix_dec(Item *item)
767-
{
768-
if (item && item->decimals == 0 && item->type() == Item::FUNC_ITEM &&
769-
((Item_func*)item)->functype() == Item_func::NOW_FUNC)
770-
item->decimals= 6;
771-
772-
return item;
773-
}
774-
775-
776755
Virtual_column_info *add_virtual_expression(THD *thd, Item *expr)
777756
{
778757
Virtual_column_info *v= new (thd->mem_root) Virtual_column_info();
@@ -813,6 +792,7 @@ Virtual_column_info *add_virtual_expression(THD *thd, Item *expr)
813792
LEX_CSTRING name;
814793
uint offset;
815794
} sp_cursor_name_and_offset;
795+
vers_history_point_t vers_history_point;
816796

817797
/* pointers */
818798
Create_field *create_field;
@@ -2046,7 +2026,8 @@ END_OF_INPUT
20462026

20472027
%type <lex_str_list> opt_with_column_list
20482028

2049-
%type <vers_range_unit> opt_trans_or_timestamp
2029+
%type <vers_range_unit> opt_history_unit
2030+
%type <vers_history_point> history_point
20502031
%type <vers_column_versioning> with_or_without_system
20512032
%%
20522033

@@ -9203,18 +9184,25 @@ select_options:
92039184
}
92049185
;
92059186

9206-
opt_trans_or_timestamp:
9207-
/* empty */
9187+
opt_history_unit:
9188+
/* empty*/
92089189
{
9209-
$$ = VERS_UNDEFINED;
9190+
$$= VERS_UNDEFINED;
92109191
}
92119192
| TRANSACTION_SYM
92129193
{
9213-
$$ = VERS_TRX_ID;
9194+
$$= VERS_TRX_ID;
92149195
}
92159196
| TIMESTAMP
92169197
{
9217-
$$ = VERS_TIMESTAMP;
9198+
$$= VERS_TIMESTAMP;
9199+
}
9200+
;
9201+
9202+
history_point:
9203+
opt_history_unit simple_expr
9204+
{
9205+
$$= Vers_history_point($1, $2);
92189206
}
92199207
;
92209208

@@ -9230,23 +9218,23 @@ opt_for_system_time_clause:
92309218
;
92319219

92329220
system_time_expr:
9233-
AS OF_SYM opt_trans_or_timestamp simple_expr
9221+
AS OF_SYM history_point
92349222
{
9235-
Lex->vers_conditions.init(SYSTEM_TIME_AS_OF, $3, $4);
9223+
Lex->vers_conditions.init(SYSTEM_TIME_AS_OF, $3);
92369224
}
92379225
| ALL
92389226
{
92399227
Lex->vers_conditions.init(SYSTEM_TIME_ALL);
92409228
}
9241-
| FROM opt_trans_or_timestamp simple_expr
9242-
TO_SYM opt_trans_or_timestamp simple_expr
9229+
| FROM history_point
9230+
TO_SYM history_point
92439231
{
9244-
Lex->vers_conditions.init(SYSTEM_TIME_FROM_TO, $2, $3, $5, $6);
9232+
Lex->vers_conditions.init(SYSTEM_TIME_FROM_TO, $2, $4);
92459233
}
9246-
| BETWEEN_SYM opt_trans_or_timestamp simple_expr
9247-
AND_SYM opt_trans_or_timestamp simple_expr
9234+
| BETWEEN_SYM history_point
9235+
AND_SYM history_point
92489236
{
9249-
Lex->vers_conditions.init(SYSTEM_TIME_BETWEEN, $2, $3, $5, $6);
9237+
Lex->vers_conditions.init(SYSTEM_TIME_BETWEEN, $2, $4);
92509238
}
92519239
;
92529240

@@ -13447,9 +13435,9 @@ opt_delete_system_time:
1344713435
{
1344813436
Lex->vers_conditions.init(SYSTEM_TIME_ALL);
1344913437
}
13450-
| BEFORE_SYM SYSTEM_TIME_SYM opt_trans_or_timestamp simple_expr
13438+
| BEFORE_SYM SYSTEM_TIME_SYM history_point
1345113439
{
13452-
Lex->vers_conditions.init(SYSTEM_TIME_BEFORE, $3, $4);
13440+
Lex->vers_conditions.init(SYSTEM_TIME_BEFORE, $3);
1345313441
}
1345413442
;
1345513443

0 commit comments

Comments
 (0)