Bug #120307 Wrong result for `<= ANY (subquery)` in a window-function query

Submitted: 21 Apr 14:02 Modified: 22 Apr 4:23
Reporter: QiFan Liu Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:9.6 OS:Linux (Observed on Linux via the `mysql:9.6` )
Assigned to: CPU Architecture:Any

[21 Apr 14:02] QiFan Liu
Description:
MySQL returns an incorrect result in a window-function query when evaluating `expr <= ANY (subquery)` over `TINYTEXT` values that are implicitly converted to numbers. In the reproduced case, the same window-driven workload reports two qualifying rows in a row-by-row validation branch, but the main filtered branch returns only one row.

The reproduced query uses a CTE, a named window, `ROW_NUMBER()`, and `LAG()` to drive predicate evaluation. Under that window-function setup, MySQL drops one qualifying row and returns only `"X"` instead of the expected `"1,X"`.

How to repeat:
DROP TABLE IF EXISTS t0;
CREATE TABLE t0 (c1 TINYTEXT);
INSERT INTO t0 (c1) VALUES ('1'), ('2'), ('X');

WITH _wf_cte AS (
  SELECT
    seed.grp_id,
    seed.ord_id,
    ROW_NUMBER() OVER w_main AS rn,
    LAG(seed.ord_id, 1, NULL) OVER w_main AS wf_probe_1
  FROM (
    SELECT 7 AS grp_id, 1 AS ord_id
    UNION ALL
    SELECT 7, 2
    UNION ALL
    SELECT 9, 1
  ) AS seed
  WINDOW w_main AS (PARTITION BY seed.grp_id ORDER BY seed.ord_id)
)
SELECT JSON_OBJECT(
  'where_count',
  (
    SELECT COUNT(*)
    FROM t0
    WHERE EXISTS (
      SELECT 1
      FROM _wf_cte AS w
      WHERE w.grp_id = 7
        AND w.rn = 1
        AND w.wf_probe_1 IS NULL
    )
      AND c1 + c1 <= ANY (SELECT c1 FROM t0)
  ),
  'where_rows',
  COALESCE(
    (
      SELECT GROUP_CONCAT(c1 ORDER BY c1 SEPARATOR ',')
      FROM t0
      WHERE EXISTS (
        SELECT 1
        FROM _wf_cte AS w
        WHERE w.grp_id = 7
          AND w.rn = 1
          AND w.wf_probe_1 IS NULL
      )
        AND c1 + c1 <= ANY (SELECT c1 FROM t0)
    ),
    ''
  ),
  'window_check_count',
  (
    SELECT SUM(flag)
    FROM (
      SELECT (
        EXISTS (
          SELECT 1
          FROM _wf_cte AS w
          WHERE w.grp_id = 7
            AND w.rn = 1
            AND w.wf_probe_1 IS NULL
        )
        AND (c1 + c1 <= ANY (SELECT c1 FROM t0))
      ) IS TRUE AS flag
      FROM t0
    ) AS q
  )
) AS observation
FROM (
  SELECT 1 AS anchor
  FROM _wf_cte
  WHERE grp_id = 7 AND rn = 1
  LIMIT 1
) AS _wf_anchor;

Expected:
{"where_count": 2, "where_rows": "1,X", "window_check_count": 2}

Actual:
{"where_count": 1, "where_rows": "X", "window_check_count": 2}
[22 Apr 4:23] Chaithra Marsur Gopala Reddy
Hi QiFan Liu,

Thank you for the test case. Verified as described.

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

Page generated in 0.037 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.