Postgres: How to overload functions having single record type input parameter

假装没事ソ 提交于 2019-12-11 16:56:54

问题


I have a user defined type:

create type match_input as (
  _values text[],    
  _name text, 
  _norm_fn text, 
  _operator text
);

and I use this as the input parameter for a function:

get_matches(mi match_input)

I want to be able to call the same function, but passing a single text value for _values. So I defined a new type:

 create type match_input_simple as (
  _values text,    
  _name text, 
  _norm_fn text, 
  _operator text
);

If I try and overload the function with the following:

create or replace function get_matches(_mis match_input_simple)
  returns setof contact_index
as $func$
  select get_matches((array[_mis._values], _mis._name, 
_mis._norm_fn, _mis._operator)::match_input);
$func$
language sql strict;

The function compiles without complaining, but when I run the function I get this error:

ERROR:  function get_matches(record) is not unique
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.

So it looks like postgres can't tell the difference between my two different record types when its trying to decide which function to run. I don't want the user to have to explicitly type cast the record each time the function is called, since that kind of defeats the purpose of trying to make the interface simple.


回答1:


It is getting confused over type on the similar functions. Explicit type cast might help. Like

SELECT get_matches(val::match_input_simple)

Or

SELECT get_matches(val::match_input )

Depending upon the type of val




回答2:


It can't be done without casting.

Remember that you can have multiple types with the exactly same content and create a overload for each one of them. How would the database choose the right type and function?



来源:https://stackoverflow.com/questions/49249408/postgres-how-to-overload-functions-having-single-record-type-input-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!