Skip to content

Commit 9f81ac6

Browse files
author
Sreeharsha Ramanavarapu
committed
Bug #28499924: INCORRECT BEHAVIOR WITH UNION IN SUBQUERY
Issue: ------ When a subquery contains UNION the count of the number of subquery columns is calculated incorrectly. Only the first query block in the subquery's UNION is considered and an array indexing goes out-of-bounds, and this is caught by an assert. Solution: --------- Sum up the columns from all query blocks of the query expression. Change specific to 5.6/5.5: --------------------------- The "child" points to the last query block of the UNION (as opposed to 5.7+ where it points to the first member of UNION). So "child->master_unit()->first_select()" is used to reach the first query block of UNION.
1 parent d3b0f81 commit 9f81ac6

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

sql/sql_yacc.yy

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14148,19 +14148,21 @@ subselect_end:
1414814148
lex->current_select = lex->current_select->return_after_parsing();
1414914149
lex->nest_level--;
1415014150
lex->current_select->n_child_sum_items += child->n_sum_items;
14151-
/*
14152-
A subselect can add fields to an outer select. Reserve space for
14153-
them.
14154-
*/
14155-
lex->current_select->select_n_where_fields+=
14156-
child->select_n_where_fields;
1415714151

1415814152
/*
14159-
Aggregate functions in having clause may add fields to an outer
14160-
select. Count them also.
14153+
A subquery (and all the subsequent query blocks in a UNION) can
14154+
add columns to an outer query block. Reserve space for them.
14155+
Aggregate functions in having clause can also add fields to an
14156+
outer select.
1416114157
*/
14162-
lex->current_select->select_n_having_items+=
14163-
child->select_n_having_items;
14158+
for (SELECT_LEX *temp= child->master_unit()->first_select();
14159+
temp != NULL; temp= temp->next_select())
14160+
{
14161+
lex->current_select->select_n_where_fields+=
14162+
temp->select_n_where_fields;
14163+
lex->current_select->select_n_having_items+=
14164+
temp->select_n_having_items;
14165+
}
1416414166
}
1416514167
;
1416614168

0 commit comments

Comments
 (0)