PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parse_coerce.h File Reference
Include dependency graph for parse_coerce.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef char TYPCATEGORY
 
typedef enum CoercionPathType CoercionPathType
 

Enumerations

enum  CoercionPathType {
  COERCION_PATH_NONE, COERCION_PATH_FUNC, COERCION_PATH_RELABELTYPE, COERCION_PATH_ARRAYCOERCE,
  COERCION_PATH_COERCEVIAIO
}
 

Functions

bool IsBinaryCoercible (Oid srctype, Oid targettype)
 
bool IsPreferredType (TYPCATEGORY category, Oid type)
 
TYPCATEGORY TypeCategory (Oid type)
 
Nodecoerce_to_target_type (ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
 
bool can_coerce_type (int nargs, Oid *input_typeids, Oid *target_typeids, CoercionContext ccontext)
 
Nodecoerce_type (ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
 
Nodecoerce_to_domain (Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionForm cformat, int location, bool hideInputCoercion, bool lengthCoercionDone)
 
Nodecoerce_to_boolean (ParseState *pstate, Node *node, const char *constructName)
 
Nodecoerce_to_specific_type (ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
 
Nodecoerce_to_specific_type_typmod (ParseState *pstate, Node *node, Oid targetTypeId, int32 targetTypmod, const char *constructName)
 
int parser_coercion_errposition (ParseState *pstate, int coerce_location, Node *input_expr)
 
Oid select_common_type (ParseState *pstate, List *exprs, const char *context, Node **which_expr)
 
Nodecoerce_to_common_type (ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
 
bool check_generic_type_consistency (Oid *actual_arg_types, Oid *declared_arg_types, int nargs)
 
Oid enforce_generic_type_consistency (Oid *actual_arg_types, Oid *declared_arg_types, int nargs, Oid rettype, bool allow_poly)
 
Oid resolve_generic_type (Oid declared_type, Oid context_actual_type, Oid context_declared_type)
 
CoercionPathType find_coercion_pathway (Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
 
CoercionPathType find_typmod_coercion_function (Oid typeId, Oid *funcid)
 

Typedef Documentation

Definition at line 21 of file parse_coerce.h.

Enumeration Type Documentation

Enumerator
COERCION_PATH_NONE 
COERCION_PATH_FUNC 
COERCION_PATH_RELABELTYPE 
COERCION_PATH_ARRAYCOERCE 
COERCION_PATH_COERCEVIAIO 

Definition at line 24 of file parse_coerce.h.

25 {
26  COERCION_PATH_NONE, /* failed to find any coercion pathway */
27  COERCION_PATH_FUNC, /* apply the specified coercion function */
28  COERCION_PATH_RELABELTYPE, /* binary-compatible cast, no function */
29  COERCION_PATH_ARRAYCOERCE, /* need an ArrayCoerceExpr node */
30  COERCION_PATH_COERCEVIAIO /* need a CoerceViaIO node */
CoercionPathType
Definition: parse_coerce.h:24

Function Documentation

bool can_coerce_type ( int  nargs,
Oid input_typeids,
Oid target_typeids,
CoercionContext  ccontext 
)

Definition at line 527 of file parse_coerce.c.

References ANYOID, check_generic_type_consistency(), COERCION_PATH_NONE, find_coercion_pathway(), i, is_complex_array(), ISCOMPLEX, IsPolymorphicType, RECORDARRAYOID, RECORDOID, typeInheritsFrom(), typeIsOfTypedTable(), and UNKNOWNOID.

Referenced by ATAddForeignKeyConstraint(), coerce_to_common_type(), coerce_to_target_type(), func_match_argtypes(), func_select_candidate(), and select_common_type().

529 {
530  bool have_generics = false;
531  int i;
532 
533  /* run through argument list... */
534  for (i = 0; i < nargs; i++)
535  {
536  Oid inputTypeId = input_typeids[i];
537  Oid targetTypeId = target_typeids[i];
538  CoercionPathType pathtype;
539  Oid funcId;
540 
541  /* no problem if same type */
542  if (inputTypeId == targetTypeId)
543  continue;
544 
545  /* accept if target is ANY */
546  if (targetTypeId == ANYOID)
547  continue;
548 
549  /* accept if target is polymorphic, for now */
550  if (IsPolymorphicType(targetTypeId))
551  {
552  have_generics = true; /* do more checking later */
553  continue;
554  }
555 
556  /*
557  * If input is an untyped string constant, assume we can convert it to
558  * anything.
559  */
560  if (inputTypeId == UNKNOWNOID)
561  continue;
562 
563  /*
564  * If pg_cast shows that we can coerce, accept. This test now covers
565  * both binary-compatible and coercion-function cases.
566  */
567  pathtype = find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
568  &funcId);
569  if (pathtype != COERCION_PATH_NONE)
570  continue;
571 
572  /*
573  * If input is RECORD and target is a composite type, assume we can
574  * coerce (may need tighter checking here)
575  */
576  if (inputTypeId == RECORDOID &&
577  ISCOMPLEX(targetTypeId))
578  continue;
579 
580  /*
581  * If input is a composite type and target is RECORD, accept
582  */
583  if (targetTypeId == RECORDOID &&
584  ISCOMPLEX(inputTypeId))
585  continue;
586 
587 #ifdef NOT_USED /* not implemented yet */
588 
589  /*
590  * If input is record[] and target is a composite array type, assume
591  * we can coerce (may need tighter checking here)
592  */
593  if (inputTypeId == RECORDARRAYOID &&
594  is_complex_array(targetTypeId))
595  continue;
596 #endif
597 
598  /*
599  * If input is a composite array type and target is record[], accept
600  */
601  if (targetTypeId == RECORDARRAYOID &&
602  is_complex_array(inputTypeId))
603  continue;
604 
605  /*
606  * If input is a class type that inherits from target, accept
607  */
608  if (typeInheritsFrom(inputTypeId, targetTypeId)
609  || typeIsOfTypedTable(inputTypeId, targetTypeId))
610  continue;
611 
612  /*
613  * Else, cannot coerce at this argument position
614  */
615  return false;
616  }
617 
618  /* If we found any generic argument types, cross-check them */
619  if (have_generics)
620  {
621  if (!check_generic_type_consistency(input_typeids, target_typeids,
622  nargs))
623  return false;
624  }
625 
626  return true;
627 }
#define RECORDARRAYOID
Definition: pg_type.h:682
unsigned int Oid
Definition: postgres_ext.h:31
CoercionPathType
Definition: parse_coerce.h:24
#define IsPolymorphicType(typid)
Definition: pg_type.h:745
#define ANYOID
Definition: pg_type.h:686
#define RECORDOID
Definition: pg_type.h:680
bool check_generic_type_consistency(Oid *actual_arg_types, Oid *declared_arg_types, int nargs)
bool typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
Definition: pg_inherits.c:306
#define ISCOMPLEX(typeid)
Definition: parse_type.h:53
static bool typeIsOfTypedTable(Oid reltypeId, Oid reloftypeId)
#define UNKNOWNOID
Definition: pg_type.h:431
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
int i
static bool is_complex_array(Oid typid)
bool check_generic_type_consistency ( Oid actual_arg_types,
Oid declared_arg_types,
int  nargs 
)

Definition at line 1405 of file parse_coerce.c.

References ANYARRAYOID, ANYELEMENTOID, ANYENUMOID, ANYNONARRAYOID, ANYRANGEOID, get_element_type(), get_range_subtype(), getBaseType(), InvalidOid, OidIsValid, type_is_array_domain, type_is_enum(), and UNKNOWNOID.

Referenced by can_coerce_type().

1408 {
1409  int j;
1410  Oid elem_typeid = InvalidOid;
1411  Oid array_typeid = InvalidOid;
1412  Oid array_typelem;
1413  Oid range_typeid = InvalidOid;
1414  Oid range_typelem;
1415  bool have_anyelement = false;
1416  bool have_anynonarray = false;
1417  bool have_anyenum = false;
1418 
1419  /*
1420  * Loop through the arguments to see if we have any that are polymorphic.
1421  * If so, require the actual types to be consistent.
1422  */
1423  for (j = 0; j < nargs; j++)
1424  {
1425  Oid decl_type = declared_arg_types[j];
1426  Oid actual_type = actual_arg_types[j];
1427 
1428  if (decl_type == ANYELEMENTOID ||
1429  decl_type == ANYNONARRAYOID ||
1430  decl_type == ANYENUMOID)
1431  {
1432  have_anyelement = true;
1433  if (decl_type == ANYNONARRAYOID)
1434  have_anynonarray = true;
1435  else if (decl_type == ANYENUMOID)
1436  have_anyenum = true;
1437  if (actual_type == UNKNOWNOID)
1438  continue;
1439  if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
1440  return false;
1441  elem_typeid = actual_type;
1442  }
1443  else if (decl_type == ANYARRAYOID)
1444  {
1445  if (actual_type == UNKNOWNOID)
1446  continue;
1447  actual_type = getBaseType(actual_type); /* flatten domains */
1448  if (OidIsValid(array_typeid) && actual_type != array_typeid)
1449  return false;
1450  array_typeid = actual_type;
1451  }
1452  else if (decl_type == ANYRANGEOID)
1453  {
1454  if (actual_type == UNKNOWNOID)
1455  continue;
1456  actual_type = getBaseType(actual_type); /* flatten domains */
1457  if (OidIsValid(range_typeid) && actual_type != range_typeid)
1458  return false;
1459  range_typeid = actual_type;
1460  }
1461  }
1462 
1463  /* Get the element type based on the array type, if we have one */
1464  if (OidIsValid(array_typeid))
1465  {
1466  if (array_typeid == ANYARRAYOID)
1467  {
1468  /* Special case for ANYARRAY input: okay iff no ANYELEMENT */
1469  if (have_anyelement)
1470  return false;
1471  return true;
1472  }
1473 
1474  array_typelem = get_element_type(array_typeid);
1475  if (!OidIsValid(array_typelem))
1476  return false; /* should be an array, but isn't */
1477 
1478  if (!OidIsValid(elem_typeid))
1479  {
1480  /*
1481  * if we don't have an element type yet, use the one we just got
1482  */
1483  elem_typeid = array_typelem;
1484  }
1485  else if (array_typelem != elem_typeid)
1486  {
1487  /* otherwise, they better match */
1488  return false;
1489  }
1490  }
1491 
1492  /* Get the element type based on the range type, if we have one */
1493  if (OidIsValid(range_typeid))
1494  {
1495  range_typelem = get_range_subtype(range_typeid);
1496  if (!OidIsValid(range_typelem))
1497  return false; /* should be a range, but isn't */
1498 
1499  if (!OidIsValid(elem_typeid))
1500  {
1501  /*
1502  * if we don't have an element type yet, use the one we just got
1503  */
1504  elem_typeid = range_typelem;
1505  }
1506  else if (range_typelem != elem_typeid)
1507  {
1508  /* otherwise, they better match */
1509  return false;
1510  }
1511  }
1512 
1513  if (have_anynonarray)
1514  {
1515  /* require the element type to not be an array or domain over array */
1516  if (type_is_array_domain(elem_typeid))
1517  return false;
1518  }
1519 
1520  if (have_anyenum)
1521  {
1522  /* require the element type to be an enum */
1523  if (!type_is_enum(elem_typeid))
1524  return false;
1525  }
1526 
1527  /* Looks valid */
1528  return true;
1529 }
#define ANYNONARRAYOID
Definition: pg_type.h:704
#define type_is_array_domain(typid)
Definition: lsyscache.h:182
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2484
#define ANYELEMENTOID
Definition: pg_type.h:702
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define ANYRANGEOID
Definition: pg_type.h:714
#define ANYENUMOID
Definition: pg_type.h:706
#define ANYARRAYOID
Definition: pg_type.h:688
#define InvalidOid
Definition: postgres_ext.h:36
bool type_is_enum(Oid typid)
Definition: lsyscache.c:2414
#define UNKNOWNOID
Definition: pg_type.h:431
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
Oid get_range_subtype(Oid rangeOid)
Definition: lsyscache.c:3074
Node* coerce_to_boolean ( ParseState pstate,
Node node,
const char *  constructName 
)

Definition at line 1063 of file parse_coerce.c.

References BOOLOID, COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, ereport, errcode(), errmsg(), ERROR, expression_returns_set(), exprLocation(), exprType(), format_type_be(), NULL, and parser_errposition().

Referenced by cookConstraint(), domainAddConstraint(), transformAExprIn(), transformBooleanTest(), transformBoolExpr(), transformCaseExpr(), transformJoinUsingClause(), transformWhereClause(), and transformXmlExpr().

1065 {
1066  Oid inputTypeId = exprType(node);
1067 
1068  if (inputTypeId != BOOLOID)
1069  {
1070  Node *newnode;
1071 
1072  newnode = coerce_to_target_type(pstate, node, inputTypeId,
1073  BOOLOID, -1,
1076  -1);
1077  if (newnode == NULL)
1078  ereport(ERROR,
1079  (errcode(ERRCODE_DATATYPE_MISMATCH),
1080  /* translator: first %s is name of a SQL construct, eg WHERE */
1081  errmsg("argument of %s must be type %s, not type %s",
1082  constructName, "boolean",
1083  format_type_be(inputTypeId)),
1084  parser_errposition(pstate, exprLocation(node))));
1085  node = newnode;
1086  }
1087 
1088  if (expression_returns_set(node))
1089  ereport(ERROR,
1090  (errcode(ERRCODE_DATATYPE_MISMATCH),
1091  /* translator: %s is name of a SQL construct, eg WHERE */
1092  errmsg("argument of %s must not return a set",
1093  constructName),
1094  parser_errposition(pstate, exprLocation(node))));
1095 
1096  return node;
1097 }
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:77
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define BOOLOID
Definition: pg_type.h:288
int errmsg(const char *fmt,...)
Definition: elog.c:797
Node* coerce_to_common_type ( ParseState pstate,
Node node,
Oid  targetTypeId,
const char *  context 
)

Definition at line 1334 of file parse_coerce.c.

References can_coerce_type(), COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, ereport, errcode(), errmsg(), ERROR, exprLocation(), exprType(), format_type_be(), and parser_errposition().

Referenced by generate_setop_tlist(), transformAExprIn(), transformArrayExpr(), transformCaseExpr(), transformCoalesceExpr(), transformMinMaxExpr(), transformSetOperationTree(), and transformValuesClause().

1336 {
1337  Oid inputTypeId = exprType(node);
1338 
1339  if (inputTypeId == targetTypeId)
1340  return node; /* no work */
1341  if (can_coerce_type(1, &inputTypeId, &targetTypeId, COERCION_IMPLICIT))
1342  node = coerce_type(pstate, node, inputTypeId, targetTypeId, -1,
1344  else
1345  ereport(ERROR,
1346  (errcode(ERRCODE_CANNOT_COERCE),
1347  /* translator: first %s is name of a SQL construct, eg CASE */
1348  errmsg("%s could not convert type %s to %s",
1349  context,
1350  format_type_be(inputTypeId),
1351  format_type_be(targetTypeId)),
1352  parser_errposition(pstate, exprLocation(node))));
1353  return node;
1354 }
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:156
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
bool can_coerce_type(int nargs, Oid *input_typeids, Oid *target_typeids, CoercionContext ccontext)
Definition: parse_coerce.c:527
int errmsg(const char *fmt,...)
Definition: elog.c:797
Node* coerce_to_domain ( Node arg,
Oid  baseTypeId,
int32  baseTypeMod,
Oid  typeId,
CoercionForm  cformat,
int  location,
bool  hideInputCoercion,
bool  lengthCoercionDone 
)

Definition at line 648 of file parse_coerce.c.

References arg, CoerceToDomain::arg, COERCE_IMPLICIT_CAST, coerce_type_typmod(), CoerceToDomain::coercionformat, getBaseTypeAndTypmod(), hide_coercion_node(), InvalidOid, CoerceToDomain::location, makeNode, result, CoerceToDomain::resulttype, and CoerceToDomain::resulttypmod.

Referenced by coerce_type(), expand_targetlist(), ReplaceVarsFromTargetList_callback(), rewriteTargetListIU(), and rewriteValuesRTE().

652 {
654 
655  /* Get the base type if it hasn't been supplied */
656  if (baseTypeId == InvalidOid)
657  baseTypeId = getBaseTypeAndTypmod(typeId, &baseTypeMod);
658 
659  /* If it isn't a domain, return the node as it was passed in */
660  if (baseTypeId == typeId)
661  return arg;
662 
663  /* Suppress display of nested coercion steps */
664  if (hideInputCoercion)
665  hide_coercion_node(arg);
666 
667  /*
668  * If the domain applies a typmod to its base type, build the appropriate
669  * coercion step. Mark it implicit for display purposes, because we don't
670  * want it shown separately by ruleutils.c; but the isExplicit flag passed
671  * to the conversion function depends on the manner in which the domain
672  * coercion is invoked, so that the semantics of implicit and explicit
673  * coercion differ. (Is that really the behavior we want?)
674  *
675  * NOTE: because we apply this as part of the fixed expression structure,
676  * ALTER DOMAIN cannot alter the typtypmod. But it's unclear that that
677  * would be safe to do anyway, without lots of knowledge about what the
678  * base type thinks the typmod means.
679  */
680  if (!lengthCoercionDone)
681  {
682  if (baseTypeMod >= 0)
683  arg = coerce_type_typmod(arg, baseTypeId, baseTypeMod,
684  COERCE_IMPLICIT_CAST, location,
685  (cformat != COERCE_IMPLICIT_CAST),
686  false);
687  }
688 
689  /*
690  * Now build the domain coercion node. This represents run-time checking
691  * of any constraints currently attached to the domain. This also ensures
692  * that the expression is properly labeled as to result type.
693  */
694  result = makeNode(CoerceToDomain);
695  result->arg = (Expr *) arg;
696  result->resulttype = typeId;
697  result->resulttypmod = -1; /* currently, always -1 for domains */
698  /* resultcollid will be set by parse_collate.c */
699  result->coercionformat = cformat;
700  result->location = location;
701 
702  return (Node *) result;
703 }
static Node * coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod, CoercionForm cformat, int location, bool isExplicit, bool hideInputCoercion)
Definition: parse_coerce.c:728
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2288
int32 resulttypmod
Definition: primnodes.h:1222
Definition: nodes.h:509
CoercionForm coercionformat
Definition: primnodes.h:1224
return result
Definition: formatting.c:1633
static void hide_coercion_node(Node *node)
Definition: parse_coerce.c:770
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:557
void * arg
Node* coerce_to_specific_type ( ParseState pstate,
Node node,
Oid  targetTypeId,
const char *  constructName 
)

Definition at line 1159 of file parse_coerce.c.

References coerce_to_specific_type_typmod().

Referenced by interpret_function_parameter_list(), transformFrameOffset(), transformLimitClause(), transformRangeTableFunc(), transformRangeTableSample(), transformXmlExpr(), and transformXmlSerialize().

1162 {
1163  return coerce_to_specific_type_typmod(pstate, node,
1164  targetTypeId, -1,
1165  constructName);
1166 }
Node * coerce_to_specific_type_typmod(ParseState *pstate, Node *node, Oid targetTypeId, int32 targetTypmod, const char *constructName)
Node* coerce_to_specific_type_typmod ( ParseState pstate,
Node node,
Oid  targetTypeId,
int32  targetTypmod,
const char *  constructName 
)

Definition at line 1110 of file parse_coerce.c.

References COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, ereport, errcode(), errmsg(), ERROR, expression_returns_set(), exprLocation(), exprType(), format_type_be(), NULL, and parser_errposition().

Referenced by coerce_to_specific_type(), and transformRangeTableFunc().

1113 {
1114  Oid inputTypeId = exprType(node);
1115 
1116  if (inputTypeId != targetTypeId)
1117  {
1118  Node *newnode;
1119 
1120  newnode = coerce_to_target_type(pstate, node, inputTypeId,
1121  targetTypeId, targetTypmod,
1124  -1);
1125  if (newnode == NULL)
1126  ereport(ERROR,
1127  (errcode(ERRCODE_DATATYPE_MISMATCH),
1128  /* translator: first %s is name of a SQL construct, eg LIMIT */
1129  errmsg("argument of %s must be type %s, not type %s",
1130  constructName,
1131  format_type_be(targetTypeId),
1132  format_type_be(inputTypeId)),
1133  parser_errposition(pstate, exprLocation(node))));
1134  node = newnode;
1135  }
1136 
1137  if (expression_returns_set(node))
1138  ereport(ERROR,
1139  (errcode(ERRCODE_DATATYPE_MISMATCH),
1140  /* translator: %s is name of a SQL construct, eg LIMIT */
1141  errmsg("argument of %s must not return a set",
1142  constructName),
1143  parser_errposition(pstate, exprLocation(node))));
1144 
1145  return node;
1146 }
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:77
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
Node* coerce_to_target_type ( ParseState pstate,
Node expr,
Oid  exprtype,
Oid  targettype,
int32  targettypmod,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location 
)

Definition at line 77 of file parse_coerce.c.

References arg, CollateExpr::arg, can_coerce_type(), COERCE_IMPLICIT_CAST, coerce_type(), coerce_type_typmod(), CollateExpr::collOid, IsA, CollateExpr::location, makeNode, NULL, and result.

Referenced by ATExecAddColumn(), ATExecAlterColumnType(), ATPrepAlterColumnType(), build_column_default(), coerce_record_to_complex(), coerce_to_boolean(), coerce_to_specific_type_typmod(), cookDefault(), EvaluateParams(), get_cast_hashentry(), transformArrayExpr(), transformArraySubscripts(), transformAssignedExpr(), transformAssignmentIndirection(), transformAssignmentSubscripts(), transformPartitionBoundValue(), transformTypeCast(), and transformXmlSerialize().

82 {
83  Node *result;
84  Node *origexpr;
85 
86  if (!can_coerce_type(1, &exprtype, &targettype, ccontext))
87  return NULL;
88 
89  /*
90  * If the input has a CollateExpr at the top, strip it off, perform the
91  * coercion, and put a new one back on. This is annoying since it
92  * duplicates logic in coerce_type, but if we don't do this then it's too
93  * hard to tell whether coerce_type actually changed anything, and we
94  * *must* know that to avoid possibly calling hide_coercion_node on
95  * something that wasn't generated by coerce_type. Note that if there are
96  * multiple stacked CollateExprs, we just discard all but the topmost.
97  */
98  origexpr = expr;
99  while (expr && IsA(expr, CollateExpr))
100  expr = (Node *) ((CollateExpr *) expr)->arg;
101 
102  result = coerce_type(pstate, expr, exprtype,
103  targettype, targettypmod,
104  ccontext, cformat, location);
105 
106  /*
107  * If the target is a fixed-length type, it may need a length coercion as
108  * well as a type coercion. If we find ourselves adding both, force the
109  * inner coercion node to implicit display form.
110  */
111  result = coerce_type_typmod(result,
112  targettype, targettypmod,
113  cformat, location,
114  (cformat != COERCE_IMPLICIT_CAST),
115  (result != expr && !IsA(result, Const)));
116 
117  if (expr != origexpr)
118  {
119  /* Reinstall top CollateExpr */
120  CollateExpr *coll = (CollateExpr *) origexpr;
121  CollateExpr *newcoll = makeNode(CollateExpr);
122 
123  newcoll->arg = (Expr *) result;
124  newcoll->collOid = coll->collOid;
125  newcoll->location = coll->location;
126  result = (Node *) newcoll;
127  }
128 
129  return result;
130 }
static Node * coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod, CoercionForm cformat, int location, bool isExplicit, bool hideInputCoercion)
Definition: parse_coerce.c:728
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:156
Definition: nodes.h:509
return result
Definition: formatting.c:1633
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
Expr * arg
Definition: primnodes.h:876
bool can_coerce_type(int nargs, Oid *input_typeids, Oid *target_typeids, CoercionContext ccontext)
Definition: parse_coerce.c:527
void * arg
int location
Definition: primnodes.h:878
Node* coerce_type ( ParseState pstate,
Node node,
Oid  inputTypeId,
Oid  targetTypeId,
int32  targetTypeMod,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location 
)

Definition at line 156 of file parse_coerce.c.

References ANYARRAYOID, ANYELEMENTOID, ANYENUMOID, ANYNONARRAYOID, ANYOID, ANYRANGEOID, ConvertRowtypeExpr::arg, CollateExpr::arg, build_coercion_expression(), cancel_parser_errposition_callback(), COERCE_IMPLICIT_CAST, coerce_record_to_complex(), coerce_to_domain(), coerce_type(), COERCION_PATH_NONE, COERCION_PATH_RELABELTYPE, CollateExpr::collOid, Const::constbyval, Const::constcollid, Const::constisnull, Const::constlen, Const::consttype, Const::consttypmod, Const::constvalue, ConvertRowtypeExpr::convertformat, DatumGetCString, datumIsEqual(), elog, ERROR, exprIsLengthCoercion(), find_coercion_pathway(), format_type_be(), getBaseType(), getBaseTypeAndTypmod(), INTERVALOID, InvalidOid, is_complex_array(), IsA, ISCOMPLEX, Const::location, RelabelType::location, ConvertRowtypeExpr::location, CollateExpr::location, makeNode, makeRelabelType(), NULL, ParseState::p_coerce_param_hook, PG_DETOAST_DATUM, PointerGetDatum, RECORDARRAYOID, RECORDOID, ReleaseSysCache(), result, ConvertRowtypeExpr::resulttype, setup_parser_errposition_callback(), stringTypeDatum(), typeByVal(), typeidType(), typeInheritsFrom(), typeIsOfTypedTable(), typeLen(), typeTypeCollation(), typeTypeName(), UNKNOWNOID, and WARNING.

Referenced by addTargetToGroupList(), addTargetToSortList(), buildMergedJoinVar(), coerce_to_common_type(), coerce_to_target_type(), coerce_type(), make_fn_arguments(), ParseFuncOrColumn(), resolveTargetListUnknowns(), transformArrayExpr(), and unify_hypothetical_args().

159 {
160  Node *result;
161  CoercionPathType pathtype;
162  Oid funcId;
163 
164  if (targetTypeId == inputTypeId ||
165  node == NULL)
166  {
167  /* no conversion needed */
168  return node;
169  }
170  if (targetTypeId == ANYOID ||
171  targetTypeId == ANYELEMENTOID ||
172  targetTypeId == ANYNONARRAYOID)
173  {
174  /*
175  * Assume can_coerce_type verified that implicit coercion is okay.
176  *
177  * Note: by returning the unmodified node here, we are saying that
178  * it's OK to treat an UNKNOWN constant as a valid input for a
179  * function accepting ANY, ANYELEMENT, or ANYNONARRAY. This should be
180  * all right, since an UNKNOWN value is still a perfectly valid Datum.
181  *
182  * NB: we do NOT want a RelabelType here: the exposed type of the
183  * function argument must be its actual type, not the polymorphic
184  * pseudotype.
185  */
186  return node;
187  }
188  if (targetTypeId == ANYARRAYOID ||
189  targetTypeId == ANYENUMOID ||
190  targetTypeId == ANYRANGEOID)
191  {
192  /*
193  * Assume can_coerce_type verified that implicit coercion is okay.
194  *
195  * These cases are unlike the ones above because the exposed type of
196  * the argument must be an actual array, enum, or range type. In
197  * particular the argument must *not* be an UNKNOWN constant. If it
198  * is, we just fall through; below, we'll call anyarray_in,
199  * anyenum_in, or anyrange_in, which will produce an error. Also, if
200  * what we have is a domain over array, enum, or range, we have to
201  * relabel it to its base type.
202  *
203  * Note: currently, we can't actually see a domain-over-enum here,
204  * since the other functions in this file will not match such a
205  * parameter to ANYENUM. But that should get changed eventually.
206  */
207  if (inputTypeId != UNKNOWNOID)
208  {
209  Oid baseTypeId = getBaseType(inputTypeId);
210 
211  if (baseTypeId != inputTypeId)
212  {
213  RelabelType *r = makeRelabelType((Expr *) node,
214  baseTypeId, -1,
215  InvalidOid,
216  cformat);
217 
218  r->location = location;
219  return (Node *) r;
220  }
221  /* Not a domain type, so return it as-is */
222  return node;
223  }
224  }
225  if (inputTypeId == UNKNOWNOID && IsA(node, Const))
226  {
227  /*
228  * Input is a string constant with previously undetermined type. Apply
229  * the target type's typinput function to it to produce a constant of
230  * the target type.
231  *
232  * NOTE: this case cannot be folded together with the other
233  * constant-input case, since the typinput function does not
234  * necessarily behave the same as a type conversion function. For
235  * example, int4's typinput function will reject "1.2", whereas
236  * float-to-int type conversion will round to integer.
237  *
238  * XXX if the typinput function is not immutable, we really ought to
239  * postpone evaluation of the function call until runtime. But there
240  * is no way to represent a typinput function call as an expression
241  * tree, because C-string values are not Datums. (XXX This *is*
242  * possible as of 7.3, do we want to do it?)
243  */
244  Const *con = (Const *) node;
245  Const *newcon = makeNode(Const);
246  Oid baseTypeId;
247  int32 baseTypeMod;
248  int32 inputTypeMod;
249  Type baseType;
250  ParseCallbackState pcbstate;
251 
252  /*
253  * If the target type is a domain, we want to call its base type's
254  * input routine, not domain_in(). This is to avoid premature failure
255  * when the domain applies a typmod: existing input routines follow
256  * implicit-coercion semantics for length checks, which is not always
257  * what we want here. The needed check will be applied properly
258  * inside coerce_to_domain().
259  */
260  baseTypeMod = targetTypeMod;
261  baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
262 
263  /*
264  * For most types we pass typmod -1 to the input routine, because
265  * existing input routines follow implicit-coercion semantics for
266  * length checks, which is not always what we want here. Any length
267  * constraint will be applied later by our caller. An exception
268  * however is the INTERVAL type, for which we *must* pass the typmod
269  * or it won't be able to obey the bizarre SQL-spec input rules. (Ugly
270  * as sin, but so is this part of the spec...)
271  */
272  if (baseTypeId == INTERVALOID)
273  inputTypeMod = baseTypeMod;
274  else
275  inputTypeMod = -1;
276 
277  baseType = typeidType(baseTypeId);
278 
279  newcon->consttype = baseTypeId;
280  newcon->consttypmod = inputTypeMod;
281  newcon->constcollid = typeTypeCollation(baseType);
282  newcon->constlen = typeLen(baseType);
283  newcon->constbyval = typeByVal(baseType);
284  newcon->constisnull = con->constisnull;
285 
286  /*
287  * We use the original literal's location regardless of the position
288  * of the coercion. This is a change from pre-9.2 behavior, meant to
289  * simplify life for pg_stat_statements.
290  */
291  newcon->location = con->location;
292 
293  /*
294  * Set up to point at the constant's text if the input routine throws
295  * an error.
296  */
297  setup_parser_errposition_callback(&pcbstate, pstate, con->location);
298 
299  /*
300  * We assume here that UNKNOWN's internal representation is the same
301  * as CSTRING.
302  */
303  if (!con->constisnull)
304  newcon->constvalue = stringTypeDatum(baseType,
306  inputTypeMod);
307  else
308  newcon->constvalue = stringTypeDatum(baseType,
309  NULL,
310  inputTypeMod);
311 
312  /*
313  * If it's a varlena value, force it to be in non-expanded
314  * (non-toasted) format; this avoids any possible dependency on
315  * external values and improves consistency of representation.
316  */
317  if (!con->constisnull && newcon->constlen == -1)
318  newcon->constvalue =
320 
321 #ifdef RANDOMIZE_ALLOCATED_MEMORY
322 
323  /*
324  * For pass-by-reference data types, repeat the conversion to see if
325  * the input function leaves any uninitialized bytes in the result. We
326  * can only detect that reliably if RANDOMIZE_ALLOCATED_MEMORY is
327  * enabled, so we don't bother testing otherwise. The reason we don't
328  * want any instability in the input function is that comparison of
329  * Const nodes relies on bytewise comparison of the datums, so if the
330  * input function leaves garbage then subexpressions that should be
331  * identical may not get recognized as such. See pgsql-hackers
332  * discussion of 2008-04-04.
333  */
334  if (!con->constisnull && !newcon->constbyval)
335  {
336  Datum val2;
337 
338  val2 = stringTypeDatum(baseType,
340  inputTypeMod);
341  if (newcon->constlen == -1)
342  val2 = PointerGetDatum(PG_DETOAST_DATUM(val2));
343  if (!datumIsEqual(newcon->constvalue, val2, false, newcon->constlen))
344  elog(WARNING, "type %s has unstable input conversion for \"%s\"",
345  typeTypeName(baseType), DatumGetCString(con->constvalue));
346  }
347 #endif
348 
350 
351  result = (Node *) newcon;
352 
353  /* If target is a domain, apply constraints. */
354  if (baseTypeId != targetTypeId)
355  result = coerce_to_domain(result,
356  baseTypeId, baseTypeMod,
357  targetTypeId,
358  cformat, location, false, false);
359 
360  ReleaseSysCache(baseType);
361 
362  return result;
363  }
364  if (IsA(node, Param) &&
365  pstate != NULL && pstate->p_coerce_param_hook != NULL)
366  {
367  /*
368  * Allow the CoerceParamHook to decide what happens. It can return a
369  * transformed node (very possibly the same Param node), or return
370  * NULL to indicate we should proceed with normal coercion.
371  */
372  result = (*pstate->p_coerce_param_hook) (pstate,
373  (Param *) node,
374  targetTypeId,
375  targetTypeMod,
376  location);
377  if (result)
378  return result;
379  }
380  if (IsA(node, CollateExpr))
381  {
382  /*
383  * If we have a COLLATE clause, we have to push the coercion
384  * underneath the COLLATE. This is really ugly, but there is little
385  * choice because the above hacks on Consts and Params wouldn't happen
386  * otherwise. This kluge has consequences in coerce_to_target_type.
387  */
388  CollateExpr *coll = (CollateExpr *) node;
389  CollateExpr *newcoll = makeNode(CollateExpr);
390 
391  newcoll->arg = (Expr *)
392  coerce_type(pstate, (Node *) coll->arg,
393  inputTypeId, targetTypeId, targetTypeMod,
394  ccontext, cformat, location);
395  newcoll->collOid = coll->collOid;
396  newcoll->location = coll->location;
397  return (Node *) newcoll;
398  }
399  pathtype = find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
400  &funcId);
401  if (pathtype != COERCION_PATH_NONE)
402  {
403  if (pathtype != COERCION_PATH_RELABELTYPE)
404  {
405  /*
406  * Generate an expression tree representing run-time application
407  * of the conversion function. If we are dealing with a domain
408  * target type, the conversion function will yield the base type,
409  * and we need to extract the correct typmod to use from the
410  * domain's typtypmod.
411  */
412  Oid baseTypeId;
413  int32 baseTypeMod;
414 
415  baseTypeMod = targetTypeMod;
416  baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
417 
418  result = build_coercion_expression(node, pathtype, funcId,
419  baseTypeId, baseTypeMod,
420  cformat, location,
421  (cformat != COERCE_IMPLICIT_CAST));
422 
423  /*
424  * If domain, coerce to the domain type and relabel with domain
425  * type ID. We can skip the internal length-coercion step if the
426  * selected coercion function was a type-and-length coercion.
427  */
428  if (targetTypeId != baseTypeId)
429  result = coerce_to_domain(result, baseTypeId, baseTypeMod,
430  targetTypeId,
431  cformat, location, true,
432  exprIsLengthCoercion(result,
433  NULL));
434  }
435  else
436  {
437  /*
438  * We don't need to do a physical conversion, but we do need to
439  * attach a RelabelType node so that the expression will be seen
440  * to have the intended type when inspected by higher-level code.
441  *
442  * Also, domains may have value restrictions beyond the base type
443  * that must be accounted for. If the destination is a domain
444  * then we won't need a RelabelType node.
445  */
446  result = coerce_to_domain(node, InvalidOid, -1, targetTypeId,
447  cformat, location, false, false);
448  if (result == node)
449  {
450  /*
451  * XXX could we label result with exprTypmod(node) instead of
452  * default -1 typmod, to save a possible length-coercion
453  * later? Would work if both types have same interpretation of
454  * typmod, which is likely but not certain.
455  */
456  RelabelType *r = makeRelabelType((Expr *) result,
457  targetTypeId, -1,
458  InvalidOid,
459  cformat);
460 
461  r->location = location;
462  result = (Node *) r;
463  }
464  }
465  return result;
466  }
467  if (inputTypeId == RECORDOID &&
468  ISCOMPLEX(targetTypeId))
469  {
470  /* Coerce a RECORD to a specific complex type */
471  return coerce_record_to_complex(pstate, node, targetTypeId,
472  ccontext, cformat, location);
473  }
474  if (targetTypeId == RECORDOID &&
475  ISCOMPLEX(inputTypeId))
476  {
477  /* Coerce a specific complex type to RECORD */
478  /* NB: we do NOT want a RelabelType here */
479  return node;
480  }
481 #ifdef NOT_USED
482  if (inputTypeId == RECORDARRAYOID &&
483  is_complex_array(targetTypeId))
484  {
485  /* Coerce record[] to a specific complex array type */
486  /* not implemented yet ... */
487  }
488 #endif
489  if (targetTypeId == RECORDARRAYOID &&
490  is_complex_array(inputTypeId))
491  {
492  /* Coerce a specific complex array type to record[] */
493  /* NB: we do NOT want a RelabelType here */
494  return node;
495  }
496  if (typeInheritsFrom(inputTypeId, targetTypeId)
497  || typeIsOfTypedTable(inputTypeId, targetTypeId))
498  {
499  /*
500  * Input class type is a subclass of target, so generate an
501  * appropriate runtime conversion (removing unneeded columns and
502  * possibly rearranging the ones that are wanted).
503  */
505 
506  r->arg = (Expr *) node;
507  r->resulttype = targetTypeId;
508  r->convertformat = cformat;
509  r->location = location;
510  return (Node *) r;
511  }
512  /* If we get here, caller blew it */
513  elog(ERROR, "failed to find conversion function from %s to %s",
514  format_type_be(inputTypeId), format_type_be(targetTypeId));
515  return NULL; /* keep compiler quiet */
516 }
Datum constvalue
Definition: primnodes.h:196
#define RECORDARRAYOID
Definition: pg_type.h:682
#define ANYNONARRAYOID
Definition: pg_type.h:704
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2288
Oid typeTypeCollation(Type typ)
Definition: parse_type.c:621
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
bool constbyval
Definition: primnodes.h:199
int16 typeLen(Type t)
Definition: parse_type.c:580
#define PointerGetDatum(X)
Definition: postgres.h:562
#define ANYELEMENTOID
Definition: pg_type.h:702
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:219
static Node * build_coercion_expression(Node *node, CoercionPathType pathtype, Oid funcId, Oid targetTypeId, int32 targetTypMod, CoercionForm cformat, int location, bool isExplicit)
Definition: parse_coerce.c:798
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:213
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:156
Definition: nodes.h:509
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:513
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
CoercionPathType
Definition: parse_coerce.h:24
signed int int32
Definition: c.h:256
int constlen
Definition: primnodes.h:195
Oid consttype
Definition: primnodes.h:192
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
bool typeByVal(Type t)
Definition: parse_type.c:590
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:572
#define ANYRANGEOID
Definition: pg_type.h:714
char * typeTypeName(Type t)
Definition: parse_type.c:600
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionForm cformat, int location, bool hideInputCoercion, bool lengthCoercionDone)
Definition: parse_coerce.c:648
Datum stringTypeDatum(Type tp, char *string, int32 atttypmod)
Definition: parse_type.c:635
Oid constcollid
Definition: primnodes.h:194
#define ANYENUMOID
Definition: pg_type.h:706
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:399
#define INTERVALOID
Definition: pg_type.h:529
#define ANYOID
Definition: pg_type.h:686
#define RECORDOID
Definition: pg_type.h:680
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
#define WARNING
Definition: elog.h:40
int location
Definition: primnodes.h:203
#define ANYARRAYOID
Definition: pg_type.h:688
uintptr_t Datum
Definition: postgres.h:372
CoercionForm convertformat
Definition: primnodes.h:862
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
bool typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
Definition: pg_inherits.c:306
#define ISCOMPLEX(typeid)
Definition: parse_type.h:53
static bool typeIsOfTypedTable(Oid reltypeId, Oid reloftypeId)
Expr * arg
Definition: primnodes.h:876
#define UNKNOWNOID
Definition: pg_type.h:431
static Node * coerce_record_to_complex(ParseState *pstate, Node *node, Oid targetTypeId, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:934
int32 consttypmod
Definition: primnodes.h:193
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:205
#define elog
Definition: elog.h:219
static bool is_complex_array(Oid typid)
Type typeidType(Oid id)
Definition: parse_type.c:559
int location
Definition: primnodes.h:878
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
int location
Definition: primnodes.h:796
bool constisnull
Definition: primnodes.h:197
Oid enforce_generic_type_consistency ( Oid actual_arg_types,
Oid declared_arg_types,
int  nargs,
Oid  rettype,
bool  allow_poly 
)

Definition at line 1602 of file parse_coerce.c.

References ANYARRAYOID, ANYELEMENTOID, ANYENUMOID, ANYNONARRAYOID, ANYRANGEOID, ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), get_array_type(), get_element_type(), get_range_subtype(), getBaseType(), InvalidOid, OidIsValid, type_is_array_domain, type_is_enum(), and UNKNOWNOID.

Referenced by lookup_agg_function(), make_op(), make_scalar_array_op(), ParseFuncOrColumn(), recheck_cast_function_args(), and resolve_aggregate_transtype().

1607 {
1608  int j;
1609  bool have_generics = false;
1610  bool have_unknowns = false;
1611  Oid elem_typeid = InvalidOid;
1612  Oid array_typeid = InvalidOid;
1613  Oid range_typeid = InvalidOid;
1614  Oid array_typelem;
1615  Oid range_typelem;
1616  bool have_anyelement = (rettype == ANYELEMENTOID ||
1617  rettype == ANYNONARRAYOID ||
1618  rettype == ANYENUMOID);
1619  bool have_anynonarray = (rettype == ANYNONARRAYOID);
1620  bool have_anyenum = (rettype == ANYENUMOID);
1621 
1622  /*
1623  * Loop through the arguments to see if we have any that are polymorphic.
1624  * If so, require the actual types to be consistent.
1625  */
1626  for (j = 0; j < nargs; j++)
1627  {
1628  Oid decl_type = declared_arg_types[j];
1629  Oid actual_type = actual_arg_types[j];
1630 
1631  if (decl_type == ANYELEMENTOID ||
1632  decl_type == ANYNONARRAYOID ||
1633  decl_type == ANYENUMOID)
1634  {
1635  have_generics = have_anyelement = true;
1636  if (decl_type == ANYNONARRAYOID)
1637  have_anynonarray = true;
1638  else if (decl_type == ANYENUMOID)
1639  have_anyenum = true;
1640  if (actual_type == UNKNOWNOID)
1641  {
1642  have_unknowns = true;
1643  continue;
1644  }
1645  if (allow_poly && decl_type == actual_type)
1646  continue; /* no new information here */
1647  if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
1648  ereport(ERROR,
1649  (errcode(ERRCODE_DATATYPE_MISMATCH),
1650  errmsg("arguments declared \"anyelement\" are not all alike"),
1651  errdetail("%s versus %s",
1652  format_type_be(elem_typeid),
1653  format_type_be(actual_type))));
1654  elem_typeid = actual_type;
1655  }
1656  else if (decl_type == ANYARRAYOID)
1657  {
1658  have_generics = true;
1659  if (actual_type == UNKNOWNOID)
1660  {
1661  have_unknowns = true;
1662  continue;
1663  }
1664  if (allow_poly && decl_type == actual_type)
1665  continue; /* no new information here */
1666  actual_type = getBaseType(actual_type); /* flatten domains */
1667  if (OidIsValid(array_typeid) && actual_type != array_typeid)
1668  ereport(ERROR,
1669  (errcode(ERRCODE_DATATYPE_MISMATCH),
1670  errmsg("arguments declared \"anyarray\" are not all alike"),
1671  errdetail("%s versus %s",
1672  format_type_be(array_typeid),
1673  format_type_be(actual_type))));
1674  array_typeid = actual_type;
1675  }
1676  else if (decl_type == ANYRANGEOID)
1677  {
1678  have_generics = true;
1679  if (actual_type == UNKNOWNOID)
1680  {
1681  have_unknowns = true;
1682  continue;
1683  }
1684  if (allow_poly && decl_type == actual_type)
1685  continue; /* no new information here */
1686  actual_type = getBaseType(actual_type); /* flatten domains */
1687  if (OidIsValid(range_typeid) && actual_type != range_typeid)
1688  ereport(ERROR,
1689  (errcode(ERRCODE_DATATYPE_MISMATCH),
1690  errmsg("arguments declared \"anyrange\" are not all alike"),
1691  errdetail("%s versus %s",
1692  format_type_be(range_typeid),
1693  format_type_be(actual_type))));
1694  range_typeid = actual_type;
1695  }
1696  }
1697 
1698  /*
1699  * Fast Track: if none of the arguments are polymorphic, return the
1700  * unmodified rettype. We assume it can't be polymorphic either.
1701  */
1702  if (!have_generics)
1703  return rettype;
1704 
1705  /* Get the element type based on the array type, if we have one */
1706  if (OidIsValid(array_typeid))
1707  {
1708  if (array_typeid == ANYARRAYOID && !have_anyelement)
1709  {
1710  /* Special case for ANYARRAY input: okay iff no ANYELEMENT */
1711  array_typelem = ANYELEMENTOID;
1712  }
1713  else
1714  {
1715  array_typelem = get_element_type(array_typeid);
1716  if (!OidIsValid(array_typelem))
1717  ereport(ERROR,
1718  (errcode(ERRCODE_DATATYPE_MISMATCH),
1719  errmsg("argument declared %s is not an array but type %s",
1720  "anyarray", format_type_be(array_typeid))));
1721  }
1722 
1723  if (!OidIsValid(elem_typeid))
1724  {
1725  /*
1726  * if we don't have an element type yet, use the one we just got
1727  */
1728  elem_typeid = array_typelem;
1729  }
1730  else if (array_typelem != elem_typeid)
1731  {
1732  /* otherwise, they better match */
1733  ereport(ERROR,
1734  (errcode(ERRCODE_DATATYPE_MISMATCH),
1735  errmsg("argument declared %s is not consistent with argument declared %s",
1736  "anyarray", "anyelement"),
1737  errdetail("%s versus %s",
1738  format_type_be(array_typeid),
1739  format_type_be(elem_typeid))));
1740  }
1741  }
1742 
1743  /* Get the element type based on the range type, if we have one */
1744  if (OidIsValid(range_typeid))
1745  {
1746  if (range_typeid == ANYRANGEOID && !have_anyelement)
1747  {
1748  /* Special case for ANYRANGE input: okay iff no ANYELEMENT */
1749  range_typelem = ANYELEMENTOID;
1750  }
1751  else
1752  {
1753  range_typelem = get_range_subtype(range_typeid);
1754  if (!OidIsValid(range_typelem))
1755  ereport(ERROR,
1756  (errcode(ERRCODE_DATATYPE_MISMATCH),
1757  errmsg("argument declared %s is not a range type but type %s",
1758  "anyrange",
1759  format_type_be(range_typeid))));
1760  }
1761 
1762  if (!OidIsValid(elem_typeid))
1763  {
1764  /*
1765  * if we don't have an element type yet, use the one we just got
1766  */
1767  elem_typeid = range_typelem;
1768  }
1769  else if (range_typelem != elem_typeid)
1770  {
1771  /* otherwise, they better match */
1772  ereport(ERROR,
1773  (errcode(ERRCODE_DATATYPE_MISMATCH),
1774  errmsg("argument declared %s is not consistent with argument declared %s",
1775  "anyrange", "anyelement"),
1776  errdetail("%s versus %s",
1777  format_type_be(range_typeid),
1778  format_type_be(elem_typeid))));
1779  }
1780  }
1781 
1782  if (!OidIsValid(elem_typeid))
1783  {
1784  if (allow_poly)
1785  {
1786  elem_typeid = ANYELEMENTOID;
1787  array_typeid = ANYARRAYOID;
1788  range_typeid = ANYRANGEOID;
1789  }
1790  else
1791  {
1792  /* Only way to get here is if all the generic args are UNKNOWN */
1793  ereport(ERROR,
1794  (errcode(ERRCODE_DATATYPE_MISMATCH),
1795  errmsg("could not determine polymorphic type because input has type %s",
1796  "unknown")));
1797  }
1798  }
1799 
1800  if (have_anynonarray && elem_typeid != ANYELEMENTOID)
1801  {
1802  /* require the element type to not be an array or domain over array */
1803  if (type_is_array_domain(elem_typeid))
1804  ereport(ERROR,
1805  (errcode(ERRCODE_DATATYPE_MISMATCH),
1806  errmsg("type matched to anynonarray is an array type: %s",
1807  format_type_be(elem_typeid))));
1808  }
1809 
1810  if (have_anyenum && elem_typeid != ANYELEMENTOID)
1811  {
1812  /* require the element type to be an enum */
1813  if (!type_is_enum(elem_typeid))
1814  ereport(ERROR,
1815  (errcode(ERRCODE_DATATYPE_MISMATCH),
1816  errmsg("type matched to anyenum is not an enum type: %s",
1817  format_type_be(elem_typeid))));
1818  }
1819 
1820  /*
1821  * If we had any unknown inputs, re-scan to assign correct types
1822  */
1823  if (have_unknowns)
1824  {
1825  for (j = 0; j < nargs; j++)
1826  {
1827  Oid decl_type = declared_arg_types[j];
1828  Oid actual_type = actual_arg_types[j];
1829 
1830  if (actual_type != UNKNOWNOID)
1831  continue;
1832 
1833  if (decl_type == ANYELEMENTOID ||
1834  decl_type == ANYNONARRAYOID ||
1835  decl_type == ANYENUMOID)
1836  declared_arg_types[j] = elem_typeid;
1837  else if (decl_type == ANYARRAYOID)
1838  {
1839  if (!OidIsValid(array_typeid))
1840  {
1841  array_typeid = get_array_type(elem_typeid);
1842  if (!OidIsValid(array_typeid))
1843  ereport(ERROR,
1844  (errcode(ERRCODE_UNDEFINED_OBJECT),
1845  errmsg("could not find array type for data type %s",
1846  format_type_be(elem_typeid))));
1847  }
1848  declared_arg_types[j] = array_typeid;
1849  }
1850  else if (decl_type == ANYRANGEOID)
1851  {
1852  if (!OidIsValid(range_typeid))
1853  {
1854  ereport(ERROR,
1855  (errcode(ERRCODE_UNDEFINED_OBJECT),
1856  errmsg("could not find range type for data type %s",
1857  format_type_be(elem_typeid))));
1858  }
1859  declared_arg_types[j] = range_typeid;
1860  }
1861  }
1862  }
1863 
1864  /* if we return ANYARRAY use the appropriate argument type */
1865  if (rettype == ANYARRAYOID)
1866  {
1867  if (!OidIsValid(array_typeid))
1868  {
1869  array_typeid = get_array_type(elem_typeid);
1870  if (!OidIsValid(array_typeid))
1871  ereport(ERROR,
1872  (errcode(ERRCODE_UNDEFINED_OBJECT),
1873  errmsg("could not find array type for data type %s",
1874  format_type_be(elem_typeid))));
1875  }
1876  return array_typeid;
1877  }
1878 
1879  /* if we return ANYRANGE use the appropriate argument type */
1880  if (rettype == ANYRANGEOID)
1881  {
1882  if (!OidIsValid(range_typeid))
1883  {
1884  ereport(ERROR,
1885  (errcode(ERRCODE_UNDEFINED_OBJECT),
1886  errmsg("could not find range type for data type %s",
1887  format_type_be(elem_typeid))));
1888  }
1889  return range_typeid;
1890  }
1891 
1892  /* if we return ANYELEMENT use the appropriate argument type */
1893  if (rettype == ANYELEMENTOID ||
1894  rettype == ANYNONARRAYOID ||
1895  rettype == ANYENUMOID)
1896  return elem_typeid;
1897 
1898  /* we don't return a generic type; send back the original return type */
1899  return rettype;
1900 }
#define ANYNONARRAYOID
Definition: pg_type.h:704
#define type_is_array_domain(typid)
Definition: lsyscache.h:182
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2484
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2512
#define ANYELEMENTOID
Definition: pg_type.h:702
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define ERROR
Definition: elog.h:43
#define ANYRANGEOID
Definition: pg_type.h:714
#define ANYENUMOID
Definition: pg_type.h:706
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
#define ANYARRAYOID
Definition: pg_type.h:688
#define InvalidOid
Definition: postgres_ext.h:36
bool type_is_enum(Oid typid)
Definition: lsyscache.c:2414
#define UNKNOWNOID
Definition: pg_type.h:431
int errmsg(const char *fmt,...)
Definition: elog.c:797
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
Oid get_range_subtype(Oid rangeOid)
Definition: lsyscache.c:3074
CoercionPathType find_coercion_pathway ( Oid  targetTypeId,
Oid  sourceTypeId,
CoercionContext  ccontext,
Oid funcid 
)

Definition at line 2162 of file parse_coerce.c.

References CASTSOURCETARGET, COERCION_ASSIGNMENT, COERCION_CODE_ASSIGNMENT, COERCION_CODE_EXPLICIT, COERCION_CODE_IMPLICIT, COERCION_EXPLICIT, COERCION_IMPLICIT, COERCION_METHOD_BINARY, COERCION_METHOD_FUNCTION, COERCION_METHOD_INOUT, COERCION_PATH_ARRAYCOERCE, COERCION_PATH_COERCEVIAIO, COERCION_PATH_FUNC, COERCION_PATH_NONE, COERCION_PATH_RELABELTYPE, elog, ERROR, find_coercion_pathway(), get_base_element_type(), get_element_type(), getBaseType(), GETSTRUCT, HeapTupleIsValid, INT2VECTOROID, InvalidOid, ObjectIdGetDatum, OidIsValid, OIDVECTOROID, ReleaseSysCache(), result, SearchSysCache2, TYPCATEGORY_STRING, and TypeCategory().

Referenced by can_coerce_type(), coerce_type(), find_coercion_pathway(), findFkeyCast(), func_get_detail(), json_categorize_type(), jsonb_categorize_type(), and ri_HashCompareOp().

2165 {
2167  HeapTuple tuple;
2168 
2169  *funcid = InvalidOid;
2170 
2171  /* Perhaps the types are domains; if so, look at their base types */
2172  if (OidIsValid(sourceTypeId))
2173  sourceTypeId = getBaseType(sourceTypeId);
2174  if (OidIsValid(targetTypeId))
2175  targetTypeId = getBaseType(targetTypeId);
2176 
2177  /* Domains are always coercible to and from their base type */
2178  if (sourceTypeId == targetTypeId)
2180 
2181  /* Look in pg_cast */
2183  ObjectIdGetDatum(sourceTypeId),
2184  ObjectIdGetDatum(targetTypeId));
2185 
2186  if (HeapTupleIsValid(tuple))
2187  {
2188  Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
2189  CoercionContext castcontext;
2190 
2191  /* convert char value for castcontext to CoercionContext enum */
2192  switch (castForm->castcontext)
2193  {
2195  castcontext = COERCION_IMPLICIT;
2196  break;
2198  castcontext = COERCION_ASSIGNMENT;
2199  break;
2201  castcontext = COERCION_EXPLICIT;
2202  break;
2203  default:
2204  elog(ERROR, "unrecognized castcontext: %d",
2205  (int) castForm->castcontext);
2206  castcontext = 0; /* keep compiler quiet */
2207  break;
2208  }
2209 
2210  /* Rely on ordering of enum for correct behavior here */
2211  if (ccontext >= castcontext)
2212  {
2213  switch (castForm->castmethod)
2214  {
2216  result = COERCION_PATH_FUNC;
2217  *funcid = castForm->castfunc;
2218  break;
2219  case COERCION_METHOD_INOUT:
2220  result = COERCION_PATH_COERCEVIAIO;
2221  break;
2223  result = COERCION_PATH_RELABELTYPE;
2224  break;
2225  default:
2226  elog(ERROR, "unrecognized castmethod: %d",
2227  (int) castForm->castmethod);
2228  break;
2229  }
2230  }
2231 
2232  ReleaseSysCache(tuple);
2233  }
2234  else
2235  {
2236  /*
2237  * If there's no pg_cast entry, perhaps we are dealing with a pair of
2238  * array types. If so, and if the element types have a suitable cast,
2239  * report that we can coerce with an ArrayCoerceExpr.
2240  *
2241  * Note that the source type can be a domain over array, but not the
2242  * target, because ArrayCoerceExpr won't check domain constraints.
2243  *
2244  * Hack: disallow coercions to oidvector and int2vector, which
2245  * otherwise tend to capture coercions that should go to "real" array
2246  * types. We want those types to be considered "real" arrays for many
2247  * purposes, but not this one. (Also, ArrayCoerceExpr isn't
2248  * guaranteed to produce an output that meets the restrictions of
2249  * these datatypes, such as being 1-dimensional.)
2250  */
2251  if (targetTypeId != OIDVECTOROID && targetTypeId != INT2VECTOROID)
2252  {
2253  Oid targetElem;
2254  Oid sourceElem;
2255 
2256  if ((targetElem = get_element_type(targetTypeId)) != InvalidOid &&
2257  (sourceElem = get_base_element_type(sourceTypeId)) != InvalidOid)
2258  {
2259  CoercionPathType elempathtype;
2260  Oid elemfuncid;
2261 
2262  elempathtype = find_coercion_pathway(targetElem,
2263  sourceElem,
2264  ccontext,
2265  &elemfuncid);
2266  if (elempathtype != COERCION_PATH_NONE &&
2267  elempathtype != COERCION_PATH_ARRAYCOERCE)
2268  {
2269  *funcid = elemfuncid;
2270  if (elempathtype == COERCION_PATH_COERCEVIAIO)
2271  result = COERCION_PATH_COERCEVIAIO;
2272  else
2273  result = COERCION_PATH_ARRAYCOERCE;
2274  }
2275  }
2276  }
2277 
2278  /*
2279  * If we still haven't found a possibility, consider automatic casting
2280  * using I/O functions. We allow assignment casts to string types and
2281  * explicit casts from string types to be handled this way. (The
2282  * CoerceViaIO mechanism is a lot more general than that, but this is
2283  * all we want to allow in the absence of a pg_cast entry.) It would
2284  * probably be better to insist on explicit casts in both directions,
2285  * but this is a compromise to preserve something of the pre-8.3
2286  * behavior that many types had implicit (yipes!) casts to text.
2287  */
2288  if (result == COERCION_PATH_NONE)
2289  {
2290  if (ccontext >= COERCION_ASSIGNMENT &&
2291  TypeCategory(targetTypeId) == TYPCATEGORY_STRING)
2292  result = COERCION_PATH_COERCEVIAIO;
2293  else if (ccontext >= COERCION_EXPLICIT &&
2294  TypeCategory(sourceTypeId) == TYPCATEGORY_STRING)
2295  result = COERCION_PATH_COERCEVIAIO;
2296  }
2297  }
2298 
2299  return result;
2300 }
#define INT2VECTOROID
Definition: pg_type.h:312
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2484
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
CoercionContext
Definition: primnodes.h:420
CoercionPathType
Definition: parse_coerce.h:24
#define OIDVECTOROID
Definition: pg_type.h:344
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
FormData_pg_cast * Form_pg_cast
Definition: pg_cast.h:42
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define InvalidOid
Definition: postgres_ext.h:36
TYPCATEGORY TypeCategory(Oid type)
#define TYPCATEGORY_STRING
Definition: pg_type.h:738
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2557
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
#define elog
Definition: elog.h:219
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:158
CoercionPathType find_typmod_coercion_function ( Oid  typeId,
Oid funcid 
)

Definition at line 2323 of file parse_coerce.c.

References CASTSOURCETARGET, COERCION_PATH_ARRAYCOERCE, COERCION_PATH_FUNC, COERCION_PATH_NONE, GETSTRUCT, HeapTupleIsValid, InvalidOid, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), result, SearchSysCache2, and typeidType().

Referenced by coerce_type_typmod().

2325 {
2327  Type targetType;
2328  Form_pg_type typeForm;
2329  HeapTuple tuple;
2330 
2331  *funcid = InvalidOid;
2332  result = COERCION_PATH_FUNC;
2333 
2334  targetType = typeidType(typeId);
2335  typeForm = (Form_pg_type) GETSTRUCT(targetType);
2336 
2337  /* Check for a varlena array type */
2338  if (typeForm->typelem != InvalidOid && typeForm->typlen == -1)
2339  {
2340  /* Yes, switch our attention to the element type */
2341  typeId = typeForm->typelem;
2342  result = COERCION_PATH_ARRAYCOERCE;
2343  }
2344  ReleaseSysCache(targetType);
2345 
2346  /* Look in pg_cast */
2348  ObjectIdGetDatum(typeId),
2349  ObjectIdGetDatum(typeId));
2350 
2351  if (HeapTupleIsValid(tuple))
2352  {
2353  Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
2354 
2355  *funcid = castForm->castfunc;
2356  ReleaseSysCache(tuple);
2357  }
2358 
2359  if (!OidIsValid(*funcid))
2360  result = COERCION_PATH_NONE;
2361 
2362  return result;
2363 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
return result
Definition: formatting.c:1633
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
#define OidIsValid(objectId)
Definition: c.h:538
CoercionPathType
Definition: parse_coerce.h:24
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
FormData_pg_cast * Form_pg_cast
Definition: pg_cast.h:42
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
Type typeidType(Oid id)
Definition: parse_type.c:559
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:158
bool IsBinaryCoercible ( Oid  srctype,
Oid  targettype 
)

Definition at line 2065 of file parse_coerce.c.

References ANYARRAYOID, ANYELEMENTOID, ANYENUMOID, ANYNONARRAYOID, ANYOID, ANYRANGEOID, CASTSOURCETARGET, COERCION_CODE_IMPLICIT, COERCION_METHOD_BINARY, getBaseType(), GETSTRUCT, HeapTupleIsValid, is_complex_array(), ISCOMPLEX, ObjectIdGetDatum, OidIsValid, RECORDARRAYOID, RECORDOID, ReleaseSysCache(), result, SearchSysCache2, type_is_array, type_is_enum(), and type_is_range().

Referenced by AggregateCreate(), build_pertrans_for_aggref(), check_amproc_signature(), check_hash_func_signature(), check_sql_fn_retval(), compatible_oper(), CreateCast(), findRangeSubOpclass(), GetDefaultOpClass(), initialize_peragg(), lookup_agg_function(), ResolveOpClass(), ri_HashCompareOp(), ts_stat_sql(), tsvector_update_trigger(), and tupledesc_match().

2066 {
2067  HeapTuple tuple;
2068  Form_pg_cast castForm;
2069  bool result;
2070 
2071  /* Fast path if same type */
2072  if (srctype == targettype)
2073  return true;
2074 
2075  /* Anything is coercible to ANY or ANYELEMENT */
2076  if (targettype == ANYOID || targettype == ANYELEMENTOID)
2077  return true;
2078 
2079  /* If srctype is a domain, reduce to its base type */
2080  if (OidIsValid(srctype))
2081  srctype = getBaseType(srctype);
2082 
2083  /* Somewhat-fast path for domain -> base type case */
2084  if (srctype == targettype)
2085  return true;
2086 
2087  /* Also accept any array type as coercible to ANYARRAY */
2088  if (targettype == ANYARRAYOID)
2089  if (type_is_array(srctype))
2090  return true;
2091 
2092  /* Also accept any non-array type as coercible to ANYNONARRAY */
2093  if (targettype == ANYNONARRAYOID)
2094  if (!type_is_array(srctype))
2095  return true;
2096 
2097  /* Also accept any enum type as coercible to ANYENUM */
2098  if (targettype == ANYENUMOID)
2099  if (type_is_enum(srctype))
2100  return true;
2101 
2102  /* Also accept any range type as coercible to ANYRANGE */
2103  if (targettype == ANYRANGEOID)
2104  if (type_is_range(srctype))
2105  return true;
2106 
2107  /* Also accept any composite type as coercible to RECORD */
2108  if (targettype == RECORDOID)
2109  if (ISCOMPLEX(srctype))
2110  return true;
2111 
2112  /* Also accept any composite array type as coercible to RECORD[] */
2113  if (targettype == RECORDARRAYOID)
2114  if (is_complex_array(srctype))
2115  return true;
2116 
2117  /* Else look in pg_cast */
2119  ObjectIdGetDatum(srctype),
2120  ObjectIdGetDatum(targettype));
2121  if (!HeapTupleIsValid(tuple))
2122  return false; /* no cast */
2123  castForm = (Form_pg_cast) GETSTRUCT(tuple);
2124 
2125  result = (castForm->castmethod == COERCION_METHOD_BINARY &&
2126  castForm->castcontext == COERCION_CODE_IMPLICIT);
2127 
2128  ReleaseSysCache(tuple);
2129 
2130  return result;
2131 }
#define RECORDARRAYOID
Definition: pg_type.h:682
#define ANYNONARRAYOID
Definition: pg_type.h:704
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ANYELEMENTOID
Definition: pg_type.h:702
bool type_is_range(Oid typid)
Definition: lsyscache.c:2424
return result
Definition: formatting.c:1633
#define OidIsValid(objectId)
Definition: c.h:538
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ANYRANGEOID
Definition: pg_type.h:714
#define ANYENUMOID
Definition: pg_type.h:706
#define ANYOID
Definition: pg_type.h:686
#define RECORDOID
Definition: pg_type.h:680
FormData_pg_cast * Form_pg_cast
Definition: pg_cast.h:42
#define ANYARRAYOID
Definition: pg_type.h:688
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define ISCOMPLEX(typeid)
Definition: parse_type.h:53
bool type_is_enum(Oid typid)
Definition: lsyscache.c:2414
#define type_is_array(typid)
Definition: lsyscache.h:180
static bool is_complex_array(Oid typid)
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:158
bool IsPreferredType ( TYPCATEGORY  category,
Oid  type 
)

Definition at line 2030 of file parse_coerce.c.

References get_type_category_preferred(), and TYPCATEGORY_INVALID.

Referenced by func_select_candidate(), and GetDefaultOpClass().

2031 {
2032  char typcategory;
2033  bool typispreferred;
2034 
2035  get_type_category_preferred(type, &typcategory, &typispreferred);
2036  if (category == typcategory || category == TYPCATEGORY_INVALID)
2037  return typispreferred;
2038  else
2039  return false;
2040 }
#define TYPCATEGORY_INVALID
Definition: pg_type.h:727
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2436
int parser_coercion_errposition ( ParseState pstate,
int  coerce_location,
Node input_expr 
)

Definition at line 1179 of file parse_coerce.c.

References exprLocation(), and parser_errposition().

Referenced by coerce_record_to_complex(), and transformTypeCast().

1182 {
1183  if (coerce_location >= 0)
1184  return parser_errposition(pstate, coerce_location);
1185  else
1186  return parser_errposition(pstate, exprLocation(input_expr));
1187 }
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid resolve_generic_type ( Oid  declared_type,
Oid  context_actual_type,
Oid  context_declared_type 
)

Definition at line 1916 of file parse_coerce.c.

References ANYARRAYOID, ANYELEMENTOID, ANYENUMOID, ANYNONARRAYOID, ANYRANGEOID, elog, ereport, errcode(), errmsg(), ERROR, format_type_be(), get_array_type(), get_element_type(), get_range_subtype(), getBaseType(), InvalidOid, and OidIsValid.

Referenced by resolve_polymorphic_argtypes(), and resolve_polymorphic_tupdesc().

1919 {
1920  if (declared_type == ANYARRAYOID)
1921  {
1922  if (context_declared_type == ANYARRAYOID)
1923  {
1924  /*
1925  * Use actual type, but it must be an array; or if it's a domain
1926  * over array, use the base array type.
1927  */
1928  Oid context_base_type = getBaseType(context_actual_type);
1929  Oid array_typelem = get_element_type(context_base_type);
1930 
1931  if (!OidIsValid(array_typelem))
1932  ereport(ERROR,
1933  (errcode(ERRCODE_DATATYPE_MISMATCH),
1934  errmsg("argument declared %s is not an array but type %s",
1935  "anyarray", format_type_be(context_base_type))));
1936  return context_base_type;
1937  }
1938  else if (context_declared_type == ANYELEMENTOID ||
1939  context_declared_type == ANYNONARRAYOID ||
1940  context_declared_type == ANYENUMOID ||
1941  context_declared_type == ANYRANGEOID)
1942  {
1943  /* Use the array type corresponding to actual type */
1944  Oid array_typeid = get_array_type(context_actual_type);
1945 
1946  if (!OidIsValid(array_typeid))
1947  ereport(ERROR,
1948  (errcode(ERRCODE_UNDEFINED_OBJECT),
1949  errmsg("could not find array type for data type %s",
1950  format_type_be(context_actual_type))));
1951  return array_typeid;
1952  }
1953  }
1954  else if (declared_type == ANYELEMENTOID ||
1955  declared_type == ANYNONARRAYOID ||
1956  declared_type == ANYENUMOID ||
1957  declared_type == ANYRANGEOID)
1958  {
1959  if (context_declared_type == ANYARRAYOID)
1960  {
1961  /* Use the element type corresponding to actual type */
1962  Oid context_base_type = getBaseType(context_actual_type);
1963  Oid array_typelem = get_element_type(context_base_type);
1964 
1965  if (!OidIsValid(array_typelem))
1966  ereport(ERROR,
1967  (errcode(ERRCODE_DATATYPE_MISMATCH),
1968  errmsg("argument declared %s is not an array but type %s",
1969  "anyarray", format_type_be(context_base_type))));
1970  return array_typelem;
1971  }
1972  else if (context_declared_type == ANYRANGEOID)
1973  {
1974  /* Use the element type corresponding to actual type */
1975  Oid context_base_type = getBaseType(context_actual_type);
1976  Oid range_typelem = get_range_subtype(context_base_type);
1977 
1978  if (!OidIsValid(range_typelem))
1979  ereport(ERROR,
1980  (errcode(ERRCODE_DATATYPE_MISMATCH),
1981  errmsg("argument declared %s is not a range type but type %s",
1982  "anyrange", format_type_be(context_base_type))));
1983  return range_typelem;
1984  }
1985  else if (context_declared_type == ANYELEMENTOID ||
1986  context_declared_type == ANYNONARRAYOID ||
1987  context_declared_type == ANYENUMOID)
1988  {
1989  /* Use the actual type; it doesn't matter if array or not */
1990  return context_actual_type;
1991  }
1992  }
1993  else
1994  {
1995  /* declared_type isn't polymorphic, so return it as-is */
1996  return declared_type;
1997  }
1998  /* If we get here, declared_type is polymorphic and context isn't */
1999  /* NB: this is a calling-code logic error, not a user error */
2000  elog(ERROR, "could not determine polymorphic type because context isn't polymorphic");
2001  return InvalidOid; /* keep compiler quiet */
2002 }
#define ANYNONARRAYOID
Definition: pg_type.h:704
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2484
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2512
#define ANYELEMENTOID
Definition: pg_type.h:702
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define ERROR
Definition: elog.h:43
#define ANYRANGEOID
Definition: pg_type.h:714
#define ANYENUMOID
Definition: pg_type.h:706
#define ereport(elevel, rest)
Definition: elog.h:122
#define ANYARRAYOID
Definition: pg_type.h:688
#define InvalidOid
Definition: postgres_ext.h:36
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
Oid get_range_subtype(Oid rangeOid)
Definition: lsyscache.c:3074
Oid select_common_type ( ParseState pstate,
List exprs,
const char *  context,
Node **  which_expr 
)

Definition at line 1205 of file parse_coerce.c.

References Assert, can_coerce_type(), COERCION_IMPLICIT, ereport, errcode(), errmsg(), ERROR, exprLocation(), exprType(), for_each_cell, format_type_be(), get_type_category_preferred(), getBaseType(), InvalidOid, lfirst, linitial, list_head(), lnext, NIL, NULL, parser_errposition(), TEXTOID, and UNKNOWNOID.

Referenced by buildMergedJoinVar(), transformAExprIn(), transformArrayExpr(), transformCaseExpr(), transformCoalesceExpr(), transformMinMaxExpr(), transformSetOperationTree(), transformValuesClause(), and unify_hypothetical_args().

1207 {
1208  Node *pexpr;
1209  Oid ptype;
1210  TYPCATEGORY pcategory;
1211  bool pispreferred;
1212  ListCell *lc;
1213 
1214  Assert(exprs != NIL);
1215  pexpr = (Node *) linitial(exprs);
1216  lc = lnext(list_head(exprs));
1217  ptype = exprType(pexpr);
1218 
1219  /*
1220  * If all input types are valid and exactly the same, just pick that type.
1221  * This is the only way that we will resolve the result as being a domain
1222  * type; otherwise domains are smashed to their base types for comparison.
1223  */
1224  if (ptype != UNKNOWNOID)
1225  {
1226  for_each_cell(lc, lc)
1227  {
1228  Node *nexpr = (Node *) lfirst(lc);
1229  Oid ntype = exprType(nexpr);
1230 
1231  if (ntype != ptype)
1232  break;
1233  }
1234  if (lc == NULL) /* got to the end of the list? */
1235  {
1236  if (which_expr)
1237  *which_expr = pexpr;
1238  return ptype;
1239  }
1240  }
1241 
1242  /*
1243  * Nope, so set up for the full algorithm. Note that at this point, lc
1244  * points to the first list item with type different from pexpr's; we need
1245  * not re-examine any items the previous loop advanced over.
1246  */
1247  ptype = getBaseType(ptype);
1248  get_type_category_preferred(ptype, &pcategory, &pispreferred);
1249 
1250  for_each_cell(lc, lc)
1251  {
1252  Node *nexpr = (Node *) lfirst(lc);
1253  Oid ntype = getBaseType(exprType(nexpr));
1254 
1255  /* move on to next one if no new information... */
1256  if (ntype != UNKNOWNOID && ntype != ptype)
1257  {
1258  TYPCATEGORY ncategory;
1259  bool nispreferred;
1260 
1261  get_type_category_preferred(ntype, &ncategory, &nispreferred);
1262  if (ptype == UNKNOWNOID)
1263  {
1264  /* so far, only unknowns so take anything... */
1265  pexpr = nexpr;
1266  ptype = ntype;
1267  pcategory = ncategory;
1268  pispreferred = nispreferred;
1269  }
1270  else if (ncategory != pcategory)
1271  {
1272  /*
1273  * both types in different categories? then not much hope...
1274  */
1275  if (context == NULL)
1276  return InvalidOid;
1277  ereport(ERROR,
1278  (errcode(ERRCODE_DATATYPE_MISMATCH),
1279  /*------
1280  translator: first %s is name of a SQL construct, eg CASE */
1281  errmsg("%s types %s and %s cannot be matched",
1282  context,
1283  format_type_be(ptype),
1284  format_type_be(ntype)),
1285  parser_errposition(pstate, exprLocation(nexpr))));
1286  }
1287  else if (!pispreferred &&
1288  can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
1289  !can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
1290  {
1291  /*
1292  * take new type if can coerce to it implicitly but not the
1293  * other way; but if we have a preferred type, stay on it.
1294  */
1295  pexpr = nexpr;
1296  ptype = ntype;
1297  pcategory = ncategory;
1298  pispreferred = nispreferred;
1299  }
1300  }
1301  }
1302 
1303  /*
1304  * If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
1305  * then resolve as type TEXT. This situation comes up with constructs
1306  * like SELECT (CASE WHEN foo THEN 'bar' ELSE 'baz' END); SELECT 'foo'
1307  * UNION SELECT 'bar'; It might seem desirable to leave the construct's
1308  * output type as UNKNOWN, but that really doesn't work, because we'd
1309  * probably end up needing a runtime coercion from UNKNOWN to something
1310  * else, and we usually won't have it. We need to coerce the unknown
1311  * literals while they are still literals, so a decision has to be made
1312  * now.
1313  */
1314  if (ptype == UNKNOWNOID)
1315  ptype = TEXTOID;
1316 
1317  if (which_expr)
1318  *which_expr = pexpr;
1319  return ptype;
1320 }
#define NIL
Definition: pg_list.h:69
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
#define TEXTOID
Definition: pg_type.h:324
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
char TYPCATEGORY
Definition: parse_coerce.h:21
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define for_each_cell(cell, initcell)
Definition: pg_list.h:169
#define UNKNOWNOID
Definition: pg_type.h:431
bool can_coerce_type(int nargs, Oid *input_typeids, Oid *target_typeids, CoercionContext ccontext)
Definition: parse_coerce.c:527
int errmsg(const char *fmt,...)
Definition: elog.c:797
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2436
TYPCATEGORY TypeCategory ( Oid  type)

Definition at line 2011 of file parse_coerce.c.

References Assert, get_type_category_preferred(), and TYPCATEGORY_INVALID.

Referenced by find_coercion_pathway(), func_get_detail(), func_select_candidate(), and GetDefaultOpClass().

2012 {
2013  char typcategory;
2014  bool typispreferred;
2015 
2016  get_type_category_preferred(type, &typcategory, &typispreferred);
2017  Assert(typcategory != TYPCATEGORY_INVALID);
2018  return (TYPCATEGORY) typcategory;
2019 }
char TYPCATEGORY
Definition: parse_coerce.h:21
#define TYPCATEGORY_INVALID
Definition: pg_type.h:727
#define Assert(condition)
Definition: c.h:676
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2436