Unix Technical Forum

BUG in Union Implementation??? Please confirm or explain if possible

This is a discussion on BUG in Union Implementation??? Please confirm or explain if possible within the MySQL forums, part of the Database Server Software category; --> Hi all, I believe to have found a bug in MySQL's union implementation. Can someone confirm this, please or ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:12 AM
CVH
 
Posts: n/a
Default BUG in Union Implementation??? Please confirm or explain if possible

Hi all,
I believe to have found a bug in MySQL's union implementation. Can someone
confirm this, please or convince me that this is not a buggy behaviour of
mysql :

UNION seems to behave like DISTINCT by default:

mysql> select 2 c1
-> union
-> select 1 c1
-> union
-> select 2 c1
-> union
-> select 1 c1;
+----+
| c1 |
+----+
| 2 |
| 1 |
+----+
2 rows in set (0.00 sec)

mysql> select 2 c1,1 union select 1 c1,2 union select 2 c1,3 union select 1
c1,4;
+----+---+
| c1 | 1 |
+----+---+
| 2 | 1 |
| 1 | 2 |
| 2 | 3 |
| 1 | 4 |
+----+---+
4 rows in set (0.00 sec)

mysql> select 2 c1,1 union select 1 c1,2 union select 2 c1,3 union select
1,2;
+----+---+
| c1 | 1 |
+----+---+
| 2 | 1 |
| 1 | 2 |
| 2 | 3 |
+----+---+
3 rows in set (0.00 sec)

mysql> select avg(c1),avg(distinct c1),sum(c1),count(c1),count(distinct
c1),count(*) from
-> (
-> select 2 c1
-> union
-> select 1 c1
-> union
-> select 1 c1
-> union
-> select 1
-> ) a
-> ;
+-------+----------------+-------+---------+-------------------+----------+
|avg(c1)|avg(distinct c1)|sum(c1)|count(c1)|count(distinct c1) | count(*) |
+-------+----------------+-------+---------+-------------------+----------+
|1.5000 | 1.5000 | 3 | 2 | 2 | 2 |
+-------+----------------+-------+---------+-------------------+----------+
1 row in set (0.00 sec)

but I would have expected:

+-------+----------------+-------+---------+-------------------+----------+
|avg(c1)|avg(distinct c1)|sum(c1)|count(c1)|count(distinct c1) | count(*) |
+-------+----------------+-------+---------+-------------------+----------+
|1.2500 | 1.5000 | 5 | 4 | 2 | 4 |
+-------+----------------+-------+---------+-------------------+----------+


TIA,

CVH


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:12 AM
Captain Paralytic
 
Posts: n/a
Default Re: BUG in Union Implementation??? Please confirm or explain if possible

On 11 Jul, 09:10, CVH <please_do_...@spam.me> wrote:
> Hi all,
> I believe to have found a bug in MySQL's union implementation. Can someone
> confirm this, please or convince me that this is not a buggy behaviour of
> mysql :
>
> UNION seems to behave like DISTINCT by default:
>
> mysql> select 2 c1
> -> union
> -> select 1 c1
> -> union
> -> select 2 c1
> -> union
> -> select 1 c1;
> +----+
> | c1 |
> +----+
> | 2 |
> | 1 |
> +----+
> 2 rows in set (0.00 sec)
>
> mysql> select 2 c1,1 union select 1 c1,2 union select 2 c1,3 union select 1
> c1,4;
> +----+---+
> | c1 | 1 |
> +----+---+
> | 2 | 1 |
> | 1 | 2 |
> | 2 | 3 |
> | 1 | 4 |
> +----+---+
> 4 rows in set (0.00 sec)
>
> mysql> select 2 c1,1 union select 1 c1,2 union select 2 c1,3 union select
> 1,2;
> +----+---+
> | c1 | 1 |
> +----+---+
> | 2 | 1 |
> | 1 | 2 |
> | 2 | 3 |
> +----+---+
> 3 rows in set (0.00 sec)
>
> mysql> select avg(c1),avg(distinct c1),sum(c1),count(c1),count(distinct
> c1),count(*) from
> -> (
> -> select 2 c1
> -> union
> -> select 1 c1
> -> union
> -> select 1 c1
> -> union
> -> select 1
> -> ) a
> -> ;
> +-------+----------------+-------+---------+-------------------+----------+
> |avg(c1)|avg(distinct c1)|sum(c1)|count(c1)|count(distinct c1) | count(*) |
> +-------+----------------+-------+---------+-------------------+----------+
> |1.5000 | 1.5000 | 3 | 2 | 2 | 2 |
> +-------+----------------+-------+---------+-------------------+----------+
> 1 row in set (0.00 sec)
>
> but I would have expected:
>
> +-------+----------------+-------+---------+-------------------+----------+
> |avg(c1)|avg(distinct c1)|sum(c1)|count(c1)|count(distinct c1) | count(*) |
> +-------+----------------+-------+---------+-------------------+----------+
> |1.2500 | 1.5000 | 5 | 4 | 2 | 4 |
> +-------+----------------+-------+---------+-------------------+----------+
>
> TIA,
>
> CVH


UNION always behaves like DISTINCT in any SQL implementation. That's
why there is also UNION ALL.

As the manual so clearly says (http://dev.mysql.com/doc/refman/5.0/en/
union.html)

"The default behavior for UNION is that duplicate rows are removed
from the result. The optional DISTINCT keyword has no effect other
than the default because it also specifies duplicate-row removal. With
the optional ALL keyword, duplicate-row removal does not occur and the
result includes all matching rows from all the SELECT statements."

Don't know how you missed that when you were reading that section
before you posted here!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:12 AM
CVH
 
Posts: n/a
Default Re: BUG in Union Implementation??? Please confirm or explain if possible

Captain Paralytic wrote:

> On 11 Jul, 09:10, CVH <please_do_...@spam.me> wrote:
>> Hi all,
>> I believe to have found a bug in MySQL's union implementation. Can
>> someone confirm this, please or convince me that this is not a buggy
>> behaviour of mysql :
>>
>> UNION seems to behave like DISTINCT by default:
>>
>> mysql> select 2 c1
>> -> union
>> -> select 1 c1
>> -> union
>> -> select 2 c1
>> -> union
>> -> select 1 c1;
>> +----+
>> | c1 |
>> +----+
>> | 2 |
>> | 1 |
>> +----+
>> 2 rows in set (0.00 sec)
>>
>> mysql> select 2 c1,1 union select 1 c1,2 union select 2 c1,3 union
>> select 1 c1,4;
>> +----+---+
>> | c1 | 1 |
>> +----+---+
>> | 2 | 1 |
>> | 1 | 2 |
>> | 2 | 3 |
>> | 1 | 4 |
>> +----+---+
>> 4 rows in set (0.00 sec)
>>
>> mysql> select 2 c1,1 union select 1 c1,2 union select 2 c1,3 union
>> select 1,2;
>> +----+---+
>> | c1 | 1 |
>> +----+---+
>> | 2 | 1 |
>> | 1 | 2 |
>> | 2 | 3 |
>> +----+---+
>> 3 rows in set (0.00 sec)
>>
>> mysql> select avg(c1),avg(distinct c1),sum(c1),count(c1),count(distinct
>> c1),count(*) from
>> -> (
>> -> select 2 c1
>> -> union
>> -> select 1 c1
>> -> union
>> -> select 1 c1
>> -> union
>> -> select 1
>> -> ) a
>> -> ;
>>

+-------+----------------+-------+---------+-------------------+----------+
>> |avg(c1)|avg(distinct c1)|sum(c1)|count(c1)|count(distinct c1) | count(*)
>> ||
>>

+-------+----------------+-------+---------+-------------------+----------+
>> |1.5000 | 1.5000 | 3 | 2 | 2 | 2
>> ||
>>

+-------+----------------+-------+---------+-------------------+----------+
>> 1 row in set (0.00 sec)
>>
>> but I would have expected:
>>
>>

+-------+----------------+-------+---------+-------------------+----------+
>> |avg(c1)|avg(distinct c1)|sum(c1)|count(c1)|count(distinct c1) | count(*)
>> ||
>>

+-------+----------------+-------+---------+-------------------+----------+
>> |1.2500 | 1.5000 | 5 | 4 | 2 | 4
>> ||
>>

+-------+----------------+-------+---------+-------------------+----------+
>>
>> TIA,
>>
>> CVH

>
> UNION always behaves like DISTINCT in any SQL implementation. That's
> why there is also UNION ALL.
>
> As the manual so clearly says (http://dev.mysql.com/doc/refman/5.0/en/
> union.html)
>
> "The default behavior for UNION is that duplicate rows are removed
> from the result. The optional DISTINCT keyword has no effect other
> than the default because it also specifies duplicate-row removal. With
> the optional ALL keyword, duplicate-row removal does not occur and the
> result includes all matching rows from all the SELECT statements."
>
> Don't know how you missed that when you were reading that section
> before you posted here!

Hmm, You're right. How could i have missed that.
Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:05 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com