idea: simple variadic functions in SQL and PL/pgSQL

From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: idea: simple variadic functions in SQL and PL/pgSQL
Date: 2008-02-24 16:54:13
Message-ID: 162867790802240854t5e9d43end50bd72ee00f4c9a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello,

I found easy implementation of variadic functions. It's based on
adapation FuncnameGetCandidates. When I found variadic function, then
I should create accurate number of last arguments (diff between
pronargs and nargs). Variadic function can be signed via flag or via
some pseudotype. Flag is better - allows variadic arguments of any
type. In static languages (like SQL or PL/pgSQL) variadic variables
can ba accessed via array (variadic arguments can be only nonarray).
This isn't problem in C language, there are arguments available
directly.

Sample:

CREATE OR REPLACE FUNCTION Least(anyelement)
RETURNS anyelement AS $$
SELECT MIN($1[i])
FROM generate_series(1, array_upper($1,1)) g(i);
$$ LANGUAGE SQL IMMUTABLE VARIADIC.

This sample is really simple. The goal is support sophistic libraries
like JSON support: http://www.mysqludf.org/lib_mysqludf_json/index.php

Main change in FuncnameGetCandidates.

if (!OidIsValid(variadic_oid))
{
memcpy(newResult->args, procform->proargtypes.values,
pronargs * sizeof(Oid));
}
else
{
int j;
/* copy nonvariadic parameters */
memcpy(newResult->args, procform->proargtypes.values,
pronargs * sizeof(Oid));
/* set variadic parameters, !!!!!!!!!!! */
for (j = pronargs - 1; j < nargs; j++)
newResult->args[j] = variadic_oid;
}

I invite any ideas, notes

Regards
Pavel Stehule

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2008-02-24 17:28:04 Re: Behaviour of rows containg not-null domains in plpgsql
Previous Message Florian G. Pflug 2008-02-24 16:49:02 Behaviour of rows containg not-null domains in plpgsql