PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parse_coerce.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_class.h"
#include "catalog/pg_inherits_fn.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_coerce.h"
#include "parser/parse_relation.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for parse_coerce.c:

Go to the source code of this file.

Functions

static Nodecoerce_type_typmod (Node *node, Oid targetTypeId, int32 targetTypMod, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
 
static void hide_coercion_node (Node *node)
 
static Nodebuild_coercion_expression (Node *node, CoercionPathType pathtype, Oid funcId, Oid targetTypeId, int32 targetTypMod, CoercionContext ccontext, CoercionForm cformat, int location)
 
static Nodecoerce_record_to_complex (ParseState *pstate, Node *node, Oid targetTypeId, CoercionContext ccontext, CoercionForm cformat, int location)
 
static bool is_complex_array (Oid typid)
 
static bool typeIsOfTypedTable (Oid reltypeId, Oid reloftypeId)
 
Nodecoerce_to_target_type (ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
 
Nodecoerce_type (ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
 
bool can_coerce_type (int nargs, Oid *input_typeids, Oid *target_typeids, CoercionContext ccontext)
 
Nodecoerce_to_domain (Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
 
Nodecoerce_to_boolean (ParseState *pstate, Node *node, const char *constructName)
 
Nodecoerce_to_specific_type_typmod (ParseState *pstate, Node *node, Oid targetTypeId, int32 targetTypmod, const char *constructName)
 
Nodecoerce_to_specific_type (ParseState *pstate, Node *node, Oid targetTypeId, 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)
 
TYPCATEGORY TypeCategory (Oid type)
 
bool IsPreferredType (TYPCATEGORY category, Oid type)
 
bool IsBinaryCoercible (Oid srctype, Oid targettype)
 
CoercionPathType find_coercion_pathway (Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
 
CoercionPathType find_typmod_coercion_function (Oid typeId, Oid *funcid)
 

Function Documentation

static Node * build_coercion_expression ( Node node,
CoercionPathType  pathtype,
Oid  funcId,
Oid  targetTypeId,
int32  targetTypMod,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location 
)
static

Definition at line 793 of file parse_coerce.c.

References CoerceViaIO::arg, ArrayCoerceExpr::arg, generate_unaccent_rules::args, Assert, BoolGetDatum, BOOLOID, coerce_to_target_type(), CoerceViaIO::coerceformat, ArrayCoerceExpr::coerceformat, COERCION_EXPLICIT, COERCION_PATH_ARRAYCOERCE, COERCION_PATH_COERCEVIAIO, COERCION_PATH_FUNC, CaseTestExpr::collation, ArrayCoerceExpr::elemexpr, elog, ERROR, exprType(), exprTypmod(), get_element_type(), getBaseTypeAndTypmod(), GETSTRUCT, HeapTupleIsValid, Int32GetDatum, INT4OID, InvalidOid, lappend(), list_make1, FuncExpr::location, CoerceViaIO::location, ArrayCoerceExpr::location, makeConst(), makeFuncExpr(), makeNode, ObjectIdGetDatum, OidIsValid, PROCOID, ReleaseSysCache(), CoerceViaIO::resulttype, ArrayCoerceExpr::resulttype, ArrayCoerceExpr::resulttypmod, SearchSysCache1(), CaseTestExpr::typeId, and CaseTestExpr::typeMod.

Referenced by coerce_type(), and coerce_type_typmod().

799 {
800  int nargs = 0;
801 
802  if (OidIsValid(funcId))
803  {
804  HeapTuple tp;
805  Form_pg_proc procstruct;
806 
808  if (!HeapTupleIsValid(tp))
809  elog(ERROR, "cache lookup failed for function %u", funcId);
810  procstruct = (Form_pg_proc) GETSTRUCT(tp);
811 
812  /*
813  * These Asserts essentially check that function is a legal coercion
814  * function. We can't make the seemingly obvious tests on prorettype
815  * and proargtypes[0], even in the COERCION_PATH_FUNC case, because of
816  * various binary-compatibility cases.
817  */
818  /* Assert(targetTypeId == procstruct->prorettype); */
819  Assert(!procstruct->proretset);
820  Assert(!procstruct->proisagg);
821  Assert(!procstruct->proiswindow);
822  nargs = procstruct->pronargs;
823  Assert(nargs >= 1 && nargs <= 3);
824  /* Assert(procstruct->proargtypes.values[0] == exprType(node)); */
825  Assert(nargs < 2 || procstruct->proargtypes.values[1] == INT4OID);
826  Assert(nargs < 3 || procstruct->proargtypes.values[2] == BOOLOID);
827 
828  ReleaseSysCache(tp);
829  }
830 
831  if (pathtype == COERCION_PATH_FUNC)
832  {
833  /* We build an ordinary FuncExpr with special arguments */
834  FuncExpr *fexpr;
835  List *args;
836  Const *cons;
837 
838  Assert(OidIsValid(funcId));
839 
840  args = list_make1(node);
841 
842  if (nargs >= 2)
843  {
844  /* Pass target typmod as an int4 constant */
845  cons = makeConst(INT4OID,
846  -1,
847  InvalidOid,
848  sizeof(int32),
849  Int32GetDatum(targetTypMod),
850  false,
851  true);
852 
853  args = lappend(args, cons);
854  }
855 
856  if (nargs == 3)
857  {
858  /* Pass it a boolean isExplicit parameter, too */
859  cons = makeConst(BOOLOID,
860  -1,
861  InvalidOid,
862  sizeof(bool),
863  BoolGetDatum(ccontext == COERCION_EXPLICIT),
864  false,
865  true);
866 
867  args = lappend(args, cons);
868  }
869 
870  fexpr = makeFuncExpr(funcId, targetTypeId, args,
871  InvalidOid, InvalidOid, cformat);
872  fexpr->location = location;
873  return (Node *) fexpr;
874  }
875  else if (pathtype == COERCION_PATH_ARRAYCOERCE)
876  {
877  /* We need to build an ArrayCoerceExpr */
880  Oid sourceBaseTypeId;
881  int32 sourceBaseTypeMod;
882  Oid targetElementType;
883  Node *elemexpr;
884 
885  /*
886  * Look through any domain over the source array type. Note we don't
887  * expect that the target type is a domain; it must be a plain array.
888  * (To get to a domain target type, we'll do coerce_to_domain later.)
889  */
890  sourceBaseTypeMod = exprTypmod(node);
891  sourceBaseTypeId = getBaseTypeAndTypmod(exprType(node),
892  &sourceBaseTypeMod);
893 
894  /* Set up CaseTestExpr representing one element of source array */
895  ctest->typeId = get_element_type(sourceBaseTypeId);
896  Assert(OidIsValid(ctest->typeId));
897  ctest->typeMod = sourceBaseTypeMod;
898  ctest->collation = InvalidOid; /* Assume coercions don't care */
899 
900  /* And coerce it to the target element type */
901  targetElementType = get_element_type(targetTypeId);
902  Assert(OidIsValid(targetElementType));
903 
904  elemexpr = coerce_to_target_type(NULL,
905  (Node *) ctest,
906  ctest->typeId,
907  targetElementType,
908  targetTypMod,
909  ccontext,
910  cformat,
911  location);
912  if (elemexpr == NULL) /* shouldn't happen */
913  elog(ERROR, "failed to coerce array element type as expected");
914 
915  acoerce->arg = (Expr *) node;
916  acoerce->elemexpr = (Expr *) elemexpr;
917  acoerce->resulttype = targetTypeId;
918 
919  /*
920  * Label the output as having a particular element typmod only if we
921  * ended up with a per-element expression that is labeled that way.
922  */
923  acoerce->resulttypmod = exprTypmod(elemexpr);
924  /* resultcollid will be set by parse_collate.c */
925  acoerce->coerceformat = cformat;
926  acoerce->location = location;
927 
928  return (Node *) acoerce;
929  }
930  else if (pathtype == COERCION_PATH_COERCEVIAIO)
931  {
932  /* We need to build a CoerceViaIO node */
933  CoerceViaIO *iocoerce = makeNode(CoerceViaIO);
934 
935  Assert(!OidIsValid(funcId));
936 
937  iocoerce->arg = (Expr *) node;
938  iocoerce->resulttype = targetTypeId;
939  /* resultcollid will be set by parse_collate.c */
940  iocoerce->coerceformat = cformat;
941  iocoerce->location = location;
942 
943  return (Node *) iocoerce;
944  }
945  else
946  {
947  elog(ERROR, "unsupported pathtype %d in build_coercion_expression",
948  (int) pathtype);
949  return NULL; /* keep compiler quiet */
950  }
951 }
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2288
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2484
Oid resulttype
Definition: primnodes.h:812
#define INT4OID
Definition: pg_type.h:316
Definition: nodes.h:510
unsigned int Oid
Definition: postgres_ext.h:31
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:296
#define OidIsValid(objectId)
Definition: c.h:532
int32 typeMod
Definition: primnodes.h:937
signed int int32
Definition: c.h:246
#define list_make1(x1)
Definition: pg_list.h:139
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#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:78
Expr * arg
Definition: primnodes.h:811
Expr * elemexpr
Definition: primnodes.h:836
List * lappend(List *list, void *datum)
Definition: list.c:128
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
CoercionForm coerceformat
Definition: primnodes.h:840
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define BoolGetDatum(X)
Definition: postgres.h:408
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:558
int location
Definition: primnodes.h:816
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:681
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define BOOLOID
Definition: pg_type.h:288
CoercionForm coerceformat
Definition: primnodes.h:815
#define Int32GetDatum(X)
Definition: postgres.h:485
int location
Definition: primnodes.h:458
int32 resulttypmod
Definition: primnodes.h:838
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Definition: makefuncs.c:517
bool can_coerce_type ( int  nargs,
Oid input_typeids,
Oid target_typeids,
CoercionContext  ccontext 
)

Definition at line 526 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().

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

1436 {
1437  int j;
1438  Oid elem_typeid = InvalidOid;
1439  Oid array_typeid = InvalidOid;
1440  Oid array_typelem;
1441  Oid range_typeid = InvalidOid;
1442  Oid range_typelem;
1443  bool have_anyelement = false;
1444  bool have_anynonarray = false;
1445  bool have_anyenum = false;
1446 
1447  /*
1448  * Loop through the arguments to see if we have any that are polymorphic.
1449  * If so, require the actual types to be consistent.
1450  */
1451  for (j = 0; j < nargs; j++)
1452  {
1453  Oid decl_type = declared_arg_types[j];
1454  Oid actual_type = actual_arg_types[j];
1455 
1456  if (decl_type == ANYELEMENTOID ||
1457  decl_type == ANYNONARRAYOID ||
1458  decl_type == ANYENUMOID)
1459  {
1460  have_anyelement = true;
1461  if (decl_type == ANYNONARRAYOID)
1462  have_anynonarray = true;
1463  else if (decl_type == ANYENUMOID)
1464  have_anyenum = true;
1465  if (actual_type == UNKNOWNOID)
1466  continue;
1467  if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
1468  return false;
1469  elem_typeid = actual_type;
1470  }
1471  else if (decl_type == ANYARRAYOID)
1472  {
1473  if (actual_type == UNKNOWNOID)
1474  continue;
1475  actual_type = getBaseType(actual_type); /* flatten domains */
1476  if (OidIsValid(array_typeid) && actual_type != array_typeid)
1477  return false;
1478  array_typeid = actual_type;
1479  }
1480  else if (decl_type == ANYRANGEOID)
1481  {
1482  if (actual_type == UNKNOWNOID)
1483  continue;
1484  actual_type = getBaseType(actual_type); /* flatten domains */
1485  if (OidIsValid(range_typeid) && actual_type != range_typeid)
1486  return false;
1487  range_typeid = actual_type;
1488  }
1489  }
1490 
1491  /* Get the element type based on the array type, if we have one */
1492  if (OidIsValid(array_typeid))
1493  {
1494  if (array_typeid == ANYARRAYOID)
1495  {
1496  /* Special case for ANYARRAY input: okay iff no ANYELEMENT */
1497  if (have_anyelement)
1498  return false;
1499  return true;
1500  }
1501 
1502  array_typelem = get_element_type(array_typeid);
1503  if (!OidIsValid(array_typelem))
1504  return false; /* should be an array, but isn't */
1505 
1506  if (!OidIsValid(elem_typeid))
1507  {
1508  /*
1509  * if we don't have an element type yet, use the one we just got
1510  */
1511  elem_typeid = array_typelem;
1512  }
1513  else if (array_typelem != elem_typeid)
1514  {
1515  /* otherwise, they better match */
1516  return false;
1517  }
1518  }
1519 
1520  /* Get the element type based on the range type, if we have one */
1521  if (OidIsValid(range_typeid))
1522  {
1523  range_typelem = get_range_subtype(range_typeid);
1524  if (!OidIsValid(range_typelem))
1525  return false; /* should be a range, but isn't */
1526 
1527  if (!OidIsValid(elem_typeid))
1528  {
1529  /*
1530  * if we don't have an element type yet, use the one we just got
1531  */
1532  elem_typeid = range_typelem;
1533  }
1534  else if (range_typelem != elem_typeid)
1535  {
1536  /* otherwise, they better match */
1537  return false;
1538  }
1539  }
1540 
1541  if (have_anynonarray)
1542  {
1543  /* require the element type to not be an array or domain over array */
1544  if (type_is_array_domain(elem_typeid))
1545  return false;
1546  }
1547 
1548  if (have_anyenum)
1549  {
1550  /* require the element type to be an enum */
1551  if (!type_is_enum(elem_typeid))
1552  return false;
1553  }
1554 
1555  /* Looks valid */
1556  return true;
1557 }
#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:532
#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
static Node * coerce_record_to_complex ( ParseState pstate,
Node node,
Oid  targetTypeId,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location 
)
static

Definition at line 962 of file parse_coerce.c.

References arg, generate_unaccent_rules::args, RowExpr::args, COERCE_IMPLICIT_CAST, coerce_to_target_type(), RowExpr::colnames, ereport, errcode(), errdetail(), errmsg(), ERROR, expandRTE(), exprType(), format_type_be(), GetRTEByRangeTablePosn(), i, INT4OID, InvalidAttrNumber, InvalidOid, IsA, lappend(), lfirst, list_head(), lnext, RowExpr::location, lookup_rowtype_tupdesc(), makeNode, makeNullConst(), tupleDesc::natts, NIL, parser_coercion_errposition(), RECORDOID, ReleaseTupleDesc, RowExpr::row_format, RowExpr::row_typeid, and TupleDescAttr.

Referenced by coerce_type().

967 {
968  RowExpr *rowexpr;
969  TupleDesc tupdesc;
970  List *args = NIL;
971  List *newargs;
972  int i;
973  int ucolno;
974  ListCell *arg;
975 
976  if (node && IsA(node, RowExpr))
977  {
978  /*
979  * Since the RowExpr must be of type RECORD, we needn't worry about it
980  * containing any dropped columns.
981  */
982  args = ((RowExpr *) node)->args;
983  }
984  else if (node && IsA(node, Var) &&
985  ((Var *) node)->varattno == InvalidAttrNumber)
986  {
987  int rtindex = ((Var *) node)->varno;
988  int sublevels_up = ((Var *) node)->varlevelsup;
989  int vlocation = ((Var *) node)->location;
990  RangeTblEntry *rte;
991 
992  rte = GetRTEByRangeTablePosn(pstate, rtindex, sublevels_up);
993  expandRTE(rte, rtindex, sublevels_up, vlocation, false,
994  NULL, &args);
995  }
996  else
997  ereport(ERROR,
998  (errcode(ERRCODE_CANNOT_COERCE),
999  errmsg("cannot cast type %s to %s",
1001  format_type_be(targetTypeId)),
1002  parser_coercion_errposition(pstate, location, node)));
1003 
1004  tupdesc = lookup_rowtype_tupdesc(targetTypeId, -1);
1005  newargs = NIL;
1006  ucolno = 1;
1007  arg = list_head(args);
1008  for (i = 0; i < tupdesc->natts; i++)
1009  {
1010  Node *expr;
1011  Node *cexpr;
1012  Oid exprtype;
1013  Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
1014 
1015  /* Fill in NULLs for dropped columns in rowtype */
1016  if (attr->attisdropped)
1017  {
1018  /*
1019  * can't use atttypid here, but it doesn't really matter what type
1020  * the Const claims to be.
1021  */
1022  newargs = lappend(newargs,
1024  continue;
1025  }
1026 
1027  if (arg == NULL)
1028  ereport(ERROR,
1029  (errcode(ERRCODE_CANNOT_COERCE),
1030  errmsg("cannot cast type %s to %s",
1032  format_type_be(targetTypeId)),
1033  errdetail("Input has too few columns."),
1034  parser_coercion_errposition(pstate, location, node)));
1035  expr = (Node *) lfirst(arg);
1036  exprtype = exprType(expr);
1037 
1038  cexpr = coerce_to_target_type(pstate,
1039  expr, exprtype,
1040  attr->atttypid,
1041  attr->atttypmod,
1042  ccontext,
1044  -1);
1045  if (cexpr == NULL)
1046  ereport(ERROR,
1047  (errcode(ERRCODE_CANNOT_COERCE),
1048  errmsg("cannot cast type %s to %s",
1050  format_type_be(targetTypeId)),
1051  errdetail("Cannot cast type %s to %s in column %d.",
1052  format_type_be(exprtype),
1053  format_type_be(attr->atttypid),
1054  ucolno),
1055  parser_coercion_errposition(pstate, location, expr)));
1056  newargs = lappend(newargs, cexpr);
1057  ucolno++;
1058  arg = lnext(arg);
1059  }
1060  if (arg != NULL)
1061  ereport(ERROR,
1062  (errcode(ERRCODE_CANNOT_COERCE),
1063  errmsg("cannot cast type %s to %s",
1065  format_type_be(targetTypeId)),
1066  errdetail("Input has too many columns."),
1067  parser_coercion_errposition(pstate, location, node)));
1068 
1069  ReleaseTupleDesc(tupdesc);
1070 
1071  rowexpr = makeNode(RowExpr);
1072  rowexpr->args = newargs;
1073  rowexpr->row_typeid = targetTypeId;
1074  rowexpr->row_format = cformat;
1075  rowexpr->colnames = NIL; /* not needed for named target type */
1076  rowexpr->location = location;
1077  return (Node *) rowexpr;
1078 }
#define NIL
Definition: pg_list.h:69
List * args
Definition: primnodes.h:986
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1579
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
#define INT4OID
Definition: pg_type.h:316
Definition: nodes.h:510
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
Definition: primnodes.h:163
int natts
Definition: tupdesc.h:73
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
#define ERROR
Definition: elog.h:43
List * colnames
Definition: primnodes.h:999
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:78
int location
Definition: primnodes.h:1000
int errdetail(const char *fmt,...)
Definition: elog.c:873
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define RECORDOID
Definition: pg_type.h:680
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:558
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid row_typeid
Definition: primnodes.h:987
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:797
int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr)
int i
void * arg
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:115
CoercionForm row_format
Definition: primnodes.h:998
Definition: pg_list.h:45
Node* coerce_to_boolean ( ParseState pstate,
Node node,
const char *  constructName 
)

Definition at line 1091 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(), and parser_errposition().

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

1093 {
1094  Oid inputTypeId = exprType(node);
1095 
1096  if (inputTypeId != BOOLOID)
1097  {
1098  Node *newnode;
1099 
1100  newnode = coerce_to_target_type(pstate, node, inputTypeId,
1101  BOOLOID, -1,
1104  -1);
1105  if (newnode == NULL)
1106  ereport(ERROR,
1107  (errcode(ERRCODE_DATATYPE_MISMATCH),
1108  /* translator: first %s is name of a SQL construct, eg WHERE */
1109  errmsg("argument of %s must be type %s, not type %s",
1110  constructName, "boolean",
1111  format_type_be(inputTypeId)),
1112  parser_errposition(pstate, exprLocation(node))));
1113  node = newnode;
1114  }
1115 
1116  if (expression_returns_set(node))
1117  ereport(ERROR,
1118  (errcode(ERRCODE_DATATYPE_MISMATCH),
1119  /* translator: %s is name of a SQL construct, eg WHERE */
1120  errmsg("argument of %s must not return a set",
1121  constructName),
1122  parser_errposition(pstate, exprLocation(node))));
1123 
1124  return node;
1125 }
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
Definition: nodes.h:510
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:78
#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
#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 1362 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().

1364 {
1365  Oid inputTypeId = exprType(node);
1366 
1367  if (inputTypeId == targetTypeId)
1368  return node; /* no work */
1369  if (can_coerce_type(1, &inputTypeId, &targetTypeId, COERCION_IMPLICIT))
1370  node = coerce_type(pstate, node, inputTypeId, targetTypeId, -1,
1372  else
1373  ereport(ERROR,
1374  (errcode(ERRCODE_CANNOT_COERCE),
1375  /* translator: first %s is name of a SQL construct, eg CASE */
1376  errmsg("%s could not convert type %s to %s",
1377  context,
1378  format_type_be(inputTypeId),
1379  format_type_be(targetTypeId)),
1380  parser_errposition(pstate, exprLocation(node))));
1381  return node;
1382 }
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:526
int errmsg(const char *fmt,...)
Definition: elog.c:797
Node* coerce_to_domain ( Node arg,
Oid  baseTypeId,
int32  baseTypeMod,
Oid  typeId,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location,
bool  hideInputCoercion 
)

Definition at line 646 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, CoerceToDomain::resulttype, and CoerceToDomain::resulttypmod.

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

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

Definition at line 1187 of file parse_coerce.c.

References coerce_to_specific_type_typmod().

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

1190 {
1191  return coerce_to_specific_type_typmod(pstate, node,
1192  targetTypeId, -1,
1193  constructName);
1194 }
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 1138 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(), and parser_errposition().

Referenced by coerce_to_specific_type(), and transformRangeTableFunc().

1141 {
1142  Oid inputTypeId = exprType(node);
1143 
1144  if (inputTypeId != targetTypeId)
1145  {
1146  Node *newnode;
1147 
1148  newnode = coerce_to_target_type(pstate, node, inputTypeId,
1149  targetTypeId, targetTypmod,
1152  -1);
1153  if (newnode == NULL)
1154  ereport(ERROR,
1155  (errcode(ERRCODE_DATATYPE_MISMATCH),
1156  /* translator: first %s is name of a SQL construct, eg LIMIT */
1157  errmsg("argument of %s must be type %s, not type %s",
1158  constructName,
1159  format_type_be(targetTypeId),
1160  format_type_be(inputTypeId)),
1161  parser_errposition(pstate, exprLocation(node))));
1162  node = newnode;
1163  }
1164 
1165  if (expression_returns_set(node))
1166  ereport(ERROR,
1167  (errcode(ERRCODE_DATATYPE_MISMATCH),
1168  /* translator: %s is name of a SQL construct, eg LIMIT */
1169  errmsg("argument of %s must not return a set",
1170  constructName),
1171  parser_errposition(pstate, exprLocation(node))));
1172 
1173  return node;
1174 }
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
Definition: nodes.h:510
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:78
#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
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 78 of file parse_coerce.c.

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

Referenced by ATExecAddColumn(), ATExecAlterColumnType(), ATPrepAlterColumnType(), build_coercion_expression(), 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().

83 {
84  Node *result;
85  Node *origexpr;
86 
87  if (!can_coerce_type(1, &exprtype, &targettype, ccontext))
88  return NULL;
89 
90  /*
91  * If the input has a CollateExpr at the top, strip it off, perform the
92  * coercion, and put a new one back on. This is annoying since it
93  * duplicates logic in coerce_type, but if we don't do this then it's too
94  * hard to tell whether coerce_type actually changed anything, and we
95  * *must* know that to avoid possibly calling hide_coercion_node on
96  * something that wasn't generated by coerce_type. Note that if there are
97  * multiple stacked CollateExprs, we just discard all but the topmost.
98  */
99  origexpr = expr;
100  while (expr && IsA(expr, CollateExpr))
101  expr = (Node *) ((CollateExpr *) expr)->arg;
102 
103  result = coerce_type(pstate, expr, exprtype,
104  targettype, targettypmod,
105  ccontext, cformat, location);
106 
107  /*
108  * If the target is a fixed-length type, it may need a length coercion as
109  * well as a type coercion. If we find ourselves adding both, force the
110  * inner coercion node to implicit display form.
111  */
112  result = coerce_type_typmod(result,
113  targettype, targettypmod,
114  ccontext, cformat, location,
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 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
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:510
static Node * coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
Definition: parse_coerce.c:723
#define makeNode(_type_)
Definition: nodes.h:558
Expr * arg
Definition: primnodes.h:876
bool can_coerce_type(int nargs, Oid *input_typeids, Oid *target_typeids, CoercionContext ccontext)
Definition: parse_coerce.c:526
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_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, 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(), ParseState::p_coerce_param_hook, PG_DETOAST_DATUM, PointerGetDatum, RECORDARRAYOID, RECORDOID, ReleaseSysCache(), 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  ccontext, cformat, location,
359  false);
360 
361  ReleaseSysCache(baseType);
362 
363  return result;
364  }
365  if (IsA(node, Param) &&
366  pstate != NULL && pstate->p_coerce_param_hook != NULL)
367  {
368  /*
369  * Allow the CoerceParamHook to decide what happens. It can return a
370  * transformed node (very possibly the same Param node), or return
371  * NULL to indicate we should proceed with normal coercion.
372  */
373  result = pstate->p_coerce_param_hook(pstate,
374  (Param *) node,
375  targetTypeId,
376  targetTypeMod,
377  location);
378  if (result)
379  return result;
380  }
381  if (IsA(node, CollateExpr))
382  {
383  /*
384  * If we have a COLLATE clause, we have to push the coercion
385  * underneath the COLLATE. This is really ugly, but there is little
386  * choice because the above hacks on Consts and Params wouldn't happen
387  * otherwise. This kluge has consequences in coerce_to_target_type.
388  */
389  CollateExpr *coll = (CollateExpr *) node;
390  CollateExpr *newcoll = makeNode(CollateExpr);
391 
392  newcoll->arg = (Expr *)
393  coerce_type(pstate, (Node *) coll->arg,
394  inputTypeId, targetTypeId, targetTypeMod,
395  ccontext, cformat, location);
396  newcoll->collOid = coll->collOid;
397  newcoll->location = coll->location;
398  return (Node *) newcoll;
399  }
400  pathtype = find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
401  &funcId);
402  if (pathtype != COERCION_PATH_NONE)
403  {
404  if (pathtype != COERCION_PATH_RELABELTYPE)
405  {
406  /*
407  * Generate an expression tree representing run-time application
408  * of the conversion function. If we are dealing with a domain
409  * target type, the conversion function will yield the base type,
410  * and we need to extract the correct typmod to use from the
411  * domain's typtypmod.
412  */
413  Oid baseTypeId;
414  int32 baseTypeMod;
415 
416  baseTypeMod = targetTypeMod;
417  baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
418 
419  result = build_coercion_expression(node, pathtype, funcId,
420  baseTypeId, baseTypeMod,
421  ccontext, cformat, location);
422 
423  /*
424  * If domain, coerce to the domain type and relabel with domain
425  * type ID, hiding the previous coercion node.
426  */
427  if (targetTypeId != baseTypeId)
428  result = coerce_to_domain(result, baseTypeId, baseTypeMod,
429  targetTypeId,
430  ccontext, cformat, location,
431  true);
432  }
433  else
434  {
435  /*
436  * We don't need to do a physical conversion, but we do need to
437  * attach a RelabelType node so that the expression will be seen
438  * to have the intended type when inspected by higher-level code.
439  *
440  * Also, domains may have value restrictions beyond the base type
441  * that must be accounted for. If the destination is a domain
442  * then we won't need a RelabelType node.
443  */
444  result = coerce_to_domain(node, InvalidOid, -1, targetTypeId,
445  ccontext, cformat, location,
446  false);
447  if (result == node)
448  {
449  /*
450  * XXX could we label result with exprTypmod(node) instead of
451  * default -1 typmod, to save a possible length-coercion
452  * later? Would work if both types have same interpretation of
453  * typmod, which is likely but not certain.
454  */
455  RelabelType *r = makeRelabelType((Expr *) result,
456  targetTypeId, -1,
457  InvalidOid,
458  cformat);
459 
460  r->location = location;
461  result = (Node *) r;
462  }
463  }
464  return result;
465  }
466  if (inputTypeId == RECORDOID &&
467  ISCOMPLEX(targetTypeId))
468  {
469  /* Coerce a RECORD to a specific complex type */
470  return coerce_record_to_complex(pstate, node, targetTypeId,
471  ccontext, cformat, location);
472  }
473  if (targetTypeId == RECORDOID &&
474  ISCOMPLEX(inputTypeId))
475  {
476  /* Coerce a specific complex type to RECORD */
477  /* NB: we do NOT want a RelabelType here */
478  return node;
479  }
480 #ifdef NOT_USED
481  if (inputTypeId == RECORDARRAYOID &&
482  is_complex_array(targetTypeId))
483  {
484  /* Coerce record[] to a specific complex array type */
485  /* not implemented yet ... */
486  }
487 #endif
488  if (targetTypeId == RECORDARRAYOID &&
489  is_complex_array(inputTypeId))
490  {
491  /* Coerce a specific complex array type to record[] */
492  /* NB: we do NOT want a RelabelType here */
493  return node;
494  }
495  if (typeInheritsFrom(inputTypeId, targetTypeId)
496  || typeIsOfTypedTable(inputTypeId, targetTypeId))
497  {
498  /*
499  * Input class type is a subclass of target, so generate an
500  * appropriate runtime conversion (removing unneeded columns and
501  * possibly rearranging the ones that are wanted).
502  */
504 
505  r->arg = (Expr *) node;
506  r->resulttype = targetTypeId;
507  r->convertformat = cformat;
508  r->location = location;
509  return (Node *) r;
510  }
511  /* If we get here, caller blew it */
512  elog(ERROR, "failed to find conversion function from %s to %s",
513  format_type_be(inputTypeId), format_type_be(targetTypeId));
514  return NULL; /* keep compiler quiet */
515 }
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:561
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
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:510
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
CoercionPathType
Definition: parse_coerce.h:24
signed int int32
Definition: c.h:246
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
Datum stringTypeDatum(Type tp, char *string, int32 atttypmod)
Definition: parse_type.c:635
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
Definition: parse_coerce.c:646
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
static Node * build_coercion_expression(Node *node, CoercionPathType pathtype, Oid funcId, Oid targetTypeId, int32 targetTypMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:793
#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:1160
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:558
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:962
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
static Node * coerce_type_typmod ( Node node,
Oid  targetTypeId,
int32  targetTypMod,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location,
bool  hideInputCoercion 
)
static

Definition at line 723 of file parse_coerce.c.

References build_coercion_expression(), COERCION_PATH_NONE, exprTypmod(), find_typmod_coercion_function(), and hide_coercion_node().

Referenced by coerce_to_domain(), and coerce_to_target_type().

727 {
728  CoercionPathType pathtype;
729  Oid funcId;
730 
731  /*
732  * A negative typmod is assumed to mean that no coercion is wanted. Also,
733  * skip coercion if already done.
734  */
735  if (targetTypMod < 0 || targetTypMod == exprTypmod(node))
736  return node;
737 
738  pathtype = find_typmod_coercion_function(targetTypeId, &funcId);
739 
740  if (pathtype != COERCION_PATH_NONE)
741  {
742  /* Suppress display of nested coercion steps */
743  if (hideInputCoercion)
744  hide_coercion_node(node);
745 
746  node = build_coercion_expression(node, pathtype, funcId,
747  targetTypeId, targetTypMod,
748  ccontext, cformat, location);
749  }
750 
751  return node;
752 }
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
unsigned int Oid
Definition: postgres_ext.h:31
CoercionPathType
Definition: parse_coerce.h:24
static void hide_coercion_node(Node *node)
Definition: parse_coerce.c:765
static Node * build_coercion_expression(Node *node, CoercionPathType pathtype, Oid funcId, Oid targetTypeId, int32 targetTypMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:793
CoercionPathType find_typmod_coercion_function(Oid typeId, Oid *funcid)
Oid enforce_generic_type_consistency ( Oid actual_arg_types,
Oid declared_arg_types,
int  nargs,
Oid  rettype,
bool  allow_poly 
)

Definition at line 1630 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().

1635 {
1636  int j;
1637  bool have_generics = false;
1638  bool have_unknowns = false;
1639  Oid elem_typeid = InvalidOid;
1640  Oid array_typeid = InvalidOid;
1641  Oid range_typeid = InvalidOid;
1642  Oid array_typelem;
1643  Oid range_typelem;
1644  bool have_anyelement = (rettype == ANYELEMENTOID ||
1645  rettype == ANYNONARRAYOID ||
1646  rettype == ANYENUMOID);
1647  bool have_anynonarray = (rettype == ANYNONARRAYOID);
1648  bool have_anyenum = (rettype == ANYENUMOID);
1649 
1650  /*
1651  * Loop through the arguments to see if we have any that are polymorphic.
1652  * If so, require the actual types to be consistent.
1653  */
1654  for (j = 0; j < nargs; j++)
1655  {
1656  Oid decl_type = declared_arg_types[j];
1657  Oid actual_type = actual_arg_types[j];
1658 
1659  if (decl_type == ANYELEMENTOID ||
1660  decl_type == ANYNONARRAYOID ||
1661  decl_type == ANYENUMOID)
1662  {
1663  have_generics = have_anyelement = true;
1664  if (decl_type == ANYNONARRAYOID)
1665  have_anynonarray = true;
1666  else if (decl_type == ANYENUMOID)
1667  have_anyenum = true;
1668  if (actual_type == UNKNOWNOID)
1669  {
1670  have_unknowns = true;
1671  continue;
1672  }
1673  if (allow_poly && decl_type == actual_type)
1674  continue; /* no new information here */
1675  if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
1676  ereport(ERROR,
1677  (errcode(ERRCODE_DATATYPE_MISMATCH),
1678  errmsg("arguments declared \"anyelement\" are not all alike"),
1679  errdetail("%s versus %s",
1680  format_type_be(elem_typeid),
1681  format_type_be(actual_type))));
1682  elem_typeid = actual_type;
1683  }
1684  else if (decl_type == ANYARRAYOID)
1685  {
1686  have_generics = true;
1687  if (actual_type == UNKNOWNOID)
1688  {
1689  have_unknowns = true;
1690  continue;
1691  }
1692  if (allow_poly && decl_type == actual_type)
1693  continue; /* no new information here */
1694  actual_type = getBaseType(actual_type); /* flatten domains */
1695  if (OidIsValid(array_typeid) && actual_type != array_typeid)
1696  ereport(ERROR,
1697  (errcode(ERRCODE_DATATYPE_MISMATCH),
1698  errmsg("arguments declared \"anyarray\" are not all alike"),
1699  errdetail("%s versus %s",
1700  format_type_be(array_typeid),
1701  format_type_be(actual_type))));
1702  array_typeid = actual_type;
1703  }
1704  else if (decl_type == ANYRANGEOID)
1705  {
1706  have_generics = true;
1707  if (actual_type == UNKNOWNOID)
1708  {
1709  have_unknowns = true;
1710  continue;
1711  }
1712  if (allow_poly && decl_type == actual_type)
1713  continue; /* no new information here */
1714  actual_type = getBaseType(actual_type); /* flatten domains */
1715  if (OidIsValid(range_typeid) && actual_type != range_typeid)
1716  ereport(ERROR,
1717  (errcode(ERRCODE_DATATYPE_MISMATCH),
1718  errmsg("arguments declared \"anyrange\" are not all alike"),
1719  errdetail("%s versus %s",
1720  format_type_be(range_typeid),
1721  format_type_be(actual_type))));
1722  range_typeid = actual_type;
1723  }
1724  }
1725 
1726  /*
1727  * Fast Track: if none of the arguments are polymorphic, return the
1728  * unmodified rettype. We assume it can't be polymorphic either.
1729  */
1730  if (!have_generics)
1731  return rettype;
1732 
1733  /* Get the element type based on the array type, if we have one */
1734  if (OidIsValid(array_typeid))
1735  {
1736  if (array_typeid == ANYARRAYOID && !have_anyelement)
1737  {
1738  /* Special case for ANYARRAY input: okay iff no ANYELEMENT */
1739  array_typelem = ANYELEMENTOID;
1740  }
1741  else
1742  {
1743  array_typelem = get_element_type(array_typeid);
1744  if (!OidIsValid(array_typelem))
1745  ereport(ERROR,
1746  (errcode(ERRCODE_DATATYPE_MISMATCH),
1747  errmsg("argument declared %s is not an array but type %s",
1748  "anyarray", format_type_be(array_typeid))));
1749  }
1750 
1751  if (!OidIsValid(elem_typeid))
1752  {
1753  /*
1754  * if we don't have an element type yet, use the one we just got
1755  */
1756  elem_typeid = array_typelem;
1757  }
1758  else if (array_typelem != elem_typeid)
1759  {
1760  /* otherwise, they better match */
1761  ereport(ERROR,
1762  (errcode(ERRCODE_DATATYPE_MISMATCH),
1763  errmsg("argument declared %s is not consistent with argument declared %s",
1764  "anyarray", "anyelement"),
1765  errdetail("%s versus %s",
1766  format_type_be(array_typeid),
1767  format_type_be(elem_typeid))));
1768  }
1769  }
1770 
1771  /* Get the element type based on the range type, if we have one */
1772  if (OidIsValid(range_typeid))
1773  {
1774  if (range_typeid == ANYRANGEOID && !have_anyelement)
1775  {
1776  /* Special case for ANYRANGE input: okay iff no ANYELEMENT */
1777  range_typelem = ANYELEMENTOID;
1778  }
1779  else
1780  {
1781  range_typelem = get_range_subtype(range_typeid);
1782  if (!OidIsValid(range_typelem))
1783  ereport(ERROR,
1784  (errcode(ERRCODE_DATATYPE_MISMATCH),
1785  errmsg("argument declared %s is not a range type but type %s",
1786  "anyrange",
1787  format_type_be(range_typeid))));
1788  }
1789 
1790  if (!OidIsValid(elem_typeid))
1791  {
1792  /*
1793  * if we don't have an element type yet, use the one we just got
1794  */
1795  elem_typeid = range_typelem;
1796  }
1797  else if (range_typelem != elem_typeid)
1798  {
1799  /* otherwise, they better match */
1800  ereport(ERROR,
1801  (errcode(ERRCODE_DATATYPE_MISMATCH),
1802  errmsg("argument declared %s is not consistent with argument declared %s",
1803  "anyrange", "anyelement"),
1804  errdetail("%s versus %s",
1805  format_type_be(range_typeid),
1806  format_type_be(elem_typeid))));
1807  }
1808  }
1809 
1810  if (!OidIsValid(elem_typeid))
1811  {
1812  if (allow_poly)
1813  {
1814  elem_typeid = ANYELEMENTOID;
1815  array_typeid = ANYARRAYOID;
1816  range_typeid = ANYRANGEOID;
1817  }
1818  else
1819  {
1820  /* Only way to get here is if all the generic args are UNKNOWN */
1821  ereport(ERROR,
1822  (errcode(ERRCODE_DATATYPE_MISMATCH),
1823  errmsg("could not determine polymorphic type because input has type %s",
1824  "unknown")));
1825  }
1826  }
1827 
1828  if (have_anynonarray && elem_typeid != ANYELEMENTOID)
1829  {
1830  /* require the element type to not be an array or domain over array */
1831  if (type_is_array_domain(elem_typeid))
1832  ereport(ERROR,
1833  (errcode(ERRCODE_DATATYPE_MISMATCH),
1834  errmsg("type matched to anynonarray is an array type: %s",
1835  format_type_be(elem_typeid))));
1836  }
1837 
1838  if (have_anyenum && elem_typeid != ANYELEMENTOID)
1839  {
1840  /* require the element type to be an enum */
1841  if (!type_is_enum(elem_typeid))
1842  ereport(ERROR,
1843  (errcode(ERRCODE_DATATYPE_MISMATCH),
1844  errmsg("type matched to anyenum is not an enum type: %s",
1845  format_type_be(elem_typeid))));
1846  }
1847 
1848  /*
1849  * If we had any unknown inputs, re-scan to assign correct types
1850  */
1851  if (have_unknowns)
1852  {
1853  for (j = 0; j < nargs; j++)
1854  {
1855  Oid decl_type = declared_arg_types[j];
1856  Oid actual_type = actual_arg_types[j];
1857 
1858  if (actual_type != UNKNOWNOID)
1859  continue;
1860 
1861  if (decl_type == ANYELEMENTOID ||
1862  decl_type == ANYNONARRAYOID ||
1863  decl_type == ANYENUMOID)
1864  declared_arg_types[j] = elem_typeid;
1865  else if (decl_type == 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  declared_arg_types[j] = array_typeid;
1877  }
1878  else if (decl_type == ANYRANGEOID)
1879  {
1880  if (!OidIsValid(range_typeid))
1881  {
1882  ereport(ERROR,
1883  (errcode(ERRCODE_UNDEFINED_OBJECT),
1884  errmsg("could not find range type for data type %s",
1885  format_type_be(elem_typeid))));
1886  }
1887  declared_arg_types[j] = range_typeid;
1888  }
1889  }
1890  }
1891 
1892  /* if we return ANYARRAY use the appropriate argument type */
1893  if (rettype == ANYARRAYOID)
1894  {
1895  if (!OidIsValid(array_typeid))
1896  {
1897  array_typeid = get_array_type(elem_typeid);
1898  if (!OidIsValid(array_typeid))
1899  ereport(ERROR,
1900  (errcode(ERRCODE_UNDEFINED_OBJECT),
1901  errmsg("could not find array type for data type %s",
1902  format_type_be(elem_typeid))));
1903  }
1904  return array_typeid;
1905  }
1906 
1907  /* if we return ANYRANGE use the appropriate argument type */
1908  if (rettype == ANYRANGEOID)
1909  {
1910  if (!OidIsValid(range_typeid))
1911  {
1912  ereport(ERROR,
1913  (errcode(ERRCODE_UNDEFINED_OBJECT),
1914  errmsg("could not find range type for data type %s",
1915  format_type_be(elem_typeid))));
1916  }
1917  return range_typeid;
1918  }
1919 
1920  /* if we return ANYELEMENT use the appropriate argument type */
1921  if (rettype == ANYELEMENTOID ||
1922  rettype == ANYNONARRAYOID ||
1923  rettype == ANYENUMOID)
1924  return elem_typeid;
1925 
1926  /* we don't return a generic type; send back the original return type */
1927  return rettype;
1928 }
#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:532
#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 2189 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_element_type(), getBaseType(), GETSTRUCT, HeapTupleIsValid, INT2VECTOROID, InvalidOid, ObjectIdGetDatum, OidIsValid, OIDVECTOROID, ReleaseSysCache(), 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().

2192 {
2194  HeapTuple tuple;
2195 
2196  *funcid = InvalidOid;
2197 
2198  /* Perhaps the types are domains; if so, look at their base types */
2199  if (OidIsValid(sourceTypeId))
2200  sourceTypeId = getBaseType(sourceTypeId);
2201  if (OidIsValid(targetTypeId))
2202  targetTypeId = getBaseType(targetTypeId);
2203 
2204  /* Domains are always coercible to and from their base type */
2205  if (sourceTypeId == targetTypeId)
2207 
2208  /* Look in pg_cast */
2210  ObjectIdGetDatum(sourceTypeId),
2211  ObjectIdGetDatum(targetTypeId));
2212 
2213  if (HeapTupleIsValid(tuple))
2214  {
2215  Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
2216  CoercionContext castcontext;
2217 
2218  /* convert char value for castcontext to CoercionContext enum */
2219  switch (castForm->castcontext)
2220  {
2222  castcontext = COERCION_IMPLICIT;
2223  break;
2225  castcontext = COERCION_ASSIGNMENT;
2226  break;
2228  castcontext = COERCION_EXPLICIT;
2229  break;
2230  default:
2231  elog(ERROR, "unrecognized castcontext: %d",
2232  (int) castForm->castcontext);
2233  castcontext = 0; /* keep compiler quiet */
2234  break;
2235  }
2236 
2237  /* Rely on ordering of enum for correct behavior here */
2238  if (ccontext >= castcontext)
2239  {
2240  switch (castForm->castmethod)
2241  {
2243  result = COERCION_PATH_FUNC;
2244  *funcid = castForm->castfunc;
2245  break;
2246  case COERCION_METHOD_INOUT:
2247  result = COERCION_PATH_COERCEVIAIO;
2248  break;
2250  result = COERCION_PATH_RELABELTYPE;
2251  break;
2252  default:
2253  elog(ERROR, "unrecognized castmethod: %d",
2254  (int) castForm->castmethod);
2255  break;
2256  }
2257  }
2258 
2259  ReleaseSysCache(tuple);
2260  }
2261  else
2262  {
2263  /*
2264  * If there's no pg_cast entry, perhaps we are dealing with a pair of
2265  * array types. If so, and if their element types have a conversion
2266  * pathway, report that we can coerce with an ArrayCoerceExpr.
2267  *
2268  * Hack: disallow coercions to oidvector and int2vector, which
2269  * otherwise tend to capture coercions that should go to "real" array
2270  * types. We want those types to be considered "real" arrays for many
2271  * purposes, but not this one. (Also, ArrayCoerceExpr isn't
2272  * guaranteed to produce an output that meets the restrictions of
2273  * these datatypes, such as being 1-dimensional.)
2274  */
2275  if (targetTypeId != OIDVECTOROID && targetTypeId != INT2VECTOROID)
2276  {
2277  Oid targetElem;
2278  Oid sourceElem;
2279 
2280  if ((targetElem = get_element_type(targetTypeId)) != InvalidOid &&
2281  (sourceElem = get_element_type(sourceTypeId)) != InvalidOid)
2282  {
2283  CoercionPathType elempathtype;
2284  Oid elemfuncid;
2285 
2286  elempathtype = find_coercion_pathway(targetElem,
2287  sourceElem,
2288  ccontext,
2289  &elemfuncid);
2290  if (elempathtype != COERCION_PATH_NONE)
2291  {
2292  result = COERCION_PATH_ARRAYCOERCE;
2293  }
2294  }
2295  }
2296 
2297  /*
2298  * If we still haven't found a possibility, consider automatic casting
2299  * using I/O functions. We allow assignment casts to string types and
2300  * explicit casts from string types to be handled this way. (The
2301  * CoerceViaIO mechanism is a lot more general than that, but this is
2302  * all we want to allow in the absence of a pg_cast entry.) It would
2303  * probably be better to insist on explicit casts in both directions,
2304  * but this is a compromise to preserve something of the pre-8.3
2305  * behavior that many types had implicit (yipes!) casts to text.
2306  */
2307  if (result == COERCION_PATH_NONE)
2308  {
2309  if (ccontext >= COERCION_ASSIGNMENT &&
2310  TypeCategory(targetTypeId) == TYPCATEGORY_STRING)
2311  result = COERCION_PATH_COERCEVIAIO;
2312  else if (ccontext >= COERCION_EXPLICIT &&
2313  TypeCategory(sourceTypeId) == TYPCATEGORY_STRING)
2314  result = COERCION_PATH_COERCEVIAIO;
2315  }
2316  }
2317 
2318  return result;
2319 }
#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
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:532
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:1160
#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
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1123
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
CoercionPathType find_typmod_coercion_function ( Oid  typeId,
Oid funcid 
)

Definition at line 2344 of file parse_coerce.c.

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

Referenced by coerce_type_typmod().

2346 {
2347  CoercionPathType result;
2348  Type targetType;
2349  Form_pg_type typeForm;
2350  HeapTuple tuple;
2351 
2352  *funcid = InvalidOid;
2353  result = COERCION_PATH_FUNC;
2354 
2355  targetType = typeidType(typeId);
2356  typeForm = (Form_pg_type) GETSTRUCT(targetType);
2357 
2358  /* Check for a varlena array type */
2359  if (typeForm->typelem != InvalidOid && typeForm->typlen == -1)
2360  {
2361  /* Yes, switch our attention to the element type */
2362  typeId = typeForm->typelem;
2363  result = COERCION_PATH_ARRAYCOERCE;
2364  }
2365  ReleaseSysCache(targetType);
2366 
2367  /* Look in pg_cast */
2369  ObjectIdGetDatum(typeId),
2370  ObjectIdGetDatum(typeId));
2371 
2372  if (HeapTupleIsValid(tuple))
2373  {
2374  Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
2375 
2376  *funcid = castForm->castfunc;
2377  ReleaseSysCache(tuple);
2378  }
2379 
2380  if (!OidIsValid(*funcid))
2381  result = COERCION_PATH_NONE;
2382 
2383  return result;
2384 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
#define OidIsValid(objectId)
Definition: c.h:532
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:1160
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1123
Type typeidType(Oid id)
Definition: parse_type.c:559
static void hide_coercion_node ( Node node)
static

Definition at line 765 of file parse_coerce.c.

References COERCE_IMPLICIT_CAST, elog, ERROR, IsA, and nodeTag.

Referenced by coerce_to_domain(), and coerce_type_typmod().

766 {
767  if (IsA(node, FuncExpr))
768  ((FuncExpr *) node)->funcformat = COERCE_IMPLICIT_CAST;
769  else if (IsA(node, RelabelType))
770  ((RelabelType *) node)->relabelformat = COERCE_IMPLICIT_CAST;
771  else if (IsA(node, CoerceViaIO))
772  ((CoerceViaIO *) node)->coerceformat = COERCE_IMPLICIT_CAST;
773  else if (IsA(node, ArrayCoerceExpr))
774  ((ArrayCoerceExpr *) node)->coerceformat = COERCE_IMPLICIT_CAST;
775  else if (IsA(node, ConvertRowtypeExpr))
776  ((ConvertRowtypeExpr *) node)->convertformat = COERCE_IMPLICIT_CAST;
777  else if (IsA(node, RowExpr))
778  ((RowExpr *) node)->row_format = COERCE_IMPLICIT_CAST;
779  else if (IsA(node, CoerceToDomain))
780  ((CoerceToDomain *) node)->coercionformat = COERCE_IMPLICIT_CAST;
781  else
782  elog(ERROR, "unsupported node type: %d", (int) nodeTag(node));
783 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
#define ERROR
Definition: elog.h:43
#define nodeTag(nodeptr)
Definition: nodes.h:515
#define elog
Definition: elog.h:219
static bool is_complex_array ( Oid  typid)
static

Definition at line 2394 of file parse_coerce.c.

References get_element_type(), ISCOMPLEX, and OidIsValid.

Referenced by can_coerce_type(), coerce_type(), and IsBinaryCoercible().

2395 {
2396  Oid elemtype = get_element_type(typid);
2397 
2398  return (OidIsValid(elemtype) && ISCOMPLEX(elemtype));
2399 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2484
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:532
#define ISCOMPLEX(typeid)
Definition: parse_type.h:53
bool IsBinaryCoercible ( Oid  srctype,
Oid  targettype 
)

Definition at line 2093 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(), 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().

2094 {
2095  HeapTuple tuple;
2096  Form_pg_cast castForm;
2097  bool result;
2098 
2099  /* Fast path if same type */
2100  if (srctype == targettype)
2101  return true;
2102 
2103  /* Anything is coercible to ANY or ANYELEMENT */
2104  if (targettype == ANYOID || targettype == ANYELEMENTOID)
2105  return true;
2106 
2107  /* If srctype is a domain, reduce to its base type */
2108  if (OidIsValid(srctype))
2109  srctype = getBaseType(srctype);
2110 
2111  /* Somewhat-fast path for domain -> base type case */
2112  if (srctype == targettype)
2113  return true;
2114 
2115  /* Also accept any array type as coercible to ANYARRAY */
2116  if (targettype == ANYARRAYOID)
2117  if (type_is_array(srctype))
2118  return true;
2119 
2120  /* Also accept any non-array type as coercible to ANYNONARRAY */
2121  if (targettype == ANYNONARRAYOID)
2122  if (!type_is_array(srctype))
2123  return true;
2124 
2125  /* Also accept any enum type as coercible to ANYENUM */
2126  if (targettype == ANYENUMOID)
2127  if (type_is_enum(srctype))
2128  return true;
2129 
2130  /* Also accept any range type as coercible to ANYRANGE */
2131  if (targettype == ANYRANGEOID)
2132  if (type_is_range(srctype))
2133  return true;
2134 
2135  /* Also accept any composite type as coercible to RECORD */
2136  if (targettype == RECORDOID)
2137  if (ISCOMPLEX(srctype))
2138  return true;
2139 
2140  /* Also accept any composite array type as coercible to RECORD[] */
2141  if (targettype == RECORDARRAYOID)
2142  if (is_complex_array(srctype))
2143  return true;
2144 
2145  /* Else look in pg_cast */
2147  ObjectIdGetDatum(srctype),
2148  ObjectIdGetDatum(targettype));
2149  if (!HeapTupleIsValid(tuple))
2150  return false; /* no cast */
2151  castForm = (Form_pg_cast) GETSTRUCT(tuple);
2152 
2153  result = (castForm->castmethod == COERCION_METHOD_BINARY &&
2154  castForm->castcontext == COERCION_CODE_IMPLICIT);
2155 
2156  ReleaseSysCache(tuple);
2157 
2158  return result;
2159 }
#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
#define OidIsValid(objectId)
Definition: c.h:532
#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:1160
#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
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1123
#define type_is_array(typid)
Definition: lsyscache.h:180
static bool is_complex_array(Oid typid)
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
bool IsPreferredType ( TYPCATEGORY  category,
Oid  type 
)

Definition at line 2058 of file parse_coerce.c.

References get_type_category_preferred(), and TYPCATEGORY_INVALID.

Referenced by func_select_candidate(), and GetDefaultOpClass().

2059 {
2060  char typcategory;
2061  bool typispreferred;
2062 
2063  get_type_category_preferred(type, &typcategory, &typispreferred);
2064  if (category == typcategory || category == TYPCATEGORY_INVALID)
2065  return typispreferred;
2066  else
2067  return false;
2068 }
#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 1207 of file parse_coerce.c.

References exprLocation(), and parser_errposition().

Referenced by coerce_record_to_complex(), and transformTypeCast().

1210 {
1211  if (coerce_location >= 0)
1212  return parser_errposition(pstate, coerce_location);
1213  else
1214  return parser_errposition(pstate, exprLocation(input_expr));
1215 }
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 1944 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().

1947 {
1948  if (declared_type == ANYARRAYOID)
1949  {
1950  if (context_declared_type == ANYARRAYOID)
1951  {
1952  /*
1953  * Use actual type, but it must be an array; or if it's a domain
1954  * over array, use the base array type.
1955  */
1956  Oid context_base_type = getBaseType(context_actual_type);
1957  Oid array_typelem = get_element_type(context_base_type);
1958 
1959  if (!OidIsValid(array_typelem))
1960  ereport(ERROR,
1961  (errcode(ERRCODE_DATATYPE_MISMATCH),
1962  errmsg("argument declared %s is not an array but type %s",
1963  "anyarray", format_type_be(context_base_type))));
1964  return context_base_type;
1965  }
1966  else if (context_declared_type == ANYELEMENTOID ||
1967  context_declared_type == ANYNONARRAYOID ||
1968  context_declared_type == ANYENUMOID ||
1969  context_declared_type == ANYRANGEOID)
1970  {
1971  /* Use the array type corresponding to actual type */
1972  Oid array_typeid = get_array_type(context_actual_type);
1973 
1974  if (!OidIsValid(array_typeid))
1975  ereport(ERROR,
1976  (errcode(ERRCODE_UNDEFINED_OBJECT),
1977  errmsg("could not find array type for data type %s",
1978  format_type_be(context_actual_type))));
1979  return array_typeid;
1980  }
1981  }
1982  else if (declared_type == ANYELEMENTOID ||
1983  declared_type == ANYNONARRAYOID ||
1984  declared_type == ANYENUMOID ||
1985  declared_type == ANYRANGEOID)
1986  {
1987  if (context_declared_type == ANYARRAYOID)
1988  {
1989  /* Use the element type corresponding to actual type */
1990  Oid context_base_type = getBaseType(context_actual_type);
1991  Oid array_typelem = get_element_type(context_base_type);
1992 
1993  if (!OidIsValid(array_typelem))
1994  ereport(ERROR,
1995  (errcode(ERRCODE_DATATYPE_MISMATCH),
1996  errmsg("argument declared %s is not an array but type %s",
1997  "anyarray", format_type_be(context_base_type))));
1998  return array_typelem;
1999  }
2000  else if (context_declared_type == ANYRANGEOID)
2001  {
2002  /* Use the element type corresponding to actual type */
2003  Oid context_base_type = getBaseType(context_actual_type);
2004  Oid range_typelem = get_range_subtype(context_base_type);
2005 
2006  if (!OidIsValid(range_typelem))
2007  ereport(ERROR,
2008  (errcode(ERRCODE_DATATYPE_MISMATCH),
2009  errmsg("argument declared %s is not a range type but type %s",
2010  "anyrange", format_type_be(context_base_type))));
2011  return range_typelem;
2012  }
2013  else if (context_declared_type == ANYELEMENTOID ||
2014  context_declared_type == ANYNONARRAYOID ||
2015  context_declared_type == ANYENUMOID)
2016  {
2017  /* Use the actual type; it doesn't matter if array or not */
2018  return context_actual_type;
2019  }
2020  }
2021  else
2022  {
2023  /* declared_type isn't polymorphic, so return it as-is */
2024  return declared_type;
2025  }
2026  /* If we get here, declared_type is polymorphic and context isn't */
2027  /* NB: this is a calling-code logic error, not a user error */
2028  elog(ERROR, "could not determine polymorphic type because context isn't polymorphic");
2029  return InvalidOid; /* keep compiler quiet */
2030 }
#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:532
#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 1233 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, parser_errposition(), TEXTOID, and UNKNOWNOID.

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

1235 {
1236  Node *pexpr;
1237  Oid ptype;
1238  TYPCATEGORY pcategory;
1239  bool pispreferred;
1240  ListCell *lc;
1241 
1242  Assert(exprs != NIL);
1243  pexpr = (Node *) linitial(exprs);
1244  lc = lnext(list_head(exprs));
1245  ptype = exprType(pexpr);
1246 
1247  /*
1248  * If all input types are valid and exactly the same, just pick that type.
1249  * This is the only way that we will resolve the result as being a domain
1250  * type; otherwise domains are smashed to their base types for comparison.
1251  */
1252  if (ptype != UNKNOWNOID)
1253  {
1254  for_each_cell(lc, lc)
1255  {
1256  Node *nexpr = (Node *) lfirst(lc);
1257  Oid ntype = exprType(nexpr);
1258 
1259  if (ntype != ptype)
1260  break;
1261  }
1262  if (lc == NULL) /* got to the end of the list? */
1263  {
1264  if (which_expr)
1265  *which_expr = pexpr;
1266  return ptype;
1267  }
1268  }
1269 
1270  /*
1271  * Nope, so set up for the full algorithm. Note that at this point, lc
1272  * points to the first list item with type different from pexpr's; we need
1273  * not re-examine any items the previous loop advanced over.
1274  */
1275  ptype = getBaseType(ptype);
1276  get_type_category_preferred(ptype, &pcategory, &pispreferred);
1277 
1278  for_each_cell(lc, lc)
1279  {
1280  Node *nexpr = (Node *) lfirst(lc);
1281  Oid ntype = getBaseType(exprType(nexpr));
1282 
1283  /* move on to next one if no new information... */
1284  if (ntype != UNKNOWNOID && ntype != ptype)
1285  {
1286  TYPCATEGORY ncategory;
1287  bool nispreferred;
1288 
1289  get_type_category_preferred(ntype, &ncategory, &nispreferred);
1290  if (ptype == UNKNOWNOID)
1291  {
1292  /* so far, only unknowns so take anything... */
1293  pexpr = nexpr;
1294  ptype = ntype;
1295  pcategory = ncategory;
1296  pispreferred = nispreferred;
1297  }
1298  else if (ncategory != pcategory)
1299  {
1300  /*
1301  * both types in different categories? then not much hope...
1302  */
1303  if (context == NULL)
1304  return InvalidOid;
1305  ereport(ERROR,
1306  (errcode(ERRCODE_DATATYPE_MISMATCH),
1307  /*------
1308  translator: first %s is name of a SQL construct, eg CASE */
1309  errmsg("%s types %s and %s cannot be matched",
1310  context,
1311  format_type_be(ptype),
1312  format_type_be(ntype)),
1313  parser_errposition(pstate, exprLocation(nexpr))));
1314  }
1315  else if (!pispreferred &&
1316  can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
1317  !can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
1318  {
1319  /*
1320  * take new type if can coerce to it implicitly but not the
1321  * other way; but if we have a preferred type, stay on it.
1322  */
1323  pexpr = nexpr;
1324  ptype = ntype;
1325  pcategory = ncategory;
1326  pispreferred = nispreferred;
1327  }
1328  }
1329  }
1330 
1331  /*
1332  * If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
1333  * then resolve as type TEXT. This situation comes up with constructs
1334  * like SELECT (CASE WHEN foo THEN 'bar' ELSE 'baz' END); SELECT 'foo'
1335  * UNION SELECT 'bar'; It might seem desirable to leave the construct's
1336  * output type as UNKNOWN, but that really doesn't work, because we'd
1337  * probably end up needing a runtime coercion from UNKNOWN to something
1338  * else, and we usually won't have it. We need to coerce the unknown
1339  * literals while they are still literals, so a decision has to be made
1340  * now.
1341  */
1342  if (ptype == UNKNOWNOID)
1343  ptype = TEXTOID;
1344 
1345  if (which_expr)
1346  *which_expr = pexpr;
1347  return ptype;
1348 }
#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:510
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 Assert(condition)
Definition: c.h:681
#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:526
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 2039 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().

2040 {
2041  char typcategory;
2042  bool typispreferred;
2043 
2044  get_type_category_preferred(type, &typcategory, &typispreferred);
2045  Assert(typcategory != TYPCATEGORY_INVALID);
2046  return (TYPCATEGORY) typcategory;
2047 }
char TYPCATEGORY
Definition: parse_coerce.h:21
#define TYPCATEGORY_INVALID
Definition: pg_type.h:727
#define Assert(condition)
Definition: c.h:681
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2436
static bool typeIsOfTypedTable ( Oid  reltypeId,
Oid  reloftypeId 
)
static

Definition at line 2408 of file parse_coerce.c.

References elog, ERROR, GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), RELOID, SearchSysCache1(), and typeidTypeRelid().

Referenced by can_coerce_type(), and coerce_type().

2409 {
2410  Oid relid = typeidTypeRelid(reltypeId);
2411  bool result = false;
2412 
2413  if (relid)
2414  {
2415  HeapTuple tp;
2416  Form_pg_class reltup;
2417 
2418  tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
2419  if (!HeapTupleIsValid(tp))
2420  elog(ERROR, "cache lookup failed for relation %u", relid);
2421 
2422  reltup = (Form_pg_class) GETSTRUCT(tp);
2423  if (reltup->reloftype == reloftypeId)
2424  result = true;
2425 
2426  ReleaseSysCache(tp);
2427  }
2428 
2429  return result;
2430 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid typeidTypeRelid(Oid type_id)
Definition: parse_type.c:646
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
#define elog
Definition: elog.h:219