From: | Marko Tiikkaja <marko(at)joh(dot)to> |
---|---|
To: | David Johnston <polobo(at)yahoo(dot)com>, pgsql-hackers(at)postgresql(dot)org |
Subject: | Re: array_length(anyarray) |
Date: | 2013-12-18 23:14:08 |
Message-ID: | 52B22C40.1070200@joh.to |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
On 12/19/13, 12:01 AM, David Johnston wrote:
> 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?
That still does the wrong thing for the empty array, multidimensional
arrays and arrays that don't start from index 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;
> $$
With this, I would still have to do
COALESCE(array_length(array_normal($1), 1), 0). That's pretty stupid
for the most common use case of arrays, don't you think?
> 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.
I really don't see the point. How often have you ever created a
function that doesn't have a lower bound of 1 on purpose? What good did
it serve you?
> 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.
What would array_length() in this case be? With what you suggested
above, you would still get NULL for an empty array.
> 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.
I'm specifically asking for something that is different from
array_length(anyarray, int), because I personally think it's too full of
caveats.
Regards,
Marko Tiikkaja
From | Date | Subject | |
---|---|---|---|
Next Message | Kevin Grittner | 2013-12-18 23:39:56 | Re: [PATCH] SQL assertions prototype |
Previous Message | Bruce Momjian | 2013-12-18 23:12:59 | Re: stats for network traffic WIP |