Thread: Single-argument variant for array_length and friends?

Single-argument variant for array_length and friends?

From
Brendan Jurd
Date:
Hi hackers,

While I was working on my empty array patch I was frequently irritated
by the absence of an array_length(anyarray).  The same goes for
array_upper and array_lower.  Most of the time when I work with
arrays, they are 1-D, and it's inelegant to having to specify which
dimension I mean when there is only one to choose from.

The question I have (and would appreciate your input on) is how such
single-argument variants should behave when operating on an array with
multiple dimensions?

I see a few different options:

a) throw an error like "Dimension argument is required for multiple
dimension arrays"

b) assume the user means the final (innermost) dimension

c) assume the user means the first (outermost) dimension

My inclination is towards (c) but I don't use multi-D arrays in
postgres in the field.  Opinions?

Cheers,
BJ



Re: Single-argument variant for array_length and friends?

From
Tom Lane
Date:
Brendan Jurd <direvus@gmail.com> writes:
> While I was working on my empty array patch I was frequently irritated
> by the absence of an array_length(anyarray).  The same goes for
> array_upper and array_lower.  Most of the time when I work with
> arrays, they are 1-D, and it's inelegant to having to specify which
> dimension I mean when there is only one to choose from.

> The question I have (and would appreciate your input on) is how such
> single-argument variants should behave when operating on an array with
> multiple dimensions?

I'm not entirely convinced that this is a good idea, but if we're going
to allow it I would argue that "array_length(a)" should be defined as
"array_length(a, 1)".  The other possibilities are too complicated to
explain in as few words.
        regards, tom lane



Re: Single-argument variant for array_length and friends?

From
Pavel Stehule
Date:
2013/3/21 Tom Lane <tgl@sss.pgh.pa.us>:
> Brendan Jurd <direvus@gmail.com> writes:
>> While I was working on my empty array patch I was frequently irritated
>> by the absence of an array_length(anyarray).  The same goes for
>> array_upper and array_lower.  Most of the time when I work with
>> arrays, they are 1-D, and it's inelegant to having to specify which
>> dimension I mean when there is only one to choose from.
>
>> The question I have (and would appreciate your input on) is how such
>> single-argument variants should behave when operating on an array with
>> multiple dimensions?
>
> I'm not entirely convinced that this is a good idea, but if we're going
> to allow it I would argue that "array_length(a)" should be defined as
> "array_length(a, 1)".  The other possibilities are too complicated to
> explain in as few words.
>

exactly

+1

Pavel

>                         regards, tom lane
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers



Re: Single-argument variant for array_length and friends?

From
Brendan Jurd
Date:
On 21 March 2013 17:08, Pavel Stehule <pavel.stehule@gmail.com> wrote:
> 2013/3/21 Tom Lane <tgl@sss.pgh.pa.us>:
>> I'm not entirely convinced that this is a good idea, but if we're going
>> to allow it I would argue that "array_length(a)" should be defined as
>> "array_length(a, 1)".  The other possibilities are too complicated to
>> explain in as few words.
>>
>
> exactly
>
> +1

Hi Pavel,

Is your +1 to array_length(a) being defined as array_length(a,1), or
to Tom's being unconvinced by the whole proposal?  Or both?

Cheers,
BJ



Re: Single-argument variant for array_length and friends?

From
Pavel Stehule
Date:
2013/3/21 Brendan Jurd <direvus@gmail.com>:
> On 21 March 2013 17:08, Pavel Stehule <pavel.stehule@gmail.com> wrote:
>> 2013/3/21 Tom Lane <tgl@sss.pgh.pa.us>:
>>> I'm not entirely convinced that this is a good idea, but if we're going
>>> to allow it I would argue that "array_length(a)" should be defined as
>>> "array_length(a, 1)".  The other possibilities are too complicated to
>>> explain in as few words.
>>>
>>
>> exactly
>>
>> +1
>
> Hi Pavel,
>
> Is your +1 to array_length(a) being defined as array_length(a,1), or
> to Tom's being unconvinced by the whole proposal?  Or both?

If I though about it more, I like to more limit one parametric
array_length function just for only 1D array. So it is your A use
case. But I understand so this variant is not orthogonal. Hard to say,
what is better.

almost all operations are done on 1D arrays and for multidimensional
arrays I don't have a problem with more verbosity (it is better there
be more verbose).

Probably a most clean solution is Tom's proposal (from API
perspective) - with full ANSI/SQL SET implementation. So for 1D
operations user can use SET instead 1D arrays.

so Tom proposal is cleaner
your @a proposal is safer

any other opinions???

Regards

Pavel



>
> Cheers,
> BJ



Re: Single-argument variant for array_length and friends?

From
Brendan Jurd
Date:
On 21 March 2013 17:32, Pavel Stehule <pavel.stehule@gmail.com> wrote:
> If I though about it more, I like to more limit one parametric
> array_length function just for only 1D array. So it is your A use
> case. But I understand so this variant is not orthogonal. Hard to say,
> what is better.
>

Yes, for me (a) is running a very close 2nd place to (c).  The
strength of (a) is it means we aren't making guesses about the user's
intention.  When a user concocts an expression that is ambiguous, I
feel it is usually good to kick it back to them and ask them to be
more precise.

On the other hand, I find it very natural to interpret "what is the
length of my multidim array" to mean "what is the length of the
outermost dimension of my multidim array", because to me a multidim
array is just an array that contains more arrays.

Cheers,
BJ



Re: Single-argument variant for array_length and friends?

From
Pavel Stehule
Date:
2013/3/21 Brendan Jurd <direvus@gmail.com>:
> On 21 March 2013 17:32, Pavel Stehule <pavel.stehule@gmail.com> wrote:
>> If I though about it more, I like to more limit one parametric
>> array_length function just for only 1D array. So it is your A use
>> case. But I understand so this variant is not orthogonal. Hard to say,
>> what is better.
>>
>
> Yes, for me (a) is running a very close 2nd place to (c).  The
> strength of (a) is it means we aren't making guesses about the user's
> intention.  When a user concocts an expression that is ambiguous, I
> feel it is usually good to kick it back to them and ask them to be
> more precise.
>
> On the other hand, I find it very natural to interpret "what is the
> length of my multidim array" to mean "what is the length of the
> outermost dimension of my multidim array", because to me a multidim
> array is just an array that contains more arrays.

lot of postgresql functions calculate with all items in array without
respect to dimensions - like unnest.

so concept "use outermost dim" is not in pg now, and should not be
introduced if it is possible. More it goes against a verbosity concept
introduced by ADA and reused in PL/SQL and PL/pgSQL.

regards

Pavel

>
> Cheers,
> BJ



Re: Single-argument variant for array_length and friends?

From
Merlin Moncure
Date:
On Thu, Mar 21, 2013 at 2:00 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:
> 2013/3/21 Brendan Jurd <direvus@gmail.com>:
>> On 21 March 2013 17:32, Pavel Stehule <pavel.stehule@gmail.com> wrote:
>>> If I though about it more, I like to more limit one parametric
>>> array_length function just for only 1D array. So it is your A use
>>> case. But I understand so this variant is not orthogonal. Hard to say,
>>> what is better.
>>>
>>
>> Yes, for me (a) is running a very close 2nd place to (c).  The
>> strength of (a) is it means we aren't making guesses about the user's
>> intention.  When a user concocts an expression that is ambiguous, I
>> feel it is usually good to kick it back to them and ask them to be
>> more precise.
>>
>> On the other hand, I find it very natural to interpret "what is the
>> length of my multidim array" to mean "what is the length of the
>> outermost dimension of my multidim array", because to me a multidim
>> array is just an array that contains more arrays.
>
> lot of postgresql functions calculate with all items in array without
> respect to dimensions - like unnest.
>
> so concept "use outermost dim" is not in pg now, and should not be
> introduced if it is possible. More it goes against a verbosity concept
> introduced by ADA and reused in PL/SQL and PL/pgSQL.

and pl/psm*

merlin



Re: Single-argument variant for array_length and friends?

From
Brendan Jurd
Date:
On 22 March 2013 09:12, Merlin Moncure <mmoncure@gmail.com> wrote:
> On Thu, Mar 21, 2013 at 2:00 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:
>> lot of postgresql functions calculate with all items in array without
>> respect to dimensions - like unnest.
>>
>> so concept "use outermost dim" is not in pg now, and should not be
>> introduced if it is possible. More it goes against a verbosity concept
>> introduced by ADA and reused in PL/SQL and PL/pgSQL.
>
> and pl/psm*

Yeah, okay.  That argument works for me.  Let's go for option (a),
only allow the user to omit the dimension argument if the array is
1-D.

We still have the issue that Tom isn't convinced that the feature is
worth pursuing -- Tom, would you please elaborate a little on what you
dislike about it?  I don't see much of a downside (just 3 extra
pg_procs).

Cheers,
BJ