idea: simple variadic functions in SQL and PL/pgSQL - Mailing list pgsql-hackers

From Pavel Stehule
Subject idea: simple variadic functions in SQL and PL/pgSQL
Date
Msg-id 162867790802240854t5e9d43end50bd72ee00f4c9a@mail.gmail.com
Whole thread Raw
Responses Re: idea: simple variadic functions in SQL and PL/pgSQL
List 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


pgsql-hackers by date:

Previous
From: andy
Date:
Subject: Re: 8.3 / 8.2.6 restore comparison
Next
From: "Florian G. Pflug"
Date:
Subject: Behaviour of rows containg not-null domains in plpgsql