| -- ----------------------------------------------------------------- |
| -- Test function default parameters |
| -- |
| -- PostgreSQL's 'polymorphism' test mostly covers default |
| -- parameters. This file contains a few extra tests. |
| -- ----------------------------------------------------------------- |
| -- inout argmode test: |
| create function dfunc(a inout int default 10, b int = 2) as $$ select $1 + $2; $$ language sql; |
| select dfunc(); |
| dfunc |
| ------- |
| 12 |
| (1 row) |
| |
| select dfunc(4); |
| dfunc |
| ------- |
| 6 |
| (1 row) |
| |
| select dfunc(4,5); |
| dfunc |
| ------- |
| 9 |
| (1 row) |
| |
| \df dfunc |
| List of functions |
| Schema | Name | Result data type | Argument data types | Type |
| --------+-------+------------------+-------------------------------------------------+------ |
| public | dfunc | integer | INOUT a integer DEFAULT 10, b integer DEFAULT 2 | func |
| (1 row) |
| |
| drop function dfunc(int, int); |
| -- ambiguity should be reported only if there's not a better match available |
| create function dfunc(text) returns text as $$ select $1; $$ language sql; |
| create function dfunc(int = 1, int = 2) returns int as $$ select 2; $$ language sql; |
| create function dfunc(int = 1, int = 2, int = 3, int = 4) returns int as $$ select 4; $$ language sql; |
| -- now, dfunc(nargs = 2) and dfunc(nargs = 4) are ambiguous when called |
| -- with 0 to 2 arguments with input type int |
| select dfunc('Hi'); -- ok, input type text |
| dfunc |
| ------- |
| Hi |
| (1 row) |
| |
| select dfunc(); -- fail |
| ERROR: function dfunc() is not unique |
| LINE 1: select dfunc(); |
| ^ |
| HINT: Could not choose a best candidate function. You might need to add explicit type casts. |
| select dfunc(1); -- fail |
| ERROR: function dfunc(integer) is not unique |
| LINE 1: select dfunc(1); |
| ^ |
| HINT: Could not choose a best candidate function. You might need to add explicit type casts. |
| select dfunc(1, 2); -- fail |
| ERROR: function dfunc(integer, integer) is not unique |
| LINE 1: select dfunc(1, 2); |
| ^ |
| HINT: Could not choose a best candidate function. You might need to add explicit type casts. |
| select dfunc(1, 2, 3); -- ok |
| dfunc |
| ------- |
| 4 |
| (1 row) |
| |
| select dfunc(1, 2, 3, 4); -- ok |
| dfunc |
| ------- |
| 4 |
| (1 row) |
| |
| drop function dfunc(text); |
| drop function dfunc(int, int); |
| drop function dfunc(int, int, int, int); |
| -- check defaults for variadics |
| create function dfunc(a variadic int[]) returns int as |
| $$ select array_upper($1, 1) $$ language sql; |
| select dfunc(); -- fail |
| ERROR: function dfunc() does not exist |
| LINE 1: select dfunc(); |
| ^ |
| HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| select dfunc(10); |
| dfunc |
| ------- |
| 1 |
| (1 row) |
| |
| select dfunc(10,20); |
| dfunc |
| ------- |
| 2 |
| (1 row) |
| |
| create or replace function dfunc(a variadic int[] default '{44,88,99}'::int[]) returns int as $$ select array_upper($1, 1) $$ language sql; |
| select dfunc(); -- now ok |
| dfunc |
| ------- |
| 3 |
| (1 row) |
| |
| select dfunc(10); |
| dfunc |
| ------- |
| 1 |
| (1 row) |
| |
| select dfunc(10,20); |
| dfunc |
| ------- |
| 2 |
| (1 row) |
| |
| -- can change default value |
| create or replace function dfunc(a variadic int[] default '{44,55,88,99}'::int[]) returns int as $$ select array_upper($1, 1) $$ language sql; |
| select dfunc(); |
| dfunc |
| ------- |
| 4 |
| (1 row) |
| |
| drop function dfunc(a variadic int[]); |
| -- test expression |
| create function dfunc(a int, b double precision default random()*10) returns double precision as $$ select $1+$2; $$ language sql; |
| \df dfunc |
| List of functions |
| Schema | Name | Result data type | Argument data types | Type |
| --------+-------+------------------+---------------------------------------------------------------------------+------ |
| public | dfunc | double precision | a integer, b double precision DEFAULT (random() * (10)::double precision) | func |
| (1 row) |
| |
| create view dview as select random()*10, dfunc(4); |
| select pg_get_viewdef('dview',true); |
| pg_get_viewdef |
| -------------------------------------------------------- |
| SELECT random() * 10::double precision AS "?column?",+ |
| dfunc(4) AS dfunc; |
| (1 row) |
| |
| -- need drop cascade |
| drop function dfunc(int, double precision); |
| ERROR: cannot drop function dfunc(integer,double precision) because other objects depend on it |
| DETAIL: view dview depends on function dfunc(integer,double precision) |
| HINT: Use DROP ... CASCADE to drop the dependent objects too. |
| drop function dfunc(int, double precision) cascade; |
| NOTICE: drop cascades to view dview |