<?xml version="1.0" encoding="utf-8"?>
<launchpad-bug id="430669">
  <date_last_updated>2011-06-16 05:44:21.821201+00:00</date_last_updated>
  <api_links>
    <bug_api_link>https://api.launchpad.net/1.0/bugs/430669</bug_api_link>
    <bug_owner_link>https://api.launchpad.net/1.0/~knielsen</bug_owner_link>
    <milestone_link>https://api.launchpad.net/1.0/maria/+milestone/5.1</milestone_link>
    <linked_branches_collection_link>https://api.launchpad.net/1.0/bugs/430669/linked_branches</linked_branches_collection_link>
    <activity_link>https://api.launchpad.net/1.0/bugs/430669/activity</activity_link>
  </api_links>
  <bug_web_link>https://bugs.launchpad.net/bugs/430669</bug_web_link>
  <owner>Kristian Nielsen</owner>
  <assignee>Sergey Petrunia</assignee>
  <milestone_title>Maria 5.1</milestone_title>
  <duplicate_link></duplicate_link>
  <duplicate_bug_id></duplicate_bug_id>
  <title>Wrong output of EXPLAIN EXTENDED on subquery with unknown column error</title>
  <status>Invalid</status>
  <importance>Wishlist</importance>
  <created>2009-09-16 11:42:10.943382+00:00</created>
  <description>
<![CDATA[Seen in lp:maria in revision revid:<email address hidden> (after push of table elimination):

CREATE TABLE t1 (a INT, b INT, c INT);
INSERT INTO t1 VALUES (1,1,1), (1,1,1);
EXPLAIN EXTENDED 
SELECT c FROM 
( SELECT 
(SELECT COUNT(a) FROM 
(SELECT COUNT(b) FROM t1) AS x GROUP BY c
) FROM t1 GROUP BY b
) AS y;
ERROR 42S22: Unknown column 'c' in 'field list'
SHOW WARNINGS;
Level	Code	Message
Note	1276	Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #2
Note	1276	Field or reference 'test.t1.c' of SELECT #3 was resolved in SELECT #2
Error	1054	Unknown column 'c' in 'field list'
Note	1003	select `c` AS `c` from (select (select count(`test`.`t1`.`a`) AS `COUNT(a)` from (select count(`test`.`t1`.`b`) AS `COUNT(b)` from `test`.`t1`) `x` group by `t1`.`c`) AS `(SELECT COUNT(a) FROM 
(SELECT COUNT(b) FROM t1) AS x GROUP BY c
)` from `test`.`t1` group by `test`.`t1`.`b`) `y`

The problem here is this output "group by `t1`.`c`". The column `c` is unknown at that place in the query, so it makes no sense to qualify it with the table name t1.

This is related to MySQL Bug#37362 (http://bugs.mysql.com/bug.php?id=37362), from which the test case originates.

Before push of table elimination, the test case crashes the server, so the table elimination push fixes the crash, but produces wrong/strange output.

I will push the test case into lp:maria with wrong/strange result file (to not block the merge of MySQL 5.1.38). This needs to be updated after fixing the bug:

=== modified file 'mysql-test/r/subselect3.result'
--- mysql-test/r/subselect3.result	2009-08-13 20:33:00 +0000
+++ mysql-test/r/subselect3.result	2009-09-16 11:27:55 +0000
@@ -864,7 +864,7 @@ Level	Code	Message
 Note	1276	Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #2
 Note	1276	Field or reference 'test.t1.c' of SELECT #3 was resolved in SELECT #2
 Error	1054	Unknown column 'c' in 'field list'
+Note	1003	select `c` AS `c` from (select (select count(`test`.`t1`.`a`) AS `COUNT(a)` from (select count(`test`.`t1`.`b`) AS `COUNT(b)` from `test`.`t1`) `x` group by `t1`.`c`) AS `(SELECT COUNT(a) FROM 
-Note	1003	select `c` AS `c` from (select (select count(`test`.`t1`.`a`) AS `COUNT(a)` from (select count(`test`.`t1`.`b`) AS `COUNT(b)` from `test`.`t1`) `x` group by `c`) AS `(SELECT COUNT(a) FROM 
 (SELECT COUNT(b) FROM t1) AS x GROUP BY c
 )` from `test`.`t1` group by `test`.`t1`.`b`) `y`
 DROP TABLE t1;]]>  </description>
  <activities>
    <activity datechanged="2009-09-16T11:42:10.943382+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[]]>      </newvalue>
      <whatchanged>bug</whatchanged>
      <person>Kristian Nielsen</person>
      <message>added bug</message>
    </activity>
    <activity datechanged="2009-09-16T11:48:32.956569+00:00">
      <oldvalue>
<![CDATA[New]]>      </oldvalue>
      <newvalue>
<![CDATA[Confirmed]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Kristian Nielsen</person>
      <message></message>
    </activity>
    <activity datechanged="2009-09-16T11:48:32.956569+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[Sergey Petrunia (sergefp)]]>      </newvalue>
      <whatchanged>maria: assignee</whatchanged>
      <person>Kristian Nielsen</person>
      <message></message>
    </activity>
    <activity datechanged="2009-11-25T17:02:06.362498+00:00">
      <oldvalue>
<![CDATA[Undecided]]>      </oldvalue>
      <newvalue>
<![CDATA[Medium]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Hakan Küçükyılmaz</person>
      <message></message>
    </activity>
    <activity datechanged="2010-05-26T22:17:41.142901+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[http://bugs.mysql.com/bug.php?id=37362]]>      </newvalue>
      <whatchanged>bug watch added</whatchanged>
      <person>Hakan Küçükyılmaz</person>
      <message></message>
    </activity>
    <activity datechanged="2010-11-30T15:57:20.400744+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[5.1]]>      </newvalue>
      <whatchanged>maria: milestone</whatchanged>
      <person>Sergei</person>
      <message></message>
    </activity>
    <activity datechanged="2011-06-15T15:15:29.357366+00:00">
      <oldvalue>
<![CDATA[Medium]]>      </oldvalue>
      <newvalue>
<![CDATA[Wishlist]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Sergey Petrunia</person>
      <message></message>
    </activity>
    <activity datechanged="2011-06-15T16:21:05.326222+00:00">
      <oldvalue>
<![CDATA[Confirmed]]>      </oldvalue>
      <newvalue>
<![CDATA[Invalid]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Sergey Petrunia</person>
      <message></message>
    </activity>
  </activities>
  <comments>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/430669/comments/0" datecreated="2009-09-16T11:42:10.943382+00:00">
      <person>Kristian Nielsen</person>
      <subject>
<![CDATA[Wrong output of EXPLAIN EXTENDED on subquery with unknown column error]]>      </subject>
      <content>
<![CDATA[Seen in lp:maria in revision revid:psergey@askmonty.org-20090902084228-3ttm7xalqglzgjvs (after push of table elimination):

CREATE TABLE t1 (a INT, b INT, c INT);
INSERT INTO t1 VALUES (1,1,1), (1,1,1);
EXPLAIN EXTENDED 
SELECT c FROM 
( SELECT 
(SELECT COUNT(a) FROM 
(SELECT COUNT(b) FROM t1) AS x GROUP BY c
) FROM t1 GROUP BY b
) AS y;
ERROR 42S22: Unknown column 'c' in 'field list'
SHOW WARNINGS;
Level	Code	Message
Note	1276	Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #2
Note	1276	Field or reference 'test.t1.c' of SELECT #3 was resolved in SELECT #2
Error	1054	Unknown column 'c' in 'field list'
Note	1003	select `c` AS `c` from (select (select count(`test`.`t1`.`a`) AS `COUNT(a)` from (select count(`test`.`t1`.`b`) AS `COUNT(b)` from `test`.`t1`) `x` group by `t1`.`c`) AS `(SELECT COUNT(a) FROM 
(SELECT COUNT(b) FROM t1) AS x GROUP BY c
)` from `test`.`t1` group by `test`.`t1`.`b`) `y`

The problem here is this output "group by `t1`.`c`". The column `c` is unknown at that place in the query, so it makes no sense to qualify it with the table name t1.

This is related to MySQL Bug#37362 (http://bugs.mysql.com/bug.php?id=37362), from which the test case originates.

Before push of table elimination, the test case crashes the server, so the table elimination push fixes the crash, but produces wrong/strange output.

I will push the test case into lp:maria with wrong/strange result file (to not block the merge of MySQL 5.1.38). This needs to be updated after fixing the bug:

=== modified file 'mysql-test/r/subselect3.result'
--- mysql-test/r/subselect3.result	2009-08-13 20:33:00 +0000
+++ mysql-test/r/subselect3.result	2009-09-16 11:27:55 +0000
@@ -864,7 +864,7 @@ Level	Code	Message
 Note	1276	Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #2
 Note	1276	Field or reference 'test.t1.c' of SELECT #3 was resolved in SELECT #2
 Error	1054	Unknown column 'c' in 'field list'
+Note	1003	select `c` AS `c` from (select (select count(`test`.`t1`.`a`) AS `COUNT(a)` from (select count(`test`.`t1`.`b`) AS `COUNT(b)` from `test`.`t1`) `x` group by `t1`.`c`) AS `(SELECT COUNT(a) FROM 
-Note	1003	select `c` AS `c` from (select (select count(`test`.`t1`.`a`) AS `COUNT(a)` from (select count(`test`.`t1`.`b`) AS `COUNT(b)` from `test`.`t1`) `x` group by `c`) AS `(SELECT COUNT(a) FROM 
 (SELECT COUNT(b) FROM t1) AS x GROUP BY c
 )` from `test`.`t1` group by `test`.`t1`.`b`) `y`
 DROP TABLE t1;]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/430669/comments/1" datecreated="2010-05-26T22:17:41.142901+00:00">
      <person>Hakan Küçükyılmaz</person>
      <subject>
<![CDATA[Re: Wrong output of EXPLAIN EXTENDED on subquery with unknown column error]]>      </subject>
      <content>
<![CDATA[Kristian,

it looks like that MySQL Bug#37362 (http://bugs.mysql.com/bug.php?id=37362) is already fixed. Do we have this fix in MariaDB, too?

Thanks,

Hakan]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/430669/comments/2" datecreated="2010-06-08T07:44:56.819723+00:00">
      <person>Kristian Nielsen</person>
      <subject>
<![CDATA[Re: Wrong output of EXPLAIN EXTENDED on subquery with unknown column error]]>      </subject>
      <content>
<![CDATA[I think we have the a for MySQL Bug#37362, since the query does not crash the server.

But note that this bug is about a different problem with the query, and it is not fixed in latest 5.2.

MySQL 5.1 does not have this bug.]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/430669/comments/3" datecreated="2011-06-15T15:16:31.278897+00:00">
      <person>Sergey Petrunia</person>
      <subject>
<![CDATA[Re: Wrong output of EXPLAIN EXTENDED on subquery with unknown column error]]>      </subject>
      <content>
<![CDATA[I think that the importance of EXPLAIN/SHOW WARNINGs message for a query that produces error is very low.
]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/430669/comments/4" datecreated="2011-06-15T16:18:18.126195+00:00">
      <person>Sergey Petrunia</person>
      <subject>
<![CDATA[Re: Wrong output of EXPLAIN EXTENDED on subquery with unknown column error]]>      </subject>
      <content>
<![CDATA[Bug report says> The problem here is this output "group by `t1`.`c`". The column `c` is unknown at that place in the query, so it makes no sense to qualify it with the table name t1.

I don't think this is the case. Let's look at the query again:

01 EXPLAIN EXTENDED SELECT c 
02 FROM 
03 ( 
04    SELECT 
05       (SELECT COUNT(a) 
06        FROM 
07           (SELECT COUNT(b) FROM t1) AS x 
08        GROUP BY c)
09    FROM 
10      t1 
11    GROUP BY b 
12 ) AS y;

The "GROUP BY c" clause we're talking about is on line 8.
it belongs to the subquery that is located on lines 5-8
that subquery is a scalar-context subquery that is located in the select list of the subquery that is located on lines 4-11.
The subquery on lines 4-11 has a table named "t1", with column t1.c.
So, the "GROUP BY c" on line 8 refers to the t1.c of table t1 mentioned on line 10.

The "unknown column c" error is produced for the reference to column "c" made from line 1.  
]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/430669/comments/5" datecreated="2011-06-15T16:20:57.350775+00:00">
      <person>Sergey Petrunia</person>
      <subject>
<![CDATA[Re: Wrong output of EXPLAIN EXTENDED on subquery with unknown column error]]>      </subject>
      <content>
<![CDATA[Didn't see anything wrong while investigating the above in debugger. I don't think anything that we observe here qualifies as a bug, or something that we'd like to fix. 

Changing status to invalid.]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/430669/comments/6" datecreated="2011-06-16T05:44:21.240085+00:00">
      <person>Kristian Nielsen</person>
      <subject>
<![CDATA[Re: Wrong output of EXPLAIN EXTENDED on subquery with unknown column error]]>      </subject>
      <content>
<![CDATA[Agree, I don't understand why I reported this as a bug, it looks correct.
I must have been confused by :-/]]>      </content>
    </comment>
  </comments>
</launchpad-bug>
