jsonb_concat: make sure we always return a non-scalar value

From: Oskari Saarenmaa <os(at)ohmu(dot)fi>
To: pgsql-hackers(at)postgresql(dot)org
Subject: jsonb_concat: make sure we always return a non-scalar value
Date: 2015-09-05 06:47:07
Message-ID: 55EA8FEB.5040004@ohmu.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

There was a long thread about concatenating jsonb objects to each other,
but that discussion didn't touch concatenating other types. Currently
jsonb_concat always just returns the other argument if one of arguments
is considered empty. This causes surprising behavior when concatenating
scalar values to empty arrays:

os=# select '[]'::jsonb || '1'::jsonb;
1

os=# select '[]'::jsonb || '[1]'::jsonb;
[1]

os=# select '[]'::jsonb || '1'::jsonb || '2'::jsonb;
[1, 2]

os=# select '0'::jsonb || '1'::jsonb;
[0, 1]

os=# select '{"x": "y"}'::jsonb || '[1]'::jsonb;
[{"x": "y"}, 1]

os=# select '{"x": "y"}'::jsonb || '1'::jsonb;
ERROR: invalid concatenation of jsonb objects

Attached a patch to fix and test this. Also added a test case for
concatenating two scalar values which currently produces an array.. I'm
not sure that behavior makes sense, but didn't want to change that in
this patch as I guess someone could consider that feature useful.

/ Oskari

Attachment Content-Type Size
0001-jsonb_concat-make-sure-we-always-return-a-non-scalar.patch text/x-patch 2.7 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fabien COELHO 2015-09-05 06:56:46 Re: checkpointer continuous flushing
Previous Message Jim Nasby 2015-09-05 06:41:41 Re: Fwd: Core dump with nested CREATE TEMP TABLE