Bug #104394 | SELECT's GROUPING() not taken in account in HAVING when JOIN used | ||
---|---|---|---|
Submitted: | 23 Jul 2021 10:18 | Modified: | 27 Aug 2021 16:41 |
Reporter: | Sébastien F. | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: DML | Severity: | S3 (Non-critical) |
Version: | 8.0 | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | grouping, rollup |
[23 Jul 2021 10:18]
Sébastien F.
[23 Jul 2021 13:06]
MySQL Verification Team
Hi Mr. F. Thank you for your bug report. We have tested your report and we agree that this is not functioning. Hence, we will accept it, but do keep in mind that it might be turned into a feature request. Verified as reported.
[23 Jul 2021 13:07]
MySQL Verification Team
This is also present in the latest 8.0 release .....
[3 Aug 2021 14:32]
Steinar Gunderson
A simpler test case: CREATE TABLE t1 (a INTEGER, b INTEGER); INSERT INTO t1 VALUES (2020, 2); CREATE TABLE t2 (b INTEGER); INSERT INTO t2 VALUES (2); SELECT a, GROUPING(a) AS ga FROM t1 JOIN t2 USING (b) GROUP BY a WITH ROLLUP HAVING ga = 0; Note that this query is illegal; you cannot refer to aliases in HAVING (as projection happens after HAVING evaluation). MySQL seems to accept it, but I'm not sure whether we should (the semantics can often be unclear). The query plan goes: -> Filter: (ga = 0) (cost=2.50..2.50 rows=0) -> Table scan on <temporary> (cost=2.50..2.50 rows=0) -> Temporary table (cost=2.50..2.50 rows=0) -> Group (no aggregates) -> Sort: t1.a -> Stream results through <temporary> (cost=0.70 rows=1) -> Inner hash join (t2.b = t1.b) (cost=0.70 rows=1) -> Table scan on t2 (cost=0.35 rows=1) -> Hash -> Table scan on t1 (cost=0.35 rows=1) The immediate problem is the stream through <temporary> (which we only need due to legacy in the old join optimizer); it materializes the expression (ga = 0), which doesn't make sense at this point, since it's below the aggregation (ga will change as the aggregation produces ROLLUP rows). We'd need to take that out of the list of materialized expressions. The hypergraph optimizer gets it right, because it doesn't depend on slices and references the same way. The immediate workaround is to write the query legally.
[4 Aug 2021 12:07]
MySQL Verification Team
Thank you, Steinar !!!!
[27 Aug 2021 16:41]
Christine Cole
Posted by developer: Fixed as of the upcoming MySQL 8.0.28 release, and here's the proposed changelog entry from the documentation team: When setting up an Item_ref to a SELECT alias, its cached properties are copied (including whether it is part of a ROLLUP expression or not). However, these might not yet be correctly computed, so the computation should to be done first or the values could be wrong. Having the wrong value could cause certain expressions to be materialized in an intermediate step when they should not (because they contain ROLLUP expressions that are not ready for computation, but having the wrong cached values is unknown at this point). The issue is fixed by forcing cached values to be recomputed when an item is designated as a rollup item. Thank you for the bug report.
[30 Aug 2021 12:06]
MySQL Verification Team
Thank you, Christine ......