Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so7HN-008PtZ-Oo for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Sep 2024 20:16:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1so7HN-001YGi-3Y for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Sep 2024 20:16:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so7HM-001YGa-Pg for pgsql-hackers@lists.postgresql.org; Tue, 10 Sep 2024 20:16:00 +0000 Received: from fhigh6-smtp.messagingengine.com ([103.168.172.157]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so7HJ-000WDE-KF for pgsql-hackers@postgresql.org; Tue, 10 Sep 2024 20:15:59 +0000 Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.phl.internal (Postfix) with ESMTP id 3979E11401A3; Tue, 10 Sep 2024 16:15:57 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-11.internal (MEProxy); Tue, 10 Sep 2024 16:15:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eisentraut.org; h=cc:cc:content-transfer-encoding:content-type:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:subject:subject:to:to; s=fm3; t=1725999357; x=1726085757; bh=+aJz8oPx3Lvp/qy4fNC08S9sGZ9gdWGd c3bshGRXxa4=; b=k0mnk8dWC8HnBYfhnjfqWdVhL5Ft/pEo1H6YnPti085a+M82 G65yo1sTFhOBaCPICk4qCm0aNXNZAhhw16tuNC6/aUOObW/zEjMALiUD4P+RGboY AGrhFkgwhFC0qTsotgLDWYB6IGAtOzusq3cXAbvzLneDAbKP0JYsh2wcjnux/U7d WlIJBQl1K8HEV+yadm+qqZINLX4ITc6gK6MadHz0H/Opay0xmR7O0JGnSHH/kBQt ndI57PeCgyaSZugfZB79tHVA3e4C8Z4wZQZDd3i9bvYI8ziHUjiJqjZXFOx4DXM0 A1jm0kmS2CKH1BThAtu7LHcG+Ojk/6HfMBAKUw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1725999357; x= 1726085757; bh=+aJz8oPx3Lvp/qy4fNC08S9sGZ9gdWGdc3bshGRXxa4=; b=Y ixwe6UQJO102OgJkVF6sF8avWyrBWD76el+BeZ+SHl/3zV5RZeYShDDJ1aL1B0vN LP+TSEdVKlsXEpmp798FlegoG9OZkvA2yeWEQCxdSJiye7yyWCLuJ4OjYC24qVai 0or7hmosiSRD96xEBDbfkrl494mWa0m4iGxkVXEEWxCzD75IRP8DEa1ONn2n3oSt kSkHdQ1YjJGMEQnUWcUqNuREtth6PbdMGLNCoAZ6EANNBdD/TP0PDv7gFKsBdFPs sdSS94/8P4MkE+c+5hSp2BN7EzVPnUAOWoHVZxmwhPRj8PE4hGRcBcdY1kQ4DC6m gY0prhZlsuHRxZnpE4OUA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudeiledgudefkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefkffggfgfuvfevfhfhjggtgfesthekredttddv jeenucfhrhhomheprfgvthgvrhcugfhishgvnhhtrhgruhhtuceophgvthgvrhesvghish gvnhhtrhgruhhtrdhorhhgqeenucggtffrrghtthgvrhhnpeejhfevhedttefgfffhhfef fefggffhffelgfeiueeukeehvdehvdefheffvdefueenucevlhhushhtvghrufhiiigvpe dtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpvghtvghrsegvihhsvghnthhrrghuthdr ohhrghdpnhgspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoh eprghmihhtlhgrnhhgohhtvgdtleesghhmrghilhdrtghomhdprhgtphhtthhopehpghhs qhhlqdhhrggtkhgvrhhssehpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: ie0a040ee:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 10 Sep 2024 16:15:56 -0400 (EDT) Message-ID: <749b08c1-a28d-4b40-82ab-e7219cff623f@eisentraut.org> Date: Tue, 10 Sep 2024 22:15:55 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: json_query conditional wrapper bug To: Amit Langote Cc: pgsql-hackers References: <8022e067-818b-45d3-8fab-6e0d94d03626@eisentraut.org> <0919c21e-af83-458b-8e40-1228429eaaa4@eisentraut.org> Content-Language: en-US From: Peter Eisentraut In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10.09.24 10:00, Amit Langote wrote: > Sorry for missing this report and thanks Andrew for the offlist heads up. > > On Wed, Sep 4, 2024 at 7:16 PM Peter Eisentraut wrote: >> On 28.08.24 11:21, Peter Eisentraut wrote: >>> These are ok: >>> >>> select json_query('{"a": 1, "b": 42}'::jsonb, 'lax $.b' without wrapper); >>> json_query >>> ------------ >>> 42 >>> >>> select json_query('{"a": 1, "b": 42}'::jsonb, 'lax $.b' with >>> unconditional wrapper); >>> json_query >>> ------------ >>> [42] >>> >>> But this appears to be wrong: >>> >>> select json_query('{"a": 1, "b": 42}'::jsonb, 'lax $.b' with conditional >>> wrapper); >>> json_query >>> ------------ >>> [42] >>> >>> This should return an unwrapped 42. >> >> If I make the code change illustrated in the attached patch, then I get >> the correct result here. And various regression test results change, >> which, to me, all look more correct after this patch. I don't know what >> the code I removed was supposed to accomplish, but it seems to be wrong >> somehow. In the current implementation, the WITH CONDITIONAL WRAPPER >> clause doesn't appear to work correctly in any case I could identify. > > Agreed that this looks wrong. > > I've wondered why the condition was like that but left it as-is, > because I thought at one point that that's needed to ensure that the > returned single scalar SQL/JSON item is valid jsonb. > > I've updated your patch to include updated test outputs and a nearby > code comment expanded. Do you intend to commit it or do you prefer > that I do? This change looks unrelated: -ERROR: new row for relation "test_jsonb_constraints" violates check constraint "test_jsonb_constraint4" +ERROR: new row for relation "test_jsonb_constraints" violates check constraint "test_jsonb_constraint5" Is this some randomness in the way these constraints are evaluated?