Bug #121965 Semijoin inner table joined with no duplicate-removal strategy: EXISTS/IN over derived tables returns duplicate rows (de

Submitted: 27 Nov 22:35
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, duplicate rows, semijoin, wrong result

[27 Nov 22:35] Abhishek Soni
Description:
With the default optimizer_switch, a join over derived tables whose definitions use IN / EXISTS subqueries returns duplicate rows. IN and EXISTS cannot add rows, so the query must return exactly the rows of the plain join it is derived from (2 rows); it returns 4 (each correct row twice).

The derived tables (written as CTEs below) only keep rows that have a match in another table:
  r1 = rows of t1 whose c0 appears in t2
  r2 = rows of t0 whose c0 appears as c1 in r1
  r3 = rows of t1 whose c0 appears as c1 in r2
so joining t2, r1, r2, r3 gives the same rows as joining t2, t1, t0, t1 with the same join conditions. By hand: r1 = {(3,1)}, r2 = {(1,4)}, r3 = {(4,5)}, and t2 has two rows with c0 = 3, so the answer is two rows (4), (4).

Results (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: 4 rows (wrong)
  semijoin=off: 2 rows
  duplicateweedout=off: 2 rows
  firstmatch=off: 2 rows
  materialization=off: 2 rows

The same wrong result appears when the CTEs are written as nested derived tables, so this does not depend on WITH. Dropping the index on t1(c0), or replacing r3 by plain t1, gives the correct answer.

Plan observation (traditional EXPLAIN, 8.0.35 and 26.10.0): the semijoin inner table y of r1 (t2 inside "x.c0 IN (SELECT y.c0 FROM t2 y)") is the second table in the join order with "Using where; Using join buffer (hash join)" and no Start temporary / End temporary, FirstMatch or LooseScan marker. In EXPLAIN FORMAT=TREE it appears as a plain "Inner hash join (y.c0 = a0.c0)" with no deduplication above it, so each matching t2 row multiplies the output.

Possibly related: Bug #121026 (duplicate-weedout missing from the plan; needs NOT EXISTS, which this query does not use) and Bug #121964 (wrong result with semijoin Materialization over similar nested EXISTS chains; there rows go missing). In our randomized testing this duplicate-row pattern appeared in several hundred generated queries under the default optimizer_switch.

Found while testing query rewrites based on semijoin reduction (Yannakakis-style).

How to repeat:
CREATE TABLE t0 (c0 INT, c1 INT);
CREATE TABLE t1 (c0 INT, c1 INT);
CREATE INDEX t1_i0 ON t1 (c0);
CREATE TABLE t2 (c0 INT, c1 INT);
INSERT INTO t0 VALUES (NULL,0),(0,1),(1,4),(NULL,1),(3,6),(2,1),(0,3),(4,2),(4,0),(0,0),(NULL,5),(0,0);
INSERT INTO t1 VALUES (0,0),(0,0),(0,0),(0,0),(3,1),(4,5),(6,5);
INSERT INTO t2 VALUES (3,4),(3,3);
ANALYZE TABLE t0, t1, t2;

-- default optimizer_switch
WITH r1 AS (SELECT * FROM t1 x WHERE x.c0 IN (SELECT y.c0 FROM t2 y)),
     r2 AS (SELECT * FROM t0 x WHERE EXISTS (SELECT 1 FROM r1 y WHERE y.c1 = x.c0)),
     r3 AS (SELECT * FROM t1 x WHERE x.c0 IN (SELECT y.c1 FROM r2 y))
SELECT a2.c1 FROM t2 a0, r1 a1, r2 a2, r3 a3
WHERE a0.c0 = a1.c0 AND a1.c1 = a2.c0 AND a2.c1 = a3.c0;
-- Actual: 4 rows (4),(4),(4),(4)
-- Expected: 2 rows (4),(4)

-- Reference: the plain join returns 2 rows (4),(4)
SELECT a2.c1 FROM t2 a0, t1 a1, t0 a2, t1 a3
WHERE a0.c0 = a1.c0 AND a1.c1 = a2.c0 AND a2.c1 = a3.c0;

-- SET optimizer_switch='semijoin=off' (or duplicateweedout=off, firstmatch=off, materialization=off): the WITH query returns 2 rows.

Suggested fix:
Not known. The plan suggests that the semijoin nest containing t2 (alias y in r1) ends up with no duplicate-elimination strategy, so it is executed as a plain inner (hash) join; see the plan observation in the description.

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

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