Re: How do I create an array?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Greg Stark <gsstark(at)mit(dot)edu>, pgsql-general(at)postgresql(dot)org
Subject: Re: How do I create an array?
Date: 2003-02-06 16:37:03
Message-ID: 12999.1044549423@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joe Conway <mail(at)joeconway(dot)com> writes:
> It seems like you should be able to define the function:
> CREATE OR REPLACE FUNCTION array (any)
> RETURNS anyarray
> AS '$libdir/plr','array'
> LANGUAGE 'C' WITH (isstrict);

You could write that definition, but the implementation could be hard:
I don't think there's any way for the function to know what datatype
it's been passed.

>> This morning I was musing about overloading the CAST syntax to allow
>> array construction, along the lines of
>> [snip]

> From SQL99
> <array value expression> ::= <array value constructor>
> | <array concatenation>
> | <value expression primary>

Oh cool, I had not thought to look in SQL99. Clearly we should lift
their syntax.

> and in section 6.4 SQL99 indicates that the array datatype should be derived
> from the datatype of its first element (again, not sure I'm reading the spec
> correctly):

Whether you are or not, I'd be inclined to ignore that and instead use
UNION's algorithm for deriving the union type. Consider

ARRAY [ 1, 1.1, 1.4e10 ]

You don't want it to type that as integer[], IMHO.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ian Harding 2003-02-06 16:44:28 Re: how to determine OID of the row I just
Previous Message Dennis Gearon 2003-02-06 16:35:50 Re: Pg_dumpall problem