Array behavior oddities

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: PostgreSQL-development <pgsql-hackers(at)postgreSQL(dot)org>
Subject: Array behavior oddities
Date: 2008-01-15 04:31:06
Message-ID: 200801150431.m0F4V6010850@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

In reading our array documentation I came across two unusual behaviors.
The issue relates to slices:

We can also access arbitrary rectangular slices of an array, or
subarrays. An array slice is denoted by writing
<literal><replaceable>lower-bound</replaceable>:<replaceable>upper-bound</replaceable></literal>
for one or more array dimensions. For example, this query retrieves
the first item on Bill's schedule for the first two days of the week:

SELECT schedule[1:2][1:1] FROM sal_emp WHERE name = 'Bill';

First issue:

If any dimension is written as a slice, i.e. contains a colon, then all
dimensions are treated as slices. Any dimension that has only a single
number (no colon) is treated as being from <literal>1</> to the number
specified. For example, <literal>[2]</> is treated as <literal>[1:2]</>,
as in this example:

Is the the behavior of assuming an entry with no colon is a slice what
we want, or are we just stuck with it?

Also:

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 <literal>schedule</> currently has the
dimensions <literal>[1:3][1:2]</> then referencing
<literal>schedule[3][3]</> yields NULL. Similarly, an array reference
with the wrong number of subscripts yields a null rather than an error.

An array slice expression likewise yields null if the array itself or
any of the subscript expressions are null. However, in other corner
cases such as selecting an array slice that is completely outside the
current array bounds, a slice expression yields an empty
(zero-dimensional) array instead of null. If the requested slice
partially overlaps the array bounds, then it is silently reduced to just
the overlapping region.

Is there a reason out-of-bounds array accesses behave differently for
slices and non-slices?

Having slices and non-slices behave differently is very confusing to me.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://postgres.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2008-01-15 04:35:30 Re: SSL over Unix-domain sockets
Previous Message Bruce Momjian 2008-01-15 04:14:29 Re: SSL over Unix-domain sockets