From: | Teodor Sigaev <teodor(at)sigaev(dot)ru> |
---|---|
To: | Robert Haas <robertmhaas(at)gmail(dot)com> |
Cc: | Aleksander Alekseev <a(dot)alekseev(at)postgrespro(dot)ru>, David Steele <david(at)pgmasters(dot)net>, Nikita Glukhov <n(dot)gluhov(at)postgrespro(dot)ru>, Darafei Praliaskouski <me(at)komzpa(dot)net>, Anastasia Lubennikova <a(dot)lubennikova(at)postgrespro(dot)ru>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
Subject: | Re: Cast jsonb to numeric, int, float, bool |
Date: | 2018-03-30 12:28:40 |
Message-ID: | 0250b45d-4a38-5453-cb5a-8fa9883da36b@sigaev.ru |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
> I would like to complain about this patch. First, I think that it
> would've been a better idea to use functions for this rather than
> operators, because now ::text does something totally unlike what ::int
> does, and that's confusing. If we had json_to_WHATEVER for various
> values of WHATEVER then all of the conversions could be spelled
> similarly; as the commit message right points out, the cast can only
> do one thing.
From another point of view, casting jsonb to text produces completely grounded
result: we get a text correctly formatted as json. Other casts produce correct
json but with non-text type.
Casting jsonb with text is two-way casting:
# select '123'::jsonb::text::jsonb, '"xxx"'::jsonb::text::jsonb;
jsonb | jsonb
-------+-------
123 | "xxx"
But casting with numeric types and bool is not, but it could be done with
intermediate cast to text (uppercase cast):
# select '123'::jsonb::int::TEXT::jsonb;
jsonb
-------
123
For completeness it's possible to add direct cast from numeric/boolean types to
jsonb. Then such casts will be mutual.
> Also, I think the error messages aren't great:
>
> +select '[]'::jsonb::bool;
> +ERROR: jsonb value must be boolean
>
> In this simple scenario, it's clear enough what has gone wrong, but in
> a more complicated case I suspect people will have a hard time
> figuring out what the source of that error message is. It seems like
> it would be better to say something about casting or converting in the
> error message, to give users a clue.
Agree, something like "could not convert jsonb value to boolean type. jsonb
value must be scalar boolean type"?
--
Teodor Sigaev E-mail: teodor(at)sigaev(dot)ru
WWW: http://www.sigaev.ru/
From | Date | Subject | |
---|---|---|---|
Next Message | Konstantin Knizhnik | 2018-03-30 12:53:39 | libpq compression |
Previous Message | Marina Polyakova | 2018-03-30 12:20:16 | Re: [HACKERS] WIP Patch: Pgbench Serialization and deadlock errors |