Re: Bug or strange result of Max() on arrays containing NULL values

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Plettenbacher, Tobias (LWF)" <Tobias(dot)Plettenbacher(at)lwf(dot)bayern(dot)de>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: Bug or strange result of Max() on arrays containing NULL values
Date: 2024-08-24 14:54:48
Message-ID: 2411054.1724511288@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

"Plettenbacher, Tobias (LWF)" <Tobias(dot)Plettenbacher(at)lwf(dot)bayern(dot)de> writes:
> With Max(ARRAY[]) I get a strange result (in this case {NULL,7}):
> SELECT Max(ARRAY[Val, ID]) FROM (VALUES (1, 5), (2, 6), (NULL, 7)) AS T(Val, ID);

This is the expected result, because

=# select array[null, 7] > array[2, 6];
?column?
----------
t
(1 row)

When comparing array elements (or members of any container type),
we treat two nulls as equal and a null as larger than any non-null.
You might think that such a comparison should yield null, but if
we did that then the comparisons would fail to provide a total
order for the container type. That would, among other things,
break the ability to build b-tree indexes on such types.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message David G. Johnston 2024-08-24 15:28:27 Re: Bug or strange result of Max() on arrays containing NULL values
Previous Message Plettenbacher, Tobias (LWF) 2024-08-24 12:54:26 Bug or strange result of Max() on arrays containing NULL values