Re: Cast jsonb to numeric, int, float, bool

From: Aleksander Alekseev <a(dot)alekseev(at)postgrespro(dot)ru>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Teodor Sigaev <teodor(at)sigaev(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:05:52
Message-ID: 20180330120552.GB8526@e733.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello Robert,

> 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.

This is not entirely accurate. ::text casts any jsonb to text, e.g:

```
select '{}'::jsonb::text;
text
------
{}
(1 row)
```

This is exactly what ::bool and ::numeric casts do. Naturally not every
jsonb value can be represented as boolean or numeric though.

> 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.

I believe it's not as convenient for users as having casts. Also this
would be inconsistent with the rest of the system since we already have
int4 -> text, int4 -> bool and various other casts.

> 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.

I agree. How about "unable to cast jsonb value to %typename%"?

--
Best regards,
Aleksander Alekseev

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Konstantin Knizhnik 2018-03-30 12:12:05 Re: JIT compiling with LLVM v12.2
Previous Message Stephen Frost 2018-03-30 11:46:02 Re: [PATCH] Verify Checksums during Basebackups