Bug #120840 VIEW and equivalent CTE differ when LOWER(JSON_TYPE(...)) is compared with JSON_TYPE() result in a UNION query
Submitted: 2 Jul 16:20 Modified: 2 Jul 20:08
Reporter: Xiaoyuan Xie Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:9.6.0 OS:Ubuntu
Assigned to: CPU Architecture:Any

[2 Jul 16:20] Xiaoyuan Xie
Description:
A query using a VIEW returns a row, while the equivalent query using a CTE returns an empty result set. The CTE result appears to be correct.

The issue seems related to VIEW handling of JSON_TYPE() result collation, LOWER(), GROUP BY, COUNT(DISTINCT), and an outer UNION.

How to repeat:
mysql> select version();
+-----------+
| version() |
+-----------+
| 9.6.0     |
+-----------+

DROP DATABASE IF EXISTS dd;
CREATE DATABASE dd;
USE dd;

CREATE TABLE a (
  b JSON,
  c FLOAT
);

INSERT INTO a VALUES ('"WIromLCWVEYJ10t"', 0);

CREATE VIEW v0 AS
SELECT
  c AS d,
  JSON_TYPE(b) AS e,
  COUNT(DISTINCT 1) AS f
FROM a
GROUP BY d, e;

SELECT e
FROM v0
WHERE d

UNION

SELECT e
FROM v0
WHERE f AND LOWER(e) = e OR 0;

WITH cte AS (
  SELECT
    c AS d,
    JSON_TYPE(b) AS e,
    COUNT(DISTINCT 1) AS f
  FROM a
  GROUP BY d, e
)
SELECT e
FROM cte
WHERE d

UNION

SELECT e
FROM cte
WHERE f AND LOWER(e) = e OR 0;

The VIEW query returns one row:

+--------+
| e      |
+--------+
| STRING |
+--------+
The equivalent CTE query returns an empty result set:

Empty set
[2 Jul 20:08] Roy Lyseng
Thank you for the bug report.
Verified as described.