Skip to content

Commit b20d94d

Browse files
Sreeharsha Ramanavarapuvuvova
authored andcommitted
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 6de2928 commit b20d94d

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
@@ -14627,19 +14627,21 @@ subselect_end:
1462714627
lex->current_select = lex->current_select->return_after_parsing();
1462814628
lex->nest_level--;
1462914629
lex->current_select->n_child_sum_items += child->n_sum_items;
14630-
/*
14631-
A subselect can add fields to an outer select. Reserve space for
14632-
them.
14633-
*/
14634-
lex->current_select->select_n_where_fields+=
14635-
child->select_n_where_fields;
1463614630

1463714631
/*
14638-
Aggregate functions in having clause may add fields to an outer
14639-
select. Count them also.
14632+
A subquery (and all the subsequent query blocks in a UNION) can
14633+
add columns to an outer query block. Reserve space for them.
14634+
Aggregate functions in having clause can also add fields to an
14635+
outer select.
1464014636
*/
14641-
lex->current_select->select_n_having_items+=
14642-
child->select_n_having_items;
14637+
for (SELECT_LEX *temp= child->master_unit()->first_select();
14638+
temp != NULL; temp= temp->next_select())
14639+
{
14640+
lex->current_select->select_n_where_fields+=
14641+
temp->select_n_where_fields;
14642+
lex->current_select->select_n_having_items+=
14643+
temp->select_n_having_items;
14644+
}
1464314645
}
1464414646
;
1464514647

0 commit comments

Comments
 (0)