Skip to content

Commit

Permalink
MDEV-14695: Assertion `n < m_size' failed in Bounds_checked_array<Ele…
Browse files Browse the repository at this point in the history
…ment_type>::operator

In this issue we hit the assert because we are adding addition fields to the field JOIN::all_fields list. This
is done because  HEAP tables can't index BIT fields so  we need to use an additional hidden field for grouping because later it will be
converted to a LONG field. Original field will remain of the BIT type and will be returned. This happens when we convert DISTINCT to
GROUP BY.

The solution is to take into account the number of such hidden fields that would be added to the field
JOIN::all_fields list while calculating the size of the ref_pointer_array.
  • Loading branch information
varunraiko committed May 16, 2018
1 parent d9f9cd1 commit 6f4534e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 8 deletions.
10 changes: 10 additions & 0 deletions mysql-test/r/distinct.result
Original file line number Diff line number Diff line change
Expand Up @@ -1039,4 +1039,14 @@ count(distinct case when id<=63 then id end)
63
drop table tb;
SET @@tmp_table_size= @tmp_table_size_save;
#
# MDEV-14695: Assertion `n < m_size' failed in Bounds_checked_array<Element_type>::operator
#
CREATE TABLE t1 (b1 BIT, b2 BIT, b3 BIT, b4 BIT , b5 BIT, b6 BIT);
INSERT INTO t1 VALUES (1,0,0,1,0,1),(0,1,0,0,1,0);
SELECT DISTINCT b1+'0', b2+'0', b3+'0', b4+'0', b5+'0', b6 +'0' FROM t1;
b1+'0' b2+'0' b3+'0' b4+'0' b5+'0' b6 +'0'
1 0 0 1 0 1
0 1 0 0 1 0
DROP TABLE t1;
End of 5.5 tests
8 changes: 8 additions & 0 deletions mysql-test/t/distinct.test
Original file line number Diff line number Diff line change
Expand Up @@ -790,4 +790,12 @@ drop table tb;

SET @@tmp_table_size= @tmp_table_size_save;

--echo #
--echo # MDEV-14695: Assertion `n < m_size' failed in Bounds_checked_array<Element_type>::operator
--echo #

CREATE TABLE t1 (b1 BIT, b2 BIT, b3 BIT, b4 BIT , b5 BIT, b6 BIT);
INSERT INTO t1 VALUES (1,0,0,1,0,1),(0,1,0,0,1,0);
SELECT DISTINCT b1+'0', b2+'0', b3+'0', b4+'0', b5+'0', b6 +'0' FROM t1;
DROP TABLE t1;
--echo End of 5.5 tests
9 changes: 6 additions & 3 deletions sql/sql_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6919,7 +6919,7 @@ static bool setup_natural_join_row_types(THD *thd,

int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields,
List<Item> *sum_func_list,
uint wild_num)
uint wild_num, uint *hidden_bit_fields)
{
if (!wild_num)
return(0);
Expand Down Expand Up @@ -6960,7 +6960,7 @@ int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields,
else if (insert_fields(thd, ((Item_field*) item)->context,
((Item_field*) item)->db_name,
((Item_field*) item)->table_name, &it,
any_privileges))
any_privileges, hidden_bit_fields))
{
if (arena)
thd->restore_active_arena(arena, &backup);
Expand Down Expand Up @@ -7425,7 +7425,7 @@ bool get_key_map_from_key_list(key_map *map, TABLE *table,
bool
insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
const char *table_name, List_iterator<Item> *it,
bool any_privileges)
bool any_privileges, uint *hidden_bit_fields)
{
Field_iterator_table_ref field_iterator;
bool found;
Expand Down Expand Up @@ -7545,6 +7545,9 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
else
it->after(item); /* Add 'item' to the SELECT list. */

if (item->type() == Item::FIELD_ITEM && item->field_type() == MYSQL_TYPE_BIT)
(*hidden_bit_fields)++;

#ifndef NO_EMBEDDED_ACCESS_CHECKS
/*
Set privilege information for the fields of newly created views.
Expand Down
5 changes: 3 additions & 2 deletions sql/sql_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,12 @@ bool fill_record_n_invoke_before_triggers(THD *thd, TABLE *table,
enum trg_event_type event);
bool insert_fields(THD *thd, Name_resolution_context *context,
const char *db_name, const char *table_name,
List_iterator<Item> *it, bool any_privileges);
List_iterator<Item> *it, bool any_privileges,
uint *hidden_bit_fields);
void make_leaves_list(THD *thd, List<TABLE_LIST> &list, TABLE_LIST *tables,
bool full_table_list, TABLE_LIST *boundary);
int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields,
List<Item> *sum_func_list, uint wild_num);
List<Item> *sum_func_list, uint wild_num, uint * hidden_bit_fields);
bool setup_fields(THD *thd, Ref_ptr_array ref_pointer_array,
List<Item> &item, enum_mark_columns mark_used_columns,
List<Item> *sum_func_list, List<Item> *pre_fix,
Expand Down
3 changes: 2 additions & 1 deletion sql/sql_delete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,8 @@ l
select_lex->leaf_tables, FALSE,
DELETE_ACL, SELECT_ACL, TRUE))
DBUG_RETURN(TRUE);
if ((wild_num && setup_wild(thd, table_list, field_list, NULL, wild_num)) ||
if ((wild_num && setup_wild(thd, table_list, field_list, NULL, wild_num,
&select_lex->hidden_bit_fields)) ||
setup_fields(thd, Ref_ptr_array(),
field_list, MARK_COLUMNS_READ, NULL, NULL, 0) ||
setup_conds(thd, table_list, select_lex->leaf_tables, conds) ||
Expand Down
8 changes: 7 additions & 1 deletion sql/sql_lex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,7 @@ void st_select_lex::init_query()
select_n_having_items= 0;
n_sum_items= 0;
n_child_sum_items= 0;
hidden_bit_fields= 0;
subquery_in_having= explicit_limit= 0;
is_item_list_lookup= 0;
first_execution= 1;
Expand Down Expand Up @@ -2673,6 +2674,10 @@ ulong st_select_lex::get_table_join_options()

bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
{

if (!((options & SELECT_DISTINCT) && !group_list.elements))
hidden_bit_fields= 0;

// find_order_in_list() may need some extra space, so multiply by two.
order_group_num*= 2;

Expand All @@ -2687,7 +2692,8 @@ bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
select_n_reserved +
select_n_having_items +
select_n_where_fields +
order_group_num) * 5;
order_group_num +
hidden_bit_fields) * 5;
if (!ref_pointer_array.is_null())
{
/*
Expand Down
5 changes: 5 additions & 0 deletions sql/sql_lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,11 @@ class st_select_lex: public st_select_lex_node
uint select_n_where_fields;
/* reserved for exists 2 in */
uint select_n_reserved;
/*
it counts the number of bit fields in the SELECT list. These are used when DISTINCT is
converted to a GROUP BY involving BIT fields.
*/
uint hidden_bit_fields;
enum_parsing_place parsing_place; /* where we are parsing expression */
enum_parsing_place context_analysis_place; /* where we are in prepare */
bool with_sum_func; /* sum function indicator */
Expand Down
4 changes: 3 additions & 1 deletion sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,9 @@ JOIN::prepare(TABLE_LIST *tables_init,
select_lex != select_lex->master_unit()->global_parameters())
real_og_num+= select_lex->order_list.elements;

if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num))
DBUG_ASSERT(select_lex->hidden_bit_fields == 0);
if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num,
&select_lex->hidden_bit_fields))
DBUG_RETURN(-1);
if (select_lex->setup_ref_array(thd, real_og_num))
DBUG_RETURN(-1);
Expand Down
1 change: 1 addition & 0 deletions sql/sql_union.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,7 @@ bool st_select_lex::cleanup()
}
inner_refs_list.empty();
exclude_from_table_unique_test= FALSE;
hidden_bit_fields= 0;
DBUG_RETURN(error);
}

Expand Down

0 comments on commit 6f4534e

Please sign in to comment.