From: | Corey Huinker <corey(dot)huinker(at)gmail(dot)com> |
---|---|
To: | Andrew Dunstan <andrew(at)dunslane(dot)net> |
Cc: | Wolfgang Walther <walther(at)technowledgy(dot)de>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: Suggestion: optionally return default value instead of error on failed cast |
Date: | 2022-01-06 18:02:19 |
Message-ID: | CADkLM=cUWFzdX4NGAbMKKKo9U2-eELg5WuQVY21x94KRThrrag@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
On Thu, Jan 6, 2022 at 12:18 PM Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>
> On 1/4/22 22:17, Corey Huinker wrote:
> >
> > currently a failed cast throws an error. It would be useful to have a
> > way to get a default value instead.
> >
> >
> > I've recently encountered situations where this would have been
> > helpful. Recently I came across some client code:
> >
> > CREATE OR REPLACE FUNCTION is_valid_json(str text) RETURNS boolean
> > LANGUAGE PLPGSQL
> > AS $$
> > DECLARE
> > j json;
> > BEGIN
> > j := str::json;
> > return true;
> > EXCEPTION WHEN OTHERS THEN return false;
> > END
> > $$;
> >
> >
> > This is a double-bummer. First, the function discards the value so we
> > have to recompute it, and secondly, the exception block prevents the
> > query from being parallelized.
>
>
> This particular case is catered for in the SQL/JSON patches which
> several people are currently reviewing:
>
>
That's great to know, but it would still be parsing the json twice, once to
learn that it is legit json, and once to get the casted value.
Also, I had a similar issue with type numeric, so having generic "x is a
type_y" support would essentially do everything that a try_catch()-ish
construct would need to do, and be more generic.
From | Date | Subject | |
---|---|---|---|
Next Message | Pavel Stehule | 2022-01-06 18:03:47 | Re: pl/pgsql feature request: shorthand for argument and local variable references |
Previous Message | Justin Pryzby | 2022-01-06 17:26:34 | Re: Emit "checkpoint skipped because system is idle" message at LOG level if log_checkpoints is set |