pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedWhy does jsonb_set() remove non-mentioned keys?
9+ messages / 4 participants
[nested] [flat]
* Why does jsonb_set() remove non-mentioned keys?
@ 2019-07-04 13:18 Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 16:20 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Thomas Kellerer @ 2019-07-04 13:18 UTC (permalink / raw)
To: pgsql-general
Why does
select jsonb_set('{"foo": 1}'::jsonb, '{bar}', to_jsonb(null::int), true)
return NULL when all it should do is to add a second key?
I would expect {"foo": 1, "bar": null} or no change at all to the original JSON value, but not that the whole JSON is set to null.
In the original case the new value to be set was the result of an expression, not a "hardcoded" null value.
Thomas
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Why does jsonb_set() remove non-mentioned keys?
2019-07-04 13:18 Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
@ 2019-07-04 16:20 ` David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 20:33 ` Re: Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
0 siblings, 1 reply; 9+ messages in thread
From: David G. Johnston @ 2019-07-04 16:20 UTC (permalink / raw)
To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-general
On Thu, Jul 4, 2019 at 6:18 AM Thomas Kellerer <spam_eater@gmx.net> wrote:
> Why does
>
> select jsonb_set('{"foo": 1}'::jsonb, '{bar}', to_jsonb(null::int),
> true)
>
> return NULL when all it should do is to add a second key?
>
Both functions involved are defined as being STRICT (null on null input).
You need to COALSESCE SQL NULL to JSON 'null' to obtain the result you
desire. This is a general truth when dealing with the JSON type in
PostgreSQL.
select jsonb_set('{"foo": 1}'::jsonb, '{bar}',
coalesce(to_jsonb(null::int), 'null'), true)
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Why does jsonb_set() remove non-mentioned keys?
2019-07-04 13:18 Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 16:20 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
@ 2019-07-04 20:33 ` Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 21:03 ` Re: Why does jsonb_set() remove non-mentioned keys? Gianni Ceccarelli <dakkar@thenautilus.net>
0 siblings, 1 reply; 9+ messages in thread
From: Thomas Kellerer @ 2019-07-04 20:33 UTC (permalink / raw)
To: pgsql-general
David G. Johnston schrieb am 04.07.2019 um 18:20:
> On Thu, Jul 4, 2019 at 6:18 AM Thomas Kellerer <spam_eater@gmx.net <mailto:spam_eater@gmx.net>> wrote:
>
> Why does
>
> select jsonb_set('{"foo": 1}'::jsonb, '{bar}', to_jsonb(null::int), true)
>
> return NULL when all it should do is to add a second key?
>
>
> Both functions involved are defined as being STRICT (null on null
> input). You need to COALSESCE SQL NULL to JSON 'null' to obtain the
> result you desire. This is a general truth when dealing with the
> JSON type in PostgreSQL.
But jsonb_set() doesn't change (or shouldn't) the whole value, only one key.
I can understand that the "bar" key would not be set (because of the NULL), but removing a key that isn't even part of the target path looks like a bug to.
Thomas
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Why does jsonb_set() remove non-mentioned keys?
2019-07-04 13:18 Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 16:20 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 20:33 ` Re: Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
@ 2019-07-04 21:03 ` Gianni Ceccarelli <dakkar@thenautilus.net>
2019-07-04 21:09 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Gianni Ceccarelli @ 2019-07-04 21:03 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Some experimentation:
> \pset null '((null))'
> select jsonb_set('{"foo":1}'::jsonb,'{bar}','null'::jsonb,true);
┌─────────────────────────┐
│ jsonb_set │
├─────────────────────────┤
│ {"bar": null, "foo": 1} │
└─────────────────────────┘
> select jsonb_set('{"foo":1}'::jsonb,'{bar}',to_jsonb(null::text),true);
┌───────────┐
│ jsonb_set │
├───────────┤
│ ((null)) │
└───────────┘
That's a bit weird already. Also:
> select null::jsonb;
┌──────────┐
│ jsonb │
├──────────┤
│ ((null)) │
└──────────┘
> select 'null'::jsonb;
┌───────┐
│ jsonb │
├───────┤
│ null │
└───────┘
> select to_jsonb(null::int);
┌──────────┐
│ to_jsonb │
├──────────┤
│ ((null)) │
└──────────┘
> select to_jsonb('null'::text);
┌──────────┐
│ to_jsonb │
├──────────┤
│ "null" │
└──────────┘
I'm sharing Thomas's confusion…
--
Dakkar - <Mobilis in mobile>
GPG public key fingerprint = A071 E618 DD2C 5901 9574
6FE2 40EA 9883 7519 3F88
key id = 0x75193F88
Work continues in this area.
-- DEC's SPR-Answering-Automaton
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Why does jsonb_set() remove non-mentioned keys?
2019-07-04 13:18 Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 16:20 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 20:33 ` Re: Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 21:03 ` Re: Why does jsonb_set() remove non-mentioned keys? Gianni Ceccarelli <dakkar@thenautilus.net>
@ 2019-07-04 21:09 ` David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 21:42 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
2019-07-05 08:00 ` Re: Why does jsonb_set() remove non-mentioned keys? Gianni Ceccarelli <dakkar@thenautilus.net>
0 siblings, 2 replies; 9+ messages in thread
From: David G. Johnston @ 2019-07-04 21:09 UTC (permalink / raw)
To: Gianni Ceccarelli <dakkar@thenautilus.net>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thursday, July 4, 2019, Gianni Ceccarelli <dakkar@thenautilus.net> wrote:
> Some experimentation:
>
> > \pset null '((null))'
>
> > select jsonb_set('{"foo":1}'::jsonb,'{bar}','null'::jsonb,true);
> ┌─────────────────────────┐
> │ jsonb_set │
> ├─────────────────────────┤
> │ {"bar": null, "foo": 1} │
> └─────────────────────────┘
No SQL null, ok
>
>
> > select jsonb_set('{"foo":1}'::jsonb,'{bar}',to_jsonb(null::text),true);
> ┌───────────┐
> │ jsonb_set │
> ├───────────┤
> │ ((null)) │
> └───────────┘
Sql null poisons the expression and so sql null is the result
>
> That's a bit weird already. Also:
>
> > select null::jsonb;
> ┌──────────┐
> │ jsonb │
> ├──────────┤
> │ ((null)) │
> └──────────┘
>
>
Sql null
> > select 'null'::jsonb;
> ┌───────┐
> │ jsonb │
> ├───────┤
> │ null │
> └───────┘
>
>
Json null
> > select to_jsonb(null::int);
> ┌──────────┐
> │ to_jsonb │
> ├──────────┤
> │ ((null)) │
> └──────────┘
>
>
Sql null poisons the function call which immediately returns sql null
> > select to_jsonb('null'::text);
> ┌──────────┐
> │ to_jsonb │
> ├──────────┤
> │ "null" │
> └──────────┘
>
>
Json null
> I'm sharing Thomas's confusion…
>
>
Sql null and json null are represented differently; strict functions with
sql null inputs yield sql null output without even executing the function
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Why does jsonb_set() remove non-mentioned keys?
2019-07-04 13:18 Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 16:20 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 20:33 ` Re: Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 21:03 ` Re: Why does jsonb_set() remove non-mentioned keys? Gianni Ceccarelli <dakkar@thenautilus.net>
2019-07-04 21:09 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
@ 2019-07-04 21:42 ` David G. Johnston <david.g.johnston@gmail.com>
1 sibling, 0 replies; 9+ messages in thread
From: David G. Johnston @ 2019-07-04 21:42 UTC (permalink / raw)
To: Gianni Ceccarelli <dakkar@thenautilus.net>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, Jul 4, 2019 at 2:09 PM David G. Johnston <david.g.johnston@gmail.com>
wrote:
> On Thursday, July 4, 2019, Gianni Ceccarelli <dakkar@thenautilus.net>
> wrote:
>
>>
>> > select to_jsonb('null'::text);
>> ┌──────────┐
>> │ to_jsonb │
>> ├──────────┤
>> │ "null" │
>> └──────────┘
>>
>>
> Json null
>
>
Sorry, my bad on this last one. You cannot use to_jsonb to construct a
json null. The only way, I think, to construct a json null scalar is with
an explicit literal.
SELECT 'null'::jsonb;
The to_jsonb function always interprets a textual value passed to it as
being the literal text and so the original query results in a json string
whose content is "null"
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Why does jsonb_set() remove non-mentioned keys?
2019-07-04 13:18 Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 16:20 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 20:33 ` Re: Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 21:03 ` Re: Why does jsonb_set() remove non-mentioned keys? Gianni Ceccarelli <dakkar@thenautilus.net>
2019-07-04 21:09 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
@ 2019-07-05 08:00 ` Gianni Ceccarelli <dakkar@thenautilus.net>
2019-07-05 08:59 ` Re: Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
1 sibling, 1 reply; 9+ messages in thread
From: Gianni Ceccarelli @ 2019-07-05 08:00 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Aha! I had mis-understood how "strict"-ness works.
Thank you David for the explanation!
Thomas: the two main pieces are these:
> SQL null and json null are represented differently
As far as SQL is concerned, `'null'::jsonb` is a valid (non-`NULL`)
value. The SQL part of Postgres doesn't "look inside" the jsonb value,
the same way it doesn't "look inside" numbers or strings or
whatever. It only cares if they're `NULL` or not, and then it passes
them to functions and operators (ok, it does look at boolean values
for `WHERE` clauses, but even `ORDER BY` is handled by comparison
operators)
> strict functions with sql null inputs yield sql null output without
> even executing the function
So when the SQL-level executor sees a call to any function declared
strict with some NULL parameters, it doesn't call the function at
all. `whatever_my_function('a string',1234,NULL)` is always `NULL`
--
Dakkar - <Mobilis in mobile>
GPG public key fingerprint = A071 E618 DD2C 5901 9574
6FE2 40EA 9883 7519 3F88
key id = 0x75193F88
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Why does jsonb_set() remove non-mentioned keys?
2019-07-04 13:18 Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 16:20 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 20:33 ` Re: Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 21:03 ` Re: Why does jsonb_set() remove non-mentioned keys? Gianni Ceccarelli <dakkar@thenautilus.net>
2019-07-04 21:09 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
2019-07-05 08:00 ` Re: Why does jsonb_set() remove non-mentioned keys? Gianni Ceccarelli <dakkar@thenautilus.net>
@ 2019-07-05 08:59 ` Thomas Kellerer <spam_eater@gmx.net>
2019-07-05 11:50 ` Re: Why does jsonb_set() remove non-mentioned keys? Peter J. Holzer <hjp-pgsql@hjp.at>
0 siblings, 1 reply; 9+ messages in thread
From: Thomas Kellerer @ 2019-07-05 08:59 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Gianni Ceccarelli schrieb am 05.07.2019 um 10:00:
>> strict functions with sql null inputs yield sql null output without
>> even executing the function
>
> So when the SQL-level executor sees a call to any function declared
> strict with some NULL parameters, it doesn't call the function at
> all. `whatever_my_function('a string',1234,NULL)` is always `NULL`
>
Ah, I see. Thanks for the clarification
Then I would question if declaring jsonb_set as "strict" makes sense
Thomas
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Why does jsonb_set() remove non-mentioned keys?
2019-07-04 13:18 Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 16:20 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 20:33 ` Re: Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 21:03 ` Re: Why does jsonb_set() remove non-mentioned keys? Gianni Ceccarelli <dakkar@thenautilus.net>
2019-07-04 21:09 ` Re: Why does jsonb_set() remove non-mentioned keys? David G. Johnston <david.g.johnston@gmail.com>
2019-07-05 08:00 ` Re: Why does jsonb_set() remove non-mentioned keys? Gianni Ceccarelli <dakkar@thenautilus.net>
2019-07-05 08:59 ` Re: Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
@ 2019-07-05 11:50 ` Peter J. Holzer <hjp-pgsql@hjp.at>
0 siblings, 0 replies; 9+ messages in thread
From: Peter J. Holzer @ 2019-07-05 11:50 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 2019-07-05 10:59:31 +0200, Thomas Kellerer wrote:
> Gianni Ceccarelli schrieb am 05.07.2019 um 10:00:
> >> strict functions with sql null inputs yield sql null output without
> >> even executing the function
> >
> > So when the SQL-level executor sees a call to any function declared
> > strict with some NULL parameters, it doesn't call the function at
> > all. `whatever_my_function('a string',1234,NULL)` is always `NULL`
> >
>
> Ah, I see. Thanks for the clarification
>
> Then I would question if declaring jsonb_set as "strict" makes sense
I think it does but I raise the same question for to_jsonb. It's defined
on anyelement and the documentation says:
| Returns the value as json or jsonb. Arrays and composites are converted
| (recursively) to arrays and objects; otherwise, if there is a cast from
| the type to json, the cast function will be used to perform the
| conversion; otherwise, a scalar value is produced. For any scalar type
| other than a number, a Boolean, or a null value, the text representation
| will be used, in such a fashion that it is a valid json or jsonb value.
The documentation explicitely singles out "a number, a Boolean, or a
null value", but doesn't specify how they are treated. I would expect
that they are treated equivalently, though: An SQL number is converted
to a JSON number, an SQL boolean is converted to JSON true or false and
an SQL null is converted to JSON null. Returning SQL null instead of a
JSON null breaks that expectation for no discernible reason. It also
isn't consistent, since an SQL null in an array or composite is
converted to a JSON null, as I would expect.
hp
--
_ | Peter J. Holzer | we build much bigger, better disasters now
|_|_) | | because we have much more sophisticated
| | | hjp@hjp.at | management tools.
__/ | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/;
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 9+ messages in thread
end of thread, other threads:[~2019-07-05 11:50 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-07-04 13:18 Why does jsonb_set() remove non-mentioned keys? Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 16:20 ` David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 20:33 ` Thomas Kellerer <spam_eater@gmx.net>
2019-07-04 21:03 ` Gianni Ceccarelli <dakkar@thenautilus.net>
2019-07-04 21:09 ` David G. Johnston <david.g.johnston@gmail.com>
2019-07-04 21:42 ` David G. Johnston <david.g.johnston@gmail.com>
2019-07-05 08:00 ` Gianni Ceccarelli <dakkar@thenautilus.net>
2019-07-05 08:59 ` Thomas Kellerer <spam_eater@gmx.net>
2019-07-05 11:50 ` Peter J. Holzer <hjp-pgsql@hjp.at>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox