Bug #84695 WL#9236: CRASHES OR WRONG RESULTS WITH WINDOW FUNCTIONS IN SUBQUERY
Submitted: 27 Jan 2017 14:00 Modified: 30 Mar 2017 17:54
Reporter: Guilhem Bichot Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:8.0.1 OS:Any
Assigned to: CPU Architecture:Any

[27 Jan 2017 14:00] Guilhem Bichot
Description:
By nesting a window function inside a scalar subquery it's possible to get crashes or absurd results.
verified with bbffe0dd6dc37bc6314f096303c9bf4580375c22

How to repeat:
create table t(a int, b int);

# Crash: put WF in subquery
select (select sum(a) over ())  from t ;

# Bad result: using the special MySQL feature which says that in a SELECT list's expression you can reference an alias of another expression of that list IF you use a subquery
# (see https://bugs.mysql.com/bug.php?id=79549 for more background). So, I reference a WF by alias, putting it inside a group aggregate, which doesn't make sense! Here:

insert into t values(1,2);
insert into t values(4,5);

select sum(a) over () as c, (SELECT sum(c))  from t ;
+------+-----------------+
| c    | (SELECT sum(c)) |
+------+-----------------+
|    5 |               6 |
|    5 |              10 |
+------+-----------------+

Notice how ORDER BY properly rejects sum(c):
select sum(a) over () as c from t order by sum(c);
ERROR 3029 (HY000): Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
[30 Mar 2017 17:54] Dag Wanvik
Posted by developer:
 
Fixed in commit a49150364, closing.