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 1qyKnW-007hQH-C6 for pgsql-bugs@arkaria.postgresql.org; Wed, 01 Nov 2023 23:38:54 +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 1qyKnV-009dXK-24 for pgsql-bugs@arkaria.postgresql.org; Wed, 01 Nov 2023 23:38:53 +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 1qyKnU-009dXB-Pz for pgsql-bugs@lists.postgresql.org; Wed, 01 Nov 2023 23:38:52 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qyKnS-004EdD-28 for pgsql-bugs@lists.postgresql.org; Wed, 01 Nov 2023 23:38:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2023062407; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-ID:Content-Description; bh=/1x8E4eBqiXwQh+WiKDRGvesMq9OAxOoloe3Q2nwgnk=; b=Ft4jtJPvidyk4YwZpJBN/3gyT3 SEhSLyfkK77Yh2Uo4GQcqs3Byi5AS2jgpSbZIXcxXs4pw3vIwyTtEoWIPcA6VThd1A/Ddaner/AZC 24KtBiQIRloWHk+wENDoqrqkaboqNXHNLJ9JSZI+nojaoE2UvotBE6X7hiZjJYAD3HLaQw5EwOI9h QWVxh/Me0Nc6+YL10BLeHz278sx4I2bbyAAHyg0BGGGZhyv6cgvjl0L+C0K0KoxRhBxW6MAMpu4v2 b2kN5yV0+Zq+CeBkFOB1Loerwi+XEIC29LJsunx6rp2/Tw6htVerllOnzXkNug23fcInpfdagZIwg zAMR5Wxw==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1qyKnP-00A43F-27; Wed, 01 Nov 2023 19:38:47 -0400 Date: Wed, 1 Nov 2023 19:38:47 -0400 From: Bruce Momjian To: "David G. Johnston" Cc: Wolfgang Walther , PostgreSQL mailing lists Subject: Re: Wrong result for comparing ROW(...) with IS NOT NULL Message-ID: References: <21ff8e9c-627a-f949-fb00-a41b9ddcc9d3@technowledgy.de> <2ba24707-e7ba-2ccb-7269-71202a400a70@technowledgy.de> <4a853592-a43b-b677-5026-212196b6e9c2@technowledgy.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nZHLo70bJvfFXIVj" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --nZHLo70bJvfFXIVj Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Thu, Nov 5, 2020 at 10:43:45AM -0700, David G. Johnston wrote: > On Thu, Nov 5, 2020 at 8:06 AM Wolfgang Walther > wrote: > > Wolfgang Walther: > > > Thank you, that explains it very well. > > > > When I realized there was something unexpected going on, I was looking > > at all the ROW() syntax in the docs and I found this (as mentioned > > upthread): > > > > https://www.postgresql.org/docs/current/sql-expressions.html# > SQL-SYNTAX-ROW-CONSTRUCTORS > > > > > > It might be worth it to either add another example for IS NOT NULL, like > > > > SELECT ROW(table.*) IS NOT NULL FROM table;  -- detect all-non-null rows > > > > or add a link to section 9.2, that you mentioned. Or do both. > > Attached are some patches. > > > I would move examples related to IS NULL to 9.2 > > I would also include a third example in 9.2: SELECT NOT(ROW(table.*) IS NOT > NULL); -- detect at least one null column in row > > Thus: > > "Row constructors can be used to build composite values to be stored in a > composite-type table column, or to be passed to a function that accepts a > composite parameter. Also, it is possible to test a row using the standard > comparison operators described in chapter 9.2, compare a row against subquery > results as described in chapter 9.23, or compare one row against another as > described in chapter 9.24." > > And drop the examples and the following paragraph. > > Also, nothing in 9.2 precludes composite and row constructor comparisons from > being included there, and the intro material suggests that they probably should > be.  That we cover the details of (composite IS DISTINCT FROM composite) in > 9.24 instead of 9.2 should be noted in 9.2 somewhere and a link to 9.24 > provided. > > I do agree with changing the identifier to be more unique but I don't know if > it is this simple. In reviewing this three-year-old email, I developed the attached patch which I think captures what David suggested above. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Only you can decide what is important to you. --nZHLo70bJvfFXIVj Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="row_nulls.diff" diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index c76ec52c55..ac4b47dc1e 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -718,7 +718,19 @@ repeat('Pg', 4) PgPgPgPg IS NULL and IS NOT NULL do not always return inverse results for row-valued expressions; in particular, a row-valued expression that contains both null and non-null fields will return false - for both tests. In some cases, it may be preferable to + for both tests. For example: + + +SELECT ROW(1,2.5,'this is a test') = ROW(1, 3, 'not the same'); + +SELECT ROW(table.*) IS NULL FROM table; -- detect all-null rows + +SELECT ROW(table.*) IS NOT NULL FROM table; -- detect all-non-null rows + +SELECT NOT(ROW(table.*) IS NOT NULL) FROM TABLE; -- detect at least one null in rows + + + In some cases, it may be preferable to write row IS DISTINCT FROM NULL or row IS NOT DISTINCT FROM NULL, which will simply check whether the overall row value is null without any diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml index 3ba844057f..7c8f7605c4 100644 --- a/doc/src/sgml/syntax.sgml +++ b/doc/src/sgml/syntax.sgml @@ -2467,17 +2467,12 @@ SELECT getf1(CAST(ROW(11,'this is a test',2.5) AS myrowtype)); Row constructors can be used to build composite values to be stored in a composite-type table column, or to be passed to a function that - accepts a composite parameter. Also, - it is possible to compare two row values or test a row with - IS NULL or IS NOT NULL, for example: - -SELECT ROW(1,2.5,'this is a test') = ROW(1, 3, 'not the same'); - -SELECT ROW(table.*) IS NULL FROM table; -- detect all-null rows - - For more detail see . - Row constructors can also be used in connection with subqueries, - as discussed in . + accepts a composite parameter. Also, it is possible to test rows + using the standard comparison operators as described in , to compare one row against another + as described in , and to + use them in connection with subqueries, as discussed in , --nZHLo70bJvfFXIVj--