[MDEV-3269] LP:900375 - Wrong result with derived_merge=ON, DISTINCT or GROUP BY, EXISTS Created: 2011-12-05  Updated: 2015-02-02  Resolved: 2012-10-04

Status: Closed
Project: MariaDB Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Critical
Reporter: Elena Stepanova Assignee: Timour Katchaounov (Inactive)
Resolution: Fixed Votes: 0
Labels: Launchpad

Attachments: XML File LPexportBug900375.xml    

 Description   

The following query

SELECT alias1.*
FROM t1, ( SELECT * FROM t1 ) AS alias1
WHERE EXISTS ( SELECT DISTINCT b FROM t1 WHERE b <= alias1.a )

with 10 rows in the table, each of which meets the WHERE condition, with derived_merge=ON returns 9 rows instead of 100.
If I remove at least one row from the table, the query starts producing the full resultset.
With derived_merge=OFF the query returns 100 rows as expected.

Same happens with GROUP BY instead of DISTINCT.

revno: 3324
revision-id: <email address hidden>

Reproducible on mariadb 5.2.9 (with default optimizer_switch) and on mysql 5.5.18.

EXPLAIN with derived_merge=on:

EXPLAIN SELECT alias1.* FROM t1, ( SELECT * FROM t1 ) AS alias1 WHERE EXISTS ( SELECT DISTINCT b FROM t1 WHERE b <= alias1.a ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 index NULL b 5 NULL 10 Using index
1 PRIMARY t1 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
3 DEPENDENT SUBQUERY t1 range b b 5 NULL 6Using where; Using index for group-by; Using temporary

EXPLAIN with derived_merge=off:

EXPLAIN SELECT alias1.* FROM t1, ( SELECT * FROM t1 ) AS alias1 WHERE EXISTS ( SELECT DISTINCT b FROM t1 WHERE b <= alias1.a ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 index NULL b 5 NULL 10 Using index
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
3 DEPENDENT SUBQUERY t1 range b b 5 NULL 6Range checked for each record (index map: 0x1); Using temporary
2 DERIVED t1 ALL NULL NULL NULL NULL 10

Minimal optimizer_switch: none (default)
Full optimizer_switch: index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=off,semijoin_with_cache=off,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on

Test case:

CREATE TABLE t1 ( a INT, b INT, KEY (b) );

INSERT INTO t1 VALUES
(100,10),(101,11),(102,12),(103,13),(104,14),
(105,15),(106,16),(107,17),(108,18),(109,19);

SELECT alias1.*
FROM t1, ( SELECT * FROM t1 ) AS alias1
WHERE EXISTS ( SELECT DISTINCT b FROM t1 WHERE b <= alias1.a ) ;



 Comments   
Comment by Elena Stepanova [ 2011-12-05 ]

Re: Wrong result with derived_merge=ON, DISTINCT or GROUP BY, EXISTS
While saying that the issue is reprodubile on mariadb 5.2 and mysql 5.5.18, I just mean that they produce the same wrong resultset.

mariadb 5.2.9:

EXPLAIN SELECT alias1.*
FROM t1, ( SELECT * FROM t1 ) AS alias1
WHERE EXISTS ( SELECT DISTINCT b FROM t1 WHERE b <= alias1.a ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 index NULL b 5 NULL 10 Using index
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10 Using where; Using join buffer
3 DEPENDENT SUBQUERY t1 range b b 5 NULL 6Using where; Using index for group-by; Using temporary
2 DERIVED t1 ALL NULL NULL NULL NULL 10

mysql 5.5.18:

EXPLAIN SELECT alias1.*
FROM t1, ( SELECT * FROM t1 ) AS alias1
WHERE EXISTS ( SELECT DISTINCT b FROM t1 WHERE b <= alias1.a ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 index NULL b 5 NULL 10 Using index
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10 Using where; Using join buffer
3 DEPENDENT SUBQUERY t1 range b b 5 NULL 6Using where; Using index for group-by; Using temporary
2 DERIVED t1 ALL NULL NULL NULL NULL 10

Comment by Timour Katchaounov (Inactive) [ 2011-12-12 ]

Re: Wrong result with derived_merge=ON, DISTINCT or GROUP BY, EXISTS
Pushed into 5.2.

Comment by Rasmus Johansson (Inactive) [ 2011-12-12 ]

Launchpad bug id: 900375

Generated at Thu Feb 08 06:47:28 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.