Skip site navigation (1) Skip section navigation (2)

RfD: more powerful "any" types

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: RfD: more powerful "any" types
Date: 2009-09-08 16:12:10
Message-ID: 20090908161210.GD549@alvh.no-ip.org (view raw or flat)
Thread:
Lists: pgsql-hackers
Hi,

I'm seeking more powerful "any" pseudotypes.  In particular, consider a
function

foo(type1, type2) returns type3

where type1 and type2 can be both any element type, but not necessarily
both of the same type.  Currently this cannot be made to work: you could
try to write the function this way:

foo(anyelement, anyelement) returns anyelement

but this will force them to be both of the same type, which is not what
we want.  In my opinion this is a missing feature of our type system.

One seemingly trivial idea would be to have anyelement2, anyelement3,
and so on.  This is not a very good solution, because we'd have to fill
the catalogs with a large bunch of new pseudotypes, and the code with a
bunch of hardcoded tests -- and there are already 27 cases of
ANYELEMENTOID in our code.


For a practical example, I am trying to write a function that returns
how many NULL arguments it has (this is useful in table CHECK
constraints).  One simple idea is to use type "unknown":

CREATE FUNCTION a (unknown, unknown, unknown) RETURNS INT LANGUAGE plpgsql AS $$
DECLARE
   count int = 0;
BEGIN
   IF $1 IS NULL THEN count = count + 1; END IF;
   IF $2 IS NULL THEN count = count + 1; END IF;
   if $3 IS NULL THEN count = count + 1; END IF;
   RETURN count;
END $$;

The problem is that it doesn't work.  This trivial query does:

alvherre=# select a(null, '2', null);
 a 
---
 2
(1 fila)

But this one does not:

alvherre=# select a(null, 2, null);
ERROR:  function a(unknown, integer, unknown) does not exist

and you cannot cast the integer:
alvherre=# select a(null, 2::unknown, null);
ERROR:  cannot cast type integer to unknown


Before I spend time trying to figure out how this works,
1. is there agreement that this is a problem and needs fixed, and
2. does anybody have an idea how to attack it?

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

Responses

pgsql-hackers by date

Next:From: Alvaro HerreraDate: 2009-09-08 16:13:23
Subject: Re: Patch: update Bonjour support to the newer non-deprecated API
Previous:From: Tom LaneDate: 2009-09-08 16:11:55
Subject: Re: Patch: update Bonjour support to the newer non-deprecated API

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group