array_length()

Started by Jim Nasbyover 17 years ago13 messageshackers
Jump to latest
#1Jim Nasby
Jim.Nasby@BlueTreble.com

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.

Attachments:

array_length.patchapplication/octet-stream; name=array_length.patch; x-unix-mode=0640Download+25-0
smime.p7sapplication/pkcs7-signature; name=smime.p7sDownload
#2Peter Eisentraut
peter_e@gmx.net
In reply to: Jim Nasby (#1)
Re: array_length()

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.

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Peter Eisentraut (#2)
Re: array_length()

Hello

2008/11/5 Peter Eisentraut <peter_e@gmx.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

Show quoted text

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

#4Peter Eisentraut
peter_e@gmx.net
In reply to: Pavel Stehule (#3)
Re: array_length()

Pavel Stehule wrote:

Hello

2008/11/5 Peter Eisentraut <peter_e@gmx.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.

#5Pavel Stehule
pavel.stehule@gmail.com
In reply to: Peter Eisentraut (#4)
Re: array_length()

2008/11/5 Peter Eisentraut <peter_e@gmx.net>:

Pavel Stehule wrote:

Hello

2008/11/5 Peter Eisentraut <peter_e@gmx.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

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#2)
Re: array_length()

Peter Eisentraut <peter_e@gmx.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

#7Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#6)
Re: array_length()

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

Attachments:

array_length.patchtext/x-diff; name=array_length.patchDownload+55-0
#8Robert Haas
robertmhaas@gmail.com
In reply to: Robert Haas (#7)
Re: array_length()

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

#9Robert Haas
robertmhaas@gmail.com
In reply to: Robert Haas (#7)
Re: array_length()

Updated version attached, this time without the compiler warning.

Sorry for the sloppy work.

...Robert

Attachments:

array_length-v2.patchtext/x-diff; name=array_length-v2.patchDownload+54-0
#10Jim Nasby
Jim.Nasby@BlueTreble.com
In reply to: Peter Eisentraut (#2)
Re: array_length()

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@decibel.org
Give your computer some brain candy! www.distributed.net Team #1828

#11Peter Eisentraut
peter_e@gmx.net
In reply to: Robert Haas (#9)
Re: array_length()

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.

#12Robert Haas
robertmhaas@gmail.com
In reply to: Peter Eisentraut (#11)
Re: array_length()

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

...Robert

Show quoted text

On Wed, Nov 12, 2008 at 8:13 AM, Peter Eisentraut <peter_e@gmx.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.

#13Peter Eisentraut
peter_e@gmx.net
In reply to: Robert Haas (#12)
Re: array_length()

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.

Show quoted text

...Robert

On Wed, Nov 12, 2008 at 8:13 AM, Peter Eisentraut <peter_e@gmx.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.