Se eu fizer CAST(1 AS SIGNED INTEGER), sempre acabo recebendo um BIGINTretorno, por exemplo:
$ mysql -u root -p --column-type-info
Enter password:
--- Copyright and help message snipped for brevity ---
mysql> select cast(1 as signed integer);
Field 1: `cast(1 as signed integer)`
Catalog: `def`
Database: ``
Table: ``
Org_table: ``
Type: LONGLONG <== LONGLONG i.e. 64 bit integer
Collation: binary (63)
Length: 1
Max_length: 1
Decimals: 0
Flags: NOT_NULL BINARY NUM
+---------------------------+
| cast(1 as signed integer) |
+---------------------------+
| 1 |
+---------------------------+
1 row in set (0.00 sec)
Eu esperava que o tipo de retorno desse elenco fosse um LONG(inteiro de 32 bits).
Se eu selecionar uma coluna de uma tabela que possui um INT, vejo que é realmente apenas um LONG:
mysql> describe contact;
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| contact_id | int(11) | NO | PRI | NULL | auto_increment |
== remainder of table snipped ==
mysql> select contact_id from contact where contact_id = 20;
Field 1: `contact_id`
Catalog: `def`
Database: `centreon`
Table: `contact`
Org_table: `contact`
Type: LONG <== LONG i.e. 32 bit integer
Collation: binary (63)
Length: 11
Max_length: 2
Decimals: 0
Flags: NOT_NULL PRI_KEY AUTO_INCREMENT NUM PART_KEY
+------------+
| contact_id |
+------------+
| 20 |
+------------+
1 row in set (0.00 sec)
mysql>
Se eu converter a mesma coluna em um número inteiro assinado, recebo novamente um número inteiro de 64 bits:
mysql> select CAST(contact_id as signed integer) from contact where contact_id = 20;
Field 1: `CAST(contact_id as signed integer)`
Catalog: `def`
Database: ``
Table: ``
Org_table: ``
Type: LONGLONG
Collation: binary (63)
Length: 11
Max_length: 2
Decimals: 0
Flags: NOT_NULL BINARY NUM
+------------------------------------+
| CAST(contact_id as signed integer) |
+------------------------------------+
| 20 |
+------------------------------------+
1 row in set (0.00 sec)
Há um problema relatado de maneira semelhante aqui:
Mas, infelizmente, o OP não recebe uma resposta direta.
Isso é um bug na CAST()função ou é por design?
SIGNED [INTEGER]na seção O tipo para o resultado pode ser um dos seguintes valores: . A é SIGNED INTEGERno contexto de um CASTnão de fato um número inteiro de 32 bits?
SELECT 1+1resultados em um BIGINT. Mas ainda não explica por que CAST()se comporta de maneira contrária à documentação (como eu a entendo) e produz um BIGINTmesmo que seja solicitado a converter em SIGNED INTEGERou UNSIGNED INTEGERcom um único valor escalar.