mysqlsqlmariadbifnull

Why ifnull() return DECIMAL instead of BIGINT in MariaDB 10.1+?


Why ifnull() return DECIMAL instead of BIGINT in MariaDB 10.1?

For example:

Query:

select a, ifnull(b, 1) from table;

10.0.22-MariaDB:

ifnull(b, 1) type is BIGINT

10.1.37-MariaDB:

ifnull(b, 1) type is DECIMAL

Moreover, in both versions the return type of this query is the same:

select 1; //type is BIGINT

Why ifnull() convert BIGINT to DECIMAL?


Solution

  • I can't reproduce the problem, see the example:

    MariaDB [test]> SELECT VERSION();
    Field   1:  `VERSION()`
    Catalog:    `def`
    Database:   ``
    Table:      ``
    Org_table:  ``
    Type:       VAR_STRING
    Collation:  utf8_general_ci (33)
    Length:     72
    Max_length: 24
    Decimals:   31
    Flags:      NOT_NULL 
    
    +-----------------+
    | VERSION()       |
    +-----------------+
    | 10.1.38-MariaDB |
    +-----------------+
    1 row in set (0.00 sec)
    
    MariaDB [test]> DROP TABLE IF EXISTS `test`;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [test]> CREATE TABLE IF NOT EXISTS `test` (
        ->   `bigint` BIGINT,
        ->   `decimal` DECIMAL(5, 2)
        -> );
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [test]> INSERT INTO `test`
        ->   (`bigint`, `decimal`)
        -> VALUES
        ->   (NULL, NULL);
    Query OK, 1 row affected (0.00 sec)
    
    MariaDB [test]> SELECT 1;
    Field   1:  `1`
    Catalog:    `def`
    Database:   ``
    Table:      ``
    Org_table:  ``
    Type:       LONGLONG
    Collation:  binary (63)
    Length:     1
    Max_length: 1
    Decimals:   0
    Flags:      NOT_NULL BINARY NUM 
    
    +---+
    | 1 |
    +---+
    | 1 |
    +---+
    1 row in set (0.00 sec)
    
    MariaDB [test]> SELECT
        ->   IFNULL(`bigint`, 1) `bigint`,
        ->   IFNULL(`decimal`, 1) `decimal`
        -> FROM
        ->   `test`;
    Field   1:  `bigint`
    Catalog:    `def`
    Database:   ``
    Table:      ``
    Org_table:  ``
    Type:       LONGLONG
    Collation:  binary (63)
    Length:     20
    Max_length: 1
    Decimals:   0
    Flags:      NOT_NULL BINARY NUM 
    
    Field   2:  `decimal`
    Catalog:    `def`
    Database:   ``
    Table:      ``
    Org_table:  ``
    Type:       NEWDECIMAL
    Collation:  binary (63)
    Length:     7
    Max_length: 4
    Decimals:   2
    Flags:      NOT_NULL BINARY NUM 
    
    +--------+---------+
    | bigint | decimal |
    +--------+---------+
    |      1 |    1.00 |
    +--------+---------+
    1 row in set (0.00 sec)