Re: doc: array_length produces null instead of 0

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Aleksander Alekseev <aleksander(at)timescale(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: doc: array_length produces null instead of 0
Date: 2022-06-21 16:02:41
Message-ID: CAKFQuwbH4P52zVst2Ri_t+nhy+sgdJDEwQ_MMRPsrtji1VwvAQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Jun 21, 2022 at 6:33 AM Aleksander Alekseev <
aleksander(at)timescale(dot)com> wrote:

> Hi David,
>
> > Per discussion here:
> >
> >
> https://www.postgresql.org/message-id/163636931138.8076.5140809232053731248%40wrigleys.postgresql.org
> >
> > We can now easily document the array_length behavior of returning null
> instead of zero for an empty array/dimension.
> >
> > I added an example to the json_array_length function to demonstrate that
> it does return 0 as one would expect, but contrary to the SQL array
> behavior.
> >
> > I did not bother to add examples to the other half dozen or so "_length"
> functions that all produce 0 as expected. Just the surprising case and the
> adjacent one.
>
> Good catch.
>
> + <literal>array_length(array[], 1)</literal>
> + <returnvalue>NULL</returnvalue>
>
> One tiny nitpick I have is that this example will not work if used
> literally, as is:
>
> ```
> =# select array_length(array[], 1);
> ERROR: cannot determine type of empty array
> LINE 1: select array_length(array[], 1);
> ```
>
> Maybe it's worth using `array_length(array[] :: int[], 1)` instead.
>
>
I think subconsciously the cast looked ugly to me so I probably skipped
adding it. I do agree the example should be executable though, and most of
the existing examples use integer[] (not the abbreviated form, int) so I'll
plan to go with that.

Thanks for the review!

David J.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David G. Johnston 2022-06-21 16:07:42 Re: doc: Bring mention of unique index forced transaction wait behavior outside of the internal section
Previous Message Przemysław Sztoch 2022-06-21 14:55:40 Re: generate_series for timestamptz and time zone problem