Re: array_length()

Lists: pgsql-hackers
From: Decibel! <decibel(at)decibel(dot)org>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: array_length()
Date: 2008-10-15 14:28:43
Message-ID: 20952F54-6730-49D8-99A3-1834697790B5@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

ISTM it'd be useful to have an array_length function (since I just
wrote one for work ;), so here's a patch. Note that I don't have the
docs toolchain setup, so I wasn't able to test the doc patches.

Attachment Content-Type Size
array_length.patch application/octet-stream 2.8 KB
unknown_filename text/plain 143 bytes

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: "Decibel!" <decibel(at)decibel(dot)org>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-05 13:00:43
Message-ID: 491198FB.7090504@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Decibel! wrote:
> ISTM it'd be useful to have an array_length function (since I just wrote
> one for work ;), so here's a patch. Note that I don't have the docs
> toolchain setup, so I wasn't able to test the doc patches.

There is a tiny problem with this implementation: It returns null for an
empty array, not zero. This is because array_lower and/or array_upper
return null for an empty array, which makes sense for those cases. We
could fix this by putting a coalesce around the expression, but since
the array functions return null for all kinds of error cases, this might
mask other problems. Or we move to a C implementation.


From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "Peter Eisentraut" <peter_e(at)gmx(dot)net>
Cc: Decibel! <decibel(at)decibel(dot)org>, "Pg Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-05 13:14:28
Message-ID: 162867790811050514y7dfca722oce4cd44ea5e76a4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello

2008/11/5 Peter Eisentraut <peter_e(at)gmx(dot)net>:
> Decibel! wrote:
>>
>> ISTM it'd be useful to have an array_length function (since I just wrote
>> one for work ;), so here's a patch. Note that I don't have the docs
>> toolchain setup, so I wasn't able to test the doc patches.
>
> There is a tiny problem with this implementation: It returns null for an
> empty array, not zero. This is because array_lower and/or array_upper
> return null for an empty array, which makes sense for those cases. We could
> fix this by putting a coalesce around the expression, but since the array
> functions return null for all kinds of error cases, this might mask other
> problems. Or we move to a C implementation.
>

we should to write function isempty(anyarray), that returns true when
param is empty.

regards
Pavel Stehule

>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: "Decibel!" <decibel(at)decibel(dot)org>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-05 14:12:52
Message-ID: 4911A9E4.8090208@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavel Stehule wrote:
> Hello
>
> 2008/11/5 Peter Eisentraut <peter_e(at)gmx(dot)net>:
>> Decibel! wrote:
>>> ISTM it'd be useful to have an array_length function (since I just wrote
>>> one for work ;), so here's a patch. Note that I don't have the docs
>>> toolchain setup, so I wasn't able to test the doc patches.
>> There is a tiny problem with this implementation: It returns null for an
>> empty array, not zero. This is because array_lower and/or array_upper
>> return null for an empty array, which makes sense for those cases. We could
>> fix this by putting a coalesce around the expression, but since the array
>> functions return null for all kinds of error cases, this might mask other
>> problems. Or we move to a C implementation.
>>
>
> we should to write function isempty(anyarray), that returns true when
> param is empty.

Well, isn't isempty() just a special case of array_length()? One or the
other needs to be implemented, so we might as well go for the general
case, IMO.


From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "Peter Eisentraut" <peter_e(at)gmx(dot)net>
Cc: Decibel! <decibel(at)decibel(dot)org>, "Pg Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-05 14:24:36
Message-ID: 162867790811050624o164ca7e9n81134ec86b8e3894@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2008/11/5 Peter Eisentraut <peter_e(at)gmx(dot)net>:
> Pavel Stehule wrote:
>>
>> Hello
>>
>> 2008/11/5 Peter Eisentraut <peter_e(at)gmx(dot)net>:
>>>
>>> Decibel! wrote:
>>>>
>>>> ISTM it'd be useful to have an array_length function (since I just wrote
>>>> one for work ;), so here's a patch. Note that I don't have the docs
>>>> toolchain setup, so I wasn't able to test the doc patches.
>>>
>>> There is a tiny problem with this implementation: It returns null for an
>>> empty array, not zero. This is because array_lower and/or array_upper
>>> return null for an empty array, which makes sense for those cases. We
>>> could
>>> fix this by putting a coalesce around the expression, but since the array
>>> functions return null for all kinds of error cases, this might mask other
>>> problems. Or we move to a C implementation.
>>>
>>
>> we should to write function isempty(anyarray), that returns true when
>> param is empty.
>
> Well, isn't isempty() just a special case of array_length()? One or the
> other needs to be implemented, so we might as well go for the general case,
> IMO.
>

sure, but I believe so 90% of using array_length will be test of emty array.

Pavel


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: "Decibel!" <decibel(at)decibel(dot)org>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-05 15:15:17
Message-ID: 14206.1225898117@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> There is a tiny problem with this implementation: It returns null for an
> empty array, not zero. This is because array_lower and/or array_upper
> return null for an empty array, which makes sense for those cases. We
> could fix this by putting a coalesce around the expression, but since
> the array functions return null for all kinds of error cases, this might
> mask other problems. Or we move to a C implementation.

Basic functionality like this shouldn't be implemented as a SQL function
anyway. People don't expect that some built-in functions should be
several orders of magnitude slower than other built-in functions of
apparently similar complexity.

regards, tom lane


From: "Robert Haas" <robertmhaas(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Peter Eisentraut" <peter_e(at)gmx(dot)net>, Decibel! <decibel(at)decibel(dot)org>, "Pg Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-07 03:51:23
Message-ID: 603c8f070811061951u16034c3fk5dfaa493a6739a24@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

>> There is a tiny problem with this implementation: It returns null for an
>> empty array, not zero. This is because array_lower and/or array_upper
>> return null for an empty array, which makes sense for those cases. We
>> could fix this by putting a coalesce around the expression, but since
>> the array functions return null for all kinds of error cases, this might
>> mask other problems. Or we move to a C implementation.

Hmm... the problem is that an empty array is really zero-dimensional.
So for what values of the second argument ought we to return 0?

It certainly seems inconsistent to say that array_length({}, 6) = 0
and array_length({1}, 6) is null.

We do need a good way to test for an empty array, though. Right now I
think the best ways is array_ndims(x) IS NULL (should it return 0
rather than NULL on an empty array?).

> Basic functionality like this shouldn't be implemented as a SQL function
> anyway. People don't expect that some built-in functions should be
> several orders of magnitude slower than other built-in functions of
> apparently similar complexity.

C implementation attached.

...Robert

Attachment Content-Type Size
array_length.patch text/x-diff 4.7 KB

From: "Robert Haas" <robertmhaas(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Peter Eisentraut" <peter_e(at)gmx(dot)net>, Decibel! <decibel(at)decibel(dot)org>, "Pg Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-07 03:56:13
Message-ID: 603c8f070811061956m1f17eef2p9ecfc7d0d1011ad2@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

> Hmm... the problem is that an empty array is really zero-dimensional.
> So for what values of the second argument ought we to return 0?
>
> It certainly seems inconsistent to say that array_length({}, 6) = 0
> and array_length({1}, 6) is null.

Ugh. I meant rather:

It certainly seems inconsistent to say that array_length({}, 6) = 0
and array_upper({1}, 6) is null.

...Robert


From: "Robert Haas" <robertmhaas(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Peter Eisentraut" <peter_e(at)gmx(dot)net>, Decibel! <decibel(at)decibel(dot)org>, "Pg Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-07 04:06:17
Message-ID: 603c8f070811062006q4f20b2bq179d4d2ffec65690@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Updated version attached, this time without the compiler warning.

Sorry for the sloppy work.

...Robert

Attachment Content-Type Size
array_length-v2.patch text/x-diff 4.7 KB

From: Decibel! <decibel(at)decibel(dot)org>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-10 00:31:45
Message-ID: 4DCC69BA-ECEE-48B8-9F09-20DCA09A7C56@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Nov 5, 2008, at 7:00 AM, Peter Eisentraut wrote:
> There is a tiny problem with this implementation: It returns null
> for an empty array, not zero. This is because array_lower and/or
> array_upper return null for an empty array, which makes sense for
> those cases. We could fix this by putting a coalesce around the
> expression, but since the array functions return null for all kinds
> of error cases, this might mask other problems.

What other error conditions? If we hit a real error, we should throw
an error.

Granted, there is some debate possible about what referencing an un-
defined dimension means, but I can't see how the results of that
should vary between array_length and array_lower/upper.

Is there some other corner case?
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Decibel!" <decibel(at)decibel(dot)org>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-12 13:13:57
Message-ID: 491AD695.7020804@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas wrote:
> Updated version attached, this time without the compiler warning.

I have committed something based on this. The issue of empty arrays
will need a separate solution.


From: "Robert Haas" <robertmhaas(at)gmail(dot)com>
To: "Peter Eisentraut" <peter_e(at)gmx(dot)net>
Cc: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Decibel! <decibel(at)decibel(dot)org>, "Pg Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-12 14:37:39
Message-ID: 603c8f070811120637q731da6fbqf9076681e384f349@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hmm, ISTM that cardinality() is implemented here in the manner
previously rejected for array_length()...

...Robert

On Wed, Nov 12, 2008 at 8:13 AM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
> Robert Haas wrote:
>>
>> Updated version attached, this time without the compiler warning.
>
> I have committed something based on this. The issue of empty arrays will
> need a separate solution.
>


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Decibel!" <decibel(at)decibel(dot)org>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length()
Date: 2008-11-12 15:56:57
Message-ID: 491AFCC9.9040609@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas wrote:
> Hmm, ISTM that cardinality() is implemented here in the manner
> previously rejected for array_length()...

The objection was that basic functionality should not be implemented in
SQL. If we want to disallow all compatibility functions implemented in
SQL as well, we have more work to do.

>
> ...Robert
>
> On Wed, Nov 12, 2008 at 8:13 AM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
>> Robert Haas wrote:
>>> Updated version attached, this time without the compiler warning.
>> I have committed something based on this. The issue of empty arrays will
>> need a separate solution.
>>
>