nullって何型?
同一関数名で別の型を受け付ける関数群が存在する場合に、その関数引数にnullを渡したらどうなるんだっけ?という素朴な疑問を受け、即答できなかったので検証してみた。
予想では明示的にキャストしてなければ型が明確でないというエラーになるとは思うが・・・
test=# create or replace function foo(int2) RETURNS int4 AS $$ SELECT 100; $$ LANGUAGE sql; CREATE FUNCTION test=# create or replace function foo(int4) RETURNS int4 AS $$ SELECT 100000; $$ LANGUAGE sql; CREATE FUNCTION test=# SELECT foo(null); ERROR: function foo(unknown) is not unique LINE 1: SELECT foo(null); ^ HINT: Could not choose a best candidate function. You might need to add explicit type casts. test=# SELECT foo(null::int2); foo ----- 100 (1 row) test=# SELECT foo(null::int4); foo -------- 100000 (1 row) test=# test=# create or replace function foo(int2) RETURNS int4 AS $$ SELECT 100; $$ LANGUAGE sql STRICT; CREATE FUNCTION test=# create or replace function foo(int4) RETURNS int4 AS $$ SELECT 100000; $$ LANGUAGE sql STRICT; CREATE FUNCTION test=# SELECT foo(null); ERROR: function foo(unknown) is not unique LINE 1: SELECT foo(null); ^ HINT: Could not choose a best candidate function. You might need to add explicit type casts. test=# SELECT foo(null::int2); foo ----- (1 row) test=# SELECT foo(null::int4); foo ----- (1 row) test=#
予想どおりでほっとした。