Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqJAd-0005N5-SC for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 17:04:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kqJAc-000078-Qq for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 17:03:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqJAb-000071-Di for pgsql-general@lists.postgresql.org; Fri, 18 Dec 2020 17:03:58 +0000 Received: from [64.147.123.25] (helo=wout2-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqJAW-0001t6-EC for pgsql-general@postgresql.org; Fri, 18 Dec 2020 17:03:57 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id B141BB00; Fri, 18 Dec 2020 12:03:39 -0500 (EST) Received: from imap2 ([10.202.2.52]) by compute5.internal (MEProxy); Fri, 18 Dec 2020 12:03:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= mime-version:message-id:in-reply-to:references:date:from:to:cc :subject:content-type; s=fm3; bh=yMU8ZaBEc0TThbRQnX2IAtk7kKom0TX 9XYd18Fl+7T8=; b=thf0fohqynmaUwp/zUm4PewILh2UJFl15/RiI+kG1wOg9/N 23VpVYbh1al40Xm9p1muX+wg7vyi7WnQOI74iDtqAibinxW0MhYARbAHwQmwCsdQ sPKTpW62GmaSvtU4nC3AIkLFdB4L3nfOlsUffhjDix2ttlWqjyOlaN1m8QnHN953 fPZ1TODkSECvwQkTvMHNxTCGqC+JWv8Dv3gpCKan8M2lKgrHolXIIFVnuRm3Q0fZ ZTAnouJg9UUKiXeArhqRq+IgS+8XHpdBkJr2y5ya8HVBHCi0VRTsrW85lOJk6IJ6 3B+ZmtCnkBKaqIzPxe/EdiuQr9a+elFL/o0djfQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=yMU8Za BEc0TThbRQnX2IAtk7kKom0TX9XYd18Fl+7T8=; b=fqCOlk4GuSr5l2mnaeM8ds l4frnyZJAM/8H3LE1MfukctsWKD5/OI1fbra2U6gmRE98BnI9aGNR/Bo5uebD8Zp QjdLzQnO03aVfY4w5Cdcfrn5xeKTCemnCzlMGGxtG+LPLr7CqHVH4qietG1KxsQh SXv/HmUCYlCA75eiT3w4WkP1CWVgDT2/9ykrO/zAqdSTjTWU3CiHPa8f04+ORJt1 AlSq7kRnKUYF6eQL34X+7Yw6kFihPr32jl6GkQV4SWad3Dy22MykVE66WJN8GLVF sujoNzRFH/zqPU92QvDKr1MvpYe58PcpO8a3smawKR8cHgrvMZvHe05nNqQQLWfA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudeliedgleeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesrgdtreerreertdenucfhrhhomhepfdflohgv lhculfgrtghosghsohhnfdcuoehjohgvlhestghomhhpihhlvghrrdhorhhgqeenucggtf frrghtthgvrhhnpedtueduffdtfeffjedutdethfdvteehjeehjeeitefgudeifffffefg gfegvdehfeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpehjohgvlhestghomhhpihhlvghrrdhorhhg X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id D5D52E04CC; Fri, 18 Dec 2020 12:03:36 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.1-61-gb52c239-fm-20201210.001-gb52c2396 Mime-Version: 1.0 Message-Id: <358bd217-0891-4f9f-b82f-082bf139191d@www.fastmail.com> In-Reply-To: <160622.1608308434@sss.pgh.pa.us> References: <0d72b76d-ca2b-4263-8888-d6dfca861c51@www.fastmail.com> <160622.1608308434@sss.pgh.pa.us> Date: Fri, 18 Dec 2020 18:03:16 +0100 From: "Joel Jacobson" To: "Tom Lane" , "David G. Johnston" Cc: pgsql-general Subject: Re: Avoid undesired flattening of jsonb arrays? Content-Type: multipart/alternative; boundary=5513dedeed5d4cd8bfbd17d22c029700 X-Host-Lookup-Failed: Reverse DNS lookup failed for 64.147.123.25 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --5513dedeed5d4cd8bfbd17d22c029700 Content-Type: text/plain The following nicer work-around was suggested to me by Andreas Karlsson: - jsonb_insert(x.jsonb_array,'{-1}',next_item.item,TRUE) + x.jsonb_array || jsonb_build_array(next_item.item) On Fri, Dec 18, 2020, at 17:20, Tom Lane wrote: > "David G. Johnston" writes: > > I'll agree that the description could discuss the case explicitly, and the > > array||scalar case could be added to the examples. > > Yeah, the documentation completely fails to explain what happens > when the inputs aren't two arrays or two objects. I'd kind of assumed > that that's an error, but it isn't. Some experimentation indicates > that the behavior in all cases except two objects is to convert any > non-array input to a one-element array, reducing the situation to the > two-array case. > > regards, tom lane > --5513dedeed5d4cd8bfbd17d22c029700 Content-Type: text/html Content-Transfer-Encoding: quoted-printable
The following n= icer work-around was suggested to me by Andreas Karlsson:
=
-    jsonb_insert(x.jsonb_array,'{-1}',nex= t_item.item,TRUE)
+    x.jsonb_array || jso= nb_build_array(next_item.item)


On Fri, Dec 18, 2020, at 17:20, Tom Lane wrote:
"David G. Johnston" <david.g.johnston@gmail.com> w= rites:
> I'll agree that the description could discuss = the case explicitly, and the
> array||scalar case could= be added to the examples.

Yeah, the docume= ntation completely fails to explain what happens
when the = inputs aren't two arrays or two objects.  I'd kind of assumed
that that's an error, but it isn't.  Some experimentation = indicates
that the behavior in all cases except two object= s is to convert any
non-array input to a one-element array= , reducing the situation to the
two-array case.
<= div>
regards, tom lane



--5513dedeed5d4cd8bfbd17d22c029700--