metadata formatString not honored when number is generated with ifnull()

LabKey Support Forum (Inactive)
metadata formatString not honored when number is generated with ifnull() Will Holtz  2016-03-02 13:54
Status: Closed
 
I have a query:
SELECT
    0.111111111111111 AS a,
    ifnull(0.111111111111111, 1) AS b,
    CAST(ifnull(0.111111111111111, 1) AS NUMERIC) AS c;

and metadata for the query:
<?xml version="1.0" encoding="UTF-8"?>

<query xmlns="http://labkey.org/data/xml/query">
    <metadata>
        <tables xmlns="http://labkey.org/data/xml">
            <table tableName="TestFormat" tableDbType="NOT_IN_DB">
                <columns>
                    <column columnName="a">
                        <formatString>##0.####E0</formatString>
                    </column>
                    <column columnName="b">
                        <formatString>##0.####E0</formatString>
                    </column>
                    <column columnName="c">
                        <formatString>##0.####E0</formatString>
                    </column>
                </columns>
            </table>
        </tables>
    </metadata>
</query>

The output I get by viewing the data in the schema browser is:
A B C
111.1111E-3    0.111111111111111    111.1111E-3

I was expecting to get:
A B C
111.1111E-3    111.1111E-3    111.1111E-3

This is using postgres 9.5 with labkey revision 41716. I couldn't find any detailed documentation on ifnull() -- perhaps my expectations are just off. I can easily add a CAST to my query, so I'm no longer impacted by this.

-Will
 
 
Jon (LabKey DevOps) responded:  2016-03-02 15:18
Hi Will,

There are two problems that are going on.

1. IFNULL() doesn't have to know a return type.

2. If the types of arguments are different, we have to guess the return type.

Currently, we are not even guessing at a type when it comes to IFNULL(), but it is something that it worth for us to explore enhancing in the future.

But in the meantime, since you're using Postgres, can you give coalesce() a try instead of IFNULL()? coalesce() does actually guess at the return type is the same as the first parameter. It's not perfect, but it is better than no guessing like IFNULL() does.

Regards,

Jon
 
Will Holtz responded:  2016-03-02 15:35
Hi Jon,

Replacing the IFNULL() statements with COALESCE() did work. Thanks!

-Will