Re: Odd behavior in functions w/ anyarray & anyelement

From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Odd behavior in functions w/ anyarray & anyelement
Date: 2013-09-12 01:39:17
Message-ID: 1378949957010-5770555.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joshua Burns wrote
> CREATE OR REPLACE FUNCTION anyel_anyar(anyelement) RETURNS anyarray AS
> $BODY$
> BEGIN
> RETURN $1;
> END;
> $BODY$ LANGUAGE plpgsql;

Two possible interpretations:

1) must return an array of whatever type is supplied; this is (apparently)
the defined behavior
2) must return an array whose base type is the same as the base type of the
input

The first interpretation seems the most useful. Your last two examples for
this function should indeed fail.

That said I am not really sure why they have to. In theory "RETURN
anyarray", if paired with an array anyelement, could output/require an array
with one additional dimension compared to the input. In your example you
should expect something like:

3) ARRAY[ARRAY['one','two']::text[]]::text[][]

I guess this could be considered a third interpretation....

So the bigger question is: should PostgreSQL really care? Option #2 then
makes the fewest assumptions: the base types must match AND the output must
be some form of array.

And the biggest question is whether there are use-cases for the more
complex/flexible behavior so that someone may be enticed to implement it -
and consider the backward compatibility concerns.

Regardless, hopefully this aids your understanding.

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Odd-behavior-in-functions-w-anyarray-anyelement-tp5770537p5770555.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2013-09-12 01:51:13 Re: Odd behavior in functions w/ anyarray & anyelement
Previous Message ascot.moss@gmail.com 2013-09-12 01:11:21 fsync and wal_sync_method