Re: variadic flag doesn't work with "any" type

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: variadic flag doesn't work with "any" type
Date: 2010-12-10 07:55:32
Message-ID: AANLkTi=L2LrEc+ohLVGs=4yHkes7yF3sejP4-m1nVi-H@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

2010/12/9 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
> Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
>> I didn't explain it well, sorry
>
>> so I have a function foo(variadic "any")
>
>> usual calling like foo(10,20) or foo('a',10) working perfectly. But I
>> have a problem with call with VARIADIC keyword
>
>> like foo(VARIADIC ARRAY[10,20]) or foo(VARIADIC ARRAY['a','10']).
>
>> Keyword VARIADIC is allowed, and this a calling doesn't raise any
>> error.
>
> What exactly is the use-case for that?  You can't expect that an array
> will hold the parameter list, since the parameters might not be all the
> same type.
>

I am working on function that can help with record updating. It's
based on polymorphic types. I would to allow a multiple modification
per one call - like UPDATE statement does.

some like:

record_set_fields(anyelement, key text, value "any" [, key text, value
"any" [..]]) returns anyelement

because we cannot work with pairs of params I designed interface

CREATE FUNCTION record_set_fields(anyelement, VARIADIC "any") RETURNS anyelement

most often case working well:

SELECT record_set_fields(row(0,0,0,0,0,0), 'f1', 10, 'f2', 20, 'f3', 30)

I prefer "any" type because a user must not use a quotes for values -
and I am able to do late conversion based on record tuple desc. But
sometimes can be interesting to use a VARIADIC value - so list of
pairs (key, value) can be created dynamically - (now I don't talk if
this is good way or not). And because we have a VARIADIC value for
VARIADIC parameter, it should be no problem - It works for non "any"
types now.

I would to do some like:

DECLARE change_set text[];
BEGIN
change_set = ARRAY(SELECT CASE WHEN i % 2 = 1 THEN 'f' || (i / 2)
ELSE (-100)::text END FROM generate_series(1,10) g(i));
-- change_set = {f0,-100,f1,-100,f2,-100,f3,-100,f4,-100}
NEW := record_set_fields(NEW, VARIADIC change_set);
...

RETURN NEW;

but NEW := record_set_fields(NEW, VARIADIC change_set) doesn't work
now - because keyword VARIADIC hasn't necessary effect for "any" type.
It's just quietly ignored. Isn't a problem to expand array inside
custom function - but I don't have a info if VARIADIC keyword was used
or not.

From some custom function's perspective there isn't difference between
(when "any" type is used):

SELECT foo(VARIADIC arrayval) and SELECT foo(arrayval).

I don't know when I have to raise exception and when I have to expand array.

Regards

Pavel

p.s. I found a workaroud - but it's a workaround and needs twice code.
I have to define second record_set_fields(anyelement, VARIADIC
anyarray), and this variant is chosen when VARIADIC value is used. But
I afraid about stability of this solution.

>>              -------- CALL foo(VARIADIC ARRAY[10,20,20]) ---> real
>> call foo(10,20,20) -- but it doesn't work now.
>
> I'm not convinced it should work that way.  Even if you had convinced me
> that this was sensible and had a real use-case, making it work like that
> would take a whole bunch of mechanism that doesn't exist.
>
>                        regards, tom lane
>

Attachment Content-Type Size
pstcoll.tgz application/x-gzip 12.2 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2010-12-10 19:26:06 Re: variadic flag doesn't work with "any" type
Previous Message nobuyuki yoshida 2010-12-10 02:41:38 BUG #5788: I can drop table , with other user's table