Re: "select ('{}'::text[])[1]" returns NULL -- is it correct? - Mailing list pgsql-hackers

From Nikolay Samokhvalov
Subject Re: "select ('{}'::text[])[1]" returns NULL -- is it correct?
Date
Msg-id e431ff4c0704100010l1f7a611laca247ecdbf9bf59@mail.gmail.com
Whole thread Raw
In response to Re: "select ('{}'::text[])[1]" returns NULL -- is it correct?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
On 4/9/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> "Nikolay Samokhvalov" <samokhvalov@gmail.com> writes:
> > As I can see here, when I ask for element that doesn't exist, the
> > database returns NULL for me. Maybe it's well-known issue (and
> > actually I understood this behaviour before), but strictly speaking it
> > seems wrong for me: the database _knows_ that there is no element, so
> > why NULL?
> [...]
>
> AFAIR it's always been like that, so changing it seems exceedingly
> likely to break some peoples' applications.  It's not completely without
> analogies in SQL, anyway: consider the behavior of INSERT when fewer
> columns are provided than the table has.  Pretending that elements
> outside the stored range of the array are null is not all that different
> from silently adding nulls to a row-to-be-stored.

OK, I see.
But if I try to INSERT to column that doesn't exist in the table, I
have an error.
Why pg's arrays are designed so that postgres doesn't produce errors
for attempts to access nonexistent element of array? Why there is no
simple sanity check (SELECT (ARRAY[6,8])[-1] -- works w/o an error)? I
remember several cases when people (e.g. me :-) ) were spending some
time trying to find an error in some pl/pgsql function and the reason
lied in incorrect work with arrays (i.e. messages like "index is out
of bounds" and "index cannot be negative number" would help, surely).

-- 
Best regards,
Nikolay


pgsql-hackers by date:

Previous
From: "Nikolay Samokhvalov"
Date:
Subject: Re: "select ('{}'::text[])[1]" returns NULL -- is it correct?
Next
From: Neil Conway
Date:
Subject: Re: [DOCS] uuid type not documented