FuncDetailCode fdresult;
AclResult aclresult;
int i;
- bool allPolyArgs = true;
/*
* func_get_detail looks up the function in the catalogs, does
func_signature_string(fnName, nargs, input_types))));
/*
- * If the given type(s) are all polymorphic, there's nothing we can check.
- * Otherwise, enforce consistency, and possibly refine the result type.
+ * If there are any polymorphic types involved, enforce consistency, and
+ * possibly refine the result type. It's OK if the result is still
+ * polymorphic at this point, though.
*/
- for (i = 0; i < nargs; i++)
- {
- if (!IsPolymorphicType(input_types[i]))
- {
- allPolyArgs = false;
- break;
- }
- }
-
- if (!allPolyArgs)
- {
- *rettype = enforce_generic_type_consistency(input_types,
- true_oid_array,
- nargs,
- *rettype);
- }
+ *rettype = enforce_generic_type_consistency(input_types,
+ true_oid_array,
+ nargs,
+ *rettype,
+ true);
/*
* func_get_detail will find functions requiring run-time argument type
* we add the extra condition that the ANYELEMENT type must not be an array.
* (This is a no-op if used in combination with ANYARRAY or ANYENUM, but
* is an extra restriction if not.)
+ *
+ * When allow_poly is false, we are not expecting any of the actual_arg_types
+ * to be polymorphic, and we should not return a polymorphic result type
+ * either. When allow_poly is true, it is okay to have polymorphic "actual"
+ * arg types, and we can return ANYARRAY or ANYELEMENT as the result. (This
+ * case is currently used only to check compatibility of an aggregate's
+ * declaration with the underlying transfn.)
*/
Oid
enforce_generic_type_consistency(Oid *actual_arg_types,
Oid *declared_arg_types,
int nargs,
- Oid rettype)
+ Oid rettype,
+ bool allow_poly)
{
int j;
bool have_generics = false;
Oid elem_typeid = InvalidOid;
Oid array_typeid = InvalidOid;
Oid array_typelem;
- bool have_anyelement = (rettype == ANYELEMENTOID ||
- rettype == ANYNONARRAYOID ||
- rettype == ANYENUMOID);
bool have_anynonarray = (rettype == ANYNONARRAYOID);
bool have_anyenum = (rettype == ANYENUMOID);
decl_type == ANYNONARRAYOID ||
decl_type == ANYENUMOID)
{
- have_generics = have_anyelement = true;
+ have_generics = true;
if (decl_type == ANYNONARRAYOID)
have_anynonarray = true;
else if (decl_type == ANYENUMOID)
have_unknowns = true;
continue;
}
+ if (allow_poly && decl_type == actual_type)
+ continue; /* no new information here */
if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
have_unknowns = true;
continue;
}
+ if (allow_poly && decl_type == actual_type)
+ continue; /* no new information here */
if (OidIsValid(array_typeid) && actual_type != array_typeid)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
/* Get the element type based on the array type, if we have one */
if (OidIsValid(array_typeid))
{
- if (array_typeid == ANYARRAYOID && !have_anyelement)
- {
- /* Special case for ANYARRAY input: okay iff no ANYELEMENT */
- array_typelem = InvalidOid;
- }
- else
- {
- array_typelem = get_element_type(array_typeid);
- if (!OidIsValid(array_typelem))
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("argument declared \"anyarray\" is not an array but type %s",
- format_type_be(array_typeid))));
- }
+ array_typelem = get_element_type(array_typeid);
+ if (!OidIsValid(array_typelem))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("argument declared \"anyarray\" is not an array but type %s",
+ format_type_be(array_typeid))));
if (!OidIsValid(elem_typeid))
{
}
else if (!OidIsValid(elem_typeid))
{
- /* Only way to get here is if all the generic args are UNKNOWN */
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("could not determine polymorphic type because input has type \"unknown\"")));
+ if (allow_poly)
+ {
+ array_typeid = ANYARRAYOID;
+ elem_typeid = ANYELEMENTOID;
+ }
+ else
+ {
+ /* Only way to get here is if all the generic args are UNKNOWN */
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("could not determine polymorphic type because input has type \"unknown\"")));
+ }
}
- if (have_anynonarray)
+ if (have_anynonarray && elem_typeid != ANYELEMENTOID)
{
/* require the element type to not be an array */
if (type_is_array(elem_typeid))
format_type_be(elem_typeid))));
}
- if (have_anyenum)
+ if (have_anyenum && elem_typeid != ANYELEMENTOID)
{
/* require the element type to be an enum */
if (!type_is_enum(elem_typeid))
-4567890123456789 | -4567890123456788
(5 rows)
+-- another kind of polymorphic aggregate
+create function add_group(grp anyarray, ad anyelement, size integer)
+ returns anyarray
+ as $$
+begin
+ if grp is null then
+ return array[ad];
+ end if;
+ if array_upper(grp, 1) < size then
+ return grp || ad;
+ end if;
+ return grp;
+end;
+$$
+ language plpgsql immutable;
+create aggregate build_group(anyelement, integer) (
+ SFUNC = add_group,
+ STYPE = anyarray
+);
+select build_group(q1,3) from int8_tbl;
+ build_group
+----------------------------
+ {123,123,4567890123456789}
+(1 row)
+
+-- this should fail because stype isn't compatible with arg
+create aggregate build_group(int8, integer) (
+ SFUNC = add_group,
+ STYPE = int2[]
+);
+ERROR: function add_group(smallint[], bigint, integer) does not exist
+-- but we can make a non-poly agg from a poly sfunc if types are OK
+create aggregate build_group(int8, integer) (
+ SFUNC = add_group,
+ STYPE = int8[]
+);
select f1, sql_if(f1 > 0, bleat(f1), bleat(f1 + 1)) from int4_tbl;
select q2, sql_if(q2 > 0, q2, q2 + 1) from int8_tbl;
+
+-- another kind of polymorphic aggregate
+
+create function add_group(grp anyarray, ad anyelement, size integer)
+ returns anyarray
+ as $$
+begin
+ if grp is null then
+ return array[ad];
+ end if;
+ if array_upper(grp, 1) < size then
+ return grp || ad;
+ end if;
+ return grp;
+end;
+$$
+ language plpgsql immutable;
+
+create aggregate build_group(anyelement, integer) (
+ SFUNC = add_group,
+ STYPE = anyarray
+);
+
+select build_group(q1,3) from int8_tbl;
+
+-- this should fail because stype isn't compatible with arg
+create aggregate build_group(int8, integer) (
+ SFUNC = add_group,
+ STYPE = int2[]
+);
+
+-- but we can make a non-poly agg from a poly sfunc if types are OK
+create aggregate build_group(int8, integer) (
+ SFUNC = add_group,
+ STYPE = int8[]
+);