PostgreSQL 11の次期リリースの新機能のトピック(前の投稿: one 、 two 、 three )を続けて、1つの小さな、しかし重要な変更についてお話したいと思います。 これを変更すると、あるタイプから別のタイプへのキャストに適用されます。 そしてもちろん、PostgresProでは彼をとても愛しているので、それはJSONBに関係しています!
これはパッチc0cbe00fについてです :
commit c0cbe00fee6d0a5e0ec72c6d68a035e674edc4cc
Author: Teodor Sigaev <teodor@sigaev.ru>
Date: Thu Mar 29 16:33:56 2018 +0300
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
問題の本質はこれです。 PostgreSQL 10を使用する場合、次の動作があります。
=# select 'true' :: jsonb :: bool;
ERROR: cannot cast type jsonb to boolean
LINE 1: select 'true' :: jsonb :: bool;
=# select '12345' :: jsonb :: int4;
ERROR: cannot cast type jsonb to integer
LINE 1: select '12345' :: jsonb :: int4;
=# select '12345.67' :: jsonb :: float8;
ERROR: cannot cast type jsonb to double precision
LINE 1: select '12345.67' :: jsonb :: float8;
つまり、JSONBは数値型およびブール値にキャストされません。 もちろん、それが非常に大きな問題だったわけではありません。 最終的に、いつでもテキストをキャストできます。
=# select '12345.67' :: jsonb :: text :: float8;
float8
----------
12345.67
それにもかかわらず、そのような解決策は松葉杖のように見え、その性能について疑問があります。
次に、masterブランチの動作を見てみましょう。
=# select 'true' :: jsonb :: bool;
bool
------
t
=# select '12345' :: jsonb :: int4;
int4
-------
12345
=# select '12345.67' :: jsonb :: float8;
float8
----------
12345.67
ご覧のとおり、JSONBをブール型と数値型にキャストできます。 やった!
特徴的に、反対方向へのキャストは、テキストの中間キャストを介してのみ可能です。
=# select 12345.67 :: jsonb;
ERROR: cannot cast type numeric to jsonb
LINE 1: select 12345.67 :: jsonb;
=# select 12345.67 :: text :: jsonb;
jsonb
----------
12345.67
(1 row)
なぜ直接キャストがないのですか? 私はまだ誰もそれを終えるために彼の手を得ていないと思います。 自己実現の機会のようですね。 ;)