Re: proposal: fix corner use case of variadic fuctions usage - Mailing list pgsql-hackers

From Tom Lane
Subject Re: proposal: fix corner use case of variadic fuctions usage
Date
Msg-id 21285.1358960284@sss.pgh.pa.us
Whole thread Raw
In response to Re: proposal: fix corner use case of variadic fuctions usage  (Pavel Stehule <pavel.stehule@gmail.com>)
Responses Re: proposal: fix corner use case of variadic fuctions usage
List pgsql-hackers
Pavel Stehule <pavel.stehule@gmail.com> writes:
> next related example

> CREATE OR REPLACE FUNCTION public.myleast(VARIADIC integer[])
>  RETURNS integer
>  LANGUAGE sql
> AS $function$
> select min(v) from unnest($1) g(v)
> $function$

The reason you get a null from that is that (1) unnest() produces zero
rows out for either a null or empty-array input, and (2) min() over
zero rows produces NULL.

In a lot of cases, it's not very sane for aggregates over no rows to
produce NULL; the best-known example is that SUM() produces NULL, when
anyone who'd not suffered brain-damage from sitting on the SQL committee
would have made it return zero.  So I'm not very comfortable with
generalizing from this specific case to decide that NULL is the
universally right result.
        regards, tom lane



pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: Prepared statements fail after schema changes with surprising error
Next
From: Andrew Dunstan
Date:
Subject: Re: [COMMITTERS] pgsql: Improve concurrency of foreign key locking