Bug #38217 | GROUP BY with subquery fails. | ||
---|---|---|---|
Submitted: | 17 Jul 2008 21:35 | Modified: | 9 Nov 2009 20:14 |
Reporter: | Adam Dixon | Email Updates: | |
Status: | Duplicate | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S3 (Non-critical) |
Version: | 5.0.60, 5.0.64, 5.1.28 | OS: | Any |
Assigned to: | Assigned Account | CPU Architecture: | Any |
[17 Jul 2008 21:35]
Adam Dixon
[24 Jul 2008 0:01]
Adam Dixon
Verified in 5.1 bzr too.
[12 Nov 2008 15:55]
Georgi Kodinov
The problem here is as follows : A subquery's external references are compiled differently dependent on where the subquery is referenced from the outer query when the outer query is a grouping one : - Item_outer_ref if the subquery is referenced from the HAVING or the SELECT list - Item_field otherwise. Now consider queries that reference a subquery from the SELECT list by name. These references are not copying the subquery, but use a single subquery (with Item_outer_ref for the external reference) instead and evaluate it in both a SELECT list context and GROUP BY context. This leads to wrong data being used for the outer references in the subquery.
[23 Mar 2009 15:21]
Georgi Kodinov
This bug is a regression from the fix for bug #27321
[3 Apr 2009 14:46]
Bugs System
A patch for this bug has been committed. After review, it may be pushed to the relevant source trees for release in the next version. You can access the patch from: http://lists.mysql.com/commits/71326 2743 Georgi Kodinov 2009-04-03 Bug #38217: GROUP BY with subquery fails. When a subquery references fields from the outer context the server creates either an Item_outer_ref that expands to either Item_ref (pointing to the last row of the previous group in a GROUP BY context) or Item_direct_ref (pointing to the first row of the new group in a GROUP BY context). This causes wrong results with scalar subqueries in the SELECT list that are referenced e.g. by name from the GROUP BY list. For these subqueries it's not possible to make both Item_ref (because the sub-query is in the SELECT list) and Item_direct_ref (because the same subquery is referenced through an Item_field from the GROUP BY field) for a single copy of Item_outer_ref in the subquery. No wrong results are returned when temporary table is used. Temporary table approach works because the scalar subquery from the GROUP BY is evaluated only once and stored in the temporary table. We have two approaches to fix this: 1. Make sure we copy the entire tree of the subquery when it's referenced in GROUP BY, thus effectively transforming : SELECT (SELECT a FROM t2 WHERE b = t1.a) c1, COUNT(*) FROM t1 GROUP BY c1 to SELECT (SELECT a FROM t2 WHERE b = t1.a) c1, COUNT(*) FROM t1 GROUP BY (SELECT a FROM t2 WHERE b = t1.a) and processing this as usual. 2. Make sure scalar subqueries in GROUP BY/ORDER BY always trigger using temporary table. This fix implements 2). This is done to avoid multiple re-calculation of the scalar subquery during the calculation of GROUP BY/ORDER BY. MySQL is already doing similar thing for UDFs and stored procedures. @ mysql-test/r/subselect.result Bug #38217: - test case - fixed a non-sorted query result (caused by a query that has ORDER BY <const>) @ mysql-test/t/subselect.test Bug #38217: - test case - fixed a non-sorted query result (caused by a query that has ORDER BY <const>) @ sql/item_subselect.h Bug #38217: subqueries are expensive expressions @ sql/sql_select.cc Bug #38217: check GROUP BY for expensive expressions as well.
[9 Nov 2009 20:14]
Gleb Shchepa
This bug duplicates the bug #45640.