Re: Implicit casts to array types

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: joshua <jzuellig(at)arbormetrix(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Implicit casts to array types
Date: 2012-12-14 15:24:27
Message-ID: CAHyXU0xBonSysRB1bacA+XfTUDftZ0_PmJwu3W5toN2AvsT5pw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Dec 14, 2012 at 9:16 AM, joshua <jzuellig(at)arbormetrix(dot)com> wrote:
> I'm trying to create an implicit cast from an unknown type into a text array
> type by creating a simple array of size 1. e.g.
>
> create function textarray(unknown)
> returns text[]
> as 'select ARRAY[$1::text];'
> language sql
> immutable;
>
> create cast (unknown as text[]) with function textarray(unknown) as
> implicit;
>
> However, when I try to use this, the planner doesn't use the implicit cast.
> Instead it still tries to cast 'a' directly to a text[] and complains that
> it's not formatted as '{a}' (ERROR: array value must start with "{" or
> dimension information)
> I added an additional parallel cast from text to text[]:

create function textarray(anyelement)
returns text[]
as 'select ARRAY[$1::text];'
language sql
immutable;

(emphasis on 'anyelement')...get rid of the cast. use 'any' type
arguments for polymorphic functions, that is when you want them to
operate over wide range of input types. hacking casts is almost never
a good idea.

merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2012-12-14 15:30:14 Re: Read recover rows
Previous Message James B. Byrne 2012-12-14 15:21:01 Re: Problem starting PG-9.2 on non-default port