From: | David Johnston <polobo(at)yahoo(dot)com> |
---|---|
To: | pgsql-hackers(at)postgresql(dot)org |
Subject: | Re: array_length(anyarray) |
Date: | 2013-12-18 23:01:03 |
Message-ID: | 1387407663511-5783972.post@n5.nabble.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
Marko Tiikkaja-4 wrote
> On 2013-12-18 22:32, Andrew Dunstan wrote:
>> You're not really free to assume it - you'll need an exception handler
>> for the other-than-1 case, or your code might blow up.
>>
>> This seems to be codifying a bad pattern, which should be using
>> array_lower() and array_upper() instead.
>
> That's the entire point -- I *want* my code to blow up. If someone
> passes a multi-dimensional array to a function that assumes its input is
> one-dimensional and its indexes start from 1, I want it to be obvious
> that the caller did something wrong. Now I either copy-paste lines and
> lines of codes to always test for the weird cases or my code breaks in
> subtle ways.
>
> This is no different from an Assert() somewhere -- if the caller breaks
> the documented interface, it's his problem, not mine. And I don't want
> to waste my time coding around the fact that this simple thing is so
> hard to do in PG.
1) Why cannot we just make the second argument of the current function
optional and default to 1?
2) How about providing a function that returns the "1-dim/lower=1" input
array or raise/exception if the input array does not conform?
<not tested/psuedo-code>
CREATE FUNCTION array_normal(arr anyarray) RETURNS anyarray
$$
begin
if (empty(arr)) return arr;
if (ndim(arr) > 1) raise exception;
if (array_lower() <> 1) raise exception
return arr;
end;
$$
I can also see wanting 1-dimensional enforced without having to require the
lower-bound to be 1 so maybe a separate function for that.
Usage:
SELECT array_length(array_normal(input_array))
I could see this being especially useful for a domain and/or column
constraint definition and also allowing for a textbook case of separation of
concerns.
I am torn, but mostly opposed, to making an array_length(anyarray) function
with these limitations enforced - especially if other similar functions are
not created at the same time. I fully agree that array_length(anyarray)
should be a valid call without requiring the user to specify ", 1" by rote.
Tangential Question:
Is there any way to define a non-1-based array without using array-literal
syntax but by using ARRAY[1,2,3] syntax?
David J.
--
View this message in context: http://postgresql.1045698.n5.nabble.com/array-length-anyarray-tp5783950p5783972.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.
From | Date | Subject | |
---|---|---|---|
Next Message | Andres Freund | 2013-12-18 23:03:59 | Re: Assertion failure in base backup code path |
Previous Message | Andres Freund | 2013-12-18 22:54:46 | Re: preserving forensic information when we freeze |