PostgreSQL Source Code git master
parse_coerce.c File Reference
#include "postgres.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_class.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_coerce.h"
#include "parser/parse_relation.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for parse_coerce.c:

Go to the source code of this file.

Functions

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

Function Documentation

◆ build_coercion_expression()

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

Definition at line 837 of file parse_coerce.c.

843{
844 int nargs = 0;
845
846 if (OidIsValid(funcId))
847 {
848 HeapTuple tp;
849 Form_pg_proc procstruct;
850
851 tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcId));
852 if (!HeapTupleIsValid(tp))
853 elog(ERROR, "cache lookup failed for function %u", funcId);
854 procstruct = (Form_pg_proc) GETSTRUCT(tp);
855
856 /*
857 * These Asserts essentially check that function is a legal coercion
858 * function. We can't make the seemingly obvious tests on prorettype
859 * and proargtypes[0], even in the COERCION_PATH_FUNC case, because of
860 * various binary-compatibility cases.
861 */
862 /* Assert(targetTypeId == procstruct->prorettype); */
863 Assert(!procstruct->proretset);
864 Assert(procstruct->prokind == PROKIND_FUNCTION);
865 nargs = procstruct->pronargs;
866 Assert(nargs >= 1 && nargs <= 3);
867 /* Assert(procstruct->proargtypes.values[0] == exprType(node)); */
868 Assert(nargs < 2 || procstruct->proargtypes.values[1] == INT4OID);
869 Assert(nargs < 3 || procstruct->proargtypes.values[2] == BOOLOID);
870
871 ReleaseSysCache(tp);
872 }
873
874 if (pathtype == COERCION_PATH_FUNC)
875 {
876 /* We build an ordinary FuncExpr with special arguments */
877 FuncExpr *fexpr;
878 List *args;
879 Const *cons;
880
881 Assert(OidIsValid(funcId));
882
883 args = list_make1(node);
884
885 if (nargs >= 2)
886 {
887 /* Pass target typmod as an int4 constant */
888 cons = makeConst(INT4OID,
889 -1,
891 sizeof(int32),
892 Int32GetDatum(targetTypMod),
893 false,
894 true);
895
896 args = lappend(args, cons);
897 }
898
899 if (nargs == 3)
900 {
901 /* Pass it a boolean isExplicit parameter, too */
902 cons = makeConst(BOOLOID,
903 -1,
905 sizeof(bool),
906 BoolGetDatum(ccontext == COERCION_EXPLICIT),
907 false,
908 true);
909
910 args = lappend(args, cons);
911 }
912
913 fexpr = makeFuncExpr(funcId, targetTypeId, args,
914 InvalidOid, InvalidOid, cformat);
915 fexpr->location = location;
916 return (Node *) fexpr;
917 }
918 else if (pathtype == COERCION_PATH_ARRAYCOERCE)
919 {
920 /* We need to build an ArrayCoerceExpr */
923 Oid sourceBaseTypeId;
924 int32 sourceBaseTypeMod;
925 Oid targetElementType;
926 Node *elemexpr;
927
928 /*
929 * Look through any domain over the source array type. Note we don't
930 * expect that the target type is a domain; it must be a plain array.
931 * (To get to a domain target type, we'll do coerce_to_domain later.)
932 */
933 sourceBaseTypeMod = exprTypmod(node);
934 sourceBaseTypeId = getBaseTypeAndTypmod(exprType(node),
935 &sourceBaseTypeMod);
936
937 /*
938 * Set up a CaseTestExpr representing one element of the source array.
939 * This is an abuse of CaseTestExpr, but it's OK as long as there
940 * can't be any CaseExpr or ArrayCoerceExpr within the completed
941 * elemexpr.
942 */
943 ctest->typeId = get_element_type(sourceBaseTypeId);
944 Assert(OidIsValid(ctest->typeId));
945 ctest->typeMod = sourceBaseTypeMod;
946 ctest->collation = InvalidOid; /* Assume coercions don't care */
947
948 /* And coerce it to the target element type */
949 targetElementType = get_element_type(targetTypeId);
950 Assert(OidIsValid(targetElementType));
951
952 elemexpr = coerce_to_target_type(NULL,
953 (Node *) ctest,
954 ctest->typeId,
955 targetElementType,
956 targetTypMod,
957 ccontext,
958 cformat,
959 location);
960 if (elemexpr == NULL) /* shouldn't happen */
961 elog(ERROR, "failed to coerce array element type as expected");
962
963 acoerce->arg = (Expr *) node;
964 acoerce->elemexpr = (Expr *) elemexpr;
965 acoerce->resulttype = targetTypeId;
966
967 /*
968 * Label the output as having a particular element typmod only if we
969 * ended up with a per-element expression that is labeled that way.
970 */
971 acoerce->resulttypmod = exprTypmod(elemexpr);
972 /* resultcollid will be set by parse_collate.c */
973 acoerce->coerceformat = cformat;
974 acoerce->location = location;
975
976 return (Node *) acoerce;
977 }
978 else if (pathtype == COERCION_PATH_COERCEVIAIO)
979 {
980 /* We need to build a CoerceViaIO node */
981 CoerceViaIO *iocoerce = makeNode(CoerceViaIO);
982
983 Assert(!OidIsValid(funcId));
984
985 iocoerce->arg = (Expr *) node;
986 iocoerce->resulttype = targetTypeId;
987 /* resultcollid will be set by parse_collate.c */
988 iocoerce->coerceformat = cformat;
989 iocoerce->location = location;
990
991 return (Node *) iocoerce;
992 }
993 else
994 {
995 elog(ERROR, "unsupported pathtype %d in build_coercion_expression",
996 (int) pathtype);
997 return NULL; /* keep compiler quiet */
998 }
999}
#define Assert(condition)
Definition: c.h:815
int32_t int32
Definition: c.h:484
#define OidIsValid(objectId)
Definition: c.h:732
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
List * lappend(List *list, void *datum)
Definition: list.c:339
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2786
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2565
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Definition: makefuncs.c:547
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:303
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:301
#define makeNode(_type_)
Definition: nodes.h:155
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
@ COERCION_PATH_COERCEVIAIO
Definition: parse_coerce.h:30
@ COERCION_PATH_ARRAYCOERCE
Definition: parse_coerce.h:29
@ COERCION_PATH_FUNC
Definition: parse_coerce.h:27
#define list_make1(x1)
Definition: pg_list.h:212
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
@ COERCION_EXPLICIT
Definition: primnodes.h:734
ParseLoc location
Definition: primnodes.h:1259
Expr * arg
Definition: primnodes.h:1224
ParseLoc location
Definition: primnodes.h:1231
Oid resulttype
Definition: primnodes.h:1225
ParseLoc location
Definition: primnodes.h:787
Definition: pg_list.h:54
Definition: nodes.h:129
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221

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

Referenced by coerce_type(), and coerce_type_typmod().

◆ can_coerce_type()

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

Definition at line 557 of file parse_coerce.c.

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

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 1737 of file parse_coerce.c.

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

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 2952 of file parse_coerce.c.

2955{
2956 if (ret_type == INTERNALOID)
2957 {
2958 for (int i = 0; i < nargs; i++)
2959 {
2960 if (declared_arg_types[i] == ret_type)
2961 return NULL; /* OK */
2962 }
2963 return pstrdup(_("A result of type internal requires at least one input of type internal."));
2964 }
2965 else
2966 return NULL; /* OK, ret_type is not INTERNAL */
2967}
#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 2875 of file parse_coerce.c.

2878{
2879 if (ret_type == ANYRANGEOID || ret_type == ANYMULTIRANGEOID)
2880 {
2881 /*
2882 * ANYRANGE and ANYMULTIRANGE require an ANYRANGE or ANYMULTIRANGE
2883 * input, else we can't tell which of several range types with the
2884 * same element type to use.
2885 */
2886 for (int i = 0; i < nargs; i++)
2887 {
2888 if (declared_arg_types[i] == ANYRANGEOID ||
2889 declared_arg_types[i] == ANYMULTIRANGEOID)
2890 return NULL; /* OK */
2891 }
2892 return psprintf(_("A result of type %s requires at least one input of type anyrange or anymultirange."),
2893 format_type_be(ret_type));
2894 }
2895 else if (ret_type == ANYCOMPATIBLERANGEOID || ret_type == ANYCOMPATIBLEMULTIRANGEOID)
2896 {
2897 /*
2898 * ANYCOMPATIBLERANGE and ANYCOMPATIBLEMULTIRANGE require an
2899 * ANYCOMPATIBLERANGE or ANYCOMPATIBLEMULTIRANGE input, else we can't
2900 * tell which of several range types with the same element type to
2901 * use.
2902 */
2903 for (int i = 0; i < nargs; i++)
2904 {
2905 if (declared_arg_types[i] == ANYCOMPATIBLERANGEOID ||
2906 declared_arg_types[i] == ANYCOMPATIBLEMULTIRANGEOID)
2907 return NULL; /* OK */
2908 }
2909 return psprintf(_("A result of type %s requires at least one input of type anycompatiblerange or anycompatiblemultirange."),
2910 format_type_be(ret_type));
2911 }
2912 else if (IsPolymorphicTypeFamily1(ret_type))
2913 {
2914 /* Otherwise, any family-1 type can be deduced from any other */
2915 for (int i = 0; i < nargs; i++)
2916 {
2917 if (IsPolymorphicTypeFamily1(declared_arg_types[i]))
2918 return NULL; /* OK */
2919 }
2920 /* Keep this list in sync with IsPolymorphicTypeFamily1! */
2921 return psprintf(_("A result of type %s requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange."),
2922 format_type_be(ret_type));
2923 }
2924 else if (IsPolymorphicTypeFamily2(ret_type))
2925 {
2926 /* Otherwise, any family-2 type can be deduced from any other */
2927 for (int i = 0; i < nargs; i++)
2928 {
2929 if (IsPolymorphicTypeFamily2(declared_arg_types[i]))
2930 return NULL; /* OK */
2931 }
2932 /* Keep this list in sync with IsPolymorphicTypeFamily2! */
2933 return psprintf(_("A result of type %s requires at least one input of type anycompatible, anycompatiblearray, anycompatiblenonarray, anycompatiblerange, or anycompatiblemultirange."),
2934 format_type_be(ret_type));
2935 }
2936 else
2937 return NULL; /* OK, ret_type is not polymorphic */
2938}
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_null_to_domain()

Node * coerce_null_to_domain ( Oid  typid,
int32  typmod,
Oid  collation,
int  typlen,
bool  typbyval 
)

Definition at line 1271 of file parse_coerce.c.

1273{
1274 Node *result;
1275 Oid baseTypeId;
1276 int32 baseTypeMod = typmod;
1277
1278 /*
1279 * The constant must appear to have the domain's base type/typmod, else
1280 * coerce_to_domain() will apply a length coercion which is useless.
1281 */
1282 baseTypeId = getBaseTypeAndTypmod(typid, &baseTypeMod);
1283 result = (Node *) makeConst(baseTypeId,
1284 baseTypeMod,
1285 collation,
1286 typlen,
1287 (Datum) 0,
1288 true, /* isnull */
1289 typbyval);
1290 if (typid != baseTypeId)
1291 result = coerce_to_domain(result,
1292 baseTypeId, baseTypeMod,
1293 typid,
1296 -1,
1297 false);
1298 return result;
1299}
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
Definition: parse_coerce.c:675
uintptr_t Datum
Definition: postgres.h:69
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:753
@ COERCION_IMPLICIT
Definition: primnodes.h:731

References COERCE_IMPLICIT_CAST, coerce_to_domain(), COERCION_IMPLICIT, getBaseTypeAndTypmod(), and makeConst().

Referenced by expand_insert_targetlist(), ReplaceVarsFromTargetList_callback(), rewriteTargetListIU(), and rewriteValuesRTE().

◆ coerce_record_to_complex()

static Node * coerce_record_to_complex ( ParseState pstate,
Node node,
Oid  targetTypeId,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location 
)
static

Definition at line 1010 of file parse_coerce.c.

1015{
1016 RowExpr *rowexpr;
1017 Oid baseTypeId;
1018 int32 baseTypeMod = -1;
1019 TupleDesc tupdesc;
1020 List *args = NIL;
1021 List *newargs;
1022 int i;
1023 int ucolno;
1024 ListCell *arg;
1025
1026 if (node && IsA(node, RowExpr))
1027 {
1028 /*
1029 * Since the RowExpr must be of type RECORD, we needn't worry about it
1030 * containing any dropped columns.
1031 */
1032 args = ((RowExpr *) node)->args;
1033 }
1034 else if (node && IsA(node, Var) &&
1035 ((Var *) node)->varattno == InvalidAttrNumber)
1036 {
1037 int rtindex = ((Var *) node)->varno;
1038 int sublevels_up = ((Var *) node)->varlevelsup;
1039 int vlocation = ((Var *) node)->location;
1040 ParseNamespaceItem *nsitem;
1041
1042 nsitem = GetNSItemByRangeTablePosn(pstate, rtindex, sublevels_up);
1043 args = expandNSItemVars(pstate, nsitem, sublevels_up, vlocation, NULL);
1044 }
1045 else
1046 ereport(ERROR,
1047 (errcode(ERRCODE_CANNOT_COERCE),
1048 errmsg("cannot cast type %s to %s",
1049 format_type_be(RECORDOID),
1050 format_type_be(targetTypeId)),
1051 parser_coercion_errposition(pstate, location, node)));
1052
1053 /*
1054 * Look up the composite type, accounting for possibility that what we are
1055 * given is a domain over composite.
1056 */
1057 baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
1058 tupdesc = lookup_rowtype_tupdesc(baseTypeId, baseTypeMod);
1059
1060 /* Process the fields */
1061 newargs = NIL;
1062 ucolno = 1;
1063 arg = list_head(args);
1064 for (i = 0; i < tupdesc->natts; i++)
1065 {
1066 Node *expr;
1067 Node *cexpr;
1068 Oid exprtype;
1069 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
1070
1071 /* Fill in NULLs for dropped columns in rowtype */
1072 if (attr->attisdropped)
1073 {
1074 /*
1075 * can't use atttypid here, but it doesn't really matter what type
1076 * the Const claims to be.
1077 */
1078 newargs = lappend(newargs,
1079 makeNullConst(INT4OID, -1, InvalidOid));
1080 continue;
1081 }
1082
1083 if (arg == NULL)
1084 ereport(ERROR,
1085 (errcode(ERRCODE_CANNOT_COERCE),
1086 errmsg("cannot cast type %s to %s",
1087 format_type_be(RECORDOID),
1088 format_type_be(targetTypeId)),
1089 errdetail("Input has too few columns."),
1090 parser_coercion_errposition(pstate, location, node)));
1091 expr = (Node *) lfirst(arg);
1092 exprtype = exprType(expr);
1093
1094 cexpr = coerce_to_target_type(pstate,
1095 expr, exprtype,
1096 attr->atttypid,
1097 attr->atttypmod,
1098 ccontext,
1100 -1);
1101 if (cexpr == NULL)
1102 ereport(ERROR,
1103 (errcode(ERRCODE_CANNOT_COERCE),
1104 errmsg("cannot cast type %s to %s",
1105 format_type_be(RECORDOID),
1106 format_type_be(targetTypeId)),
1107 errdetail("Cannot cast type %s to %s in column %d.",
1108 format_type_be(exprtype),
1109 format_type_be(attr->atttypid),
1110 ucolno),
1111 parser_coercion_errposition(pstate, location, expr)));
1112 newargs = lappend(newargs, cexpr);
1113 ucolno++;
1114 arg = lnext(args, arg);
1115 }
1116 if (arg != NULL)
1117 ereport(ERROR,
1118 (errcode(ERRCODE_CANNOT_COERCE),
1119 errmsg("cannot cast type %s to %s",
1120 format_type_be(RECORDOID),
1121 format_type_be(targetTypeId)),
1122 errdetail("Input has too many columns."),
1123 parser_coercion_errposition(pstate, location, node)));
1124
1125 ReleaseTupleDesc(tupdesc);
1126
1127 rowexpr = makeNode(RowExpr);
1128 rowexpr->args = newargs;
1129 rowexpr->row_typeid = baseTypeId;
1130 rowexpr->row_format = cformat;
1131 rowexpr->colnames = NIL; /* not needed for named target type */
1132 rowexpr->location = location;
1133
1134 /* If target is a domain, apply constraints */
1135 if (baseTypeId != targetTypeId)
1136 {
1137 rowexpr->row_format = COERCE_IMPLICIT_CAST;
1138 return coerce_to_domain((Node *) rowexpr,
1139 baseTypeId, baseTypeMod,
1140 targetTypeId,
1141 ccontext, cformat, location,
1142 false);
1143 }
1144
1145 return (Node *) rowexpr;
1146}
#define InvalidAttrNumber
Definition: attnum.h:23
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:341
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr)
List * expandNSItemVars(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
ParseNamespaceItem * GetNSItemByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
void * arg
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
List * args
Definition: primnodes.h:1428
ParseLoc location
Definition: primnodes.h:1452
Definition: primnodes.h:262
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:213
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:154
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1920

References arg, generate_unaccent_rules::args, RowExpr::args, COERCE_IMPLICIT_CAST, coerce_to_domain(), coerce_to_target_type(), ereport, errcode(), errdetail(), errmsg(), ERROR, expandNSItemVars(), exprType(), format_type_be(), getBaseTypeAndTypmod(), GetNSItemByRangeTablePosn(), i, InvalidAttrNumber, InvalidOid, IsA, lappend(), lfirst, list_head(), lnext(), RowExpr::location, lookup_rowtype_tupdesc(), makeNode, makeNullConst(), TupleDescData::natts, NIL, parser_coercion_errposition(), ReleaseTupleDesc, and TupleDescAttr().

Referenced by coerce_type().

◆ coerce_to_boolean()

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

Definition at line 1159 of file parse_coerce.c.

1161{
1162 Oid inputTypeId = exprType(node);
1163
1164 if (inputTypeId != BOOLOID)
1165 {
1166 Node *newnode;
1167
1168 newnode = coerce_to_target_type(pstate, node, inputTypeId,
1169 BOOLOID, -1,
1172 -1);
1173 if (newnode == NULL)
1174 ereport(ERROR,
1175 (errcode(ERRCODE_DATATYPE_MISMATCH),
1176 /* translator: first %s is name of a SQL construct, eg WHERE */
1177 errmsg("argument of %s must be type %s, not type %s",
1178 constructName, "boolean",
1179 format_type_be(inputTypeId)),
1180 parser_errposition(pstate, exprLocation(node))));
1181 node = newnode;
1182 }
1183
1184 if (expression_returns_set(node))
1185 ereport(ERROR,
1186 (errcode(ERRCODE_DATATYPE_MISMATCH),
1187 /* translator: %s is name of a SQL construct, eg WHERE */
1188 errmsg("argument of %s must not return a set",
1189 constructName),
1190 parser_errposition(pstate, exprLocation(node))));
1191
1192 return node;
1193}
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1388
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:763
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
@ COERCION_ASSIGNMENT
Definition: primnodes.h:732

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 1572 of file parse_coerce.c.

1574{
1575 Oid inputTypeId = exprType(node);
1576
1577 if (inputTypeId == targetTypeId)
1578 return node; /* no work */
1579 if (can_coerce_type(1, &inputTypeId, &targetTypeId, COERCION_IMPLICIT))
1580 node = coerce_type(pstate, node, inputTypeId, targetTypeId, -1,
1582 else
1583 ereport(ERROR,
1584 (errcode(ERRCODE_CANNOT_COERCE),
1585 /* translator: first %s is name of a SQL construct, eg CASE */
1586 errmsg("%s could not convert type %s to %s",
1587 context,
1588 format_type_be(inputTypeId),
1589 format_type_be(targetTypeId)),
1590 parser_errposition(pstate, exprLocation(node))));
1591 return node;
1592}
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:557

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

Referenced by 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 675 of file parse_coerce.c.

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

References arg, CoerceToDomain::arg, Assert, COERCE_IMPLICIT_CAST, coerce_type_typmod(), hide_coercion_node(), CoerceToDomain::location, makeNode, OidIsValid, and CoerceToDomain::resulttype.

Referenced by coerce_null_to_domain(), coerce_record_to_complex(), coerce_type(), and transformAssignmentIndirection().

◆ coerce_to_specific_type()

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

Definition at line 1255 of file parse_coerce.c.

1258{
1259 return coerce_to_specific_type_typmod(pstate, node,
1260 targetTypeId, -1,
1261 constructName);
1262}
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 1206 of file parse_coerce.c.

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

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:3108
Expr * arg
Definition: primnodes.h:1296
ParseLoc location
Definition: primnodes.h:1298

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,
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 Oid baseTypeId;
418 int32 baseTypeMod;
419
420 baseTypeMod = targetTypeMod;
421 baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
422
423 if (pathtype != COERCION_PATH_RELABELTYPE)
424 {
425 /*
426 * Generate an expression tree representing run-time application
427 * of the conversion function. If we are dealing with a domain
428 * target type, the conversion function will yield the base type,
429 * and we need to extract the correct typmod to use from the
430 * domain's typtypmod.
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, baseTypeId, baseTypeMod,
458 targetTypeId,
459 ccontext, cformat, location,
460 false);
461 if (result == node)
462 {
463 /*
464 * XXX could we label result with exprTypmod(node) instead of
465 * default -1 typmod, to save a possible length-coercion
466 * later? Would work if both types have same interpretation of
467 * typmod, which is likely but not certain.
468 */
469 RelabelType *r = makeRelabelType((Expr *) result,
470 targetTypeId, -1,
472 cformat);
473
474 r->location = location;
475 result = (Node *) r;
476 }
477 }
478 return result;
479 }
480 if (inputTypeId == RECORDOID &&
481 ISCOMPLEX(targetTypeId))
482 {
483 /* Coerce a RECORD to a specific complex type */
484 return coerce_record_to_complex(pstate, node, targetTypeId,
485 ccontext, cformat, location);
486 }
487 if (targetTypeId == RECORDOID &&
488 ISCOMPLEX(inputTypeId))
489 {
490 /* Coerce a specific complex type to RECORD */
491 /* NB: we do NOT want a RelabelType here */
492 return node;
493 }
494#ifdef NOT_USED
495 if (inputTypeId == RECORDARRAYOID &&
496 is_complex_array(targetTypeId))
497 {
498 /* Coerce record[] to a specific complex array type */
499 /* not implemented yet ... */
500 }
501#endif
502 if (targetTypeId == RECORDARRAYOID &&
503 is_complex_array(inputTypeId))
504 {
505 /* Coerce a specific complex array type to record[] */
506 /* NB: we do NOT want a RelabelType here */
507 return node;
508 }
509 if (typeInheritsFrom(inputTypeId, targetTypeId)
510 || typeIsOfTypedTable(inputTypeId, targetTypeId))
511 {
512 /*
513 * Input class type is a subclass of target, so generate an
514 * appropriate runtime conversion (removing unneeded columns and
515 * possibly rearranging the ones that are wanted).
516 *
517 * We will also get here when the input is a domain over a subclass of
518 * the target type. To keep life simple for the executor, we define
519 * ConvertRowtypeExpr as only working between regular composite types;
520 * therefore, in such cases insert a RelabelType to smash the input
521 * expression down to its base type.
522 */
523 Oid baseTypeId = getBaseType(inputTypeId);
525
526 if (baseTypeId != inputTypeId)
527 {
528 RelabelType *rt = makeRelabelType((Expr *) node,
529 baseTypeId, -1,
532
533 rt->location = location;
534 node = (Node *) rt;
535 }
536 r->arg = (Expr *) node;
537 r->resulttype = targetTypeId;
538 r->convertformat = cformat;
539 r->location = location;
540 return (Node *) r;
541 }
542 /* If we get here, caller blew it */
543 elog(ERROR, "failed to find conversion function from %s to %s",
544 format_type_be(inputTypeId), format_type_be(targetTypeId));
545 return NULL; /* keep compiler quiet */
546}
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:223
#define WARNING
Definition: elog.h:36
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:406
static Node * coerce_record_to_complex(ParseState *pstate, Node *node, Oid targetTypeId, CoercionContext ccontext, CoercionForm cformat, int location)
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:837
@ COERCION_PATH_RELABELTYPE
Definition: parse_coerce.h:28
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
char * typeTypeName(Type t)
Definition: parse_type.c:619
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
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
static char * DatumGetCString(Datum X)
Definition: postgres.h:340
Oid consttype
Definition: primnodes.h:329
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:257
ParseLoc location
Definition: primnodes.h:1209

References ConvertRowtypeExpr::arg, CollateExpr::arg, build_coercion_expression(), cancel_parser_errposition_callback(), COERCE_IMPLICIT_CAST, coerce_record_to_complex(), coerce_to_domain(), coerce_type(), COERCION_PATH_NONE, COERCION_PATH_RELABELTYPE, CollateExpr::collOid, Const::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(), coerce_type(), jsonb_subscript_transform(), make_fn_arguments(), ParseFuncOrColumn(), resolveTargetListUnknowns(), transformArrayExpr(), and unify_hypothetical_args().

◆ coerce_type_typmod()

static Node * coerce_type_typmod ( Node node,
Oid  targetTypeId,
int32  targetTypMod,
CoercionContext  ccontext,
CoercionForm  cformat,
int  location,
bool  hideInputCoercion 
)
static

Definition at line 751 of file parse_coerce.c.

755{
756 CoercionPathType pathtype;
757 Oid funcId;
758
759 /* Skip coercion if already done */
760 if (targetTypMod == exprTypmod(node))
761 return node;
762
763 /* Suppress display of nested coercion steps */
764 if (hideInputCoercion)
765 hide_coercion_node(node);
766
767 /*
768 * A negative typmod means that no actual coercion is needed, but we still
769 * want a RelabelType to ensure that the expression exposes the intended
770 * typmod.
771 */
772 if (targetTypMod < 0)
773 pathtype = COERCION_PATH_NONE;
774 else
775 pathtype = find_typmod_coercion_function(targetTypeId, &funcId);
776
777 if (pathtype != COERCION_PATH_NONE)
778 {
779 node = build_coercion_expression(node, pathtype, funcId,
780 targetTypeId, targetTypMod,
781 ccontext, cformat, location);
782 }
783 else
784 {
785 /*
786 * We don't need to perform any actual coercion step, but we should
787 * apply a RelabelType to ensure that the expression exposes the
788 * intended typmod.
789 */
790 node = applyRelabelType(node, targetTypeId, targetTypMod,
791 exprCollation(node),
792 cformat, location, false);
793 }
794
795 return node;
796}
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
Node * applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat, int rlocation, bool overwrite_ok)
Definition: nodeFuncs.c:636
CoercionPathType find_typmod_coercion_function(Oid typeId, Oid *funcid)

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

Referenced by coerce_to_domain(), and coerce_to_target_type().

◆ 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 2131 of file parse_coerce.c.

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

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 3153 of file parse_coerce.c.

3156{
3158 HeapTuple tuple;
3159
3160 *funcid = InvalidOid;
3161
3162 /* Perhaps the types are domains; if so, look at their base types */
3163 if (OidIsValid(sourceTypeId))
3164 sourceTypeId = getBaseType(sourceTypeId);
3165 if (OidIsValid(targetTypeId))
3166 targetTypeId = getBaseType(targetTypeId);
3167
3168 /* Domains are always coercible to and from their base type */
3169 if (sourceTypeId == targetTypeId)
3171
3172 /* Look in pg_cast */
3173 tuple = SearchSysCache2(CASTSOURCETARGET,
3174 ObjectIdGetDatum(sourceTypeId),
3175 ObjectIdGetDatum(targetTypeId));
3176
3177 if (HeapTupleIsValid(tuple))
3178 {
3179 Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
3180 CoercionContext castcontext;
3181
3182 /* convert char value for castcontext to CoercionContext enum */
3183 switch (castForm->castcontext)
3184 {
3185 case COERCION_CODE_IMPLICIT:
3186 castcontext = COERCION_IMPLICIT;
3187 break;
3188 case COERCION_CODE_ASSIGNMENT:
3189 castcontext = COERCION_ASSIGNMENT;
3190 break;
3191 case COERCION_CODE_EXPLICIT:
3192 castcontext = COERCION_EXPLICIT;
3193 break;
3194 default:
3195 elog(ERROR, "unrecognized castcontext: %d",
3196 (int) castForm->castcontext);
3197 castcontext = 0; /* keep compiler quiet */
3198 break;
3199 }
3200
3201 /* Rely on ordering of enum for correct behavior here */
3202 if (ccontext >= castcontext)
3203 {
3204 switch (castForm->castmethod)
3205 {
3206 case COERCION_METHOD_FUNCTION:
3207 result = COERCION_PATH_FUNC;
3208 *funcid = castForm->castfunc;
3209 break;
3210 case COERCION_METHOD_INOUT:
3212 break;
3213 case COERCION_METHOD_BINARY:
3215 break;
3216 default:
3217 elog(ERROR, "unrecognized castmethod: %d",
3218 (int) castForm->castmethod);
3219 break;
3220 }
3221 }
3222
3223 ReleaseSysCache(tuple);
3224 }
3225 else
3226 {
3227 /*
3228 * If there's no pg_cast entry, perhaps we are dealing with a pair of
3229 * array types. If so, and if their element types have a conversion
3230 * pathway, report that we can coerce with an ArrayCoerceExpr.
3231 *
3232 * Hack: disallow coercions to oidvector and int2vector, which
3233 * otherwise tend to capture coercions that should go to "real" array
3234 * types. We want those types to be considered "real" arrays for many
3235 * purposes, but not this one. (Also, ArrayCoerceExpr isn't
3236 * guaranteed to produce an output that meets the restrictions of
3237 * these datatypes, such as being 1-dimensional.)
3238 */
3239 if (targetTypeId != OIDVECTOROID && targetTypeId != INT2VECTOROID)
3240 {
3241 Oid targetElem;
3242 Oid sourceElem;
3243
3244 if ((targetElem = get_element_type(targetTypeId)) != InvalidOid &&
3245 (sourceElem = get_element_type(sourceTypeId)) != InvalidOid)
3246 {
3247 CoercionPathType elempathtype;
3248 Oid elemfuncid;
3249
3250 elempathtype = find_coercion_pathway(targetElem,
3251 sourceElem,
3252 ccontext,
3253 &elemfuncid);
3254 if (elempathtype != COERCION_PATH_NONE)
3255 {
3257 }
3258 }
3259 }
3260
3261 /*
3262 * If we still haven't found a possibility, consider automatic casting
3263 * using I/O functions. We allow assignment casts to string types and
3264 * explicit casts from string types to be handled this way. (The
3265 * CoerceViaIO mechanism is a lot more general than that, but this is
3266 * all we want to allow in the absence of a pg_cast entry.) It would
3267 * probably be better to insist on explicit casts in both directions,
3268 * but this is a compromise to preserve something of the pre-8.3
3269 * behavior that many types had implicit (yipes!) casts to text.
3270 */
3271 if (result == COERCION_PATH_NONE)
3272 {
3273 if (ccontext >= COERCION_ASSIGNMENT &&
3274 TypeCategory(targetTypeId) == TYPCATEGORY_STRING)
3276 else if (ccontext >= COERCION_EXPLICIT &&
3277 TypeCategory(sourceTypeId) == TYPCATEGORY_STRING)
3279 }
3280 }
3281
3282 /*
3283 * When parsing PL/pgSQL assignments, allow an I/O cast to be used
3284 * whenever no normal coercion is available.
3285 */
3286 if (result == COERCION_PATH_NONE &&
3287 ccontext == COERCION_PLPGSQL)
3289
3290 return result;
3291}
TYPCATEGORY TypeCategory(Oid type)
FormData_pg_cast * Form_pg_cast
Definition: pg_cast.h:57
CoercionContext
Definition: primnodes.h:730
@ COERCION_PLPGSQL
Definition: primnodes.h:733
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, find_coercion_pathway(), get_element_type(), getBaseType(), GETSTRUCT(), HeapTupleIsValid, InvalidOid, ObjectIdGetDatum(), OidIsValid, ReleaseSysCache(), SearchSysCache2(), and TypeCategory().

Referenced by can_coerce_type(), coerce_type(), find_coercion_pathway(), 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 3316 of file parse_coerce.c.

3318{
3319 CoercionPathType result;
3320 Type targetType;
3321 Form_pg_type typeForm;
3322 HeapTuple tuple;
3323
3324 *funcid = InvalidOid;
3325 result = COERCION_PATH_FUNC;
3326
3327 targetType = typeidType(typeId);
3328 typeForm = (Form_pg_type) GETSTRUCT(targetType);
3329
3330 /* Check for a "true" array type */
3331 if (IsTrueArrayType(typeForm))
3332 {
3333 /* Yes, switch our attention to the element type */
3334 typeId = typeForm->typelem;
3336 }
3337 ReleaseSysCache(targetType);
3338
3339 /* Look in pg_cast */
3340 tuple = SearchSysCache2(CASTSOURCETARGET,
3341 ObjectIdGetDatum(typeId),
3342 ObjectIdGetDatum(typeId));
3343
3344 if (HeapTupleIsValid(tuple))
3345 {
3346 Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
3347
3348 *funcid = castForm->castfunc;
3349 ReleaseSysCache(tuple);
3350 }
3351
3352 if (!OidIsValid(*funcid))
3353 result = COERCION_PATH_NONE;
3354
3355 return result;
3356}
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().

◆ hide_coercion_node()

static void hide_coercion_node ( Node node)
static

Definition at line 809 of file parse_coerce.c.

810{
811 if (IsA(node, FuncExpr))
812 ((FuncExpr *) node)->funcformat = COERCE_IMPLICIT_CAST;
813 else if (IsA(node, RelabelType))
814 ((RelabelType *) node)->relabelformat = COERCE_IMPLICIT_CAST;
815 else if (IsA(node, CoerceViaIO))
816 ((CoerceViaIO *) node)->coerceformat = COERCE_IMPLICIT_CAST;
817 else if (IsA(node, ArrayCoerceExpr))
818 ((ArrayCoerceExpr *) node)->coerceformat = COERCE_IMPLICIT_CAST;
819 else if (IsA(node, ConvertRowtypeExpr))
820 ((ConvertRowtypeExpr *) node)->convertformat = COERCE_IMPLICIT_CAST;
821 else if (IsA(node, RowExpr))
822 ((RowExpr *) node)->row_format = COERCE_IMPLICIT_CAST;
823 else if (IsA(node, CoerceToDomain))
824 ((CoerceToDomain *) node)->coercionformat = COERCE_IMPLICIT_CAST;
825 else
826 elog(ERROR, "unsupported node type: %d", (int) nodeTag(node));
827}
#define nodeTag(nodeptr)
Definition: nodes.h:133

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

Referenced by coerce_to_domain(), and coerce_type_typmod().

◆ is_complex_array()

static bool is_complex_array ( Oid  typid)
static

Definition at line 3366 of file parse_coerce.c.

3367{
3368 Oid elemtype = get_element_type(typid);
3369
3370 return (OidIsValid(elemtype) && ISCOMPLEX(elemtype));
3371}

References get_element_type(), ISCOMPLEX, and OidIsValid.

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

◆ IsBinaryCoercible()

bool IsBinaryCoercible ( Oid  srctype,
Oid  targettype 
)

◆ IsBinaryCoercibleWithCast()

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

Definition at line 3045 of file parse_coerce.c.

3047{
3048 HeapTuple tuple;
3049 Form_pg_cast castForm;
3050 bool result;
3051
3052 *castoid = InvalidOid;
3053
3054 /* Fast path if same type */
3055 if (srctype == targettype)
3056 return true;
3057
3058 /* Anything is coercible to ANY or ANYELEMENT or ANYCOMPATIBLE */
3059 if (targettype == ANYOID || targettype == ANYELEMENTOID ||
3060 targettype == ANYCOMPATIBLEOID)
3061 return true;
3062
3063 /* If srctype is a domain, reduce to its base type */
3064 if (OidIsValid(srctype))
3065 srctype = getBaseType(srctype);
3066
3067 /* Somewhat-fast path for domain -> base type case */
3068 if (srctype == targettype)
3069 return true;
3070
3071 /* Also accept any array type as coercible to ANY[COMPATIBLE]ARRAY */
3072 if (targettype == ANYARRAYOID || targettype == ANYCOMPATIBLEARRAYOID)
3073 if (type_is_array(srctype))
3074 return true;
3075
3076 /* Also accept any non-array type as coercible to ANY[COMPATIBLE]NONARRAY */
3077 if (targettype == ANYNONARRAYOID || targettype == ANYCOMPATIBLENONARRAYOID)
3078 if (!type_is_array(srctype))
3079 return true;
3080
3081 /* Also accept any enum type as coercible to ANYENUM */
3082 if (targettype == ANYENUMOID)
3083 if (type_is_enum(srctype))
3084 return true;
3085
3086 /* Also accept any range type as coercible to ANY[COMPATIBLE]RANGE */
3087 if (targettype == ANYRANGEOID || targettype == ANYCOMPATIBLERANGEOID)
3088 if (type_is_range(srctype))
3089 return true;
3090
3091 /* Also, any multirange type is coercible to ANY[COMPATIBLE]MULTIRANGE */
3092 if (targettype == ANYMULTIRANGEOID || targettype == ANYCOMPATIBLEMULTIRANGEOID)
3093 if (type_is_multirange(srctype))
3094 return true;
3095
3096 /* Also accept any composite type as coercible to RECORD */
3097 if (targettype == RECORDOID)
3098 if (ISCOMPLEX(srctype))
3099 return true;
3100
3101 /* Also accept any composite array type as coercible to RECORD[] */
3102 if (targettype == RECORDARRAYOID)
3103 if (is_complex_array(srctype))
3104 return true;
3105
3106 /* Else look in pg_cast */
3107 tuple = SearchSysCache2(CASTSOURCETARGET,
3108 ObjectIdGetDatum(srctype),
3109 ObjectIdGetDatum(targettype));
3110 if (!HeapTupleIsValid(tuple))
3111 return false; /* no cast */
3112 castForm = (Form_pg_cast) GETSTRUCT(tuple);
3113
3114 result = (castForm->castmethod == COERCION_METHOD_BINARY &&
3115 castForm->castcontext == COERCION_CODE_IMPLICIT);
3116
3117 if (result)
3118 *castoid = castForm->oid;
3119
3120 ReleaseSysCache(tuple);
3121
3122 return result;
3123}
bool type_is_range(Oid typid)
Definition: lsyscache.c:2715
bool type_is_multirange(Oid typid)
Definition: lsyscache.c:2725
#define type_is_array(typid)
Definition: lsyscache.h:210

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 2995 of file parse_coerce.c.

2996{
2997 char typcategory;
2998 bool typispreferred;
2999
3000 get_type_category_preferred(type, &typcategory, &typispreferred);
3001 if (category == typcategory || category == TYPCATEGORY_INVALID)
3002 return typispreferred;
3003 else
3004 return false;
3005}
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2737
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 1312 of file parse_coerce.c.

1315{
1316 if (coerce_location >= 0)
1317 return parser_errposition(pstate, coerce_location);
1318 else
1319 return parser_errposition(pstate, exprLocation(input_expr));
1320}

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 1342 of file parse_coerce.c.

1344{
1345 Node *pexpr;
1346 Oid ptype;
1347 TYPCATEGORY pcategory;
1348 bool pispreferred;
1349 ListCell *lc;
1350
1351 Assert(exprs != NIL);
1352 pexpr = (Node *) linitial(exprs);
1353 lc = list_second_cell(exprs);
1354 ptype = exprType(pexpr);
1355
1356 /*
1357 * If all input types are valid and exactly the same, just pick that type.
1358 * This is the only way that we will resolve the result as being a domain
1359 * type; otherwise domains are smashed to their base types for comparison.
1360 */
1361 if (ptype != UNKNOWNOID)
1362 {
1363 for_each_cell(lc, exprs, lc)
1364 {
1365 Node *nexpr = (Node *) lfirst(lc);
1366 Oid ntype = exprType(nexpr);
1367
1368 if (ntype != ptype)
1369 break;
1370 }
1371 if (lc == NULL) /* got to the end of the list? */
1372 {
1373 if (which_expr)
1374 *which_expr = pexpr;
1375 return ptype;
1376 }
1377 }
1378
1379 /*
1380 * Nope, so set up for the full algorithm. Note that at this point, lc
1381 * points to the first list item with type different from pexpr's; we need
1382 * not re-examine any items the previous loop advanced over.
1383 */
1384 ptype = getBaseType(ptype);
1385 get_type_category_preferred(ptype, &pcategory, &pispreferred);
1386
1387 for_each_cell(lc, exprs, lc)
1388 {
1389 Node *nexpr = (Node *) lfirst(lc);
1390 Oid ntype = getBaseType(exprType(nexpr));
1391
1392 /* move on to next one if no new information... */
1393 if (ntype != UNKNOWNOID && ntype != ptype)
1394 {
1395 TYPCATEGORY ncategory;
1396 bool nispreferred;
1397
1398 get_type_category_preferred(ntype, &ncategory, &nispreferred);
1399 if (ptype == UNKNOWNOID)
1400 {
1401 /* so far, only unknowns so take anything... */
1402 pexpr = nexpr;
1403 ptype = ntype;
1404 pcategory = ncategory;
1405 pispreferred = nispreferred;
1406 }
1407 else if (ncategory != pcategory)
1408 {
1409 /*
1410 * both types in different categories? then not much hope...
1411 */
1412 if (context == NULL)
1413 return InvalidOid;
1414 ereport(ERROR,
1415 (errcode(ERRCODE_DATATYPE_MISMATCH),
1416 /*------
1417 translator: first %s is name of a SQL construct, eg CASE */
1418 errmsg("%s types %s and %s cannot be matched",
1419 context,
1420 format_type_be(ptype),
1421 format_type_be(ntype)),
1422 parser_errposition(pstate, exprLocation(nexpr))));
1423 }
1424 else if (!pispreferred &&
1425 can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
1426 !can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
1427 {
1428 /*
1429 * take new type if can coerce to it implicitly but not the
1430 * other way; but if we have a preferred type, stay on it.
1431 */
1432 pexpr = nexpr;
1433 ptype = ntype;
1434 pcategory = ncategory;
1435 pispreferred = nispreferred;
1436 }
1437 }
1438 }
1439
1440 /*
1441 * If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
1442 * then resolve as type TEXT. This situation comes up with constructs
1443 * like SELECT (CASE WHEN foo THEN 'bar' ELSE 'baz' END); SELECT 'foo'
1444 * UNION SELECT 'bar'; It might seem desirable to leave the construct's
1445 * output type as UNKNOWN, but that really doesn't work, because we'd
1446 * probably end up needing a runtime coercion from UNKNOWN to something
1447 * else, and we usually won't have it. We need to coerce the unknown
1448 * literals while they are still literals, so a decision has to be made
1449 * now.
1450 */
1451 if (ptype == UNKNOWNOID)
1452 ptype = TEXTOID;
1453
1454 if (which_expr)
1455 *which_expr = pexpr;
1456 return ptype;
1457}
char TYPCATEGORY
Definition: parse_coerce.h:21
#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, 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_type_from_oids()

static Oid select_common_type_from_oids ( int  nargs,
const Oid typeids,
bool  noerror 
)
static

Definition at line 1478 of file parse_coerce.c.

1479{
1480 Oid ptype;
1481 TYPCATEGORY pcategory;
1482 bool pispreferred;
1483 int i = 1;
1484
1485 Assert(nargs > 0);
1486 ptype = typeids[0];
1487
1488 /* If all input types are valid and exactly the same, pick that type. */
1489 if (ptype != UNKNOWNOID)
1490 {
1491 for (; i < nargs; i++)
1492 {
1493 if (typeids[i] != ptype)
1494 break;
1495 }
1496 if (i == nargs)
1497 return ptype;
1498 }
1499
1500 /*
1501 * Nope, so set up for the full algorithm. Note that at this point, we
1502 * can skip array entries before "i"; they are all equal to ptype.
1503 */
1504 ptype = getBaseType(ptype);
1505 get_type_category_preferred(ptype, &pcategory, &pispreferred);
1506
1507 for (; i < nargs; i++)
1508 {
1509 Oid ntype = getBaseType(typeids[i]);
1510
1511 /* move on to next one if no new information... */
1512 if (ntype != UNKNOWNOID && ntype != ptype)
1513 {
1514 TYPCATEGORY ncategory;
1515 bool nispreferred;
1516
1517 get_type_category_preferred(ntype, &ncategory, &nispreferred);
1518 if (ptype == UNKNOWNOID)
1519 {
1520 /* so far, only unknowns so take anything... */
1521 ptype = ntype;
1522 pcategory = ncategory;
1523 pispreferred = nispreferred;
1524 }
1525 else if (ncategory != pcategory)
1526 {
1527 /*
1528 * both types in different categories? then not much hope...
1529 */
1530 if (noerror)
1531 return InvalidOid;
1532 ereport(ERROR,
1533 (errcode(ERRCODE_DATATYPE_MISMATCH),
1534 errmsg("argument types %s and %s cannot be matched",
1535 format_type_be(ptype),
1536 format_type_be(ntype))));
1537 }
1538 else if (!pispreferred &&
1539 can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
1540 !can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
1541 {
1542 /*
1543 * take new type if can coerce to it implicitly but not the
1544 * other way; but if we have a preferred type, stay on it.
1545 */
1546 ptype = ntype;
1547 pcategory = ncategory;
1548 pispreferred = nispreferred;
1549 }
1550 }
1551 }
1552
1553 /* Like select_common_type(), choose TEXT if all inputs were UNKNOWN */
1554 if (ptype == UNKNOWNOID)
1555 ptype = TEXTOID;
1556
1557 return ptype;
1558}

References Assert, can_coerce_type(), COERCION_IMPLICIT, ereport, errcode(), errmsg(), ERROR, format_type_be(), get_type_category_preferred(), getBaseType(), i, and InvalidOid.

Referenced by check_generic_type_consistency(), and enforce_generic_type_consistency().

◆ select_common_typmod()

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

Definition at line 1644 of file parse_coerce.c.

1645{
1646 ListCell *lc;
1647 bool first = true;
1648 int32 result = -1;
1649
1650 foreach(lc, exprs)
1651 {
1652 Node *expr = (Node *) lfirst(lc);
1653
1654 /* Types must match */
1655 if (exprType(expr) != common_type)
1656 return -1;
1657 else if (first)
1658 {
1659 result = exprTypmod(expr);
1660 first = false;
1661 }
1662 else
1663 {
1664 /* As soon as we see a non-matching typmod, fall back to -1 */
1665 if (result != exprTypmod(expr))
1666 return -1;
1667 }
1668 }
1669
1670 return result;
1671}

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

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

◆ TypeCategory()

TYPCATEGORY TypeCategory ( Oid  type)

Definition at line 2976 of file parse_coerce.c.

2977{
2978 char typcategory;
2979 bool typispreferred;
2980
2981 get_type_category_preferred(type, &typcategory, &typispreferred);
2982 Assert(typcategory != TYPCATEGORY_INVALID);
2983 return (TYPCATEGORY) typcategory;
2984}

References Assert, get_type_category_preferred(), and type.

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

◆ typeIsOfTypedTable()

static bool typeIsOfTypedTable ( Oid  reltypeId,
Oid  reloftypeId 
)
static

Definition at line 3380 of file parse_coerce.c.

3381{
3382 Oid relid = typeOrDomainTypeRelid(reltypeId);
3383 bool result = false;
3384
3385 if (relid)
3386 {
3387 HeapTuple tp;
3388 Form_pg_class reltup;
3389
3390 tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
3391 if (!HeapTupleIsValid(tp))
3392 elog(ERROR, "cache lookup failed for relation %u", relid);
3393
3394 reltup = (Form_pg_class) GETSTRUCT(tp);
3395 if (reltup->reloftype == reloftypeId)
3396 result = true;
3397
3398 ReleaseSysCache(tp);
3399 }
3400
3401 return result;
3402}
Oid typeOrDomainTypeRelid(Oid type_id)
Definition: parse_type.c:689
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153

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

Referenced by can_coerce_type(), and coerce_type().

◆ verify_common_type()

bool verify_common_type ( Oid  common_type,
List exprs 
)

Definition at line 1606 of file parse_coerce.c.

1607{
1608 ListCell *lc;
1609
1610 foreach(lc, exprs)
1611 {
1612 Node *nexpr = (Node *) lfirst(lc);
1613 Oid ntype = exprType(nexpr);
1614
1615 if (!can_coerce_type(1, &ntype, &common_type, COERCION_IMPLICIT))
1616 return false;
1617 }
1618 return true;
1619}

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

Referenced by transformAExprIn().

◆ verify_common_type_from_oids()

static bool verify_common_type_from_oids ( Oid  common_type,
int  nargs,
const Oid typeids 
)
static

Definition at line 1626 of file parse_coerce.c.

1627{
1628 for (int i = 0; i < nargs; i++)
1629 {
1630 if (!can_coerce_type(1, &typeids[i], &common_type, COERCION_IMPLICIT))
1631 return false;
1632 }
1633 return true;
1634}

References can_coerce_type(), COERCION_IMPLICIT, and i.

Referenced by check_generic_type_consistency(), and enforce_generic_type_consistency().