I have a set of functions with the following pattern:
create or replace function example_1(x1 integer)
returns table (y integer) as $$
select ...
$$ language sql immutable;
create or replace function example_2(x1 integer, x2 integer)
returns table (y integer) as $$
select ...
$$ language sql immutable;
create or replace function example_N(x1 integer, x2 integer, ..., xN integer)
returns table (y integer) as $$
select ...
$$ language sql immutable;
I want to create a single function that encapsulate the functions above in the following way:
create or replace function example(x integer[])
returns table (y integer) as $$
select case length(x, 1)
when 1 then example_1(x[1])
when 2 then example_2(x[1], x[2])
...
when N then select example_2(x[1], x[2], ..., x[N])
end
$$ language sql immutable;
The problem is that set-returning functions are not allowed in CASE.
ERROR: set-returning functions are not allowed in CASE LINE 9: else (example_2(x[1], x[2]^ HINT: You might be able to move the set-returning function into a LATERAL FROM item. SQL state: 0A000 Character: 575
Is there a way how else I can implement example function?