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 1tvGn2-002xPZ-5T for pgsql-docs@arkaria.postgresql.org; Thu, 20 Mar 2025 14:22:32 +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 1tvGn0-002QT7-TU for pgsql-docs@arkaria.postgresql.org; Thu, 20 Mar 2025 14:22:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tvGn0-002QSz-JL for pgsql-docs@lists.postgresql.org; Thu, 20 Mar 2025 14:22:30 +0000 Received: from mail-qt1-x829.google.com ([2607:f8b0:4864:20::829]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tvGmy-0009An-05 for pgsql-docs@lists.postgresql.org; Thu, 20 Mar 2025 14:22:30 +0000 Received: by mail-qt1-x829.google.com with SMTP id d75a77b69052e-476b4c9faa2so10183921cf.3 for ; Thu, 20 Mar 2025 07:22:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=equatoria-us.20230601.gappssmtp.com; s=20230601; t=1742480546; x=1743085346; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=x51jIUBfgx2DrXt4miMfnyUrx1sJrxE/1M2lIpnelvc=; b=HUnvxbO0jre2ffeIWSsGxw+IxQEALheHCt9630Mly87yXoQUUz89v9JNhEHrvJL/5v CG6yvLwNDMqgEm48VemSVN0LsmxyDFQU4CkeWuqSSBuz79Z/rca2+J7ybhjcPimWKIRt DWxT2XDxhCnCQOZXxpOP3DkTa+xSG+TuAPlLLAav83mRLRxw3l8x3Czkgd3GR/CH83tM 9MLPS0fOqj5NT+pLTHm/zL4SnmC1gPshZhxNcv3gUkhnYkL9MReoBzosUrbX3MujYOKD paLCihaXc/wWjYnHNvGLk88zlrU//C9cAkccZazCuMOgHfmWvHCLAeGCjjlAVARP1V2E bAAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742480546; x=1743085346; h=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=x51jIUBfgx2DrXt4miMfnyUrx1sJrxE/1M2lIpnelvc=; b=HUCo/1n88CpmdunFhqpAl75v+p/oExUYZ4Nt5kBmXTYmfxaTIwum01EgXsHUXMqqj2 yg42Rt4YLstBvBbQRztfZk/dUb7RZ5EscDgyJHwob1qw2BNziCZhGXbudqB8Yli6tq3s 3Uicw6LxdfU2A4NIgwX1htPI19LumGRXfRDU9LyZH20yjOdHmk+aMo+Gp0LqMajwMhEL ngdJeffGhkHxCbJhZNnUJ7Hg8T9ASUcArgbwpxLvGikIqNHYRIPu7Jn8haWG+VfQiK6U cnox5CDDX39UkH6qsae6OUypaN1vf20BrmKgTpTRez/2MF3/NUXAqb3Lkft25Fno1kck qa0w== X-Forwarded-Encrypted: i=1; AJvYcCXck2JBtQGjNiLdXPnNjRHEoatQcTJAG+6zw7Yy5WIpZilW0p+XNjtrEWuY3KcOHCa6vkS0VOeSoWvg@lists.postgresql.org X-Gm-Message-State: AOJu0YxBJwtzUgo/qwliTVlnGudUP8lWf6e7N8yM81sV5/KcjkcmPjDE bBwHeKJh8METpd+iAWA37+Vav/nLIMqm/LEF6miGO5FGWuGPtjhVgBF6u1wxd6rnkbxl6VrYU3V bwtclQar1DTBuIhh6KrSXiDadJYUNvmaeRJr/BDRN7thCEyfYKIM= X-Gm-Gg: ASbGnctcW9mdSmpSj3+I4XaP+rciNh7HRLF/xNumqPl9yw03FqzQ0XZf5wVjfHYlZsB t5KylM7p5SdJGgavuidt/ysvkai9wgoUlnI1iOs4sP4aSwOU47INgP6n3eUfZAXwEL2kPfvrROz epjyO0YHddR/H5rrXiQSr5ncb0Jn4Hp9qjrxFr8W+KUP0sMRfyRctK2eY7Dw== X-Google-Smtp-Source: AGHT+IE87cwBjE7gGB+9yPjTMPA/zDsjbwGqn4RNc6NBDfoFu7jcPnlxeSD2GuaG4y/D4btD/OEtTU2kYmlDBFOrJAc= X-Received: by 2002:a05:622a:5a93:b0:476:8e3e:2da4 with SMTP id d75a77b69052e-47710dd562dmr71583681cf.38.1742480546368; Thu, 20 Mar 2025 07:22:26 -0700 (PDT) MIME-Version: 1.0 References: <174238647361.682.12732328104350596711@wrigleys.postgresql.org> In-Reply-To: From: Kirk Parker Date: Thu, 20 Mar 2025 07:22:14 -0700 X-Gm-Features: AQ5f1JpzVcRfXavC0bS88iAnNfctvw2BTpuNSswvNNPDH-axqToQEbNvLmsBwgM Message-ID: Subject: Re: Ambiguity in IS JSON description and logic To: vavankaru@gmail.com, pgsql-docs@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000fe9de60630c6dd34" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000fe9de60630c6dd34 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Mar 20, 2025 at 7:08=E2=80=AFAM Kirk Parker wrot= e: > > On Thu, Mar 20, 2025 at 2:46=E2=80=AFAM PG Doc comments form < > noreply@postgresql.org> wrote: > >> The following documentation comment has been logged on the website: >> >> Page: https://www.postgresql.org/docs/17/functions-json.html >> Description: >> >> On the manual page >> https://www.postgresql.org/docs/current/functions-json.html, in the Tabl= e >> 9.48. "SQL/JSON Testing Functions" there is a description of IS JSON. It >> includes the next sentence: "If WITH UNIQUE KEYS is specified, then any >> object in the expression is also tested to see if it has duplicate keys.= " >> And such text is ambiguous, because the term "object" has certain meanin= g >> regarding json format. In reality the option WITH UNIQUE KEYS allows to >> check for duplicated keys any array element not object. For objects, bot= h >> WITH UNIQUE KEYS and WITHOUT UNIQUE KEYS return false, and both IS JSON >> ARRAY WITH UNIQUE KEY and IS JSON ARRAY WITHOUT UNIQUE KEY return true (= it >> is at the same time with and without unique values, how it is possible?)= , >> i.e. it works the same as just IS JSON ARRAY. The example code that >> confirms >> my reasoning: >> SELECT >> js.vl AS "tested str", >> >> >> js.vl IS JSON OBJECT WITH UNIQUE KEYS AS ".. object w. UQ >> keys", >> >> js.vl IS JSON OBJECT WITHOUT UNIQUE KEYS AS ".. object w/o UQ keys= ", >> js.vl IS JSON ARRAY WITH UNIQUE KEYS AS ".. array w. UQ keys", >> >> >> js.vl IS JSON ARRAY WITHOUT UNIQUE KEYS AS ".. array w/o UQ keys"= , >> js.vl IS JSON ARRAY ".. array" >> FROM (VALUES ('{{"a": "a1"}, {"a": "a2"}}'), ('[{"a": "a1"}, {"a": >> "a2"}]'), >> ('["a", "a"]')) AS js(vl); >> >> I'm not sure what should be the right logic for this option, for me it >> looks >> now the same as simple IS JSON ARRAY without any UNIQUE KEY option, but = if >> we use an option it should be either true for WITH UNIQUE KEYS or WITHOU= T >> UNIQUE KEYS but not for both at the same time. But anyway the sentence I >> showed above should contain "array" instead of "object" because for >> objects >> it returns false independently of applied option. I tested it on >> "PostgreSQL 17.0 on x86_64-windows, compiled by msvc-19.41.34120, 64-bit= ". >> > > First, WITHOUT UNIQUE KEYS does not mean "confirm that there are duplicat= e > keys", it's just a way of stating the default explicitly. In other words = it > means "w/o testing for duplicate keys". Thus IS JSON OBJECT and IS JSON > OBJECT WITHOUT UNIQUE KEYS will both always return identical results on t= he > same JSON expression. > > Secondly, the UNIQUE test is recursive; for objects maybe the meaning is > intuitive, but for JSON arrays -- which don't have any concept of keys; > JSON arrays are just ordered lists -- it means "does this array contain a= ny > embedded objects with duplicate keys". > > See: > > SELECT js, > js IS JSON "json?", > js IS JSON OBJECT "object?", > js IS JSON OBJECT WITH UNIQUE KEYS "object w. UK?", > js IS JSON OBJECT WITHOUT UNIQUE KEYS "object w/o UK?", > js IS JSON ARRAY "array?", > js IS JSON ARRAY WITH UNIQUE KEYS "array w. UK?", > js IS JSON ARRAY WITHOUT UNIQUE KEYS "array w/o UK?" > FROM (VALUES > ('[{"a":1},{"b":2,"b":3}]'), -- expect t for array, array w/o UK > ('[{"a":1},{"b":2,"c":3}]'), -- expect t for ALL array tests > ('{"b":2,"b":3}'), -- expect t for object, object w/o U= K > ('{"c":2,"d":3}'), -- expect t for ALL object tests > ('{"c":2,"d":{ "e": 0, "e": 1}}'), -- WITH UNIQUE is recursive for > nested objects > ('{"c":2,"d":{ "e": 0, "f": {"g":1,"g":2}}}'), -- no matter how deep > ('[{"a":1},{"b":2,"c":{"d":1, "d":2}}]') -- and also tests arrays > recursively for embedded objecs > ) foo(js); > > > A couple of side notes: > > 1. Your first data example is not JSON at all. It's helpful for this kin= d > of test to include a plain IS JSON column, since any of the IS JSON X tes= ts > can fail for two reasons: (a) it's not JSON, or (b) it is JSON but it's n= ot > an X. > > 2. Curiously, the JSON spec itself is completely silent on the meaning of > objects with duplicate keys. PostgreSQL is more helpful in this > regard--the docs explicitly state that the last value is the one that is > retained by JSONB and used in processing functions. > > To improve the documentation here, I would suggest simply adding the word "recursively" after "tested": If WITH UNIQUE KEYS is specified, then any object in the *expression* i= s also tested recursively to see if it has duplicate keys As for clarifying -- if we need to -- that WITHOUT UNIQUE KEYS means "without testing for uniqueness" rather than "confirming that non-unique keys are present", I will defer to anyone who has more familiarity with the PostgreSQL documentation style. I probed around looking at other situations where there are phrases that make the default condition explicit (e.g. CREATE INDEX ... NULLS DISTINCT) that didn't help me much; all my attempts ended up too wordy for my liking. --000000000000fe9de60630c6dd34 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, Mar 20, 2025 at 7:08=E2=80=AFAM K= irk Parker <khp@eq= uatoria.us> wrote:
=

On Thu, Mar 20= , 2025 at 2:46=E2=80=AFAM PG Doc comments form <noreply@postgresql.org> wrote:
The following doc= umentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/17/funct= ions-json.html
Description:

On the manual page
https://www.postgresql.org/docs/current/f= unctions-json.html, in the Table
9.48. "SQL/JSON Testing Functions" there is a description of IS J= SON. It
includes the next sentence: "If WITH UNIQUE KEYS is specified, then an= y
object in the expression is also tested to see if it has duplicate keys.&qu= ot;
And such text is ambiguous, because the term "object" has certain= meaning
regarding json format. In reality the option WITH UNIQUE KEYS allows to
check for duplicated keys any array element not object. For objects, both WITH UNIQUE KEYS and WITHOUT UNIQUE KEYS return false, and both IS JSON
ARRAY WITH UNIQUE KEY and IS JSON ARRAY WITHOUT UNIQUE KEY return true (it<= br> is at the same time with and without unique values, how it is possible?), i.e. it works the same as just IS JSON ARRAY. The example code that confirm= s
my reasoning:=C2=A0
SELECT
=C2=A0 =C2=A0 js.vl AS=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 "tested str",=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0

=C2=A0 =C2=A0 js.vl IS JSON OBJECT WITH UNIQUE KEYS AS=C2=A0 =C2=A0 =C2=A0 = ".. object w. UQ keys",=C2=A0 =C2=A0

=C2=A0 =C2=A0 js.vl IS JSON OBJECT WITHOUT UNIQUE KEYS AS=C2=A0 =C2=A0"= ;.. object w/o UQ keys",
=C2=A0 =C2=A0 js.vl IS JSON ARRAY WITH UNIQUE KEYS AS=C2=A0 =C2=A0 =C2=A0 = =C2=A0".. array w. UQ keys",=C2=A0 =C2=A0

=C2=A0 =C2=A0 js.vl IS JSON ARRAY WITHOUT UNIQUE KEYS AS=C2=A0 =C2=A0 "= ;.. array w/o UQ keys",
=C2=A0 =C2=A0 js.vl IS JSON ARRAY ".. array"
FROM (VALUES ('{{"a": "a1"}, {"a": "= a2"}}'), ('[{"a": "a1"}, {"a": &= quot;a2"}]'),
('["a", "a"]')) AS js(vl);

I'm not sure what should be the right logic for this option, for me it = looks
now the same as simple IS JSON ARRAY without any UNIQUE KEY option, but if<= br> we use an option it should be either true for WITH UNIQUE KEYS or WITHOUT UNIQUE KEYS but not for both at the same time. But anyway the sentence I showed above should contain "array" instead of "object"= because for objects
it returns false independently of applied option.=C2=A0 I tested it on
"PostgreSQL 17.0 on x86_64-windows, compiled by msvc-19.41.34120, 64-b= it".

First, WITHOUT UNIQUE KEYS do= es not mean "confirm that there are duplicate keys", it's jus= t a way of stating the default explicitly. In other words it means "w/= o testing for duplicate keys". Thus IS JSON OBJECT and IS JSON OBJECT = WITHOUT UNIQUE KEYS will both always return identical results on the same J= SON expression.=C2=A0

Secondly, the UNIQUE test is= recursive; for objects maybe the meaning is intuitive, but for JSON arrays= -- which don't have any concept of keys; JSON arrays are just ordered = lists -- it means "does this array contain any embedded objects with d= uplicate keys".

See:
=C2=A0SELECT js,
<= div class=3D"gmail_quote">
=C2=A0 js IS JSON &= quot;json?",
=C2=A0 js IS JSON OBJECT "object?",
=C2=A0 js I= S JSON OBJECT WITH UNIQUE KEYS "object w. UK?",
=C2=A0 js IS JSON= OBJECT WITHOUT UNIQUE KEYS "object w/o UK?",
<= div class=3D"gmail_quote">
=C2=A0 js IS JSON A= RRAY "array?",
= =C2=A0 js IS JSON ARRAY WITH UNIQUE KEYS "arr= ay w. UK?",
=C2=A0 js IS JSON ARRAY WITHOUT UNIQUE KEYS "array w/= o UK?"
FROM (VALUES
= =C2=A0 ('[{"a":1},{"b":2,&= quot;b":3}]'), =C2=A0 =C2=A0 =C2=A0 -- expect t for array, array w= /o UK
=C2=A0 ('[{"a":1},{"b":2,"c":3}]= 9;), =C2=A0 =C2=A0 =C2=A0 -- expect t for ALL array tests
=C2=A0 ('{&qu= ot;b":2,"b":3}'), =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 -- expect t for object, object w/o UK
<= div class=3D"gmail_quote">
=C2=A0 ('{"= ;c":2,"d":3}'), =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 -- expect t for ALL object tests
=C2=A0 ('{"c&qu= ot;:2,"d":{ "e": 0, "e": 1}}'), -- WITH U= NIQUE is recursive for nested objects
=C2=A0 ('{"c":2,"d= ":{ "e": 0, "f": {"g":1,"g":2}= }}'), -- no matter how deep
=C2=A0 ('[{"a":1},{"b&qu= ot;:2,"c":{"d":1, "d":2}}]') -- and also = tests arrays recursively for embedded objecs
) foo(js);

A couple of side notes:

1. Your first data examp= le is not JSON at all.=C2=A0 It's helpful for this kind of test to incl= ude a plain IS JSON column, since any of the IS JSON X tests can fail for t= wo reasons: (a) it's not JSON, or (b) it is JSON but it's not an X.=

2. Curiously, the JSON spec itself is comple= tely silent on the meaning of objects with duplicate=C2=A0keys.=C2=A0=C2=A0= PostgreSQL is more helpful in this regard--the d= ocs explicitly state that the last value is the one that is retained by JSO= NB and used in processing functions.

=C2=A0
To improve the documentation=C2=A0here,= I would suggest simply adding the word "recursively" after "= ;tested":

=C2=A0 =C2=A0 If=C2=A0<= code class=3D"gmail-literal" style=3D"box-sizing:border-box;font-family:mon= ospace,monospace;font-size:14.4px;color:rgb(33,37,41);border-radius:0.25rem= ;margin:0px;padding:0px;word-break:unset">WITH UNIQUE KEYS=C2=A0is specified, then any object in the=C2=A0expression=C2=A0is also tested recu= rsively to see if it has duplicate keys

As for clarifying -- if we need to -- tha= t WITHOUT UNIQUE KEYS means "without testing for uniqueness" rath= er than "confirming that non-unique keys are present", I will def= er to anyone who has more familiarity with the PostgreSQL documentation=C2= =A0style.=C2=A0 I probed around looking at other situations where there are= phrases that make the default condition explicit (e.g. CREATE INDEX ... NU= LLS DISTINCT) that didn't help me much; all my attempts ended up too wo= rdy for my liking.
--000000000000fe9de60630c6dd34--