Bug #107653 Union results auto-truncate with zerofill fields
Submitted: 25 Jun 2022 9:33 Modified: 25 Jun 2022 12:27
Reporter: yi qian Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Data Types Severity:S1 (Critical)
Version:8.0.25 OS:Any
Assigned to: CPU Architecture:Any
Tags: union; zerofill

[25 Jun 2022 9:33] yi qian
Description:
When the fields in the table have the zerofill attribute, the union result will auto-truncate zerofill fields, which not what we expected.

How to repeat:
1、prepare data:
mysql> create table t1(a BIGINT(5) ZEROFILL);
Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> show warnings;
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                   |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1681 | The ZEROFILL attribute is deprecated and will be removed in a future release. Use the LPAD function to zero-pad numbers, or store the formatted numbers in a CHAR column. |
| Warning | 1681 | Integer display width is deprecated and will be removed in a future release.                                                                                              |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> insert into t1 values(0);
Query OK, 1 row affected (0.01 sec)

mysql> select a from t1 where ifnull(t1.a,1)=0 and t1.a=0;
+-------+
| a     |
+-------+
| 00000 |
+-------+
1 row in set (0.01 sec)

2、Abnormal condition:
When we make the above result a union operation, the returned result will automatically truncate the zerofill field. As follows:

mysql> select a from t1 where ifnull(t1.a,1)=0 and t1.a=0 union select 1;
+------+
| a    |
+------+
|    0 |
|    1 |
+------+
2 rows in set (0.00 sec)
[25 Jun 2022 12:27] MySQL Verification Team
Hello yi qian,

Thank you for the report and test case.
Imho this is a known and documented behavior. Quoting from manual "The ZEROFILL attribute is ignored for columns involved in expressions or UNION queries. ", please see here https://dev.mysql.com/doc/refman/8.0/en/numeric-type-attributes.html

regards,
Umesh