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 1p17UT-0007XD-7Y for pgsql-bugs@arkaria.postgresql.org; Fri, 02 Dec 2022 14:58:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p17UR-0004j6-SH for pgsql-bugs@arkaria.postgresql.org; Fri, 02 Dec 2022 14:58:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p17UR-0004ix-HV for pgsql-bugs@lists.postgresql.org; Fri, 02 Dec 2022 14:58:11 +0000 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p17UP-0000E1-9v for pgsql-bugs@lists.postgresql.org; Fri, 02 Dec 2022 14:58:10 +0000 Received: by mail-pf1-x42d.google.com with SMTP id h28so5106244pfq.9 for ; Fri, 02 Dec 2022 06:58:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=O+gGCAackO21NT3n0dV0yk2GFF4xPVDzL3jOXTH49q0=; b=BBj9Xca+0D6Ttt5gh1vsVMVG7Wjt4ZsTdOEBdS8dCS1zf6lFhFwcp0CXWGuaEwKtfC fi1s1wpC7+egToiwgzOCRGyREtrnaFlOJjnatlIZj+NHo6GWLDAN6skLeC/PQ1z8uNHX Xz5ERTsNg5ZL4L9Z1k6vGpOL+UahyiEty7nm9p42TBv5PkcOvOimS6E3ZpnnH/blX5Su T1NSVSz5KeRSjNFvbTaDRVeUjIe3BYYKRqPwfPjIp/gt2nRfyrg/MKWhTj7NSeBV6sa0 AbNV4M6Rc9rrkfBBXQBanwjLRy9Fca3MK1B0cIvRD95IVr1qYJHZp/wQu9JOAZ+AXxiB Haog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O+gGCAackO21NT3n0dV0yk2GFF4xPVDzL3jOXTH49q0=; b=bgG8mxPowqB3bYVG/ilulYL8OUH8pB6TCrq9gP+1nPKuWLz1s5Uuhdw6GwzTabyaVU bbXsNSaKlG7rYm/mwS71faAkRfXrx55A5s4X3ArC2yIpaYNFccmMam+JkoVJOdpMHvvk JZ9GOSzcKusvJC2oKqO3RBKFBJlc697oju+YZzv70eoL6vgQIxJ4/OfH5EWCB08a2WKW NS/ENOsHsKKf6fz3YVx2sjJFuL//mffoXnPeJN0V9W2sakVHpaHnZBfH7KzW1jGQfpfE CzZRrFtT3DuAJuHfyfRMWHBlaEscLCnWteJpvJDAjfQqgPr70ZYKFIvCMqW/QL/wYuLS eL6g== X-Gm-Message-State: ANoB5plSrhE25ENWqRV5k0vb/18G6gf52n40RTLHX+Ekn8xp6X8b5pag 1OxLFymmo2aFdzAxMijc71cZyd3Vz2EtdCSdebY= X-Google-Smtp-Source: AA0mqf716JXkWFSQsSnlBZqFh9P29bPAE/2AfxNHjn7GA2AEx8wureMvCk8xhq8Of1cadMB7axV7QhBCcA8klcqkKQQ= X-Received: by 2002:a65:6741:0:b0:477:ac77:2366 with SMTP id c1-20020a656741000000b00477ac772366mr42651720pgu.413.1669993087869; Fri, 02 Dec 2022 06:58:07 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Alexander Korotkov Date: Fri, 2 Dec 2022 17:57:56 +0300 Message-ID: Subject: Re: Bug in jsonb_path_exists (maybe _match) one-element scalar/variable jsonpath handling To: "David G. Johnston" Cc: PostgreSQL Bug List Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Dec 2, 2022 at 5:24 PM David G. Johnston wrote: > On Fri, Dec 2, 2022 at 5:18 AM Alexander Korotkov = wrote: >> >> On Thu, Nov 24, 2022 at 8:31 AM David G. Johnston >> wrote: >> > The following query produces an incorrect result. It should error (or= at worse produce "false"), but it instead produces "true" (this applies to= @? too) >> > >> > select jsonb_path_exists('{"foo": true}'::jsonb, '$bar', '{}', false); >> >> Variable case is definitely broken, but I don't think other cases are >> broken. If we're checking for existence and there is a constant, we >> can immediately return true because constant exists indeed. That >> logic doesn't work for variable, which could be non-existent. >> >> > select jsonb_path_exists('{"foo": true}'::jsonb, '"bar"', '{}', false)= ; -- true (bar in double quotes) > > > I think my issue with the constant is that the function itself is said to= return whether or not the provided path matches the input json. It is imp= ossible to match the input json if there is no reference to the input json = in the jsonpath expression. As the existing wording promises: "Checks whet= her the JSON path returns any item for the specified JSON value" - the word= item is rightly taken to mean that the path at minimum references the root= (i.e., mandatory $) - and that any true result from exists will, if the ex= pression is used for _match, produce the "item for the specified JSON value= " that was found. > > So I'll stand by my conclusion that the behavior of constants is buggy - = though I suppose fixing the bug is probably most readily accomplished by ch= anging the definition of what behavior we are promising and fixing up the d= ocumentation to express that change. In short, it is really an error to no= t specify "$" in your expression - but if you don't you will simply get a t= rue outcome for the existence test - for backward compatibility reasons. Thank you for explaining your point, but I can't agree with that. Constant jsonpath expression is always returning item for the input JSON value. Even despite the input value is ignored. This is redundant case, but still correct. >> > select jsonb_path_match('{"foo": true}'::jsonb, '"bar"', '{}', false);= -- ERROR: single boolean result is expected >> > select jsonb_path_match('{"foo": true}'::jsonb, '$bar', '{"bar":"foo"}= ', false); -- same error as above, as expected >> > >> > I expect the missing variable specification to produce jperError and t= he rest of the block to produce jperNotFound. The "single boolean result e= xpected" error seems incorrect though I'm not sure where that is coming fro= m. But I'm also not considering, or am even aware of, what the standard we= are guided by here says should actually happen. >> >> I think jsonb_path_match() behaves correctly, it expects jsonpatch >> expression to return single boolend and throws an error otherwise. > > Yeah, I may have mis-interpreted the meaning of the error message. Somet= hing like: "jsonpath expression must produce a single boolean result" would= be a bit more clear. OK, I'm not a native English speaker and can't judge about this. I propose this should be considered separately. BTW, what do you think about the patch? ------ Regards, Alexander Korotkov