Skip to content

Commit

Permalink
SQL: NULL instead of optimized fields in versioned queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgs committed Nov 8, 2016
1 parent 748af78 commit 4f893dc
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
68 changes: 68 additions & 0 deletions mysql-test/suite/versioning/r/optimized_fields.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
create table t(
a int,
b int without system versioning
) with system versioning;
insert into t values(1, 2);
insert into t values(3, 4);
select * from t;
a b
1 2
3 4
select a from t for system_time as of timestamp now(6);
a
1
3
select a, b, b+0 from t for system_time as of timestamp now(6);
a b b+0
1 NULL NULL
3 NULL NULL
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select * from t for system_time as of timestamp now(6);
a b
1 NULL
3 NULL
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select count(*) from t group by b for system_time as of timestamp now(6);
count(*)
2
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select * from t for system_time as of timestamp now(6) order by b asc;
a b
1 NULL
3 NULL
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select * from t for system_time as of timestamp now(6) order by b desc;
a b
1 NULL
3 NULL
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select * from t group by a having a=2 for system_time as of timestamp now(6);
a b
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select * from t group by b having b=2 for system_time as of timestamp now(6);
a b
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select a from t where b=2 for system_time as of timestamp now(6);
a
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select a from t where b=NULL for system_time as of timestamp now(6);
a
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select count(*), b from t group by b having b=NULL for system_time as of timestamp now(6);
count(*) b
Warnings:
Warning 4050 Attempt to read unversioned field 'b' in historical query
select a, b from t;
a b
1 2
3 4
drop table t;
22 changes: 22 additions & 0 deletions mysql-test/suite/versioning/t/optimized_fields.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
create table t(
a int,
b int without system versioning
) with system versioning;

insert into t values(1, 2);
insert into t values(3, 4);
select * from t;
select a from t for system_time as of timestamp now(6);
select a, b, b+0 from t for system_time as of timestamp now(6);
select * from t for system_time as of timestamp now(6);
select count(*) from t group by b for system_time as of timestamp now(6);
select * from t for system_time as of timestamp now(6) order by b asc;
select * from t for system_time as of timestamp now(6) order by b desc;
select * from t group by a having a=2 for system_time as of timestamp now(6);
select * from t group by b having b=2 for system_time as of timestamp now(6);
select a from t where b=2 for system_time as of timestamp now(6);
select a from t where b=NULL for system_time as of timestamp now(6);
select count(*), b from t group by b having b=NULL for system_time as of timestamp now(6);
select a, b from t;

drop table t;
2 changes: 1 addition & 1 deletion sql/field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
:ptr(ptr_arg), null_ptr(null_ptr_arg), table(0), orig_table(0),
table_name(0), field_name(field_name_arg), option_list(0),
option_struct(0), key_start(0), part_of_key(0),
part_of_key_not_clustered(0), part_of_sortkey(0),
part_of_key_not_clustered(0), force_null(false), part_of_sortkey(0),
unireg_check(unireg_check_arg), field_length(length_arg),
null_bit(null_bit_arg), is_created_from_null_item(FALSE),
read_stats(NULL), collected_stats(0), vcol_info(0), check_constraint(0),
Expand Down
4 changes: 4 additions & 0 deletions sql/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ class Field: public Value_source
/* Field is part of the following keys */
key_map key_start, part_of_key, part_of_key_not_clustered;

bool force_null;

/*
Bitmap of indexes that have records ordered by col1, ... this_field, ...
Expand Down Expand Up @@ -1076,6 +1078,8 @@ class Field: public Value_source
virtual uint size_of() const =0; // For new field
inline bool is_null(my_ptrdiff_t row_offset= 0) const
{
if (force_null)
return true;
/*
The table may have been marked as containing only NULL values
for all fields if it is a NULL-complemented row of an OUTER JOIN
Expand Down
17 changes: 17 additions & 0 deletions sql/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,21 @@ void Item_field::set_field(Field *field_par)
fixed= 1;
if (field->table->s->tmp_table == SYSTEM_TMP_TABLE)
any_privileges= 0;

if (field->is_versioning_disabled() && context && context->select_lex &&
context->select_lex->vers_conditions.type !=
FOR_SYSTEM_TIME_UNSPECIFIED &&
!field->force_null)
{
DBUG_ASSERT(context->select_lex->parent_lex &&
context->select_lex->parent_lex->thd);
field->force_null= true;
THD *thd= context->select_lex->parent_lex->thd;
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
ER_NON_VERSIONED_FIELD_IN_VERSIONED_QUERY,
ER_THD(thd, ER_NON_VERSIONED_FIELD_IN_VERSIONED_QUERY),
field_name);
}
}


Expand Down Expand Up @@ -5418,6 +5433,8 @@ void Item_field::cleanup()
it will be linked correctly next time by name of field and table alias.
I.e. we can drop 'field'.
*/
if (field)
field->force_null= false;
field= 0;
item_equal= NULL;
null_value= FALSE;
Expand Down
3 changes: 3 additions & 0 deletions sql/share/errmsg-utf8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7290,3 +7290,6 @@ ER_VERS_TRX_ID_UNSUPPORTED

ER_VERS_RANGE_UNITS_MISMATCH
eng "Range units mismatch"

ER_NON_VERSIONED_FIELD_IN_VERSIONED_QUERY
eng "Attempt to read unversioned field '%s' in historical query"

0 comments on commit 4f893dc

Please sign in to comment.