Bug #121992 Correlated EXISTS combined with a window function in one expression returns one row's value for all rows when the query

Submitted: 30 Nov 10:54
Reporter: Zafar Malik Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:8.4.10 OS:Any
Assigned to: CPU Architecture:Any
Tags: query statistics

[30 Nov 10:54] Zafar Malik
Description:
In a query block with two or more window functions, a correlated EXISTS (or NOT EXISTS) in the same expression as one of them returns the same value on every row. That value belongs to a single row (the last row read by the first window step), and no warning is raised.
The result is correct in these cases:
the query has one window function;
the EXISTS is its own column or sits in WHERE;
IN (subquery) or a scalar subquery is used instead.

This looks related to #105045 (fixed in 8.0.28), but it still reproduces on 8.4.10.

How to repeat:
Case1:
mysql> CREATE DATABASE bugtest; USE bugtest;
Query OK, 1 row affected (0.03 sec)

Database changed
mysql> CREATE TABLE t (id INT PRIMARY KEY, g INT NOT NULL);
Query OK, 0 rows affected (0.08 sec)

mysql> CREATE TABLE o (g INT NOT NULL);
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO t VALUES (1,1), (2,1), (3,2);
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> INSERT INTO o VALUES (1);
Query OK, 1 row affected (0.01 sec)

mysql> SELECT id,
    -> ROW_NUMBER() OVER (PARTITION BY g ORDER BY id DESC) * 10 + EXISTS (SELECT 1 FROM o WHERE o.g = t.g) AS x,
    -> MAX(id) OVER () AS m
    -> FROM t ORDER BY id;
+----+----+------+
| id | x  | m    |
+----+----+------+
|  1 | 20 |    3 |
|  2 | 10 |    3 |
|  3 | 10 |    3 |
+----+----+------+
3 rows in set (0.01 sec)

Expected results for x column should be : 21, 11, 10
But query is generating results for x column : 20, 10, 10

Without without "MAX() OVER ()", it is providing correct results, please check below-
mysql> SELECT id,
    -> ROW_NUMBER() OVER (PARTITION BY g ORDER BY id DESC) * 10 + EXISTS (SELECT 1 FROM o WHERE o.g = t.g) AS x
    -> FROM t ORDER BY id;
+----+----+
| id | x  |
+----+----+
|  1 | 21 |
|  2 | 11 |
|  3 | 10 |
+----+----+
3 rows in set (0.00 sec)

Case2:
If we change table g vallue from 1 to 2 as per below-
mysql> DELETE FROM o;
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO o VALUES (2);
Query OK, 1 row affected (0.01 sec)

mysql> SELECT id,
    -> ROW_NUMBER() OVER (PARTITION BY g ORDER BY id DESC) * 10 + EXISTS (SELECT 1 FROM o WHERE o.g = t.g) AS x,
    -> MAX(t.id) OVER () AS m
    -> FROM t ORDER BY id;
+----+----+------+
| id | x  | m    |
+----+----+------+
|  1 | 21 |    3 |
|  2 | 11 |    3 |
|  3 | 11 |    3 |
+----+----+------+
3 rows in set (0.00 sec)

Here Expected results for x column should be : 20, 10, 11
But it is producing : 21, 11, 11

Note: Workaround: move the EXISTS out of the window function's expression, into WHERE, a derived table or a separate statement.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.048 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.