Bug #120097 Incorrect result set when use INTERSECT ALL
| Submitted: | 18 Mar 9:34 | Modified: | 18 Mar 9:48 |
|---|---|---|---|
| Reporter: | Guo Yuxiao | Email Updates: | |
| Status: | Verified | Impact on me: | |
| Category: | MySQL Server: Optimizer | Severity: | S1 (Critical) |
| Version: | 8.0.45 | OS: | Any |
| Assigned to: | CPU Architecture: | Any |
[18 Mar 9:48]
Roy Lyseng
Thank you for the bug report. Verified as described.
[4 Nov 21:11]
Abhishek Soni
Additional information: the same row loss happens with EXCEPT ALL, with a smaller test case, and it seems to come from duplicate weedout over the materialized set operation.
How to repeat:
CREATE TABLE t0 (c0 INT);
CREATE TABLE t1 (c0 INT);
INSERT INTO t0 VALUES (7), (7);
SELECT COUNT(*)
FROM ((SELECT c0 FROM t0) EXCEPT ALL (SELECT c0 FROM t1)) AS a
WHERE EXISTS (SELECT 1 FROM t0 AS b WHERE a.c0 >= b.c0);
-- expected 2, returns 1
Observations (26.10.0):
- (SELECT c0 FROM t0) INTERSECT ALL (SELECT c0 FROM t0) in the same place also returns 1 instead of 2.
- UNION ALL in the same place returns 2 (correct).
- Correct with SET optimizer_switch='duplicateweedout=off' or 'semijoin=off'.
- Still wrong with internal_tmp_mem_storage_engine=MEMORY.
- (Checked on 9.4.0) Correct with an equality correlation (a.c0 = b.c0), with IN, or with a base table instead of the derived table; still wrong with firstmatch=off, loosescan=off, materialization=off, derived_merge=off or hash_set_operations=off.
EXPLAIN FORMAT=TREE on 26.10.0 (costs removed):
-> Aggregate: count(0)
-> Remove duplicate a rows using temporary table (weedout)
-> Filter: (a.c0 >= b.c0)
-> Inner hash join (no condition)
-> Table scan on a
-> Except all materialize
-> Table scan on t0
-> Table scan on t1
-> Hash
-> Table scan on b
The query in this report behaves the same way on 26.10.0: its plan has "Remove duplicate t_wrapper_0 rows using temporary table (weedout)" above "Intersect all materialize", and it returns 16 rows with duplicateweedout=off or semijoin=off and 1 row otherwise.
Affected versions (official Docker images, default settings): 8.0.31 (the first release with INTERSECT / EXCEPT), 8.0.46, 8.4.13, 9.4.0, 9.7.2, 9.7.4, 26.7.0, 26.10.0.
A possible cause (not checked in the source): the documentation of TableScanIterator::m_remaining_dups says that for EXCEPT and INTERSECT "we only ever materialize one row even if there are duplicates of it, but with a counter". If weedout identifies rows of the derived table by their row position, all copies of a value share one position and are removed as duplicates. Bug #119225 has a different trigger with the same kind of mechanism (weedout relying on row positions of a temporary table).
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.041 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.
Description: I found a logic bug in MySQL 9.6.0. The queries Q2 and Q4 are equivalent, yet they yield different execution results. Below is the construction process of Q2 and Q4: -- Q1, result:{''*16,'NULL'} SELECT t2.c0 AS c0 FROM t0 AS t1 RIGHT JOIN t2 AS t2 ON TRUE LEFT JOIN t2 AS t3 ON (t2.c0 = t3.c0); -- Q2 (Q1 + WHERE), result:{''*16} SELECT t2.c0 AS c0 FROM t0 AS t1 RIGHT JOIN t2 AS t2 ON TRUE LEFT JOIN t2 AS t3 ON (t2.c0 = t3.c0) WHERE EXISTS(SELECT t1.c0 AS c0 FROM t2 AS t4 WHERE NOT t2.c0 IS NULL); -- Q3 result:{''*16,'NULL'} -- Q3 = Q1 INTERSECT ALL Q1 SELECT t_wrapper_0.c0 FROM ( SELECT t2.c0 AS c0 FROM t0 AS t1 RIGHT JOIN t2 AS t2 ON TRUE LEFT JOIN t2 AS t3 ON (t2.c0 = t3.c0) INTERSECT ALL SELECT t2.c0 AS c0 FROM t0 AS t1 RIGHT JOIN t2 AS t2 ON TRUE LEFT JOIN t2 AS t3 ON (t2.c0 = t3.c0) ) AS t_wrapper_0; -- Q4, expected result:{''*16}, actual result:{''} -- Q4 = Q3 + WHERE -- It looks different from the where condition in Q2, but it is actually equivalent SELECT t_wrapper_0.c0 FROM ( SELECT t2.c0 AS c0 FROM t0 AS t1 RIGHT JOIN t2 AS t2 ON TRUE LEFT JOIN t2 AS t3 ON (t2.c0 = t3.c0) INTERSECT ALL SELECT t2.c0 AS c0 FROM t0 AS t1 RIGHT JOIN t2 AS t2 ON TRUE LEFT JOIN t2 AS t3 ON (t2.c0 = t3.c0) ) AS t_wrapper_0 WHERE EXISTS( SELECT t_wrapper_0.c0 AS c0 FROM t2 AS t4 WHERE NOT t_wrapper_0.c0 IS NULL ); How to repeat: -- create table DROP TABLE IF EXISTS t0; CREATE TABLE t0 ( c0 longtext ) CHARSET=utf8mb4; INSERT INTO t0 VALUES (NULL); DROP TABLE IF EXISTS t2; CREATE TABLE t2 ( c0 longtext ) CHARSET=utf8mb4; INSERT INTO t2 VALUES (''),(''),(''),(''),(NULL); -- Q2, result:{''*16} SELECT t2.c0 AS c0 FROM t0 AS t1 RIGHT JOIN t2 AS t2 ON TRUE LEFT JOIN t2 AS t3 ON (t2.c0 = t3.c0) WHERE EXISTS(SELECT t1.c0 AS c0 FROM t2 AS t4 WHERE NOT t2.c0 IS NULL); -- Q4, expected result:{''*16}, actual result:{''} SELECT t_wrapper_0.c0 FROM ( SELECT t2.c0 AS c0 FROM t0 AS t1 RIGHT JOIN t2 AS t2 ON TRUE LEFT JOIN t2 AS t3 ON (t2.c0 = t3.c0) INTERSECT ALL SELECT t2.c0 AS c0 FROM t0 AS t1 RIGHT JOIN t2 AS t2 ON TRUE LEFT JOIN t2 AS t3 ON (t2.c0 = t3.c0) ) AS t_wrapper_0 WHERE EXISTS( SELECT t_wrapper_0.c0 AS c0 FROM t2 AS t4 WHERE NOT t_wrapper_0.c0 IS NULL );