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 1o2Vgi-0000NU-S6 for pgsql-general@arkaria.postgresql.org; Sat, 18 Jun 2022 10:28:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o2Vgg-0001pY-0t for pgsql-general@arkaria.postgresql.org; Sat, 18 Jun 2022 10:28:18 +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 1o2Vgf-0001pP-L0 for pgsql-general@lists.postgresql.org; Sat, 18 Jun 2022 10:28:17 +0000 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1o2VgY-0004Z8-Ur for pgsql-general@lists.postgresql.org; Sat, 18 Jun 2022 10:28:16 +0000 Received: by mail-ej1-x62f.google.com with SMTP id o7so12888599eja.1 for ; Sat, 18 Jun 2022 03:28:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=z3b/jp+UibU1FsS8W0Wxqu+0LEUf5FtBdN8ccckCCEM=; b=glFnVKCcMM/m/FTk4j/8nU59UDK5bBfw/88JliISmIhxAMkXAN7tX0H8UhB+8pybYw C1W2owV8rI78FC3wcaUJOyBalN/G6P4Shr1HHGc/d5SodKOucnwbimaTGYIz7z2V5MtV PAN9626qbNHRlyQ16GabhKoUyrmnFSK29UpLskh9wM/caXRLSUNwVO7/KLTmHU3+KKJg 8uZGgtRiCEQ2abZWLAdkr4LLbJReaacP0daRXI5/UHTdwiWgu4En0zzCLc6PX7EaUYNw HLGn9wfvbvC3lF0ELZpcPmGgbr/cTC9sgyMruH8EMouEKXsNXomiaQRtOTPLI6WGTxnG hAYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=z3b/jp+UibU1FsS8W0Wxqu+0LEUf5FtBdN8ccckCCEM=; b=1TSnWLcHOdDFT9uTU3OngnkID/yWJh/F4TdGlKLJaGDRzK95GLdqohEbmE8rMGygW5 2VAuDfttDGOFxMhHmZ2ajDGhl/eYhs9jB37UVvhR72hE2kK7IYCNBHfGeFp1LuTyaxe/ A0hxqLUD+RjRWepvcsezilwbPjJ4d9XnfRuQI1gPorrn60/yX82dtkP0UMuSJCVl0L0C 8AzssEqq+xMB2xQf1Oqv9TqlmZMSeucIaVFODbRdSVJ2oXaaDFd2xSGMd+q6+oQCek1L DVE7DmfMu4Ni87GhrSc/uWXKHXVPDb1niwIm9+OnfPk+hEgO9sv1Fstjr5Z/vUcdEzAZ /Kcw== X-Gm-Message-State: AJIora/gWt2ETzqvpfyxYTNo+Z2USqrS3PPuUvlRoAfi89DM/y4j/KuR 7W/LX3SqMyFRwpn9C8rNLEU= X-Google-Smtp-Source: AGRyM1u96k3eYqU8dCSfVnD93fJbx7lo2X6kJYBl9N9zAps7SIQO0TSK+w4ZewTTTsY0i/FZIqZmvA== X-Received: by 2002:a17:906:74d1:b0:712:2293:8f41 with SMTP id z17-20020a17090674d100b0071222938f41mr12620107ejl.495.1655548088663; Sat, 18 Jun 2022 03:28:08 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id b6-20020aa7dc06000000b0042dc882c823sm5707556edu.70.2022.06.18.03.28.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 18 Jun 2022 03:28:07 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: Re: '{"x": 42, "y": null}'::jsonb != '{"x": 42}'::jsonb ... Really? From: Alban Hertroys In-Reply-To: <780CD5C2-B5B4-4C07-A9E7-33E1352D441A@yugabyte.com> Date: Sat, 18 Jun 2022 12:28:01 +0200 Cc: Tom Lane PostgreSQL , "David G. Johnston" , mmoncure@gmail.com, pgsql-general list Content-Transfer-Encoding: quoted-printable Message-Id: References: <7E0BF7DF-CC12-48B6-8550-9C2B5A8F78B1@yugabyte.com> <426968.1654321063@sss.pgh.pa.us> <780CD5C2-B5B4-4C07-A9E7-33E1352D441A@yugabyte.com> To: Bryn Llewellyn X-Mailer: Apple Mail (2.3608.120.23.2.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 18 Jun 2022, at 2:14, Bryn Llewellyn wrote: >=20 > I implemented two complementary functions: >=20 > =E2=80=94"no_null_keys()" checks that a "jsonb" value has no = occurrences of =C2=AB "some key": null =C2=BB >=20 > =E2=80=94"strip_null_keys()" removes =C2=AB "some key": null =C2=BB = occurrences from a "jsonb" value >=20 > The code checks with "no_null_keys()" that, as expected, no ingested = JSON document has an occurrence of =C2=AB "some key": null =C2=BB. >=20 > And it uses "strip_null_keys()" on the output of "to_jsonb()" =E2=80=94 = and, as appropriate, any other built-in JSON function that produces a = "jsonb" value. >=20 > It was straightforward to implement these two functions by using = REGEXP built-in functionality on the canonically formatted "text" value = produced by the "jsonb::text" typecast. In my experience, using regular expressions applied to document formats = tends to get you false positives. I=E2=80=99d be worried about books = with titles similar to 'How we wrote a regular expression to detect = occurrences of "some key": null in our JSON documents', for example. For stripping those null occurrences, you are aware of the = json_strip_nulls(json) and jsonb_strip_nulls(jsonb) functions, right? For detecting them on a recent PG, the @? operator or = json_path_exists(json, jsonpath) functions would probably do the trick. I am not too familiar with JSONPATH expressions, but I expect (it passed = some preliminary testing) this would detect your nulls just fine, while = taking JSON semantics into account: jsonb_path_exists(document, '$.** ? (@ =3D=3D null)'::jsonpath) For PG-specifics on JSONPATH, see section 9.16.2 on: = https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-SQLJ= SON-OP-TABLE A recursive query is another possible solution. It would probably = perform far worse, but I find them more rewarding to write. Some people = prefer Sodoku. Regards, Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.