Re: Cast jsonb to numeric, int, float, bool - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Cast jsonb to numeric, int, float, bool
Date
Msg-id 29252.1520866819@sss.pgh.pa.us
Whole thread Raw
In response to Re: Cast jsonb to numeric, int, float, bool  (Nikita Glukhov <n.gluhov@postgrespro.ru>)
Responses Re: Cast jsonb to numeric, int, float, bool  (Darafei "Komяpa" Praliaskouski <me@komzpa.net>)
List pgsql-hackers
Nikita Glukhov <n.gluhov@postgrespro.ru> writes:
> On 01.03.2018 11:19, Darafei "Komяpa" Praliaskouski wrote:
>> I would expect some casts to be implicit, so that chaining with other 
>> functions is possible:

> I think that only cast to a numeric type can be made implicit, because 
> it does not lose precision.

I hadn't been following this thread particularly, but I happened to notice
this bit, and I thought I'd better pop up to say No Way.  There will be
*no* implicit casts from json to any numeric type.  We have learned the
hard way that implicit cross-category casts are dangerous.  See the
advice in the CREATE CAST man page:

    It is wise to be conservative about marking casts as implicit. An
    overabundance of implicit casting paths can cause PostgreSQL to choose
    surprising interpretations of commands, or to be unable to resolve
    commands at all because there are multiple possible interpretations. A
    good rule of thumb is to make a cast implicitly invokable only for
    information-preserving transformations between types in the same
    general type category. For example, the cast from int2 to int4 can
    reasonably be implicit, but the cast from float8 to int4 should
    probably be assignment-only. Cross-type-category casts, such as text
    to int4, are best made explicit-only.


            regards, tom lane


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: [patch] BUG #15005: ANALYZE can make pg_class.reltuples inaccurate.
Next
From: Pavel Stehule
Date:
Subject: Re: Transform for pl/perl