Bug #81969 case when returns weird results when meets unsigned
Submitted: 22 Jun 2016 9:03 Modified: 24 Dec 2019 13:45
Reporter: 帅 Bang Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:5.6, 5.6.31 OS:Linux
Assigned to: CPU Architecture:Any

[22 Jun 2016 9:03] 帅 Bang
Description:
mysql> drop table t1;
mysql> create table t1(a bigint unsigned);
mysql> insert into t1 values (18446744073709551615);
mysql> select case when true then a end from t1;
+---------------------------+
| case when true then a end |
+---------------------------+
|      18446744073709551615 |
+---------------------------+
1 row in set (0.00 sec)

it is good for now, but when we type this :

mysql> select case when true then a else null end from t1;
+-------------------------------------+
| case when true then a else null end |
+-------------------------------------+
|                                  -1 |
+-------------------------------------+

so weird... let us digg into this. 

connect the mysql server with --column-type-info option in client and check the metainfo

mysql> select case when true then a end from t1;
Field   1:  `case when true then a end`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     20
Max_length: 20
Decimals:   0
Flags:      UNSIGNED BINARY NUM  //yeah, unsigned flag

+---------------------------+
| case when true then a end |
+---------------------------+
|      18446744073709551615 |
+---------------------------+
1 row in set (0.00 sec)

mysql> select case when true then a else null end from t1;
Field   1:  `case when true then a else null end`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     21
Max_length: 2
Decimals:   0
Flags:      BINARY NUM   //no unsigned flag

+-------------------------------------+
| case when true then a else null end |
+-------------------------------------+
|                                  -1 |
+-------------------------------------+
1 row in set (0.01 sec)

How to repeat:
drop table t1; 
create table t1(a bigint unsigned);
insert into t1 values (18446744073709551615);
select case when true then a end from t1;
select case when true then a else null end from t1;

Suggested fix:
select case when true then a end from t1; 

select case when true then a else null end from t1; 

both return same results
[24 Jun 2016 8:59] MySQL Verification Team
Hello Bang,

Thank you for the report and test case.

Thanks,
Umesh
[4 Aug 2016 9:03] Srinivasarao Ravuri
Posted by developer:
 
This bug is re-opened as the issue is not fixed.
[24 Dec 2019 13:45] Roy Lyseng
Posted by developer:
 
Fixed in 5.7.13