PostgreSQL Source Code  git master
parse_coerce.h File Reference
Include dependency graph for parse_coerce.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef char TYPCATEGORY
 
typedef enum CoercionPathType CoercionPathType
 

Enumerations

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

Functions

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

Typedef Documentation

◆ CoercionPathType

◆ TYPCATEGORY

typedef char TYPCATEGORY

Definition at line 21 of file parse_coerce.h.

Enumeration Type Documentation

◆ CoercionPathType

Enumerator
COERCION_PATH_NONE 
COERCION_PATH_FUNC 
COERCION_PATH_RELABELTYPE 
COERCION_PATH_ARRAYCOERCE 
COERCION_PATH_COERCEVIAIO 

Definition at line 24 of file parse_coerce.h.

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

Function Documentation

◆ can_coerce_type()

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

Definition at line 556 of file parse_coerce.c.

558 {
559  bool have_generics = false;
560  int i;
561 
562  /* run through argument list... */
563  for (i = 0; i < nargs; i++)
564  {
565  Oid inputTypeId = input_typeids[i];
566  Oid targetTypeId = target_typeids[i];
567  CoercionPathType pathtype;
568  Oid funcId;
569 
570  /* no problem if same type */
571  if (inputTypeId == targetTypeId)
572  continue;
573 
574  /* accept if target is ANY */
575  if (targetTypeId == ANYOID)
576  continue;
577 
578  /* accept if target is polymorphic, for now */
579  if (IsPolymorphicType(targetTypeId))
580  {
581  have_generics = true; /* do more checking later */
582  continue;
583  }
584 
585  /*
586  * If input is an untyped string constant, assume we can convert it to
587  * anything.
588  */
589  if (inputTypeId == UNKNOWNOID)
590  continue;
591 
592  /*
593  * If pg_cast shows that we can coerce, accept. This test now covers
594  * both binary-compatible and coercion-function cases.
595  */
596  pathtype = find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
597  &funcId);
598  if (pathtype != COERCION_PATH_NONE)
599  continue;
600 
601  /*
602  * If input is RECORD and target is a composite type, assume we can
603  * coerce (may need tighter checking here)
604  */
605  if (inputTypeId == RECORDOID &&
606  ISCOMPLEX(targetTypeId))
607  continue;
608 
609  /*
610  * If input is a composite type and target is RECORD, accept
611  */
612  if (targetTypeId == RECORDOID &&
613  ISCOMPLEX(inputTypeId))
614  continue;
615 
616 #ifdef NOT_USED /* not implemented yet */
617 
618  /*
619  * If input is record[] and target is a composite array type, assume
620  * we can coerce (may need tighter checking here)
621  */
622  if (inputTypeId == RECORDARRAYOID &&
623  is_complex_array(targetTypeId))
624  continue;
625 #endif
626 
627  /*
628  * If input is a composite array type and target is record[], accept
629  */
630  if (targetTypeId == RECORDARRAYOID &&
631  is_complex_array(inputTypeId))
632  continue;
633 
634  /*
635  * If input is a class type that inherits from target, accept
636  */
637  if (typeInheritsFrom(inputTypeId, targetTypeId)
638  || typeIsOfTypedTable(inputTypeId, targetTypeId))
639  continue;
640 
641  /*
642  * Else, cannot coerce at this argument position
643  */
644  return false;
645  }
646 
647  /* If we found any generic argument types, cross-check them */
648  if (have_generics)
649  {
650  if (!check_generic_type_consistency(input_typeids, target_typeids,
651  nargs))
652  return false;
653  }
654 
655  return true;
656 }
int i
Definition: isn.c:72
static bool typeIsOfTypedTable(Oid reltypeId, Oid reloftypeId)
bool check_generic_type_consistency(const Oid *actual_arg_types, const Oid *declared_arg_types, int nargs)
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
static bool is_complex_array(Oid typid)
#define ISCOMPLEX(typeid)
Definition: parse_type.h:59
bool typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
Definition: pg_inherits.c:406
unsigned int Oid
Definition: postgres_ext.h:31

References check_generic_type_consistency(), COERCION_PATH_NONE, find_coercion_pathway(), i, is_complex_array(), ISCOMPLEX, typeInheritsFrom(), and typeIsOfTypedTable().

Referenced by ATAddForeignKeyConstraint(), coerce_to_common_type(), coerce_to_target_type(), func_match_argtypes(), func_select_candidate(), jsonb_subscript_transform(), select_common_type(), select_common_type_from_oids(), transformFrameOffset(), verify_common_type(), and verify_common_type_from_oids().

◆ check_generic_type_consistency()

bool check_generic_type_consistency ( const Oid actual_arg_types,
const Oid declared_arg_types,
int  nargs 
)

Definition at line 1702 of file parse_coerce.c.

1705 {
1706  Oid elem_typeid = InvalidOid;
1707  Oid array_typeid = InvalidOid;
1708  Oid range_typeid = InvalidOid;
1709  Oid multirange_typeid = InvalidOid;
1710  Oid anycompatible_range_typeid = InvalidOid;
1711  Oid anycompatible_range_typelem = InvalidOid;
1712  Oid anycompatible_multirange_typeid = InvalidOid;
1713  Oid anycompatible_multirange_typelem = InvalidOid;
1714  Oid range_typelem = InvalidOid;
1715  bool have_anynonarray = false;
1716  bool have_anyenum = false;
1717  bool have_anycompatible_nonarray = false;
1718  int n_anycompatible_args = 0;
1719  Oid anycompatible_actual_types[FUNC_MAX_ARGS];
1720 
1721  /*
1722  * Loop through the arguments to see if we have any that are polymorphic.
1723  * If so, require the actual types to be consistent.
1724  */
1725  Assert(nargs <= FUNC_MAX_ARGS);
1726  for (int j = 0; j < nargs; j++)
1727  {
1728  Oid decl_type = declared_arg_types[j];
1729  Oid actual_type = actual_arg_types[j];
1730 
1731  if (decl_type == ANYELEMENTOID ||
1732  decl_type == ANYNONARRAYOID ||
1733  decl_type == ANYENUMOID)
1734  {
1735  if (decl_type == ANYNONARRAYOID)
1736  have_anynonarray = true;
1737  else if (decl_type == ANYENUMOID)
1738  have_anyenum = true;
1739  if (actual_type == UNKNOWNOID)
1740  continue;
1741  if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
1742  return false;
1743  elem_typeid = actual_type;
1744  }
1745  else if (decl_type == ANYARRAYOID)
1746  {
1747  if (actual_type == UNKNOWNOID)
1748  continue;
1749  actual_type = getBaseType(actual_type); /* flatten domains */
1750  if (OidIsValid(array_typeid) && actual_type != array_typeid)
1751  return false;
1752  array_typeid = actual_type;
1753  }
1754  else if (decl_type == ANYRANGEOID)
1755  {
1756  if (actual_type == UNKNOWNOID)
1757  continue;
1758  actual_type = getBaseType(actual_type); /* flatten domains */
1759  if (OidIsValid(range_typeid) && actual_type != range_typeid)
1760  return false;
1761  range_typeid = actual_type;
1762  }
1763  else if (decl_type == ANYMULTIRANGEOID)
1764  {
1765  if (actual_type == UNKNOWNOID)
1766  continue;
1767  actual_type = getBaseType(actual_type); /* flatten domains */
1768  if (OidIsValid(multirange_typeid) && actual_type != multirange_typeid)
1769  return false;
1770  multirange_typeid = actual_type;
1771  }
1772  else if (decl_type == ANYCOMPATIBLEOID ||
1773  decl_type == ANYCOMPATIBLENONARRAYOID)
1774  {
1775  if (decl_type == ANYCOMPATIBLENONARRAYOID)
1776  have_anycompatible_nonarray = true;
1777  if (actual_type == UNKNOWNOID)
1778  continue;
1779  /* collect the actual types of non-unknown COMPATIBLE args */
1780  anycompatible_actual_types[n_anycompatible_args++] = actual_type;
1781  }
1782  else if (decl_type == ANYCOMPATIBLEARRAYOID)
1783  {
1784  Oid elem_type;
1785 
1786  if (actual_type == UNKNOWNOID)
1787  continue;
1788  actual_type = getBaseType(actual_type); /* flatten domains */
1789  elem_type = get_element_type(actual_type);
1790  if (!OidIsValid(elem_type))
1791  return false; /* not an array */
1792  /* collect the element type for common-supertype choice */
1793  anycompatible_actual_types[n_anycompatible_args++] = elem_type;
1794  }
1795  else if (decl_type == ANYCOMPATIBLERANGEOID)
1796  {
1797  if (actual_type == UNKNOWNOID)
1798  continue;
1799  actual_type = getBaseType(actual_type); /* flatten domains */
1800  if (OidIsValid(anycompatible_range_typeid))
1801  {
1802  /* All ANYCOMPATIBLERANGE arguments must be the same type */
1803  if (anycompatible_range_typeid != actual_type)
1804  return false;
1805  }
1806  else
1807  {
1808  anycompatible_range_typeid = actual_type;
1809  anycompatible_range_typelem = get_range_subtype(actual_type);
1810  if (!OidIsValid(anycompatible_range_typelem))
1811  return false; /* not a range type */
1812  /* collect the subtype for common-supertype choice */
1813  anycompatible_actual_types[n_anycompatible_args++] = anycompatible_range_typelem;
1814  }
1815  }
1816  else if (decl_type == ANYCOMPATIBLEMULTIRANGEOID)
1817  {
1818  if (actual_type == UNKNOWNOID)
1819  continue;
1820  actual_type = getBaseType(actual_type); /* flatten domains */
1821  if (OidIsValid(anycompatible_multirange_typeid))
1822  {
1823  /* All ANYCOMPATIBLEMULTIRANGE arguments must be the same type */
1824  if (anycompatible_multirange_typeid != actual_type)
1825  return false;
1826  }
1827  else
1828  {
1829  anycompatible_multirange_typeid = actual_type;
1830  anycompatible_multirange_typelem = get_multirange_range(actual_type);
1831  if (!OidIsValid(anycompatible_multirange_typelem))
1832  return false; /* not a multirange type */
1833  /* we'll consider the subtype below */
1834  }
1835  }
1836  }
1837 
1838  /* Get the element type based on the array type, if we have one */
1839  if (OidIsValid(array_typeid))
1840  {
1841  if (array_typeid == ANYARRAYOID)
1842  {
1843  /*
1844  * Special case for matching ANYARRAY input to an ANYARRAY
1845  * argument: allow it for now. enforce_generic_type_consistency()
1846  * might complain later, depending on the presence of other
1847  * polymorphic arguments or results, but it will deliver a less
1848  * surprising error message than "function does not exist".
1849  *
1850  * (If you think to change this, note that can_coerce_type will
1851  * consider such a situation as a match, so that we might not even
1852  * get here.)
1853  */
1854  }
1855  else
1856  {
1857  Oid array_typelem;
1858 
1859  array_typelem = get_element_type(array_typeid);
1860  if (!OidIsValid(array_typelem))
1861  return false; /* should be an array, but isn't */
1862 
1863  if (!OidIsValid(elem_typeid))
1864  {
1865  /*
1866  * if we don't have an element type yet, use the one we just
1867  * got
1868  */
1869  elem_typeid = array_typelem;
1870  }
1871  else if (array_typelem != elem_typeid)
1872  {
1873  /* otherwise, they better match */
1874  return false;
1875  }
1876  }
1877  }
1878 
1879  /* Deduce range type from multirange type, or check that they agree */
1880  if (OidIsValid(multirange_typeid))
1881  {
1882  Oid multirange_typelem;
1883 
1884  multirange_typelem = get_multirange_range(multirange_typeid);
1885  if (!OidIsValid(multirange_typelem))
1886  return false; /* should be a multirange, but isn't */
1887 
1888  if (!OidIsValid(range_typeid))
1889  {
1890  /* If we don't have a range type yet, use the one we just got */
1891  range_typeid = multirange_typelem;
1892  range_typelem = get_range_subtype(multirange_typelem);
1893  if (!OidIsValid(range_typelem))
1894  return false; /* should be a range, but isn't */
1895  }
1896  else if (multirange_typelem != range_typeid)
1897  {
1898  /* otherwise, they better match */
1899  return false;
1900  }
1901  }
1902 
1903  /* Get the element type based on the range type, if we have one */
1904  if (OidIsValid(range_typeid))
1905  {
1906  range_typelem = get_range_subtype(range_typeid);
1907  if (!OidIsValid(range_typelem))
1908  return false; /* should be a range, but isn't */
1909 
1910  if (!OidIsValid(elem_typeid))
1911  {
1912  /*
1913  * If we don't have an element type yet, use the one we just got
1914  */
1915  elem_typeid = range_typelem;
1916  }
1917  else if (range_typelem != elem_typeid)
1918  {
1919  /* otherwise, they better match */
1920  return false;
1921  }
1922  }
1923 
1924  if (have_anynonarray)
1925  {
1926  /* require the element type to not be an array or domain over array */
1927  if (type_is_array_domain(elem_typeid))
1928  return false;
1929  }
1930 
1931  if (have_anyenum)
1932  {
1933  /* require the element type to be an enum */
1934  if (!type_is_enum(elem_typeid))
1935  return false;
1936  }
1937 
1938  /* Deduce range type from multirange type, or check that they agree */
1939  if (OidIsValid(anycompatible_multirange_typeid))
1940  {
1941  if (OidIsValid(anycompatible_range_typeid))
1942  {
1943  if (anycompatible_multirange_typelem !=
1944  anycompatible_range_typeid)
1945  return false;
1946  }
1947  else
1948  {
1949  anycompatible_range_typeid = anycompatible_multirange_typelem;
1950  anycompatible_range_typelem = get_range_subtype(anycompatible_range_typeid);
1951  if (!OidIsValid(anycompatible_range_typelem))
1952  return false; /* not a range type */
1953  /* collect the subtype for common-supertype choice */
1954  anycompatible_actual_types[n_anycompatible_args++] =
1955  anycompatible_range_typelem;
1956  }
1957  }
1958 
1959  /* Check matching of ANYCOMPATIBLE-family arguments, if any */
1960  if (n_anycompatible_args > 0)
1961  {
1962  Oid anycompatible_typeid;
1963 
1964  anycompatible_typeid =
1965  select_common_type_from_oids(n_anycompatible_args,
1966  anycompatible_actual_types,
1967  true);
1968 
1969  if (!OidIsValid(anycompatible_typeid))
1970  return false; /* there's definitely no common supertype */
1971 
1972  /* We have to verify that the selected type actually works */
1973  if (!verify_common_type_from_oids(anycompatible_typeid,
1974  n_anycompatible_args,
1975  anycompatible_actual_types))
1976  return false;
1977 
1978  if (have_anycompatible_nonarray)
1979  {
1980  /*
1981  * require the anycompatible type to not be an array or domain
1982  * over array
1983  */
1984  if (type_is_array_domain(anycompatible_typeid))
1985  return false;
1986  }
1987 
1988  /*
1989  * The anycompatible type must exactly match the range element type,
1990  * if we were able to identify one. This checks compatibility for
1991  * anycompatiblemultirange too since that also sets
1992  * anycompatible_range_typelem above.
1993  */
1994  if (OidIsValid(anycompatible_range_typelem) &&
1995  anycompatible_range_typelem != anycompatible_typeid)
1996  return false;
1997  }
1998 
1999  /* Looks valid */
2000  return true;
2001 }
#define Assert(condition)
Definition: c.h:812
#define OidIsValid(objectId)
Definition: c.h:729
int j
Definition: isn.c:73
Oid get_range_subtype(Oid rangeOid)
Definition: lsyscache.c:3407
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2759
bool type_is_enum(Oid typid)
Definition: lsyscache.c:2678
Oid get_multirange_range(Oid multirangeOid)
Definition: lsyscache.c:3483
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2521
#define type_is_array_domain(typid)
Definition: lsyscache.h:211
static bool verify_common_type_from_oids(Oid common_type, int nargs, const Oid *typeids)
static Oid select_common_type_from_oids(int nargs, const Oid *typeids, bool noerror)
#define FUNC_MAX_ARGS
#define InvalidOid
Definition: postgres_ext.h:36

References Assert, FUNC_MAX_ARGS, get_element_type(), get_multirange_range(), get_range_subtype(), getBaseType(), InvalidOid, j, OidIsValid, select_common_type_from_oids(), type_is_array_domain, type_is_enum(), and verify_common_type_from_oids().

Referenced by can_coerce_type().

◆ check_valid_internal_signature()

char* check_valid_internal_signature ( Oid  ret_type,
const Oid declared_arg_types,
int  nargs 
)

Definition at line 2917 of file parse_coerce.c.

2920 {
2921  if (ret_type == INTERNALOID)
2922  {
2923  for (int i = 0; i < nargs; i++)
2924  {
2925  if (declared_arg_types[i] == ret_type)
2926  return NULL; /* OK */
2927  }
2928  return pstrdup(_("A result of type internal requires at least one input of type internal."));
2929  }
2930  else
2931  return NULL; /* OK, ret_type is not INTERNAL */
2932 }
#define _(x)
Definition: elog.c:90
char * pstrdup(const char *in)
Definition: mcxt.c:1696

References _, i, and pstrdup().

Referenced by AggregateCreate(), and ProcedureCreate().

◆ check_valid_polymorphic_signature()

char* check_valid_polymorphic_signature ( Oid  ret_type,
const Oid declared_arg_types,
int  nargs 
)

Definition at line 2840 of file parse_coerce.c.

2843 {
2844  if (ret_type == ANYRANGEOID || ret_type == ANYMULTIRANGEOID)
2845  {
2846  /*
2847  * ANYRANGE and ANYMULTIRANGE require an ANYRANGE or ANYMULTIRANGE
2848  * input, else we can't tell which of several range types with the
2849  * same element type to use.
2850  */
2851  for (int i = 0; i < nargs; i++)
2852  {
2853  if (declared_arg_types[i] == ANYRANGEOID ||
2854  declared_arg_types[i] == ANYMULTIRANGEOID)
2855  return NULL; /* OK */
2856  }
2857  return psprintf(_("A result of type %s requires at least one input of type anyrange or anymultirange."),
2858  format_type_be(ret_type));
2859  }
2860  else if (ret_type == ANYCOMPATIBLERANGEOID || ret_type == ANYCOMPATIBLEMULTIRANGEOID)
2861  {
2862  /*
2863  * ANYCOMPATIBLERANGE and ANYCOMPATIBLEMULTIRANGE require an
2864  * ANYCOMPATIBLERANGE or ANYCOMPATIBLEMULTIRANGE input, else we can't
2865  * tell which of several range types with the same element type to
2866  * use.
2867  */
2868  for (int i = 0; i < nargs; i++)
2869  {
2870  if (declared_arg_types[i] == ANYCOMPATIBLERANGEOID ||
2871  declared_arg_types[i] == ANYCOMPATIBLEMULTIRANGEOID)
2872  return NULL; /* OK */
2873  }
2874  return psprintf(_("A result of type %s requires at least one input of type anycompatiblerange or anycompatiblemultirange."),
2875  format_type_be(ret_type));
2876  }
2877  else if (IsPolymorphicTypeFamily1(ret_type))
2878  {
2879  /* Otherwise, any family-1 type can be deduced from any other */
2880  for (int i = 0; i < nargs; i++)
2881  {
2882  if (IsPolymorphicTypeFamily1(declared_arg_types[i]))
2883  return NULL; /* OK */
2884  }
2885  /* Keep this list in sync with IsPolymorphicTypeFamily1! */
2886  return psprintf(_("A result of type %s requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange."),
2887  format_type_be(ret_type));
2888  }
2889  else if (IsPolymorphicTypeFamily2(ret_type))
2890  {
2891  /* Otherwise, any family-2 type can be deduced from any other */
2892  for (int i = 0; i < nargs; i++)
2893  {
2894  if (IsPolymorphicTypeFamily2(declared_arg_types[i]))
2895  return NULL; /* OK */
2896  }
2897  /* Keep this list in sync with IsPolymorphicTypeFamily2! */
2898  return psprintf(_("A result of type %s requires at least one input of type anycompatible, anycompatiblearray, anycompatiblenonarray, anycompatiblerange, or anycompatiblemultirange."),
2899  format_type_be(ret_type));
2900  }
2901  else
2902  return NULL; /* OK, ret_type is not polymorphic */
2903 }
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43

References _, format_type_be(), i, and psprintf().

Referenced by AggregateCreate(), and ProcedureCreate().

◆ coerce_to_boolean()

Node* coerce_to_boolean ( ParseState pstate,
Node node,
const char *  constructName 
)

Definition at line 1161 of file parse_coerce.c.

1163 {
1164  Oid inputTypeId = exprType(node);
1165 
1166  if (inputTypeId != BOOLOID)
1167  {
1168  Node *newnode;
1169 
1170  newnode = coerce_to_target_type(pstate, node, inputTypeId,
1171  BOOLOID, -1,
1174  -1);
1175  if (newnode == NULL)
1176  ereport(ERROR,
1177  (errcode(ERRCODE_DATATYPE_MISMATCH),
1178  /* translator: first %s is name of a SQL construct, eg WHERE */
1179  errmsg("argument of %s must be type %s, not type %s",
1180  constructName, "boolean",
1181  format_type_be(inputTypeId)),
1182  parser_errposition(pstate, exprLocation(node))));
1183  node = newnode;
1184  }
1185 
1186  if (expression_returns_set(node))
1187  ereport(ERROR,
1188  (errcode(ERRCODE_DATATYPE_MISMATCH),
1189  /* translator: %s is name of a SQL construct, eg WHERE */
1190  errmsg("argument of %s must not return a set",
1191  constructName),
1192  parser_errposition(pstate, exprLocation(node))));
1193 
1194  return node;
1195 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1380
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:758
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 parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:736
@ COERCION_ASSIGNMENT
Definition: primnodes.h:715
Definition: nodes.h:129

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 cookConstraint(), DoCopy(), domainAddCheckConstraint(), transformAExprIn(), transformBooleanTest(), transformBoolExpr(), transformCaseExpr(), transformJoinUsingClause(), transformWhereClause(), and transformXmlExpr().

◆ coerce_to_common_type()

Node* coerce_to_common_type ( ParseState pstate,
Node node,
Oid  targetTypeId,
const char *  context 
)

Definition at line 1537 of file parse_coerce.c.

1539 {
1540  Oid inputTypeId = exprType(node);
1541 
1542  if (inputTypeId == targetTypeId)
1543  return node; /* no work */
1544  if (can_coerce_type(1, &inputTypeId, &targetTypeId, COERCION_IMPLICIT))
1545  node = coerce_type(pstate, node, inputTypeId, targetTypeId, -1,
1547  else
1548  ereport(ERROR,
1549  (errcode(ERRCODE_CANNOT_COERCE),
1550  /* translator: first %s is name of a SQL construct, eg CASE */
1551  errmsg("%s could not convert type %s to %s",
1552  context,
1553  format_type_be(inputTypeId),
1554  format_type_be(targetTypeId)),
1555  parser_errposition(pstate, exprLocation(node))));
1556  return node;
1557 }
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:157
bool can_coerce_type(int nargs, const Oid *input_typeids, const Oid *target_typeids, CoercionContext ccontext)
Definition: parse_coerce.c:556
@ COERCION_IMPLICIT
Definition: primnodes.h:714
tree context
Definition: radixtree.h:1835

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

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

◆ coerce_to_domain()

Node* coerce_to_domain ( Node arg,
Oid  baseTypeId,
int32  baseTypeMod,
Oid  typeId,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location,
bool  hideInputCoercion 
)

Definition at line 676 of file parse_coerce.c.

679 {
680  CoerceToDomain *result;
681 
682  /* Get the base type if it hasn't been supplied */
683  if (baseTypeId == InvalidOid)
684  baseTypeId = getBaseTypeAndTypmod(typeId, &baseTypeMod);
685 
686  /* If it isn't a domain, return the node as it was passed in */
687  if (baseTypeId == typeId)
688  return arg;
689 
690  /* Suppress display of nested coercion steps */
691  if (hideInputCoercion)
693 
694  /*
695  * If the domain applies a typmod to its base type, build the appropriate
696  * coercion step. Mark it implicit for display purposes, because we don't
697  * want it shown separately by ruleutils.c; but the isExplicit flag passed
698  * to the conversion function depends on the manner in which the domain
699  * coercion is invoked, so that the semantics of implicit and explicit
700  * coercion differ. (Is that really the behavior we want?)
701  *
702  * NOTE: because we apply this as part of the fixed expression structure,
703  * ALTER DOMAIN cannot alter the typtypmod. But it's unclear that that
704  * would be safe to do anyway, without lots of knowledge about what the
705  * base type thinks the typmod means.
706  */
707  arg = coerce_type_typmod(arg, baseTypeId, baseTypeMod,
708  ccontext, COERCE_IMPLICIT_CAST, location,
709  false);
710 
711  /*
712  * Now build the domain coercion node. This represents run-time checking
713  * of any constraints currently attached to the domain. This also ensures
714  * that the expression is properly labeled as to result type.
715  */
716  result = makeNode(CoerceToDomain);
717  result->arg = (Expr *) arg;
718  result->resulttype = typeId;
719  result->resulttypmod = -1; /* currently, always -1 for domains */
720  /* resultcollid will be set by parse_collate.c */
721  result->coercionformat = cformat;
722  result->location = location;
723 
724  return (Node *) result;
725 }
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2538
#define makeNode(_type_)
Definition: nodes.h:155
static Node * coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
Definition: parse_coerce.c:753
static void hide_coercion_node(Node *node)
Definition: parse_coerce.c:811
void * arg
ParseLoc location
Definition: primnodes.h:2036

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

Referenced by coerce_record_to_complex(), coerce_type(), expand_insert_targetlist(), ReplaceVarsFromTargetList_callback(), rewriteTargetListIU(), rewriteValuesRTE(), and transformAssignmentIndirection().

◆ coerce_to_specific_type()

Node* coerce_to_specific_type ( ParseState pstate,
Node node,
Oid  targetTypeId,
const char *  constructName 
)

Definition at line 1257 of file parse_coerce.c.

1260 {
1261  return coerce_to_specific_type_typmod(pstate, node,
1262  targetTypeId, -1,
1263  constructName);
1264 }
Node * coerce_to_specific_type_typmod(ParseState *pstate, Node *node, Oid targetTypeId, int32 targetTypmod, const char *constructName)

References coerce_to_specific_type_typmod().

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

◆ coerce_to_specific_type_typmod()

Node* coerce_to_specific_type_typmod ( ParseState pstate,
Node node,
Oid  targetTypeId,
int32  targetTypmod,
const char *  constructName 
)

Definition at line 1208 of file parse_coerce.c.

1211 {
1212  Oid inputTypeId = exprType(node);
1213 
1214  if (inputTypeId != targetTypeId)
1215  {
1216  Node *newnode;
1217 
1218  newnode = coerce_to_target_type(pstate, node, inputTypeId,
1219  targetTypeId, targetTypmod,
1222  -1);
1223  if (newnode == NULL)
1224  ereport(ERROR,
1225  (errcode(ERRCODE_DATATYPE_MISMATCH),
1226  /* translator: first %s is name of a SQL construct, eg LIMIT */
1227  errmsg("argument of %s must be type %s, not type %s",
1228  constructName,
1229  format_type_be(targetTypeId),
1230  format_type_be(inputTypeId)),
1231  parser_errposition(pstate, exprLocation(node))));
1232  node = newnode;
1233  }
1234 
1235  if (expression_returns_set(node))
1236  ereport(ERROR,
1237  (errcode(ERRCODE_DATATYPE_MISMATCH),
1238  /* translator: %s is name of a SQL construct, eg LIMIT */
1239  errmsg("argument of %s must not return a set",
1240  constructName),
1241  parser_errposition(pstate, exprLocation(node))));
1242 
1243  return node;
1244 }

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().

◆ coerce_to_target_type()

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.

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  * Also, if the target type isn't collatable, we discard the CollateExpr.
99  */
100  origexpr = expr;
101  while (expr && IsA(expr, CollateExpr))
102  expr = (Node *) ((CollateExpr *) expr)->arg;
103 
104  result = coerce_type(pstate, expr, exprtype,
105  targettype, targettypmod,
106  ccontext, cformat, location);
107 
108  /*
109  * If the target is a fixed-length type, it may need a length coercion as
110  * well as a type coercion. If we find ourselves adding both, force the
111  * inner coercion node to implicit display form.
112  */
113  result = coerce_type_typmod(result,
114  targettype, targettypmod,
115  ccontext, cformat, location,
116  (result != expr && !IsA(result, Const)));
117 
118  if (expr != origexpr && type_is_collatable(targettype))
119  {
120  /* Reinstall top CollateExpr */
121  CollateExpr *coll = (CollateExpr *) origexpr;
122  CollateExpr *newcoll = makeNode(CollateExpr);
123 
124  newcoll->arg = (Expr *) result;
125  newcoll->collOid = coll->collOid;
126  newcoll->location = coll->location;
127  result = (Node *) newcoll;
128  }
129 
130  return result;
131 }
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3081
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
Expr * arg
Definition: primnodes.h:1279
ParseLoc location
Definition: primnodes.h:1281

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

Referenced by array_subscript_transform(), ATExecAddColumn(), ATExecAlterColumnType(), ATPrepAlterColumnType(), build_coercion_expression(), build_column_default(), coerce_fn_result_column(), coerce_record_to_complex(), coerce_to_boolean(), coerce_to_specific_type_typmod(), coerceJsonFuncExpr(), cookDefault(), EvaluateParams(), get_cast_hashentry(), hstore_subscript_transform(), transformArrayExpr(), transformAssignedExpr(), transformAssignmentIndirection(), transformAssignmentSubscripts(), transformJsonBehavior(), transformJsonFuncExpr(), transformJsonParseArg(), transformJsonValueExpr(), transformPartitionBoundValue(), transformPLAssignStmt(), transformTypeCast(), and transformXmlSerialize().

◆ coerce_type()

Node* coerce_type ( ParseState pstate,
Node node,
Oid  inputTypeId,
Oid  targetTypeId,
int32  targetTypeMod,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location 
)

Definition at line 157 of file parse_coerce.c.

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

References ConvertRowtypeExpr::arg, CollateExpr::arg, build_coercion_expression(), cancel_parser_errposition_callback(), COERCE_IMPLICIT_CAST, coerce_record_to_complex(), coerce_to_domain(), COERCION_PATH_NONE, COERCION_PATH_RELABELTYPE, CollateExpr::collOid, Const::consttype, DatumGetCString(), datumIsEqual(), elog, ERROR, find_coercion_pathway(), format_type_be(), getBaseType(), getBaseTypeAndTypmod(), InvalidOid, is_complex_array(), IsA, ISCOMPLEX, RelabelType::location, ConvertRowtypeExpr::location, CollateExpr::location, makeNode, makeRelabelType(), ParseState::p_coerce_param_hook, PG_DETOAST_DATUM, PointerGetDatum(), ReleaseSysCache(), ConvertRowtypeExpr::resulttype, setup_parser_errposition_callback(), stringTypeDatum(), type_is_collatable(), typeByVal(), typeidType(), typeInheritsFrom(), typeIsOfTypedTable(), typeLen(), typeTypeCollation(), typeTypeName(), and WARNING.

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

◆ enforce_generic_type_consistency()

Oid enforce_generic_type_consistency ( const Oid actual_arg_types,
Oid declared_arg_types,
int  nargs,
Oid  rettype,
bool  allow_poly 
)

Definition at line 2096 of file parse_coerce.c.

2101 {
2102  bool have_poly_anycompatible = false;
2103  bool have_poly_unknowns = false;
2104  Oid elem_typeid = InvalidOid;
2105  Oid array_typeid = InvalidOid;
2106  Oid range_typeid = InvalidOid;
2107  Oid multirange_typeid = InvalidOid;
2108  Oid anycompatible_typeid = InvalidOid;
2109  Oid anycompatible_array_typeid = InvalidOid;
2110  Oid anycompatible_range_typeid = InvalidOid;
2111  Oid anycompatible_range_typelem = InvalidOid;
2112  Oid anycompatible_multirange_typeid = InvalidOid;
2113  Oid anycompatible_multirange_typelem = InvalidOid;
2114  bool have_anynonarray = (rettype == ANYNONARRAYOID);
2115  bool have_anyenum = (rettype == ANYENUMOID);
2116  bool have_anymultirange = (rettype == ANYMULTIRANGEOID);
2117  bool have_anycompatible_nonarray = (rettype == ANYCOMPATIBLENONARRAYOID);
2118  bool have_anycompatible_array = (rettype == ANYCOMPATIBLEARRAYOID);
2119  bool have_anycompatible_range = (rettype == ANYCOMPATIBLERANGEOID);
2120  bool have_anycompatible_multirange = (rettype == ANYCOMPATIBLEMULTIRANGEOID);
2121  int n_poly_args = 0; /* this counts all family-1 arguments */
2122  int n_anycompatible_args = 0; /* this counts only non-unknowns */
2123  Oid anycompatible_actual_types[FUNC_MAX_ARGS];
2124 
2125  /*
2126  * Loop through the arguments to see if we have any that are polymorphic.
2127  * If so, require the actual types to be consistent.
2128  */
2129  Assert(nargs <= FUNC_MAX_ARGS);
2130  for (int j = 0; j < nargs; j++)
2131  {
2132  Oid decl_type = declared_arg_types[j];
2133  Oid actual_type = actual_arg_types[j];
2134 
2135  if (decl_type == ANYELEMENTOID ||
2136  decl_type == ANYNONARRAYOID ||
2137  decl_type == ANYENUMOID)
2138  {
2139  n_poly_args++;
2140  if (decl_type == ANYNONARRAYOID)
2141  have_anynonarray = true;
2142  else if (decl_type == ANYENUMOID)
2143  have_anyenum = true;
2144  if (actual_type == UNKNOWNOID)
2145  {
2146  have_poly_unknowns = true;
2147  continue;
2148  }
2149  if (allow_poly && decl_type == actual_type)
2150  continue; /* no new information here */
2151  if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
2152  ereport(ERROR,
2153  (errcode(ERRCODE_DATATYPE_MISMATCH),
2154  errmsg("arguments declared \"%s\" are not all alike", "anyelement"),
2155  errdetail("%s versus %s",
2156  format_type_be(elem_typeid),
2157  format_type_be(actual_type))));
2158  elem_typeid = actual_type;
2159  }
2160  else if (decl_type == ANYARRAYOID)
2161  {
2162  n_poly_args++;
2163  if (actual_type == UNKNOWNOID)
2164  {
2165  have_poly_unknowns = true;
2166  continue;
2167  }
2168  if (allow_poly && decl_type == actual_type)
2169  continue; /* no new information here */
2170  actual_type = getBaseType(actual_type); /* flatten domains */
2171  if (OidIsValid(array_typeid) && actual_type != array_typeid)
2172  ereport(ERROR,
2173  (errcode(ERRCODE_DATATYPE_MISMATCH),
2174  errmsg("arguments declared \"%s\" are not all alike", "anyarray"),
2175  errdetail("%s versus %s",
2176  format_type_be(array_typeid),
2177  format_type_be(actual_type))));
2178  array_typeid = actual_type;
2179  }
2180  else if (decl_type == ANYRANGEOID)
2181  {
2182  n_poly_args++;
2183  if (actual_type == UNKNOWNOID)
2184  {
2185  have_poly_unknowns = true;
2186  continue;
2187  }
2188  if (allow_poly && decl_type == actual_type)
2189  continue; /* no new information here */
2190  actual_type = getBaseType(actual_type); /* flatten domains */
2191  if (OidIsValid(range_typeid) && actual_type != range_typeid)
2192  ereport(ERROR,
2193  (errcode(ERRCODE_DATATYPE_MISMATCH),
2194  errmsg("arguments declared \"%s\" are not all alike", "anyrange"),
2195  errdetail("%s versus %s",
2196  format_type_be(range_typeid),
2197  format_type_be(actual_type))));
2198  range_typeid = actual_type;
2199  }
2200  else if (decl_type == ANYMULTIRANGEOID)
2201  {
2202  n_poly_args++;
2203  have_anymultirange = true;
2204  if (actual_type == UNKNOWNOID)
2205  {
2206  have_poly_unknowns = true;
2207  continue;
2208  }
2209  if (allow_poly && decl_type == actual_type)
2210  continue; /* no new information here */
2211  actual_type = getBaseType(actual_type); /* flatten domains */
2212  if (OidIsValid(multirange_typeid) && actual_type != multirange_typeid)
2213  ereport(ERROR,
2214  (errcode(ERRCODE_DATATYPE_MISMATCH),
2215  errmsg("arguments declared \"%s\" are not all alike", "anymultirange"),
2216  errdetail("%s versus %s",
2217  format_type_be(multirange_typeid),
2218  format_type_be(actual_type))));
2219  multirange_typeid = actual_type;
2220  }
2221  else if (decl_type == ANYCOMPATIBLEOID ||
2222  decl_type == ANYCOMPATIBLENONARRAYOID)
2223  {
2224  have_poly_anycompatible = true;
2225  if (decl_type == ANYCOMPATIBLENONARRAYOID)
2226  have_anycompatible_nonarray = true;
2227  if (actual_type == UNKNOWNOID)
2228  continue;
2229  if (allow_poly && decl_type == actual_type)
2230  continue; /* no new information here */
2231  /* collect the actual types of non-unknown COMPATIBLE args */
2232  anycompatible_actual_types[n_anycompatible_args++] = actual_type;
2233  }
2234  else if (decl_type == ANYCOMPATIBLEARRAYOID)
2235  {
2236  Oid anycompatible_elem_type;
2237 
2238  have_poly_anycompatible = true;
2239  have_anycompatible_array = true;
2240  if (actual_type == UNKNOWNOID)
2241  continue;
2242  if (allow_poly && decl_type == actual_type)
2243  continue; /* no new information here */
2244  actual_type = getBaseType(actual_type); /* flatten domains */
2245  anycompatible_elem_type = get_element_type(actual_type);
2246  if (!OidIsValid(anycompatible_elem_type))
2247  ereport(ERROR,
2248  (errcode(ERRCODE_DATATYPE_MISMATCH),
2249  errmsg("argument declared %s is not an array but type %s",
2250  "anycompatiblearray",
2251  format_type_be(actual_type))));
2252  /* collect the element type for common-supertype choice */
2253  anycompatible_actual_types[n_anycompatible_args++] = anycompatible_elem_type;
2254  }
2255  else if (decl_type == ANYCOMPATIBLERANGEOID)
2256  {
2257  have_poly_anycompatible = true;
2258  have_anycompatible_range = true;
2259  if (actual_type == UNKNOWNOID)
2260  continue;
2261  if (allow_poly && decl_type == actual_type)
2262  continue; /* no new information here */
2263  actual_type = getBaseType(actual_type); /* flatten domains */
2264  if (OidIsValid(anycompatible_range_typeid))
2265  {
2266  /* All ANYCOMPATIBLERANGE arguments must be the same type */
2267  if (anycompatible_range_typeid != actual_type)
2268  ereport(ERROR,
2269  (errcode(ERRCODE_DATATYPE_MISMATCH),
2270  errmsg("arguments declared \"%s\" are not all alike", "anycompatiblerange"),
2271  errdetail("%s versus %s",
2272  format_type_be(anycompatible_range_typeid),
2273  format_type_be(actual_type))));
2274  }
2275  else
2276  {
2277  anycompatible_range_typeid = actual_type;
2278  anycompatible_range_typelem = get_range_subtype(actual_type);
2279  if (!OidIsValid(anycompatible_range_typelem))
2280  ereport(ERROR,
2281  (errcode(ERRCODE_DATATYPE_MISMATCH),
2282  errmsg("argument declared %s is not a range type but type %s",
2283  "anycompatiblerange",
2284  format_type_be(actual_type))));
2285  /* collect the subtype for common-supertype choice */
2286  anycompatible_actual_types[n_anycompatible_args++] = anycompatible_range_typelem;
2287  }
2288  }
2289  else if (decl_type == ANYCOMPATIBLEMULTIRANGEOID)
2290  {
2291  have_poly_anycompatible = true;
2292  have_anycompatible_multirange = true;
2293  if (actual_type == UNKNOWNOID)
2294  continue;
2295  if (allow_poly && decl_type == actual_type)
2296  continue; /* no new information here */
2297  actual_type = getBaseType(actual_type); /* flatten domains */
2298  if (OidIsValid(anycompatible_multirange_typeid))
2299  {
2300  /* All ANYCOMPATIBLEMULTIRANGE arguments must be the same type */
2301  if (anycompatible_multirange_typeid != actual_type)
2302  ereport(ERROR,
2303  (errcode(ERRCODE_DATATYPE_MISMATCH),
2304  errmsg("arguments declared \"%s\" are not all alike", "anycompatiblemultirange"),
2305  errdetail("%s versus %s",
2306  format_type_be(anycompatible_multirange_typeid),
2307  format_type_be(actual_type))));
2308  }
2309  else
2310  {
2311  anycompatible_multirange_typeid = actual_type;
2312  anycompatible_multirange_typelem = get_multirange_range(actual_type);
2313  if (!OidIsValid(anycompatible_multirange_typelem))
2314  ereport(ERROR,
2315  (errcode(ERRCODE_DATATYPE_MISMATCH),
2316  errmsg("argument declared %s is not a multirange type but type %s",
2317  "anycompatiblemultirange",
2318  format_type_be(actual_type))));
2319  /* we'll consider the subtype below */
2320  }
2321  }
2322  }
2323 
2324  /*
2325  * Fast Track: if none of the arguments are polymorphic, return the
2326  * unmodified rettype. Not our job to resolve it if it's polymorphic.
2327  */
2328  if (n_poly_args == 0 && !have_poly_anycompatible)
2329  return rettype;
2330 
2331  /* Check matching of family-1 polymorphic arguments, if any */
2332  if (n_poly_args)
2333  {
2334  /* Get the element type based on the array type, if we have one */
2335  if (OidIsValid(array_typeid))
2336  {
2337  Oid array_typelem;
2338 
2339  if (array_typeid == ANYARRAYOID)
2340  {
2341  /*
2342  * Special case for matching ANYARRAY input to an ANYARRAY
2343  * argument: allow it iff no other arguments are family-1
2344  * polymorphics (otherwise we couldn't be sure whether the
2345  * array element type matches up) and the result type doesn't
2346  * require us to infer a specific element type.
2347  */
2348  if (n_poly_args != 1 ||
2349  (rettype != ANYARRAYOID &&
2350  IsPolymorphicTypeFamily1(rettype)))
2351  ereport(ERROR,
2352  (errcode(ERRCODE_DATATYPE_MISMATCH),
2353  errmsg("cannot determine element type of \"anyarray\" argument")));
2354  array_typelem = ANYELEMENTOID;
2355  }
2356  else
2357  {
2358  array_typelem = get_element_type(array_typeid);
2359  if (!OidIsValid(array_typelem))
2360  ereport(ERROR,
2361  (errcode(ERRCODE_DATATYPE_MISMATCH),
2362  errmsg("argument declared %s is not an array but type %s",
2363  "anyarray", format_type_be(array_typeid))));
2364  }
2365 
2366  if (!OidIsValid(elem_typeid))
2367  {
2368  /*
2369  * if we don't have an element type yet, use the one we just
2370  * got
2371  */
2372  elem_typeid = array_typelem;
2373  }
2374  else if (array_typelem != elem_typeid)
2375  {
2376  /* otherwise, they better match */
2377  ereport(ERROR,
2378  (errcode(ERRCODE_DATATYPE_MISMATCH),
2379  errmsg("argument declared %s is not consistent with argument declared %s",
2380  "anyarray", "anyelement"),
2381  errdetail("%s versus %s",
2382  format_type_be(array_typeid),
2383  format_type_be(elem_typeid))));
2384  }
2385  }
2386 
2387  /* Deduce range type from multirange type, or vice versa */
2388  if (OidIsValid(multirange_typeid))
2389  {
2390  Oid multirange_typelem;
2391 
2392  multirange_typelem = get_multirange_range(multirange_typeid);
2393  if (!OidIsValid(multirange_typelem))
2394  ereport(ERROR,
2395  (errcode(ERRCODE_DATATYPE_MISMATCH),
2396  errmsg("argument declared %s is not a multirange type but type %s",
2397  "anymultirange",
2398  format_type_be(multirange_typeid))));
2399 
2400  if (!OidIsValid(range_typeid))
2401  {
2402  /* if we don't have a range type yet, use the one we just got */
2403  range_typeid = multirange_typelem;
2404  }
2405  else if (multirange_typelem != range_typeid)
2406  {
2407  /* otherwise, they better match */
2408  ereport(ERROR,
2409  (errcode(ERRCODE_DATATYPE_MISMATCH),
2410  errmsg("argument declared %s is not consistent with argument declared %s",
2411  "anymultirange", "anyrange"),
2412  errdetail("%s versus %s",
2413  format_type_be(multirange_typeid),
2414  format_type_be(range_typeid))));
2415  }
2416  }
2417  else if (have_anymultirange && OidIsValid(range_typeid))
2418  {
2419  multirange_typeid = get_range_multirange(range_typeid);
2420  /* We'll complain below if that didn't work */
2421  }
2422 
2423  /* Get the element type based on the range type, if we have one */
2424  if (OidIsValid(range_typeid))
2425  {
2426  Oid range_typelem;
2427 
2428  range_typelem = get_range_subtype(range_typeid);
2429  if (!OidIsValid(range_typelem))
2430  ereport(ERROR,
2431  (errcode(ERRCODE_DATATYPE_MISMATCH),
2432  errmsg("argument declared %s is not a range type but type %s",
2433  "anyrange",
2434  format_type_be(range_typeid))));
2435 
2436  if (!OidIsValid(elem_typeid))
2437  {
2438  /*
2439  * if we don't have an element type yet, use the one we just
2440  * got
2441  */
2442  elem_typeid = range_typelem;
2443  }
2444  else if (range_typelem != elem_typeid)
2445  {
2446  /* otherwise, they better match */
2447  ereport(ERROR,
2448  (errcode(ERRCODE_DATATYPE_MISMATCH),
2449  errmsg("argument declared %s is not consistent with argument declared %s",
2450  "anyrange", "anyelement"),
2451  errdetail("%s versus %s",
2452  format_type_be(range_typeid),
2453  format_type_be(elem_typeid))));
2454  }
2455  }
2456 
2457  if (!OidIsValid(elem_typeid))
2458  {
2459  if (allow_poly)
2460  {
2461  elem_typeid = ANYELEMENTOID;
2462  array_typeid = ANYARRAYOID;
2463  range_typeid = ANYRANGEOID;
2464  multirange_typeid = ANYMULTIRANGEOID;
2465  }
2466  else
2467  {
2468  /*
2469  * Only way to get here is if all the family-1 polymorphic
2470  * arguments have UNKNOWN inputs.
2471  */
2472  ereport(ERROR,
2473  (errcode(ERRCODE_DATATYPE_MISMATCH),
2474  errmsg("could not determine polymorphic type because input has type %s",
2475  "unknown")));
2476  }
2477  }
2478 
2479  if (have_anynonarray && elem_typeid != ANYELEMENTOID)
2480  {
2481  /*
2482  * require the element type to not be an array or domain over
2483  * array
2484  */
2485  if (type_is_array_domain(elem_typeid))
2486  ereport(ERROR,
2487  (errcode(ERRCODE_DATATYPE_MISMATCH),
2488  errmsg("type matched to anynonarray is an array type: %s",
2489  format_type_be(elem_typeid))));
2490  }
2491 
2492  if (have_anyenum && elem_typeid != ANYELEMENTOID)
2493  {
2494  /* require the element type to be an enum */
2495  if (!type_is_enum(elem_typeid))
2496  ereport(ERROR,
2497  (errcode(ERRCODE_DATATYPE_MISMATCH),
2498  errmsg("type matched to anyenum is not an enum type: %s",
2499  format_type_be(elem_typeid))));
2500  }
2501  }
2502 
2503  /* Check matching of family-2 polymorphic arguments, if any */
2504  if (have_poly_anycompatible)
2505  {
2506  /* Deduce range type from multirange type, or vice versa */
2507  if (OidIsValid(anycompatible_multirange_typeid))
2508  {
2509  if (OidIsValid(anycompatible_range_typeid))
2510  {
2511  if (anycompatible_multirange_typelem !=
2512  anycompatible_range_typeid)
2513  ereport(ERROR,
2514  (errcode(ERRCODE_DATATYPE_MISMATCH),
2515  errmsg("argument declared %s is not consistent with argument declared %s",
2516  "anycompatiblemultirange",
2517  "anycompatiblerange"),
2518  errdetail("%s versus %s",
2519  format_type_be(anycompatible_multirange_typeid),
2520  format_type_be(anycompatible_range_typeid))));
2521  }
2522  else
2523  {
2524  anycompatible_range_typeid = anycompatible_multirange_typelem;
2525  anycompatible_range_typelem = get_range_subtype(anycompatible_range_typeid);
2526  if (!OidIsValid(anycompatible_range_typelem))
2527  ereport(ERROR,
2528  (errcode(ERRCODE_DATATYPE_MISMATCH),
2529  errmsg("argument declared %s is not a multirange type but type %s",
2530  "anycompatiblemultirange",
2531  format_type_be(anycompatible_multirange_typeid))));
2532  /* this enables element type matching check below */
2533  have_anycompatible_range = true;
2534  /* collect the subtype for common-supertype choice */
2535  anycompatible_actual_types[n_anycompatible_args++] =
2536  anycompatible_range_typelem;
2537  }
2538  }
2539  else if (have_anycompatible_multirange &&
2540  OidIsValid(anycompatible_range_typeid))
2541  {
2542  anycompatible_multirange_typeid = get_range_multirange(anycompatible_range_typeid);
2543  /* We'll complain below if that didn't work */
2544  }
2545 
2546  if (n_anycompatible_args > 0)
2547  {
2548  anycompatible_typeid =
2549  select_common_type_from_oids(n_anycompatible_args,
2550  anycompatible_actual_types,
2551  false);
2552 
2553  /* We have to verify that the selected type actually works */
2554  if (!verify_common_type_from_oids(anycompatible_typeid,
2555  n_anycompatible_args,
2556  anycompatible_actual_types))
2557  ereport(ERROR,
2558  (errcode(ERRCODE_DATATYPE_MISMATCH),
2559  errmsg("arguments of anycompatible family cannot be cast to a common type")));
2560 
2561  if (have_anycompatible_array)
2562  {
2563  anycompatible_array_typeid = get_array_type(anycompatible_typeid);
2564  if (!OidIsValid(anycompatible_array_typeid))
2565  ereport(ERROR,
2566  (errcode(ERRCODE_UNDEFINED_OBJECT),
2567  errmsg("could not find array type for data type %s",
2568  format_type_be(anycompatible_typeid))));
2569  }
2570 
2571  if (have_anycompatible_range)
2572  {
2573  /* we can't infer a range type from the others */
2574  if (!OidIsValid(anycompatible_range_typeid))
2575  ereport(ERROR,
2576  (errcode(ERRCODE_DATATYPE_MISMATCH),
2577  errmsg("could not determine polymorphic type %s because input has type %s",
2578  "anycompatiblerange", "unknown")));
2579 
2580  /*
2581  * the anycompatible type must exactly match the range element
2582  * type
2583  */
2584  if (anycompatible_range_typelem != anycompatible_typeid)
2585  ereport(ERROR,
2586  (errcode(ERRCODE_DATATYPE_MISMATCH),
2587  errmsg("anycompatiblerange type %s does not match anycompatible type %s",
2588  format_type_be(anycompatible_range_typeid),
2589  format_type_be(anycompatible_typeid))));
2590  }
2591 
2592  if (have_anycompatible_multirange)
2593  {
2594  /* we can't infer a multirange type from the others */
2595  if (!OidIsValid(anycompatible_multirange_typeid))
2596  ereport(ERROR,
2597  (errcode(ERRCODE_DATATYPE_MISMATCH),
2598  errmsg("could not determine polymorphic type %s because input has type %s",
2599  "anycompatiblemultirange", "unknown")));
2600 
2601  /*
2602  * the anycompatible type must exactly match the multirange
2603  * element type
2604  */
2605  if (anycompatible_range_typelem != anycompatible_typeid)
2606  ereport(ERROR,
2607  (errcode(ERRCODE_DATATYPE_MISMATCH),
2608  errmsg("anycompatiblemultirange type %s does not match anycompatible type %s",
2609  format_type_be(anycompatible_multirange_typeid),
2610  format_type_be(anycompatible_typeid))));
2611  }
2612 
2613  if (have_anycompatible_nonarray)
2614  {
2615  /*
2616  * require the element type to not be an array or domain over
2617  * array
2618  */
2619  if (type_is_array_domain(anycompatible_typeid))
2620  ereport(ERROR,
2621  (errcode(ERRCODE_DATATYPE_MISMATCH),
2622  errmsg("type matched to anycompatiblenonarray is an array type: %s",
2623  format_type_be(anycompatible_typeid))));
2624  }
2625  }
2626  else
2627  {
2628  if (allow_poly)
2629  {
2630  anycompatible_typeid = ANYCOMPATIBLEOID;
2631  anycompatible_array_typeid = ANYCOMPATIBLEARRAYOID;
2632  anycompatible_range_typeid = ANYCOMPATIBLERANGEOID;
2633  anycompatible_multirange_typeid = ANYCOMPATIBLEMULTIRANGEOID;
2634  }
2635  else
2636  {
2637  /*
2638  * Only way to get here is if all the family-2 polymorphic
2639  * arguments have UNKNOWN inputs. Resolve to TEXT as
2640  * select_common_type() would do. That doesn't license us to
2641  * use TEXTRANGE or TEXTMULTIRANGE, though.
2642  */
2643  anycompatible_typeid = TEXTOID;
2644  anycompatible_array_typeid = TEXTARRAYOID;
2645  if (have_anycompatible_range)
2646  ereport(ERROR,
2647  (errcode(ERRCODE_DATATYPE_MISMATCH),
2648  errmsg("could not determine polymorphic type %s because input has type %s",
2649  "anycompatiblerange", "unknown")));
2650  if (have_anycompatible_multirange)
2651  ereport(ERROR,
2652  (errcode(ERRCODE_DATATYPE_MISMATCH),
2653  errmsg("could not determine polymorphic type %s because input has type %s",
2654  "anycompatiblemultirange", "unknown")));
2655  }
2656  }
2657 
2658  /* replace family-2 polymorphic types by selected types */
2659  for (int j = 0; j < nargs; j++)
2660  {
2661  Oid decl_type = declared_arg_types[j];
2662 
2663  if (decl_type == ANYCOMPATIBLEOID ||
2664  decl_type == ANYCOMPATIBLENONARRAYOID)
2665  declared_arg_types[j] = anycompatible_typeid;
2666  else if (decl_type == ANYCOMPATIBLEARRAYOID)
2667  declared_arg_types[j] = anycompatible_array_typeid;
2668  else if (decl_type == ANYCOMPATIBLERANGEOID)
2669  declared_arg_types[j] = anycompatible_range_typeid;
2670  else if (decl_type == ANYCOMPATIBLEMULTIRANGEOID)
2671  declared_arg_types[j] = anycompatible_multirange_typeid;
2672  }
2673  }
2674 
2675  /*
2676  * If we had any UNKNOWN inputs for family-1 polymorphic arguments,
2677  * re-scan to assign correct types to them.
2678  *
2679  * Note: we don't have to consider unknown inputs that were matched to
2680  * family-2 polymorphic arguments, because we forcibly updated their
2681  * declared_arg_types[] positions just above.
2682  */
2683  if (have_poly_unknowns)
2684  {
2685  for (int j = 0; j < nargs; j++)
2686  {
2687  Oid decl_type = declared_arg_types[j];
2688  Oid actual_type = actual_arg_types[j];
2689 
2690  if (actual_type != UNKNOWNOID)
2691  continue;
2692 
2693  if (decl_type == ANYELEMENTOID ||
2694  decl_type == ANYNONARRAYOID ||
2695  decl_type == ANYENUMOID)
2696  declared_arg_types[j] = elem_typeid;
2697  else if (decl_type == ANYARRAYOID)
2698  {
2699  if (!OidIsValid(array_typeid))
2700  {
2701  array_typeid = get_array_type(elem_typeid);
2702  if (!OidIsValid(array_typeid))
2703  ereport(ERROR,
2704  (errcode(ERRCODE_UNDEFINED_OBJECT),
2705  errmsg("could not find array type for data type %s",
2706  format_type_be(elem_typeid))));
2707  }
2708  declared_arg_types[j] = array_typeid;
2709  }
2710  else if (decl_type == ANYRANGEOID)
2711  {
2712  if (!OidIsValid(range_typeid))
2713  {
2714  /* we can't infer a range type from the others */
2715  ereport(ERROR,
2716  (errcode(ERRCODE_DATATYPE_MISMATCH),
2717  errmsg("could not determine polymorphic type %s because input has type %s",
2718  "anyrange", "unknown")));
2719  }
2720  declared_arg_types[j] = range_typeid;
2721  }
2722  else if (decl_type == ANYMULTIRANGEOID)
2723  {
2724  if (!OidIsValid(multirange_typeid))
2725  {
2726  /* we can't infer a multirange type from the others */
2727  ereport(ERROR,
2728  (errcode(ERRCODE_DATATYPE_MISMATCH),
2729  errmsg("could not determine polymorphic type %s because input has type %s",
2730  "anymultirange", "unknown")));
2731  }
2732  declared_arg_types[j] = multirange_typeid;
2733  }
2734  }
2735  }
2736 
2737  /* if we return ANYELEMENT use the appropriate argument type */
2738  if (rettype == ANYELEMENTOID ||
2739  rettype == ANYNONARRAYOID ||
2740  rettype == ANYENUMOID)
2741  return elem_typeid;
2742 
2743  /* if we return ANYARRAY use the appropriate argument type */
2744  if (rettype == ANYARRAYOID)
2745  {
2746  if (!OidIsValid(array_typeid))
2747  {
2748  array_typeid = get_array_type(elem_typeid);
2749  if (!OidIsValid(array_typeid))
2750  ereport(ERROR,
2751  (errcode(ERRCODE_UNDEFINED_OBJECT),
2752  errmsg("could not find array type for data type %s",
2753  format_type_be(elem_typeid))));
2754  }
2755  return array_typeid;
2756  }
2757 
2758  /* if we return ANYRANGE use the appropriate argument type */
2759  if (rettype == ANYRANGEOID)
2760  {
2761  /* this error is unreachable if the function signature is valid: */
2762  if (!OidIsValid(range_typeid))
2763  ereport(ERROR,
2764  (errcode(ERRCODE_DATATYPE_MISMATCH),
2765  errmsg_internal("could not determine polymorphic type %s because input has type %s",
2766  "anyrange", "unknown")));
2767  return range_typeid;
2768  }
2769 
2770  /* if we return ANYMULTIRANGE use the appropriate argument type */
2771  if (rettype == ANYMULTIRANGEOID)
2772  {
2773  /* this error is unreachable if the function signature is valid: */
2774  if (!OidIsValid(multirange_typeid))
2775  ereport(ERROR,
2776  (errcode(ERRCODE_DATATYPE_MISMATCH),
2777  errmsg_internal("could not determine polymorphic type %s because input has type %s",
2778  "anymultirange", "unknown")));
2779  return multirange_typeid;
2780  }
2781 
2782  /* if we return ANYCOMPATIBLE use the appropriate type */
2783  if (rettype == ANYCOMPATIBLEOID ||
2784  rettype == ANYCOMPATIBLENONARRAYOID)
2785  {
2786  /* this error is unreachable if the function signature is valid: */
2787  if (!OidIsValid(anycompatible_typeid))
2788  ereport(ERROR,
2789  (errcode(ERRCODE_DATATYPE_MISMATCH),
2790  errmsg_internal("could not identify anycompatible type")));
2791  return anycompatible_typeid;
2792  }
2793 
2794  /* if we return ANYCOMPATIBLEARRAY use the appropriate type */
2795  if (rettype == ANYCOMPATIBLEARRAYOID)
2796  {
2797  /* this error is unreachable if the function signature is valid: */
2798  if (!OidIsValid(anycompatible_array_typeid))
2799  ereport(ERROR,
2800  (errcode(ERRCODE_DATATYPE_MISMATCH),
2801  errmsg_internal("could not identify anycompatiblearray type")));
2802  return anycompatible_array_typeid;
2803  }
2804 
2805  /* if we return ANYCOMPATIBLERANGE use the appropriate argument type */
2806  if (rettype == ANYCOMPATIBLERANGEOID)
2807  {
2808  /* this error is unreachable if the function signature is valid: */
2809  if (!OidIsValid(anycompatible_range_typeid))
2810  ereport(ERROR,
2811  (errcode(ERRCODE_DATATYPE_MISMATCH),
2812  errmsg_internal("could not identify anycompatiblerange type")));
2813  return anycompatible_range_typeid;
2814  }
2815 
2816  /* if we return ANYCOMPATIBLEMULTIRANGE use the appropriate argument type */
2817  if (rettype == ANYCOMPATIBLEMULTIRANGEOID)
2818  {
2819  /* this error is unreachable if the function signature is valid: */
2820  if (!OidIsValid(anycompatible_multirange_typeid))
2821  ereport(ERROR,
2822  (errcode(ERRCODE_DATATYPE_MISMATCH),
2823  errmsg_internal("could not identify anycompatiblemultirange type")));
2824  return anycompatible_multirange_typeid;
2825  }
2826 
2827  /* we don't return a generic type; send back the original return type */
2828  return rettype;
2829 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errdetail(const char *fmt,...)
Definition: elog.c:1203
Oid get_range_multirange(Oid rangeOid)
Definition: lsyscache.c:3458
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2787

References Assert, ereport, errcode(), errdetail(), errmsg(), errmsg_internal(), ERROR, format_type_be(), FUNC_MAX_ARGS, get_array_type(), get_element_type(), get_multirange_range(), get_range_multirange(), get_range_subtype(), getBaseType(), InvalidOid, j, OidIsValid, select_common_type_from_oids(), type_is_array_domain, type_is_enum(), and verify_common_type_from_oids().

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

◆ find_coercion_pathway()

CoercionPathType find_coercion_pathway ( Oid  targetTypeId,
Oid  sourceTypeId,
CoercionContext  ccontext,
Oid funcid 
)

Definition at line 3118 of file parse_coerce.c.

3121 {
3123  HeapTuple tuple;
3124 
3125  *funcid = InvalidOid;
3126 
3127  /* Perhaps the types are domains; if so, look at their base types */
3128  if (OidIsValid(sourceTypeId))
3129  sourceTypeId = getBaseType(sourceTypeId);
3130  if (OidIsValid(targetTypeId))
3131  targetTypeId = getBaseType(targetTypeId);
3132 
3133  /* Domains are always coercible to and from their base type */
3134  if (sourceTypeId == targetTypeId)
3136 
3137  /* Look in pg_cast */
3138  tuple = SearchSysCache2(CASTSOURCETARGET,
3139  ObjectIdGetDatum(sourceTypeId),
3140  ObjectIdGetDatum(targetTypeId));
3141 
3142  if (HeapTupleIsValid(tuple))
3143  {
3144  Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
3145  CoercionContext castcontext;
3146 
3147  /* convert char value for castcontext to CoercionContext enum */
3148  switch (castForm->castcontext)
3149  {
3150  case COERCION_CODE_IMPLICIT:
3151  castcontext = COERCION_IMPLICIT;
3152  break;
3153  case COERCION_CODE_ASSIGNMENT:
3154  castcontext = COERCION_ASSIGNMENT;
3155  break;
3156  case COERCION_CODE_EXPLICIT:
3157  castcontext = COERCION_EXPLICIT;
3158  break;
3159  default:
3160  elog(ERROR, "unrecognized castcontext: %d",
3161  (int) castForm->castcontext);
3162  castcontext = 0; /* keep compiler quiet */
3163  break;
3164  }
3165 
3166  /* Rely on ordering of enum for correct behavior here */
3167  if (ccontext >= castcontext)
3168  {
3169  switch (castForm->castmethod)
3170  {
3171  case COERCION_METHOD_FUNCTION:
3172  result = COERCION_PATH_FUNC;
3173  *funcid = castForm->castfunc;
3174  break;
3175  case COERCION_METHOD_INOUT:
3176  result = COERCION_PATH_COERCEVIAIO;
3177  break;
3178  case COERCION_METHOD_BINARY:
3179  result = COERCION_PATH_RELABELTYPE;
3180  break;
3181  default:
3182  elog(ERROR, "unrecognized castmethod: %d",
3183  (int) castForm->castmethod);
3184  break;
3185  }
3186  }
3187 
3188  ReleaseSysCache(tuple);
3189  }
3190  else
3191  {
3192  /*
3193  * If there's no pg_cast entry, perhaps we are dealing with a pair of
3194  * array types. If so, and if their element types have a conversion
3195  * pathway, report that we can coerce with an ArrayCoerceExpr.
3196  *
3197  * Hack: disallow coercions to oidvector and int2vector, which
3198  * otherwise tend to capture coercions that should go to "real" array
3199  * types. We want those types to be considered "real" arrays for many
3200  * purposes, but not this one. (Also, ArrayCoerceExpr isn't
3201  * guaranteed to produce an output that meets the restrictions of
3202  * these datatypes, such as being 1-dimensional.)
3203  */
3204  if (targetTypeId != OIDVECTOROID && targetTypeId != INT2VECTOROID)
3205  {
3206  Oid targetElem;
3207  Oid sourceElem;
3208 
3209  if ((targetElem = get_element_type(targetTypeId)) != InvalidOid &&
3210  (sourceElem = get_element_type(sourceTypeId)) != InvalidOid)
3211  {
3212  CoercionPathType elempathtype;
3213  Oid elemfuncid;
3214 
3215  elempathtype = find_coercion_pathway(targetElem,
3216  sourceElem,
3217  ccontext,
3218  &elemfuncid);
3219  if (elempathtype != COERCION_PATH_NONE)
3220  {
3221  result = COERCION_PATH_ARRAYCOERCE;
3222  }
3223  }
3224  }
3225 
3226  /*
3227  * If we still haven't found a possibility, consider automatic casting
3228  * using I/O functions. We allow assignment casts to string types and
3229  * explicit casts from string types to be handled this way. (The
3230  * CoerceViaIO mechanism is a lot more general than that, but this is
3231  * all we want to allow in the absence of a pg_cast entry.) It would
3232  * probably be better to insist on explicit casts in both directions,
3233  * but this is a compromise to preserve something of the pre-8.3
3234  * behavior that many types had implicit (yipes!) casts to text.
3235  */
3236  if (result == COERCION_PATH_NONE)
3237  {
3238  if (ccontext >= COERCION_ASSIGNMENT &&
3239  TypeCategory(targetTypeId) == TYPCATEGORY_STRING)
3240  result = COERCION_PATH_COERCEVIAIO;
3241  else if (ccontext >= COERCION_EXPLICIT &&
3242  TypeCategory(sourceTypeId) == TYPCATEGORY_STRING)
3243  result = COERCION_PATH_COERCEVIAIO;
3244  }
3245  }
3246 
3247  /*
3248  * When parsing PL/pgSQL assignments, allow an I/O cast to be used
3249  * whenever no normal coercion is available.
3250  */
3251  if (result == COERCION_PATH_NONE &&
3252  ccontext == COERCION_PLPGSQL)
3253  result = COERCION_PATH_COERCEVIAIO;
3254 
3255  return result;
3256 }
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
TYPCATEGORY TypeCategory(Oid type)
FormData_pg_cast * Form_pg_cast
Definition: pg_cast.h:57
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
CoercionContext
Definition: primnodes.h:713
@ COERCION_PLPGSQL
Definition: primnodes.h:716
@ COERCION_EXPLICIT
Definition: primnodes.h:717
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:232

References COERCION_ASSIGNMENT, COERCION_EXPLICIT, COERCION_IMPLICIT, COERCION_PATH_ARRAYCOERCE, COERCION_PATH_COERCEVIAIO, COERCION_PATH_FUNC, COERCION_PATH_NONE, COERCION_PATH_RELABELTYPE, COERCION_PLPGSQL, elog, ERROR, get_element_type(), getBaseType(), GETSTRUCT, HeapTupleIsValid, InvalidOid, ObjectIdGetDatum(), OidIsValid, ReleaseSysCache(), SearchSysCache2(), and TypeCategory().

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

◆ find_typmod_coercion_function()

CoercionPathType find_typmod_coercion_function ( Oid  typeId,
Oid funcid 
)

Definition at line 3281 of file parse_coerce.c.

3283 {
3284  CoercionPathType result;
3285  Type targetType;
3286  Form_pg_type typeForm;
3287  HeapTuple tuple;
3288 
3289  *funcid = InvalidOid;
3290  result = COERCION_PATH_FUNC;
3291 
3292  targetType = typeidType(typeId);
3293  typeForm = (Form_pg_type) GETSTRUCT(targetType);
3294 
3295  /* Check for a "true" array type */
3296  if (IsTrueArrayType(typeForm))
3297  {
3298  /* Yes, switch our attention to the element type */
3299  typeId = typeForm->typelem;
3300  result = COERCION_PATH_ARRAYCOERCE;
3301  }
3302  ReleaseSysCache(targetType);
3303 
3304  /* Look in pg_cast */
3305  tuple = SearchSysCache2(CASTSOURCETARGET,
3306  ObjectIdGetDatum(typeId),
3307  ObjectIdGetDatum(typeId));
3308 
3309  if (HeapTupleIsValid(tuple))
3310  {
3311  Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
3312 
3313  *funcid = castForm->castfunc;
3314  ReleaseSysCache(tuple);
3315  }
3316 
3317  if (!OidIsValid(*funcid))
3318  result = COERCION_PATH_NONE;
3319 
3320  return result;
3321 }
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261

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

Referenced by coerce_type_typmod().

◆ IsBinaryCoercible()

bool IsBinaryCoercible ( Oid  srctype,
Oid  targettype 
)

◆ IsBinaryCoercibleWithCast()

bool IsBinaryCoercibleWithCast ( Oid  srctype,
Oid  targettype,
Oid castoid 
)

Definition at line 3010 of file parse_coerce.c.

3012 {
3013  HeapTuple tuple;
3014  Form_pg_cast castForm;
3015  bool result;
3016 
3017  *castoid = InvalidOid;
3018 
3019  /* Fast path if same type */
3020  if (srctype == targettype)
3021  return true;
3022 
3023  /* Anything is coercible to ANY or ANYELEMENT or ANYCOMPATIBLE */
3024  if (targettype == ANYOID || targettype == ANYELEMENTOID ||
3025  targettype == ANYCOMPATIBLEOID)
3026  return true;
3027 
3028  /* If srctype is a domain, reduce to its base type */
3029  if (OidIsValid(srctype))
3030  srctype = getBaseType(srctype);
3031 
3032  /* Somewhat-fast path for domain -> base type case */
3033  if (srctype == targettype)
3034  return true;
3035 
3036  /* Also accept any array type as coercible to ANY[COMPATIBLE]ARRAY */
3037  if (targettype == ANYARRAYOID || targettype == ANYCOMPATIBLEARRAYOID)
3038  if (type_is_array(srctype))
3039  return true;
3040 
3041  /* Also accept any non-array type as coercible to ANY[COMPATIBLE]NONARRAY */
3042  if (targettype == ANYNONARRAYOID || targettype == ANYCOMPATIBLENONARRAYOID)
3043  if (!type_is_array(srctype))
3044  return true;
3045 
3046  /* Also accept any enum type as coercible to ANYENUM */
3047  if (targettype == ANYENUMOID)
3048  if (type_is_enum(srctype))
3049  return true;
3050 
3051  /* Also accept any range type as coercible to ANY[COMPATIBLE]RANGE */
3052  if (targettype == ANYRANGEOID || targettype == ANYCOMPATIBLERANGEOID)
3053  if (type_is_range(srctype))
3054  return true;
3055 
3056  /* Also, any multirange type is coercible to ANY[COMPATIBLE]MULTIRANGE */
3057  if (targettype == ANYMULTIRANGEOID || targettype == ANYCOMPATIBLEMULTIRANGEOID)
3058  if (type_is_multirange(srctype))
3059  return true;
3060 
3061  /* Also accept any composite type as coercible to RECORD */
3062  if (targettype == RECORDOID)
3063  if (ISCOMPLEX(srctype))
3064  return true;
3065 
3066  /* Also accept any composite array type as coercible to RECORD[] */
3067  if (targettype == RECORDARRAYOID)
3068  if (is_complex_array(srctype))
3069  return true;
3070 
3071  /* Else look in pg_cast */
3072  tuple = SearchSysCache2(CASTSOURCETARGET,
3073  ObjectIdGetDatum(srctype),
3074  ObjectIdGetDatum(targettype));
3075  if (!HeapTupleIsValid(tuple))
3076  return false; /* no cast */
3077  castForm = (Form_pg_cast) GETSTRUCT(tuple);
3078 
3079  result = (castForm->castmethod == COERCION_METHOD_BINARY &&
3080  castForm->castcontext == COERCION_CODE_IMPLICIT);
3081 
3082  if (result)
3083  *castoid = castForm->oid;
3084 
3085  ReleaseSysCache(tuple);
3086 
3087  return result;
3088 }
bool type_is_range(Oid typid)
Definition: lsyscache.c:2688
bool type_is_multirange(Oid typid)
Definition: lsyscache.c:2698
#define type_is_array(typid)
Definition: lsyscache.h:209

References getBaseType(), GETSTRUCT, HeapTupleIsValid, InvalidOid, is_complex_array(), ISCOMPLEX, ObjectIdGetDatum(), OidIsValid, ReleaseSysCache(), SearchSysCache2(), type_is_array, type_is_enum(), type_is_multirange(), and type_is_range().

Referenced by CreateCast(), and IsBinaryCoercible().

◆ IsPreferredType()

bool IsPreferredType ( TYPCATEGORY  category,
Oid  type 
)

Definition at line 2960 of file parse_coerce.c.

2961 {
2962  char typcategory;
2963  bool typispreferred;
2964 
2965  get_type_category_preferred(type, &typcategory, &typispreferred);
2966  if (category == typcategory || category == TYPCATEGORY_INVALID)
2967  return typispreferred;
2968  else
2969  return false;
2970 }
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2710
const char * type

References get_type_category_preferred(), and type.

Referenced by func_select_candidate(), and GetDefaultOpClass().

◆ parser_coercion_errposition()

int parser_coercion_errposition ( ParseState pstate,
int  coerce_location,
Node input_expr 
)

Definition at line 1277 of file parse_coerce.c.

1280 {
1281  if (coerce_location >= 0)
1282  return parser_errposition(pstate, coerce_location);
1283  else
1284  return parser_errposition(pstate, exprLocation(input_expr));
1285 }

References exprLocation(), and parser_errposition().

Referenced by coerce_record_to_complex(), coerceJsonFuncExpr(), and transformTypeCast().

◆ select_common_type()

Oid select_common_type ( ParseState pstate,
List exprs,
const char *  context,
Node **  which_expr 
)

Definition at line 1307 of file parse_coerce.c.

1309 {
1310  Node *pexpr;
1311  Oid ptype;
1312  TYPCATEGORY pcategory;
1313  bool pispreferred;
1314  ListCell *lc;
1315 
1316  Assert(exprs != NIL);
1317  pexpr = (Node *) linitial(exprs);
1318  lc = list_second_cell(exprs);
1319  ptype = exprType(pexpr);
1320 
1321  /*
1322  * If all input types are valid and exactly the same, just pick that type.
1323  * This is the only way that we will resolve the result as being a domain
1324  * type; otherwise domains are smashed to their base types for comparison.
1325  */
1326  if (ptype != UNKNOWNOID)
1327  {
1328  for_each_cell(lc, exprs, lc)
1329  {
1330  Node *nexpr = (Node *) lfirst(lc);
1331  Oid ntype = exprType(nexpr);
1332 
1333  if (ntype != ptype)
1334  break;
1335  }
1336  if (lc == NULL) /* got to the end of the list? */
1337  {
1338  if (which_expr)
1339  *which_expr = pexpr;
1340  return ptype;
1341  }
1342  }
1343 
1344  /*
1345  * Nope, so set up for the full algorithm. Note that at this point, lc
1346  * points to the first list item with type different from pexpr's; we need
1347  * not re-examine any items the previous loop advanced over.
1348  */
1349  ptype = getBaseType(ptype);
1350  get_type_category_preferred(ptype, &pcategory, &pispreferred);
1351 
1352  for_each_cell(lc, exprs, lc)
1353  {
1354  Node *nexpr = (Node *) lfirst(lc);
1355  Oid ntype = getBaseType(exprType(nexpr));
1356 
1357  /* move on to next one if no new information... */
1358  if (ntype != UNKNOWNOID && ntype != ptype)
1359  {
1360  TYPCATEGORY ncategory;
1361  bool nispreferred;
1362 
1363  get_type_category_preferred(ntype, &ncategory, &nispreferred);
1364  if (ptype == UNKNOWNOID)
1365  {
1366  /* so far, only unknowns so take anything... */
1367  pexpr = nexpr;
1368  ptype = ntype;
1369  pcategory = ncategory;
1370  pispreferred = nispreferred;
1371  }
1372  else if (ncategory != pcategory)
1373  {
1374  /*
1375  * both types in different categories? then not much hope...
1376  */
1377  if (context == NULL)
1378  return InvalidOid;
1379  ereport(ERROR,
1380  (errcode(ERRCODE_DATATYPE_MISMATCH),
1381  /*------
1382  translator: first %s is name of a SQL construct, eg CASE */
1383  errmsg("%s types %s and %s cannot be matched",
1384  context,
1385  format_type_be(ptype),
1386  format_type_be(ntype)),
1387  parser_errposition(pstate, exprLocation(nexpr))));
1388  }
1389  else if (!pispreferred &&
1390  can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
1391  !can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
1392  {
1393  /*
1394  * take new type if can coerce to it implicitly but not the
1395  * other way; but if we have a preferred type, stay on it.
1396  */
1397  pexpr = nexpr;
1398  ptype = ntype;
1399  pcategory = ncategory;
1400  pispreferred = nispreferred;
1401  }
1402  }
1403  }
1404 
1405  /*
1406  * If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
1407  * then resolve as type TEXT. This situation comes up with constructs
1408  * like SELECT (CASE WHEN foo THEN 'bar' ELSE 'baz' END); SELECT 'foo'
1409  * UNION SELECT 'bar'; It might seem desirable to leave the construct's
1410  * output type as UNKNOWN, but that really doesn't work, because we'd
1411  * probably end up needing a runtime coercion from UNKNOWN to something
1412  * else, and we usually won't have it. We need to coerce the unknown
1413  * literals while they are still literals, so a decision has to be made
1414  * now.
1415  */
1416  if (ptype == UNKNOWNOID)
1417  ptype = TEXTOID;
1418 
1419  if (which_expr)
1420  *which_expr = pexpr;
1421  return ptype;
1422 }
char TYPCATEGORY
Definition: parse_coerce.h:21
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:438
#define linitial(l)
Definition: pg_list.h:178
static ListCell * list_second_cell(const List *l)
Definition: pg_list.h:142

References Assert, can_coerce_type(), COERCION_IMPLICIT, context, ereport, errcode(), errmsg(), ERROR, exprLocation(), exprType(), for_each_cell, format_type_be(), get_type_category_preferred(), getBaseType(), InvalidOid, lfirst, linitial, list_second_cell(), NIL, and parser_errposition().

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

◆ select_common_typmod()

int32 select_common_typmod ( ParseState pstate,
List exprs,
Oid  common_type 
)

Definition at line 1609 of file parse_coerce.c.

1610 {
1611  ListCell *lc;
1612  bool first = true;
1613  int32 result = -1;
1614 
1615  foreach(lc, exprs)
1616  {
1617  Node *expr = (Node *) lfirst(lc);
1618 
1619  /* Types must match */
1620  if (exprType(expr) != common_type)
1621  return -1;
1622  else if (first)
1623  {
1624  result = exprTypmod(expr);
1625  first = false;
1626  }
1627  else
1628  {
1629  /* As soon as we see a non-matching typmod, fall back to -1 */
1630  if (result != exprTypmod(expr))
1631  return -1;
1632  }
1633  }
1634 
1635  return result;
1636 }
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298

References exprType(), exprTypmod(), and lfirst.

Referenced by analyzeCTE(), buildMergedJoinVar(), transformSetOperationTree(), transformValuesClause(), and unify_hypothetical_args().

◆ TypeCategory()

TYPCATEGORY TypeCategory ( Oid  type)

Definition at line 2941 of file parse_coerce.c.

2942 {
2943  char typcategory;
2944  bool typispreferred;
2945 
2946  get_type_category_preferred(type, &typcategory, &typispreferred);
2947  Assert(typcategory != TYPCATEGORY_INVALID);
2948  return (TYPCATEGORY) typcategory;
2949 }

References Assert, get_type_category_preferred(), and type.

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

◆ verify_common_type()

bool verify_common_type ( Oid  common_type,
List exprs 
)

Definition at line 1571 of file parse_coerce.c.

1572 {
1573  ListCell *lc;
1574 
1575  foreach(lc, exprs)
1576  {
1577  Node *nexpr = (Node *) lfirst(lc);
1578  Oid ntype = exprType(nexpr);
1579 
1580  if (!can_coerce_type(1, &ntype, &common_type, COERCION_IMPLICIT))
1581  return false;
1582  }
1583  return true;
1584 }

References can_coerce_type(), COERCION_IMPLICIT, exprType(), and lfirst.

Referenced by transformAExprIn().