Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4dbcb3f

Browse files
committed
Restructure polymorphic-type resolution in funcapi.c.
resolve_polymorphic_tupdesc() and resolve_polymorphic_argtypes() failed to cover the case of having to resolve anyarray given only an anyrange input. The bug was masked if anyelement was also used (as either input or output), which probably helps account for our not having noticed. While looking at this I noticed that resolve_generic_type() would produce the wrong answer if asked to make that same resolution. ISTM that resolve_generic_type() is confusingly defined and overly complex, so rather than fix it, let's just make funcapi.c do the specific lookups it requires for itself. With this change, resolve_generic_type() is not used anywhere, so remove it in HEAD. In the back branches, leave it alone (complete with bug) just in case any external code is using it. While we're here, make some other refactoring adjustments in funcapi.c with an eye to upcoming future expansion of the set of polymorphic types: * Simplify quick-exit tests by adding an overall have_polymorphic_result flag. This is about a wash now but will be a win when there are more flags. * Reduce duplication of code between resolve_polymorphic_tupdesc() and resolve_polymorphic_argtypes(). * Don't bother to validate correct matching of anynonarray or anyenum; the parser should have done that, and even if it didn't, just doing "return false" here would lead to a very confusing, off-point error message. (Really, "return false" in these two functions should only occur if the call_expr isn't supplied or we can't obtain data type info from it.) * For the same reason, throw an elog rather than "return false" if we fail to resolve a polymorphic type. The bug's been there since we added anyrange, so back-patch to all supported branches. Discussion: https://postgr.es/m/6093.1584202130@sss.pgh.pa.us
1 parent e83daa7 commit 4dbcb3f

File tree

5 files changed

+216
-251
lines changed

5 files changed

+216
-251
lines changed

src/backend/parser/parse_coerce.c

-104
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
#include "postgres.h"
1616

17-
#include "access/htup_details.h"
1817
#include "catalog/pg_cast.h"
1918
#include "catalog/pg_class.h"
2019
#include "catalog/pg_inherits.h"
@@ -26,7 +25,6 @@
2625
#include "parser/parse_relation.h"
2726
#include "parser/parse_type.h"
2827
#include "utils/builtins.h"
29-
#include "utils/datum.h"
3028
#include "utils/lsyscache.h"
3129
#include "utils/syscache.h"
3230
#include "utils/typcache.h"
@@ -1968,108 +1966,6 @@ enforce_generic_type_consistency(const Oid *actual_arg_types,
19681966
return rettype;
19691967
}
19701968

1971-
/*
1972-
* resolve_generic_type()
1973-
* Deduce an individual actual datatype on the assumption that
1974-
* the rules for polymorphic types are being followed.
1975-
*
1976-
* declared_type is the declared datatype we want to resolve.
1977-
* context_actual_type is the actual input datatype to some argument
1978-
* that has declared datatype context_declared_type.
1979-
*
1980-
* If declared_type isn't polymorphic, we just return it. Otherwise,
1981-
* context_declared_type must be polymorphic, and we deduce the correct
1982-
* return type based on the relationship of the two polymorphic types.
1983-
*/
1984-
Oid
1985-
resolve_generic_type(Oid declared_type,
1986-
Oid context_actual_type,
1987-
Oid context_declared_type)
1988-
{
1989-
if (declared_type == ANYARRAYOID)
1990-
{
1991-
if (context_declared_type == ANYARRAYOID)
1992-
{
1993-
/*
1994-
* Use actual type, but it must be an array; or if it's a domain
1995-
* over array, use the base array type.
1996-
*/
1997-
Oid context_base_type = getBaseType(context_actual_type);
1998-
Oid array_typelem = get_element_type(context_base_type);
1999-
2000-
if (!OidIsValid(array_typelem))
2001-
ereport(ERROR,
2002-
(errcode(ERRCODE_DATATYPE_MISMATCH),
2003-
errmsg("argument declared %s is not an array but type %s",
2004-
"anyarray", format_type_be(context_base_type))));
2005-
return context_base_type;
2006-
}
2007-
else if (context_declared_type == ANYELEMENTOID ||
2008-
context_declared_type == ANYNONARRAYOID ||
2009-
context_declared_type == ANYENUMOID ||
2010-
context_declared_type == ANYRANGEOID)
2011-
{
2012-
/* Use the array type corresponding to actual type */
2013-
Oid array_typeid = get_array_type(context_actual_type);
2014-
2015-
if (!OidIsValid(array_typeid))
2016-
ereport(ERROR,
2017-
(errcode(ERRCODE_UNDEFINED_OBJECT),
2018-
errmsg("could not find array type for data type %s",
2019-
format_type_be(context_actual_type))));
2020-
return array_typeid;
2021-
}
2022-
}
2023-
else if (declared_type == ANYELEMENTOID ||
2024-
declared_type == ANYNONARRAYOID ||
2025-
declared_type == ANYENUMOID ||
2026-
declared_type == ANYRANGEOID)
2027-
{
2028-
if (context_declared_type == ANYARRAYOID)
2029-
{
2030-
/* Use the element type corresponding to actual type */
2031-
Oid context_base_type = getBaseType(context_actual_type);
2032-
Oid array_typelem = get_element_type(context_base_type);
2033-
2034-
if (!OidIsValid(array_typelem))
2035-
ereport(ERROR,
2036-
(errcode(ERRCODE_DATATYPE_MISMATCH),
2037-
errmsg("argument declared %s is not an array but type %s",
2038-
"anyarray", format_type_be(context_base_type))));
2039-
return array_typelem;
2040-
}
2041-
else if (context_declared_type == ANYRANGEOID)
2042-
{
2043-
/* Use the element type corresponding to actual type */
2044-
Oid context_base_type = getBaseType(context_actual_type);
2045-
Oid range_typelem = get_range_subtype(context_base_type);
2046-
2047-
if (!OidIsValid(range_typelem))
2048-
ereport(ERROR,
2049-
(errcode(ERRCODE_DATATYPE_MISMATCH),
2050-
errmsg("argument declared %s is not a range type but type %s",
2051-
"anyrange", format_type_be(context_base_type))));
2052-
return range_typelem;
2053-
}
2054-
else if (context_declared_type == ANYELEMENTOID ||
2055-
context_declared_type == ANYNONARRAYOID ||
2056-
context_declared_type == ANYENUMOID)
2057-
{
2058-
/* Use the actual type; it doesn't matter if array or not */
2059-
return context_actual_type;
2060-
}
2061-
}
2062-
else
2063-
{
2064-
/* declared_type isn't polymorphic, so return it as-is */
2065-
return declared_type;
2066-
}
2067-
/* If we get here, declared_type is polymorphic and context isn't */
2068-
/* NB: this is a calling-code logic error, not a user error */
2069-
elog(ERROR, "could not determine polymorphic type because context isn't polymorphic");
2070-
return InvalidOid; /* keep compiler quiet */
2071-
}
2072-
20731969

20741970
/* TypeCategory()
20751971
* Assign a category to the specified type OID.

0 commit comments

Comments
 (0)