pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Wolfgang Walther <walther@technowledgy.de>
Cc: PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Subject: Re: Wrong result for comparing ROW(...) with IS NOT NULL
Date: Wed, 1 Nov 2023 19:38:47 -0400
Message-ID: <ZULhh0y98-U67JRy@momjian.us> (raw)
In-Reply-To: <CAKFQuwY336Y0zQz-j=O-LXTMSDpxD2gB_ayiueMzCfkv5n6=vg@mail.gmail.com>
References: <21ff8e9c-627a-f949-fb00-a41b9ddcc9d3@technowledgy.de>
	<CAFj8pRAKiVUUFQawx70MXwd72rJ6D4UQFwAcmL5VVByE90HuYg@mail.gmail.com>
	<2ba24707-e7ba-2ccb-7269-71202a400a70@technowledgy.de>
	<CALT9ZEEN9x-OoFqixs-Gs9XXhJ3uUJzMerEJ-d3Lo4geWFzAMw@mail.gmail.com>
	<4a853592-a43b-b677-5026-212196b6e9c2@technowledgy.de>
	<fdb5eb66-8f31-4e45-7388-f83fdbaef507@technowledgy.de>
	<CAKFQuwY336Y0zQz-j=O-LXTMSDpxD2gB_ayiueMzCfkv5n6=vg@mail.gmail.com>

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 <walther@technowledgy.de>
> 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  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  Only you can decide what is important to you.

Attachments:

row_nulls.diff (text/x-diff, 2.5K) download 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) <returnvalue>PgPgPgPg</returnvalue> <literal>IS NULL</literal> and <literal>IS NOT NULL</literal> 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: + +<programlisting> +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 +</programlisting> + + In some cases, it may be preferable to write <replaceable>row</replaceable> <literal>IS DISTINCT FROM NULL</literal> or <replaceable>row</replaceable> <literal>IS NOT DISTINCT FROM NULL</literal>, 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)); <para> 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 - <literal>IS NULL</literal> or <literal>IS NOT NULL</literal>, for example: -<programlisting> -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 -</programlisting> - For more detail see <xref linkend="functions-comparisons"/>. - Row constructors can also be used in connection with subqueries, - as discussed in <xref linkend="functions-subquery"/>. + accepts a composite parameter. Also, it is possible to test rows + using the standard comparison operators as described in <xref + linkend="functions-comparison"/>, to compare one row against another + as described in <xref linkend="functions-comparisons"/>, and to + use them in connection with subqueries, as discussed in <xref + linkend="functions-subquery"/>, </para> </sect2>

view thread (9+ messages)  latest in thread

Message-ID: <ZULhh0y98-U67JRy@momjian.us>
Permalink:  ../ZULhh0y98-U67JRy@momjian.us/
Also on:    postgresql.org/message-id/ZULhh0y98-U67JRy@momjian.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bruce@momjian.us, david.g.johnston@gmail.com, walther@technowledgy.de
  Subject: Re: Wrong result for comparing ROW(...) with IS NOT NULL
  In-Reply-To: <ZULhh0y98-U67JRy@momjian.us>
  References: <21ff8e9c-627a-f949-fb00-a41b9ddcc9d3@technowledgy.de> <CAFj8pRAKiVUUFQawx70MXwd72rJ6D4UQFwAcmL5VVByE90HuYg@mail.gmail.com> <2ba24707-e7ba-2ccb-7269-71202a400a70@technowledgy.de> <CALT9ZEEN9x-OoFqixs-Gs9XXhJ3uUJzMerEJ-d3Lo4geWFzAMw@mail.gmail.com> <4a853592-a43b-b677-5026-212196b6e9c2@technowledgy.de> <fdb5eb66-8f31-4e45-7388-f83fdbaef507@technowledgy.de> <CAKFQuwY336Y0zQz-j=O-LXTMSDpxD2gB_ayiueMzCfkv5n6=vg@mail.gmail.com> <ZULhh0y98-U67JRy@momjian.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox