Bug #121964 Semijoin Materialization returns no rows for nested EXISTS over derived tables (1-row table, correct answer 1 row)

Submitted: 27 Nov 22:23
Reporter: Abhishek Soni Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:8.0.35, 8.4.13, 9.7.4, 26.10.0 OS:Linux (Amazon Linux 2023 (aarch64) and Ubuntu 24.04 (x86_64), official generic Linux tarballs)
Assigned to: CPU Architecture:Any (aarch64 and x86_64)
Tags: derived table, materialization, semijoin, wrong result

[27 Nov 22:23] Abhishek Soni
Description:
A query whose derived tables (written as CTEs here) are each filtered by EXISTS on the previous one returns an empty result when the semijoin Materialization strategy is used. The table has one row, and the same query returns that row with semijoin=off and with every other single semijoin strategy (FirstMatch, LooseScan, DuplicateWeedout).

Each CTE only keeps rows of t that have a match in another CTE, and every CTE contains the single row (0,1), so the query must return (0,1), the same as the plain join of t with itself (shown in How to repeat).

The result is the same if the CTEs are replaced by nested derived tables or by views, so this does not depend on WITH. All eight EXISTS steps are needed: removing any one of them gives the correct row. In our randomized tests the same symptom (missing rows) also appeared with the default optimizer_switch when the cost model chose Materialization (147 of 164 such cases); the switch setting in the test case only makes it deterministic.

Results are identical on 8.0.35, 8.4.13, 9.7.4 and 26.10.0 (official generic Linux tarballs, aarch64 and x86_64):
  default optimizer_switch: 1 row
  Materialization only:     0 rows (wrong)
  semijoin=off:             1 row

Found while testing query rewrites based on semijoin reduction (Yannakakis-style): the rewritten query must return the same rows as the original join.

How to repeat:
CREATE TABLE t (c0 INT, c1 INT);
INSERT INTO t VALUES (0, 1);

SET optimizer_switch = 'semijoin=on,firstmatch=off,loosescan=off,materialization=on,duplicateweedout=off';

WITH r1 AS (SELECT * FROM t WHERE EXISTS (SELECT 1 FROM t y WHERE y.c0 = t.c0)),
     r2 AS (SELECT * FROM t WHERE EXISTS (SELECT 1 FROM r1 y WHERE y.c0 = t.c0)),
     r3 AS (SELECT * FROM t WHERE EXISTS (SELECT 1 FROM r2 y WHERE y.c1 = t.c1)),
     r4 AS (SELECT * FROM t WHERE EXISTS (SELECT 1 FROM r3 y WHERE y.c0 = t.c0)),
     r5 AS (SELECT * FROM r3 x WHERE EXISTS (SELECT 1 FROM r4 y WHERE y.c0 = x.c0)),
     r6 AS (SELECT * FROM r2 x WHERE EXISTS (SELECT 1 FROM r5 y WHERE y.c1 = x.c1)),
     r7 AS (SELECT * FROM r1 x WHERE EXISTS (SELECT 1 FROM r6 y WHERE y.c0 = x.c0)),
     r8 AS (SELECT * FROM t WHERE EXISTS (SELECT 1 FROM r7 y WHERE y.c0 = t.c0))
SELECT a.c0, a.c1 FROM r8 a, r7 b, r6 c, r5 d, r4 e
WHERE a.c0 = b.c0 AND a.c0 = c.c0 AND c.c1 = d.c1 AND d.c0 = e.c0;
-- Actual: Empty set
-- Expected: one row (0, 1)

-- Reference: the plain join returns (0, 1)
SELECT a.c0, a.c1 FROM t a, t b, t c, t d, t e
WHERE a.c0 = b.c0 AND a.c0 = c.c0 AND c.c1 = d.c1 AND d.c0 = e.c0;

-- With SET optimizer_switch = DEFAULT, or 'semijoin=off', or FirstMatch / LooseScan / DuplicateWeedout only, the WITH query returns (0, 1).

Suggested fix:
Not known. One observation from EXPLAIN FORMAT=TREE on 26.10.0 with the switch above: the outermost step is "Single-row index lookup on <subquery95> using <auto_distinct_key> (c0 = `<subquery95>`.c0)", i.e. the lookup key refers to the materialized table it is looking up, and conditions such as (`<subquery85>`.c0 = `<subquery95>`.c0) appear inside the materialization of other semijoin nests. This suggests that equalities propagated across several materialized semijoin nests reference a nest outside its scope.

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

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