From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | nikolay(at)samokhvalov(dot)com |
Cc: | pgsql-hackers(at)postgresql(dot)org |
Subject: | Re: "select ('{}'::text[])[1]" returns NULL -- is it correct? |
Date: | 2007-04-09 15:18:23 |
Message-ID: | 28240.1176131903@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
"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?
This is operating as designed, per
http://www.postgresql.org/docs/8.2/static/arrays.html
: An array subscript expression will return null if either the array
: itself or any of the subscript expressions are null. Also, null is
: returned if a subscript is outside the array bounds (this case does not
: raise an error). For example, if schedule currently has the dimensions
: [1:3][1:2] then referencing schedule[3][3] yields NULL. Similarly, an
: array reference with the wrong number of subscripts yields a null rather
: than an error.
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.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2007-04-09 16:10:47 | Adjusting index special storage for pg_filedump's convenience |
Previous Message | Nikolay Samokhvalov | 2007-04-09 14:59:01 | "select ('{}'::text[])[1]" returns NULL -- is it correct? |