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

From: "Nikolay Samokhvalov" <nikolay(at)samokhvalov(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: "select ('{}'::text[])[1]" returns NULL -- is it correct?
Date: 2007-04-10 07:10:15
Message-ID: e431ff4c0704100010l1f7a611laca247ecdbf9bf59@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 4/9/07, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> "Nikolay Samokhvalov" <samokhvalov(at)gmail(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Koichi Suzuki 2007-04-10 07:23:51 Re: [HACKERS] Full page writes improvement, code update
Previous Message Anu Gupta DCSA 2007-04-10 05:21:15 A Survey on Defect Management Practices in Free/Open Source Software