<?xml version="1.0" encoding="utf-8"?>
<launchpad-bug id="825075">
  <date_last_updated>2012-04-05 09:43:32.947833+00:00</date_last_updated>
  <api_links>
    <bug_api_link>https://api.launchpad.net/1.0/bugs/825075</bug_api_link>
    <bug_owner_link>https://api.launchpad.net/1.0/~pstoev-askmonty</bug_owner_link>
    <milestone_link>https://api.launchpad.net/1.0/maria/+milestone/5.2</milestone_link>
    <linked_branches_collection_link>https://api.launchpad.net/1.0/bugs/825075/linked_branches</linked_branches_collection_link>
    <activity_link>https://api.launchpad.net/1.0/bugs/825075/activity</activity_link>
  </api_links>
  <bug_web_link>https://bugs.launchpad.net/bugs/825075</bug_web_link>
  <owner>Philip Stoev</owner>
  <assignee>Timour Katchaounov</assignee>
  <milestone_title>Maria 5.2</milestone_title>
  <duplicate_link></duplicate_link>
  <duplicate_bug_id></duplicate_bug_id>
  <title>Wrong result with GROUP BY + multipart key + MIN/MAX loose scan</title>
  <status>Confirmed</status>
  <importance>High</importance>
  <created>2011-08-12 09:20:57.394133+00:00</created>
  <description>
<![CDATA[The following query:

SELECT MIN(a), b
FROM t1
WHERE a > ( SELECT a FROM t2 WHERE a = 0 )
GROUP BY b;

does not return as many rows as if the subquery is replaced with a constant.

explain:

id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
1       PRIMARY t1      range   NULL    b       10      NULL    10      Using where; Using index for group-by
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       Using where

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=off,derived_merge=off,derived_with_keys=off,firstmatch=off,loosescan=off,materialization=off,in_to_exists=on,semijoin=off,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=off,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, a)) ;
INSERT INTO t1 VALUES (0,99),(9,99),(4,0),(7,0),(99,0),(7,0),(8,0),(99,0),(1,0);
CREATE TABLE t2 (a int) ;
INSERT INTO t2 VALUES (0),(1);
SELECT MIN(a), b
FROM t1
WHERE a > ( SELECT a FROM t2 WHERE a = 0 )
GROUP BY b;

returns:

MIN(a)  b
1       0

SELECT MIN(a), b
FROM t1
WHERE a > 0
GROUP BY b;

returns:

MIN(a)  b
1       0
9       99

bzr version-info:

revision-id: <email address hidden>
date: 2011-08-11 22:34:41 -0700
build-date: 2011-08-12 12:20:36 +0300
revno: 3151
branch-nick: maria-5.3

Repeatable on maria-5.3. Not repeatable on maria-5.2, mysql-5.5. Not repeatable with other subquery operators. Does not involve NULLs or constant tables.]]>  </description>
  <activities>
    <activity datechanged="2011-08-12T09:20:57.394133+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[]]>      </newvalue>
      <whatchanged>bug</whatchanged>
      <person>Philip Stoev</person>
      <message>added bug</message>
    </activity>
    <activity datechanged="2011-08-12T09:21:00.959358+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[5.3]]>      </newvalue>
      <whatchanged>maria: milestone</whatchanged>
      <person>Philip Stoev</person>
      <message></message>
    </activity>
    <activity datechanged="2011-08-12T09:29:22.910150+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[Oleksandr "Sanja" Byelkin (sanja-byelkin)]]>      </newvalue>
      <whatchanged>maria: assignee</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-10-11T13:52:30.643987+00:00">
      <oldvalue>
<![CDATA[5.3]]>      </oldvalue>
      <newvalue>
<![CDATA[]]>      </newvalue>
      <whatchanged>maria: milestone</whatchanged>
      <person>Igor Babaev</person>
      <message></message>
    </activity>
    <activity datechanged="2011-10-12T06:52:36.617633+00:00">
      <oldvalue>
<![CDATA[Undecided]]>      </oldvalue>
      <newvalue>
<![CDATA[High]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2011-10-12T08:09:37.261832+00:00">
      <oldvalue>
<![CDATA[High]]>      </oldvalue>
      <newvalue>
<![CDATA[Medium]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2011-10-12T08:09:39.274922+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[5.3]]>      </newvalue>
      <whatchanged>maria: milestone</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2011-10-12T08:10:48.017142+00:00">
      <oldvalue>
<![CDATA[New]]>      </oldvalue>
      <newvalue>
<![CDATA[In Progress]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2011-10-12T08:11:04.554044+00:00">
      <oldvalue>
<![CDATA[Medium]]>      </oldvalue>
      <newvalue>
<![CDATA[High]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2011-10-25T19:00:13.432481+00:00">
      <oldvalue>
<![CDATA[5.3]]>      </oldvalue>
      <newvalue>
<![CDATA[5.2]]>      </newvalue>
      <whatchanged>maria: milestone</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2011-10-27T15:50:18.369985+00:00">
      <oldvalue>
<![CDATA[Oleksandr "Sanja" Byelkin (sanja-byelkin)]]>      </oldvalue>
      <newvalue>
<![CDATA[Timour Katchaounov (timour)]]>      </newvalue>
      <whatchanged>maria: assignee</whatchanged>
      <person>Sergey Petrunia</person>
      <message></message>
    </activity>
    <activity datechanged="2011-10-28T08:00:18.527486+00:00">
      <oldvalue>
<![CDATA[In Progress]]>      </oldvalue>
      <newvalue>
<![CDATA[Confirmed]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-11-22T14:31:22.835546+00:00">
      <oldvalue>
<![CDATA[Wrong result with GROUP BY + scalar subquery  + multipart key ]]>      </oldvalue>
      <newvalue>
<![CDATA[Wrong result with GROUP BY +  scalar subquery  + multipart key + MIN/MAX loose scan]]>      </newvalue>
      <whatchanged>summary</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-11-30T12:20:22.052490+00:00">
      <oldvalue>
<![CDATA[Confirmed]]>      </oldvalue>
      <newvalue>
<![CDATA[In Progress]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-12-07T11:55:01.435624+00:00">
      <oldvalue>
<![CDATA[In Progress]]>      </oldvalue>
      <newvalue>
<![CDATA[Fix Committed]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-12-13T14:35:53.666260+00:00">
      <oldvalue>
<![CDATA[Fix Committed]]>      </oldvalue>
      <newvalue>
<![CDATA[In Progress]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2012-03-23T14:31:46.265879+00:00">
      <oldvalue>
<![CDATA[Wrong result with GROUP BY +  scalar subquery  + multipart key + MIN/MAX loose scan]]>      </oldvalue>
      <newvalue>
<![CDATA[Wrong result with GROUP BY + multipart key + MIN/MAX loose scan]]>      </newvalue>
      <whatchanged>summary</whatchanged>
      <person>Elena Stepanova</person>
      <message></message>
    </activity>
    <activity datechanged="2012-04-03T23:13:23.126233+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[optimizer upstream wrong-result]]>      </newvalue>
      <whatchanged>tags</whatchanged>
      <person>Elena Stepanova</person>
      <message></message>
    </activity>
    <activity datechanged="2012-04-05T09:43:31.715419+00:00">
      <oldvalue>
<![CDATA[In Progress]]>      </oldvalue>
      <newvalue>
<![CDATA[Confirmed]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
  </activities>
  <comments>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/825075/comments/0" datecreated="2011-08-12T09:20:57.394133+00:00">
      <person>Philip Stoev</person>
      <subject>
<![CDATA[Wrong result with GROUP BY + scalar subquery  + multipart key ]]>      </subject>
      <content>
<![CDATA[The following query:

SELECT MIN(a), b
FROM t1
WHERE a > ( SELECT a FROM t2 WHERE a = 0 )
GROUP BY b;

does not return as many rows as if the subquery is replaced with a constant.

explain:

id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
1       PRIMARY t1      range   NULL    b       10      NULL    10      Using where; Using index for group-by
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       Using where

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=off,derived_merge=off,derived_with_keys=off,firstmatch=off,loosescan=off,materialization=off,in_to_exists=on,semijoin=off,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=off,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, a)) ;
INSERT INTO t1 VALUES (0,99),(9,99),(4,0),(7,0),(99,0),(7,0),(8,0),(99,0),(1,0);
CREATE TABLE t2 (a int) ;
INSERT INTO t2 VALUES (0),(1);
SELECT MIN(a), b
FROM t1
WHERE a > ( SELECT a FROM t2 WHERE a = 0 )
GROUP BY b;

returns:

MIN(a)  b
1       0

SELECT MIN(a), b
FROM t1
WHERE a > 0
GROUP BY b;

returns:

MIN(a)  b
1       0
9       99

bzr version-info:

revision-id: igor@askmonty.org-20110812053441-rrax06kfhjlmkq45
date: 2011-08-11 22:34:41 -0700
build-date: 2011-08-12 12:20:36 +0300
revno: 3151
branch-nick: maria-5.3

Repeatable on maria-5.3. Not repeatable on maria-5.2, mysql-5.5. Not repeatable with other subquery operators. Does not involve NULLs or constant tables.]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/825075/comments/1" datecreated="2011-10-12T08:30:51.372154+00:00">
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <subject>
<![CDATA[Re: Wrong result with GROUP BY + scalar subquery  + multipart key ]]>      </subject>
      <content>
<![CDATA[It looks like it is important to have range access, explain of incorrect execution:

+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	PRIMARY	t1	range	NULL	b	10	NULL	10	90.00	Using where; Using index for group-by
+2	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
+Warnings:
+Note	1003	select min(`test`.`t1`.`a`) AS `MIN(a)`,`test`.`t1`.`b` AS `b` from `test`.`t1` where (`test`.`t1`.`a` > (select `test`.`t2`.`a` from `test`.`t2` where (`test`.`t2`.`a` = 0))) group by `test`.`t1`.`b`

If reduce test suite table to INSERT INTO t1 VALUES (0,99),(9,99),(4,0),(7,0),(99,0),(7,0),(8,0),(99,0),(1,0); it works correctly with other explain:

+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	PRIMARY	t1	index	NULL	b	10	NULL	6	100.00	Using where; Using index
+2	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
+Warnings:
+Note	1003	select min(`test`.`t1`.`a`) AS `MIN(a)`,`test`.`t1`.`b` AS `b` from `test`.`t1` where (`test`.`t1`.`a` > (select `test`.`t2`.`a` from `test`.`t2` where (`test`.`t2`.`a` = 0))) group by `test`.`t1`.`b`]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/825075/comments/2" datecreated="2011-10-20T11:55:31.078075+00:00">
      <person>Philip Stoev</person>
      <subject>
<![CDATA[Re: Wrong result with GROUP BY + scalar subquery  + multipart key ]]>      </subject>
      <content>
<![CDATA[This is repeatable without a subquery:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (  a int,  b varchar(1), KEY (b,a));
INSERT INTO t1 VALUES (0,NULL),(9,NULL),(8,'c'),(4,'d'),(7,'d'),(NULL,'f'),(7,'f'),(8,'g'),(NULL,'j');

SELECT a , b FROM t1 WHERE a IS NULL OR b = 'z' ;
SELECT max(a) , b FROM t1 WHERE a IS NULL OR b = 'z' GROUP BY b;







]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/825075/comments/3" datecreated="2011-10-20T11:56:09.718122+00:00">
      <person>Philip Stoev</person>
      <subject>
<![CDATA[Re: Wrong result with GROUP BY + scalar subquery  + multipart key ]]>      </subject>
      <content>
<![CDATA[Also, for a certain set of values, it is also repeatable in mysql 5.5 . So, it may be a legacy thing.]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/825075/comments/4" datecreated="2011-10-25T19:00:05.705154+00:00">
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <subject>
<![CDATA[Re: Wrong result with GROUP BY + scalar subquery  + multipart key ]]>      </subject>
      <content>
<![CDATA[I checked the bug in 5.1 and it is repeatable.]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/825075/comments/5" datecreated="2011-10-27T15:50:39.247037+00:00">
      <person>Sergey Petrunia</person>
      <subject>
<![CDATA[Re: Wrong result with GROUP BY + scalar subquery  + multipart key ]]>      </subject>
      <content>
<![CDATA[Re-assigning to Timour because the problem is with min-max loose scan.]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/825075/comments/6" datecreated="2011-12-01T14:27:44.892949+00:00">
      <person>Timour Katchaounov</person>
      <subject>
<![CDATA[Re: Wrong result with GROUP BY +  scalar subquery  + multipart key + MIN/MAX loose scan]]>      </subject>
      <content>
<![CDATA[The following query also produces wrong result:


SELECT b, min(a) FROM t1 WHERE a = 7 OR b = 'z' GROUP BY b;
+------+--------+
| b    | min(a) |
+------+--------+
| f    |      7 |
+------+--------+

While the correct result is:

+------+--------+
| b    | min(a) |
+------+--------+
| d    |      7 |
| f    |      7 |
+------+--------+
]]>      </content>
    </comment>
  </comments>
</launchpad-bug>
