30#include "utils/fmgroids.h"
37 Oid targetTypeId,
int32 targetTypMod,
40 bool hideInputCoercion);
45 Oid targetTypeId,
int32 targetTypMod,
106 targettype, targettypmod,
107 ccontext, cformat, location);
115 targettype, targettypmod,
116 ccontext, cformat, location,
117 (result != expr && !
IsA(result,
Const)));
125 newcoll->
arg = (
Expr *) result;
128 result = (
Node *) newcoll;
159 Oid inputTypeId,
Oid targetTypeId,
int32 targetTypeMod,
166 if (targetTypeId == inputTypeId ||
172 if (targetTypeId == ANYOID ||
173 targetTypeId == ANYELEMENTOID ||
174 targetTypeId == ANYNONARRAYOID ||
175 targetTypeId == ANYCOMPATIBLEOID ||
176 targetTypeId == ANYCOMPATIBLENONARRAYOID)
192 if (targetTypeId == ANYARRAYOID ||
193 targetTypeId == ANYENUMOID ||
194 targetTypeId == ANYRANGEOID ||
195 targetTypeId == ANYMULTIRANGEOID ||
196 targetTypeId == ANYCOMPATIBLEARRAYOID ||
197 targetTypeId == ANYCOMPATIBLERANGEOID ||
198 targetTypeId == ANYCOMPATIBLEMULTIRANGEOID)
215 if (inputTypeId != UNKNOWNOID)
219 if (baseTypeId != inputTypeId)
233 if (inputTypeId == UNKNOWNOID &&
IsA(node,
Const))
268 baseTypeMod = targetTypeMod;
280 if (baseTypeId == INTERVALOID)
281 inputTypeMod = baseTypeMod;
288 newcon->consttypmod = inputTypeMod;
290 newcon->constlen =
typeLen(baseType);
291 newcon->constbyval =
typeByVal(baseType);
292 newcon->constisnull = con->constisnull;
299 newcon->location = con->location;
311 if (!con->constisnull)
325 if (!con->constisnull && newcon->constlen == -1)
329#ifdef RANDOMIZE_ALLOCATED_MEMORY
342 if (!con->constisnull && !newcon->constbyval)
349 if (newcon->constlen == -1)
351 if (!
datumIsEqual(newcon->constvalue, val2,
false, newcon->constlen))
352 elog(
WARNING,
"type %s has unstable input conversion for \"%s\"",
359 result = (
Node *) newcon;
362 if (baseTypeId != targetTypeId)
364 baseTypeId, baseTypeMod,
366 ccontext, cformat, location,
401 inputTypeId, targetTypeId, targetTypeMod,
402 ccontext, cformat, location);
407 newcoll->
arg = (
Expr *) result;
410 result = (
Node *) newcoll;
421 baseTypeMod = targetTypeMod;
434 baseTypeId, baseTypeMod,
435 ccontext, cformat, location);
441 if (targetTypeId != baseTypeId)
444 ccontext, cformat, location,
460 ccontext, cformat, location,
481 if (inputTypeId == RECORDOID &&
486 ccontext, cformat, location);
488 if (targetTypeId == RECORDOID &&
496 if (inputTypeId == RECORDARRAYOID &&
503 if (targetTypeId == RECORDARRAYOID &&
527 if (baseTypeId != inputTypeId)
539 r->convertformat = cformat;
544 elog(
ERROR,
"failed to find conversion function from %s to %s",
561 bool have_generics =
false;
565 for (
i = 0;
i < nargs;
i++)
567 Oid inputTypeId = input_typeids[
i];
568 Oid targetTypeId = target_typeids[
i];
573 if (inputTypeId == targetTypeId)
577 if (targetTypeId == ANYOID)
581 if (IsPolymorphicType(targetTypeId))
583 have_generics =
true;
591 if (inputTypeId == UNKNOWNOID)
607 if (inputTypeId == RECORDOID &&
614 if (targetTypeId == RECORDOID &&
624 if (inputTypeId == RECORDARRAYOID &&
632 if (targetTypeId == RECORDARRAYOID &&
678 bool hideInputCoercion)
686 if (baseTypeId == typeId)
690 if (hideInputCoercion)
718 result->resulttypmod = -1;
720 result->coercionformat = cformat;
723 return (
Node *) result;
755 bool hideInputCoercion)
765 if (hideInputCoercion)
773 if (targetTypMod < 0)
781 targetTypeId, targetTypMod,
782 ccontext, cformat, location);
793 cformat, location,
false);
841 Oid targetTypeId,
int32 targetTypMod,
854 elog(
ERROR,
"cache lookup failed for function %u", funcId);
864 Assert(!procstruct->proretset);
865 Assert(procstruct->prokind == PROKIND_FUNCTION);
866 nargs = procstruct->pronargs;
867 Assert(nargs >= 1 && nargs <= 3);
869 Assert(nargs < 2 || procstruct->proargtypes.values[1] == INT4OID);
870 Assert(nargs < 3 || procstruct->proargtypes.values[2] == BOOLOID);
917 return (
Node *) fexpr;
924 Oid sourceBaseTypeId;
925 int32 sourceBaseTypeMod;
926 Oid targetElementType;
946 ctest->typeMod = sourceBaseTypeMod;
961 if (elemexpr == NULL)
962 elog(
ERROR,
"failed to coerce array element type as expected");
974 acoerce->coerceformat = cformat;
977 return (
Node *) acoerce;
989 iocoerce->coerceformat = cformat;
992 return (
Node *) iocoerce;
996 elog(
ERROR,
"unsupported pathtype %d in build_coercion_expression",
1019 int32 baseTypeMod = -1;
1035 else if (node &&
IsA(node,
Var) &&
1038 int rtindex = ((
Var *) node)->varno;
1039 int sublevels_up = ((
Var *) node)->varlevelsup;
1040 int vlocation = ((
Var *) node)->location;
1048 (
errcode(ERRCODE_CANNOT_COERCE),
1049 errmsg(
"cannot cast type %s to %s",
1065 for (
i = 0;
i < tupdesc->
natts;
i++)
1073 if (attr->attisdropped)
1086 (
errcode(ERRCODE_CANNOT_COERCE),
1087 errmsg(
"cannot cast type %s to %s",
1090 errdetail(
"Input has too few columns."),
1104 (
errcode(ERRCODE_CANNOT_COERCE),
1105 errmsg(
"cannot cast type %s to %s",
1108 errdetail(
"Cannot cast type %s to %s in column %d.",
1113 newargs =
lappend(newargs, cexpr);
1119 (
errcode(ERRCODE_CANNOT_COERCE),
1120 errmsg(
"cannot cast type %s to %s",
1123 errdetail(
"Input has too many columns."),
1129 rowexpr->
args = newargs;
1130 rowexpr->row_typeid = baseTypeId;
1131 rowexpr->row_format = cformat;
1132 rowexpr->colnames =
NIL;
1136 if (baseTypeId != targetTypeId)
1140 baseTypeId, baseTypeMod,
1142 ccontext, cformat, location,
1146 return (
Node *) rowexpr;
1161 const char *constructName)
1165 if (inputTypeId != BOOLOID)
1174 if (newnode == NULL)
1176 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1178 errmsg(
"argument of %s must be type %s, not type %s",
1179 constructName,
"boolean",
1187 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1189 errmsg(
"argument of %s must not return a set",
1208 Oid targetTypeId,
int32 targetTypmod,
1209 const char *constructName)
1213 if (inputTypeId != targetTypeId)
1218 targetTypeId, targetTypmod,
1222 if (newnode == NULL)
1224 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1226 errmsg(
"argument of %s must be type %s, not type %s",
1236 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1238 errmsg(
"argument of %s must not return a set",
1258 const char *constructName)
1273 int typlen,
bool typbyval)
1277 int32 baseTypeMod = typmod;
1291 if (typid != baseTypeId)
1293 baseTypeId, baseTypeMod,
1314 int coerce_location,
1317 if (coerce_location >= 0)
1362 if (ptype != UNKNOWNOID)
1375 *which_expr = pexpr;
1394 if (ntype != UNKNOWNOID && ntype != ptype)
1400 if (ptype == UNKNOWNOID)
1405 pcategory = ncategory;
1406 pispreferred = nispreferred;
1408 else if (ncategory != pcategory)
1413 if (context == NULL)
1416 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1419 errmsg(
"%s types %s and %s cannot be matched",
1425 else if (!pispreferred &&
1435 pcategory = ncategory;
1436 pispreferred = nispreferred;
1452 if (ptype == UNKNOWNOID)
1456 *which_expr = pexpr;
1490 if (ptype != UNKNOWNOID)
1492 for (;
i < nargs;
i++)
1494 if (typeids[
i] != ptype)
1508 for (;
i < nargs;
i++)
1513 if (ntype != UNKNOWNOID && ntype != ptype)
1519 if (ptype == UNKNOWNOID)
1523 pcategory = ncategory;
1524 pispreferred = nispreferred;
1526 else if (ncategory != pcategory)
1534 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1535 errmsg(
"argument types %s and %s cannot be matched",
1539 else if (!pispreferred &&
1548 pcategory = ncategory;
1549 pispreferred = nispreferred;
1555 if (ptype == UNKNOWNOID)
1574 Oid targetTypeId,
const char *context)
1578 if (inputTypeId == targetTypeId)
1581 node =
coerce_type(pstate, node, inputTypeId, targetTypeId, -1,
1585 (
errcode(ERRCODE_CANNOT_COERCE),
1587 errmsg(
"%s could not convert type %s to %s",
1629 for (
int i = 0;
i < nargs;
i++)
1739 const Oid *declared_arg_types,
1751 bool have_anynonarray =
false;
1752 bool have_anyenum =
false;
1753 bool have_anycompatible_nonarray =
false;
1754 int n_anycompatible_args = 0;
1762 for (
int j = 0;
j < nargs;
j++)
1764 Oid decl_type = declared_arg_types[
j];
1765 Oid actual_type = actual_arg_types[
j];
1767 if (decl_type == ANYELEMENTOID ||
1768 decl_type == ANYNONARRAYOID ||
1769 decl_type == ANYENUMOID)
1771 if (decl_type == ANYNONARRAYOID)
1772 have_anynonarray =
true;
1773 else if (decl_type == ANYENUMOID)
1774 have_anyenum =
true;
1775 if (actual_type == UNKNOWNOID)
1777 if (
OidIsValid(elem_typeid) && actual_type != elem_typeid)
1779 elem_typeid = actual_type;
1781 else if (decl_type == ANYARRAYOID)
1783 if (actual_type == UNKNOWNOID)
1786 if (
OidIsValid(array_typeid) && actual_type != array_typeid)
1788 array_typeid = actual_type;
1790 else if (decl_type == ANYRANGEOID)
1792 if (actual_type == UNKNOWNOID)
1795 if (
OidIsValid(range_typeid) && actual_type != range_typeid)
1797 range_typeid = actual_type;
1799 else if (decl_type == ANYMULTIRANGEOID)
1801 if (actual_type == UNKNOWNOID)
1804 if (
OidIsValid(multirange_typeid) && actual_type != multirange_typeid)
1806 multirange_typeid = actual_type;
1808 else if (decl_type == ANYCOMPATIBLEOID ||
1809 decl_type == ANYCOMPATIBLENONARRAYOID)
1811 if (decl_type == ANYCOMPATIBLENONARRAYOID)
1812 have_anycompatible_nonarray =
true;
1813 if (actual_type == UNKNOWNOID)
1816 anycompatible_actual_types[n_anycompatible_args++] = actual_type;
1818 else if (decl_type == ANYCOMPATIBLEARRAYOID)
1822 if (actual_type == UNKNOWNOID)
1829 anycompatible_actual_types[n_anycompatible_args++] = elem_type;
1831 else if (decl_type == ANYCOMPATIBLERANGEOID)
1833 if (actual_type == UNKNOWNOID)
1839 if (anycompatible_range_typeid != actual_type)
1844 anycompatible_range_typeid = actual_type;
1846 if (!
OidIsValid(anycompatible_range_typelem))
1849 anycompatible_actual_types[n_anycompatible_args++] = anycompatible_range_typelem;
1852 else if (decl_type == ANYCOMPATIBLEMULTIRANGEOID)
1854 if (actual_type == UNKNOWNOID)
1857 if (
OidIsValid(anycompatible_multirange_typeid))
1860 if (anycompatible_multirange_typeid != actual_type)
1865 anycompatible_multirange_typeid = actual_type;
1867 if (!
OidIsValid(anycompatible_multirange_typelem))
1877 if (array_typeid == ANYARRAYOID)
1905 elem_typeid = array_typelem;
1907 else if (array_typelem != elem_typeid)
1918 Oid multirange_typelem;
1927 range_typeid = multirange_typelem;
1932 else if (multirange_typelem != range_typeid)
1951 elem_typeid = range_typelem;
1953 else if (range_typelem != elem_typeid)
1960 if (have_anynonarray)
1975 if (
OidIsValid(anycompatible_multirange_typeid))
1979 if (anycompatible_multirange_typelem !=
1980 anycompatible_range_typeid)
1985 anycompatible_range_typeid = anycompatible_multirange_typelem;
1987 if (!
OidIsValid(anycompatible_range_typelem))
1990 anycompatible_actual_types[n_anycompatible_args++] =
1991 anycompatible_range_typelem;
1996 if (n_anycompatible_args > 0)
1998 Oid anycompatible_typeid;
2000 anycompatible_typeid =
2002 anycompatible_actual_types,
2010 n_anycompatible_args,
2011 anycompatible_actual_types))
2014 if (have_anycompatible_nonarray)
2030 if (
OidIsValid(anycompatible_range_typelem) &&
2031 anycompatible_range_typelem != anycompatible_typeid)
2133 Oid *declared_arg_types,
2138 bool have_poly_anycompatible =
false;
2139 bool have_poly_unknowns =
false;
2150 bool have_anynonarray = (rettype == ANYNONARRAYOID);
2151 bool have_anyenum = (rettype == ANYENUMOID);
2152 bool have_anymultirange = (rettype == ANYMULTIRANGEOID);
2153 bool have_anycompatible_nonarray = (rettype == ANYCOMPATIBLENONARRAYOID);
2154 bool have_anycompatible_array = (rettype == ANYCOMPATIBLEARRAYOID);
2155 bool have_anycompatible_range = (rettype == ANYCOMPATIBLERANGEOID);
2156 bool have_anycompatible_multirange = (rettype == ANYCOMPATIBLEMULTIRANGEOID);
2157 int n_poly_args = 0;
2158 int n_anycompatible_args = 0;
2166 for (
int j = 0;
j < nargs;
j++)
2168 Oid decl_type = declared_arg_types[
j];
2169 Oid actual_type = actual_arg_types[
j];
2171 if (decl_type == ANYELEMENTOID ||
2172 decl_type == ANYNONARRAYOID ||
2173 decl_type == ANYENUMOID)
2176 if (decl_type == ANYNONARRAYOID)
2177 have_anynonarray =
true;
2178 else if (decl_type == ANYENUMOID)
2179 have_anyenum =
true;
2180 if (actual_type == UNKNOWNOID)
2182 have_poly_unknowns =
true;
2185 if (allow_poly && decl_type == actual_type)
2187 if (
OidIsValid(elem_typeid) && actual_type != elem_typeid)
2189 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2190 errmsg(
"arguments declared \"%s\" are not all alike",
"anyelement"),
2194 elem_typeid = actual_type;
2196 else if (decl_type == ANYARRAYOID)
2199 if (actual_type == UNKNOWNOID)
2201 have_poly_unknowns =
true;
2204 if (allow_poly && decl_type == actual_type)
2207 if (
OidIsValid(array_typeid) && actual_type != array_typeid)
2209 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2210 errmsg(
"arguments declared \"%s\" are not all alike",
"anyarray"),
2214 array_typeid = actual_type;
2216 else if (decl_type == ANYRANGEOID)
2219 if (actual_type == UNKNOWNOID)
2221 have_poly_unknowns =
true;
2224 if (allow_poly && decl_type == actual_type)
2227 if (
OidIsValid(range_typeid) && actual_type != range_typeid)
2229 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2230 errmsg(
"arguments declared \"%s\" are not all alike",
"anyrange"),
2234 range_typeid = actual_type;
2236 else if (decl_type == ANYMULTIRANGEOID)
2239 have_anymultirange =
true;
2240 if (actual_type == UNKNOWNOID)
2242 have_poly_unknowns =
true;
2245 if (allow_poly && decl_type == actual_type)
2248 if (
OidIsValid(multirange_typeid) && actual_type != multirange_typeid)
2250 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2251 errmsg(
"arguments declared \"%s\" are not all alike",
"anymultirange"),
2255 multirange_typeid = actual_type;
2257 else if (decl_type == ANYCOMPATIBLEOID ||
2258 decl_type == ANYCOMPATIBLENONARRAYOID)
2260 have_poly_anycompatible =
true;
2261 if (decl_type == ANYCOMPATIBLENONARRAYOID)
2262 have_anycompatible_nonarray =
true;
2263 if (actual_type == UNKNOWNOID)
2265 if (allow_poly && decl_type == actual_type)
2268 anycompatible_actual_types[n_anycompatible_args++] = actual_type;
2270 else if (decl_type == ANYCOMPATIBLEARRAYOID)
2272 Oid anycompatible_elem_type;
2274 have_poly_anycompatible =
true;
2275 have_anycompatible_array =
true;
2276 if (actual_type == UNKNOWNOID)
2278 if (allow_poly && decl_type == actual_type)
2284 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2285 errmsg(
"argument declared %s is not an array but type %s",
2286 "anycompatiblearray",
2289 anycompatible_actual_types[n_anycompatible_args++] = anycompatible_elem_type;
2291 else if (decl_type == ANYCOMPATIBLERANGEOID)
2293 have_poly_anycompatible =
true;
2294 have_anycompatible_range =
true;
2295 if (actual_type == UNKNOWNOID)
2297 if (allow_poly && decl_type == actual_type)
2303 if (anycompatible_range_typeid != actual_type)
2305 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2306 errmsg(
"arguments declared \"%s\" are not all alike",
"anycompatiblerange"),
2313 anycompatible_range_typeid = actual_type;
2315 if (!
OidIsValid(anycompatible_range_typelem))
2317 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2318 errmsg(
"argument declared %s is not a range type but type %s",
2319 "anycompatiblerange",
2322 anycompatible_actual_types[n_anycompatible_args++] = anycompatible_range_typelem;
2325 else if (decl_type == ANYCOMPATIBLEMULTIRANGEOID)
2327 have_poly_anycompatible =
true;
2328 have_anycompatible_multirange =
true;
2329 if (actual_type == UNKNOWNOID)
2331 if (allow_poly && decl_type == actual_type)
2334 if (
OidIsValid(anycompatible_multirange_typeid))
2337 if (anycompatible_multirange_typeid != actual_type)
2339 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2340 errmsg(
"arguments declared \"%s\" are not all alike",
"anycompatiblemultirange"),
2347 anycompatible_multirange_typeid = actual_type;
2349 if (!
OidIsValid(anycompatible_multirange_typelem))
2351 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2352 errmsg(
"argument declared %s is not a multirange type but type %s",
2353 "anycompatiblemultirange",
2364 if (n_poly_args == 0 && !have_poly_anycompatible)
2375 if (array_typeid == ANYARRAYOID)
2384 if (n_poly_args != 1 ||
2385 (rettype != ANYARRAYOID &&
2386 IsPolymorphicTypeFamily1(rettype)))
2388 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2389 errmsg(
"cannot determine element type of \"anyarray\" argument")));
2390 array_typelem = ANYELEMENTOID;
2397 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2398 errmsg(
"argument declared %s is not an array but type %s",
2408 elem_typeid = array_typelem;
2410 else if (array_typelem != elem_typeid)
2414 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2415 errmsg(
"argument declared %s is not consistent with argument declared %s",
2416 "anyarray",
"anyelement"),
2426 Oid multirange_typelem;
2431 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2432 errmsg(
"argument declared %s is not a multirange type but type %s",
2439 range_typeid = multirange_typelem;
2441 else if (multirange_typelem != range_typeid)
2445 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2446 errmsg(
"argument declared %s is not consistent with argument declared %s",
2447 "anymultirange",
"anyrange"),
2453 else if (have_anymultirange &&
OidIsValid(range_typeid))
2467 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2468 errmsg(
"argument declared %s is not a range type but type %s",
2478 elem_typeid = range_typelem;
2480 else if (range_typelem != elem_typeid)
2484 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2485 errmsg(
"argument declared %s is not consistent with argument declared %s",
2486 "anyrange",
"anyelement"),
2497 elem_typeid = ANYELEMENTOID;
2498 array_typeid = ANYARRAYOID;
2499 range_typeid = ANYRANGEOID;
2500 multirange_typeid = ANYMULTIRANGEOID;
2509 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2510 errmsg(
"could not determine polymorphic type because input has type %s",
2515 if (have_anynonarray && elem_typeid != ANYELEMENTOID)
2523 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2524 errmsg(
"type matched to anynonarray is an array type: %s",
2528 if (have_anyenum && elem_typeid != ANYELEMENTOID)
2533 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2534 errmsg(
"type matched to anyenum is not an enum type: %s",
2540 if (have_poly_anycompatible)
2543 if (
OidIsValid(anycompatible_multirange_typeid))
2547 if (anycompatible_multirange_typelem !=
2548 anycompatible_range_typeid)
2550 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2551 errmsg(
"argument declared %s is not consistent with argument declared %s",
2552 "anycompatiblemultirange",
2553 "anycompatiblerange"),
2560 anycompatible_range_typeid = anycompatible_multirange_typelem;
2562 if (!
OidIsValid(anycompatible_range_typelem))
2564 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2565 errmsg(
"argument declared %s is not a multirange type but type %s",
2566 "anycompatiblemultirange",
2569 have_anycompatible_range =
true;
2571 anycompatible_actual_types[n_anycompatible_args++] =
2572 anycompatible_range_typelem;
2575 else if (have_anycompatible_multirange &&
2582 if (n_anycompatible_args > 0)
2584 anycompatible_typeid =
2586 anycompatible_actual_types,
2591 n_anycompatible_args,
2592 anycompatible_actual_types))
2594 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2595 errmsg(
"arguments of anycompatible family cannot be cast to a common type")));
2597 if (have_anycompatible_array)
2599 anycompatible_array_typeid =
get_array_type(anycompatible_typeid);
2602 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2603 errmsg(
"could not find array type for data type %s",
2607 if (have_anycompatible_range)
2612 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2613 errmsg(
"could not determine polymorphic type %s because input has type %s",
2614 "anycompatiblerange",
"unknown")));
2620 if (anycompatible_range_typelem != anycompatible_typeid)
2622 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2623 errmsg(
"anycompatiblerange type %s does not match anycompatible type %s",
2628 if (have_anycompatible_multirange)
2631 if (!
OidIsValid(anycompatible_multirange_typeid))
2633 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2634 errmsg(
"could not determine polymorphic type %s because input has type %s",
2635 "anycompatiblemultirange",
"unknown")));
2641 if (anycompatible_range_typelem != anycompatible_typeid)
2643 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2644 errmsg(
"anycompatiblemultirange type %s does not match anycompatible type %s",
2649 if (have_anycompatible_nonarray)
2657 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2658 errmsg(
"type matched to anycompatiblenonarray is an array type: %s",
2666 anycompatible_typeid = ANYCOMPATIBLEOID;
2667 anycompatible_array_typeid = ANYCOMPATIBLEARRAYOID;
2668 anycompatible_range_typeid = ANYCOMPATIBLERANGEOID;
2669 anycompatible_multirange_typeid = ANYCOMPATIBLEMULTIRANGEOID;
2679 anycompatible_typeid = TEXTOID;
2680 anycompatible_array_typeid = TEXTARRAYOID;
2681 if (have_anycompatible_range)
2683 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2684 errmsg(
"could not determine polymorphic type %s because input has type %s",
2685 "anycompatiblerange",
"unknown")));
2686 if (have_anycompatible_multirange)
2688 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2689 errmsg(
"could not determine polymorphic type %s because input has type %s",
2690 "anycompatiblemultirange",
"unknown")));
2695 for (
int j = 0;
j < nargs;
j++)
2697 Oid decl_type = declared_arg_types[
j];
2699 if (decl_type == ANYCOMPATIBLEOID ||
2700 decl_type == ANYCOMPATIBLENONARRAYOID)
2701 declared_arg_types[
j] = anycompatible_typeid;
2702 else if (decl_type == ANYCOMPATIBLEARRAYOID)
2703 declared_arg_types[
j] = anycompatible_array_typeid;
2704 else if (decl_type == ANYCOMPATIBLERANGEOID)
2705 declared_arg_types[
j] = anycompatible_range_typeid;
2706 else if (decl_type == ANYCOMPATIBLEMULTIRANGEOID)
2707 declared_arg_types[
j] = anycompatible_multirange_typeid;
2719 if (have_poly_unknowns)
2721 for (
int j = 0;
j < nargs;
j++)
2723 Oid decl_type = declared_arg_types[
j];
2724 Oid actual_type = actual_arg_types[
j];
2726 if (actual_type != UNKNOWNOID)
2729 if (decl_type == ANYELEMENTOID ||
2730 decl_type == ANYNONARRAYOID ||
2731 decl_type == ANYENUMOID)
2732 declared_arg_types[
j] = elem_typeid;
2733 else if (decl_type == ANYARRAYOID)
2740 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2741 errmsg(
"could not find array type for data type %s",
2744 declared_arg_types[
j] = array_typeid;
2746 else if (decl_type == ANYRANGEOID)
2752 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2753 errmsg(
"could not determine polymorphic type %s because input has type %s",
2754 "anyrange",
"unknown")));
2756 declared_arg_types[
j] = range_typeid;
2758 else if (decl_type == ANYMULTIRANGEOID)
2764 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2765 errmsg(
"could not determine polymorphic type %s because input has type %s",
2766 "anymultirange",
"unknown")));
2768 declared_arg_types[
j] = multirange_typeid;
2774 if (rettype == ANYELEMENTOID ||
2775 rettype == ANYNONARRAYOID ||
2776 rettype == ANYENUMOID)
2780 if (rettype == ANYARRAYOID)
2787 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2788 errmsg(
"could not find array type for data type %s",
2791 return array_typeid;
2795 if (rettype == ANYRANGEOID)
2800 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2801 errmsg_internal(
"could not determine polymorphic type %s because input has type %s",
2802 "anyrange",
"unknown")));
2803 return range_typeid;
2807 if (rettype == ANYMULTIRANGEOID)
2812 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2813 errmsg_internal(
"could not determine polymorphic type %s because input has type %s",
2814 "anymultirange",
"unknown")));
2815 return multirange_typeid;
2819 if (rettype == ANYCOMPATIBLEOID ||
2820 rettype == ANYCOMPATIBLENONARRAYOID)
2825 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2827 return anycompatible_typeid;
2831 if (rettype == ANYCOMPATIBLEARRAYOID)
2836 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2838 return anycompatible_array_typeid;
2842 if (rettype == ANYCOMPATIBLERANGEOID)
2847 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2849 return anycompatible_range_typeid;
2853 if (rettype == ANYCOMPATIBLEMULTIRANGEOID)
2856 if (!
OidIsValid(anycompatible_multirange_typeid))
2858 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2860 return anycompatible_multirange_typeid;
2877 const Oid *declared_arg_types,
2880 if (ret_type == ANYRANGEOID || ret_type == ANYMULTIRANGEOID)
2887 for (
int i = 0;
i < nargs;
i++)
2889 if (declared_arg_types[
i] == ANYRANGEOID ||
2890 declared_arg_types[
i] == ANYMULTIRANGEOID)
2893 return psprintf(
_(
"A result of type %s requires at least one input of type anyrange or anymultirange."),
2896 else if (ret_type == ANYCOMPATIBLERANGEOID || ret_type == ANYCOMPATIBLEMULTIRANGEOID)
2904 for (
int i = 0;
i < nargs;
i++)
2906 if (declared_arg_types[
i] == ANYCOMPATIBLERANGEOID ||
2907 declared_arg_types[
i] == ANYCOMPATIBLEMULTIRANGEOID)
2910 return psprintf(
_(
"A result of type %s requires at least one input of type anycompatiblerange or anycompatiblemultirange."),
2913 else if (IsPolymorphicTypeFamily1(ret_type))
2916 for (
int i = 0;
i < nargs;
i++)
2918 if (IsPolymorphicTypeFamily1(declared_arg_types[
i]))
2922 return psprintf(
_(
"A result of type %s requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange."),
2925 else if (IsPolymorphicTypeFamily2(ret_type))
2928 for (
int i = 0;
i < nargs;
i++)
2930 if (IsPolymorphicTypeFamily2(declared_arg_types[
i]))
2934 return psprintf(
_(
"A result of type %s requires at least one input of type anycompatible, anycompatiblearray, anycompatiblenonarray, anycompatiblerange, or anycompatiblemultirange."),
2954 const Oid *declared_arg_types,
2957 if (ret_type == INTERNALOID)
2959 for (
int i = 0;
i < nargs;
i++)
2961 if (declared_arg_types[
i] == ret_type)
2964 return pstrdup(
_(
"A result of type internal requires at least one input of type internal."));
2980 bool typispreferred;
2983 Assert(typcategory != TYPCATEGORY_INVALID);
2999 bool typispreferred;
3002 if (category == typcategory || category == TYPCATEGORY_INVALID)
3003 return typispreferred;
3056 if (srctype == targettype)
3060 if (targettype == ANYOID || targettype == ANYELEMENTOID ||
3061 targettype == ANYCOMPATIBLEOID)
3069 if (srctype == targettype)
3073 if (targettype == ANYARRAYOID || targettype == ANYCOMPATIBLEARRAYOID)
3078 if (targettype == ANYNONARRAYOID || targettype == ANYCOMPATIBLENONARRAYOID)
3083 if (targettype == ANYENUMOID)
3088 if (targettype == ANYRANGEOID || targettype == ANYCOMPATIBLERANGEOID)
3093 if (targettype == ANYMULTIRANGEOID || targettype == ANYCOMPATIBLEMULTIRANGEOID)
3098 if (targettype == RECORDOID)
3103 if (targettype == RECORDARRAYOID)
3115 result = (castForm->castmethod == COERCION_METHOD_BINARY &&
3116 castForm->castcontext == COERCION_CODE_IMPLICIT);
3119 *castoid = castForm->oid;
3170 if (sourceTypeId == targetTypeId)
3184 switch (castForm->castcontext)
3186 case COERCION_CODE_IMPLICIT:
3189 case COERCION_CODE_ASSIGNMENT:
3192 case COERCION_CODE_EXPLICIT:
3196 elog(
ERROR,
"unrecognized castcontext: %d",
3197 (
int) castForm->castcontext);
3203 if (ccontext >= castcontext)
3205 switch (castForm->castmethod)
3207 case COERCION_METHOD_FUNCTION:
3209 *funcid = castForm->castfunc;
3211 case COERCION_METHOD_INOUT:
3214 case COERCION_METHOD_BINARY:
3218 elog(
ERROR,
"unrecognized castmethod: %d",
3219 (
int) castForm->castmethod);
3240 if (targetTypeId != OIDVECTOROID && targetTypeId != INT2VECTOROID)
3332 if (IsTrueArrayType(typeForm))
3335 typeId = typeForm->typelem;
3349 *funcid = castForm->castfunc;
3384 bool result =
false;
3393 elog(
ERROR,
"cache lookup failed for relation %u", relid);
3396 if (reltup->reloftype == reloftypeId)
#define InvalidAttrNumber
#define OidIsValid(objectId)
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
int errmsg_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define PG_DETOAST_DATUM(datum)
Assert(PointerIsAligned(start, uint64))
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
List * lappend(List *list, void *datum)
Oid get_range_subtype(Oid rangeOid)
Oid get_element_type(Oid typid)
bool type_is_range(Oid typid)
bool type_is_enum(Oid typid)
Oid get_multirange_range(Oid multirangeOid)
Oid get_range_multirange(Oid rangeOid)
bool type_is_collatable(Oid typid)
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Oid getBaseType(Oid typid)
Oid get_array_type(Oid typid)
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
bool type_is_multirange(Oid typid)
#define type_is_array(typid)
#define type_is_array_domain(typid)
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
char * pstrdup(const char *in)
Oid exprType(const Node *expr)
int32 exprTypmod(const Node *expr)
Oid exprCollation(const Node *expr)
Node * applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat, int rlocation, bool overwrite_ok)
int exprLocation(const Node *expr)
bool expression_returns_set(Node *clause)
#define IsA(nodeptr, _type_)
TYPCATEGORY TypeCategory(Oid type)
static bool verify_common_type_from_oids(Oid common_type, int nargs, const Oid *typeids)
Oid enforce_generic_type_consistency(const Oid *actual_arg_types, Oid *declared_arg_types, int nargs, Oid rettype, bool allow_poly)
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
static bool typeIsOfTypedTable(Oid reltypeId, Oid reloftypeId)
static Node * coerce_record_to_complex(ParseState *pstate, Node *node, Oid targetTypeId, CoercionContext ccontext, CoercionForm cformat, int location)
CoercionPathType find_typmod_coercion_function(Oid typeId, Oid *funcid)
static Node * coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
bool check_generic_type_consistency(const Oid *actual_arg_types, const Oid *declared_arg_types, int nargs)
bool verify_common_type(Oid common_type, List *exprs)
char * check_valid_internal_signature(Oid ret_type, const Oid *declared_arg_types, int nargs)
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
char * check_valid_polymorphic_signature(Oid ret_type, const Oid *declared_arg_types, int nargs)
Node * coerce_to_specific_type_typmod(ParseState *pstate, Node *node, Oid targetTypeId, int32 targetTypmod, const char *constructName)
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr)
Node * coerce_null_to_domain(Oid typid, int32 typmod, Oid collation, int typlen, bool typbyval)
int32 select_common_typmod(ParseState *pstate, List *exprs, Oid common_type)
static Node * build_coercion_expression(Node *node, CoercionPathType pathtype, Oid funcId, Oid targetTypeId, int32 targetTypMod, CoercionContext ccontext, CoercionForm cformat, int location)
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
static bool is_complex_array(Oid typid)
bool IsBinaryCoercible(Oid srctype, Oid targettype)
bool IsPreferredType(TYPCATEGORY category, Oid type)
static void hide_coercion_node(Node *node)
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
bool can_coerce_type(int nargs, const Oid *input_typeids, const Oid *target_typeids, CoercionContext ccontext)
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
static Oid select_common_type_from_oids(int nargs, const Oid *typeids, bool noerror)
bool IsBinaryCoercibleWithCast(Oid srctype, Oid targettype, Oid *castoid)
@ COERCION_PATH_COERCEVIAIO
@ COERCION_PATH_ARRAYCOERCE
@ COERCION_PATH_RELABELTYPE
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
int parser_errposition(ParseState *pstate, int location)
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
List * expandNSItemVars(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
ParseNamespaceItem * GetNSItemByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
Oid typeOrDomainTypeRelid(Oid type_id)
Oid typeTypeCollation(Type typ)
char * typeTypeName(Type t)
Datum stringTypeDatum(Type tp, char *string, int32 atttypmod)
#define ISCOMPLEX(typeid)
FormData_pg_attribute * Form_pg_attribute
FormData_pg_cast * Form_pg_cast
FormData_pg_class * Form_pg_class
bool typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
#define for_each_cell(cell, lst, initcell)
static ListCell * list_head(const List *l)
static ListCell * list_second_cell(const List *l)
static ListCell * lnext(const List *l, const ListCell *c)
FormData_pg_proc * Form_pg_proc
FormData_pg_type * Form_pg_type
static Datum PointerGetDatum(const void *X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static char * DatumGetCString(Datum X)
static Datum Int32GetDatum(int32 X)
char * psprintf(const char *fmt,...)
CoerceParamHook p_coerce_param_hook
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
#define ReleaseTupleDesc(tupdesc)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)