proposal sql: labeled function params - Mailing list pgsql-hackers

From Pavel Stehule
Subject proposal sql: labeled function params
Date
Msg-id 162867790808140256w533324bj25183d476d3aa89a@mail.gmail.com
Whole thread Raw
Responses Re: proposal sql: labeled function params  (Hannu Krosing <hannu@2ndQuadrant.com>)
Re: proposal sql: labeled function params  (Peter Eisentraut <peter_e@gmx.net>)
List 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


pgsql-hackers by date:

Previous
From: Jan Urbański
Date:
Subject: Re: gsoc, oprrest function for text search take 2
Next
From: Simon Riggs
Date:
Subject: Re: Join Removal/ Vertical Partitioning