Bug #28782 Alias with union all & order by & subquery crashes session
Submitted: 30 May 2007 17:44 Modified: 31 May 2007 0:49
Reporter: Igor Monakhov Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: General Severity:S2 (Serious)
Version:5.0.37-max-log OS:Linux (MySQL-Max-5.0.37-0.glibc23.i386.rpm)
Assigned to: CPU Architecture:Any

[30 May 2007 17:44] Igor Monakhov
Description:
Session crashes after adding alias into query.

[root@home log]# uname -a
Linux home.lstel.net 2.6.20-1.2948.fc6 #1 SMP Fri Apr 27 19:48:40 EDT 2007 i686 i686 i386 GNU/Linux
[root@home log]# rpm -q fedora-release
fedora-release-6-4

070531  1:32:02 [Note] /usr/sbin/mysqld-max: ready for connections.
Version: '5.0.37-max-log'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Edition - Experimental (GPL)
mysqld got signal 11;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
We will try our best to scrape up some info that will hopefully help diagnose
the problem, but since we have already crashed, something is definitely wrong
and this may fail.

key_buffer_size=8388600
read_buffer_size=131072
max_used_connections=4
max_connections=100
threads_connected=1
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_connections = 225791 K
bytes of memory
Hope that's ok; if not, decrease some variables in the equation.

thd=0x946d078
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
Cannot determine thread, fp=0xb089a0d8, backtrace may not be correct.
Stack range sanity check OK, backtrace follows:
0x819fc00
0x812b1ed
0x812bc72
0x81fca91
0x81fcd2d
0x81e4b00
0x81e8a0d
0x82a80eb
0x82a8e00
0x82a8a14
0x81d9ce7
0x81b6709
0x81bdaf2
0x81b4ccf
0x81b4714
0x81b3b32
0x6a13db
0x5fb26e
New value of fp=(nil) failed sanity check, terminating stack trace!
Please read http://dev.mysql.com/doc/mysql/en/using-stack-trace.html and follow instructions on how to resolve the stack trace. Resolved
stack trace is much more helpful in diagnosing the problem, so please do
resolve it
Trying to get some variables.
Some pointers may be invalid and cause the dump to abort...
thd->query at 0x94a0fb0 = select * from (select id, val1 CRASH_ALIAS from x union all select id, val2 from x order by val1) base
thd->thread_id=6
The manual page at http://www.mysql.com/doc/en/Crashing.html contains
information that should help you find out what is causing the crash.

Number of processes running now: 0

How to repeat:
mysql> use test
Database changed
mysql> drop table if exists x;
Query OK, 0 rows affected (0.00 sec)

mysql> create table x (id int, val1 int, val2 int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into x values (0,1,2),(1,2,3);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from (select id, val1 from x union all select id, val2 from x order by val1) base;
+------+------+
| id   | val1 |
+------+------+
|    0 |    1 |
|    1 |    2 |
|    0 |    2 |
|    1 |    3 |
+------+------+
4 rows in set (0.01 sec)

mysql> select * from (select id, val1 CRASH_ALIAS from x union all select id, val2 from x order by val1) base;
ERROR 2013 (HY000): Lost connection to MySQL server during query

Suggested fix:
Use properly alias, this query should be:

mysql> select * from (select id, val1 CRASH_ALIAS from x union all select id, val2 from x order by CRASH_ALIAS) base;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    1
Current database: test

+------+-------------+
| id   | CRASH_ALIAS |
+------+-------------+
|    0 |           1 |
|    1 |           2 |
|    0 |           2 |
|    1 |           3 |
+------+-------------+
4 rows in set (0.01 sec)

mysql>
[30 May 2007 18:18] Valeriy Kravchuk
Thank you for a problem report. Sorry, but I was not able to repeat the crash with latest 5.0.44-BK on Linux:

openxs@suse:~/dbs/5.0> bin/mysql -uroot test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.44-debug Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> drop table if exists x;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table x (id int, val1 int, val2 int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into x values (0,1,2),(1,2,3);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from (select id, val1 from x union all select id, val2 from x
    -> order by val1) base;
+------+------+
| id   | val1 |
+------+------+
|    0 |    1 |
|    1 |    2 |
|    0 |    2 |
|    1 |    3 |
+------+------+
4 rows in set (0.03 sec)

mysql> select * from (select id, val1 CRASH_ALIAS from x union all select id,
    -> val2 from x order by CRASH_ALIAS) base;
+------+-------------+
| id   | CRASH_ALIAS |
+------+-------------+
|    0 |           1 |
|    1 |           2 |
|    0 |           2 |
|    1 |           3 |
+------+-------------+
4 rows in set (0.01 sec)
[31 May 2007 0:49] Igor Monakhov
You've read unwell:

select * from (select id, val1 from x union all select id, val2 from x order by val1) base; -- ok
select * from (select id, val1 CRASH_ALIAS from x union all select id, val2 from x order by CRASH_ALIAS) base; -- ok
select * from (select id, val1 CRASH_ALIAS from x union all select id, val2 from x order by val1) base; -- CRASH!
[31 May 2007 5:21] Valeriy Kravchuk
Still no crash - just proper error message:

openxs@suse:~/dbs/5.0> bin/mysql -uroot test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.44-debug Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> select * from (select id, val1 CRASH_ALIAS from x union all select id, v
al2 from x order by CRASH_ALIAS) base;
+------+-------------+
| id   | CRASH_ALIAS |
+------+-------------+
|    0 |           1 |
|    1 |           2 |
|    0 |           2 |
|    1 |           3 |
+------+-------------+
4 rows in set (0.01 sec)

mysql> select * from (select id, val1 CRASH_ALIAS from x union all select id, v
al2 from x order by val1) base;
ERROR 1054 (42S22): Unknown column 'val1' in 'order clause'