proposal sql: labeled function params

From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "PostgreSQL-development Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: proposal sql: labeled function params
Date: 2008-08-14 09:56:58
Message-ID: 162867790808140256w533324bj25183d476d3aa89a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello

I propose enhance current syntax that allows to specify label for any
function parameter:

fcename(expr [as label], ...)
fcename(colname, ...)

I would to allow same behave of custom functions like xmlforest function:
postgres=# select xmlforest(a) from foo;
xmlforest
-----------
<a>10</a>
(1 row)

postgres=# select xmlforest(a as b) from foo;
xmlforest
-----------
<b>10</b>
(1 row)

Actually I am not sure what is best way for PL languages for acces to
these info. Using some system variables needed new column in pg_proc,
because collecting these needs some time and in 99% cases we don't
need it. So I prefere some system function that returns labels for
outer function call. Like

-- test
create function getlabels() returns varchar[] as $$select '{name,
age}'::varchar[]$$ language sql immutable;

create or replace function json(variadic varchar[])
returns varchar as $$
select '[' || array_to_string(
array(
select (getlabels())[i]|| ':' || $1[i]
from generate_subscripts($1,1) g(i))
,',') || ']'
$$ language sql immutable strict;

postgres=# select json('Zdenek' as name,'30' as age);
json
----------------------
[name:Zdenek,age:30]
(1 row)

postgres=# select json(name, age) from person;
json
----------------------
[name:Zdenek,age:30]
(1 row)

There are two possibilities
a) collect labels in parse time
b) collect labels in executor time

@a needs info in pg_proc, but it is simpler, @b is little bit
difficult, but doesn't need any changes in system catalog. I thinking
about b now.

Necessary changes:
=================
labels are searched in parse tree fcinfo->flinfo->fn_expr. I need
insert label into parse tree, so I it needs special node
labeled_param, For getting column reference I need to put current
exprstate to fcinfo. Function getlabels() should take code from
ExecEvalVar function.

Any notes, ideas?

Pavel Stehule

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2008-08-14 10:03:54 Re: gsoc, oprrest function for text search take 2
Previous Message Jan UrbaƄski 2008-08-14 09:53:25 Re: gsoc, oprrest function for text search take 2