[MDEV-30256] Wrong result (missing rows) upon join with empty table Created: 2022-12-17  Updated: 2023-02-10  Resolved: 2023-01-05

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: N/A
Fix Version/s: 11.0.1

Type: Bug Priority: Critical
Reporter: Elena Stepanova Assignee: Michael Widenius
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Problem/Incident
is caused by MDEV-26974 Improve selectivity and related costs... Closed
Relates

 Description   

CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2);
 
CREATE TABLE t2 (b INT);
INSERT INTO t2 VALUES (3),(4);
 
CREATE TABLE t3 (c INT PRIMARY KEY) ENGINE=HEAP;
 
SELECT * FROM t1 LEFT JOIN t2 JOIN t3 ON (t2.b >= t3.c) ON (t1.a < t2.b);

bb-11.0 d8531ea4b3

SELECT * FROM t1 LEFT JOIN t2 JOIN t3 ON (t2.b >= t3.c) ON (t1.a < t2.b);
a	b	c
 
EXPLAIN EXTENDED
SELECT * FROM t1 LEFT JOIN t2 JOIN t3 ON (t2.b >= t3.c) ON (t1.a < t2.b);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
Warnings:
Note	1003	select `test`.`t1`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t3`.`c` AS `c` from `test`.`t1` left join (`test`.`t2` join `test`.`t3`) on(`test`.`t1`.`a` < `test`.`t2`.`b` and `test`.`t2`.`b` >= `test`.`t3`.`c`) where 1

The baseline produces the result set as expected:

10.11 936436ef4

SELECT * FROM t1 LEFT JOIN t2 JOIN t3 ON (t2.b >= t3.c) ON (t1.a < t2.b);
a	b	c
1	NULL	NULL
2	NULL	NULL
 
EXPLAIN EXTENDED
SELECT * FROM t1 LEFT JOIN t2 JOIN t3 ON (t2.b >= t3.c) ON (t1.a < t2.b);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	
1	SIMPLE	t3	ALL	PRIMARY	NULL	NULL	NULL	0	0.00	Using join buffer (flat, BNL join)
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using join buffer (incremental, BNL join)
Warnings:
Note	1003	select `test`.`t1`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t3`.`c` AS `c` from `test`.`t1` left join (`test`.`t2` join `test`.`t3`) on(`test`.`t1`.`a` < `test`.`t2`.`b` and `test`.`t2`.`b` >= `test`.`t3`.`c`) where 1



 Comments   
Comment by Michael Widenius [ 2022-12-27 ]

It looks like one gets the same wrong result with any engine that has exact record count (myisam, aria, heap)

Comment by Michael Widenius [ 2023-01-05 ]

The problem was an assignment in test_quick_select() that flagged empty
tables with "Impossible where". This test was however wrong as it didn't work correctly for left join.

Removed the test, but added checking of empty tables in DELETE and UPDATE to get similar EXPLAIN as before.

Generated at Thu Feb 08 10:14:52 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.