1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-20 05:03:10 +03:00

Add casts from jsonb

Add explicit cast from scalar jsonb to all numeric and bool types. It would be
better to have cast from scalar jsonb to text too but there is already a cast
from jsonb to text as just text representation of json. There is no way to have
two different casts for the same type's pair.

Bump catalog version

Author: Anastasia Lubennikova with editorization by Nikita Glukhov and me
Review by: Aleksander Alekseev, Nikita Glukhov, Darafei Praliaskouski
Discussion: https://www.postgresql.org/message-id/flat/0154d35a-24ae-f063-5273-9ffcdf1c7f2e@postgrespro.ru
This commit is contained in:
Teodor Sigaev
2018-03-29 16:33:56 +03:00
parent 7fe04ce920
commit c0cbe00fee
6 changed files with 328 additions and 1 deletions

View File

@ -4191,3 +4191,108 @@ select ts_headline('[]'::jsonb, tsquery('aaa & bbb'));
[]
(1 row)
-- casts
select 'true'::jsonb::bool;
bool
------
t
(1 row)
select '[]'::jsonb::bool;
ERROR: jsonb value must be boolean
select '1.0'::jsonb::float;
float8
--------
1
(1 row)
select '[1.0]'::jsonb::float;
ERROR: jsonb value must be numeric
select '12345'::jsonb::int4;
int4
-------
12345
(1 row)
select '"hello"'::jsonb::int4;
ERROR: jsonb value must be numeric
select '12345'::jsonb::numeric;
numeric
---------
12345
(1 row)
select '{}'::jsonb::numeric;
ERROR: jsonb value must be numeric
select '12345.05'::jsonb::numeric;
numeric
----------
12345.05
(1 row)
select '12345.05'::jsonb::float4;
float4
--------
12345
(1 row)
select '12345.05'::jsonb::float8;
float8
----------
12345.05
(1 row)
select '12345.05'::jsonb::int2;
int2
-------
12345
(1 row)
select '12345.05'::jsonb::int4;
int4
-------
12345
(1 row)
select '12345.05'::jsonb::int8;
int8
-------
12345
(1 row)
select '12345.0000000000000000000000000000000000000000000005'::jsonb::numeric;
numeric
------------------------------------------------------
12345.0000000000000000000000000000000000000000000005
(1 row)
select '12345.0000000000000000000000000000000000000000000005'::jsonb::float4;
float4
--------
12345
(1 row)
select '12345.0000000000000000000000000000000000000000000005'::jsonb::float8;
float8
--------
12345
(1 row)
select '12345.0000000000000000000000000000000000000000000005'::jsonb::int2;
int2
-------
12345
(1 row)
select '12345.0000000000000000000000000000000000000000000005'::jsonb::int4;
int4
-------
12345
(1 row)
select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8;
int8
-------
12345
(1 row)

View File

@ -1105,3 +1105,25 @@ select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1":
select ts_headline('null'::jsonb, tsquery('aaa & bbb'));
select ts_headline('{}'::jsonb, tsquery('aaa & bbb'));
select ts_headline('[]'::jsonb, tsquery('aaa & bbb'));
-- casts
select 'true'::jsonb::bool;
select '[]'::jsonb::bool;
select '1.0'::jsonb::float;
select '[1.0]'::jsonb::float;
select '12345'::jsonb::int4;
select '"hello"'::jsonb::int4;
select '12345'::jsonb::numeric;
select '{}'::jsonb::numeric;
select '12345.05'::jsonb::numeric;
select '12345.05'::jsonb::float4;
select '12345.05'::jsonb::float8;
select '12345.05'::jsonb::int2;
select '12345.05'::jsonb::int4;
select '12345.05'::jsonb::int8;
select '12345.0000000000000000000000000000000000000000000005'::jsonb::numeric;
select '12345.0000000000000000000000000000000000000000000005'::jsonb::float4;
select '12345.0000000000000000000000000000000000000000000005'::jsonb::float8;
select '12345.0000000000000000000000000000000000000000000005'::jsonb::int2;
select '12345.0000000000000000000000000000000000000000000005'::jsonb::int4;
select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8;