Thread: idea: simple variadic functions in SQL and PL/pgSQL
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
Pavel Stehule wrote: > 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. > There are a whole slew of questions around this, ISTM. For example: What will be the type inferred for the array of variadic args in plpgsql? If we are going to do this I think we need some discussion on design before we rush into it. cheers andrew
Hello On 26/02/2008, Andrew Dunstan <andrew@dunslane.net> wrote: > > > Pavel Stehule wrote: > > 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. > > > > > There are a whole slew of questions around this, ISTM. sure. It's time to thing about it. > > For example: What will be the type inferred for the array of variadic > args in plpgsql? SQL and PL/SQL should be limited. In my prototype I use fragment: /* replace ANYNONARRAYS argument with ARRAY OF some */ argtype = get_array_type(argtype); if (!OidIsValid(argtype)) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("could not determine actual array type for variadic argument"))); SPI is unlimited (C language). I can use list of arrays in perl or python - there I don't see complications too. Domains and arrays can be problem. Using variadic array variables isn't typical. So I will to solve only domains. But it's more general problem. Maybe 8.4 can support array of domains. > > If we are going to do this I think we need some discussion on design > before we rush into it. > yes, I agree. Variadic functions are simple. And I belive it can simplify life to some programmers that have to manage functions like: foo(int), foo(int, int) foo(int, int, int)... regards Pavel > cheers > > > andrew > > > >